Merge branch 'master' into micropython-25ae98f-merge

This commit is contained in:
Dan Halbert 2018-07-16 19:36:40 -04:00
commit 2809b4f9dd
71 changed files with 23480 additions and 10934 deletions

1
.gitattributes vendored
View File

@ -12,6 +12,7 @@
*.dxf binary
*.mpy binary
*.deb binary
*.zip binary
# These should also not be modified by git.
tests/basics/string_cr_conversion.py -text

6
.gitignore vendored
View File

@ -7,7 +7,7 @@
*.bin
*.map
*.hex
!ports/nrf/**/bootloader/*.hex
!ports/nrf/**/bootloader/**/*.hex
*.dis
*.exe
@ -58,4 +58,8 @@ TAGS
#################
*.orig
# Emacs backup files
####################
*~
*.DS_Store

13
.gitmodules vendored
View File

@ -37,9 +37,9 @@
url = https://github.com/micropython/stm32lib
branch = work-F4-1.13.1+F7-1.5.0+L4-1.3.0
[submodule "atmel-samd/asf4"]
path = ports/atmel-samd/asf4
url = https://github.com/adafruit/asf4.git
branch = circuitpython
path = ports/atmel-samd/asf4
url = https://github.com/adafruit/asf4.git
branch = circuitpython
[submodule "tools/usb_descriptor"]
path = tools/usb_descriptor
url = https://github.com/adafruit/usb_descriptor.git
@ -70,6 +70,13 @@
[submodule "ports/atmel-samd/peripherals"]
path = ports/atmel-samd/peripherals
url = https://github.com/adafruit/samd-peripherals.git
[submodule "frozen/Adafruit_CircuitPython_Crickit"]
path = frozen/Adafruit_CircuitPython_Crickit
url = https://github.com/adafruit/Adafruit_CircuitPython_Crickit
[submodule "ports/nrf/nrfx"]
path = ports/nrf/nrfx
url = https://github.com/NordicSemiconductor/nrfx.git
[submodule "lib/tinyusb"]
path = lib/tinyusb
url = https://github.com/hathach/tinyusb.git
branch = develop

View File

@ -117,6 +117,7 @@ exclude_patterns = ["**/build*",
"ports/nrf/hal",
"ports/nrf/modules",
"ports/nrf/nrfx",
"ports/nrf/usb",
"ports/pic16bit",
"ports/qemu-arm",
"ports/stm32",

View File

@ -191,6 +191,7 @@ These provide functionality similar to `analogio`, `digitalio`, `pulseio`, and `
Adafruit SeeSaw <https://circuitpython.readthedocs.io/projects/seesaw/en/latest/>
ADS1x15 Analog-to-Digital Converter <https://circuitpython.readthedocs.io/projects/ads1x15/en/latest/>
Crickit Robotics Boards <<https://circuitpython.readthedocs.io/projects/crickit/en/latest/>
DS2413 OneWire GPIO Expander <https://circuitpython.readthedocs.io/projects/ds2413/en/latest/>
FocalTech Capacitive Touch <https://circuitpython.readthedocs.io/projects/focaltouch/en/latest/>
MCP230xx GPIO Expander <https://circuitpython.readthedocs.io/projects/mcp230xx/en/latest/>
@ -200,7 +201,6 @@ These provide functionality similar to `analogio`, `digitalio`, `pulseio`, and `
TLC59711 12 x 16-bit PWM Driver <https://circuitpython.readthedocs.io/projects/tlc59711/en/latest/>
MPR121 Capacitive Touch Sensor <https://circuitpython.readthedocs.io/projects/mpr121/en/latest/>
Miscellaneous
----------------

@ -1 +1 @@
Subproject commit 376a80e62acb3ba8b9a1e5c11bb75d36352f2190
Subproject commit 07919641470edb602585c6a91f7b8eacf17e664b

@ -1 +1 @@
Subproject commit d0022de9d5e9b4c3d4998a5d708e59ee147f0a21
Subproject commit d0aa6dc56d66decfae92daced7384c1e3518a666

@ -0,0 +1 @@
Subproject commit 44f52c5dacd9fc605565e5794e95c9a785aaf693

@ -1 +1 @@
Subproject commit 1da880cee3c2d64e2e7e4ca5082f3a74c4a37a5a
Subproject commit 5c2f6ef1ed80f24b6a3878067d40350d3725e198

@ -1 +1 @@
Subproject commit 6da5d14b98ac3d929662731f73ce03c0c958b520
Subproject commit e0b709f1710555da67705360870ba0d14ced7e06

@ -1 +1 @@
Subproject commit 498f59bf926477b3a8fb8eb157ca05eb12c3e298
Subproject commit 340cd17fad0c29d3a70d6e298a30ecc753df054e

@ -1 +1 @@
Subproject commit d48561b07386132b849d0125b0ff6a532d833d8b
Subproject commit 9e7dfb28a5c6f3d7a19340971b32e0c2b4128ecf

1
lib/tinyusb Submodule

@ -0,0 +1 @@
Subproject commit a0849fee9f4b268ccc9798c70c654a835f8e20c8

View File

@ -14,7 +14,13 @@ endif
include ../py/mkenv.mk
# define main target
ifeq ($(OS),Windows_NT)
# Detect a MINGW32 build, and change the name of the final executable.
PROG = mpy-cross.exe
else
PROG = mpy-cross
endif
# qstr definitions (must come before including py.mk)
QSTR_DEFS = qstrdefsport.h
@ -35,6 +41,13 @@ CWARN += -Wpointer-arith -Wuninitialized
CFLAGS = $(INC) $(CWARN) -std=gnu99 $(CFLAGS_MOD) $(COPT) $(CFLAGS_EXTRA)
CFLAGS += -fdata-sections -ffunction-sections -fno-asynchronous-unwind-tables
# Build a static executable.
# Useful for Windows builds, etc., that must run on multiple operating system versions.
ifdef STATIC_BUILD
CFLAGS += -static -static-libgcc -static-libstdc++
endif
# Debugging/Optimization
ifdef DEBUG
CFLAGS += -g
@ -57,6 +70,10 @@ LDFLAGS_ARCH = -Wl,-Map=$@.map,--cref -Wl,--gc-sections
endif
LDFLAGS = $(LDFLAGS_MOD) $(LDFLAGS_ARCH) -lm $(LDFLAGS_EXTRA)
ifdef STATIC_BUILD
LDFLAGS += -static -static-libgcc -static-libstdc++
endif
# source files
SRC_C = \
main.c \

View File

@ -85,6 +85,50 @@
#define MICROPY_PY_IO (0)
#define MICROPY_PY_SYS (0)
// MINGW only handles these errno names.
#ifdef __MINGW32__
#define MICROPY_PY_UERRNO_LIST \
X(EPERM) \
X(ENOENT) \
X(ESRCH) \
X(EINTR) \
X(EIO) \
X(ENXIO) \
X(E2BIG) \
X(ENOEXEC) \
X(EBADF) \
X(ECHILD) \
X(EAGAIN) \
X(ENOMEM) \
X(EACCES) \
X(EFAULT) \
X(EBUSY) \
X(EEXIST) \
X(EXDEV) \
X(ENODEV) \
X(ENOTDIR) \
X(EISDIR) \
X(EINVAL) \
X(ENFILE) \
X(EMFILE) \
X(ENOTTY) \
X(EFBIG) \
X(ENOSPC) \
X(ESPIPE) \
X(EROFS) \
X(EMLINK) \
X(EPIPE) \
X(EDOM) \
X(ERANGE) \
X(EDEADLOCK) \
X(EDEADLK) \
X(ENAMETOOLONG) \
X(ENOLCK) \
X(ENOSYS) \
X(ENOTEMPTY) \
X(EILSEQ)
#endif
// type definitions for the specific machine
#ifdef __LP64__

View File

@ -119,6 +119,9 @@ else
ifdef INTERNAL_FLASH_FILESYSTEM
CFLAGS += -finline-limit=55
endif
ifdef CFLAGS_INLINE_LIMIT
CFLAGS += -finline-limit=$(CFLAGS_INLINE_LIMIT)
endif
CFLAGS += -flto
endif

View File

@ -11,9 +11,12 @@ LONGINT_IMPL = NONE
CHIP_VARIANT = SAMD21G18A
CHIP_FAMILY = samd21
CFLAGS_INLINE_LIMIT = 55
# Include these Python libraries in firmware.
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_CircuitPlayground
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_Crickit
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_LIS3DH
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_Motor
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_NeoPixel

View File

@ -10,8 +10,11 @@ LONGINT_IMPL = MPZ
CHIP_VARIANT = SAMD21G18A
CHIP_FAMILY = samd21
CFLAGS_INLINE_LIMIT = 55
# Include these Python libraries in firmware.
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_Crickit
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_Motor
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_NeoPixel
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_seesaw

View File

@ -40,8 +40,9 @@ digitalinout_result_t common_hal_digitalio_digitalinout_construct(
claim_pin(pin);
self->pin = pin;
gpio_set_pin_pull_mode(pin->pin, GPIO_PULL_OFF);
// Must set pull after setting direction.
gpio_set_pin_direction(pin->pin, GPIO_DIRECTION_IN);
gpio_set_pin_pull_mode(pin->pin, GPIO_PULL_OFF);
return DIGITALINOUT_OK;
}
@ -60,7 +61,7 @@ void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t* self
void common_hal_digitalio_digitalinout_switch_to_input(
digitalio_digitalinout_obj_t* self, digitalio_pull_t pull) {
self->output = false;
// This also sets direction to input.
common_hal_digitalio_digitalinout_set_pull(self, pull);
}
@ -69,13 +70,13 @@ void common_hal_digitalio_digitalinout_switch_to_output(
digitalio_drive_mode_t drive_mode) {
const uint8_t pin = self->pin->pin;
gpio_set_pin_pull_mode(pin, GPIO_PULL_OFF);
gpio_set_pin_direction(pin, GPIO_DIRECTION_OUT);
// Turn on "strong" pin driving (more current available). See DRVSTR doc in datasheet.
hri_port_set_PINCFG_DRVSTR_bit(PORT, (enum gpio_port)GPIO_PORT(pin), GPIO_PIN(pin));
self->output = true;
self->open_drain = drive_mode == DRIVE_MODE_OPEN_DRAIN;
// Direction is set in set_value. We don't need to do it here.
common_hal_digitalio_digitalinout_set_value(self, value);
}
@ -86,16 +87,22 @@ digitalio_direction_t common_hal_digitalio_digitalinout_get_direction(
void common_hal_digitalio_digitalinout_set_value(
digitalio_digitalinout_obj_t* self, bool value) {
const uint8_t pin = self->pin->pin;
const uint8_t port = GPIO_PORT(pin);
const uint32_t pin_mask = 1U << GPIO_PIN(pin);
if (value) {
if (self->open_drain) {
gpio_set_pin_direction(self->pin->pin, GPIO_DIRECTION_IN);
// Assertion: pull is off, so the pin is floating in this case.
// We do open-drain high output (no sinking of current)
// by changing the direction to input with no pulls.
hri_port_clear_DIR_DIR_bf(PORT, port, pin_mask);
} else {
gpio_set_pin_level(self->pin->pin, true);
gpio_set_pin_direction(self->pin->pin, GPIO_DIRECTION_OUT);
hri_port_set_DIR_DIR_bf(PORT, port, pin_mask);
hri_port_set_OUT_OUT_bf(PORT, port, pin_mask);
}
} else {
gpio_set_pin_level(self->pin->pin, false);
gpio_set_pin_direction(self->pin->pin, GPIO_DIRECTION_OUT);
hri_port_set_DIR_DIR_bf(PORT, port, pin_mask);
hri_port_clear_OUT_OUT_bf(PORT,port, pin_mask);
}
}
@ -105,10 +112,10 @@ bool common_hal_digitalio_digitalinout_get_value(
if (!self->output) {
return gpio_get_pin_level(pin);
} else {
if (self->open_drain && hri_port_get_DIR_reg(PORT, (enum gpio_port)GPIO_PORT(pin), 1U << GPIO_PIN(pin)) == 0) {
if (self->open_drain && hri_port_get_DIR_reg(PORT, GPIO_PORT(pin), 1U << GPIO_PIN(pin)) == 0) {
return true;
} else {
return hri_port_get_OUT_reg(PORT, (enum gpio_port)GPIO_PORT(pin), 1U << GPIO_PIN(pin));
return hri_port_get_OUT_reg(PORT, GPIO_PORT(pin), 1U << GPIO_PIN(pin));
}
}
}
@ -119,7 +126,7 @@ void common_hal_digitalio_digitalinout_set_drive_mode(
bool value = common_hal_digitalio_digitalinout_get_value(self);
self->open_drain = drive_mode == DRIVE_MODE_OPEN_DRAIN;
// True is implemented differently between modes so reset the value to make
// sure its correct for the new mode.
// sure it's correct for the new mode.
if (value) {
common_hal_digitalio_digitalinout_set_value(self, value);
}
@ -148,6 +155,8 @@ void common_hal_digitalio_digitalinout_set_pull(
default:
break;
}
// Must set pull after setting direction.
gpio_set_pin_direction(self->pin->pin, GPIO_DIRECTION_IN);
gpio_set_pin_pull_mode(self->pin->pin, asf_pull);
}
@ -158,9 +167,9 @@ digitalio_pull_t common_hal_digitalio_digitalinout_get_pull(
mp_raise_AttributeError("Cannot get pull while in output mode");
return PULL_NONE;
} else {
if (hri_port_get_PINCFG_PULLEN_bit(PORT, (enum gpio_port)GPIO_PORT(pin), GPIO_PIN(pin)) == 0) {
if (hri_port_get_PINCFG_PULLEN_bit(PORT, GPIO_PORT(pin), GPIO_PIN(pin)) == 0) {
return PULL_NONE;
} if (hri_port_get_OUT_reg(PORT, (enum gpio_port)GPIO_PORT(pin), 1U << GPIO_PIN(pin)) > 0) {
} if (hri_port_get_OUT_reg(PORT, GPIO_PORT(pin), 1U << GPIO_PIN(pin)) > 0) {
return PULL_UP;
} else {
return PULL_DOWN;

View File

@ -1,3 +1,29 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2016 Scott Shawcroft
*
* 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 <string.h>
#include "lib/mp-readline/readline.h"
@ -14,6 +40,7 @@
#include "hal/include/hal_delay.h"
#include "hal/include/hal_gpio.h"
#include "hal/include/hal_sleep.h"
#include "sam.h"
#include "mpconfigboard.h"
#include "mphalport.h"
@ -22,6 +49,7 @@
#include "usb.h"
extern struct usart_module usart_instance;
extern uint32_t common_hal_mcu_processor_get_frequency(void);
int mp_hal_stdin_rx_chr(void) {
for (;;) {
@ -71,8 +99,23 @@ void mp_hal_delay_ms(mp_uint_t delay) {
}
}
// Use mp_hal_delay_us() for timing of less than 1ms.
// Do a simple timing loop to wait for a certain number of microseconds.
// Can be used when interrupts are disabled, which makes tick_delay() unreliable.
//
// Testing done at 48 MHz on SAMD21 and 120 MHz on SAMD51, multiplication and division cancel out.
// But get the frequency just in case.
#ifdef SAMD21
#define DELAY_LOOP_ITERATIONS_PER_US ( (10U*48000000U) / common_hal_mcu_processor_get_frequency())
#endif
#ifdef SAMD51
#define DELAY_LOOP_ITERATIONS_PER_US ( (30U*120000000U) / common_hal_mcu_processor_get_frequency())
#endif
void mp_hal_delay_us(mp_uint_t delay) {
tick_delay(delay);
for (uint32_t i = delay*DELAY_LOOP_ITERATIONS_PER_US; i > 0; i--) {
asm volatile("nop");
}
}
void mp_hal_disable_all_interrupts(void) {

View File

@ -48,7 +48,6 @@ int receive_usb(void);
void mp_hal_set_interrupt_char(int c);
void mp_hal_disable_all_interrupts(void);
void mp_hal_enable_all_interrupts(void);
#endif // MICROPY_INCLUDED_ATMEL_SAMD_MPHALPORT_H

View File

@ -64,8 +64,8 @@ void tick_init() {
for (uint16_t i = 0; i < PERIPH_COUNT_IRQn; i++) {
NVIC_SetPriority(i, (1UL << __NVIC_PRIO_BITS) - 1UL);
}
// Bump up the systick interrupt.
NVIC_SetPriority(SysTick_IRQn, 1);
// Bump up the systick interrupt so nothing else interferes with timekeeping.
NVIC_SetPriority(SysTick_IRQn, 0);
#ifdef SAMD21
NVIC_SetPriority(USB_IRQn, 1);
#endif

View File

@ -1,22 +1,23 @@
# Select the board to build for: if not given on the command line,
# then default to feather52832.
BOARD ?= feather52832
ifeq ($(wildcard boards/$(BOARD)/.),)
$(error Invalid BOARD specified)
# Select the board to build for.
ifeq ($(BOARD),)
$(info You must provide a BOARD parameter with 'BOARD=')
$(info Possible values are:)
$(info $(sort $(subst /.,,$(subst boards/,,$(wildcard boards/*/.)))))
$(error BOARD not defined)
else
ifeq ($(wildcard boards/$(BOARD)/.),)
$(error Invalid BOARD specified)
endif
endif
# If SoftDevice is selected, try to use that one.
# Default to SD132 (exact version can be set with SOFTDEV_VERSION)
SD ?= s132
include boards/$(BOARD)/mpconfigboard.mk
SD_LOWER = $(shell echo $(SD) | tr '[:upper:]' '[:lower:]')
# TODO: Verify that it is a valid target.
# If the build directory is not given, make it reflect the board name
# Build directory with SD
BUILD ?= $(if $(SD),build-$(BOARD)-$(SD_LOWER),build-$(BOARD))
include ../../py/mkenv.mk
include boards/$(BOARD)/mpconfigboard.mk
-include mpconfigport.mk
ifneq ($(SD), )
@ -52,6 +53,8 @@ INC += -I./nrfx/drivers/include
INC += -I../../lib/mp-readline
INC += -I./drivers/bluetooth
INC += -I./drivers
INC += -I../../lib/tinyusb/src
INC += -I./usb
NRF_DEFINES += -DCONFIG_GPIO_AS_PINRESET
@ -87,6 +90,7 @@ LIBS += -L $(dir $(LIBC_FILE_NAME)) -lc
LIBS += -L $(dir $(LIBGCC_FILE_NAME)) -lgcc
SRC_NRFX = $(addprefix nrfx/,\
drivers/src/nrfx_power.c \
drivers/src/nrfx_spim.c \
drivers/src/nrfx_twim.c \
drivers/src/nrfx_uart.c \
@ -96,10 +100,13 @@ SRC_C += \
mphalport.c \
fatfs_port.c \
tick.c \
background.c \
internal_flash.c \
drivers/bluetooth/ble_drv.c \
drivers/bluetooth/ble_uart.c \
boards/$(BOARD)/board.c \
nrfx/mdk/system_$(MCU_SUB_VARIANT).c \
nrfx/hal/nrf_nvmc.c \
device/$(MCU_VARIANT)/startup_$(MCU_SUB_VARIANT).c \
lib/oofatfs/ff.c \
lib/oofatfs/option/ccsbcs.c \
@ -108,10 +115,25 @@ SRC_C += \
lib/utils/context_manager_helpers.c \
lib/utils/interrupt_char.c \
lib/utils/pyexec.c \
lib/utils/stdout_helpers.c \
lib/libc/string0.c \
lib/mp-readline/readline.c \
internal_flash.c \
ifeq ($(MCU_SUB_VARIANT),nrf52840)
SRC_C += \
usb/tusb_descriptors.c \
usb/usb_msc_flash.c \
lib/tinyusb/src/portable/nordic/nrf5x/dcd_nrf5x.c \
lib/tinyusb/src/portable/nordic/nrf5x/hal_nrf5x.c \
lib/tinyusb/src/common/tusb_fifo.c \
lib/tinyusb/src/device/usbd.c \
lib/tinyusb/src/device/usbd_desc.c \
lib/tinyusb/src/class/msc/msc_device.c \
lib/tinyusb/src/class/cdc/cdc_device.c \
lib/tinyusb/src/tusb.c \
endif
DRIVERS_SRC_C += $(addprefix modules/,\
ubluepy/modubluepy.c \
@ -124,7 +146,6 @@ DRIVERS_SRC_C += $(addprefix modules/,\
ubluepy/ubluepy_descriptor.c \
ubluepy/ubluepy_scanner.c \
ubluepy/ubluepy_scan_entry.c \
ble/modble.c \
)
SRC_COMMON_HAL += \
@ -151,6 +172,12 @@ SRC_COMMON_HAL += \
supervisor/__init__.c \
supervisor/Runtime.c \
ifneq ($(SD), )
SRC_COMMON_HAL += \
bleio/__init__.c \
bleio/Adapter.c
endif
# These don't have corresponding files in each port but are still located in
# shared-bindings to make it clear what the contents of the modules are.
SRC_BINDINGS_ENUMS = \
@ -213,9 +240,9 @@ OBJ += $(addprefix $(BUILD)/, $(SRC_S:.s=.o))
$(BUILD)/$(FATFS_DIR)/ff.o: COPT += -Os
$(filter $(PY_BUILD)/../extmod/vfs_fat_%.o, $(PY_O)): COPT += -Os
.phony: all flash sd binary hex
.phony: all flash sd binary hex bootloader
all: binary hex
all: binary hex uf2
OUTPUT_FILENAME = firmware
@ -231,12 +258,18 @@ hex: $(BUILD)/$(OUTPUT_FILENAME).hex
$(BUILD)/$(OUTPUT_FILENAME).hex: $(BUILD)/$(OUTPUT_FILENAME).elf
$(OBJCOPY) -O ihex $< $@
#####################
# Flash with debugger
#####################
FLASHER ?=
ifeq ($(FLASHER),)
# Also update to bootloader settting to validate application and skip checksum ( app valid = 0x0001, crc = 0x0000 )
flash: $(BUILD)/$(OUTPUT_FILENAME).hex
nrfjprog --program $< --sectorerase -f $(MCU_VARIANT)
nrfjprog --erasepage $(BOOT_SETTING_ADDR) -f $(MCU_VARIANT)
nrfjprog --memwr $(BOOT_SETTING_ADDR) --val 0x00000001 -f $(MCU_VARIANT)
nrfjprog --reset -f $(MCU_VARIANT)
sd: $(BUILD)/$(OUTPUT_FILENAME).hex
@ -245,18 +278,64 @@ sd: $(BUILD)/$(OUTPUT_FILENAME).hex
nrfjprog --program $< --sectorerase -f $(MCU_VARIANT)
nrfjprog --reset -f $(MCU_VARIANT)
bootloader:
nrfjprog --program $(BOOT_FILE).hex -f nrf52 --chiperase --reset
else ifeq ($(FLASHER), pyocd)
flash: $(BUILD)/$(OUTPUT_FILENAME).hex
pyocd-flashtool -t $(MCU_VARIANT) $<
pyocd-flashtool -t $(MCU_SUB_VARIANT) $< --sector_erase
pyocd-tool -t $(MCU_SUB_VARIANT) erase $(BOOT_SETTING_ADDR)
pyocd-tool -t $(MCU_SUB_VARIANT) write32 $(BOOT_SETTING_ADDR) 0x00000001
pyocd-tool -t $(MCU_SUB_VARIANT) reset
sd: $(BUILD)/$(OUTPUT_FILENAME).hex
pyocd-flashtool -t $(MCU_VARIANT) --chip_erase
pyocd-flashtool -t $(MCU_VARIANT) $(SOFTDEV_HEX)
pyocd-flashtool -t $(MCU_VARIANT) $<
pyocd-flashtool -t $(MCU_SUB_VARIANT) --chip_erase
pyocd-flashtool -t $(MCU_SUB_VARIANT) $(SOFTDEV_HEX)
pyocd-flashtool -t $(MCU_SUB_VARIANT) $< --sector_erase
pyocd-tool -t $(MCU_SUB_VARIANT) reset $(BOOT_SETTING_ADDR)
bootloader:
pyocd-flashtool -t $(MCU_SUB_VARIANT) $(BOOT_FILE).hex --chip_erase
pyocd-tool -t $(MCU_SUB_VARIANT) reset
endif
#####################
# Flash with DFU
#####################
.phony: dfu-gen dfu-flash dfu-bootloader
ifeq ($(OS),Windows_NT)
NRFUTIL = ../../lib/nrfutil/binaries/win32/nrfutil.exe
else
NRFUTIL = nrfutil
endif
check_defined = \
$(strip $(foreach 1,$1, \
$(call __check_defined,$1,$(strip $(value 2)))))
__check_defined = \
$(if $(value $1),, \
$(error Undefined make flag: $1$(if $2, ($2))))
dfu-gen: $(BUILD)/$(OUTPUT_FILENAME).hex
$(NRFUTIL) dfu genpkg --sd-req 0xFFFE --dev-type 0x0052 --application $^ $(BUILD)/dfu-package.zip
dfu-flash: $(BUILD)/dfu-package.zip
@:$(call check_defined, SERIAL, example: SERIAL=/dev/ttyUSB0)
$(NRFUTIL) --verbose dfu serial --package $^ -p $(SERIAL) -b 115200 --singlebank
dfu-bootloader:
@:$(call check_defined, SERIAL, example: SERIAL=/dev/ttyACM0)
$(NRFUTIL) --verbose dfu serial --package $(BOOT_FILE).zip -p $(SERIAL) -b 115200
uf2: $(BUILD)/$(OUTPUT_FILENAME).hex
$(ECHO) "Create $(OUTPUT_FILENAME).uf2"
$(PYTHON2) $(TOP)/tools/uf2/utils/uf2conv.py -f 0xADA52840 -c -o "$(BUILD)/$(OUTPUT_FILENAME).uf2" $^
$(BUILD)/dfu-package.zip: dfu-gen
$(BUILD)/$(OUTPUT_FILENAME).elf: $(OBJ)
$(ECHO) "LINK $@"
$(Q)$(CC) $(LDFLAGS) -o $@ $(OBJ) -Wl,--start-group $(LIBS) -Wl,--end-group

View File

@ -28,6 +28,20 @@ This is a port of CircuitPython to the Nordic Semiconductor nRF52 series of chip
* nRF52840
* [PCA10056](http://www.nordicsemi.com/eng/Products/nRF52840-Preview-DK)
## Board Specific Instructions
For board-specific instructions on building and flashing CircuitPython, see
the following links:
> **NOTE**: These board specific readmes may be more up to date than the
generic board-neutral documentation further down.
* Adafruit [Feather nRF52](boards/feather52/README.md): 512KB Flash, 64KB SRAM
* Adafruit [Feather nRF52840](boards/feather52840/README.md): 1MB Flash, 256KB SRAM
* Nordic PCA10056 see [Feather nRF52840](boards/feather52840/README.md)
For all other board targets, see the generic notes below.
## Compile and Flash
Prerequisite steps for building the nrf port:

34
ports/nrf/background.c Normal file
View File

@ -0,0 +1,34 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
*
* 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"
void run_background_tasks(void) {
#ifdef NRF52840_XXAA
tusb_task();
tud_cdc_flush();
#endif
}

View File

@ -1,11 +0,0 @@
#ifndef BLUETOOTH_CONF_H__
#define BLUETOOTH_CONF_H__
#define MICROPY_PY_BLE (1)
#define MICROPY_PY_BLE_NUS (0)
#define BLUETOOTH_WEBBLUETOOTH_REPL (0)
#define MICROPY_PY_UBLUEPY (1)
#define MICROPY_PY_UBLUEPY_PERIPHERAL (1)
#define MICROPY_PY_UBLUEPY_CENTRAL (1)
#endif

View File

@ -7,14 +7,13 @@
---------- ---------- ------- -----------------------------------------
0x000FF000..0x000FFFFF ( 4KB) Bootloader Settings
0x000FE000..0x000FEFFF ( 4KB) Master Boot Record Params
0x000F4000..0x000FDFFF ( 40KB) Serial + OTA Bootloader
0x000F4000..0x000FDFFF ( 40KB) Bootloader (UF2 + CDC + OTA)
0x000F3000..0x000F3FFF ( 4KB ) Private Config Data (Bonding, Keys, etc.)
0x000F2000..0x000F2FFF ( 4KB ) User NVM data
0x000B2000..0x000F1FFF (256KB) User Filesystem
0x000ED000..0x000F3FFF (28KB ) Private Config Data (Bonding, Keys, etc.)
0x000AD000..0x000ECFFF (256KB) User Filesystem
0x00025000..0x000B1FFF (564KB) Application Code (including ISR vector)
0x00001000..0x00024FFF (144KB) SoftDevice
0x00026000..0x000ACFFF (540KB) Application Code (including ISR vector)
0x00001000..0x00025FFF (148KB) SoftDevice
0x00000000..0x00000FFF (4KB) Master Boot Record
*/
@ -23,9 +22,9 @@ MEMORY
{
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x100000
FLASH_ISR (rx) : ORIGIN = 0x00025000, LENGTH = 0x001000
FLASH_TEXT (rx) : ORIGIN = 0x00026000, LENGTH = 0x08C000
FLASH_FATFS (r) : ORIGIN = 0x000B2000, LENGTH = 0x040000
FLASH_ISR (rx) : ORIGIN = 0x00026000, LENGTH = 0x001000
FLASH_TEXT (rx) : ORIGIN = 0x00027000, LENGTH = 0x086000
FLASH_FATFS (r) : ORIGIN = 0x000AD000, LENGTH = 0x040000
/* 0x2000000 - RAM:ORIGIN is reserved for Softdevice */
RAM (xrw) : ORIGIN = 0x20004000, LENGTH = 0x20040000 - 0x20004000
@ -42,6 +41,6 @@ _estack = ORIGIN(RAM) + LENGTH(RAM);
/* RAM extents for the garbage collector */
_ram_end = ORIGIN(RAM) + LENGTH(RAM);
_heap_end = 0x20007000; /* tunable */
_heap_end = 0x20020000; /* tunable */
INCLUDE "boards/common.ld"

View File

@ -1,37 +1,11 @@
MCU_SERIES = m4
MCU_VARIANT = nrf52
MCU_SUB_VARIANT = nrf52
SD ?= s132
SOFTDEV_VERSION ?= 2.0.1
LD_FILE = boards/feather52832/custom_nrf52832_dfu_app_$(SOFTDEV_VERSION).ld
BOOTLOADER_PKG = boards/feather52832/bootloader/feather52_bootloader_$(SOFTDEV_VERSION)_s132_single.zip
BOOT_FILE = boards/feather52832/bootloader/feather52_bootloader_$(SOFTDEV_VERSION)_s132_single
BOOT_SETTING_ADDR = 0x7F000
NRF_DEFINES += -DNRF52832_XXAA
ifeq ($(OS),Windows_NT)
NRFUTIL = ../../lib/nrfutil/binaries/win32/nrfutil.exe
else
NRFUTIL = nrfutil
endif
CFLAGS += -DADAFRUIT_FEATHER52
check_defined = \
$(strip $(foreach 1,$1, \
$(call __check_defined,$1,$(strip $(value 2)))))
__check_defined = \
$(if $(value $1),, \
$(error Undefined make flag: $1$(if $2, ($2))))
.PHONY: dfu-gen dfu-flash boot-flash
dfu-gen:
$(NRFUTIL) dfu genpkg --dev-type 0x0052 --application $(BUILD)/$(OUTPUT_FILENAME).hex $(BUILD)/dfu-package.zip
dfu-flash:
@:$(call check_defined, SERIAL, example: SERIAL=/dev/ttyUSB0)
$(NRFUTIL) dfu serial --package $(BUILD)/dfu-package.zip -p $(SERIAL) -b 115200
boot-flash:
@:$(call check_defined, SERIAL, example: SERIAL=/dev/ttyUSB0)
$(NRFUTIL) dfu serial --package $(BOOTLOADER_PKG) -p $(SERIAL) -b 115200

View File

@ -74,7 +74,7 @@ nrfjprog version: 9.7.2
JLinkARM.dll version: 6.20f
```
### Flash the Bootloader with `nrfjprog`
### Flash the USB CDC Bootloader with 'nrfjprog'
> This operation only needs to be done once, and only on boards that don't
already have the serial bootloader installed.
@ -83,14 +83,14 @@ Once `nrfjprog` is installed and available in `PATH` you can flash your
board with the serial bootloader via the following command:
```
make SD=s140 BOARD=feather52840 boot-flash
make SD=s140 BOARD=feather52840 bootloader
```
This should give you the following (or very similar) output, and you will see
a DFU blinky pattern on one of the board LEDs:
```
$ make SD=s140 BOARD=feather52840 boot-flash
$ make SD=s140 BOARD=feather52840 bootloader
Use make V=1, make V=2 or set BUILD_VERBOSE similarly in your environment to increase build verbosity.
nrfjprog --program boards/feather52840/bootloader/feather52840_bootloader_6.0.0_s140_single.hex -f nrf52 --chiperase --reset
Parsing hex file.
@ -150,9 +150,9 @@ $ sudo pip install -r requirements.txt
$ sudo python setup.py install
```
#### Changes to `nrfutil` in 0.5.2b
#### Changes to `nrfutil` in 0.5.2d
**IMPORTANT**: Make sure that you have version **0.5.2b**, since a small
**IMPORTANT**: Make sure that you have version **0.5.2d**, since a small
change was required to `dfu_transport_serial.py` to account for the
increased minimum flash erase time on the nRF52840 compared to the earlier
nRF52832!
@ -160,12 +160,7 @@ nRF52832!
You can also manually change the file with the following new values (lines
67-68), and reinstall the utility via `sudo python setup.py install`:
```
FLASH_PAGE_ERASE_MAX_TIME = 0.1 # Worst time to erase a page 100 ms
FLASH_PAGE_ERASE_MIN_TIME = 0.09 # Best time to erase a page 90 ms
```
### Flashing CircuitPython
### Flashing CircuitPython with USB CDC
With the serial bootloader present on your board, you first need to force your
board into DFU mode by holding down BUTTON1 and RESETTING the board (with
@ -174,28 +169,48 @@ BUTTON1 still pressed as you come out of reset).
This will give you a **fast blinky DFU pattern** to indicate you are in DFU
mode.
At this point, you can build and flash a CircuitPython binary via the following
At this point, you can **build and flash** a CircuitPython binary via the following
command:
```
$ make V=1 SD=s140 SERIAL=/dev/tty.usbmodem1411 BOARD=feather52840 dfu-gen dfu-flash
$ make V=1 SD=s140 SERIAL=/dev/tty.usbmodem1411 BOARD=feather52840 all dfu-gen dfu-flash
```
This should give you the following results:
```
$ make V=1 SD=s140 SERIAL=/dev/tty.usbmodem1411 BOARD=feather52840 dfu-gen dfu-flash
$make V=1 BOARD=feather52840 SD=s140 SERIAL=/dev/tty.usbmodem1411 dfu-gen dfu-flash
nrfutil dfu genpkg --sd-req 0xFFFE --dev-type 0x0052 --application build-feather52840-s140/firmware.hex build-feather52840-s140/dfu-package.zip
Zip created at build-feather52840-s140/dfu-package.zip
nrfutil --verbose dfu serial --package build-feather52840-s140/dfu-package.zip -p /dev/tty.usbmodem1411 -b 115200
Upgrading target on /dev/tty.usbmodem1411 with DFU package /Users/kevintownsend/Dropbox/microBuilder/Code/CircuitPython/circuitpython-mb/ports/nrf/build-feather52840-s140/dfu-package.zip. Flow control is disabled.
Starting DFU upgrade of type 4, SoftDevice size: 0, bootloader size: 0, application size: 195252
nrfutil --verbose dfu serial --package build-feather52840-s140/dfu-package.zip -p /dev/ttyACM1 -b 115200 --singlebank
Upgrading target on /dev/ttyACM1 with DFU package /home/hathach/Dropbox/adafruit/circuitpython/ada_cp/ports/nrf/build-feather52840-s140/dfu-package.zip. Flow control is disabled, Single bank mode
Starting DFU upgrade of type 4, SoftDevice size: 0, bootloader size: 0, application size: 199840
Sending DFU start packet
Sending DFU init packet
Sending firmware file
################################################################################################################################################################################################################################################################################################################################################################################################
#########################################################################################################################################################################################################################################################################################################################################################################################################
Activating new firmware
DFU upgrade took 41.6610329151s
DFU upgrade took 8.50606513023s
Device programmed.
```
### Flashing CircuitPython with MSC UF2
Make `uf2` target to generate the uf2
```
$ make V=1 SD=s140 SERIAL=/dev/tty.usbmodem1411 BOARD=feather52840 all uf2
Create firmware.uf2
../../tools/uf2/utils/uf2conv.py -c -o "build-feather52840-s140/firmware.uf2" "build-feather52840-s140/firmware.hex"
Converting to uf2, output size: 392192, start address: 0x26000
Wrote 392192 bytes to build-feather52840-s140/firmware.uf2.
```
Simply drag and drop firmware.uf2 to the MSC, the nrf52840 will blink fast and reset after done.
**Note**: you need to update `tools/uf2` for uf2conv.py to support hex file input, current circuitpython's master use older verion of uf2conv.py which only support biin file input. To update, change directory to top folder of circuitpython and run. The size of uf2 should be ~400KB, if using the old uf2conv.py the output file would be 1 MB which is not correct.
```
git submodule update --init
```

View File

@ -28,7 +28,7 @@
#define MICROPY_HW_BOARD_NAME "Feather52840"
#define MICROPY_HW_MCU_NAME "nRF52840"
#define MICROPY_PY_SYS_PLATFORM "nRF52840-PDK"
#define MICROPY_PY_SYS_PLATFORM "Feather52840"
#define MICROPY_HW_UART_RX NRF_GPIO_PIN_MAP(0, 8)
#define MICROPY_HW_UART_TX NRF_GPIO_PIN_MAP(0, 6)

View File

@ -1,36 +1,16 @@
MCU_SERIES = m4
MCU_VARIANT = nrf52
MCU_SUB_VARIANT = nrf52840
SD ?= s140
SOFTDEV_VERSION ?= 6.0.0
LD_FILE = boards/feather52840/bluefruit_nrf52840_s140_6.0.0.ld
BOOTLOADER_FILENAME = boards/feather52840/bootloader/feather52840_bootloader_6.0.0_s140_single
BOOT_SETTING_ADDR = 0xFF000
BOOT_FILE = boards/$(BOARD)/bootloader/$(SOFTDEV_VERSION)/$(BOARD)_bootloader_$(SOFTDEV_VERSION)_s140
NRF_DEFINES += -DNRF52840_XXAA
ifeq ($(OS),Windows_NT)
NRFUTIL = ../../lib/nrfutil/binaries/win32/nrfutil.exe
ifeq ($(SD),)
LD_FILE = boards/nrf52840_1M_256k.ld
else
NRFUTIL = nrfutil
LD_FILE = boards/bluefruit_$(MCU_SUB_VARIANT)_$(SD_LOWER)_$(SOFTDEV_VERSION).ld
endif
CFLAGS += -DADAFRUIT_FEATHER52840
check_defined = \
$(strip $(foreach 1,$1, \
$(call __check_defined,$1,$(strip $(value 2)))))
__check_defined = \
$(if $(value $1),, \
$(error Undefined make flag: $1$(if $2, ($2))))
.PHONY: dfu-gen dfu-flash boot-flash
dfu-gen:
$(NRFUTIL) dfu genpkg --sd-req 0xFFFE --dev-type 0x0052 --application $(BUILD)/$(OUTPUT_FILENAME).hex $(BUILD)/dfu-package.zip
dfu-flash:
@:$(call check_defined, SERIAL, example: SERIAL=/dev/ttyUSB0)
$(NRFUTIL) --verbose dfu serial --package $(BUILD)/dfu-package.zip -p $(SERIAL) -b 115200
boot-flash:
nrfjprog --program $(BOOTLOADER_FILENAME).hex -f nrf52 --chiperase --reset
NRF_DEFINES += -DNRF52840_XXAA

View File

@ -1,6 +1,7 @@
MCU_SERIES = m4
MCU_VARIANT = nrf52
MCU_SUB_VARIANT = nrf52
SD ?= s132
SOFTDEV_VERSION ?= 5.0.0
ifeq ($(SD),)

View File

@ -27,12 +27,40 @@
#include <string.h>
#include <stdbool.h>
#include "nrf.h"
#include "nrfx.h"
#include "nrfx_power.h"
#include "boards/board.h"
#include "tick.h"
#include "tusb.h"
void board_init(void) {
// Clock
NRF_CLOCK->LFCLKSRC = (uint32_t)((CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos) & CLOCK_LFCLKSRC_SRC_Msk);
NRF_CLOCK->TASKS_LFCLKSTART = 1UL;
// Init USB
#ifdef SOFTDEVICE_PRESENT
// TODO support Softdevice config
#else
// Softdevice is not present, init power module and register tusb power event function
// for vusb detect, ready, removed
extern void tusb_hal_nrf_power_event(uint32_t event);
// Power module init
const nrfx_power_config_t pwr_cfg = { 0 };
nrfx_power_init(&pwr_cfg);
// USB Power detection
const nrfx_power_usbevt_config_t config = { .handler = (nrfx_power_usb_event_handler_t) tusb_hal_nrf_power_event };
nrfx_power_usbevt_init(&config);
nrfx_power_usbevt_enable();
#endif
tusb_init();
}
bool board_requests_safe_mode(void) {
@ -42,3 +70,23 @@ bool board_requests_safe_mode(void) {
void reset_board(void) {
}
//--------------------------------------------------------------------+
// tinyusb callbacks
//--------------------------------------------------------------------+
void tud_mount_cb(uint8_t rhport) {
(void) rhport;
}
void tud_umount_cb(uint8_t rhport) {
(void) rhport;
}
uint32_t tusb_hal_millis(void) {
uint64_t ms;
uint32_t us;
current_tick(&ms, &us);
return (uint32_t) ms;
}

File diff suppressed because it is too large Load Diff

View File

@ -26,7 +26,7 @@
#define MICROPY_HW_BOARD_NAME "PCA10056"
#define MICROPY_HW_MCU_NAME "nRF52840"
#define MICROPY_PY_SYS_PLATFORM "nRF52840-PDK"
#define MICROPY_PY_SYS_PLATFORM "nRF52840-DK"
#define MICROPY_HW_UART_RX NRF_GPIO_PIN_MAP(0, 8)
#define MICROPY_HW_UART_TX NRF_GPIO_PIN_MAP(0, 6)
@ -34,3 +34,7 @@
#define PORT_HEAP_SIZE (128 * 1024)
#define CIRCUITPY_AUTORELOAD_DELAY_MS 500
// Temp (could be removed) 0: usb cdc (default), 1 : hwuart (jlink)
#define CFG_HWUART_FOR_SERIAL 0

View File

@ -1,12 +1,17 @@
MCU_SERIES = m4
MCU_VARIANT = nrf52
MCU_SUB_VARIANT = nrf52840
SD ?= s140
SOFTDEV_VERSION ?= 6.0.0
BOOT_SETTING_ADDR = 0xFF000
BOOT_FILE = boards/$(BOARD)/bootloader/$(SOFTDEV_VERSION)/$(BOARD)_bootloader_$(SOFTDEV_VERSION)_s140
ifeq ($(SD),)
LD_FILE = boards/nrf52840_1M_256k.ld
LD_FILE = boards/nrf52840_1M_256k.ld
else
LD_FILE = boards/nrf52840_1M_256k_s140_$(SOFTDEV_VERSION).ld
LD_FILE = boards/bluefruit_$(MCU_SUB_VARIANT)_$(SD_LOWER)_$(SOFTDEV_VERSION).ld
endif
NRF_DEFINES += -DNRF52840_XXAA

View File

@ -1,4 +1,4 @@
P0_00,P0_01
P0_00,P0_00
P0_01,P0_01
P0_02,P0_02
P0_03,P0_03

1 P0_00 P0_01 P0_00
2 P0_01 P0_01
3 P0_02 P0_02
4 P0_03 P0_03

View File

@ -0,0 +1,60 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2016 Glenn Ruben Bakke
* Copyright (c) 2018 Artur Pacholec
*
* 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 <stdio.h>
#include "ble_drv.h"
#include "nrfx.h"
#include "nrf_error.h"
#include "py/misc.h"
void common_hal_bleio_adapter_set_enabled(bool enabled) {
if (enabled) {
const uint32_t err = ble_drv_stack_enable();
if (err != NRF_SUCCESS) {
NRFX_ASSERT(err);
}
printf("SoftDevice enabled\n");
} else {
ble_drv_stack_disable();
}
}
bool common_hal_bleio_adapter_get_enabled(void) {
return ble_drv_stack_enabled();
}
void common_hal_bleio_adapter_get_address(vstr_t *vstr) {
ble_drv_addr_t address;
ble_drv_address_get(&address);
vstr_printf(vstr, ""HEX2_FMT":"HEX2_FMT":"HEX2_FMT":" \
HEX2_FMT":"HEX2_FMT":"HEX2_FMT"",
address.addr[5], address.addr[4], address.addr[3],
address.addr[2], address.addr[1], address.addr[0]);
}

View File

@ -4,6 +4,7 @@
* The MIT License (MIT)
*
* Copyright (c) 2016 Glenn Ruben Bakke
* Copyright (c) 2018 Artur Pacholec
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@ -24,24 +25,13 @@
* THE SOFTWARE.
*/
#ifndef HELP_SD_H__
#define HELP_SD_H__
#ifndef MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_ADAPTER_H
#define MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_ADAPTER_H
#include "bluetooth_conf.h"
#include "py/obj.h"
#if MICROPY_PY_BLE
typedef struct {
mp_obj_base_t base;
} super_adapter_obj_t;
#define HELP_TEXT_SD \
"If compiled with SD=<softdevice> the additional commands are\n" \
"available:\n" \
" ble.enable() -- enable bluetooth stack\n" \
" ble.disable() -- disable bluetooth stack\n" \
" ble.enabled() -- check whether bluetooth stack is enabled\n" \
" ble.address() -- return device address as text string\n" \
"\n"
#else
#define HELP_TEXT_SD
#endif // MICROPY_PY_BLE
#endif
#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_ADAPTER_H

View File

@ -0,0 +1,37 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2016 Glenn Ruben Bakke
* Copyright (c) 2018 Artur Pacholec
*
* 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 "shared-bindings/bleio/__init__.h"
#include "shared-bindings/bleio/Adapter.h"
// The singleton bleio.Adapter object, bound to bleio.adapter
// It currently only has properties and no state
const super_adapter_obj_t common_hal_bleio_adapter_obj = {
.base = {
.type = &bleio_adapter_type,
},
};

View File

@ -34,6 +34,8 @@
#include "supervisor/filesystem.h"
#include "nrfx_glue.h"
// This routine should work even when interrupts are disabled. Used by OneWire
// for precise timing.
void common_hal_mcu_delay_us(uint32_t delay) {
NRFX_DELAY_US(delay);
}
@ -60,4 +62,3 @@ const mcu_processor_obj_t common_hal_mcu_processor_obj = {
.type = &mcu_processor_type,
},
};

View File

@ -35,8 +35,9 @@
extern volatile bool mp_msc_enabled;
void common_hal_storage_remount(const char *mount_path, bool readonly) {
if (strcmp(mount_path, "/") != 0)
if (strcmp(mount_path, "/") != 0) {
mp_raise_OSError(MP_EINVAL);
}
}
void common_hal_storage_erase_filesystem(void) {

View File

@ -30,12 +30,15 @@
#include <string.h>
#include <stdbool.h>
#if (BLUETOOTH_SD == 132)
#define NRF52 // Needed for SD132 v2
#endif
#include "py/runtime.h"
#include "ble_drv.h"
#include "mpconfigport.h"
#include "nrf_sdm.h"
#include "nrfx_power.h"
#include "ble_gap.h"
#include "ble.h" // sd_ble_uuid_encode
@ -142,6 +145,12 @@ uint32_t ble_drv_stack_enable(void) {
#endif
};
#endif
#if (BLUETOOTH_SD == 140)
// The SD takes over the POWER IRQ and will fail if the IRQ is already in use
nrfx_power_uninit();
#endif
uint32_t err_code = sd_softdevice_enable(&clock_config,
softdevice_assert_handler);

View File

@ -29,14 +29,11 @@
#include <string.h>
#include "ble_uart.h"
#include "ringbuffer.h"
#include "py/mphal.h"
#include "lib/utils/interrupt_char.h"
#if MICROPY_PY_BLE_NUS
#if BLUETOOTH_WEBBLUETOOTH_REPL
#include "hal_time.h"
#endif // BLUETOOTH_WEBBLUETOOTH_REPL
static ubluepy_uuid_obj_t uuid_obj_service = {
.base.type = &ubluepy_uuid_type,
.type = UBLUEPY_UUID_128_BIT,

View File

@ -36,7 +36,7 @@
#include "lib/oofatfs/ff.h"
#include "supervisor/shared/rgb_led_status.h"
#include "nrf.h"
#include "nrf_nvmc.h"
#ifdef BLUETOOTH_SD
#include "nrf_sdm.h"
@ -46,17 +46,30 @@
extern uint32_t __fatfs_flash_start_addr[];
extern uint32_t __fatfs_flash_length[];
#define NO_CACHE 0xffffffff
#define FL_PAGE_SZ 4096
uint8_t _flash_cache[FL_PAGE_SZ] __attribute__((aligned(4)));
uint32_t _flash_page_addr = NO_CACHE;
/*------------------------------------------------------------------*/
/* Internal Flash API
*------------------------------------------------------------------*/
static inline uint32_t lba2addr(uint32_t block) {
return ((uint32_t)__fatfs_flash_start_addr) + block * FILESYSTEM_BLOCK_SIZE;
}
void internal_flash_init(void) {
// Activity LED for flash writes.
#ifdef MICROPY_HW_LED_MSC
struct port_config pin_conf;
port_get_config_defaults(&pin_conf);
#ifdef MICROPY_HW_LED_MSC
struct port_config pin_conf;
port_get_config_defaults(&pin_conf);
pin_conf.direction = PORT_PIN_DIR_OUTPUT;
port_pin_set_config(MICROPY_HW_LED_MSC, &pin_conf);
port_pin_set_output_level(MICROPY_HW_LED_MSC, false);
#endif
// flash_init(&internal_flash_desc, NVMCTRL);
pin_conf.direction = PORT_PIN_DIR_OUTPUT;
port_pin_set_config(MICROPY_HW_LED_MSC, &pin_conf);
port_pin_set_output_level(MICROPY_HW_LED_MSC, false);
#endif
}
uint32_t internal_flash_get_block_size(void) {
@ -67,101 +80,63 @@ uint32_t internal_flash_get_block_count(void) {
return ((uint32_t) __fatfs_flash_length) / FILESYSTEM_BLOCK_SIZE ;
}
// TODO support flashing with SD enabled
void internal_flash_flush(void) {
}
if (_flash_page_addr == NO_CACHE) return;
void flash_flush(void) {
internal_flash_flush();
}
static uint32_t convert_block_to_flash_addr(uint32_t block) {
return ((uint32_t)__fatfs_flash_start_addr) + block * FILESYSTEM_BLOCK_SIZE;
}
bool internal_flash_write_block(const uint8_t *src, uint32_t block) {
uint8_t sd_en = 0;
#ifdef MICROPY_HW_LED_MSC
port_pin_set_output_level(MICROPY_HW_LED_MSC, true);
#endif
temp_status_color(ACTIVE_WRITE);
// non-MBR block, copy to cache
uint32_t dest = convert_block_to_flash_addr(block);
uint32_t pagenum = dest / FLASH_PAGE_SIZE;
uint32_t* flash_align = (uint32_t*) (pagenum*FLASH_PAGE_SIZE);
// Read back current page to update only 512 portion
__ALIGN(4) uint8_t buf[FLASH_PAGE_SIZE];
memcpy(buf, flash_align, FLASH_PAGE_SIZE);
memcpy(buf + (dest%FLASH_PAGE_SIZE), src, FILESYSTEM_BLOCK_SIZE);
#ifdef BLUETOOTH_SD
(void) sd_softdevice_is_enabled(&sd_en);
if (sd_en) {
if (NRF_SUCCESS != sd_flash_page_erase(pagenum)) {
return false;
// Skip if data is the same
if (memcmp(_flash_cache, (void *)_flash_page_addr, FL_PAGE_SZ) != 0) {
// _is_flashing = true;
nrf_nvmc_page_erase(_flash_page_addr);
nrf_nvmc_write_words(_flash_page_addr, (uint32_t *)_flash_cache, FL_PAGE_SZ / sizeof(uint32_t));
}
if (NRF_SUCCESS != sd_flash_write(flash_align, (uint32_t*) buf, FLASH_PAGE_SIZE / sizeof(uint32_t))) {
return false;
}
}
#endif
if (!sd_en) {
// Erase
NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Een << NVMC_CONFIG_WEN_Pos);
while (NRF_NVMC->READY == NVMC_READY_READY_Busy);
NRF_NVMC->ERASEPAGE = dest;
while (NRF_NVMC->READY == NVMC_READY_READY_Busy);
NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos);
while (NRF_NVMC->READY == NVMC_READY_READY_Busy);
// Write
uint32_t *p_src = (uint32_t*) buf;
uint32_t *p_dest = flash_align;
uint32_t i = 0;
while (i < (FLASH_PAGE_SIZE / sizeof(uint32_t))) {
NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos);
while (NRF_NVMC->READY == NVMC_READY_READY_Busy);
*p_dest++ = *p_src++;
while (NRF_NVMC->READY == NVMC_READY_READY_Busy);
NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos);
while (NRF_NVMC->READY == NVMC_READY_READY_Busy);
++i;
}
}
clear_temp_status();
#ifdef MICROPY_HW_LED_MSC
port_pin_set_output_level(MICROPY_HW_LED_MSC, false);
#endif
return true;
_flash_page_addr = NO_CACHE;
}
mp_uint_t internal_flash_read_blocks(uint8_t *dest, uint32_t block, uint32_t num_blocks) {
uint32_t src = convert_block_to_flash_addr(block);
memcpy(dest, (uint8_t*) src, FILESYSTEM_BLOCK_SIZE*num_blocks);
return 0; // success
uint32_t src = lba2addr(block);
memcpy(dest, (uint8_t*) src, FILESYSTEM_BLOCK_SIZE*num_blocks);
return 0; // success
}
mp_uint_t internal_flash_write_blocks(const uint8_t *src, uint32_t block_num, uint32_t num_blocks) {
for (size_t i = 0; i < num_blocks; i++) {
if (!internal_flash_write_block(src + i * FILESYSTEM_BLOCK_SIZE, block_num + i)) {
return 1; // error
mp_uint_t internal_flash_write_blocks(const uint8_t *src, uint32_t lba, uint32_t num_blocks) {
#ifdef MICROPY_HW_LED_MSC
port_pin_set_output_level(MICROPY_HW_LED_MSC, true);
#endif
while (num_blocks) {
uint32_t const addr = lba2addr(lba);
uint32_t const page_addr = addr & ~(FL_PAGE_SZ - 1);
uint32_t count = 8 - (lba % 8); // up to page boundary
count = MIN(num_blocks, count);
if (page_addr != _flash_page_addr) {
internal_flash_flush();
// writing previous cached data, skip current data until flashing is done
// tinyusb stack will invoke write_block() with the same parameters later on
// if ( _is_flashing ) return;
_flash_page_addr = page_addr;
memcpy(_flash_cache, (void *)page_addr, FL_PAGE_SZ);
}
memcpy(_flash_cache + (addr & (FL_PAGE_SZ - 1)), src, count * FILESYSTEM_BLOCK_SIZE);
// adjust for next run
lba += count;
src += count * FILESYSTEM_BLOCK_SIZE;
num_blocks -= count;
}
}
return 0; // success
#ifdef MICROPY_HW_LED_MSC
port_pin_set_output_level(MICROPY_HW_LED_MSC, false);
#endif
return 0; // success
}
/******************************************************************************/
@ -224,6 +199,10 @@ const mp_obj_type_t internal_flash_type = {
.locals_dict = (mp_obj_t)&internal_flash_obj_locals_dict,
};
/*------------------------------------------------------------------*/
/* Flash API
*------------------------------------------------------------------*/
void flash_init_vfs(fs_user_mount_t *vfs) {
vfs->base.type = &mp_fat_vfs_type;
vfs->flags |= FSUSER_NATIVE | FSUSER_HAVE_IOCTL;
@ -241,3 +220,7 @@ void flash_init_vfs(fs_user_mount_t *vfs) {
vfs->u.ioctl[0] = (mp_obj_t)&internal_flash_obj_ioctl_obj;
vfs->u.ioctl[1] = (mp_obj_t)&internal_flash_obj;
}
void flash_flush(void) {
internal_flash_flush();
}

View File

@ -33,19 +33,17 @@
#define FLASH_ROOT_POINTERS
#define FLASH_PAGE_SIZE 0x1000
#define CIRCUITPY_INTERNAL_NVM_SIZE 0
#define FLASH_PAGE_SIZE 0x1000
#define CIRCUITPY_INTERNAL_NVM_SIZE 0
#define INTERNAL_FLASH_SYSTICK_MASK (0x1ff) // 512ms
#define INTERNAL_FLASH_IDLE_TICK(tick) (((tick) & INTERNAL_FLASH_SYSTICK_MASK) == 2)
#define INTERNAL_FLASH_SYSTICK_MASK (0x1ff) // 512ms
#define INTERNAL_FLASH_IDLE_TICK(tick) (((tick) & INTERNAL_FLASH_SYSTICK_MASK) == 2)
void internal_flash_init(void);
uint32_t internal_flash_get_block_size(void);
uint32_t internal_flash_get_block_count(void);
void internal_flash_irq_handler(void);
void internal_flash_flush(void);
bool internal_flash_read_block(uint8_t *dest, uint32_t block);
bool internal_flash_write_block(const uint8_t *src, uint32_t block);
void internal_flash_init(void);
uint32_t internal_flash_get_block_size(void);
uint32_t internal_flash_get_block_count(void);
void internal_flash_irq_handler(void);
void internal_flash_flush(void);
// these return 0 on success, non-zero on error
mp_uint_t internal_flash_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blocks);

View File

@ -1,104 +0,0 @@
/*
* 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.
*/
#include <stdio.h>
#include <stdint.h>
#include "py/runtime.h"
#if MICROPY_PY_BLE
#include "mpconfigboard.h"
#include "ble_drv.h"
/// \method enable()
/// Enable BLE softdevice.
mp_obj_t ble_obj_enable(void) {
printf("SoftDevice enabled\n");
uint32_t err_code = ble_drv_stack_enable();
if (err_code < 0) {
// TODO: raise exception.
}
return mp_const_none;
}
/// \method disable()
/// Disable BLE softdevice.
mp_obj_t ble_obj_disable(void) {
ble_drv_stack_disable();
return mp_const_none;
}
/// \method enabled()
/// Get state of whether the softdevice is enabled or not.
mp_obj_t ble_obj_enabled(void) {
uint8_t is_enabled = ble_drv_stack_enabled();
mp_int_t enabled = is_enabled;
return MP_OBJ_NEW_SMALL_INT(enabled);
}
/// \method address()
/// Return device address as text string.
mp_obj_t ble_obj_address(void) {
ble_drv_addr_t local_addr;
ble_drv_address_get(&local_addr);
vstr_t vstr;
vstr_init(&vstr, 17);
vstr_printf(&vstr, ""HEX2_FMT":"HEX2_FMT":"HEX2_FMT":" \
HEX2_FMT":"HEX2_FMT":"HEX2_FMT"",
local_addr.addr[5], local_addr.addr[4], local_addr.addr[3],
local_addr.addr[2], local_addr.addr[1], local_addr.addr[0]);
mp_obj_t mac_str = mp_obj_new_str(vstr.buf, vstr.len, false);
vstr_clear(&vstr);
return mac_str;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(ble_obj_enable_obj, ble_obj_enable);
STATIC MP_DEFINE_CONST_FUN_OBJ_0(ble_obj_disable_obj, ble_obj_disable);
STATIC MP_DEFINE_CONST_FUN_OBJ_0(ble_obj_enabled_obj, ble_obj_enabled);
STATIC MP_DEFINE_CONST_FUN_OBJ_0(ble_obj_address_obj, ble_obj_address);
STATIC const mp_rom_map_elem_t ble_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ble) },
{ MP_ROM_QSTR(MP_QSTR_enable), MP_ROM_PTR(&ble_obj_enable_obj) },
{ MP_ROM_QSTR(MP_QSTR_disable), MP_ROM_PTR(&ble_obj_disable_obj) },
{ MP_ROM_QSTR(MP_QSTR_enabled), MP_ROM_PTR(&ble_obj_enabled_obj) },
{ MP_ROM_QSTR(MP_QSTR_address), MP_ROM_PTR(&ble_obj_address_obj) },
};
STATIC MP_DEFINE_CONST_DICT(ble_module_globals, ble_module_globals_table);
const mp_obj_module_t ble_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&ble_module_globals,
};
#endif // MICROPY_PY_BLE

View File

@ -126,27 +126,27 @@
#define MICROPY_KBD_EXCEPTION (1)
#ifndef MICROPY_PY_HW_RNG
#define MICROPY_PY_HW_RNG (1)
#endif
#define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (1)
#define MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE (0)
// Scan gamepad every 32ms
#define CIRCUITPY_GAMEPAD_TICKS 0x1f
// if sdk is in use, import configuration
#if BLUETOOTH_SD
#include "bluetooth_conf.h"
#define MICROPY_PY_BLEIO (1)
#define MICROPY_PY_BLE_NUS (0)
#define MICROPY_PY_UBLUEPY (1)
#define MICROPY_PY_UBLUEPY_PERIPHERAL (1)
#define MICROPY_PY_UBLUEPY_CENTRAL (1)
#define BLUETOOTH_WEBBLUETOOTH_REPL (0)
#endif
#ifndef MICROPY_PY_BLEIO
#define MICROPY_PY_BLEIO (0)
#endif
#ifndef MICROPY_PY_UBLUEPY
#define MICROPY_PY_UBLUEPY (0)
#endif
#ifndef MICROPY_PY_BLE_NUS
#define MICROPY_PY_BLE_NUS (0)
#define MICROPY_PY_UBLUEPY (0)
#endif
// type definitions for the specific machine
@ -181,22 +181,20 @@ extern const struct _mp_obj_module_t struct_module;
extern const struct _mp_obj_module_t time_module;
extern const struct _mp_obj_module_t supervisor_module;
extern const struct _mp_obj_module_t gamepad_module;
extern const struct _mp_obj_module_t bleio_module;
extern const struct _mp_obj_module_t mp_module_ubluepy;
extern const struct _mp_obj_module_t ble_module;
#if MICROPY_PY_UBLUEPY
#define UBLUEPY_MODULE { MP_ROM_QSTR(MP_QSTR_ubluepy), MP_ROM_PTR(&mp_module_ubluepy) },
#else
#define UBLUEPY_MODULE
#endif
#if MICROPY_PY_BLE
extern const struct _mp_obj_module_t ble_module;
#define BLE_MODULE { MP_ROM_QSTR(MP_QSTR_ble), MP_ROM_PTR(&ble_module) },
#if MICROPY_PY_BLEIO
#define BLEIO_MODULE { MP_ROM_QSTR(MP_QSTR_bleio), MP_ROM_PTR(&bleio_module) },
#else
#define BLE_MODULE
#define BLEIO_MODULE
#endif
#define MICROPY_PORT_BUILTIN_MODULES \
@ -214,7 +212,7 @@ extern const struct _mp_obj_module_t ble_module;
{ MP_OBJ_NEW_QSTR (MP_QSTR_supervisor ), (mp_obj_t)&supervisor_module }, \
{ MP_OBJ_NEW_QSTR (MP_QSTR_gamepad ), (mp_obj_t)&gamepad_module }, \
{ MP_OBJ_NEW_QSTR (MP_QSTR_time ), (mp_obj_t)&time_module }, \
BLE_MODULE \
BLEIO_MODULE \
UBLUEPY_MODULE \
// extra built in names to add to the global namespace
@ -223,10 +221,6 @@ extern const struct _mp_obj_module_t ble_module;
{ MP_OBJ_NEW_QSTR (MP_QSTR_input), (mp_obj_t)&mp_builtin_input_obj }, \
{ MP_ROM_QSTR (MP_QSTR_open), MP_ROM_PTR(&mp_builtin_open_obj) }, \
// extra constants
#define MICROPY_PORT_CONSTANTS \
BLE_MODULE \
#define MP_STATE_PORT MP_STATE_VM
#define MICROPY_PORT_ROOT_POINTERS \
@ -238,6 +232,10 @@ extern const struct _mp_obj_module_t ble_module;
// We need to provide a declaration/definition of alloca()
#include <alloca.h>
extern void run_background_tasks(void);
#define MICROPY_VM_HOOK_LOOP run_background_tasks();
#define MICROPY_VM_HOOK_RETURN run_background_tasks();
//#define CIRCUITPY_BOOT_OUTPUT_FILE "/boot_out.txt"
#endif

View File

@ -28,10 +28,12 @@
#include <string.h>
#include "mphalport.h"
#include "py/mpstate.h"
void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len);
#if (MICROPY_PY_BLE_NUS == 0)
#if !defined( NRF52840_XXAA) || ( defined(CFG_HWUART_FOR_SERIAL) && CFG_HWUART_FOR_SERIAL == 1 )
int mp_hal_stdin_rx_chr(void) {
uint8_t data = 0;
@ -57,26 +59,71 @@ void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
NRFX_ASSERT(err);
}
void mp_hal_stdout_tx_strn_cooked(const char *str, mp_uint_t len) {
const char cr = '\r';
#else
while (len--) {
if (*str == '\n')
mp_hal_stdout_tx_strn(&cr, sizeof(cr));
#include "tusb.h"
mp_hal_stdout_tx_strn(str++, sizeof(char));
int mp_hal_stdin_rx_chr(void) {
for (;;) {
#ifdef MICROPY_VM_HOOK_LOOP
MICROPY_VM_HOOK_LOOP
#endif
// if (reload_requested) {
// return CHAR_CTRL_D;
// }
if (tud_cdc_available()) {
#ifdef MICROPY_HW_LED_RX
gpio_toggle_pin_level(MICROPY_HW_LED_RX);
#endif
return tud_cdc_read_char();
}
}
return 0;
}
bool mp_hal_stdin_any(void) {
return tud_cdc_available() > 0;
}
void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
#ifdef MICROPY_HW_LED_TX
gpio_toggle_pin_level(MICROPY_HW_LED_TX);
#endif
#ifdef CIRCUITPY_BOOT_OUTPUT_FILE
if (boot_output_file != NULL) {
UINT bytes_written = 0;
f_write(boot_output_file, str, len, &bytes_written);
}
#endif
tud_cdc_write(str, len);
}
#endif // USB
#endif // NUS
/*------------------------------------------------------------------*/
/* delay
*------------------------------------------------------------------*/
void mp_hal_delay_ms(mp_uint_t delay) {
uint64_t start_tick = ticks_ms;
uint64_t duration = 0;
while (duration < delay) {
#ifdef MICROPY_VM_HOOK_LOOP
MICROPY_VM_HOOK_LOOP
#endif
// Check to see if we've been CTRL-Ced by autoreload or the user.
if(MP_STATE_VM(mp_pending_exception) == MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception))) {
break;
}
duration = (ticks_ms - start_tick);
// TODO(tannewt): Go to sleep for a little while while we wait.
}
}
#endif
void mp_hal_stdout_tx_str(const char *str) {
mp_hal_stdout_tx_strn(str, strlen(str));
}
void mp_hal_delay_ms(mp_uint_t ms) {
NRFX_DELAY_US(ms * 1000);
}
void mp_hal_delay_us(mp_uint_t us) {
NRFX_DELAY_US(us);
}

View File

@ -46,7 +46,6 @@ int mp_hal_stdin_rx_chr(void);
void mp_hal_stdout_tx_str(const char *str);
bool mp_hal_stdin_any(void);
void mp_hal_delay_ms(mp_uint_t ms);
void mp_hal_delay_us(mp_uint_t us);
#define mp_hal_delay_us(us) NRFX_DELAY_US((uint32_t) (us))
#endif

View File

@ -1,6 +1,10 @@
#ifndef NRFX_CONFIG_H__
#define NRFX_CONFIG_H__
// Power
#define NRFX_POWER_ENABLED 1
#define NRFX_POWER_CONFIG_IRQ_PRIORITY 7
// SPI
#define NRFX_SPIM_ENABLED 1

View File

@ -80,6 +80,11 @@ void __assert_func(const char *file, int line, const char *func, const char *exp
*/
#define NRFX_STATIC_ASSERT(expression)
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
#endif
//------------------------------------------------------------------------------
#ifdef SOFTDEVICE_PRESENT

View File

@ -32,23 +32,23 @@
#include "internal_flash.h"
fs_user_mount_t fs_user_mount_flash;
mp_vfs_mount_t mp_vfs_mount_flash;
static mp_vfs_mount_t _mp_vfs;
static fs_user_mount_t _internal_vfs;
void filesystem_init(bool create_allowed) {
// init the vfs object
fs_user_mount_t *vfs_fat = &fs_user_mount_flash;
vfs_fat->flags = 0;
flash_init_vfs(vfs_fat);
fs_user_mount_t *int_vfs = &_internal_vfs;
int_vfs->flags = 0;
flash_init_vfs(int_vfs);
// try to mount the flash
FRESULT res = f_mount(&vfs_fat->fatfs);
FRESULT res = f_mount(&int_vfs->fatfs);
if (res == FR_NO_FILESYSTEM && create_allowed) {
// no filesystem so create a fresh one
uint8_t working_buf[_MAX_SS];
res = f_mkfs(&vfs_fat->fatfs, FM_FAT | FM_SFD, 4096, working_buf, sizeof(working_buf));
res = f_mkfs(&int_vfs->fatfs, FM_FAT | FM_SFD, 4096, working_buf, sizeof(working_buf));
// Flush the new file system to make sure its repaired immediately.
flash_flush();
if (res != FR_OK) {
@ -56,24 +56,26 @@ void filesystem_init(bool create_allowed) {
}
// set label
f_setlabel(&vfs_fat->fatfs, "CIRCUITPY");
flash_flush();
f_setlabel(&int_vfs->fatfs, "CIRCUITPY");
// create lib folder
f_mkdir(&vfs_fat->fatfs, "/lib");
f_mkdir(&int_vfs->fatfs, "/lib");
flash_flush();
} else if (res != FR_OK) {
return;
}
mp_vfs_mount_t *vfs = &mp_vfs_mount_flash;
vfs->str = "/";
vfs->len = 1;
vfs->obj = MP_OBJ_FROM_PTR(vfs_fat);
vfs->next = NULL;
MP_STATE_VM(vfs_mount_table) = vfs;
mp_vfs_mount_t *mp_vfs = &_mp_vfs;
mp_vfs->str = "/";
mp_vfs->len = 1;
mp_vfs->obj = MP_OBJ_FROM_PTR(int_vfs);
mp_vfs->next = NULL;
MP_STATE_VM(vfs_mount_table) = mp_vfs;
// The current directory is used as the boot up directory.
// It is set to the internal flash filesystem by default.
MP_STATE_PORT(vfs_cur) = vfs;
MP_STATE_PORT(vfs_cur) = mp_vfs;
}
void filesystem_flush(void) {
@ -81,6 +83,13 @@ void filesystem_flush(void) {
}
void filesystem_writable_by_python(bool writable) {
fs_user_mount_t *vfs = &_internal_vfs;
if (writable) {
vfs->flags |= FSUSER_USB_WRITABLE;
} else {
vfs->flags &= ~FSUSER_USB_WRITABLE;
}
}
bool filesystem_present(void) {

View File

@ -33,6 +33,8 @@
#include "pin.h"
#endif
#if !defined( NRF52840_XXAA) || ( defined(CFG_HWUART_FOR_SERIAL) && CFG_HWUART_FOR_SERIAL == 1 )
#define INST_NO 0
nrfx_uart_t serial_instance = NRFX_UART_INSTANCE(INST_NO);
@ -78,3 +80,30 @@ bool serial_bytes_available(void) {
void serial_write(const char *text) {
mp_hal_stdout_tx_str(text);
}
#else
#include "tusb.h"
void serial_init(void) {
// usb is already initialized in board_init()
}
bool serial_connected(void) {
return tud_cdc_connected();
}
char serial_read(void) {
return (char) tud_cdc_read_char();
}
bool serial_bytes_available(void) {
return tud_cdc_available() > 0;
}
void serial_write(const char* text) {
tud_cdc_write(text, strlen(text));
}
#endif

View File

@ -51,8 +51,7 @@ void SysTick_Handler(void) {
void tick_init() {
uint32_t ticks_per_ms = common_hal_mcu_processor_get_frequency() / 1000;
SysTick_Config(ticks_per_ms);
// NVIC_EnableIRQ(SysTick_IRQn);
SysTick_Config(ticks_per_ms); // interrupt is enabled
}
void tick_delay(uint32_t us) {

125
ports/nrf/usb/tusb_config.h Normal file
View File

@ -0,0 +1,125 @@
/**************************************************************************/
/*!
@file tusb_config.h
@author hathach (tinyusb.org)
@section LICENSE
Software License Agreement (BSD License)
Copyright (c) 2013, hathach (tinyusb.org)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holders 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 ''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 HOLDER 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 _TUSB_CONFIG_H_
#define _TUSB_CONFIG_H_
#ifdef __cplusplus
extern "C" {
#endif
//--------------------------------------------------------------------+
// COMMON CONFIGURATION
//--------------------------------------------------------------------+
#define CFG_TUSB_MCU OPT_MCU_NRF5X
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE
#define CFG_TUSB_DEBUG 0
/*------------- RTOS -------------*/
#define CFG_TUSB_OS OPT_OS_NONE
//#define CFG_TUD_TASK_QUEUE_SZ 16
//#define CFG_TUD_TASK_PRIO 0
//#define CFG_TUD_TASK_STACK_SZ 150
//--------------------------------------------------------------------+
// DEVICE CONFIGURATION
//--------------------------------------------------------------------+
/*------------- Core -------------*/
#define CFG_TUD_DESC_AUTO 1
#define CFG_TUD_DESC_VID 0x239A
#define CFG_TUD_DESC_PID 0x802A
#define CFG_TUD_ENDOINT0_SIZE 64
//------------- CLASS -------------//
#define CFG_TUD_CDC 1
#define CFG_TUD_MSC 1
#define CFG_TUD_HID_KEYBOARD 0
#define CFG_TUD_HID_MOUSE 0
#define CFG_TUD_HID_GENERIC 0
/*------------------------------------------------------------------*/
/* CLASS DRIVER
*------------------------------------------------------------------*/
// FIFO size of CDC TX and RX
#define CFG_TUD_CDC_RX_BUFSIZE 1024
#define CFG_TUD_CDC_TX_BUFSIZE 1024
/* TX is sent automatically on every Start of Frame event ~ 1ms.
* If not enabled, application must call tud_cdc_flush() periodically
* Note: Enabled this could overflow device task, if it does, define
* CFG_TUD_TASK_QUEUE_SZ with large value
*/
#define CFG_TUD_CDC_FLUSH_ON_SOF 0
// Number of supported Logical Unit Number (At least 1)
#define CFG_TUD_MSC_MAXLUN 1
// Number of Blocks
#define CFG_TUD_MSC_BLOCK_NUM (256*1024)/512
// Block size
#define CFG_TUD_MSC_BLOCK_SZ 512
// Buffer size for each read/write transfer, the more the better
#define CFG_TUD_MSC_BUFSIZE (4*1024)
// Vendor name included in Inquiry response, max 8 bytes
#define CFG_TUD_MSC_VENDOR "Adafruit"
// Product name included in Inquiry response, max 16 bytes
#define CFG_TUD_MSC_PRODUCT "CircuitPY nRF52"
// Product revision string included in Inquiry response, max 4 bytes
#define CFG_TUD_MSC_PRODUCT_REV "1.0"
//--------------------------------------------------------------------+
// USB RAM PLACEMENT
//--------------------------------------------------------------------+
#define CFG_TUSB_ATTR_USBRAM
#define CFG_TUSB_MEM_ALIGN ATTR_ALIGNED(4)
#ifdef __cplusplus
}
#endif
#endif /* _TUSB_CONFIG_H_ */

View File

@ -0,0 +1,78 @@
/**************************************************************************/
/*!
@file tusb_descriptors.c
@author hathach (tinyusb.org)
@section LICENSE
Software License Agreement (BSD License)
Copyright (c) 2013, hathach (tinyusb.org)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holders 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 ''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 HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This file is part of the tinyusb stack.
*/
/**************************************************************************/
#include "tusb.h"
//--------------------------------------------------------------------+
// STRING DESCRIPTORS
//--------------------------------------------------------------------+
// array of pointer to string descriptors
uint16_t const * const string_desc_arr [] =
{
// 0 index is supported language = English
TUD_DESC_STRCONV(0x0409),
// Manufacturer
TUD_DESC_STRCONV('A','d','a','f','r','u','i','t',' ','I','n','d','u','s','t','r','i','e','s'),
// Product
TUD_DESC_STRCONV('C','i','r','c','u','i','t','P','Y',' ','n','R','F','5','2'),
// Serials TODO use chip ID
TUD_DESC_STRCONV('1', '2', '3', '4', '5'),
// CDC Interface
TUD_DESC_STRCONV('B','l','u','e','f','r','u','i','t',' ','S','e','r','i','a','l'),
// MSC Interface
TUD_DESC_STRCONV('B','l','u','e','f','r','u','i','t',' ','S','t','o','r','a','g','e'),
// Custom Interface
TUD_DESC_STRCONV('B','l','u','e','f','r','u','i','t',' ','C','u','s','t','o','m')
};
// tud_desc_set is required by tinyusb stack
// since CFG_TUD_DESC_AUTO is enabled, we only need to set string_arr
tud_desc_set_t tud_desc_set =
{
.device = NULL,
.config = NULL,
.string_arr = (uint8_t const **) string_desc_arr,
.hid_report = NULL
};

View File

@ -0,0 +1,176 @@
/**************************************************************************/
/*!
@file usb_msc_flash.c
@author hathach (tinyusb.org)
@section LICENSE
Software License Agreement (BSD License)
Copyright (c) 2018, Adafruit Industries (adafruit.com)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holders 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 ''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 HOLDER 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 "tusb.h"
#include "internal_flash.h"
// For updating fatfs's cache
#include "extmod/vfs.h"
#include "extmod/vfs_fat.h"
#include "lib/oofatfs/ff.h"
#include "py/mpstate.h"
/*------------------------------------------------------------------*/
/* MACRO TYPEDEF CONSTANT ENUM
*------------------------------------------------------------------*/
#define MSC_FLASH_ADDR_END 0xED000
#define MSC_FLASH_SIZE (256*1024)
#define MSC_FLASH_ADDR_START (MSC_FLASH_ADDR_END-MSC_FLASH_SIZE)
#define MSC_FLASH_BLOCK_SIZE 512
#define FL_PAGE_SZ 4096
static scsi_sense_fixed_data_t mscd_sense_data =
{
.response_code = 0x70,
.sense_key = 0, // no errors
.additional_sense_len = sizeof(scsi_sense_fixed_data_t) - 8
};
static scsi_mode_parameters_t const msc_dev_mode_para =
{
.mode_data_length = 3,
.medium_type = 0,
.device_specific_para = 0,
.block_descriptor_length = 0
};
//--------------------------------------------------------------------+
// tinyusb callbacks
//--------------------------------------------------------------------+
int32_t tud_msc_scsi_cb (uint8_t rhport, uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, uint16_t bufsize) {
// read10 & write10 has their own callback and MUST not be handled here
void const* ptr = NULL;
uint16_t len = 0;
// most scsi handled is input
bool in_xfer = true;
switch ( scsi_cmd[0] )
{
case SCSI_CMD_REQUEST_SENSE:
ptr = &mscd_sense_data;
len = sizeof(scsi_sense_fixed_data_t);
break;
case SCSI_CMD_MODE_SENSE_6:
ptr = &msc_dev_mode_para;
len = sizeof(msc_dev_mode_para);
break;
case SCSI_CMD_TEST_UNIT_READY:
ptr = NULL;
len = 0;
break;
case SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL:
ptr = NULL;
len = 0;
break;
default:
// negative is error -> Data stage is STALL, status = failed
return -1;
}
// return len must not larger than bufsize
TU_ASSERT(bufsize >= len);
if ( ptr && len )
{
if ( in_xfer )
{
memcpy(buffer, ptr, len);
} else
{
// SCSI output
}
}
//------------- clear sense data if it is not request sense command -------------//
if ( SCSI_CMD_REQUEST_SENSE != scsi_cmd[0] )
{
mscd_sense_data.sense_key = SCSI_SENSEKEY_NONE;
mscd_sense_data.additional_sense_code = 0;
mscd_sense_data.additional_sense_qualifier = 0;
}
return len;
}
/*------------------------------------------------------------------*/
/* Tinyusb Flash READ10 & WRITE10
*------------------------------------------------------------------*/
int32_t tud_msc_read10_cb (uint8_t rhport, uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize) {
(void) rhport;
(void) lun;
(void) offset;
uint32_t const block_count = bufsize / MSC_FLASH_BLOCK_SIZE;
internal_flash_read_blocks(buffer, lba, block_count);
return block_count * MSC_FLASH_BLOCK_SIZE;
}
int32_t tud_msc_write10_cb (uint8_t rhport, uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize) {
(void) rhport;
(void) lun;
(void) offset;
uint32_t const block_count = bufsize / MSC_FLASH_BLOCK_SIZE;
// bufsize <= CFG_TUD_MSC_BUFSIZE (4096)
internal_flash_write_blocks(buffer, lba, block_count);
// update fatfs's cache if address matches
fs_user_mount_t* vfs = MP_STATE_VM(vfs_mount_table)->obj;
if ( (lba <= vfs->fatfs.winsect) && (vfs->fatfs.winsect <= (lba + bufsize / MSC_FLASH_BLOCK_SIZE)) ) {
memcpy(vfs->fatfs.win, buffer + MSC_FLASH_BLOCK_SIZE * (vfs->fatfs.winsect - lba), MSC_FLASH_BLOCK_SIZE);
}
return block_count * MSC_FLASH_BLOCK_SIZE;
}
void tud_msc_write10_complete_cb (uint8_t rhport, uint8_t lun) {
(void) rhport;
(void) lun;
// flush pending cache when write10 is complete
internal_flash_flush();
}

View File

@ -1049,7 +1049,8 @@ void mp_convert_member_lookup(mp_obj_t self, const mp_obj_type_t *type, mp_obj_t
dest[1] = self;
}
#if MICROPY_PY_BUILTINS_PROPERTY
} else if (MP_OBJ_IS_TYPE(member, &mp_type_property) && mp_obj_is_native_type(type)) {
// If self is MP_OBJ_NULL, we looking at the class itself, not an instance.
} else if (MP_OBJ_IS_TYPE(member, &mp_type_property) && mp_obj_is_native_type(type) && self != MP_OBJ_NULL) {
// object member is a property; delegate the load to the property
// Note: This is an optimisation for code size and execution time.
// The proper way to do it is have the functionality just below
@ -1166,7 +1167,8 @@ void mp_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) {
assert(type->locals_dict->base.type == &mp_type_dict); // Micro Python 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(attr), MP_MAP_LOOKUP);
if (elem != NULL && MP_OBJ_IS_TYPE(elem->value, &mp_type_property)) {
// If base is MP_OBJ_NULL, we looking at the class itself, not an instance.
if (elem != NULL && MP_OBJ_IS_TYPE(elem->value, &mp_type_property) && base != MP_OBJ_NULL) {
// attribute exists and is a property; delegate the store/delete
// Note: This is an optimisation for code size and execution time.
// The proper way to do it is have the functionality just below in

View File

@ -0,0 +1,115 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2018 Artur Pacholec
*
* 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/objproperty.h"
#include "shared-bindings/bleio/Adapter.h"
//| .. currentmodule:: bleio
//|
//| :class:`Adapter` --- BLE adapter information
//| ----------------------------------------------------
//|
//| Get current status of the BLE adapter
//|
//| Usage::
//|
//| import bleio
//| bleio.adapter.enabled = True
//| print(bleio.adapter.address)
//|
//| .. class:: Adapter()
//|
//| You cannot create an instance of `bleio.Adapter`.
//| Use `bleio.adapter` to access the sole instance available.
//|
//| .. attribute:: adapter.enabled
//|
//| State of the BLE adapter.
//|
//| .. attribute:: adapter.address
//|
//| MAC address of the BLE adapter. (read-only)
//|
#define BLE_ADDRESS_LEN 17
STATIC mp_obj_t bleio_adapter_get_enabled(mp_obj_t self) {
return mp_obj_new_bool(common_hal_bleio_adapter_get_enabled());
}
MP_DEFINE_CONST_FUN_OBJ_1(bleio_adapter_get_enabled_obj, bleio_adapter_get_enabled);
static mp_obj_t bleio_adapter_set_enabled(mp_obj_t self, mp_obj_t value) {
const bool enabled = mp_obj_is_true(value);
common_hal_bleio_adapter_set_enabled(enabled);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(bleio_adapter_set_enabled_obj, bleio_adapter_set_enabled);
const mp_obj_property_t bleio_adapter_enabled_obj = {
.base.type = &mp_type_property,
.proxy = { (mp_obj_t)&bleio_adapter_get_enabled_obj,
(mp_obj_t)&bleio_adapter_set_enabled_obj,
(mp_obj_t)&mp_const_none_obj },
};
STATIC mp_obj_t bleio_adapter_get_address(mp_obj_t self) {
vstr_t vstr;
vstr_init(&vstr, BLE_ADDRESS_LEN);
common_hal_bleio_adapter_get_address(&vstr);
const mp_obj_t mac_str = mp_obj_new_str(vstr.buf, vstr.len, false);
vstr_clear(&vstr);
return mac_str;
}
MP_DEFINE_CONST_FUN_OBJ_1(bleio_adapter_get_address_obj, bleio_adapter_get_address);
const mp_obj_property_t bleio_adapter_address_obj = {
.base.type = &mp_type_property,
.proxy = { (mp_obj_t)&bleio_adapter_get_address_obj,
(mp_obj_t)&mp_const_none_obj,
(mp_obj_t)&mp_const_none_obj },
};
STATIC const mp_rom_map_elem_t bleio_adapter_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_enabled), MP_ROM_PTR(&bleio_adapter_enabled_obj) },
{ MP_ROM_QSTR(MP_QSTR_address), MP_ROM_PTR(&bleio_adapter_address_obj) },
};
STATIC MP_DEFINE_CONST_DICT(bleio_adapter_locals_dict, bleio_adapter_locals_dict_table);
const mp_obj_type_t bleio_adapter_type = {
.base = { &mp_type_type },
.name = MP_QSTR_Adapter,
.locals_dict = (mp_obj_t)&bleio_adapter_locals_dict,
};

View File

@ -0,0 +1,38 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2018 Artur Pacholec
*
* 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_SHARED_BINDINGS_BLEIO_ADAPTER_H
#define MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_ADAPTER_H
#include "py/obj.h"
const mp_obj_type_t bleio_adapter_type;
extern bool common_hal_bleio_adapter_get_enabled(void);
extern void common_hal_bleio_adapter_set_enabled(bool enabled);
extern void common_hal_bleio_adapter_get_address(vstr_t *address);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_ADAPTER_H

View File

@ -0,0 +1,64 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2016 Glenn Ruben Bakke
* Copyright (c) 2018 Artur Pacholec
*
* 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/obj.h"
#include "shared-bindings/bleio/__init__.h"
//| :mod:`bleio` --- Bluetooth Low Energy functionality
//| ================================================================
//|
//| .. module:: bleio
//| :synopsis: Bluetooth Low Energy functionality
//| :platform: nRF
//|
//| The `bleio` module contains methods for managing the BLE adapter.
//|
//| Libraries
//|
//| .. toctree::
//| :maxdepth: 3
//|
//| Adapter
//|
//| .. attribute:: adapter
//|
//| BLE Adapter information, such as enabled state as well as MAC
//| address.
//| This object is the sole instance of `bleio.Adapter`.
//|
STATIC const mp_rom_map_elem_t bleio_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_bleio) },
{ MP_ROM_QSTR(MP_QSTR_adapter), MP_ROM_PTR(&common_hal_bleio_adapter_obj) },
};
STATIC MP_DEFINE_CONST_DICT(bleio_module_globals, bleio_module_globals_table);
const mp_obj_module_t bleio_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&bleio_module_globals,
};

View File

@ -0,0 +1,35 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2016 Glenn Ruben Bakke
* Copyright (c) 2018 Artur Pacholec
*
* 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_SHARED_BINDINGS_BLEIO___INIT___H
#define MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO___INIT___H
#include "common-hal/bleio/Adapter.h"
extern const super_adapter_obj_t common_hal_bleio_adapter_obj;
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO___INIT___H

View File

@ -29,6 +29,7 @@ Module Supported Ports
`binascii` **ESP8266**
`bitbangio` **SAMD Express, ESP8266**
`board` **All Supported**
`bleio` **nRF**
`busio` **All Supported**
`digitalio` **All Supported**
`gamepad` **SAMD Express, nRF**

View File

@ -49,6 +49,9 @@ void shared_module_bitbangio_onewire_deinit(bitbangio_onewire_obj_t* self) {
common_hal_digitalio_digitalinout_deinit(&self->pin);
}
// We use common_hal_mcu_delay_us(). It should not be dependent on interrupts
// to do accurate timekeeping, since we disable interrupts during the delays below.
bool shared_module_bitbangio_onewire_reset(bitbangio_onewire_obj_t* self) {
common_hal_mcu_disable_interrupts();
common_hal_digitalio_digitalinout_switch_to_output(&self->pin, false, DRIVE_MODE_OPEN_DRAIN);

@ -1 +1 @@
Subproject commit 449f26673855736dbc0fc3f18d72403f25f62974
Subproject commit 6c37e72342d814079bc9b760e2840dc0d766f94f