From 526925f1c61f7be74c7c4a808d98e802204ba589 Mon Sep 17 00:00:00 2001 From: Kamil Tomaszewski Date: Wed, 9 Oct 2019 08:27:08 +0200 Subject: Add Spresense board --- ports/spresense/Makefile | 186 +++++++++++++++++++ ports/spresense/README.md | 86 +++++++++ ports/spresense/alloca.h | 6 + ports/spresense/background.c | 53 ++++++ ports/spresense/background.h | 33 ++++ ports/spresense/common-hal/analogio/AnalogIn.c | 131 ++++++++++++++ ports/spresense/common-hal/analogio/AnalogIn.h | 42 +++++ ports/spresense/common-hal/analogio/AnalogOut.c | 43 +++++ ports/spresense/common-hal/analogio/AnalogOut.h | 36 ++++ ports/spresense/common-hal/analogio/__init__.c | 1 + ports/spresense/common-hal/board/__init__.c | 80 +++++++++ ports/spresense/common-hal/busio/I2C.c | 123 +++++++++++++ ports/spresense/common-hal/busio/I2C.h | 43 +++++ ports/spresense/common-hal/busio/OneWire.h | 33 ++++ ports/spresense/common-hal/busio/SPI.c | 155 ++++++++++++++++ ports/spresense/common-hal/busio/SPI.h | 49 ++++++ ports/spresense/common-hal/busio/UART.c | 168 ++++++++++++++++++ ports/spresense/common-hal/busio/UART.h | 43 +++++ ports/spresense/common-hal/busio/__init__.c | 1 + .../spresense/common-hal/digitalio/DigitalInOut.c | 136 ++++++++++++++ .../spresense/common-hal/digitalio/DigitalInOut.h | 42 +++++ ports/spresense/common-hal/digitalio/__init__.c | 1 + ports/spresense/common-hal/microcontroller/Pin.c | 162 +++++++++++++++++ ports/spresense/common-hal/microcontroller/Pin.h | 92 ++++++++++ .../common-hal/microcontroller/Processor.c | 41 +++++ .../common-hal/microcontroller/Processor.h | 40 +++++ .../common-hal/microcontroller/__init__.c | 111 ++++++++++++ ports/spresense/common-hal/os/__init__.c | 72 ++++++++ ports/spresense/common-hal/pulseio/PWMOut.c | 157 +++++++++++++++++ ports/spresense/common-hal/pulseio/PWMOut.h | 48 +++++ ports/spresense/common-hal/pulseio/PulseIn.c | 196 +++++++++++++++++++++ ports/spresense/common-hal/pulseio/PulseIn.h | 47 +++++ ports/spresense/common-hal/pulseio/PulseOut.c | 126 +++++++++++++ ports/spresense/common-hal/pulseio/PulseOut.h | 39 ++++ ports/spresense/common-hal/pulseio/__init__.c | 1 + ports/spresense/common-hal/rtc/RTC.c | 54 ++++++ ports/spresense/common-hal/rtc/RTC.h | 30 ++++ ports/spresense/common-hal/rtc/__init__.c | 1 + ports/spresense/common-hal/supervisor/Runtime.c | 36 ++++ ports/spresense/common-hal/supervisor/Runtime.h | 36 ++++ ports/spresense/common-hal/supervisor/__init__.c | 36 ++++ ports/spresense/common-hal/time/__init__.c | 37 ++++ ports/spresense/fatfs_port.c | 46 +++++ ports/spresense/mpconfigboard.h | 39 ++++ ports/spresense/mpconfigport.h | 38 ++++ ports/spresense/mpconfigport.mk | 24 +++ ports/spresense/mphalport.c | 88 +++++++++ ports/spresense/mphalport.h | 36 ++++ ports/spresense/qstrdefsport.h | 1 + ports/spresense/supervisor/cpu.s | 27 +++ ports/spresense/supervisor/internal_flash.c | 114 ++++++++++++ ports/spresense/supervisor/internal_flash.h | 30 ++++ .../supervisor/internal_flash_root_pointers.h | 31 ++++ ports/spresense/supervisor/port.c | 73 ++++++++ ports/spresense/supervisor/usb.c | 30 ++++ ports/spresense/tick.c | 45 +++++ ports/spresense/tick.h | 34 ++++ 57 files changed, 3509 insertions(+) create mode 100644 ports/spresense/Makefile create mode 100644 ports/spresense/README.md create mode 100644 ports/spresense/alloca.h create mode 100644 ports/spresense/background.c create mode 100644 ports/spresense/background.h create mode 100644 ports/spresense/common-hal/analogio/AnalogIn.c create mode 100644 ports/spresense/common-hal/analogio/AnalogIn.h create mode 100644 ports/spresense/common-hal/analogio/AnalogOut.c create mode 100644 ports/spresense/common-hal/analogio/AnalogOut.h create mode 100644 ports/spresense/common-hal/analogio/__init__.c create mode 100644 ports/spresense/common-hal/board/__init__.c create mode 100644 ports/spresense/common-hal/busio/I2C.c create mode 100644 ports/spresense/common-hal/busio/I2C.h create mode 100644 ports/spresense/common-hal/busio/OneWire.h create mode 100644 ports/spresense/common-hal/busio/SPI.c create mode 100644 ports/spresense/common-hal/busio/SPI.h create mode 100644 ports/spresense/common-hal/busio/UART.c create mode 100644 ports/spresense/common-hal/busio/UART.h create mode 100644 ports/spresense/common-hal/busio/__init__.c create mode 100644 ports/spresense/common-hal/digitalio/DigitalInOut.c create mode 100644 ports/spresense/common-hal/digitalio/DigitalInOut.h create mode 100644 ports/spresense/common-hal/digitalio/__init__.c create mode 100644 ports/spresense/common-hal/microcontroller/Pin.c create mode 100644 ports/spresense/common-hal/microcontroller/Pin.h create mode 100644 ports/spresense/common-hal/microcontroller/Processor.c create mode 100644 ports/spresense/common-hal/microcontroller/Processor.h create mode 100644 ports/spresense/common-hal/microcontroller/__init__.c create mode 100644 ports/spresense/common-hal/os/__init__.c create mode 100644 ports/spresense/common-hal/pulseio/PWMOut.c create mode 100644 ports/spresense/common-hal/pulseio/PWMOut.h create mode 100644 ports/spresense/common-hal/pulseio/PulseIn.c create mode 100644 ports/spresense/common-hal/pulseio/PulseIn.h create mode 100644 ports/spresense/common-hal/pulseio/PulseOut.c create mode 100644 ports/spresense/common-hal/pulseio/PulseOut.h create mode 100644 ports/spresense/common-hal/pulseio/__init__.c create mode 100644 ports/spresense/common-hal/rtc/RTC.c create mode 100644 ports/spresense/common-hal/rtc/RTC.h create mode 100644 ports/spresense/common-hal/rtc/__init__.c create mode 100755 ports/spresense/common-hal/supervisor/Runtime.c create mode 100755 ports/spresense/common-hal/supervisor/Runtime.h create mode 100755 ports/spresense/common-hal/supervisor/__init__.c create mode 100644 ports/spresense/common-hal/time/__init__.c create mode 100644 ports/spresense/fatfs_port.c create mode 100644 ports/spresense/mpconfigboard.h create mode 100644 ports/spresense/mpconfigport.h create mode 100644 ports/spresense/mpconfigport.mk create mode 100644 ports/spresense/mphalport.c create mode 100644 ports/spresense/mphalport.h create mode 100644 ports/spresense/qstrdefsport.h create mode 100755 ports/spresense/supervisor/cpu.s create mode 100644 ports/spresense/supervisor/internal_flash.c create mode 100644 ports/spresense/supervisor/internal_flash.h create mode 100644 ports/spresense/supervisor/internal_flash_root_pointers.h create mode 100644 ports/spresense/supervisor/port.c create mode 100644 ports/spresense/supervisor/usb.c create mode 100644 ports/spresense/tick.c create mode 100644 ports/spresense/tick.h diff --git a/ports/spresense/Makefile b/ports/spresense/Makefile new file mode 100644 index 000000000..d8f809635 --- /dev/null +++ b/ports/spresense/Makefile @@ -0,0 +1,186 @@ +# This file is part of the MicroPython project, http://micropython.org/ +# +# The MIT License (MIT) +# +# Copyright 2019 Sony Semiconductor Solutions 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 ../../py/mkenv.mk + +# Port-specific +include mpconfigport.mk + +# CircuitPython-specific +include $(TOP)/py/circuitpy_mpconfig.mk + +# qstr definitions (must come before including py.mk) +QSTR_DEFS = qstrdefsport.h + +# include py core make definitions +include $(TOP)/py/py.mk + +include $(TOP)/supervisor/supervisor.mk + +# Include make rules and variables common across CircuitPython builds. +include $(TOP)/py/circuitpy_defns.mk + +CROSS_COMPILE = arm-none-eabi- + +SPRESENSE_SDK = spresense-exported-sdk + +FIRMWARE = $(SPRESENSE_SDK)/firmware + +INC += \ + -I. \ + -I../.. \ + -I../lib/mp-readline \ + -I../lib/timeutils \ + -I../../lib/tinyusb/src \ + -I../../supervisor/shared/usb \ + -I$(BUILD) \ + -I$(SPRESENSE_SDK)/nuttx/include \ + -I$(SPRESENSE_SDK)/nuttx/arch \ + -I$(SPRESENSE_SDK)/nuttx/arch/chip \ + -I$(SPRESENSE_SDK)/sdk/bsp/include \ + -I$(SPRESENSE_SDK)/sdk/bsp/include/sdk \ + +CFLAGS += \ + $(INC) \ + -DCONFIG_WCHAR_BUILTIN \ + -DCONFIG_HAVE_DOUBLE \ + -Dmain=spresense_main \ + -D_estack=__stack \ + -c \ + -Os \ + -pipe \ + -std=gnu11 \ + -mcpu=cortex-m4 \ + -mthumb \ + -mfpu=fpv4-sp-d16 \ + -mfloat-abi=hard \ + -mabi=aapcs \ + -fno-builtin \ + -fno-strict-aliasing \ + -fno-strength-reduce \ + -fomit-frame-pointer \ + -ffunction-sections \ + -fdata-sections \ + -Wall \ + +LIBM = "${shell "$(CC)" $(CFLAGS) -print-file-name=libm.a}" + +LIBGCC = "${shell "$(CC)" $(CFLAGS) -print-libgcc-file-name}" + +LDFLAGS = \ + --entry=__start \ + -nostartfiles \ + -nodefaultlibs \ + --defsym __stack=_vectors+786432 \ + -T$(SPRESENSE_SDK)/nuttx/build/ramconfig.ld \ + --gc-sections \ + -Map=$(BUILD)/output.map \ + -o $(BUILD)/circuitpython.elf \ + --start-group \ + -u spresense_main \ + $(BUILD)/libmpy.a \ + $(SPRESENSE_SDK)/sdk/libs/libapps.a \ + $(SPRESENSE_SDK)/sdk/libs/libsdk.a \ + $(LIBM) \ + $(LIBGCC) \ + --end-group \ + -L$(BUILD) \ + +CFLAGS += -DCFG_TUSB_MCU=OPT_MCU_CXD56 -DCFG_TUD_MIDI_RX_BUFSIZE=128 -DCFG_TUD_CDC_RX_BUFSIZE=256 -DCFG_TUD_MIDI_TX_BUFSIZE=128 -DCFG_TUD_CDC_TX_BUFSIZE=256 -DCFG_TUD_MSC_BUFSIZE=512 $(CFLAGS_MOD) + +SRC_COMMON_HAL_EXPANDED = $(addprefix shared-bindings/, $(SRC_COMMON_HAL)) \ + $(addprefix shared-bindings/, $(SRC_BINDINGS_ENUMS)) \ + $(addprefix common-hal/, $(SRC_COMMON_HAL)) + +SRC_SHARED_MODULE_EXPANDED = $(addprefix shared-bindings/, $(SRC_SHARED_MODULE)) \ + $(addprefix shared-module/, $(SRC_SHARED_MODULE)) + +SRC_S = supervisor/cpu.s + +SRC_C = \ + tick.c \ + background.c \ + fatfs_port.c \ + mphalport.c \ + lib/utils/stdout_helpers.c \ + lib/utils/pyexec.c \ + lib/libc/string0.c \ + lib/mp-readline/readline.c \ + lib/timeutils/timeutils.c \ + lib/oofatfs/ff.c \ + lib/oofatfs/option/ccsbcs.c \ + lib/utils/interrupt_char.c \ + lib/utils/sys_stdio_mphal.c \ + lib/utils/context_manager_helpers.c \ + lib/utils/buffer_helper.c \ + supervisor/shared/memory.c \ + lib/tinyusb/src/portable/sony/cxd56/dcd_cxd56.c \ + +OBJ = $(PY_O) $(SUPERVISOR_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) +OBJ += $(addprefix $(BUILD)/, $(SRC_S:.s=.o)) +OBJ += $(addprefix $(BUILD)/, $(SRC_COMMON_HAL_EXPANDED:.c=.o)) +OBJ += $(addprefix $(BUILD)/, $(SRC_SHARED_MODULE_EXPANDED:.c=.o)) + +# List of sources for qstr extraction +SRC_QSTR += $(SRC_C) $(SRC_SUPERVISOR) $(SRC_COMMON_HAL_EXPANDED) $(SRC_SHARED_MODULE_EXPANDED) +# Sources that only hold QSTRs after pre-processing. +SRC_QSTR_PREPROCESSOR += + +all: $(BUILD)/circuitpython.spk + +$(FIRMWARE): + $(ECHO) "" + $(ECHO) "Download the spresense binaries zip archive from:" + $(ECHO) "https://developer.sony.com/file/download/download-spresense-firmware-v1-4-000" + $(ECHO) "Extract spresense binaries to $(FIRMWARE)" + $(ECHO) "" + $(ECHO) "run make flash-bootloader again to flash bootloader." + exit 1 + +$(BUILD)/libmpy.a: $(SPRESENSE_SDK) $(OBJ) + $(ECHO) "AR $@" + $(Q)$(AR) rcs $(BUILD)/libmpy.a $(OBJ) + +$(BUILD)/circuitpython.elf: $(BUILD)/libmpy.a + $(ECHO) "LD $@" + $(Q)$(LD) $(LDFLAGS) + +$(BUILD)/circuitpython.spk: $(BUILD)/circuitpython.elf + $(ECHO) "Creating $@" + $(SPRESENSE_SDK)/sdk/tools/linux/mkspk -c 2 $(BUILD)/circuitpython.elf nuttx $(BUILD)/circuitpython.spk + +flash: $(BUILD)/circuitpython.spk + $(ECHO) "Writing $< to the board" + $(SPRESENSE_SDK)/sdk/tools/flash.sh -c /dev/ttyUSB0 $(BUILD)/circuitpython.spk + +flash-bootloader: $(SPRESENSE_SDK) $(FIRMWARE) + $(ECHO) "Writing loader to the board" + $(SPRESENSE_SDK)/sdk/tools/flash.sh -l $(FIRMWARE) -c /dev/ttyUSB0 + +include $(TOP)/py/mkrules.mk + +# Print out the value of a make variable. +# https://stackoverflow.com/questions/16467718/how-to-print-out-a-variable-in-makefile +print-%: + @echo $* = $($*) diff --git a/ports/spresense/README.md b/ports/spresense/README.md new file mode 100644 index 000000000..af4a6dea1 --- /dev/null +++ b/ports/spresense/README.md @@ -0,0 +1,86 @@ +CircuitPython port to Spresense +============================== + +This directory contains the port of CircuitPython to Spresense. It is a compact +development board based on Sony’s power-efficient multicore microcontroller +CXD5602. + +Board features: + +* Integrated GPS + * The embedded GNSS with support for GPS, QZSS and GLONASS enables applications + where tracking is required. +* Hi-res audio output and multi mic inputs + * Advanced 192kHz/24 bit audio codec and amplifier for audio output, and + support for up to 8 mic input channels. +* Multicore microcontroller + * Spresense is powered by Sony's CXD5602 microcontroller (ARM® Cortex®-M4F × 6 + cores), with a clock speed of 156 MHz. + +Currently, Spresense port does not support GNSS, Audio and Multicore. + +Refer to [developer.sony.com/develop/spresense/](https://developer.sony.com/develop/spresense/) +for further information about this board. + +Build instructions +------------------ + +Pull all submodules into your clone: + + $ git submodule update --init --recursive + +Build the MicroPython cross-compiler: + + $ make -C mpy-cross + +Change directory to spresense: + + $ cd ports/spresense + +To build circuitpython image run: + + $ make + +USB connection +-------------- + +Add user to `dialout` group: + + $ sudo usermod -a -G dialout + +Connect the Spresense main board to the PC via the USB cable. + +Flash the bootloader +-------------------- + +The correct bootloader is required for the Spresense board to function. + +Bootloader information: + +* The bootloader has to be flashed the very first time the board is used. + +* You have to accept the End User License Agreement to be able to download and use the Spresense bootloader binary. + +Download the spresense binaries zip archive from: + +Extract spresense binaries in your PC to ports/spresense/spresense-exported-sdk/firmware/ + +To flash the bootloader run the command: + + $ make flash-bootloader + +Flash the circuitpython.spk image +--------------------------------- + +To flash the firmware run the command: + + $ make flash + +Accessing the board +------------------- + +Connect the Spresense extension board to the PC via the USB cable. + +Once built and deployed, access the CircuitPython REPL (the Python prompt) via USB. You can run: + + $ screen /dev/ttyACM0 115200 diff --git a/ports/spresense/alloca.h b/ports/spresense/alloca.h new file mode 100644 index 000000000..aad2f8b5b --- /dev/null +++ b/ports/spresense/alloca.h @@ -0,0 +1,6 @@ +#ifndef _ALLOCA_H +#define _ALLOCA_H + +#define alloca __builtin_alloca + +#endif /* _ALLOCA_H */ diff --git a/ports/spresense/background.c b/ports/spresense/background.c new file mode 100644 index 000000000..ade257dd2 --- /dev/null +++ b/ports/spresense/background.c @@ -0,0 +1,53 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 "background.h" + +#include "supervisor/usb.h" +#include "supervisor/filesystem.h" +#include "supervisor/shared/stack.h" + +static bool running_background_tasks = false; + +void background_tasks_reset(void) { + running_background_tasks = false; +} + +void run_background_tasks(void) { + // Don't call ourselves recursively. + if (running_background_tasks) { + return; + } + + assert_heap_ok(); + running_background_tasks = true; + + usb_background(); + filesystem_background(); + + running_background_tasks = false; + assert_heap_ok(); +} diff --git a/ports/spresense/background.h b/ports/spresense/background.h new file mode 100644 index 000000000..39e7c4fc8 --- /dev/null +++ b/ports/spresense/background.h @@ -0,0 +1,33 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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. + */ + +#ifndef MICROPY_INCLUDED_SPRESENSE_BACKGROUND_H +#define MICROPY_INCLUDED_SPRESENSE_BACKGROUND_H + +void background_tasks_reset(void); +void run_background_tasks(void); + +#endif // MICROPY_INCLUDED_SPRESENSE_BACKGROUND_H diff --git a/ports/spresense/common-hal/analogio/AnalogIn.c b/ports/spresense/common-hal/analogio/AnalogIn.c new file mode 100644 index 000000000..438fbbcae --- /dev/null +++ b/ports/spresense/common-hal/analogio/AnalogIn.c @@ -0,0 +1,131 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 + +#include +#include + +#include "py/runtime.h" + +#include "shared-bindings/analogio/AnalogIn.h" + +typedef struct { + const char* devpath; + const mcu_pin_obj_t *pin; + int fd; +} analogin_dev_t; + +STATIC analogin_dev_t analogin_dev[] = { + {"/dev/lpadc0", &pin_A0, -1}, + {"/dev/lpadc1", &pin_A1, -1}, + {"/dev/lpadc2", &pin_A2, -1}, + {"/dev/lpadc3", &pin_A3, -1}, + {"/dev/hpadc0", &pin_A4, -1}, + {"/dev/hpadc1", &pin_A5, -1}, +}; + +void common_hal_analogio_analogin_construct(analogio_analogin_obj_t *self, const mcu_pin_obj_t *pin) { + if (!pin->analog) { + mp_raise_ValueError(translate("AnalogIn not supported on given pin")); + } + + self->number = -1; + + for (int i = 0; i < MP_ARRAY_SIZE(analogin_dev); i++) { + if (pin->number == analogin_dev[i].pin->number) { + self->number = i; + break; + } + } + + if (self->number < 0) { + mp_raise_ValueError(translate("Pin does not have ADC capabilities")); + } + + analogin_dev[self->number].fd = open(analogin_dev[pin->number].devpath, O_RDONLY); + if (analogin_dev[self->number].fd < 0) { + mp_raise_ValueError(translate("Pin does not have ADC capabilities")); + } + + // SCU FIFO overwrite + ioctl(analogin_dev[self->number].fd, SCUIOC_SETFIFOMODE, 1); + + // ADC FIFO size + ioctl(analogin_dev[self->number].fd, ANIOC_CXD56_FIFOSIZE, 2); + + // start ADC + ioctl(analogin_dev[self->number].fd, ANIOC_CXD56_START, 0); + + self->pin = pin; +} + +void common_hal_analogio_analogin_deinit(analogio_analogin_obj_t *self) { + if (common_hal_analogio_analogin_deinited(self)) { + return; + } + + // stop ADC + ioctl(analogin_dev[self->number].fd, ANIOC_CXD56_STOP, 0); + close(analogin_dev[self->number].fd); + analogin_dev[self->number].fd = -1; + + self->pin = mp_const_none; +} + +bool common_hal_analogio_analogin_deinited(analogio_analogin_obj_t *self) { + return self->pin == mp_const_none; +} + +uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) { + uint16_t value = 0; + + read(analogin_dev[self->number].fd, &value, sizeof(value)); + + return value; +} + +// Reference voltage is a fixed value which is depending on the board. +// e.g.) +// - Reference Voltage of A4 and A5 pins on Main Board is 0.7V. +// - Reference Voltage of A0 ~ A5 pins on External Interface board +// is selected 3.3V or 5.0V by a IO Volt jumper pin. +float common_hal_analogio_analogin_get_reference_voltage(analogio_analogin_obj_t *self) { + return 3.3f; +} + +void analogin_reset(void) { + for (int i = 0; i < MP_ARRAY_SIZE(analogin_dev); i++) { + if (analogin_dev[i].fd >= 0) { + // stop ADC + ioctl(analogin_dev[i].fd, ANIOC_CXD56_STOP, 0); + close(analogin_dev[i].fd); + analogin_dev[i].fd = -1; + } + } +} diff --git a/ports/spresense/common-hal/analogio/AnalogIn.h b/ports/spresense/common-hal/analogio/AnalogIn.h new file mode 100644 index 000000000..7fc197f98 --- /dev/null +++ b/ports/spresense/common-hal/analogio/AnalogIn.h @@ -0,0 +1,42 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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. + */ + +#ifndef MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_ANALOGIO_ANALOGIN_H +#define MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_ANALOGIO_ANALOGIN_H + +#include "py/obj.h" + +#include "common-hal/microcontroller/Pin.h" + +typedef struct { + mp_obj_base_t base; + const mcu_pin_obj_t *pin; + uint8_t number; +} analogio_analogin_obj_t; + +void analogin_reset(void); + +#endif // MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_ANALOGIO_ANALOGIN_H diff --git a/ports/spresense/common-hal/analogio/AnalogOut.c b/ports/spresense/common-hal/analogio/AnalogOut.c new file mode 100644 index 000000000..3f1abe80d --- /dev/null +++ b/ports/spresense/common-hal/analogio/AnalogOut.c @@ -0,0 +1,43 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 "py/runtime.h" + +#include "shared-bindings/analogio/AnalogOut.h" + +void common_hal_analogio_analogout_construct(analogio_analogout_obj_t *self, const mcu_pin_obj_t *pin) { + mp_raise_RuntimeError(translate("AnalogOut functionality not supported")); +} + +void common_hal_analogio_analogout_deinit(analogio_analogout_obj_t *self) { +} + +bool common_hal_analogio_analogout_deinited(analogio_analogout_obj_t *self) { + return true; +} + +void common_hal_analogio_analogout_set_value(analogio_analogout_obj_t *self, uint16_t value) { +} diff --git a/ports/spresense/common-hal/analogio/AnalogOut.h b/ports/spresense/common-hal/analogio/AnalogOut.h new file mode 100644 index 000000000..60d3cf0a1 --- /dev/null +++ b/ports/spresense/common-hal/analogio/AnalogOut.h @@ -0,0 +1,36 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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. + */ + +#ifndef MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_ANALOGIO_ANALOGOUT_H +#define MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_ANALOGIO_ANALOGOUT_H + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; +} analogio_analogout_obj_t; + +#endif // MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_ANALOGIO_ANALOGOUT_H diff --git a/ports/spresense/common-hal/analogio/__init__.c b/ports/spresense/common-hal/analogio/__init__.c new file mode 100644 index 000000000..eea58c77d --- /dev/null +++ b/ports/spresense/common-hal/analogio/__init__.c @@ -0,0 +1 @@ +// No analogio module functions. diff --git a/ports/spresense/common-hal/board/__init__.c b/ports/spresense/common-hal/board/__init__.c new file mode 100644 index 000000000..d123969fe --- /dev/null +++ b/ports/spresense/common-hal/board/__init__.c @@ -0,0 +1,80 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 "shared-bindings/board/__init__.h" + +STATIC const mp_rom_map_elem_t board_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR_D0), MP_ROM_PTR(&pin_D0) }, + { MP_ROM_QSTR(MP_QSTR_D1), MP_ROM_PTR(&pin_D1) }, + { MP_ROM_QSTR(MP_QSTR_D2), MP_ROM_PTR(&pin_D2) }, + { MP_ROM_QSTR(MP_QSTR_D3), MP_ROM_PTR(&pin_D3) }, + { MP_ROM_QSTR(MP_QSTR_D4), MP_ROM_PTR(&pin_D4) }, + { MP_ROM_QSTR(MP_QSTR_D5), MP_ROM_PTR(&pin_D5) }, + { MP_ROM_QSTR(MP_QSTR_D6), MP_ROM_PTR(&pin_D6) }, + { MP_ROM_QSTR(MP_QSTR_D7), MP_ROM_PTR(&pin_D7) }, + { MP_ROM_QSTR(MP_QSTR_D8), MP_ROM_PTR(&pin_D8) }, + { MP_ROM_QSTR(MP_QSTR_D9), MP_ROM_PTR(&pin_D9) }, + { MP_ROM_QSTR(MP_QSTR_D10), MP_ROM_PTR(&pin_D10) }, + { MP_ROM_QSTR(MP_QSTR_D11), MP_ROM_PTR(&pin_D11) }, + { MP_ROM_QSTR(MP_QSTR_D12), MP_ROM_PTR(&pin_D12) }, + { MP_ROM_QSTR(MP_QSTR_D13), MP_ROM_PTR(&pin_D13) }, + { MP_ROM_QSTR(MP_QSTR_D14), MP_ROM_PTR(&pin_D14) }, + { MP_ROM_QSTR(MP_QSTR_D15), MP_ROM_PTR(&pin_D15) }, + { MP_ROM_QSTR(MP_QSTR_D16), MP_ROM_PTR(&pin_D16) }, + { MP_ROM_QSTR(MP_QSTR_D17), MP_ROM_PTR(&pin_D17) }, + { MP_ROM_QSTR(MP_QSTR_D18), MP_ROM_PTR(&pin_D18) }, + { MP_ROM_QSTR(MP_QSTR_D19), MP_ROM_PTR(&pin_D19) }, + { MP_ROM_QSTR(MP_QSTR_D20), MP_ROM_PTR(&pin_D20) }, + { MP_ROM_QSTR(MP_QSTR_D21), MP_ROM_PTR(&pin_D21) }, + { MP_ROM_QSTR(MP_QSTR_D22), MP_ROM_PTR(&pin_D22) }, + { MP_ROM_QSTR(MP_QSTR_D23), MP_ROM_PTR(&pin_D23) }, + { MP_ROM_QSTR(MP_QSTR_D24), MP_ROM_PTR(&pin_D24) }, + { MP_ROM_QSTR(MP_QSTR_D25), MP_ROM_PTR(&pin_D25) }, + { MP_ROM_QSTR(MP_QSTR_D26), MP_ROM_PTR(&pin_D26) }, + { MP_ROM_QSTR(MP_QSTR_D27), MP_ROM_PTR(&pin_D27) }, + { MP_ROM_QSTR(MP_QSTR_D28), MP_ROM_PTR(&pin_D28) }, + { MP_ROM_QSTR(MP_QSTR_LED0), MP_ROM_PTR(&pin_LED0) }, + { MP_ROM_QSTR(MP_QSTR_LED1), MP_ROM_PTR(&pin_LED1) }, + { MP_ROM_QSTR(MP_QSTR_LED2), MP_ROM_PTR(&pin_LED2) }, + { MP_ROM_QSTR(MP_QSTR_LED3), MP_ROM_PTR(&pin_LED3) }, + { MP_ROM_QSTR(MP_QSTR_A0), MP_ROM_PTR(&pin_A0) }, + { MP_ROM_QSTR(MP_QSTR_A1), MP_ROM_PTR(&pin_A1) }, + { MP_ROM_QSTR(MP_QSTR_A2), MP_ROM_PTR(&pin_A2) }, + { MP_ROM_QSTR(MP_QSTR_A3), MP_ROM_PTR(&pin_A3) }, + { MP_ROM_QSTR(MP_QSTR_A4), MP_ROM_PTR(&pin_A4) }, + { MP_ROM_QSTR(MP_QSTR_A5), MP_ROM_PTR(&pin_A5) }, + { MP_ROM_QSTR(MP_QSTR_SDA), MP_ROM_PTR(&pin_D14) }, + { MP_ROM_QSTR(MP_QSTR_SCL), MP_ROM_PTR(&pin_D15) }, + { MP_ROM_QSTR(MP_QSTR_SCK), MP_ROM_PTR(&pin_D13) }, + { MP_ROM_QSTR(MP_QSTR_MISO), MP_ROM_PTR(&pin_D12) }, + { MP_ROM_QSTR(MP_QSTR_MOSI), MP_ROM_PTR(&pin_D11) }, + { MP_ROM_QSTR(MP_QSTR_RX), MP_ROM_PTR(&pin_D0) }, + { MP_ROM_QSTR(MP_QSTR_TX), MP_ROM_PTR(&pin_D1) }, + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, + { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, + { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&board_uart_obj) }, +}; +MP_DEFINE_CONST_DICT(board_module_globals, board_module_globals_table); diff --git a/ports/spresense/common-hal/busio/I2C.c b/ports/spresense/common-hal/busio/I2C.c new file mode 100644 index 000000000..c163c183a --- /dev/null +++ b/ports/spresense/common-hal/busio/I2C.c @@ -0,0 +1,123 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 + +#include "py/runtime.h" + +#include "shared-bindings/busio/I2C.h" + +void common_hal_busio_i2c_construct(busio_i2c_obj_t *self, const mcu_pin_obj_t *scl, + const mcu_pin_obj_t *sda, uint32_t frequency, uint32_t timeout) { + if (frequency != I2C_SPEED_STANDARD && frequency != I2C_SPEED_FAST) { + mp_raise_ValueError(translate("Unsupported baudrate")); + } + + if (scl->number != PIN_I2C0_BCK || sda->number != PIN_I2C0_BDT) { + mp_raise_ValueError(translate("Invalid pins")); + } + + claim_pin(scl); + claim_pin(sda); + + self->scl_pin = scl; + self->sda_pin = sda; + self->frequency = frequency; + self->i2c_dev = cxd56_i2cbus_initialize(0); +} + +void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) { + if (common_hal_busio_i2c_deinited(self)) { + return; + } + + cxd56_i2cbus_uninitialize(self->i2c_dev); + self->i2c_dev = NULL; + + reset_pin_number(self->scl_pin->number); + reset_pin_number(self->sda_pin->number); +} + +bool common_hal_busio_i2c_deinited(busio_i2c_obj_t *self) { + return self->i2c_dev == NULL; +} + +bool common_hal_busio_i2c_try_lock(busio_i2c_obj_t *self) { + bool grabbed_lock = false; + if (!self->has_lock) { + grabbed_lock = true; + self->has_lock = true; + } + return grabbed_lock; +} + +bool common_hal_busio_i2c_has_lock(busio_i2c_obj_t *self) { + return self->has_lock; +} + +void common_hal_busio_i2c_unlock(busio_i2c_obj_t *self) { + self->has_lock = false; +} + +bool common_hal_busio_i2c_probe(busio_i2c_obj_t *self, uint8_t addr) { + struct i2c_msg_s msg; + + msg.frequency = self->frequency; + msg.addr = addr; + msg.flags = 0; + msg.buffer = NULL; + msg.length = 0; + return I2C_TRANSFER(self->i2c_dev, &msg, 1) < 0 ? false : true; +} + +uint8_t common_hal_busio_i2c_write(busio_i2c_obj_t *self, uint16_t address, const uint8_t *data, size_t len, bool stop) { + struct i2c_msg_s msg; + + msg.frequency = self->frequency; + msg.addr = address; + msg.flags = (stop ? 0 : I2C_M_NOSTOP); + msg.buffer = (uint8_t *) data; + msg.length = len; + return I2C_TRANSFER(self->i2c_dev, &msg, 1); +} + +uint8_t common_hal_busio_i2c_read(busio_i2c_obj_t *self, uint16_t address, uint8_t *data, size_t len) { + struct i2c_msg_s msg; + + msg.frequency = self->frequency; + msg.addr = address; + msg.flags = I2C_M_READ; + msg.buffer = data; + msg.length = len; + return I2C_TRANSFER(self->i2c_dev, &msg, 1); +} + +void common_hal_busio_i2c_never_reset(busio_i2c_obj_t *self) { + never_reset_pin_number(self->scl_pin->number); + never_reset_pin_number(self->sda_pin->number); +} diff --git a/ports/spresense/common-hal/busio/I2C.h b/ports/spresense/common-hal/busio/I2C.h new file mode 100644 index 000000000..ec2acfb53 --- /dev/null +++ b/ports/spresense/common-hal/busio/I2C.h @@ -0,0 +1,43 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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. + */ + +#ifndef MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_BUSIO_I2C_H +#define MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_BUSIO_I2C_H + +#include "py/obj.h" + +#include "common-hal/microcontroller/Pin.h" + +typedef struct { + mp_obj_base_t base; + struct i2c_master_s* i2c_dev; + uint32_t frequency; + bool has_lock; + const mcu_pin_obj_t *scl_pin; + const mcu_pin_obj_t *sda_pin; +} busio_i2c_obj_t; + +#endif // MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_BUSIO_I2C_H diff --git a/ports/spresense/common-hal/busio/OneWire.h b/ports/spresense/common-hal/busio/OneWire.h new file mode 100644 index 000000000..a76ee8c77 --- /dev/null +++ b/ports/spresense/common-hal/busio/OneWire.h @@ -0,0 +1,33 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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. + */ + +#ifndef MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_BUSIO_ONEWIRE_H +#define MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_BUSIO_ONEWIRE_H + +// Use bitbangio. +#include "shared-module/busio/OneWire.h" + +#endif // MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_BUSIO_ONEWIRE_H diff --git a/ports/spresense/common-hal/busio/SPI.c b/ports/spresense/common-hal/busio/SPI.c new file mode 100644 index 000000000..9a41011f2 --- /dev/null +++ b/ports/spresense/common-hal/busio/SPI.c @@ -0,0 +1,155 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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/runtime.h" + +#include "shared-bindings/busio/SPI.h" + +void common_hal_busio_spi_construct(busio_spi_obj_t *self, const mcu_pin_obj_t *clock, + const mcu_pin_obj_t *mosi, const mcu_pin_obj_t *miso) { + int port = -1; + + if (clock->number == PIN_SPI4_SCK && mosi->number == PIN_SPI4_MOSI && miso->number == PIN_SPI4_MISO) { + port = 4; + } else if (clock->number == PIN_EMMC_CLK && mosi->number == PIN_EMMC_DATA0 && miso->number == PIN_EMMC_DATA1) { + port = 5; + } + + if (port < 0) { + mp_raise_ValueError(translate("Invalid pins")); + } + + claim_pin(clock); + claim_pin(mosi); + claim_pin(miso); + + self->clock_pin = clock; + self->mosi_pin = mosi; + self->miso_pin = miso; + self->spi_dev = cxd56_spibus_initialize(port); +} + +void common_hal_busio_spi_deinit(busio_spi_obj_t *self) { + if (common_hal_busio_spi_deinited(self)) { + return; + } + + self->spi_dev = NULL; + + reset_pin_number(self->clock_pin->number); + reset_pin_number(self->mosi_pin->number); + reset_pin_number(self->miso_pin->number); +} + +bool common_hal_busio_spi_deinited(busio_spi_obj_t *self) { + return self->spi_dev == NULL; +} + +bool common_hal_busio_spi_configure(busio_spi_obj_t *self, uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t bits) { + uint8_t mode; + + self->frequency = baudrate; + SPI_SETFREQUENCY(self->spi_dev, baudrate); + + if (polarity == 0) { + if (phase == 0) { + mode = SPIDEV_MODE0; + } else { + mode = SPIDEV_MODE1; + } + } else { + if (phase == 0) { + mode = SPIDEV_MODE2; + } else { + mode = SPIDEV_MODE3; + } + } + + self->polarity = polarity; + self->phase = phase; + SPI_SETMODE(self->spi_dev, mode); + + self->bits = bits; + SPI_SETBITS(self->spi_dev, bits); + + return true; +} + +bool common_hal_busio_spi_try_lock(busio_spi_obj_t *self) { + bool grabbed_lock = false; + if (!self->has_lock) { + grabbed_lock = true; + self->has_lock = true; + } + return grabbed_lock; +} + +bool common_hal_busio_spi_has_lock(busio_spi_obj_t *self) { + return self->has_lock; +} + +void common_hal_busio_spi_unlock(busio_spi_obj_t *self) { + self->has_lock = false; +} + +bool common_hal_busio_spi_write(busio_spi_obj_t *self, const uint8_t *data, size_t len) { + SPI_EXCHANGE(self->spi_dev, data, NULL, len); + + return true; +} + +bool common_hal_busio_spi_read(busio_spi_obj_t *self, uint8_t *data, size_t len, uint8_t write_value) { + SPI_EXCHANGE(self->spi_dev, NULL, data, len); + + return true; +} + +bool common_hal_busio_spi_transfer(busio_spi_obj_t *self, uint8_t *data_out, uint8_t *data_in, size_t len) { + SPI_EXCHANGE(self->spi_dev, data_out, data_in, len); + + return true; +} + +uint32_t common_hal_busio_spi_get_frequency(busio_spi_obj_t *self) { + return self->frequency; +} + +uint8_t common_hal_busio_spi_get_phase(busio_spi_obj_t* self) { + return self->phase; +} + +uint8_t common_hal_busio_spi_get_polarity(busio_spi_obj_t *self) { + return self->polarity; +} + +void common_hal_busio_spi_never_reset(busio_spi_obj_t *self) { + never_reset_pin_number(self->clock_pin->number); + never_reset_pin_number(self->mosi_pin->number); + never_reset_pin_number(self->miso_pin->number); +} diff --git a/ports/spresense/common-hal/busio/SPI.h b/ports/spresense/common-hal/busio/SPI.h new file mode 100644 index 000000000..90c2f6b81 --- /dev/null +++ b/ports/spresense/common-hal/busio/SPI.h @@ -0,0 +1,49 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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. + */ + +#ifndef MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_BUSIO_SPI_H +#define MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_BUSIO_SPI_H + +#include + +#include "py/obj.h" + +#include "common-hal/microcontroller/Pin.h" + +typedef struct { + mp_obj_base_t base; + struct spi_dev_s* spi_dev; + uint32_t frequency; + uint8_t phase; + uint8_t polarity; + uint8_t bits; + bool has_lock; + const mcu_pin_obj_t *clock_pin; + const mcu_pin_obj_t *mosi_pin; + const mcu_pin_obj_t *miso_pin; +} busio_spi_obj_t; + +#endif // MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_BUSIO_SPI_H diff --git a/ports/spresense/common-hal/busio/UART.c b/ports/spresense/common-hal/busio/UART.c new file mode 100644 index 000000000..2389cbf96 --- /dev/null +++ b/ports/spresense/common-hal/busio/UART.c @@ -0,0 +1,168 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 +#include + +#include +#include +#include +#include +#include +#include + +#include "py/mperrno.h" +#include "py/stream.h" +#include "py/runtime.h" + +#include "shared-bindings/busio/UART.h" + +void common_hal_busio_uart_construct(busio_uart_obj_t *self, + const mcu_pin_obj_t *tx, const mcu_pin_obj_t *rx, uint32_t baudrate, + uint8_t bits, uart_parity_t parity, uint8_t stop, mp_float_t timeout, + uint16_t receiver_buffer_size) { + struct termios tio; + + self->uart_fd = open("/dev/ttyS2", O_RDWR); + if (self->uart_fd < 0) { + mp_raise_ValueError(translate("Could not initialize UART")); + } + + ioctl(self->uart_fd, TCGETS, (long unsigned int)&tio); + tio.c_speed = baudrate; + ioctl(self->uart_fd, TCSETS, (long unsigned int)&tio); + ioctl(self->uart_fd, TCFLSH, (long unsigned int)NULL); + + if (bits != 8) { + mp_raise_ValueError(translate("Could not initialize UART")); + } + + if (parity != PARITY_NONE) { + mp_raise_ValueError(translate("Could not initialize UART")); + } + + if (stop != 1) { + mp_raise_ValueError(translate("Could not initialize UART")); + } + + if (tx->number != PIN_UART2_TXD || rx->number != PIN_UART2_RXD) { + mp_raise_ValueError(translate("Invalid pins")); + } + + claim_pin(tx); + claim_pin(rx); + + self->tx_pin = tx; + self->rx_pin = rx; + self->baudrate = baudrate; + self->timeout = timeout; +} + +void common_hal_busio_uart_deinit(busio_uart_obj_t *self) { + if (common_hal_busio_uart_deinited(self)) { + return; + } + + close(self->uart_fd); + self->uart_fd = -1; + + reset_pin_number(self->tx_pin->number); + reset_pin_number(self->rx_pin->number); +} + +bool common_hal_busio_uart_deinited(busio_uart_obj_t *self) { + return self->uart_fd < 0; +} + +size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t len, int *errcode) { + fd_set rfds; + struct timeval tv; + int retval, bytes_read; + + // make sure we want at least 1 char + if (len == 0) { + return 0; + } + + FD_ZERO(&rfds); + FD_SET(self->uart_fd, &rfds); + + tv.tv_sec = 0; + tv.tv_usec = self->timeout * 1000; + + retval = select(self->uart_fd + 1, &rfds, NULL, NULL, &tv); + + if (retval) { + bytes_read = read(self->uart_fd, data, len); + } else { + *errcode = EAGAIN; + return MP_STREAM_ERROR; + } + + return bytes_read; +} + +size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data, size_t len, int *errcode) { + int bytes_written = write(self->uart_fd, data, len); + + if (bytes_written < 0) { + *errcode = MP_EAGAIN; + return MP_STREAM_ERROR; + } + + return bytes_written; +} + +uint32_t common_hal_busio_uart_get_baudrate(busio_uart_obj_t *self) { + return self->baudrate; +} + +void common_hal_busio_uart_set_baudrate(busio_uart_obj_t *self, uint32_t baudrate) { + struct termios tio; + + ioctl(self->uart_fd, TCGETS, (long unsigned int)&tio); + tio.c_speed = baudrate; + ioctl(self->uart_fd, TCSETS, (long unsigned int)&tio); + ioctl(self->uart_fd, TCFLSH, (long unsigned int)NULL); +} + +uint32_t common_hal_busio_uart_rx_characters_available(busio_uart_obj_t *self) { + int count = 0; + + ioctl(self->uart_fd, FIONREAD, (long unsigned int)&count); + + return count; +} + +void common_hal_busio_uart_clear_rx_buffer(busio_uart_obj_t *self) { +} + +bool common_hal_busio_uart_ready_to_tx(busio_uart_obj_t *self) { + ioctl(self->uart_fd, TCFLSH, (long unsigned int)NULL); + return true; +} diff --git a/ports/spresense/common-hal/busio/UART.h b/ports/spresense/common-hal/busio/UART.h new file mode 100644 index 000000000..7cdbe4ff3 --- /dev/null +++ b/ports/spresense/common-hal/busio/UART.h @@ -0,0 +1,43 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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. + */ + +#ifndef MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_BUSIO_UART_H +#define MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_BUSIO_UART_H + +#include "py/obj.h" + +#include "common-hal/microcontroller/Pin.h" + +typedef struct { + mp_obj_base_t base; + int uart_fd; + const mcu_pin_obj_t *tx_pin; + const mcu_pin_obj_t *rx_pin; + uint32_t baudrate; + uint32_t timeout; +} busio_uart_obj_t; + +#endif // MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_BUSIO_UART_H diff --git a/ports/spresense/common-hal/busio/__init__.c b/ports/spresense/common-hal/busio/__init__.c new file mode 100644 index 000000000..41761b674 --- /dev/null +++ b/ports/spresense/common-hal/busio/__init__.c @@ -0,0 +1 @@ +// No busio module functions. diff --git a/ports/spresense/common-hal/digitalio/DigitalInOut.c b/ports/spresense/common-hal/digitalio/DigitalInOut.c new file mode 100644 index 000000000..7b1f6cc03 --- /dev/null +++ b/ports/spresense/common-hal/digitalio/DigitalInOut.c @@ -0,0 +1,136 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 "py/runtime.h" + +#include "shared-bindings/digitalio/DigitalInOut.h" + +digitalinout_result_t common_hal_digitalio_digitalinout_construct(digitalio_digitalinout_obj_t *self, const mcu_pin_obj_t *pin) { + if (pin->analog) { + mp_raise_ValueError(translate("DigitalInOut not supported on given pin")); + } + + claim_pin(pin); + + self->pin = pin; + self->input = true; + self->open_drain = false; + + board_gpio_write(self->pin->number, -1); + board_gpio_config(self->pin->number, 0, true, true, PIN_FLOAT); + + return DIGITALINOUT_OK; +} + +void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t *self) { + if (common_hal_digitalio_digitalinout_deinited(self)) { + return; + } + + board_gpio_write(self->pin->number, -1); + board_gpio_config(self->pin->number, 0, false, true, PIN_FLOAT); + + reset_pin_number(self->pin->number); + self->pin = mp_const_none; +} + +bool common_hal_digitalio_digitalinout_deinited(digitalio_digitalinout_obj_t *self) { + return self->pin == mp_const_none; +} + +void common_hal_digitalio_digitalinout_switch_to_input(digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) { + self->input = true; + self->pull = pull; + board_gpio_write(self->pin->number, -1); + board_gpio_config(self->pin->number, 0, true, true, pull); +} + +void common_hal_digitalio_digitalinout_switch_to_output(digitalio_digitalinout_obj_t *self, bool value, digitalio_drive_mode_t drive_mode) { + self->input = false; + self->open_drain = drive_mode == DRIVE_MODE_OPEN_DRAIN; + board_gpio_write(self->pin->number, -1); + board_gpio_config(self->pin->number, 0, false, true, PIN_FLOAT); + + if (self->open_drain) { + board_gpio_write(self->pin->number, 0); + } + common_hal_digitalio_digitalinout_set_value(self, value); +} + +digitalio_direction_t common_hal_digitalio_digitalinout_get_direction(digitalio_digitalinout_obj_t *self) { + return self->input ? DIRECTION_INPUT : DIRECTION_OUTPUT; +} + +void common_hal_digitalio_digitalinout_set_value(digitalio_digitalinout_obj_t *self, bool value) { + if (self->open_drain) { + if (value) { + board_gpio_write(self->pin->number, -1); + board_gpio_config(self->pin->number, 0, true, true, PIN_PULLUP); + } else { + board_gpio_config(self->pin->number, 0, false, true, PIN_FLOAT); + board_gpio_write(self->pin->number, 0); + } + } else { + board_gpio_write(self->pin->number, value); + } +} + +bool common_hal_digitalio_digitalinout_get_value(digitalio_digitalinout_obj_t *self) { + return board_gpio_read(self->pin->number); +} + +void common_hal_digitalio_digitalinout_set_drive_mode(digitalio_digitalinout_obj_t *self, digitalio_drive_mode_t drive_mode) { + if (drive_mode == DRIVE_MODE_PUSH_PULL) { + board_gpio_write(self->pin->number, -1); + board_gpio_config(self->pin->number, 0, false, true, PIN_FLOAT); + self->open_drain = false; + } else { + board_gpio_write(self->pin->number, -1); + board_gpio_config(self->pin->number, 0, false, true, PIN_FLOAT); + board_gpio_write(self->pin->number, 0); + self->open_drain = true; + } +} + +digitalio_drive_mode_t common_hal_digitalio_digitalinout_get_drive_mode(digitalio_digitalinout_obj_t *self) { + return self->open_drain ? DRIVE_MODE_OPEN_DRAIN : DRIVE_MODE_PUSH_PULL; +} + +void common_hal_digitalio_digitalinout_set_pull(digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) { + self->pull = pull; + board_gpio_write(self->pin->number, -1); + board_gpio_config(self->pin->number, 0, true, true, pull); +} + +digitalio_pull_t common_hal_digitalio_digitalinout_get_pull(digitalio_digitalinout_obj_t *self) { + return self->pull; +} + +void common_hal_digitalio_digitalinout_never_reset(digitalio_digitalinout_obj_t *self) { + never_reset_pin_number(self->pin->number); +} diff --git a/ports/spresense/common-hal/digitalio/DigitalInOut.h b/ports/spresense/common-hal/digitalio/DigitalInOut.h new file mode 100644 index 000000000..a5bfd22bd --- /dev/null +++ b/ports/spresense/common-hal/digitalio/DigitalInOut.h @@ -0,0 +1,42 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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. + */ + +#ifndef MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_DIGITALIO_DIGITALINOUT_H +#define MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_DIGITALIO_DIGITALINOUT_H + +#include "py/obj.h" + +#include "common-hal/microcontroller/Pin.h" + +typedef struct { + mp_obj_base_t base; + const mcu_pin_obj_t *pin; + bool input; + bool open_drain; + uint8_t pull; +} digitalio_digitalinout_obj_t; + +#endif // MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_DIGITALIO_DIGITALINOUT_H diff --git a/ports/spresense/common-hal/digitalio/__init__.c b/ports/spresense/common-hal/digitalio/__init__.c new file mode 100644 index 000000000..20fad4595 --- /dev/null +++ b/ports/spresense/common-hal/digitalio/__init__.c @@ -0,0 +1 @@ +// No digitalio module functions. diff --git a/ports/spresense/common-hal/microcontroller/Pin.c b/ports/spresense/common-hal/microcontroller/Pin.c new file mode 100644 index 000000000..005defa84 --- /dev/null +++ b/ports/spresense/common-hal/microcontroller/Pin.c @@ -0,0 +1,162 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 +#include +#include + +#include "shared-bindings/microcontroller/Pin.h" + +typedef struct { + const mcu_pin_obj_t *pin; + bool reset; + bool free; +} pin_status_t; + +const mcu_pin_obj_t pin_D0 = PIN(PIN_UART2_RXD, false); +const mcu_pin_obj_t pin_D1 = PIN(PIN_UART2_TXD, false); +const mcu_pin_obj_t pin_D2 = PIN(PIN_HIF_IRQ_OUT, false); +const mcu_pin_obj_t pin_D3 = PIN(PIN_PWM3, false); +const mcu_pin_obj_t pin_D4 = PIN(PIN_SPI2_MOSI, false); +const mcu_pin_obj_t pin_D5 = PIN(PIN_PWM1, false); +const mcu_pin_obj_t pin_D6 = PIN(PIN_PWM0, false); +const mcu_pin_obj_t pin_D7 = PIN(PIN_SPI3_CS1_X, false); +const mcu_pin_obj_t pin_D8 = PIN(PIN_SPI2_MISO, false); +const mcu_pin_obj_t pin_D9 = PIN(PIN_PWM2, false); +const mcu_pin_obj_t pin_D10 = PIN(PIN_SPI4_CS_X, false); +const mcu_pin_obj_t pin_D11 = PIN(PIN_SPI4_MOSI, false); +const mcu_pin_obj_t pin_D12 = PIN(PIN_SPI4_MISO, false); +const mcu_pin_obj_t pin_D13 = PIN(PIN_SPI4_SCK, false); +const mcu_pin_obj_t pin_D14 = PIN(PIN_I2C0_BDT, false); +const mcu_pin_obj_t pin_D15 = PIN(PIN_I2C0_BCK, false); +const mcu_pin_obj_t pin_D16 = PIN(PIN_EMMC_DATA0, false); +const mcu_pin_obj_t pin_D17 = PIN(PIN_EMMC_DATA1, false); +const mcu_pin_obj_t pin_D18 = PIN(PIN_I2S0_DATA_OUT, false); +const mcu_pin_obj_t pin_D19 = PIN(PIN_I2S0_DATA_IN, false); +const mcu_pin_obj_t pin_D20 = PIN(PIN_EMMC_DATA2, false); +const mcu_pin_obj_t pin_D21 = PIN(PIN_EMMC_DATA3, false); +const mcu_pin_obj_t pin_D22 = PIN(PIN_SEN_IRQ_IN, false); +const mcu_pin_obj_t pin_D23 = PIN(PIN_EMMC_CLK, false); +const mcu_pin_obj_t pin_D24 = PIN(PIN_EMMC_CMD, false); +const mcu_pin_obj_t pin_D25 = PIN(PIN_I2S0_LRCK, false); +const mcu_pin_obj_t pin_D26 = PIN(PIN_I2S0_BCK, false); +const mcu_pin_obj_t pin_D27 = PIN(PIN_UART2_CTS, false); +const mcu_pin_obj_t pin_D28 = PIN(PIN_UART2_RTS, false); +const mcu_pin_obj_t pin_LED0 = PIN(PIN_I2S1_BCK, false); +const mcu_pin_obj_t pin_LED1 = PIN(PIN_I2S1_LRCK, false); +const mcu_pin_obj_t pin_LED2 = PIN(PIN_I2S1_DATA_IN, false); +const mcu_pin_obj_t pin_LED3 = PIN(PIN_I2S1_DATA_OUT, false); +const mcu_pin_obj_t pin_A0 = PIN(0, true); +const mcu_pin_obj_t pin_A1 = PIN(1, true); +const mcu_pin_obj_t pin_A2 = PIN(2, true); +const mcu_pin_obj_t pin_A3 = PIN(3, true); +const mcu_pin_obj_t pin_A4 = PIN(4, true); +const mcu_pin_obj_t pin_A5 = PIN(5, true); + +STATIC pin_status_t pins[] = { + { &pin_D0, true, true }, + { &pin_D1, true, true }, + { &pin_D2, true, true }, + { &pin_D3, true, true }, + { &pin_D4, true, true }, + { &pin_D5, true, true }, + { &pin_D6, true, true }, + { &pin_D7, true, true }, + { &pin_D8, true, true }, + { &pin_D9, true, true }, + { &pin_D10, true, true }, + { &pin_D11, true, true }, + { &pin_D12, true, true }, + { &pin_D13, true, true }, + { &pin_D14, true, true }, + { &pin_D15, true, true }, + { &pin_D16, true, true }, + { &pin_D17, true, true }, + { &pin_D18, true, true }, + { &pin_D19, true, true }, + { &pin_D20, true, true }, + { &pin_D21, true, true }, + { &pin_D22, true, true }, + { &pin_D23, true, true }, + { &pin_D24, true, true }, + { &pin_D25, true, true }, + { &pin_D26, true, true }, + { &pin_D27, true, true }, + { &pin_D28, true, true }, + { &pin_LED0, true, true }, + { &pin_LED1, true, true }, + { &pin_LED2, true, true }, + { &pin_LED3, true, true }, +}; + +bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t *pin) { + for (int i = 0; i < MP_ARRAY_SIZE(pins); i++) { + if (pins[i].pin->number == pin->number) { + return pins[i].free; + } + } + + return true; +} + +void never_reset_pin_number(uint8_t pin_number) { + for (int i = 0; i < MP_ARRAY_SIZE(pins); i++) { + if (pins[i].pin->number == pin_number) { + pins[i].reset = false; + } + } +} + +void reset_pin_number(uint8_t pin_number) { + for (int i = 0; i < MP_ARRAY_SIZE(pins); i++) { + if (pins[i].pin->number == pin_number) { + pins[i].free = true; + } + } +} + +void reset_all_pins(void) { + for (int i = 0; i < MP_ARRAY_SIZE(pins); i++) { + if (!pins[i].free && pins[i].reset) { + board_gpio_write(pins[i].pin->number, -1); + board_gpio_config(pins[i].pin->number, 0, false, true, PIN_FLOAT); + board_gpio_int(pins[i].pin->number, false); + board_gpio_intconfig(pins[i].pin->number, 0, false, NULL); + pins[i].free = true; + } + } +} + +void claim_pin(const mcu_pin_obj_t *pin) { + for (int i = 0; i < MP_ARRAY_SIZE(pins); i++) { + if (pins[i].pin->number == pin->number) { + pins[i].free = false; + } + } +} diff --git a/ports/spresense/common-hal/microcontroller/Pin.h b/ports/spresense/common-hal/microcontroller/Pin.h new file mode 100644 index 000000000..2d995d0dd --- /dev/null +++ b/ports/spresense/common-hal/microcontroller/Pin.h @@ -0,0 +1,92 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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. + */ + +#ifndef MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_MICROCONTROLLER_PIN_H +#define MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_MICROCONTROLLER_PIN_H + +#include "py/obj.h" + +extern const mp_obj_type_t mcu_pin_type; + +#define PIN(pin, a) \ +{ \ + { &mcu_pin_type }, \ + .number = (pin), \ + .analog = (a) \ +} + +typedef struct { + mp_obj_base_t base; + uint8_t number; + bool analog; +} mcu_pin_obj_t; + +extern const mcu_pin_obj_t pin_D0; +extern const mcu_pin_obj_t pin_D1; +extern const mcu_pin_obj_t pin_D2; +extern const mcu_pin_obj_t pin_D3; +extern const mcu_pin_obj_t pin_D4; +extern const mcu_pin_obj_t pin_D5; +extern const mcu_pin_obj_t pin_D6; +extern const mcu_pin_obj_t pin_D7; +extern const mcu_pin_obj_t pin_D8; +extern const mcu_pin_obj_t pin_D9; +extern const mcu_pin_obj_t pin_D10; +extern const mcu_pin_obj_t pin_D11; +extern const mcu_pin_obj_t pin_D12; +extern const mcu_pin_obj_t pin_D13; +extern const mcu_pin_obj_t pin_D14; +extern const mcu_pin_obj_t pin_D15; +extern const mcu_pin_obj_t pin_D16; +extern const mcu_pin_obj_t pin_D17; +extern const mcu_pin_obj_t pin_D18; +extern const mcu_pin_obj_t pin_D19; +extern const mcu_pin_obj_t pin_D20; +extern const mcu_pin_obj_t pin_D21; +extern const mcu_pin_obj_t pin_D22; +extern const mcu_pin_obj_t pin_D23; +extern const mcu_pin_obj_t pin_D24; +extern const mcu_pin_obj_t pin_D25; +extern const mcu_pin_obj_t pin_D26; +extern const mcu_pin_obj_t pin_D27; +extern const mcu_pin_obj_t pin_D28; +extern const mcu_pin_obj_t pin_LED0; +extern const mcu_pin_obj_t pin_LED1; +extern const mcu_pin_obj_t pin_LED2; +extern const mcu_pin_obj_t pin_LED3; +extern const mcu_pin_obj_t pin_A0; +extern const mcu_pin_obj_t pin_A1; +extern const mcu_pin_obj_t pin_A2; +extern const mcu_pin_obj_t pin_A3; +extern const mcu_pin_obj_t pin_A4; +extern const mcu_pin_obj_t pin_A5; + +void never_reset_pin_number(uint8_t pin_number); +void reset_pin_number(uint8_t pin_number); +void reset_all_pins(void); +void claim_pin(const mcu_pin_obj_t* pin); + +#endif // MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_MICROCONTROLLER_PIN_H diff --git a/ports/spresense/common-hal/microcontroller/Processor.c b/ports/spresense/common-hal/microcontroller/Processor.c new file mode 100644 index 000000000..355ee01df --- /dev/null +++ b/ports/spresense/common-hal/microcontroller/Processor.c @@ -0,0 +1,41 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 "py/mphal.h" + +uint32_t common_hal_mcu_processor_get_frequency(void) { + return mp_hal_ticks_cpu(); +} + +float common_hal_mcu_processor_get_temperature(void) { + return 0; +} + +void common_hal_mcu_processor_get_uid(uint8_t raw_id[]) { + boardctl(BOARDIOC_UNIQUEID, (uintptr_t) raw_id); +} diff --git a/ports/spresense/common-hal/microcontroller/Processor.h b/ports/spresense/common-hal/microcontroller/Processor.h new file mode 100644 index 000000000..6c4c17b03 --- /dev/null +++ b/ports/spresense/common-hal/microcontroller/Processor.h @@ -0,0 +1,40 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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. + */ + +#ifndef MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_MICROCONTROLLER_PROCESSOR_H +#define MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_MICROCONTROLLER_PROCESSOR_H + +#define COMMON_HAL_MCU_PROCESSOR_UID_LENGTH CONFIG_BOARDCTL_UNIQUEID_SIZE + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; +} mcu_processor_obj_t; + +const mp_obj_type_t mcu_processor_type; + +#endif // MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_MICROCONTROLLER_PROCESSOR_H diff --git a/ports/spresense/common-hal/microcontroller/__init__.c b/ports/spresense/common-hal/microcontroller/__init__.c new file mode 100644 index 000000000..576896eb2 --- /dev/null +++ b/ports/spresense/common-hal/microcontroller/__init__.c @@ -0,0 +1,111 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 "py/mphal.h" +#include "py/runtime.h" + +#include "shared-bindings/microcontroller/__init__.h" +#include "common-hal/microcontroller/Pin.h" +#include "supervisor/filesystem.h" +#include "supervisor/shared/safe_mode.h" + +// The singleton microcontroller.Processor object, bound to microcontroller.cpu +// It currently only has properties, and no state. +const mcu_processor_obj_t common_hal_mcu_processor_obj = { + .base = { + .type = &mcu_processor_type, + }, +}; + +void common_hal_mcu_delay_us(uint32_t delay) { + mp_hal_delay_us(delay); +} + +void common_hal_mcu_disable_interrupts(void) { + __asm volatile ("cpsid i" : : : "memory"); +} + +void common_hal_mcu_enable_interrupts(void) { + __asm volatile ("cpsie i" : : : "memory"); +} + +void common_hal_mcu_on_next_reset(mcu_runmode_t runmode) { + if(runmode == RUNMODE_BOOTLOADER) { + mp_raise_ValueError(translate("Cannot reset into bootloader because no bootloader is present.")); + } else if(runmode == RUNMODE_SAFE_MODE) { + safe_mode_on_next_reset(PROGRAMMATIC_SAFE_MODE); + } +} + +void common_hal_mcu_reset(void) { + filesystem_flush(); + boardctl(BOARDIOC_RESET, 0); +} + +STATIC const mp_rom_map_elem_t mcu_pin_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR_D0), MP_ROM_PTR(&pin_D0) }, + { MP_ROM_QSTR(MP_QSTR_D1), MP_ROM_PTR(&pin_D1) }, + { MP_ROM_QSTR(MP_QSTR_D2), MP_ROM_PTR(&pin_D2) }, + { MP_ROM_QSTR(MP_QSTR_D3), MP_ROM_PTR(&pin_D3) }, + { MP_ROM_QSTR(MP_QSTR_D4), MP_ROM_PTR(&pin_D4) }, + { MP_ROM_QSTR(MP_QSTR_D5), MP_ROM_PTR(&pin_D5) }, + { MP_ROM_QSTR(MP_QSTR_D6), MP_ROM_PTR(&pin_D6) }, + { MP_ROM_QSTR(MP_QSTR_D7), MP_ROM_PTR(&pin_D7) }, + { MP_ROM_QSTR(MP_QSTR_D8), MP_ROM_PTR(&pin_D8) }, + { MP_ROM_QSTR(MP_QSTR_D9), MP_ROM_PTR(&pin_D9) }, + { MP_ROM_QSTR(MP_QSTR_D10), MP_ROM_PTR(&pin_D10) }, + { MP_ROM_QSTR(MP_QSTR_D11), MP_ROM_PTR(&pin_D11) }, + { MP_ROM_QSTR(MP_QSTR_D12), MP_ROM_PTR(&pin_D12) }, + { MP_ROM_QSTR(MP_QSTR_D13), MP_ROM_PTR(&pin_D13) }, + { MP_ROM_QSTR(MP_QSTR_D14), MP_ROM_PTR(&pin_D14) }, + { MP_ROM_QSTR(MP_QSTR_D15), MP_ROM_PTR(&pin_D15) }, + { MP_ROM_QSTR(MP_QSTR_D16), MP_ROM_PTR(&pin_D16) }, + { MP_ROM_QSTR(MP_QSTR_D17), MP_ROM_PTR(&pin_D17) }, + { MP_ROM_QSTR(MP_QSTR_D18), MP_ROM_PTR(&pin_D18) }, + { MP_ROM_QSTR(MP_QSTR_D19), MP_ROM_PTR(&pin_D19) }, + { MP_ROM_QSTR(MP_QSTR_D20), MP_ROM_PTR(&pin_D20) }, + { MP_ROM_QSTR(MP_QSTR_D21), MP_ROM_PTR(&pin_D21) }, + { MP_ROM_QSTR(MP_QSTR_D22), MP_ROM_PTR(&pin_D22) }, + { MP_ROM_QSTR(MP_QSTR_D23), MP_ROM_PTR(&pin_D23) }, + { MP_ROM_QSTR(MP_QSTR_D24), MP_ROM_PTR(&pin_D24) }, + { MP_ROM_QSTR(MP_QSTR_D25), MP_ROM_PTR(&pin_D25) }, + { MP_ROM_QSTR(MP_QSTR_D26), MP_ROM_PTR(&pin_D26) }, + { MP_ROM_QSTR(MP_QSTR_D27), MP_ROM_PTR(&pin_D27) }, + { MP_ROM_QSTR(MP_QSTR_D28), MP_ROM_PTR(&pin_D28) }, + { MP_ROM_QSTR(MP_QSTR_LED0), MP_ROM_PTR(&pin_LED0) }, + { MP_ROM_QSTR(MP_QSTR_LED1), MP_ROM_PTR(&pin_LED1) }, + { MP_ROM_QSTR(MP_QSTR_LED2), MP_ROM_PTR(&pin_LED2) }, + { MP_ROM_QSTR(MP_QSTR_LED3), MP_ROM_PTR(&pin_LED3) }, + { MP_ROM_QSTR(MP_QSTR_A0), MP_ROM_PTR(&pin_A0) }, + { MP_ROM_QSTR(MP_QSTR_A1), MP_ROM_PTR(&pin_A1) }, + { MP_ROM_QSTR(MP_QSTR_A2), MP_ROM_PTR(&pin_A2) }, + { MP_ROM_QSTR(MP_QSTR_A3), MP_ROM_PTR(&pin_A3) }, + { MP_ROM_QSTR(MP_QSTR_A4), MP_ROM_PTR(&pin_A4) }, + { MP_ROM_QSTR(MP_QSTR_A5), MP_ROM_PTR(&pin_A5) }, +}; +MP_DEFINE_CONST_DICT(mcu_pin_globals, mcu_pin_globals_table); diff --git a/ports/spresense/common-hal/os/__init__.c b/ports/spresense/common-hal/os/__init__.c new file mode 100644 index 000000000..d4b0e23be --- /dev/null +++ b/ports/spresense/common-hal/os/__init__.c @@ -0,0 +1,72 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 "genhdr/mpversion.h" +#include "py/objstr.h" +#include "py/objtuple.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, "spresense"); +STATIC const MP_DEFINE_STR_OBJ(os_uname_info_nodename_obj, "spresense"); +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); +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 +); + +mp_obj_t common_hal_os_uname(void) { + return (mp_obj_t)&os_uname_info_obj; +} + +bool common_hal_os_urandom(uint8_t* buffer, mp_uint_t length) { + uint32_t i = 0; + + while (i < length) { + uint32_t new_random = rand(); + for (int j = 0; j < 4 && i < length; j++) { + buffer[i] = new_random & 0xff; + i++; + new_random >>= 8; + } + } + + return true; +} diff --git a/ports/spresense/common-hal/pulseio/PWMOut.c b/ports/spresense/common-hal/pulseio/PWMOut.c new file mode 100644 index 000000000..fae49aed3 --- /dev/null +++ b/ports/spresense/common-hal/pulseio/PWMOut.c @@ -0,0 +1,157 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 + +#include "py/runtime.h" + +#include "shared-bindings/pulseio/PWMOut.h" + +typedef struct { + const char* devpath; + const mcu_pin_obj_t *pin; + int fd; + bool reset; +} pwmout_dev_t; + +STATIC pwmout_dev_t pwmout_dev[] = { + {"/dev/pwm0", &pin_D6, -1, true}, + {"/dev/pwm1", &pin_D5, -1, true}, + {"/dev/pwm2", &pin_D9, -1, true}, + {"/dev/pwm3", &pin_D3, -1, true} +}; + +pwmout_result_t common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t *self, + const mcu_pin_obj_t *pin, uint16_t duty, uint32_t frequency, + bool variable_frequency) { + self->number = -1; + + for (int i = 0; i < MP_ARRAY_SIZE(pwmout_dev); i++) { + if (pin->number == pwmout_dev[i].pin->number) { + self->number = i; + break; + } + } + + if (self->number < 0) { + return PWMOUT_INVALID_PIN; + } + + pwmout_dev[self->number].fd = open(pwmout_dev[self->number].devpath, O_RDONLY); + if (pwmout_dev[self->number].fd < 0) { + return PWMOUT_INVALID_PIN; + } + + self->info.frequency = frequency; + self->info.duty = duty; + self->variable_frequency = variable_frequency; + + if (ioctl(pwmout_dev[self->number].fd, PWMIOC_SETCHARACTERISTICS, (unsigned long)((uintptr_t)&self->info)) < 0) { + mp_raise_ValueError(translate("Invalid PWM frequency")); + } + ioctl(pwmout_dev[self->number].fd, PWMIOC_START, 0); + + claim_pin(pin); + + self->pin = pin; + + return PWMOUT_OK; +} + +void common_hal_pulseio_pwmout_deinit(pulseio_pwmout_obj_t *self) { + if (common_hal_pulseio_pwmout_deinited(self)) { + return; + } + + ioctl(pwmout_dev[self->number].fd, PWMIOC_STOP, 0); + close(pwmout_dev[self->number].fd); + pwmout_dev[self->number].fd = -1; + + reset_pin_number(self->pin->number); + self->pin = mp_const_none; +} + +bool common_hal_pulseio_pwmout_deinited(pulseio_pwmout_obj_t *self) { + return self->pin == mp_const_none; +} + +void common_hal_pulseio_pwmout_set_duty_cycle(pulseio_pwmout_obj_t *self, uint16_t duty) { + self->info.duty = duty; + + ioctl(pwmout_dev[self->number].fd, PWMIOC_SETCHARACTERISTICS, (unsigned long)((uintptr_t)&self->info)); +} + +uint16_t common_hal_pulseio_pwmout_get_duty_cycle(pulseio_pwmout_obj_t *self) { + return self->info.duty; +} + +void common_hal_pulseio_pwmout_set_frequency(pulseio_pwmout_obj_t *self, uint32_t frequency) { + self->info.frequency = frequency; + + if (ioctl(pwmout_dev[self->number].fd, PWMIOC_SETCHARACTERISTICS, (unsigned long)((uintptr_t)&self->info)) < 0) { + mp_raise_ValueError(translate("Invalid PWM frequency")); + } +} + +uint32_t common_hal_pulseio_pwmout_get_frequency(pulseio_pwmout_obj_t *self) { + return self->info.frequency; +} + +bool common_hal_pulseio_pwmout_get_variable_frequency(pulseio_pwmout_obj_t *self) { + return self->variable_frequency; +} + +void common_hal_pulseio_pwmout_never_reset(pulseio_pwmout_obj_t *self) { + never_reset_pin_number(self->pin->number); + + pwmout_dev[self->number].reset = false; +} + +void common_hal_pulseio_pwmout_reset_ok(pulseio_pwmout_obj_t *self) { + pwmout_dev[self->number].reset = true; +} + +void pwmout_reset(void) { + for (int i = 0; i < MP_ARRAY_SIZE(pwmout_dev); i++) { + if (pwmout_dev[i].fd >= 0 && pwmout_dev[i].reset) { + ioctl(pwmout_dev[i].fd, PWMIOC_STOP, 0); + close(pwmout_dev[i].fd); + pwmout_dev[i].fd = -1; + + reset_pin_number(pwmout_dev[i].pin->number); + } + } +} + +void pwmout_start(uint8_t pwm_num) { + ioctl(pwmout_dev[pwm_num].fd, PWMIOC_START, 0); +} + +void pwmout_stop(uint8_t pwm_num) { + ioctl(pwmout_dev[pwm_num].fd, PWMIOC_STOP, 0); +} diff --git a/ports/spresense/common-hal/pulseio/PWMOut.h b/ports/spresense/common-hal/pulseio/PWMOut.h new file mode 100644 index 000000000..486d5add9 --- /dev/null +++ b/ports/spresense/common-hal/pulseio/PWMOut.h @@ -0,0 +1,48 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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. + */ + +#ifndef MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_PULSEIO_PWMOUT_H +#define MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_PULSEIO_PWMOUT_H + +#include + +#include "common-hal/microcontroller/Pin.h" + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + const mcu_pin_obj_t *pin; + struct pwm_info_s info; + bool variable_frequency; + uint8_t number; +} pulseio_pwmout_obj_t; + +void pwmout_reset(void); +void pwmout_start(uint8_t pwm_num); +void pwmout_stop(uint8_t pwm_num); + +#endif // MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_PULSEIO_PWMOUT_H diff --git a/ports/spresense/common-hal/pulseio/PulseIn.c b/ports/spresense/common-hal/pulseio/PulseIn.c new file mode 100644 index 000000000..dd2773d1d --- /dev/null +++ b/ports/spresense/common-hal/pulseio/PulseIn.c @@ -0,0 +1,196 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 "py/runtime.h" +#include "py/mphal.h" + +#include "shared-bindings/pulseio/PulseIn.h" +#include "shared-bindings/microcontroller/__init__.h" + +static pulseio_pulsein_obj_t *pulsein_objects[12]; + +static int pulsein_interrupt_handler(int irq, FAR void *context, FAR void *arg); + +static int pulsein_set_config(pulseio_pulsein_obj_t *self, bool first_edge) { + int mode; + + if (!first_edge) { + mode = INT_BOTH_EDGE; + } else if (self->idle_state) { + mode = INT_FALLING_EDGE; + } else { + mode = INT_RISING_EDGE; + } + return board_gpio_intconfig(self->pin->number, mode, false, pulsein_interrupt_handler); +} + +static int pulsein_interrupt_handler(int irq, FAR void *context, FAR void *arg) { + // Grab the current time first. + uint32_t current_us = mp_hal_ticks_us(); + + pulseio_pulsein_obj_t *self = pulsein_objects[irq - CXD56_IRQ_EXDEVICE_0]; + + if (self->first_edge) { + self->first_edge = false; + pulsein_set_config(self, false); + board_gpio_int(self->pin->number, true); + } else { + uint32_t us_diff = current_us - self->last_us; + + uint16_t duration = 0xffff; + if (us_diff < duration) { + duration = us_diff; + } + + uint16_t i = (self->start + self->len) % self->maxlen; + self->buffer[i] = duration; + if (self->len < self->maxlen) { + self->len++; + } else { + self->start++; + } + } + self->last_us = current_us; + + return 0; +} + +void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self, + const mcu_pin_obj_t *pin, uint16_t maxlen, bool idle_state) { + self->buffer = (uint16_t *) m_malloc(maxlen * sizeof(uint16_t), false); + if (self->buffer == NULL) { + mp_raise_msg_varg(&mp_type_MemoryError, translate("Failed to allocate RX buffer of %d bytes"), maxlen * sizeof(uint16_t)); + } + + self->pin = pin; + self->maxlen = maxlen; + self->idle_state = idle_state; + self->start = 0; + self->len = 0; + self->first_edge = true; + self->paused = false; + + int irq = pulsein_set_config(self, true); + if (irq < 0) { + mp_raise_RuntimeError(translate("EXTINT channel already in use")); + } else { + pulsein_objects[irq - CXD56_IRQ_EXDEVICE_0] = self; + } + + claim_pin(pin); + + board_gpio_int(self->pin->number, true); +} + +void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t *self) { + if (common_hal_pulseio_pulsein_deinited(self)) { + return; + } + + board_gpio_int(self->pin->number, false); + board_gpio_intconfig(self->pin->number, 0, false, NULL); + + reset_pin_number(self->pin->number); + self->pin = mp_const_none; +} + +bool common_hal_pulseio_pulsein_deinited(pulseio_pulsein_obj_t *self) { + return self->pin == mp_const_none; +} + +void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t *self) { + board_gpio_int(self->pin->number, false); + self->paused = true; +} + +void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t *self, uint16_t trigger_duration) { + // Make sure we're paused. + common_hal_pulseio_pulsein_pause(self); + + // Send the trigger pulse. + if (trigger_duration > 0) { + board_gpio_config(self->pin->number, 0, false, true, PIN_FLOAT); + board_gpio_write(self->pin->number, !self->idle_state); + common_hal_mcu_delay_us((uint32_t)trigger_duration); + board_gpio_write(self->pin->number, self->idle_state); + } + + // Reconfigure the pin and make sure its set to detect the first edge. + self->first_edge = true; + self->paused = false; + + pulsein_set_config(self, true); + board_gpio_int(self->pin->number, true); +} + +void common_hal_pulseio_pulsein_clear(pulseio_pulsein_obj_t *self) { + common_hal_mcu_disable_interrupts(); + self->start = 0; + self->len = 0; + common_hal_mcu_enable_interrupts(); +} + +uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t *self) { + if (self->len == 0) { + mp_raise_IndexError(translate("pop from an empty PulseIn")); + } + common_hal_mcu_disable_interrupts(); + uint16_t value = self->buffer[self->start]; + self->start = (self->start + 1) % self->maxlen; + self->len--; + common_hal_mcu_enable_interrupts(); + + return value; +} + +uint16_t common_hal_pulseio_pulsein_get_maxlen(pulseio_pulsein_obj_t *self) { + return self->maxlen; +} + +bool common_hal_pulseio_pulsein_get_paused(pulseio_pulsein_obj_t *self) { + return self->paused; +} + +uint16_t common_hal_pulseio_pulsein_get_len(pulseio_pulsein_obj_t *self) { + return self->len; +} + +uint16_t common_hal_pulseio_pulsein_get_item(pulseio_pulsein_obj_t *self, int16_t index) { + common_hal_mcu_disable_interrupts(); + if (index < 0) { + index += self->len; + } + if (index < 0 || index >= self->len) { + common_hal_mcu_enable_interrupts(); + mp_raise_IndexError(translate("index out of range")); + } + uint16_t value = self->buffer[(self->start + index) % self->maxlen]; + common_hal_mcu_enable_interrupts(); + return value; +} + \ No newline at end of file diff --git a/ports/spresense/common-hal/pulseio/PulseIn.h b/ports/spresense/common-hal/pulseio/PulseIn.h new file mode 100644 index 000000000..5f2f31811 --- /dev/null +++ b/ports/spresense/common-hal/pulseio/PulseIn.h @@ -0,0 +1,47 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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. + */ + +#ifndef MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_PULSEIO_PULSEIN_H +#define MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_PULSEIO_PULSEIN_H + +#include "common-hal/microcontroller/Pin.h" + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + const mcu_pin_obj_t *pin; + uint16_t *buffer; + uint16_t maxlen; + uint16_t start; + uint16_t len; + uint32_t last_us; + bool idle_state; + bool first_edge; + bool paused; +} pulseio_pulsein_obj_t; + +#endif // MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_PULSEIO_PULSEIN_H diff --git a/ports/spresense/common-hal/pulseio/PulseOut.c b/ports/spresense/common-hal/pulseio/PulseOut.c new file mode 100644 index 000000000..5e1d5a2ed --- /dev/null +++ b/ports/spresense/common-hal/pulseio/PulseOut.c @@ -0,0 +1,126 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 +#include + +#include "py/runtime.h" + +#include "shared-bindings/pulseio/PulseOut.h" + +static uint16_t *pulse_buffer = NULL; +static uint16_t pulse_index = 0; +static uint16_t pulse_length; +static int pulse_fd = -1; + +static bool pulseout_timer_handler(unsigned int *next_interval_us, void *arg) +{ + uint8_t pwm_num = (uint8_t)(int)arg; + pulse_index++; + + if (pulse_index >= pulse_length) { + return false; + } + + *next_interval_us = pulse_buffer[pulse_index] * 1000; + + if (pulse_index % 2 == 0) { + pwmout_start(pwm_num); + } else { + pwmout_stop(pwm_num); + } + + return true; +} + +void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t *self, + const pulseio_pwmout_obj_t *carrier) { + if (pulse_fd < 0) { + pulse_fd = open("/dev/timer0", O_RDONLY); + } + + if (pulse_fd < 0) { + mp_raise_RuntimeError(translate("All timers in use")); + } + + self->pwm_num = carrier->number; +} + +void common_hal_pulseio_pulseout_deinit(pulseio_pulseout_obj_t *self) { + if (common_hal_pulseio_pulseout_deinited(self)) { + return; + } + + ioctl(pulse_fd, TCIOC_STOP, 0); + close(pulse_fd); + pulse_fd = -1; + + pulse_buffer = NULL; +} + +bool common_hal_pulseio_pulseout_deinited(pulseio_pulseout_obj_t *self) { + return pulse_fd < 0; +} + +void common_hal_pulseio_pulseout_send(pulseio_pulseout_obj_t *self, uint16_t *pulses, uint16_t len) { + if (pulse_buffer != NULL) { + mp_raise_RuntimeError(translate("Another send is already active")); + } + + struct timer_sethandler_s sethandler; + + pulse_buffer = pulses; + pulse_index = 0; + pulse_length = len; + + unsigned long timeout = pulse_buffer[0] * 1000; + + ioctl(pulse_fd, TCIOC_SETTIMEOUT, timeout); + + sethandler.handler = pulseout_timer_handler; + sethandler.arg = (void *)(int)self->pwm_num; + + ioctl(pulse_fd, TCIOC_SETHANDLER, (unsigned long)&sethandler); + ioctl(pulse_fd, TCIOC_START, 0); + + while(pulse_index < len) { + // Do other things while we wait. The interrupts will handle sending the + // signal. + RUN_BACKGROUND_TASKS; + } + + pulse_buffer = NULL; +} + +void pulseout_reset(void) { + ioctl(pulse_fd, TCIOC_STOP, 0); + close(pulse_fd); + pulse_fd = -1; + + pulse_buffer = NULL; +} diff --git a/ports/spresense/common-hal/pulseio/PulseOut.h b/ports/spresense/common-hal/pulseio/PulseOut.h new file mode 100644 index 000000000..652a1197e --- /dev/null +++ b/ports/spresense/common-hal/pulseio/PulseOut.h @@ -0,0 +1,39 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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. + */ + +#ifndef MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_PULSEIO_PULSEOUT_H +#define MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_PULSEIO_PULSEOUT_H + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + uint8_t pwm_num; +} pulseio_pulseout_obj_t; + +void pulseout_reset(void); + +#endif // MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_PULSEIO_PULSEOUT_H diff --git a/ports/spresense/common-hal/pulseio/__init__.c b/ports/spresense/common-hal/pulseio/__init__.c new file mode 100644 index 000000000..2bee925bc --- /dev/null +++ b/ports/spresense/common-hal/pulseio/__init__.c @@ -0,0 +1 @@ +// No pulseio module functions. diff --git a/ports/spresense/common-hal/rtc/RTC.c b/ports/spresense/common-hal/rtc/RTC.c new file mode 100644 index 000000000..ce65e6acd --- /dev/null +++ b/ports/spresense/common-hal/rtc/RTC.c @@ -0,0 +1,54 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 "py/obj.h" +#include "py/runtime.h" + +#include "shared-bindings/rtc/RTC.h" + +void common_hal_rtc_get_time(timeutils_struct_time_t *tm) { + struct timeval tv; + + gettimeofday(&tv, NULL); + timeutils_seconds_since_2000_to_struct_time(tv.tv_sec, tm); +} + +void common_hal_rtc_set_time(timeutils_struct_time_t *tm) { + struct timeval tv = {0}; + + tv.tv_sec = timeutils_seconds_since_2000(tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec); + settimeofday(&tv, NULL); +} + +int common_hal_rtc_get_calibration(void) { + return 0; +} + +void common_hal_rtc_set_calibration(int calibration) { + mp_raise_NotImplementedError(translate("RTC calibration is not supported on this board")); +} diff --git a/ports/spresense/common-hal/rtc/RTC.h b/ports/spresense/common-hal/rtc/RTC.h new file mode 100644 index 000000000..7a3ef18bd --- /dev/null +++ b/ports/spresense/common-hal/rtc/RTC.h @@ -0,0 +1,30 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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. + */ + +#ifndef MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_RTC_RTC_H +#define MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_RTC_RTC_H + +#endif // MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_RTC_RTC_H diff --git a/ports/spresense/common-hal/rtc/__init__.c b/ports/spresense/common-hal/rtc/__init__.c new file mode 100644 index 000000000..92d25d563 --- /dev/null +++ b/ports/spresense/common-hal/rtc/__init__.c @@ -0,0 +1 @@ +// No rtc module functions. diff --git a/ports/spresense/common-hal/supervisor/Runtime.c b/ports/spresense/common-hal/supervisor/Runtime.c new file mode 100755 index 000000000..a0d9e70ab --- /dev/null +++ b/ports/spresense/common-hal/supervisor/Runtime.c @@ -0,0 +1,36 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 "shared-bindings/supervisor/Runtime.h" +#include "supervisor/serial.h" + +bool common_hal_get_serial_connected(void) { + return (bool) serial_connected(); +} + +bool common_hal_get_serial_bytes_available(void) { + return (bool) serial_bytes_available(); +} diff --git a/ports/spresense/common-hal/supervisor/Runtime.h b/ports/spresense/common-hal/supervisor/Runtime.h new file mode 100755 index 000000000..c3ed62a25 --- /dev/null +++ b/ports/spresense/common-hal/supervisor/Runtime.h @@ -0,0 +1,36 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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. + */ + +#ifndef MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_SUPERVISOR_RUNTIME_H +#define MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_SUPERVISOR_RUNTIME_H + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; +} super_runtime_obj_t; + +#endif // MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_SUPERVISOR_RUNTIME_H diff --git a/ports/spresense/common-hal/supervisor/__init__.c b/ports/spresense/common-hal/supervisor/__init__.c new file mode 100755 index 000000000..e240525f2 --- /dev/null +++ b/ports/spresense/common-hal/supervisor/__init__.c @@ -0,0 +1,36 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 "shared-bindings/supervisor/__init__.h" +#include "shared-bindings/supervisor/Runtime.h" + +// The singleton supervisor.Runtime object, bound to supervisor.runtime +// It currently only has properties, and no state. +const super_runtime_obj_t common_hal_supervisor_runtime_obj = { + .base = { + .type = &supervisor_runtime_type, + }, +}; diff --git a/ports/spresense/common-hal/time/__init__.c b/ports/spresense/common-hal/time/__init__.c new file mode 100644 index 000000000..8f7326b62 --- /dev/null +++ b/ports/spresense/common-hal/time/__init__.c @@ -0,0 +1,37 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 "py/mphal.h" + +#include "tick.h" + +uint64_t common_hal_time_monotonic(void) { + return ticks_ms; +} + +void common_hal_time_delay_ms(uint32_t delay) { + mp_hal_delay_ms(delay); +} diff --git a/ports/spresense/fatfs_port.c b/ports/spresense/fatfs_port.c new file mode 100644 index 000000000..e986b4c6e --- /dev/null +++ b/ports/spresense/fatfs_port.c @@ -0,0 +1,46 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 "py/mphal.h" +#include "py/runtime.h" +#include "lib/oofatfs/ff.h" /* FatFs lower layer API */ +#include "lib/oofatfs/diskio.h" /* FatFs lower layer API */ +#include "lib/timeutils/timeutils.h" + +#if CIRCUITPY_RTC +#include "shared-bindings/rtc/RTC.h" +#endif + +DWORD get_fattime(void) { +#if CIRCUITPY_RTC + timeutils_struct_time_t tm; + common_hal_rtc_get_time(&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 >> 1); +#else + return ((2016 - 1980) << 25) | ((9) << 21) | ((1) << 16) | ((16) << 11) | ((43) << 5) | (35 / 2); +#endif +} diff --git a/ports/spresense/mpconfigboard.h b/ports/spresense/mpconfigboard.h new file mode 100644 index 000000000..e0fef60f0 --- /dev/null +++ b/ports/spresense/mpconfigboard.h @@ -0,0 +1,39 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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. + */ + +#define MICROPY_HW_BOARD_NAME "SPRESENSE" +#define MICROPY_HW_MCU_NAME "CXD5602" +#define MICROPY_PY_SYS_PLATFORM "SPRESENSE" + +#define DEFAULT_I2C_BUS_SCL (&pin_D15) +#define DEFAULT_I2C_BUS_SDA (&pin_D14) + +#define DEFAULT_SPI_BUS_SCK (&pin_D13) +#define DEFAULT_SPI_BUS_MISO (&pin_D12) +#define DEFAULT_SPI_BUS_MOSI (&pin_D11) + +#define DEFAULT_UART_BUS_RX (&pin_D0) +#define DEFAULT_UART_BUS_TX (&pin_D1) diff --git a/ports/spresense/mpconfigport.h b/ports/spresense/mpconfigport.h new file mode 100644 index 000000000..233b7b9a7 --- /dev/null +++ b/ports/spresense/mpconfigport.h @@ -0,0 +1,38 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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. + */ + +#ifndef __INCLUDED_MPCONFIGPORT_H +#define __INCLUDED_MPCONFIGPORT_H + +// 24kiB stack +#define CIRCUITPY_DEFAULT_STACK_SIZE 0x6000 + +#include "py/circuitpy_mpconfig.h" + +#define MICROPY_PORT_ROOT_POINTERS \ + CIRCUITPY_COMMON_ROOT_POINTERS \ + +#endif // __INCLUDED_MPCONFIGPORT_H diff --git a/ports/spresense/mpconfigport.mk b/ports/spresense/mpconfigport.mk new file mode 100644 index 000000000..53dba2840 --- /dev/null +++ b/ports/spresense/mpconfigport.mk @@ -0,0 +1,24 @@ +USB_VID = 0x054c +USB_PID = 0x0bc2 +USB_PRODUCT = "Spresense" +USB_MANUFACTURER = "Sony" +USB_SERIAL_NUMBER_LENGTH = 10 +USB_DEVICES = "CDC,MSC" +USB_MSC_MAX_PACKET_SIZE = 512 +USB_RENUMBER_ENDPOINTS = 0 +USB_CDC_EP_NUM_NOTIFICATION = 3 +USB_CDC_EP_NUM_DATA_OUT = 2 +USB_CDC_EP_NUM_DATA_IN = 1 +USB_MSC_EP_NUM_OUT = 5 +USB_MSC_EP_NUM_IN = 4 + +CIRCUITPY_AUDIOIO = 0 +CIRCUITPY_AUDIOBUSIO = 0 +CIRCUITPY_I2CSLAVE = 0 +CIRCUITPY_ROTARYIO = 0 +CIRCUITPY_TOUCHIO = 0 +CIRCUITPY_GAMEPAD = 0 +CIRCUITPY_NEOPIXEL_WRITE = 0 +CIRCUITPY_NVM = 0 +CIRCUITPY_DISPLAYIO = 0 +CIRCUITPY_FREQUENCYIO = 0 diff --git a/ports/spresense/mphalport.c b/ports/spresense/mphalport.c new file mode 100644 index 000000000..79d93f975 --- /dev/null +++ b/ports/spresense/mphalport.c @@ -0,0 +1,88 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 +#include + +#include "py/mpstate.h" + +#include "tick.h" + +#define DELAY_CORRECTION (700) +#define DELAY_INTERVAL (50) + +void mp_hal_init(void) { + boardctl(BOARDIOC_INIT, 0); +} + +mp_uint_t mp_hal_ticks_ms(void) { + struct timeval tv; + gettimeofday(&tv, NULL); + return tv.tv_sec * 1000 + tv.tv_usec / 1000; +} + +mp_uint_t mp_hal_ticks_us(void) { + struct timeval tv; + gettimeofday(&tv, NULL); + return tv.tv_sec * 1000000 + tv.tv_usec; +} + +mp_uint_t mp_hal_ticks_cpu(void) { + return cxd56_get_cpu_baseclk(); +} + +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)) || + MP_STATE_VM(mp_pending_exception) == MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_reload_exception))) { + break; + } + duration = (ticks_ms - start_tick); + // TODO(tannewt): Go to sleep for a little while while we wait. + } +} + +void mp_hal_delay_us(uint32_t us) { + if (us) { + unsigned long long ticks = mp_hal_ticks_cpu() / 1000000L * us; + if (ticks < DELAY_CORRECTION) return; // delay time already used in calculation + + ticks -= DELAY_CORRECTION; + ticks /= 6; + // following loop takes 6 cycles + do { + __asm__ __volatile__("nop"); + } while(--ticks); + } +} diff --git a/ports/spresense/mphalport.h b/ports/spresense/mphalport.h new file mode 100644 index 000000000..8b397d41e --- /dev/null +++ b/ports/spresense/mphalport.h @@ -0,0 +1,36 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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. + */ + +#ifndef MICROPY_INCLUDED_SPRESENSE_MPHALPORT_H +#define MICROPY_INCLUDED_SPRESENSE_MPHALPORT_H + +#include + +#include "lib/utils/interrupt_char.h" + +extern volatile uint64_t ticks_ms; + +#endif // MICROPY_INCLUDED_SPRESENSE_MPHALPORT_H diff --git a/ports/spresense/qstrdefsport.h b/ports/spresense/qstrdefsport.h new file mode 100644 index 000000000..3ba897069 --- /dev/null +++ b/ports/spresense/qstrdefsport.h @@ -0,0 +1 @@ +// qstrs specific to this port diff --git a/ports/spresense/supervisor/cpu.s b/ports/spresense/supervisor/cpu.s new file mode 100755 index 000000000..9e6807a5e --- /dev/null +++ b/ports/spresense/supervisor/cpu.s @@ -0,0 +1,27 @@ +.syntax unified +.cpu cortex-m4 +.thumb +.text +.align 2 + +@ uint cpu_get_regs_and_sp(r0=uint regs[10]) +.global cpu_get_regs_and_sp +.thumb +.thumb_func +.type cpu_get_regs_and_sp, %function +cpu_get_regs_and_sp: +@ store registers into given array +str r4, [r0], #4 +str r5, [r0], #4 +str r6, [r0], #4 +str r7, [r0], #4 +str r8, [r0], #4 +str r9, [r0], #4 +str r10, [r0], #4 +str r11, [r0], #4 +str r12, [r0], #4 +str r13, [r0], #4 + +@ return the sp +mov r0, sp +bx lr diff --git a/ports/spresense/supervisor/internal_flash.c b/ports/spresense/supervisor/internal_flash.c new file mode 100644 index 000000000..0c9a61e06 --- /dev/null +++ b/ports/spresense/supervisor/internal_flash.c @@ -0,0 +1,114 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 "supervisor/flash.h" + +/* Prototypes for Remote API */ + +int FM_RawWrite(uint32_t offset, const void *buf, uint32_t size); +int FM_RawVerifyWrite(uint32_t offset, const void *buf, uint32_t size); +int FM_RawRead(uint32_t offset, void *buf, uint32_t size); +int FM_RawEraseSector(uint32_t sector); + +#define CXD56_SPIFLASHSIZE (16 * 1024 * 1024) + +#define SECTOR_SHIFT 12 +#define SECTOR_SIZE (1 << SECTOR_SHIFT) + +#define PAGE_SHIFT 9 +#define PAGE_SIZE (1 << PAGE_SHIFT) + +#define NO_SECTOR 0xffffffff + +uint8_t flash_cache[SECTOR_SIZE]; +uint32_t flash_sector = NO_SECTOR; + +void supervisor_flash_init(void) { +} + +uint32_t supervisor_flash_get_block_size(void) { + return FILESYSTEM_BLOCK_SIZE; +} + +uint32_t supervisor_flash_get_block_count(void) { + return CXD56_SPIFLASHSIZE >> PAGE_SHIFT; +} + +void supervisor_flash_flush(void) { + if (flash_sector == NO_SECTOR) { + return; + } + + FM_RawEraseSector(flash_sector); + FM_RawWrite(flash_sector << SECTOR_SHIFT, flash_cache, SECTOR_SIZE); + + flash_sector = NO_SECTOR; +} + +mp_uint_t supervisor_flash_read_blocks(uint8_t *dest, uint32_t block, uint32_t num_blocks) { + supervisor_flash_flush(); + + if (FM_RawRead(block << PAGE_SHIFT, dest, num_blocks << PAGE_SHIFT) < 0) { + return 1; + } + + return 0; // success +} + +mp_uint_t supervisor_flash_write_blocks(const uint8_t *src, uint32_t lba, uint32_t num_blocks) { + uint32_t sector; + uint32_t block_position; + uint32_t current_block = 0; + + while (num_blocks--) { + sector = (lba << PAGE_SHIFT) >> SECTOR_SHIFT; + block_position = lba - 8 * sector; + + if (sector != flash_sector) { + supervisor_flash_flush(); + + if (FM_RawRead(sector << SECTOR_SHIFT, flash_cache, SECTOR_SIZE) < 0) { + return 1; + } + + if (memcmp(&flash_cache[block_position << PAGE_SHIFT], &src[current_block << PAGE_SHIFT], PAGE_SIZE) != 0) { + flash_sector = sector; + } + } + + memcpy(&flash_cache[block_position << PAGE_SHIFT], &src[current_block << PAGE_SHIFT], PAGE_SIZE); + + lba++; + current_block++; + } + + return 0; // success +} + +void supervisor_flash_release_cache(void) { +} diff --git a/ports/spresense/supervisor/internal_flash.h b/ports/spresense/supervisor/internal_flash.h new file mode 100644 index 000000000..bc4e98baf --- /dev/null +++ b/ports/spresense/supervisor/internal_flash.h @@ -0,0 +1,30 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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. + */ + +#ifndef MICROPY_INCLUDED_SPRESENSE_INTERNAL_FLASH_H +#define MICROPY_INCLUDED_SPRESENSE_INTERNAL_FLASH_H + +#endif // MICROPY_INCLUDED_SPRESENSE_INTERNAL_FLASH_H diff --git a/ports/spresense/supervisor/internal_flash_root_pointers.h b/ports/spresense/supervisor/internal_flash_root_pointers.h new file mode 100644 index 000000000..87f478d13 --- /dev/null +++ b/ports/spresense/supervisor/internal_flash_root_pointers.h @@ -0,0 +1,31 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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. + */ +#ifndef MICROPY_INCLUDED_SPRESENSE_INTERNAL_FLASH_ROOT_POINTERS_H +#define MICROPY_INCLUDED_SPRESENSE_INTERNAL_FLASH_ROOT_POINTERS_H + +#define FLASH_ROOT_POINTERS + +#endif // MICROPY_INCLUDED_SPRESENSE_INTERNAL_FLASH_ROOT_POINTERS_H diff --git a/ports/spresense/supervisor/port.c b/ports/spresense/supervisor/port.c new file mode 100644 index 000000000..ab1ea5273 --- /dev/null +++ b/ports/spresense/supervisor/port.c @@ -0,0 +1,73 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 "supervisor/port.h" + +#include "common-hal/microcontroller/Pin.h" +#include "common-hal/analogio/AnalogIn.h" +#include "common-hal/pulseio/PulseOut.h" +#include "common-hal/pulseio/PWMOut.h" + +safe_mode_t port_init(void) { + boardctl(BOARDIOC_INIT, 0); + return NO_SAFE_MODE; +} + +void reset_cpu(void) { + boardctl(BOARDIOC_RESET, 0); +} + +void reset_port(void) { +#if CIRCUITPY_ANALOGIO + analogin_reset(); +#endif +#if CIRCUITPY_PULSEIO + pulseout_reset(); + pwmout_reset(); +#endif + + reset_all_pins(); +} + +void reset_board(void) { +} + +void reset_to_bootloader(void) { +} + +extern uint32_t _ebss; + +// Place the word to save just after our BSS section that gets blanked. +void port_set_saved_word(uint32_t value) { + _ebss = value; +} + +uint32_t port_get_saved_word(void) { + return _ebss; +} diff --git a/ports/spresense/supervisor/usb.c b/ports/spresense/supervisor/usb.c new file mode 100644 index 000000000..6ad253c6d --- /dev/null +++ b/ports/spresense/supervisor/usb.c @@ -0,0 +1,30 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 "supervisor/usb.h" + +void init_usb_hardware(void) { +} diff --git a/ports/spresense/tick.c b/ports/spresense/tick.c new file mode 100644 index 000000000..6529db790 --- /dev/null +++ b/ports/spresense/tick.c @@ -0,0 +1,45 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 "tick.h" + +#include "supervisor/shared/autoreload.h" +#include "supervisor/filesystem.h" + +// Global millisecond tick count +volatile uint64_t ticks_ms = 0; + +void board_timerhook(void) +{ + ticks_ms += 1; + +#if CIRCUITPY_FILESYSTEM_FLUSH_INTERVAL_MS > 0 + filesystem_tick(); +#endif +#ifdef CIRCUITPY_AUTORELOAD_DELAY_MS + autoreload_tick(); +#endif +} diff --git a/ports/spresense/tick.h b/ports/spresense/tick.h new file mode 100644 index 000000000..7c4d5725f --- /dev/null +++ b/ports/spresense/tick.h @@ -0,0 +1,34 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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. + */ + +#ifndef MICROPY_INCLUDED_SPRESENSE_TICK_H +#define MICROPY_INCLUDED_SPRESENSE_TICK_H + +#include "py/mpconfig.h" + +extern volatile uint64_t ticks_ms; + +#endif // MICROPY_INCLUDED_SPRESENSE_TICK_H -- cgit v1.2.3 From 49db102bdd6ae29a312b34d806a39fd639bc06da Mon Sep 17 00:00:00 2001 From: Kamil Tomaszewski Date: Wed, 9 Oct 2019 08:32:58 +0200 Subject: Update tinyUSB --- lib/tinyusb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tinyusb b/lib/tinyusb index d3e48da5a..c42edc080 160000 --- a/lib/tinyusb +++ b/lib/tinyusb @@ -1 +1 @@ -Subproject commit d3e48da5a1266a88f5f0fdfe28818c8599b09844 +Subproject commit c42edc080cb61d4af8015df6c1d404273b3a5bfa -- cgit v1.2.3 From 6d1748fd59d9e224d6d928fc796a797483e5fe49 Mon Sep 17 00:00:00 2001 From: Kamil Tomaszewski Date: Wed, 9 Oct 2019 08:38:50 +0200 Subject: Add spresense-exported-sdk submodule --- .gitmodules | 3 +++ ports/spresense/spresense-exported-sdk | 1 + 2 files changed, 4 insertions(+) create mode 160000 ports/spresense/spresense-exported-sdk diff --git a/.gitmodules b/.gitmodules index 7b57cab54..d21ed511e 100644 --- a/.gitmodules +++ b/.gitmodules @@ -98,3 +98,6 @@ [submodule "ports/stm32f4/stm32f4"] path = ports/stm32f4/stm32f4 url = https://github.com/adafruit/stm32f4.git +[submodule "ports/spresense/spresense-exported-sdk"] + path = ports/spresense/spresense-exported-sdk + url = https://github.com/sonydevworld/spresense-exported-sdk.git diff --git a/ports/spresense/spresense-exported-sdk b/ports/spresense/spresense-exported-sdk new file mode 160000 index 000000000..b473b28a1 --- /dev/null +++ b/ports/spresense/spresense-exported-sdk @@ -0,0 +1 @@ +Subproject commit b473b28a14a03f3d416b6e2c071bcfd4fb92cb63 -- cgit v1.2.3 From 24c0fe26d86833465bab2dd365effa7bee67c2df Mon Sep 17 00:00:00 2001 From: Kamil Tomaszewski Date: Wed, 9 Oct 2019 12:31:17 +0200 Subject: Exclude spresense-exported-sdk from docs --- conf.py | 1 + 1 file changed, 1 insertion(+) diff --git a/conf.py b/conf.py index 3df958fe1..df6e6ab60 100644 --- a/conf.py +++ b/conf.py @@ -142,6 +142,7 @@ exclude_patterns = ["**/build*", "ports/stm32f4/README.md", "ports/pic16bit", "ports/qemu-arm", + "ports/spresense/spresense-exported-sdk", "ports/stm32", "ports/stm32/hal", "ports/stm32/cmsis", -- cgit v1.2.3 From 0a48282e517413ce402b0ed8fd0be49927236516 Mon Sep 17 00:00:00 2001 From: Kamil Tomaszewski Date: Wed, 9 Oct 2019 13:11:22 +0200 Subject: Add Spresense to supported_ports --- docs/supported_ports.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/supported_ports.rst b/docs/supported_ports.rst index 039ddea68..c256315f7 100644 --- a/docs/supported_ports.rst +++ b/docs/supported_ports.rst @@ -10,3 +10,4 @@ and ESP8266. ../ports/atmel-samd/README ../ports/esp8266/README ../ports/nrf/README + ../ports/spresense/README -- cgit v1.2.3 From 1fa8841d20ef973e71dce52978de59c6dd6f918b Mon Sep 17 00:00:00 2001 From: Kamil Tomaszewski Date: Fri, 11 Oct 2019 08:09:15 +0200 Subject: Change port name to cxd56 --- .gitmodules | 4 +- conf.py | 2 +- docs/supported_ports.rst | 2 +- ports/cxd56/Makefile | 186 +++++++++++++++++++ ports/cxd56/README.md | 86 +++++++++ ports/cxd56/alloca.h | 6 + ports/cxd56/background.c | 53 ++++++ ports/cxd56/background.h | 33 ++++ ports/cxd56/common-hal/analogio/AnalogIn.c | 131 ++++++++++++++ ports/cxd56/common-hal/analogio/AnalogIn.h | 42 +++++ ports/cxd56/common-hal/analogio/AnalogOut.c | 43 +++++ ports/cxd56/common-hal/analogio/AnalogOut.h | 36 ++++ ports/cxd56/common-hal/analogio/__init__.c | 1 + ports/cxd56/common-hal/board/__init__.c | 80 +++++++++ ports/cxd56/common-hal/busio/I2C.c | 123 +++++++++++++ ports/cxd56/common-hal/busio/I2C.h | 43 +++++ ports/cxd56/common-hal/busio/OneWire.h | 33 ++++ ports/cxd56/common-hal/busio/SPI.c | 155 ++++++++++++++++ ports/cxd56/common-hal/busio/SPI.h | 49 ++++++ ports/cxd56/common-hal/busio/UART.c | 168 ++++++++++++++++++ ports/cxd56/common-hal/busio/UART.h | 43 +++++ ports/cxd56/common-hal/busio/__init__.c | 1 + ports/cxd56/common-hal/digitalio/DigitalInOut.c | 136 ++++++++++++++ ports/cxd56/common-hal/digitalio/DigitalInOut.h | 42 +++++ ports/cxd56/common-hal/digitalio/__init__.c | 1 + ports/cxd56/common-hal/microcontroller/Pin.c | 162 +++++++++++++++++ ports/cxd56/common-hal/microcontroller/Pin.h | 92 ++++++++++ ports/cxd56/common-hal/microcontroller/Processor.c | 41 +++++ ports/cxd56/common-hal/microcontroller/Processor.h | 40 +++++ ports/cxd56/common-hal/microcontroller/__init__.c | 111 ++++++++++++ ports/cxd56/common-hal/os/__init__.c | 72 ++++++++ ports/cxd56/common-hal/pulseio/PWMOut.c | 157 +++++++++++++++++ ports/cxd56/common-hal/pulseio/PWMOut.h | 48 +++++ ports/cxd56/common-hal/pulseio/PulseIn.c | 196 +++++++++++++++++++++ ports/cxd56/common-hal/pulseio/PulseIn.h | 47 +++++ ports/cxd56/common-hal/pulseio/PulseOut.c | 126 +++++++++++++ ports/cxd56/common-hal/pulseio/PulseOut.h | 39 ++++ ports/cxd56/common-hal/pulseio/__init__.c | 1 + ports/cxd56/common-hal/rtc/RTC.c | 54 ++++++ ports/cxd56/common-hal/rtc/RTC.h | 30 ++++ ports/cxd56/common-hal/rtc/__init__.c | 1 + ports/cxd56/common-hal/supervisor/Runtime.c | 36 ++++ ports/cxd56/common-hal/supervisor/Runtime.h | 36 ++++ ports/cxd56/common-hal/supervisor/__init__.c | 36 ++++ ports/cxd56/common-hal/time/__init__.c | 37 ++++ ports/cxd56/fatfs_port.c | 46 +++++ ports/cxd56/mpconfigboard.h | 39 ++++ ports/cxd56/mpconfigport.h | 38 ++++ ports/cxd56/mpconfigport.mk | 24 +++ ports/cxd56/mphalport.c | 88 +++++++++ ports/cxd56/mphalport.h | 36 ++++ ports/cxd56/qstrdefsport.h | 1 + ports/cxd56/spresense-exported-sdk | 1 + ports/cxd56/supervisor/cpu.s | 27 +++ ports/cxd56/supervisor/internal_flash.c | 114 ++++++++++++ ports/cxd56/supervisor/internal_flash.h | 30 ++++ .../supervisor/internal_flash_root_pointers.h | 31 ++++ ports/cxd56/supervisor/port.c | 73 ++++++++ ports/cxd56/supervisor/usb.c | 30 ++++ ports/cxd56/tick.c | 45 +++++ ports/cxd56/tick.h | 34 ++++ ports/spresense/Makefile | 186 ------------------- ports/spresense/README.md | 86 --------- ports/spresense/alloca.h | 6 - ports/spresense/background.c | 53 ------ ports/spresense/background.h | 33 ---- ports/spresense/common-hal/analogio/AnalogIn.c | 131 -------------- ports/spresense/common-hal/analogio/AnalogIn.h | 42 ----- ports/spresense/common-hal/analogio/AnalogOut.c | 43 ----- ports/spresense/common-hal/analogio/AnalogOut.h | 36 ---- ports/spresense/common-hal/analogio/__init__.c | 1 - ports/spresense/common-hal/board/__init__.c | 80 --------- ports/spresense/common-hal/busio/I2C.c | 123 ------------- ports/spresense/common-hal/busio/I2C.h | 43 ----- ports/spresense/common-hal/busio/OneWire.h | 33 ---- ports/spresense/common-hal/busio/SPI.c | 155 ---------------- ports/spresense/common-hal/busio/SPI.h | 49 ------ ports/spresense/common-hal/busio/UART.c | 168 ------------------ ports/spresense/common-hal/busio/UART.h | 43 ----- ports/spresense/common-hal/busio/__init__.c | 1 - .../spresense/common-hal/digitalio/DigitalInOut.c | 136 -------------- .../spresense/common-hal/digitalio/DigitalInOut.h | 42 ----- ports/spresense/common-hal/digitalio/__init__.c | 1 - ports/spresense/common-hal/microcontroller/Pin.c | 162 ----------------- ports/spresense/common-hal/microcontroller/Pin.h | 92 ---------- .../common-hal/microcontroller/Processor.c | 41 ----- .../common-hal/microcontroller/Processor.h | 40 ----- .../common-hal/microcontroller/__init__.c | 111 ------------ ports/spresense/common-hal/os/__init__.c | 72 -------- ports/spresense/common-hal/pulseio/PWMOut.c | 157 ----------------- ports/spresense/common-hal/pulseio/PWMOut.h | 48 ----- ports/spresense/common-hal/pulseio/PulseIn.c | 196 --------------------- ports/spresense/common-hal/pulseio/PulseIn.h | 47 ----- ports/spresense/common-hal/pulseio/PulseOut.c | 126 ------------- ports/spresense/common-hal/pulseio/PulseOut.h | 39 ---- ports/spresense/common-hal/pulseio/__init__.c | 1 - ports/spresense/common-hal/rtc/RTC.c | 54 ------ ports/spresense/common-hal/rtc/RTC.h | 30 ---- ports/spresense/common-hal/rtc/__init__.c | 1 - ports/spresense/common-hal/supervisor/Runtime.c | 36 ---- ports/spresense/common-hal/supervisor/Runtime.h | 36 ---- ports/spresense/common-hal/supervisor/__init__.c | 36 ---- ports/spresense/common-hal/time/__init__.c | 37 ---- ports/spresense/fatfs_port.c | 46 ----- ports/spresense/mpconfigboard.h | 39 ---- ports/spresense/mpconfigport.h | 38 ---- ports/spresense/mpconfigport.mk | 24 --- ports/spresense/mphalport.c | 88 --------- ports/spresense/mphalport.h | 36 ---- ports/spresense/qstrdefsport.h | 1 - ports/spresense/spresense-exported-sdk | 1 - ports/spresense/supervisor/cpu.s | 27 --- ports/spresense/supervisor/internal_flash.c | 114 ------------ ports/spresense/supervisor/internal_flash.h | 30 ---- .../supervisor/internal_flash_root_pointers.h | 31 ---- ports/spresense/supervisor/port.c | 73 -------- ports/spresense/supervisor/usb.c | 30 ---- ports/spresense/tick.c | 45 ----- ports/spresense/tick.h | 34 ---- 119 files changed, 3514 insertions(+), 3514 deletions(-) create mode 100644 ports/cxd56/Makefile create mode 100644 ports/cxd56/README.md create mode 100644 ports/cxd56/alloca.h create mode 100644 ports/cxd56/background.c create mode 100644 ports/cxd56/background.h create mode 100644 ports/cxd56/common-hal/analogio/AnalogIn.c create mode 100644 ports/cxd56/common-hal/analogio/AnalogIn.h create mode 100644 ports/cxd56/common-hal/analogio/AnalogOut.c create mode 100644 ports/cxd56/common-hal/analogio/AnalogOut.h create mode 100644 ports/cxd56/common-hal/analogio/__init__.c create mode 100644 ports/cxd56/common-hal/board/__init__.c create mode 100644 ports/cxd56/common-hal/busio/I2C.c create mode 100644 ports/cxd56/common-hal/busio/I2C.h create mode 100644 ports/cxd56/common-hal/busio/OneWire.h create mode 100644 ports/cxd56/common-hal/busio/SPI.c create mode 100644 ports/cxd56/common-hal/busio/SPI.h create mode 100644 ports/cxd56/common-hal/busio/UART.c create mode 100644 ports/cxd56/common-hal/busio/UART.h create mode 100644 ports/cxd56/common-hal/busio/__init__.c create mode 100644 ports/cxd56/common-hal/digitalio/DigitalInOut.c create mode 100644 ports/cxd56/common-hal/digitalio/DigitalInOut.h create mode 100644 ports/cxd56/common-hal/digitalio/__init__.c create mode 100644 ports/cxd56/common-hal/microcontroller/Pin.c create mode 100644 ports/cxd56/common-hal/microcontroller/Pin.h create mode 100644 ports/cxd56/common-hal/microcontroller/Processor.c create mode 100644 ports/cxd56/common-hal/microcontroller/Processor.h create mode 100644 ports/cxd56/common-hal/microcontroller/__init__.c create mode 100644 ports/cxd56/common-hal/os/__init__.c create mode 100644 ports/cxd56/common-hal/pulseio/PWMOut.c create mode 100644 ports/cxd56/common-hal/pulseio/PWMOut.h create mode 100644 ports/cxd56/common-hal/pulseio/PulseIn.c create mode 100644 ports/cxd56/common-hal/pulseio/PulseIn.h create mode 100644 ports/cxd56/common-hal/pulseio/PulseOut.c create mode 100644 ports/cxd56/common-hal/pulseio/PulseOut.h create mode 100644 ports/cxd56/common-hal/pulseio/__init__.c create mode 100644 ports/cxd56/common-hal/rtc/RTC.c create mode 100644 ports/cxd56/common-hal/rtc/RTC.h create mode 100644 ports/cxd56/common-hal/rtc/__init__.c create mode 100755 ports/cxd56/common-hal/supervisor/Runtime.c create mode 100755 ports/cxd56/common-hal/supervisor/Runtime.h create mode 100755 ports/cxd56/common-hal/supervisor/__init__.c create mode 100644 ports/cxd56/common-hal/time/__init__.c create mode 100644 ports/cxd56/fatfs_port.c create mode 100644 ports/cxd56/mpconfigboard.h create mode 100644 ports/cxd56/mpconfigport.h create mode 100644 ports/cxd56/mpconfigport.mk create mode 100644 ports/cxd56/mphalport.c create mode 100644 ports/cxd56/mphalport.h create mode 100644 ports/cxd56/qstrdefsport.h create mode 160000 ports/cxd56/spresense-exported-sdk create mode 100755 ports/cxd56/supervisor/cpu.s create mode 100644 ports/cxd56/supervisor/internal_flash.c create mode 100644 ports/cxd56/supervisor/internal_flash.h create mode 100644 ports/cxd56/supervisor/internal_flash_root_pointers.h create mode 100644 ports/cxd56/supervisor/port.c create mode 100644 ports/cxd56/supervisor/usb.c create mode 100644 ports/cxd56/tick.c create mode 100644 ports/cxd56/tick.h delete mode 100644 ports/spresense/Makefile delete mode 100644 ports/spresense/README.md delete mode 100644 ports/spresense/alloca.h delete mode 100644 ports/spresense/background.c delete mode 100644 ports/spresense/background.h delete mode 100644 ports/spresense/common-hal/analogio/AnalogIn.c delete mode 100644 ports/spresense/common-hal/analogio/AnalogIn.h delete mode 100644 ports/spresense/common-hal/analogio/AnalogOut.c delete mode 100644 ports/spresense/common-hal/analogio/AnalogOut.h delete mode 100644 ports/spresense/common-hal/analogio/__init__.c delete mode 100644 ports/spresense/common-hal/board/__init__.c delete mode 100644 ports/spresense/common-hal/busio/I2C.c delete mode 100644 ports/spresense/common-hal/busio/I2C.h delete mode 100644 ports/spresense/common-hal/busio/OneWire.h delete mode 100644 ports/spresense/common-hal/busio/SPI.c delete mode 100644 ports/spresense/common-hal/busio/SPI.h delete mode 100644 ports/spresense/common-hal/busio/UART.c delete mode 100644 ports/spresense/common-hal/busio/UART.h delete mode 100644 ports/spresense/common-hal/busio/__init__.c delete mode 100644 ports/spresense/common-hal/digitalio/DigitalInOut.c delete mode 100644 ports/spresense/common-hal/digitalio/DigitalInOut.h delete mode 100644 ports/spresense/common-hal/digitalio/__init__.c delete mode 100644 ports/spresense/common-hal/microcontroller/Pin.c delete mode 100644 ports/spresense/common-hal/microcontroller/Pin.h delete mode 100644 ports/spresense/common-hal/microcontroller/Processor.c delete mode 100644 ports/spresense/common-hal/microcontroller/Processor.h delete mode 100644 ports/spresense/common-hal/microcontroller/__init__.c delete mode 100644 ports/spresense/common-hal/os/__init__.c delete mode 100644 ports/spresense/common-hal/pulseio/PWMOut.c delete mode 100644 ports/spresense/common-hal/pulseio/PWMOut.h delete mode 100644 ports/spresense/common-hal/pulseio/PulseIn.c delete mode 100644 ports/spresense/common-hal/pulseio/PulseIn.h delete mode 100644 ports/spresense/common-hal/pulseio/PulseOut.c delete mode 100644 ports/spresense/common-hal/pulseio/PulseOut.h delete mode 100644 ports/spresense/common-hal/pulseio/__init__.c delete mode 100644 ports/spresense/common-hal/rtc/RTC.c delete mode 100644 ports/spresense/common-hal/rtc/RTC.h delete mode 100644 ports/spresense/common-hal/rtc/__init__.c delete mode 100755 ports/spresense/common-hal/supervisor/Runtime.c delete mode 100755 ports/spresense/common-hal/supervisor/Runtime.h delete mode 100755 ports/spresense/common-hal/supervisor/__init__.c delete mode 100644 ports/spresense/common-hal/time/__init__.c delete mode 100644 ports/spresense/fatfs_port.c delete mode 100644 ports/spresense/mpconfigboard.h delete mode 100644 ports/spresense/mpconfigport.h delete mode 100644 ports/spresense/mpconfigport.mk delete mode 100644 ports/spresense/mphalport.c delete mode 100644 ports/spresense/mphalport.h delete mode 100644 ports/spresense/qstrdefsport.h delete mode 160000 ports/spresense/spresense-exported-sdk delete mode 100755 ports/spresense/supervisor/cpu.s delete mode 100644 ports/spresense/supervisor/internal_flash.c delete mode 100644 ports/spresense/supervisor/internal_flash.h delete mode 100644 ports/spresense/supervisor/internal_flash_root_pointers.h delete mode 100644 ports/spresense/supervisor/port.c delete mode 100644 ports/spresense/supervisor/usb.c delete mode 100644 ports/spresense/tick.c delete mode 100644 ports/spresense/tick.h diff --git a/.gitmodules b/.gitmodules index d21ed511e..bb62a5c39 100644 --- a/.gitmodules +++ b/.gitmodules @@ -98,6 +98,6 @@ [submodule "ports/stm32f4/stm32f4"] path = ports/stm32f4/stm32f4 url = https://github.com/adafruit/stm32f4.git -[submodule "ports/spresense/spresense-exported-sdk"] - path = ports/spresense/spresense-exported-sdk +[submodule "ports/cxd56/spresense-exported-sdk"] + path = ports/cxd56/spresense-exported-sdk url = https://github.com/sonydevworld/spresense-exported-sdk.git diff --git a/conf.py b/conf.py index df6e6ab60..a477016dc 100644 --- a/conf.py +++ b/conf.py @@ -125,6 +125,7 @@ exclude_patterns = ["**/build*", "ports/cc3200", "ports/cc3200/FreeRTOS", "ports/cc3200/hal", + "ports/cxd56/spresense-exported-sdk", "ports/esp32", "ports/esp8266/boards", "ports/esp8266/common-hal", @@ -142,7 +143,6 @@ exclude_patterns = ["**/build*", "ports/stm32f4/README.md", "ports/pic16bit", "ports/qemu-arm", - "ports/spresense/spresense-exported-sdk", "ports/stm32", "ports/stm32/hal", "ports/stm32/cmsis", diff --git a/docs/supported_ports.rst b/docs/supported_ports.rst index c256315f7..e221d8665 100644 --- a/docs/supported_ports.rst +++ b/docs/supported_ports.rst @@ -10,4 +10,4 @@ and ESP8266. ../ports/atmel-samd/README ../ports/esp8266/README ../ports/nrf/README - ../ports/spresense/README + ../ports/cxd56/README diff --git a/ports/cxd56/Makefile b/ports/cxd56/Makefile new file mode 100644 index 000000000..d8f809635 --- /dev/null +++ b/ports/cxd56/Makefile @@ -0,0 +1,186 @@ +# This file is part of the MicroPython project, http://micropython.org/ +# +# The MIT License (MIT) +# +# Copyright 2019 Sony Semiconductor Solutions 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 ../../py/mkenv.mk + +# Port-specific +include mpconfigport.mk + +# CircuitPython-specific +include $(TOP)/py/circuitpy_mpconfig.mk + +# qstr definitions (must come before including py.mk) +QSTR_DEFS = qstrdefsport.h + +# include py core make definitions +include $(TOP)/py/py.mk + +include $(TOP)/supervisor/supervisor.mk + +# Include make rules and variables common across CircuitPython builds. +include $(TOP)/py/circuitpy_defns.mk + +CROSS_COMPILE = arm-none-eabi- + +SPRESENSE_SDK = spresense-exported-sdk + +FIRMWARE = $(SPRESENSE_SDK)/firmware + +INC += \ + -I. \ + -I../.. \ + -I../lib/mp-readline \ + -I../lib/timeutils \ + -I../../lib/tinyusb/src \ + -I../../supervisor/shared/usb \ + -I$(BUILD) \ + -I$(SPRESENSE_SDK)/nuttx/include \ + -I$(SPRESENSE_SDK)/nuttx/arch \ + -I$(SPRESENSE_SDK)/nuttx/arch/chip \ + -I$(SPRESENSE_SDK)/sdk/bsp/include \ + -I$(SPRESENSE_SDK)/sdk/bsp/include/sdk \ + +CFLAGS += \ + $(INC) \ + -DCONFIG_WCHAR_BUILTIN \ + -DCONFIG_HAVE_DOUBLE \ + -Dmain=spresense_main \ + -D_estack=__stack \ + -c \ + -Os \ + -pipe \ + -std=gnu11 \ + -mcpu=cortex-m4 \ + -mthumb \ + -mfpu=fpv4-sp-d16 \ + -mfloat-abi=hard \ + -mabi=aapcs \ + -fno-builtin \ + -fno-strict-aliasing \ + -fno-strength-reduce \ + -fomit-frame-pointer \ + -ffunction-sections \ + -fdata-sections \ + -Wall \ + +LIBM = "${shell "$(CC)" $(CFLAGS) -print-file-name=libm.a}" + +LIBGCC = "${shell "$(CC)" $(CFLAGS) -print-libgcc-file-name}" + +LDFLAGS = \ + --entry=__start \ + -nostartfiles \ + -nodefaultlibs \ + --defsym __stack=_vectors+786432 \ + -T$(SPRESENSE_SDK)/nuttx/build/ramconfig.ld \ + --gc-sections \ + -Map=$(BUILD)/output.map \ + -o $(BUILD)/circuitpython.elf \ + --start-group \ + -u spresense_main \ + $(BUILD)/libmpy.a \ + $(SPRESENSE_SDK)/sdk/libs/libapps.a \ + $(SPRESENSE_SDK)/sdk/libs/libsdk.a \ + $(LIBM) \ + $(LIBGCC) \ + --end-group \ + -L$(BUILD) \ + +CFLAGS += -DCFG_TUSB_MCU=OPT_MCU_CXD56 -DCFG_TUD_MIDI_RX_BUFSIZE=128 -DCFG_TUD_CDC_RX_BUFSIZE=256 -DCFG_TUD_MIDI_TX_BUFSIZE=128 -DCFG_TUD_CDC_TX_BUFSIZE=256 -DCFG_TUD_MSC_BUFSIZE=512 $(CFLAGS_MOD) + +SRC_COMMON_HAL_EXPANDED = $(addprefix shared-bindings/, $(SRC_COMMON_HAL)) \ + $(addprefix shared-bindings/, $(SRC_BINDINGS_ENUMS)) \ + $(addprefix common-hal/, $(SRC_COMMON_HAL)) + +SRC_SHARED_MODULE_EXPANDED = $(addprefix shared-bindings/, $(SRC_SHARED_MODULE)) \ + $(addprefix shared-module/, $(SRC_SHARED_MODULE)) + +SRC_S = supervisor/cpu.s + +SRC_C = \ + tick.c \ + background.c \ + fatfs_port.c \ + mphalport.c \ + lib/utils/stdout_helpers.c \ + lib/utils/pyexec.c \ + lib/libc/string0.c \ + lib/mp-readline/readline.c \ + lib/timeutils/timeutils.c \ + lib/oofatfs/ff.c \ + lib/oofatfs/option/ccsbcs.c \ + lib/utils/interrupt_char.c \ + lib/utils/sys_stdio_mphal.c \ + lib/utils/context_manager_helpers.c \ + lib/utils/buffer_helper.c \ + supervisor/shared/memory.c \ + lib/tinyusb/src/portable/sony/cxd56/dcd_cxd56.c \ + +OBJ = $(PY_O) $(SUPERVISOR_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) +OBJ += $(addprefix $(BUILD)/, $(SRC_S:.s=.o)) +OBJ += $(addprefix $(BUILD)/, $(SRC_COMMON_HAL_EXPANDED:.c=.o)) +OBJ += $(addprefix $(BUILD)/, $(SRC_SHARED_MODULE_EXPANDED:.c=.o)) + +# List of sources for qstr extraction +SRC_QSTR += $(SRC_C) $(SRC_SUPERVISOR) $(SRC_COMMON_HAL_EXPANDED) $(SRC_SHARED_MODULE_EXPANDED) +# Sources that only hold QSTRs after pre-processing. +SRC_QSTR_PREPROCESSOR += + +all: $(BUILD)/circuitpython.spk + +$(FIRMWARE): + $(ECHO) "" + $(ECHO) "Download the spresense binaries zip archive from:" + $(ECHO) "https://developer.sony.com/file/download/download-spresense-firmware-v1-4-000" + $(ECHO) "Extract spresense binaries to $(FIRMWARE)" + $(ECHO) "" + $(ECHO) "run make flash-bootloader again to flash bootloader." + exit 1 + +$(BUILD)/libmpy.a: $(SPRESENSE_SDK) $(OBJ) + $(ECHO) "AR $@" + $(Q)$(AR) rcs $(BUILD)/libmpy.a $(OBJ) + +$(BUILD)/circuitpython.elf: $(BUILD)/libmpy.a + $(ECHO) "LD $@" + $(Q)$(LD) $(LDFLAGS) + +$(BUILD)/circuitpython.spk: $(BUILD)/circuitpython.elf + $(ECHO) "Creating $@" + $(SPRESENSE_SDK)/sdk/tools/linux/mkspk -c 2 $(BUILD)/circuitpython.elf nuttx $(BUILD)/circuitpython.spk + +flash: $(BUILD)/circuitpython.spk + $(ECHO) "Writing $< to the board" + $(SPRESENSE_SDK)/sdk/tools/flash.sh -c /dev/ttyUSB0 $(BUILD)/circuitpython.spk + +flash-bootloader: $(SPRESENSE_SDK) $(FIRMWARE) + $(ECHO) "Writing loader to the board" + $(SPRESENSE_SDK)/sdk/tools/flash.sh -l $(FIRMWARE) -c /dev/ttyUSB0 + +include $(TOP)/py/mkrules.mk + +# Print out the value of a make variable. +# https://stackoverflow.com/questions/16467718/how-to-print-out-a-variable-in-makefile +print-%: + @echo $* = $($*) diff --git a/ports/cxd56/README.md b/ports/cxd56/README.md new file mode 100644 index 000000000..d66ea40b4 --- /dev/null +++ b/ports/cxd56/README.md @@ -0,0 +1,86 @@ +CircuitPython port to Spresense +============================== + +This directory contains the port of CircuitPython to Spresense. It is a compact +development board based on Sony’s power-efficient multicore microcontroller +CXD5602. + +Board features: + +* Integrated GPS + * The embedded GNSS with support for GPS, QZSS and GLONASS enables applications + where tracking is required. +* Hi-res audio output and multi mic inputs + * Advanced 192kHz/24 bit audio codec and amplifier for audio output, and + support for up to 8 mic input channels. +* Multicore microcontroller + * Spresense is powered by Sony's CXD5602 microcontroller (ARM® Cortex®-M4F × 6 + cores), with a clock speed of 156 MHz. + +Currently, Spresense port does not support GNSS, Audio and Multicore. + +Refer to [developer.sony.com/develop/spresense/](https://developer.sony.com/develop/spresense/) +for further information about this board. + +Build instructions +------------------ + +Pull all submodules into your clone: + + $ git submodule update --init --recursive + +Build the MicroPython cross-compiler: + + $ make -C mpy-cross + +Change directory to cxd56: + + $ cd ports/cxd56 + +To build circuitpython image run: + + $ make + +USB connection +-------------- + +Add user to `dialout` group: + + $ sudo usermod -a -G dialout + +Connect the Spresense main board to the PC via the USB cable. + +Flash the bootloader +-------------------- + +The correct bootloader is required for the Spresense board to function. + +Bootloader information: + +* The bootloader has to be flashed the very first time the board is used. + +* You have to accept the End User License Agreement to be able to download and use the Spresense bootloader binary. + +Download the spresense binaries zip archive from: + +Extract spresense binaries in your PC to ports/spresense/spresense-exported-sdk/firmware/ + +To flash the bootloader run the command: + + $ make flash-bootloader + +Flash the circuitpython.spk image +--------------------------------- + +To flash the firmware run the command: + + $ make flash + +Accessing the board +------------------- + +Connect the Spresense extension board to the PC via the USB cable. + +Once built and deployed, access the CircuitPython REPL (the Python prompt) via USB. You can run: + + $ screen /dev/ttyACM0 115200 diff --git a/ports/cxd56/alloca.h b/ports/cxd56/alloca.h new file mode 100644 index 000000000..aad2f8b5b --- /dev/null +++ b/ports/cxd56/alloca.h @@ -0,0 +1,6 @@ +#ifndef _ALLOCA_H +#define _ALLOCA_H + +#define alloca __builtin_alloca + +#endif /* _ALLOCA_H */ diff --git a/ports/cxd56/background.c b/ports/cxd56/background.c new file mode 100644 index 000000000..ade257dd2 --- /dev/null +++ b/ports/cxd56/background.c @@ -0,0 +1,53 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 "background.h" + +#include "supervisor/usb.h" +#include "supervisor/filesystem.h" +#include "supervisor/shared/stack.h" + +static bool running_background_tasks = false; + +void background_tasks_reset(void) { + running_background_tasks = false; +} + +void run_background_tasks(void) { + // Don't call ourselves recursively. + if (running_background_tasks) { + return; + } + + assert_heap_ok(); + running_background_tasks = true; + + usb_background(); + filesystem_background(); + + running_background_tasks = false; + assert_heap_ok(); +} diff --git a/ports/cxd56/background.h b/ports/cxd56/background.h new file mode 100644 index 000000000..a38e3faed --- /dev/null +++ b/ports/cxd56/background.h @@ -0,0 +1,33 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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. + */ + +#ifndef MICROPY_INCLUDED_CXD56_BACKGROUND_H +#define MICROPY_INCLUDED_CXD56_BACKGROUND_H + +void background_tasks_reset(void); +void run_background_tasks(void); + +#endif // MICROPY_INCLUDED_CXD56_BACKGROUND_H diff --git a/ports/cxd56/common-hal/analogio/AnalogIn.c b/ports/cxd56/common-hal/analogio/AnalogIn.c new file mode 100644 index 000000000..438fbbcae --- /dev/null +++ b/ports/cxd56/common-hal/analogio/AnalogIn.c @@ -0,0 +1,131 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 + +#include +#include + +#include "py/runtime.h" + +#include "shared-bindings/analogio/AnalogIn.h" + +typedef struct { + const char* devpath; + const mcu_pin_obj_t *pin; + int fd; +} analogin_dev_t; + +STATIC analogin_dev_t analogin_dev[] = { + {"/dev/lpadc0", &pin_A0, -1}, + {"/dev/lpadc1", &pin_A1, -1}, + {"/dev/lpadc2", &pin_A2, -1}, + {"/dev/lpadc3", &pin_A3, -1}, + {"/dev/hpadc0", &pin_A4, -1}, + {"/dev/hpadc1", &pin_A5, -1}, +}; + +void common_hal_analogio_analogin_construct(analogio_analogin_obj_t *self, const mcu_pin_obj_t *pin) { + if (!pin->analog) { + mp_raise_ValueError(translate("AnalogIn not supported on given pin")); + } + + self->number = -1; + + for (int i = 0; i < MP_ARRAY_SIZE(analogin_dev); i++) { + if (pin->number == analogin_dev[i].pin->number) { + self->number = i; + break; + } + } + + if (self->number < 0) { + mp_raise_ValueError(translate("Pin does not have ADC capabilities")); + } + + analogin_dev[self->number].fd = open(analogin_dev[pin->number].devpath, O_RDONLY); + if (analogin_dev[self->number].fd < 0) { + mp_raise_ValueError(translate("Pin does not have ADC capabilities")); + } + + // SCU FIFO overwrite + ioctl(analogin_dev[self->number].fd, SCUIOC_SETFIFOMODE, 1); + + // ADC FIFO size + ioctl(analogin_dev[self->number].fd, ANIOC_CXD56_FIFOSIZE, 2); + + // start ADC + ioctl(analogin_dev[self->number].fd, ANIOC_CXD56_START, 0); + + self->pin = pin; +} + +void common_hal_analogio_analogin_deinit(analogio_analogin_obj_t *self) { + if (common_hal_analogio_analogin_deinited(self)) { + return; + } + + // stop ADC + ioctl(analogin_dev[self->number].fd, ANIOC_CXD56_STOP, 0); + close(analogin_dev[self->number].fd); + analogin_dev[self->number].fd = -1; + + self->pin = mp_const_none; +} + +bool common_hal_analogio_analogin_deinited(analogio_analogin_obj_t *self) { + return self->pin == mp_const_none; +} + +uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) { + uint16_t value = 0; + + read(analogin_dev[self->number].fd, &value, sizeof(value)); + + return value; +} + +// Reference voltage is a fixed value which is depending on the board. +// e.g.) +// - Reference Voltage of A4 and A5 pins on Main Board is 0.7V. +// - Reference Voltage of A0 ~ A5 pins on External Interface board +// is selected 3.3V or 5.0V by a IO Volt jumper pin. +float common_hal_analogio_analogin_get_reference_voltage(analogio_analogin_obj_t *self) { + return 3.3f; +} + +void analogin_reset(void) { + for (int i = 0; i < MP_ARRAY_SIZE(analogin_dev); i++) { + if (analogin_dev[i].fd >= 0) { + // stop ADC + ioctl(analogin_dev[i].fd, ANIOC_CXD56_STOP, 0); + close(analogin_dev[i].fd); + analogin_dev[i].fd = -1; + } + } +} diff --git a/ports/cxd56/common-hal/analogio/AnalogIn.h b/ports/cxd56/common-hal/analogio/AnalogIn.h new file mode 100644 index 000000000..4e8b41e52 --- /dev/null +++ b/ports/cxd56/common-hal/analogio/AnalogIn.h @@ -0,0 +1,42 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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. + */ + +#ifndef MICROPY_INCLUDED_CXD56_COMMON_HAL_ANALOGIO_ANALOGIN_H +#define MICROPY_INCLUDED_CXD56_COMMON_HAL_ANALOGIO_ANALOGIN_H + +#include "py/obj.h" + +#include "common-hal/microcontroller/Pin.h" + +typedef struct { + mp_obj_base_t base; + const mcu_pin_obj_t *pin; + uint8_t number; +} analogio_analogin_obj_t; + +void analogin_reset(void); + +#endif // MICROPY_INCLUDED_CXD56_COMMON_HAL_ANALOGIO_ANALOGIN_H diff --git a/ports/cxd56/common-hal/analogio/AnalogOut.c b/ports/cxd56/common-hal/analogio/AnalogOut.c new file mode 100644 index 000000000..3f1abe80d --- /dev/null +++ b/ports/cxd56/common-hal/analogio/AnalogOut.c @@ -0,0 +1,43 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 "py/runtime.h" + +#include "shared-bindings/analogio/AnalogOut.h" + +void common_hal_analogio_analogout_construct(analogio_analogout_obj_t *self, const mcu_pin_obj_t *pin) { + mp_raise_RuntimeError(translate("AnalogOut functionality not supported")); +} + +void common_hal_analogio_analogout_deinit(analogio_analogout_obj_t *self) { +} + +bool common_hal_analogio_analogout_deinited(analogio_analogout_obj_t *self) { + return true; +} + +void common_hal_analogio_analogout_set_value(analogio_analogout_obj_t *self, uint16_t value) { +} diff --git a/ports/cxd56/common-hal/analogio/AnalogOut.h b/ports/cxd56/common-hal/analogio/AnalogOut.h new file mode 100644 index 000000000..b0fd65265 --- /dev/null +++ b/ports/cxd56/common-hal/analogio/AnalogOut.h @@ -0,0 +1,36 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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. + */ + +#ifndef MICROPY_INCLUDED_CXD56_COMMON_HAL_ANALOGIO_ANALOGOUT_H +#define MICROPY_INCLUDED_CXD56_COMMON_HAL_ANALOGIO_ANALOGOUT_H + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; +} analogio_analogout_obj_t; + +#endif // MICROPY_INCLUDED_CXD56_COMMON_HAL_ANALOGIO_ANALOGOUT_H diff --git a/ports/cxd56/common-hal/analogio/__init__.c b/ports/cxd56/common-hal/analogio/__init__.c new file mode 100644 index 000000000..eea58c77d --- /dev/null +++ b/ports/cxd56/common-hal/analogio/__init__.c @@ -0,0 +1 @@ +// No analogio module functions. diff --git a/ports/cxd56/common-hal/board/__init__.c b/ports/cxd56/common-hal/board/__init__.c new file mode 100644 index 000000000..d123969fe --- /dev/null +++ b/ports/cxd56/common-hal/board/__init__.c @@ -0,0 +1,80 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 "shared-bindings/board/__init__.h" + +STATIC const mp_rom_map_elem_t board_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR_D0), MP_ROM_PTR(&pin_D0) }, + { MP_ROM_QSTR(MP_QSTR_D1), MP_ROM_PTR(&pin_D1) }, + { MP_ROM_QSTR(MP_QSTR_D2), MP_ROM_PTR(&pin_D2) }, + { MP_ROM_QSTR(MP_QSTR_D3), MP_ROM_PTR(&pin_D3) }, + { MP_ROM_QSTR(MP_QSTR_D4), MP_ROM_PTR(&pin_D4) }, + { MP_ROM_QSTR(MP_QSTR_D5), MP_ROM_PTR(&pin_D5) }, + { MP_ROM_QSTR(MP_QSTR_D6), MP_ROM_PTR(&pin_D6) }, + { MP_ROM_QSTR(MP_QSTR_D7), MP_ROM_PTR(&pin_D7) }, + { MP_ROM_QSTR(MP_QSTR_D8), MP_ROM_PTR(&pin_D8) }, + { MP_ROM_QSTR(MP_QSTR_D9), MP_ROM_PTR(&pin_D9) }, + { MP_ROM_QSTR(MP_QSTR_D10), MP_ROM_PTR(&pin_D10) }, + { MP_ROM_QSTR(MP_QSTR_D11), MP_ROM_PTR(&pin_D11) }, + { MP_ROM_QSTR(MP_QSTR_D12), MP_ROM_PTR(&pin_D12) }, + { MP_ROM_QSTR(MP_QSTR_D13), MP_ROM_PTR(&pin_D13) }, + { MP_ROM_QSTR(MP_QSTR_D14), MP_ROM_PTR(&pin_D14) }, + { MP_ROM_QSTR(MP_QSTR_D15), MP_ROM_PTR(&pin_D15) }, + { MP_ROM_QSTR(MP_QSTR_D16), MP_ROM_PTR(&pin_D16) }, + { MP_ROM_QSTR(MP_QSTR_D17), MP_ROM_PTR(&pin_D17) }, + { MP_ROM_QSTR(MP_QSTR_D18), MP_ROM_PTR(&pin_D18) }, + { MP_ROM_QSTR(MP_QSTR_D19), MP_ROM_PTR(&pin_D19) }, + { MP_ROM_QSTR(MP_QSTR_D20), MP_ROM_PTR(&pin_D20) }, + { MP_ROM_QSTR(MP_QSTR_D21), MP_ROM_PTR(&pin_D21) }, + { MP_ROM_QSTR(MP_QSTR_D22), MP_ROM_PTR(&pin_D22) }, + { MP_ROM_QSTR(MP_QSTR_D23), MP_ROM_PTR(&pin_D23) }, + { MP_ROM_QSTR(MP_QSTR_D24), MP_ROM_PTR(&pin_D24) }, + { MP_ROM_QSTR(MP_QSTR_D25), MP_ROM_PTR(&pin_D25) }, + { MP_ROM_QSTR(MP_QSTR_D26), MP_ROM_PTR(&pin_D26) }, + { MP_ROM_QSTR(MP_QSTR_D27), MP_ROM_PTR(&pin_D27) }, + { MP_ROM_QSTR(MP_QSTR_D28), MP_ROM_PTR(&pin_D28) }, + { MP_ROM_QSTR(MP_QSTR_LED0), MP_ROM_PTR(&pin_LED0) }, + { MP_ROM_QSTR(MP_QSTR_LED1), MP_ROM_PTR(&pin_LED1) }, + { MP_ROM_QSTR(MP_QSTR_LED2), MP_ROM_PTR(&pin_LED2) }, + { MP_ROM_QSTR(MP_QSTR_LED3), MP_ROM_PTR(&pin_LED3) }, + { MP_ROM_QSTR(MP_QSTR_A0), MP_ROM_PTR(&pin_A0) }, + { MP_ROM_QSTR(MP_QSTR_A1), MP_ROM_PTR(&pin_A1) }, + { MP_ROM_QSTR(MP_QSTR_A2), MP_ROM_PTR(&pin_A2) }, + { MP_ROM_QSTR(MP_QSTR_A3), MP_ROM_PTR(&pin_A3) }, + { MP_ROM_QSTR(MP_QSTR_A4), MP_ROM_PTR(&pin_A4) }, + { MP_ROM_QSTR(MP_QSTR_A5), MP_ROM_PTR(&pin_A5) }, + { MP_ROM_QSTR(MP_QSTR_SDA), MP_ROM_PTR(&pin_D14) }, + { MP_ROM_QSTR(MP_QSTR_SCL), MP_ROM_PTR(&pin_D15) }, + { MP_ROM_QSTR(MP_QSTR_SCK), MP_ROM_PTR(&pin_D13) }, + { MP_ROM_QSTR(MP_QSTR_MISO), MP_ROM_PTR(&pin_D12) }, + { MP_ROM_QSTR(MP_QSTR_MOSI), MP_ROM_PTR(&pin_D11) }, + { MP_ROM_QSTR(MP_QSTR_RX), MP_ROM_PTR(&pin_D0) }, + { MP_ROM_QSTR(MP_QSTR_TX), MP_ROM_PTR(&pin_D1) }, + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, + { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, + { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&board_uart_obj) }, +}; +MP_DEFINE_CONST_DICT(board_module_globals, board_module_globals_table); diff --git a/ports/cxd56/common-hal/busio/I2C.c b/ports/cxd56/common-hal/busio/I2C.c new file mode 100644 index 000000000..c163c183a --- /dev/null +++ b/ports/cxd56/common-hal/busio/I2C.c @@ -0,0 +1,123 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 + +#include "py/runtime.h" + +#include "shared-bindings/busio/I2C.h" + +void common_hal_busio_i2c_construct(busio_i2c_obj_t *self, const mcu_pin_obj_t *scl, + const mcu_pin_obj_t *sda, uint32_t frequency, uint32_t timeout) { + if (frequency != I2C_SPEED_STANDARD && frequency != I2C_SPEED_FAST) { + mp_raise_ValueError(translate("Unsupported baudrate")); + } + + if (scl->number != PIN_I2C0_BCK || sda->number != PIN_I2C0_BDT) { + mp_raise_ValueError(translate("Invalid pins")); + } + + claim_pin(scl); + claim_pin(sda); + + self->scl_pin = scl; + self->sda_pin = sda; + self->frequency = frequency; + self->i2c_dev = cxd56_i2cbus_initialize(0); +} + +void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) { + if (common_hal_busio_i2c_deinited(self)) { + return; + } + + cxd56_i2cbus_uninitialize(self->i2c_dev); + self->i2c_dev = NULL; + + reset_pin_number(self->scl_pin->number); + reset_pin_number(self->sda_pin->number); +} + +bool common_hal_busio_i2c_deinited(busio_i2c_obj_t *self) { + return self->i2c_dev == NULL; +} + +bool common_hal_busio_i2c_try_lock(busio_i2c_obj_t *self) { + bool grabbed_lock = false; + if (!self->has_lock) { + grabbed_lock = true; + self->has_lock = true; + } + return grabbed_lock; +} + +bool common_hal_busio_i2c_has_lock(busio_i2c_obj_t *self) { + return self->has_lock; +} + +void common_hal_busio_i2c_unlock(busio_i2c_obj_t *self) { + self->has_lock = false; +} + +bool common_hal_busio_i2c_probe(busio_i2c_obj_t *self, uint8_t addr) { + struct i2c_msg_s msg; + + msg.frequency = self->frequency; + msg.addr = addr; + msg.flags = 0; + msg.buffer = NULL; + msg.length = 0; + return I2C_TRANSFER(self->i2c_dev, &msg, 1) < 0 ? false : true; +} + +uint8_t common_hal_busio_i2c_write(busio_i2c_obj_t *self, uint16_t address, const uint8_t *data, size_t len, bool stop) { + struct i2c_msg_s msg; + + msg.frequency = self->frequency; + msg.addr = address; + msg.flags = (stop ? 0 : I2C_M_NOSTOP); + msg.buffer = (uint8_t *) data; + msg.length = len; + return I2C_TRANSFER(self->i2c_dev, &msg, 1); +} + +uint8_t common_hal_busio_i2c_read(busio_i2c_obj_t *self, uint16_t address, uint8_t *data, size_t len) { + struct i2c_msg_s msg; + + msg.frequency = self->frequency; + msg.addr = address; + msg.flags = I2C_M_READ; + msg.buffer = data; + msg.length = len; + return I2C_TRANSFER(self->i2c_dev, &msg, 1); +} + +void common_hal_busio_i2c_never_reset(busio_i2c_obj_t *self) { + never_reset_pin_number(self->scl_pin->number); + never_reset_pin_number(self->sda_pin->number); +} diff --git a/ports/cxd56/common-hal/busio/I2C.h b/ports/cxd56/common-hal/busio/I2C.h new file mode 100644 index 000000000..cdef270bf --- /dev/null +++ b/ports/cxd56/common-hal/busio/I2C.h @@ -0,0 +1,43 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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. + */ + +#ifndef MICROPY_INCLUDED_CXD56_COMMON_HAL_BUSIO_I2C_H +#define MICROPY_INCLUDED_CXD56_COMMON_HAL_BUSIO_I2C_H + +#include "py/obj.h" + +#include "common-hal/microcontroller/Pin.h" + +typedef struct { + mp_obj_base_t base; + struct i2c_master_s* i2c_dev; + uint32_t frequency; + bool has_lock; + const mcu_pin_obj_t *scl_pin; + const mcu_pin_obj_t *sda_pin; +} busio_i2c_obj_t; + +#endif // MICROPY_INCLUDED_CXD56_COMMON_HAL_BUSIO_I2C_H diff --git a/ports/cxd56/common-hal/busio/OneWire.h b/ports/cxd56/common-hal/busio/OneWire.h new file mode 100644 index 000000000..17c1b2237 --- /dev/null +++ b/ports/cxd56/common-hal/busio/OneWire.h @@ -0,0 +1,33 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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. + */ + +#ifndef MICROPY_INCLUDED_CXD56_COMMON_HAL_BUSIO_ONEWIRE_H +#define MICROPY_INCLUDED_CXD56_COMMON_HAL_BUSIO_ONEWIRE_H + +// Use bitbangio. +#include "shared-module/busio/OneWire.h" + +#endif // MICROPY_INCLUDED_CXD56_COMMON_HAL_BUSIO_ONEWIRE_H diff --git a/ports/cxd56/common-hal/busio/SPI.c b/ports/cxd56/common-hal/busio/SPI.c new file mode 100644 index 000000000..9a41011f2 --- /dev/null +++ b/ports/cxd56/common-hal/busio/SPI.c @@ -0,0 +1,155 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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/runtime.h" + +#include "shared-bindings/busio/SPI.h" + +void common_hal_busio_spi_construct(busio_spi_obj_t *self, const mcu_pin_obj_t *clock, + const mcu_pin_obj_t *mosi, const mcu_pin_obj_t *miso) { + int port = -1; + + if (clock->number == PIN_SPI4_SCK && mosi->number == PIN_SPI4_MOSI && miso->number == PIN_SPI4_MISO) { + port = 4; + } else if (clock->number == PIN_EMMC_CLK && mosi->number == PIN_EMMC_DATA0 && miso->number == PIN_EMMC_DATA1) { + port = 5; + } + + if (port < 0) { + mp_raise_ValueError(translate("Invalid pins")); + } + + claim_pin(clock); + claim_pin(mosi); + claim_pin(miso); + + self->clock_pin = clock; + self->mosi_pin = mosi; + self->miso_pin = miso; + self->spi_dev = cxd56_spibus_initialize(port); +} + +void common_hal_busio_spi_deinit(busio_spi_obj_t *self) { + if (common_hal_busio_spi_deinited(self)) { + return; + } + + self->spi_dev = NULL; + + reset_pin_number(self->clock_pin->number); + reset_pin_number(self->mosi_pin->number); + reset_pin_number(self->miso_pin->number); +} + +bool common_hal_busio_spi_deinited(busio_spi_obj_t *self) { + return self->spi_dev == NULL; +} + +bool common_hal_busio_spi_configure(busio_spi_obj_t *self, uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t bits) { + uint8_t mode; + + self->frequency = baudrate; + SPI_SETFREQUENCY(self->spi_dev, baudrate); + + if (polarity == 0) { + if (phase == 0) { + mode = SPIDEV_MODE0; + } else { + mode = SPIDEV_MODE1; + } + } else { + if (phase == 0) { + mode = SPIDEV_MODE2; + } else { + mode = SPIDEV_MODE3; + } + } + + self->polarity = polarity; + self->phase = phase; + SPI_SETMODE(self->spi_dev, mode); + + self->bits = bits; + SPI_SETBITS(self->spi_dev, bits); + + return true; +} + +bool common_hal_busio_spi_try_lock(busio_spi_obj_t *self) { + bool grabbed_lock = false; + if (!self->has_lock) { + grabbed_lock = true; + self->has_lock = true; + } + return grabbed_lock; +} + +bool common_hal_busio_spi_has_lock(busio_spi_obj_t *self) { + return self->has_lock; +} + +void common_hal_busio_spi_unlock(busio_spi_obj_t *self) { + self->has_lock = false; +} + +bool common_hal_busio_spi_write(busio_spi_obj_t *self, const uint8_t *data, size_t len) { + SPI_EXCHANGE(self->spi_dev, data, NULL, len); + + return true; +} + +bool common_hal_busio_spi_read(busio_spi_obj_t *self, uint8_t *data, size_t len, uint8_t write_value) { + SPI_EXCHANGE(self->spi_dev, NULL, data, len); + + return true; +} + +bool common_hal_busio_spi_transfer(busio_spi_obj_t *self, uint8_t *data_out, uint8_t *data_in, size_t len) { + SPI_EXCHANGE(self->spi_dev, data_out, data_in, len); + + return true; +} + +uint32_t common_hal_busio_spi_get_frequency(busio_spi_obj_t *self) { + return self->frequency; +} + +uint8_t common_hal_busio_spi_get_phase(busio_spi_obj_t* self) { + return self->phase; +} + +uint8_t common_hal_busio_spi_get_polarity(busio_spi_obj_t *self) { + return self->polarity; +} + +void common_hal_busio_spi_never_reset(busio_spi_obj_t *self) { + never_reset_pin_number(self->clock_pin->number); + never_reset_pin_number(self->mosi_pin->number); + never_reset_pin_number(self->miso_pin->number); +} diff --git a/ports/cxd56/common-hal/busio/SPI.h b/ports/cxd56/common-hal/busio/SPI.h new file mode 100644 index 000000000..8985a60d9 --- /dev/null +++ b/ports/cxd56/common-hal/busio/SPI.h @@ -0,0 +1,49 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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. + */ + +#ifndef MICROPY_INCLUDED_CXD56_COMMON_HAL_BUSIO_SPI_H +#define MICROPY_INCLUDED_CXD56_COMMON_HAL_BUSIO_SPI_H + +#include + +#include "py/obj.h" + +#include "common-hal/microcontroller/Pin.h" + +typedef struct { + mp_obj_base_t base; + struct spi_dev_s* spi_dev; + uint32_t frequency; + uint8_t phase; + uint8_t polarity; + uint8_t bits; + bool has_lock; + const mcu_pin_obj_t *clock_pin; + const mcu_pin_obj_t *mosi_pin; + const mcu_pin_obj_t *miso_pin; +} busio_spi_obj_t; + +#endif // MICROPY_INCLUDED_CXD56_COMMON_HAL_BUSIO_SPI_H diff --git a/ports/cxd56/common-hal/busio/UART.c b/ports/cxd56/common-hal/busio/UART.c new file mode 100644 index 000000000..2389cbf96 --- /dev/null +++ b/ports/cxd56/common-hal/busio/UART.c @@ -0,0 +1,168 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 +#include + +#include +#include +#include +#include +#include +#include + +#include "py/mperrno.h" +#include "py/stream.h" +#include "py/runtime.h" + +#include "shared-bindings/busio/UART.h" + +void common_hal_busio_uart_construct(busio_uart_obj_t *self, + const mcu_pin_obj_t *tx, const mcu_pin_obj_t *rx, uint32_t baudrate, + uint8_t bits, uart_parity_t parity, uint8_t stop, mp_float_t timeout, + uint16_t receiver_buffer_size) { + struct termios tio; + + self->uart_fd = open("/dev/ttyS2", O_RDWR); + if (self->uart_fd < 0) { + mp_raise_ValueError(translate("Could not initialize UART")); + } + + ioctl(self->uart_fd, TCGETS, (long unsigned int)&tio); + tio.c_speed = baudrate; + ioctl(self->uart_fd, TCSETS, (long unsigned int)&tio); + ioctl(self->uart_fd, TCFLSH, (long unsigned int)NULL); + + if (bits != 8) { + mp_raise_ValueError(translate("Could not initialize UART")); + } + + if (parity != PARITY_NONE) { + mp_raise_ValueError(translate("Could not initialize UART")); + } + + if (stop != 1) { + mp_raise_ValueError(translate("Could not initialize UART")); + } + + if (tx->number != PIN_UART2_TXD || rx->number != PIN_UART2_RXD) { + mp_raise_ValueError(translate("Invalid pins")); + } + + claim_pin(tx); + claim_pin(rx); + + self->tx_pin = tx; + self->rx_pin = rx; + self->baudrate = baudrate; + self->timeout = timeout; +} + +void common_hal_busio_uart_deinit(busio_uart_obj_t *self) { + if (common_hal_busio_uart_deinited(self)) { + return; + } + + close(self->uart_fd); + self->uart_fd = -1; + + reset_pin_number(self->tx_pin->number); + reset_pin_number(self->rx_pin->number); +} + +bool common_hal_busio_uart_deinited(busio_uart_obj_t *self) { + return self->uart_fd < 0; +} + +size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t len, int *errcode) { + fd_set rfds; + struct timeval tv; + int retval, bytes_read; + + // make sure we want at least 1 char + if (len == 0) { + return 0; + } + + FD_ZERO(&rfds); + FD_SET(self->uart_fd, &rfds); + + tv.tv_sec = 0; + tv.tv_usec = self->timeout * 1000; + + retval = select(self->uart_fd + 1, &rfds, NULL, NULL, &tv); + + if (retval) { + bytes_read = read(self->uart_fd, data, len); + } else { + *errcode = EAGAIN; + return MP_STREAM_ERROR; + } + + return bytes_read; +} + +size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data, size_t len, int *errcode) { + int bytes_written = write(self->uart_fd, data, len); + + if (bytes_written < 0) { + *errcode = MP_EAGAIN; + return MP_STREAM_ERROR; + } + + return bytes_written; +} + +uint32_t common_hal_busio_uart_get_baudrate(busio_uart_obj_t *self) { + return self->baudrate; +} + +void common_hal_busio_uart_set_baudrate(busio_uart_obj_t *self, uint32_t baudrate) { + struct termios tio; + + ioctl(self->uart_fd, TCGETS, (long unsigned int)&tio); + tio.c_speed = baudrate; + ioctl(self->uart_fd, TCSETS, (long unsigned int)&tio); + ioctl(self->uart_fd, TCFLSH, (long unsigned int)NULL); +} + +uint32_t common_hal_busio_uart_rx_characters_available(busio_uart_obj_t *self) { + int count = 0; + + ioctl(self->uart_fd, FIONREAD, (long unsigned int)&count); + + return count; +} + +void common_hal_busio_uart_clear_rx_buffer(busio_uart_obj_t *self) { +} + +bool common_hal_busio_uart_ready_to_tx(busio_uart_obj_t *self) { + ioctl(self->uart_fd, TCFLSH, (long unsigned int)NULL); + return true; +} diff --git a/ports/cxd56/common-hal/busio/UART.h b/ports/cxd56/common-hal/busio/UART.h new file mode 100644 index 000000000..1c01cfbb9 --- /dev/null +++ b/ports/cxd56/common-hal/busio/UART.h @@ -0,0 +1,43 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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. + */ + +#ifndef MICROPY_INCLUDED_CXD56_COMMON_HAL_BUSIO_UART_H +#define MICROPY_INCLUDED_CXD56_COMMON_HAL_BUSIO_UART_H + +#include "py/obj.h" + +#include "common-hal/microcontroller/Pin.h" + +typedef struct { + mp_obj_base_t base; + int uart_fd; + const mcu_pin_obj_t *tx_pin; + const mcu_pin_obj_t *rx_pin; + uint32_t baudrate; + uint32_t timeout; +} busio_uart_obj_t; + +#endif // MICROPY_INCLUDED_CXD56_COMMON_HAL_BUSIO_UART_H diff --git a/ports/cxd56/common-hal/busio/__init__.c b/ports/cxd56/common-hal/busio/__init__.c new file mode 100644 index 000000000..41761b674 --- /dev/null +++ b/ports/cxd56/common-hal/busio/__init__.c @@ -0,0 +1 @@ +// No busio module functions. diff --git a/ports/cxd56/common-hal/digitalio/DigitalInOut.c b/ports/cxd56/common-hal/digitalio/DigitalInOut.c new file mode 100644 index 000000000..7b1f6cc03 --- /dev/null +++ b/ports/cxd56/common-hal/digitalio/DigitalInOut.c @@ -0,0 +1,136 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 "py/runtime.h" + +#include "shared-bindings/digitalio/DigitalInOut.h" + +digitalinout_result_t common_hal_digitalio_digitalinout_construct(digitalio_digitalinout_obj_t *self, const mcu_pin_obj_t *pin) { + if (pin->analog) { + mp_raise_ValueError(translate("DigitalInOut not supported on given pin")); + } + + claim_pin(pin); + + self->pin = pin; + self->input = true; + self->open_drain = false; + + board_gpio_write(self->pin->number, -1); + board_gpio_config(self->pin->number, 0, true, true, PIN_FLOAT); + + return DIGITALINOUT_OK; +} + +void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t *self) { + if (common_hal_digitalio_digitalinout_deinited(self)) { + return; + } + + board_gpio_write(self->pin->number, -1); + board_gpio_config(self->pin->number, 0, false, true, PIN_FLOAT); + + reset_pin_number(self->pin->number); + self->pin = mp_const_none; +} + +bool common_hal_digitalio_digitalinout_deinited(digitalio_digitalinout_obj_t *self) { + return self->pin == mp_const_none; +} + +void common_hal_digitalio_digitalinout_switch_to_input(digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) { + self->input = true; + self->pull = pull; + board_gpio_write(self->pin->number, -1); + board_gpio_config(self->pin->number, 0, true, true, pull); +} + +void common_hal_digitalio_digitalinout_switch_to_output(digitalio_digitalinout_obj_t *self, bool value, digitalio_drive_mode_t drive_mode) { + self->input = false; + self->open_drain = drive_mode == DRIVE_MODE_OPEN_DRAIN; + board_gpio_write(self->pin->number, -1); + board_gpio_config(self->pin->number, 0, false, true, PIN_FLOAT); + + if (self->open_drain) { + board_gpio_write(self->pin->number, 0); + } + common_hal_digitalio_digitalinout_set_value(self, value); +} + +digitalio_direction_t common_hal_digitalio_digitalinout_get_direction(digitalio_digitalinout_obj_t *self) { + return self->input ? DIRECTION_INPUT : DIRECTION_OUTPUT; +} + +void common_hal_digitalio_digitalinout_set_value(digitalio_digitalinout_obj_t *self, bool value) { + if (self->open_drain) { + if (value) { + board_gpio_write(self->pin->number, -1); + board_gpio_config(self->pin->number, 0, true, true, PIN_PULLUP); + } else { + board_gpio_config(self->pin->number, 0, false, true, PIN_FLOAT); + board_gpio_write(self->pin->number, 0); + } + } else { + board_gpio_write(self->pin->number, value); + } +} + +bool common_hal_digitalio_digitalinout_get_value(digitalio_digitalinout_obj_t *self) { + return board_gpio_read(self->pin->number); +} + +void common_hal_digitalio_digitalinout_set_drive_mode(digitalio_digitalinout_obj_t *self, digitalio_drive_mode_t drive_mode) { + if (drive_mode == DRIVE_MODE_PUSH_PULL) { + board_gpio_write(self->pin->number, -1); + board_gpio_config(self->pin->number, 0, false, true, PIN_FLOAT); + self->open_drain = false; + } else { + board_gpio_write(self->pin->number, -1); + board_gpio_config(self->pin->number, 0, false, true, PIN_FLOAT); + board_gpio_write(self->pin->number, 0); + self->open_drain = true; + } +} + +digitalio_drive_mode_t common_hal_digitalio_digitalinout_get_drive_mode(digitalio_digitalinout_obj_t *self) { + return self->open_drain ? DRIVE_MODE_OPEN_DRAIN : DRIVE_MODE_PUSH_PULL; +} + +void common_hal_digitalio_digitalinout_set_pull(digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) { + self->pull = pull; + board_gpio_write(self->pin->number, -1); + board_gpio_config(self->pin->number, 0, true, true, pull); +} + +digitalio_pull_t common_hal_digitalio_digitalinout_get_pull(digitalio_digitalinout_obj_t *self) { + return self->pull; +} + +void common_hal_digitalio_digitalinout_never_reset(digitalio_digitalinout_obj_t *self) { + never_reset_pin_number(self->pin->number); +} diff --git a/ports/cxd56/common-hal/digitalio/DigitalInOut.h b/ports/cxd56/common-hal/digitalio/DigitalInOut.h new file mode 100644 index 000000000..58a11817c --- /dev/null +++ b/ports/cxd56/common-hal/digitalio/DigitalInOut.h @@ -0,0 +1,42 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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. + */ + +#ifndef MICROPY_INCLUDED_CXD56_COMMON_HAL_DIGITALIO_DIGITALINOUT_H +#define MICROPY_INCLUDED_CXD56_COMMON_HAL_DIGITALIO_DIGITALINOUT_H + +#include "py/obj.h" + +#include "common-hal/microcontroller/Pin.h" + +typedef struct { + mp_obj_base_t base; + const mcu_pin_obj_t *pin; + bool input; + bool open_drain; + uint8_t pull; +} digitalio_digitalinout_obj_t; + +#endif // MICROPY_INCLUDED_CXD56_COMMON_HAL_DIGITALIO_DIGITALINOUT_H diff --git a/ports/cxd56/common-hal/digitalio/__init__.c b/ports/cxd56/common-hal/digitalio/__init__.c new file mode 100644 index 000000000..20fad4595 --- /dev/null +++ b/ports/cxd56/common-hal/digitalio/__init__.c @@ -0,0 +1 @@ +// No digitalio module functions. diff --git a/ports/cxd56/common-hal/microcontroller/Pin.c b/ports/cxd56/common-hal/microcontroller/Pin.c new file mode 100644 index 000000000..005defa84 --- /dev/null +++ b/ports/cxd56/common-hal/microcontroller/Pin.c @@ -0,0 +1,162 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 +#include +#include + +#include "shared-bindings/microcontroller/Pin.h" + +typedef struct { + const mcu_pin_obj_t *pin; + bool reset; + bool free; +} pin_status_t; + +const mcu_pin_obj_t pin_D0 = PIN(PIN_UART2_RXD, false); +const mcu_pin_obj_t pin_D1 = PIN(PIN_UART2_TXD, false); +const mcu_pin_obj_t pin_D2 = PIN(PIN_HIF_IRQ_OUT, false); +const mcu_pin_obj_t pin_D3 = PIN(PIN_PWM3, false); +const mcu_pin_obj_t pin_D4 = PIN(PIN_SPI2_MOSI, false); +const mcu_pin_obj_t pin_D5 = PIN(PIN_PWM1, false); +const mcu_pin_obj_t pin_D6 = PIN(PIN_PWM0, false); +const mcu_pin_obj_t pin_D7 = PIN(PIN_SPI3_CS1_X, false); +const mcu_pin_obj_t pin_D8 = PIN(PIN_SPI2_MISO, false); +const mcu_pin_obj_t pin_D9 = PIN(PIN_PWM2, false); +const mcu_pin_obj_t pin_D10 = PIN(PIN_SPI4_CS_X, false); +const mcu_pin_obj_t pin_D11 = PIN(PIN_SPI4_MOSI, false); +const mcu_pin_obj_t pin_D12 = PIN(PIN_SPI4_MISO, false); +const mcu_pin_obj_t pin_D13 = PIN(PIN_SPI4_SCK, false); +const mcu_pin_obj_t pin_D14 = PIN(PIN_I2C0_BDT, false); +const mcu_pin_obj_t pin_D15 = PIN(PIN_I2C0_BCK, false); +const mcu_pin_obj_t pin_D16 = PIN(PIN_EMMC_DATA0, false); +const mcu_pin_obj_t pin_D17 = PIN(PIN_EMMC_DATA1, false); +const mcu_pin_obj_t pin_D18 = PIN(PIN_I2S0_DATA_OUT, false); +const mcu_pin_obj_t pin_D19 = PIN(PIN_I2S0_DATA_IN, false); +const mcu_pin_obj_t pin_D20 = PIN(PIN_EMMC_DATA2, false); +const mcu_pin_obj_t pin_D21 = PIN(PIN_EMMC_DATA3, false); +const mcu_pin_obj_t pin_D22 = PIN(PIN_SEN_IRQ_IN, false); +const mcu_pin_obj_t pin_D23 = PIN(PIN_EMMC_CLK, false); +const mcu_pin_obj_t pin_D24 = PIN(PIN_EMMC_CMD, false); +const mcu_pin_obj_t pin_D25 = PIN(PIN_I2S0_LRCK, false); +const mcu_pin_obj_t pin_D26 = PIN(PIN_I2S0_BCK, false); +const mcu_pin_obj_t pin_D27 = PIN(PIN_UART2_CTS, false); +const mcu_pin_obj_t pin_D28 = PIN(PIN_UART2_RTS, false); +const mcu_pin_obj_t pin_LED0 = PIN(PIN_I2S1_BCK, false); +const mcu_pin_obj_t pin_LED1 = PIN(PIN_I2S1_LRCK, false); +const mcu_pin_obj_t pin_LED2 = PIN(PIN_I2S1_DATA_IN, false); +const mcu_pin_obj_t pin_LED3 = PIN(PIN_I2S1_DATA_OUT, false); +const mcu_pin_obj_t pin_A0 = PIN(0, true); +const mcu_pin_obj_t pin_A1 = PIN(1, true); +const mcu_pin_obj_t pin_A2 = PIN(2, true); +const mcu_pin_obj_t pin_A3 = PIN(3, true); +const mcu_pin_obj_t pin_A4 = PIN(4, true); +const mcu_pin_obj_t pin_A5 = PIN(5, true); + +STATIC pin_status_t pins[] = { + { &pin_D0, true, true }, + { &pin_D1, true, true }, + { &pin_D2, true, true }, + { &pin_D3, true, true }, + { &pin_D4, true, true }, + { &pin_D5, true, true }, + { &pin_D6, true, true }, + { &pin_D7, true, true }, + { &pin_D8, true, true }, + { &pin_D9, true, true }, + { &pin_D10, true, true }, + { &pin_D11, true, true }, + { &pin_D12, true, true }, + { &pin_D13, true, true }, + { &pin_D14, true, true }, + { &pin_D15, true, true }, + { &pin_D16, true, true }, + { &pin_D17, true, true }, + { &pin_D18, true, true }, + { &pin_D19, true, true }, + { &pin_D20, true, true }, + { &pin_D21, true, true }, + { &pin_D22, true, true }, + { &pin_D23, true, true }, + { &pin_D24, true, true }, + { &pin_D25, true, true }, + { &pin_D26, true, true }, + { &pin_D27, true, true }, + { &pin_D28, true, true }, + { &pin_LED0, true, true }, + { &pin_LED1, true, true }, + { &pin_LED2, true, true }, + { &pin_LED3, true, true }, +}; + +bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t *pin) { + for (int i = 0; i < MP_ARRAY_SIZE(pins); i++) { + if (pins[i].pin->number == pin->number) { + return pins[i].free; + } + } + + return true; +} + +void never_reset_pin_number(uint8_t pin_number) { + for (int i = 0; i < MP_ARRAY_SIZE(pins); i++) { + if (pins[i].pin->number == pin_number) { + pins[i].reset = false; + } + } +} + +void reset_pin_number(uint8_t pin_number) { + for (int i = 0; i < MP_ARRAY_SIZE(pins); i++) { + if (pins[i].pin->number == pin_number) { + pins[i].free = true; + } + } +} + +void reset_all_pins(void) { + for (int i = 0; i < MP_ARRAY_SIZE(pins); i++) { + if (!pins[i].free && pins[i].reset) { + board_gpio_write(pins[i].pin->number, -1); + board_gpio_config(pins[i].pin->number, 0, false, true, PIN_FLOAT); + board_gpio_int(pins[i].pin->number, false); + board_gpio_intconfig(pins[i].pin->number, 0, false, NULL); + pins[i].free = true; + } + } +} + +void claim_pin(const mcu_pin_obj_t *pin) { + for (int i = 0; i < MP_ARRAY_SIZE(pins); i++) { + if (pins[i].pin->number == pin->number) { + pins[i].free = false; + } + } +} diff --git a/ports/cxd56/common-hal/microcontroller/Pin.h b/ports/cxd56/common-hal/microcontroller/Pin.h new file mode 100644 index 000000000..113387cef --- /dev/null +++ b/ports/cxd56/common-hal/microcontroller/Pin.h @@ -0,0 +1,92 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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. + */ + +#ifndef MICROPY_INCLUDED_CXD56_COMMON_HAL_MICROCONTROLLER_PIN_H +#define MICROPY_INCLUDED_CXD56_COMMON_HAL_MICROCONTROLLER_PIN_H + +#include "py/obj.h" + +extern const mp_obj_type_t mcu_pin_type; + +#define PIN(pin, a) \ +{ \ + { &mcu_pin_type }, \ + .number = (pin), \ + .analog = (a) \ +} + +typedef struct { + mp_obj_base_t base; + uint8_t number; + bool analog; +} mcu_pin_obj_t; + +extern const mcu_pin_obj_t pin_D0; +extern const mcu_pin_obj_t pin_D1; +extern const mcu_pin_obj_t pin_D2; +extern const mcu_pin_obj_t pin_D3; +extern const mcu_pin_obj_t pin_D4; +extern const mcu_pin_obj_t pin_D5; +extern const mcu_pin_obj_t pin_D6; +extern const mcu_pin_obj_t pin_D7; +extern const mcu_pin_obj_t pin_D8; +extern const mcu_pin_obj_t pin_D9; +extern const mcu_pin_obj_t pin_D10; +extern const mcu_pin_obj_t pin_D11; +extern const mcu_pin_obj_t pin_D12; +extern const mcu_pin_obj_t pin_D13; +extern const mcu_pin_obj_t pin_D14; +extern const mcu_pin_obj_t pin_D15; +extern const mcu_pin_obj_t pin_D16; +extern const mcu_pin_obj_t pin_D17; +extern const mcu_pin_obj_t pin_D18; +extern const mcu_pin_obj_t pin_D19; +extern const mcu_pin_obj_t pin_D20; +extern const mcu_pin_obj_t pin_D21; +extern const mcu_pin_obj_t pin_D22; +extern const mcu_pin_obj_t pin_D23; +extern const mcu_pin_obj_t pin_D24; +extern const mcu_pin_obj_t pin_D25; +extern const mcu_pin_obj_t pin_D26; +extern const mcu_pin_obj_t pin_D27; +extern const mcu_pin_obj_t pin_D28; +extern const mcu_pin_obj_t pin_LED0; +extern const mcu_pin_obj_t pin_LED1; +extern const mcu_pin_obj_t pin_LED2; +extern const mcu_pin_obj_t pin_LED3; +extern const mcu_pin_obj_t pin_A0; +extern const mcu_pin_obj_t pin_A1; +extern const mcu_pin_obj_t pin_A2; +extern const mcu_pin_obj_t pin_A3; +extern const mcu_pin_obj_t pin_A4; +extern const mcu_pin_obj_t pin_A5; + +void never_reset_pin_number(uint8_t pin_number); +void reset_pin_number(uint8_t pin_number); +void reset_all_pins(void); +void claim_pin(const mcu_pin_obj_t* pin); + +#endif // MICROPY_INCLUDED_CXD56_COMMON_HAL_MICROCONTROLLER_PIN_H diff --git a/ports/cxd56/common-hal/microcontroller/Processor.c b/ports/cxd56/common-hal/microcontroller/Processor.c new file mode 100644 index 000000000..355ee01df --- /dev/null +++ b/ports/cxd56/common-hal/microcontroller/Processor.c @@ -0,0 +1,41 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 "py/mphal.h" + +uint32_t common_hal_mcu_processor_get_frequency(void) { + return mp_hal_ticks_cpu(); +} + +float common_hal_mcu_processor_get_temperature(void) { + return 0; +} + +void common_hal_mcu_processor_get_uid(uint8_t raw_id[]) { + boardctl(BOARDIOC_UNIQUEID, (uintptr_t) raw_id); +} diff --git a/ports/cxd56/common-hal/microcontroller/Processor.h b/ports/cxd56/common-hal/microcontroller/Processor.h new file mode 100644 index 000000000..12555e82c --- /dev/null +++ b/ports/cxd56/common-hal/microcontroller/Processor.h @@ -0,0 +1,40 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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. + */ + +#ifndef MICROPY_INCLUDED_CXD56_COMMON_HAL_MICROCONTROLLER_PROCESSOR_H +#define MICROPY_INCLUDED_CXD56_COMMON_HAL_MICROCONTROLLER_PROCESSOR_H + +#define COMMON_HAL_MCU_PROCESSOR_UID_LENGTH CONFIG_BOARDCTL_UNIQUEID_SIZE + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; +} mcu_processor_obj_t; + +const mp_obj_type_t mcu_processor_type; + +#endif // MICROPY_INCLUDED_CXD56_COMMON_HAL_MICROCONTROLLER_PROCESSOR_H diff --git a/ports/cxd56/common-hal/microcontroller/__init__.c b/ports/cxd56/common-hal/microcontroller/__init__.c new file mode 100644 index 000000000..576896eb2 --- /dev/null +++ b/ports/cxd56/common-hal/microcontroller/__init__.c @@ -0,0 +1,111 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 "py/mphal.h" +#include "py/runtime.h" + +#include "shared-bindings/microcontroller/__init__.h" +#include "common-hal/microcontroller/Pin.h" +#include "supervisor/filesystem.h" +#include "supervisor/shared/safe_mode.h" + +// The singleton microcontroller.Processor object, bound to microcontroller.cpu +// It currently only has properties, and no state. +const mcu_processor_obj_t common_hal_mcu_processor_obj = { + .base = { + .type = &mcu_processor_type, + }, +}; + +void common_hal_mcu_delay_us(uint32_t delay) { + mp_hal_delay_us(delay); +} + +void common_hal_mcu_disable_interrupts(void) { + __asm volatile ("cpsid i" : : : "memory"); +} + +void common_hal_mcu_enable_interrupts(void) { + __asm volatile ("cpsie i" : : : "memory"); +} + +void common_hal_mcu_on_next_reset(mcu_runmode_t runmode) { + if(runmode == RUNMODE_BOOTLOADER) { + mp_raise_ValueError(translate("Cannot reset into bootloader because no bootloader is present.")); + } else if(runmode == RUNMODE_SAFE_MODE) { + safe_mode_on_next_reset(PROGRAMMATIC_SAFE_MODE); + } +} + +void common_hal_mcu_reset(void) { + filesystem_flush(); + boardctl(BOARDIOC_RESET, 0); +} + +STATIC const mp_rom_map_elem_t mcu_pin_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR_D0), MP_ROM_PTR(&pin_D0) }, + { MP_ROM_QSTR(MP_QSTR_D1), MP_ROM_PTR(&pin_D1) }, + { MP_ROM_QSTR(MP_QSTR_D2), MP_ROM_PTR(&pin_D2) }, + { MP_ROM_QSTR(MP_QSTR_D3), MP_ROM_PTR(&pin_D3) }, + { MP_ROM_QSTR(MP_QSTR_D4), MP_ROM_PTR(&pin_D4) }, + { MP_ROM_QSTR(MP_QSTR_D5), MP_ROM_PTR(&pin_D5) }, + { MP_ROM_QSTR(MP_QSTR_D6), MP_ROM_PTR(&pin_D6) }, + { MP_ROM_QSTR(MP_QSTR_D7), MP_ROM_PTR(&pin_D7) }, + { MP_ROM_QSTR(MP_QSTR_D8), MP_ROM_PTR(&pin_D8) }, + { MP_ROM_QSTR(MP_QSTR_D9), MP_ROM_PTR(&pin_D9) }, + { MP_ROM_QSTR(MP_QSTR_D10), MP_ROM_PTR(&pin_D10) }, + { MP_ROM_QSTR(MP_QSTR_D11), MP_ROM_PTR(&pin_D11) }, + { MP_ROM_QSTR(MP_QSTR_D12), MP_ROM_PTR(&pin_D12) }, + { MP_ROM_QSTR(MP_QSTR_D13), MP_ROM_PTR(&pin_D13) }, + { MP_ROM_QSTR(MP_QSTR_D14), MP_ROM_PTR(&pin_D14) }, + { MP_ROM_QSTR(MP_QSTR_D15), MP_ROM_PTR(&pin_D15) }, + { MP_ROM_QSTR(MP_QSTR_D16), MP_ROM_PTR(&pin_D16) }, + { MP_ROM_QSTR(MP_QSTR_D17), MP_ROM_PTR(&pin_D17) }, + { MP_ROM_QSTR(MP_QSTR_D18), MP_ROM_PTR(&pin_D18) }, + { MP_ROM_QSTR(MP_QSTR_D19), MP_ROM_PTR(&pin_D19) }, + { MP_ROM_QSTR(MP_QSTR_D20), MP_ROM_PTR(&pin_D20) }, + { MP_ROM_QSTR(MP_QSTR_D21), MP_ROM_PTR(&pin_D21) }, + { MP_ROM_QSTR(MP_QSTR_D22), MP_ROM_PTR(&pin_D22) }, + { MP_ROM_QSTR(MP_QSTR_D23), MP_ROM_PTR(&pin_D23) }, + { MP_ROM_QSTR(MP_QSTR_D24), MP_ROM_PTR(&pin_D24) }, + { MP_ROM_QSTR(MP_QSTR_D25), MP_ROM_PTR(&pin_D25) }, + { MP_ROM_QSTR(MP_QSTR_D26), MP_ROM_PTR(&pin_D26) }, + { MP_ROM_QSTR(MP_QSTR_D27), MP_ROM_PTR(&pin_D27) }, + { MP_ROM_QSTR(MP_QSTR_D28), MP_ROM_PTR(&pin_D28) }, + { MP_ROM_QSTR(MP_QSTR_LED0), MP_ROM_PTR(&pin_LED0) }, + { MP_ROM_QSTR(MP_QSTR_LED1), MP_ROM_PTR(&pin_LED1) }, + { MP_ROM_QSTR(MP_QSTR_LED2), MP_ROM_PTR(&pin_LED2) }, + { MP_ROM_QSTR(MP_QSTR_LED3), MP_ROM_PTR(&pin_LED3) }, + { MP_ROM_QSTR(MP_QSTR_A0), MP_ROM_PTR(&pin_A0) }, + { MP_ROM_QSTR(MP_QSTR_A1), MP_ROM_PTR(&pin_A1) }, + { MP_ROM_QSTR(MP_QSTR_A2), MP_ROM_PTR(&pin_A2) }, + { MP_ROM_QSTR(MP_QSTR_A3), MP_ROM_PTR(&pin_A3) }, + { MP_ROM_QSTR(MP_QSTR_A4), MP_ROM_PTR(&pin_A4) }, + { MP_ROM_QSTR(MP_QSTR_A5), MP_ROM_PTR(&pin_A5) }, +}; +MP_DEFINE_CONST_DICT(mcu_pin_globals, mcu_pin_globals_table); diff --git a/ports/cxd56/common-hal/os/__init__.c b/ports/cxd56/common-hal/os/__init__.c new file mode 100644 index 000000000..d4b0e23be --- /dev/null +++ b/ports/cxd56/common-hal/os/__init__.c @@ -0,0 +1,72 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 "genhdr/mpversion.h" +#include "py/objstr.h" +#include "py/objtuple.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, "spresense"); +STATIC const MP_DEFINE_STR_OBJ(os_uname_info_nodename_obj, "spresense"); +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); +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 +); + +mp_obj_t common_hal_os_uname(void) { + return (mp_obj_t)&os_uname_info_obj; +} + +bool common_hal_os_urandom(uint8_t* buffer, mp_uint_t length) { + uint32_t i = 0; + + while (i < length) { + uint32_t new_random = rand(); + for (int j = 0; j < 4 && i < length; j++) { + buffer[i] = new_random & 0xff; + i++; + new_random >>= 8; + } + } + + return true; +} diff --git a/ports/cxd56/common-hal/pulseio/PWMOut.c b/ports/cxd56/common-hal/pulseio/PWMOut.c new file mode 100644 index 000000000..fae49aed3 --- /dev/null +++ b/ports/cxd56/common-hal/pulseio/PWMOut.c @@ -0,0 +1,157 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 + +#include "py/runtime.h" + +#include "shared-bindings/pulseio/PWMOut.h" + +typedef struct { + const char* devpath; + const mcu_pin_obj_t *pin; + int fd; + bool reset; +} pwmout_dev_t; + +STATIC pwmout_dev_t pwmout_dev[] = { + {"/dev/pwm0", &pin_D6, -1, true}, + {"/dev/pwm1", &pin_D5, -1, true}, + {"/dev/pwm2", &pin_D9, -1, true}, + {"/dev/pwm3", &pin_D3, -1, true} +}; + +pwmout_result_t common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t *self, + const mcu_pin_obj_t *pin, uint16_t duty, uint32_t frequency, + bool variable_frequency) { + self->number = -1; + + for (int i = 0; i < MP_ARRAY_SIZE(pwmout_dev); i++) { + if (pin->number == pwmout_dev[i].pin->number) { + self->number = i; + break; + } + } + + if (self->number < 0) { + return PWMOUT_INVALID_PIN; + } + + pwmout_dev[self->number].fd = open(pwmout_dev[self->number].devpath, O_RDONLY); + if (pwmout_dev[self->number].fd < 0) { + return PWMOUT_INVALID_PIN; + } + + self->info.frequency = frequency; + self->info.duty = duty; + self->variable_frequency = variable_frequency; + + if (ioctl(pwmout_dev[self->number].fd, PWMIOC_SETCHARACTERISTICS, (unsigned long)((uintptr_t)&self->info)) < 0) { + mp_raise_ValueError(translate("Invalid PWM frequency")); + } + ioctl(pwmout_dev[self->number].fd, PWMIOC_START, 0); + + claim_pin(pin); + + self->pin = pin; + + return PWMOUT_OK; +} + +void common_hal_pulseio_pwmout_deinit(pulseio_pwmout_obj_t *self) { + if (common_hal_pulseio_pwmout_deinited(self)) { + return; + } + + ioctl(pwmout_dev[self->number].fd, PWMIOC_STOP, 0); + close(pwmout_dev[self->number].fd); + pwmout_dev[self->number].fd = -1; + + reset_pin_number(self->pin->number); + self->pin = mp_const_none; +} + +bool common_hal_pulseio_pwmout_deinited(pulseio_pwmout_obj_t *self) { + return self->pin == mp_const_none; +} + +void common_hal_pulseio_pwmout_set_duty_cycle(pulseio_pwmout_obj_t *self, uint16_t duty) { + self->info.duty = duty; + + ioctl(pwmout_dev[self->number].fd, PWMIOC_SETCHARACTERISTICS, (unsigned long)((uintptr_t)&self->info)); +} + +uint16_t common_hal_pulseio_pwmout_get_duty_cycle(pulseio_pwmout_obj_t *self) { + return self->info.duty; +} + +void common_hal_pulseio_pwmout_set_frequency(pulseio_pwmout_obj_t *self, uint32_t frequency) { + self->info.frequency = frequency; + + if (ioctl(pwmout_dev[self->number].fd, PWMIOC_SETCHARACTERISTICS, (unsigned long)((uintptr_t)&self->info)) < 0) { + mp_raise_ValueError(translate("Invalid PWM frequency")); + } +} + +uint32_t common_hal_pulseio_pwmout_get_frequency(pulseio_pwmout_obj_t *self) { + return self->info.frequency; +} + +bool common_hal_pulseio_pwmout_get_variable_frequency(pulseio_pwmout_obj_t *self) { + return self->variable_frequency; +} + +void common_hal_pulseio_pwmout_never_reset(pulseio_pwmout_obj_t *self) { + never_reset_pin_number(self->pin->number); + + pwmout_dev[self->number].reset = false; +} + +void common_hal_pulseio_pwmout_reset_ok(pulseio_pwmout_obj_t *self) { + pwmout_dev[self->number].reset = true; +} + +void pwmout_reset(void) { + for (int i = 0; i < MP_ARRAY_SIZE(pwmout_dev); i++) { + if (pwmout_dev[i].fd >= 0 && pwmout_dev[i].reset) { + ioctl(pwmout_dev[i].fd, PWMIOC_STOP, 0); + close(pwmout_dev[i].fd); + pwmout_dev[i].fd = -1; + + reset_pin_number(pwmout_dev[i].pin->number); + } + } +} + +void pwmout_start(uint8_t pwm_num) { + ioctl(pwmout_dev[pwm_num].fd, PWMIOC_START, 0); +} + +void pwmout_stop(uint8_t pwm_num) { + ioctl(pwmout_dev[pwm_num].fd, PWMIOC_STOP, 0); +} diff --git a/ports/cxd56/common-hal/pulseio/PWMOut.h b/ports/cxd56/common-hal/pulseio/PWMOut.h new file mode 100644 index 000000000..c16084fe6 --- /dev/null +++ b/ports/cxd56/common-hal/pulseio/PWMOut.h @@ -0,0 +1,48 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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. + */ + +#ifndef MICROPY_INCLUDED_CXD56_COMMON_HAL_PULSEIO_PWMOUT_H +#define MICROPY_INCLUDED_CXD56_COMMON_HAL_PULSEIO_PWMOUT_H + +#include + +#include "common-hal/microcontroller/Pin.h" + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + const mcu_pin_obj_t *pin; + struct pwm_info_s info; + bool variable_frequency; + uint8_t number; +} pulseio_pwmout_obj_t; + +void pwmout_reset(void); +void pwmout_start(uint8_t pwm_num); +void pwmout_stop(uint8_t pwm_num); + +#endif // MICROPY_INCLUDED_CXD56_COMMON_HAL_PULSEIO_PWMOUT_H diff --git a/ports/cxd56/common-hal/pulseio/PulseIn.c b/ports/cxd56/common-hal/pulseio/PulseIn.c new file mode 100644 index 000000000..dd2773d1d --- /dev/null +++ b/ports/cxd56/common-hal/pulseio/PulseIn.c @@ -0,0 +1,196 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 "py/runtime.h" +#include "py/mphal.h" + +#include "shared-bindings/pulseio/PulseIn.h" +#include "shared-bindings/microcontroller/__init__.h" + +static pulseio_pulsein_obj_t *pulsein_objects[12]; + +static int pulsein_interrupt_handler(int irq, FAR void *context, FAR void *arg); + +static int pulsein_set_config(pulseio_pulsein_obj_t *self, bool first_edge) { + int mode; + + if (!first_edge) { + mode = INT_BOTH_EDGE; + } else if (self->idle_state) { + mode = INT_FALLING_EDGE; + } else { + mode = INT_RISING_EDGE; + } + return board_gpio_intconfig(self->pin->number, mode, false, pulsein_interrupt_handler); +} + +static int pulsein_interrupt_handler(int irq, FAR void *context, FAR void *arg) { + // Grab the current time first. + uint32_t current_us = mp_hal_ticks_us(); + + pulseio_pulsein_obj_t *self = pulsein_objects[irq - CXD56_IRQ_EXDEVICE_0]; + + if (self->first_edge) { + self->first_edge = false; + pulsein_set_config(self, false); + board_gpio_int(self->pin->number, true); + } else { + uint32_t us_diff = current_us - self->last_us; + + uint16_t duration = 0xffff; + if (us_diff < duration) { + duration = us_diff; + } + + uint16_t i = (self->start + self->len) % self->maxlen; + self->buffer[i] = duration; + if (self->len < self->maxlen) { + self->len++; + } else { + self->start++; + } + } + self->last_us = current_us; + + return 0; +} + +void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self, + const mcu_pin_obj_t *pin, uint16_t maxlen, bool idle_state) { + self->buffer = (uint16_t *) m_malloc(maxlen * sizeof(uint16_t), false); + if (self->buffer == NULL) { + mp_raise_msg_varg(&mp_type_MemoryError, translate("Failed to allocate RX buffer of %d bytes"), maxlen * sizeof(uint16_t)); + } + + self->pin = pin; + self->maxlen = maxlen; + self->idle_state = idle_state; + self->start = 0; + self->len = 0; + self->first_edge = true; + self->paused = false; + + int irq = pulsein_set_config(self, true); + if (irq < 0) { + mp_raise_RuntimeError(translate("EXTINT channel already in use")); + } else { + pulsein_objects[irq - CXD56_IRQ_EXDEVICE_0] = self; + } + + claim_pin(pin); + + board_gpio_int(self->pin->number, true); +} + +void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t *self) { + if (common_hal_pulseio_pulsein_deinited(self)) { + return; + } + + board_gpio_int(self->pin->number, false); + board_gpio_intconfig(self->pin->number, 0, false, NULL); + + reset_pin_number(self->pin->number); + self->pin = mp_const_none; +} + +bool common_hal_pulseio_pulsein_deinited(pulseio_pulsein_obj_t *self) { + return self->pin == mp_const_none; +} + +void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t *self) { + board_gpio_int(self->pin->number, false); + self->paused = true; +} + +void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t *self, uint16_t trigger_duration) { + // Make sure we're paused. + common_hal_pulseio_pulsein_pause(self); + + // Send the trigger pulse. + if (trigger_duration > 0) { + board_gpio_config(self->pin->number, 0, false, true, PIN_FLOAT); + board_gpio_write(self->pin->number, !self->idle_state); + common_hal_mcu_delay_us((uint32_t)trigger_duration); + board_gpio_write(self->pin->number, self->idle_state); + } + + // Reconfigure the pin and make sure its set to detect the first edge. + self->first_edge = true; + self->paused = false; + + pulsein_set_config(self, true); + board_gpio_int(self->pin->number, true); +} + +void common_hal_pulseio_pulsein_clear(pulseio_pulsein_obj_t *self) { + common_hal_mcu_disable_interrupts(); + self->start = 0; + self->len = 0; + common_hal_mcu_enable_interrupts(); +} + +uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t *self) { + if (self->len == 0) { + mp_raise_IndexError(translate("pop from an empty PulseIn")); + } + common_hal_mcu_disable_interrupts(); + uint16_t value = self->buffer[self->start]; + self->start = (self->start + 1) % self->maxlen; + self->len--; + common_hal_mcu_enable_interrupts(); + + return value; +} + +uint16_t common_hal_pulseio_pulsein_get_maxlen(pulseio_pulsein_obj_t *self) { + return self->maxlen; +} + +bool common_hal_pulseio_pulsein_get_paused(pulseio_pulsein_obj_t *self) { + return self->paused; +} + +uint16_t common_hal_pulseio_pulsein_get_len(pulseio_pulsein_obj_t *self) { + return self->len; +} + +uint16_t common_hal_pulseio_pulsein_get_item(pulseio_pulsein_obj_t *self, int16_t index) { + common_hal_mcu_disable_interrupts(); + if (index < 0) { + index += self->len; + } + if (index < 0 || index >= self->len) { + common_hal_mcu_enable_interrupts(); + mp_raise_IndexError(translate("index out of range")); + } + uint16_t value = self->buffer[(self->start + index) % self->maxlen]; + common_hal_mcu_enable_interrupts(); + return value; +} + \ No newline at end of file diff --git a/ports/cxd56/common-hal/pulseio/PulseIn.h b/ports/cxd56/common-hal/pulseio/PulseIn.h new file mode 100644 index 000000000..ff31712ab --- /dev/null +++ b/ports/cxd56/common-hal/pulseio/PulseIn.h @@ -0,0 +1,47 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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. + */ + +#ifndef MICROPY_INCLUDED_CXD56_COMMON_HAL_PULSEIO_PULSEIN_H +#define MICROPY_INCLUDED_CXD56_COMMON_HAL_PULSEIO_PULSEIN_H + +#include "common-hal/microcontroller/Pin.h" + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + const mcu_pin_obj_t *pin; + uint16_t *buffer; + uint16_t maxlen; + uint16_t start; + uint16_t len; + uint32_t last_us; + bool idle_state; + bool first_edge; + bool paused; +} pulseio_pulsein_obj_t; + +#endif // MICROPY_INCLUDED_CXD56_COMMON_HAL_PULSEIO_PULSEIN_H diff --git a/ports/cxd56/common-hal/pulseio/PulseOut.c b/ports/cxd56/common-hal/pulseio/PulseOut.c new file mode 100644 index 000000000..5e1d5a2ed --- /dev/null +++ b/ports/cxd56/common-hal/pulseio/PulseOut.c @@ -0,0 +1,126 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 +#include + +#include "py/runtime.h" + +#include "shared-bindings/pulseio/PulseOut.h" + +static uint16_t *pulse_buffer = NULL; +static uint16_t pulse_index = 0; +static uint16_t pulse_length; +static int pulse_fd = -1; + +static bool pulseout_timer_handler(unsigned int *next_interval_us, void *arg) +{ + uint8_t pwm_num = (uint8_t)(int)arg; + pulse_index++; + + if (pulse_index >= pulse_length) { + return false; + } + + *next_interval_us = pulse_buffer[pulse_index] * 1000; + + if (pulse_index % 2 == 0) { + pwmout_start(pwm_num); + } else { + pwmout_stop(pwm_num); + } + + return true; +} + +void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t *self, + const pulseio_pwmout_obj_t *carrier) { + if (pulse_fd < 0) { + pulse_fd = open("/dev/timer0", O_RDONLY); + } + + if (pulse_fd < 0) { + mp_raise_RuntimeError(translate("All timers in use")); + } + + self->pwm_num = carrier->number; +} + +void common_hal_pulseio_pulseout_deinit(pulseio_pulseout_obj_t *self) { + if (common_hal_pulseio_pulseout_deinited(self)) { + return; + } + + ioctl(pulse_fd, TCIOC_STOP, 0); + close(pulse_fd); + pulse_fd = -1; + + pulse_buffer = NULL; +} + +bool common_hal_pulseio_pulseout_deinited(pulseio_pulseout_obj_t *self) { + return pulse_fd < 0; +} + +void common_hal_pulseio_pulseout_send(pulseio_pulseout_obj_t *self, uint16_t *pulses, uint16_t len) { + if (pulse_buffer != NULL) { + mp_raise_RuntimeError(translate("Another send is already active")); + } + + struct timer_sethandler_s sethandler; + + pulse_buffer = pulses; + pulse_index = 0; + pulse_length = len; + + unsigned long timeout = pulse_buffer[0] * 1000; + + ioctl(pulse_fd, TCIOC_SETTIMEOUT, timeout); + + sethandler.handler = pulseout_timer_handler; + sethandler.arg = (void *)(int)self->pwm_num; + + ioctl(pulse_fd, TCIOC_SETHANDLER, (unsigned long)&sethandler); + ioctl(pulse_fd, TCIOC_START, 0); + + while(pulse_index < len) { + // Do other things while we wait. The interrupts will handle sending the + // signal. + RUN_BACKGROUND_TASKS; + } + + pulse_buffer = NULL; +} + +void pulseout_reset(void) { + ioctl(pulse_fd, TCIOC_STOP, 0); + close(pulse_fd); + pulse_fd = -1; + + pulse_buffer = NULL; +} diff --git a/ports/cxd56/common-hal/pulseio/PulseOut.h b/ports/cxd56/common-hal/pulseio/PulseOut.h new file mode 100644 index 000000000..61bc17527 --- /dev/null +++ b/ports/cxd56/common-hal/pulseio/PulseOut.h @@ -0,0 +1,39 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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. + */ + +#ifndef MICROPY_INCLUDED_CXD56_COMMON_HAL_PULSEIO_PULSEOUT_H +#define MICROPY_INCLUDED_CXD56_COMMON_HAL_PULSEIO_PULSEOUT_H + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + uint8_t pwm_num; +} pulseio_pulseout_obj_t; + +void pulseout_reset(void); + +#endif // MICROPY_INCLUDED_CXD56_COMMON_HAL_PULSEIO_PULSEOUT_H diff --git a/ports/cxd56/common-hal/pulseio/__init__.c b/ports/cxd56/common-hal/pulseio/__init__.c new file mode 100644 index 000000000..2bee925bc --- /dev/null +++ b/ports/cxd56/common-hal/pulseio/__init__.c @@ -0,0 +1 @@ +// No pulseio module functions. diff --git a/ports/cxd56/common-hal/rtc/RTC.c b/ports/cxd56/common-hal/rtc/RTC.c new file mode 100644 index 000000000..ce65e6acd --- /dev/null +++ b/ports/cxd56/common-hal/rtc/RTC.c @@ -0,0 +1,54 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 "py/obj.h" +#include "py/runtime.h" + +#include "shared-bindings/rtc/RTC.h" + +void common_hal_rtc_get_time(timeutils_struct_time_t *tm) { + struct timeval tv; + + gettimeofday(&tv, NULL); + timeutils_seconds_since_2000_to_struct_time(tv.tv_sec, tm); +} + +void common_hal_rtc_set_time(timeutils_struct_time_t *tm) { + struct timeval tv = {0}; + + tv.tv_sec = timeutils_seconds_since_2000(tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec); + settimeofday(&tv, NULL); +} + +int common_hal_rtc_get_calibration(void) { + return 0; +} + +void common_hal_rtc_set_calibration(int calibration) { + mp_raise_NotImplementedError(translate("RTC calibration is not supported on this board")); +} diff --git a/ports/cxd56/common-hal/rtc/RTC.h b/ports/cxd56/common-hal/rtc/RTC.h new file mode 100644 index 000000000..5647fdcf1 --- /dev/null +++ b/ports/cxd56/common-hal/rtc/RTC.h @@ -0,0 +1,30 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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. + */ + +#ifndef MICROPY_INCLUDED_CXD56_COMMON_HAL_RTC_RTC_H +#define MICROPY_INCLUDED_CXD56_COMMON_HAL_RTC_RTC_H + +#endif // MICROPY_INCLUDED_CXD56_COMMON_HAL_RTC_RTC_H diff --git a/ports/cxd56/common-hal/rtc/__init__.c b/ports/cxd56/common-hal/rtc/__init__.c new file mode 100644 index 000000000..92d25d563 --- /dev/null +++ b/ports/cxd56/common-hal/rtc/__init__.c @@ -0,0 +1 @@ +// No rtc module functions. diff --git a/ports/cxd56/common-hal/supervisor/Runtime.c b/ports/cxd56/common-hal/supervisor/Runtime.c new file mode 100755 index 000000000..a0d9e70ab --- /dev/null +++ b/ports/cxd56/common-hal/supervisor/Runtime.c @@ -0,0 +1,36 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 "shared-bindings/supervisor/Runtime.h" +#include "supervisor/serial.h" + +bool common_hal_get_serial_connected(void) { + return (bool) serial_connected(); +} + +bool common_hal_get_serial_bytes_available(void) { + return (bool) serial_bytes_available(); +} diff --git a/ports/cxd56/common-hal/supervisor/Runtime.h b/ports/cxd56/common-hal/supervisor/Runtime.h new file mode 100755 index 000000000..f4669c6ab --- /dev/null +++ b/ports/cxd56/common-hal/supervisor/Runtime.h @@ -0,0 +1,36 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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. + */ + +#ifndef MICROPY_INCLUDED_CXD56_COMMON_HAL_SUPERVISOR_RUNTIME_H +#define MICROPY_INCLUDED_CXD56_COMMON_HAL_SUPERVISOR_RUNTIME_H + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; +} super_runtime_obj_t; + +#endif // MICROPY_INCLUDED_CXD56_COMMON_HAL_SUPERVISOR_RUNTIME_H diff --git a/ports/cxd56/common-hal/supervisor/__init__.c b/ports/cxd56/common-hal/supervisor/__init__.c new file mode 100755 index 000000000..e240525f2 --- /dev/null +++ b/ports/cxd56/common-hal/supervisor/__init__.c @@ -0,0 +1,36 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 "shared-bindings/supervisor/__init__.h" +#include "shared-bindings/supervisor/Runtime.h" + +// The singleton supervisor.Runtime object, bound to supervisor.runtime +// It currently only has properties, and no state. +const super_runtime_obj_t common_hal_supervisor_runtime_obj = { + .base = { + .type = &supervisor_runtime_type, + }, +}; diff --git a/ports/cxd56/common-hal/time/__init__.c b/ports/cxd56/common-hal/time/__init__.c new file mode 100644 index 000000000..8f7326b62 --- /dev/null +++ b/ports/cxd56/common-hal/time/__init__.c @@ -0,0 +1,37 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 "py/mphal.h" + +#include "tick.h" + +uint64_t common_hal_time_monotonic(void) { + return ticks_ms; +} + +void common_hal_time_delay_ms(uint32_t delay) { + mp_hal_delay_ms(delay); +} diff --git a/ports/cxd56/fatfs_port.c b/ports/cxd56/fatfs_port.c new file mode 100644 index 000000000..e986b4c6e --- /dev/null +++ b/ports/cxd56/fatfs_port.c @@ -0,0 +1,46 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 "py/mphal.h" +#include "py/runtime.h" +#include "lib/oofatfs/ff.h" /* FatFs lower layer API */ +#include "lib/oofatfs/diskio.h" /* FatFs lower layer API */ +#include "lib/timeutils/timeutils.h" + +#if CIRCUITPY_RTC +#include "shared-bindings/rtc/RTC.h" +#endif + +DWORD get_fattime(void) { +#if CIRCUITPY_RTC + timeutils_struct_time_t tm; + common_hal_rtc_get_time(&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 >> 1); +#else + return ((2016 - 1980) << 25) | ((9) << 21) | ((1) << 16) | ((16) << 11) | ((43) << 5) | (35 / 2); +#endif +} diff --git a/ports/cxd56/mpconfigboard.h b/ports/cxd56/mpconfigboard.h new file mode 100644 index 000000000..e0fef60f0 --- /dev/null +++ b/ports/cxd56/mpconfigboard.h @@ -0,0 +1,39 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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. + */ + +#define MICROPY_HW_BOARD_NAME "SPRESENSE" +#define MICROPY_HW_MCU_NAME "CXD5602" +#define MICROPY_PY_SYS_PLATFORM "SPRESENSE" + +#define DEFAULT_I2C_BUS_SCL (&pin_D15) +#define DEFAULT_I2C_BUS_SDA (&pin_D14) + +#define DEFAULT_SPI_BUS_SCK (&pin_D13) +#define DEFAULT_SPI_BUS_MISO (&pin_D12) +#define DEFAULT_SPI_BUS_MOSI (&pin_D11) + +#define DEFAULT_UART_BUS_RX (&pin_D0) +#define DEFAULT_UART_BUS_TX (&pin_D1) diff --git a/ports/cxd56/mpconfigport.h b/ports/cxd56/mpconfigport.h new file mode 100644 index 000000000..233b7b9a7 --- /dev/null +++ b/ports/cxd56/mpconfigport.h @@ -0,0 +1,38 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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. + */ + +#ifndef __INCLUDED_MPCONFIGPORT_H +#define __INCLUDED_MPCONFIGPORT_H + +// 24kiB stack +#define CIRCUITPY_DEFAULT_STACK_SIZE 0x6000 + +#include "py/circuitpy_mpconfig.h" + +#define MICROPY_PORT_ROOT_POINTERS \ + CIRCUITPY_COMMON_ROOT_POINTERS \ + +#endif // __INCLUDED_MPCONFIGPORT_H diff --git a/ports/cxd56/mpconfigport.mk b/ports/cxd56/mpconfigport.mk new file mode 100644 index 000000000..53dba2840 --- /dev/null +++ b/ports/cxd56/mpconfigport.mk @@ -0,0 +1,24 @@ +USB_VID = 0x054c +USB_PID = 0x0bc2 +USB_PRODUCT = "Spresense" +USB_MANUFACTURER = "Sony" +USB_SERIAL_NUMBER_LENGTH = 10 +USB_DEVICES = "CDC,MSC" +USB_MSC_MAX_PACKET_SIZE = 512 +USB_RENUMBER_ENDPOINTS = 0 +USB_CDC_EP_NUM_NOTIFICATION = 3 +USB_CDC_EP_NUM_DATA_OUT = 2 +USB_CDC_EP_NUM_DATA_IN = 1 +USB_MSC_EP_NUM_OUT = 5 +USB_MSC_EP_NUM_IN = 4 + +CIRCUITPY_AUDIOIO = 0 +CIRCUITPY_AUDIOBUSIO = 0 +CIRCUITPY_I2CSLAVE = 0 +CIRCUITPY_ROTARYIO = 0 +CIRCUITPY_TOUCHIO = 0 +CIRCUITPY_GAMEPAD = 0 +CIRCUITPY_NEOPIXEL_WRITE = 0 +CIRCUITPY_NVM = 0 +CIRCUITPY_DISPLAYIO = 0 +CIRCUITPY_FREQUENCYIO = 0 diff --git a/ports/cxd56/mphalport.c b/ports/cxd56/mphalport.c new file mode 100644 index 000000000..79d93f975 --- /dev/null +++ b/ports/cxd56/mphalport.c @@ -0,0 +1,88 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 +#include + +#include "py/mpstate.h" + +#include "tick.h" + +#define DELAY_CORRECTION (700) +#define DELAY_INTERVAL (50) + +void mp_hal_init(void) { + boardctl(BOARDIOC_INIT, 0); +} + +mp_uint_t mp_hal_ticks_ms(void) { + struct timeval tv; + gettimeofday(&tv, NULL); + return tv.tv_sec * 1000 + tv.tv_usec / 1000; +} + +mp_uint_t mp_hal_ticks_us(void) { + struct timeval tv; + gettimeofday(&tv, NULL); + return tv.tv_sec * 1000000 + tv.tv_usec; +} + +mp_uint_t mp_hal_ticks_cpu(void) { + return cxd56_get_cpu_baseclk(); +} + +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)) || + MP_STATE_VM(mp_pending_exception) == MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_reload_exception))) { + break; + } + duration = (ticks_ms - start_tick); + // TODO(tannewt): Go to sleep for a little while while we wait. + } +} + +void mp_hal_delay_us(uint32_t us) { + if (us) { + unsigned long long ticks = mp_hal_ticks_cpu() / 1000000L * us; + if (ticks < DELAY_CORRECTION) return; // delay time already used in calculation + + ticks -= DELAY_CORRECTION; + ticks /= 6; + // following loop takes 6 cycles + do { + __asm__ __volatile__("nop"); + } while(--ticks); + } +} diff --git a/ports/cxd56/mphalport.h b/ports/cxd56/mphalport.h new file mode 100644 index 000000000..25bca97ad --- /dev/null +++ b/ports/cxd56/mphalport.h @@ -0,0 +1,36 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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. + */ + +#ifndef MICROPY_INCLUDED_CXD56_MPHALPORT_H +#define MICROPY_INCLUDED_CXD56_MPHALPORT_H + +#include + +#include "lib/utils/interrupt_char.h" + +extern volatile uint64_t ticks_ms; + +#endif // MICROPY_INCLUDED_CXD56_MPHALPORT_H diff --git a/ports/cxd56/qstrdefsport.h b/ports/cxd56/qstrdefsport.h new file mode 100644 index 000000000..3ba897069 --- /dev/null +++ b/ports/cxd56/qstrdefsport.h @@ -0,0 +1 @@ +// qstrs specific to this port diff --git a/ports/cxd56/spresense-exported-sdk b/ports/cxd56/spresense-exported-sdk new file mode 160000 index 000000000..b473b28a1 --- /dev/null +++ b/ports/cxd56/spresense-exported-sdk @@ -0,0 +1 @@ +Subproject commit b473b28a14a03f3d416b6e2c071bcfd4fb92cb63 diff --git a/ports/cxd56/supervisor/cpu.s b/ports/cxd56/supervisor/cpu.s new file mode 100755 index 000000000..9e6807a5e --- /dev/null +++ b/ports/cxd56/supervisor/cpu.s @@ -0,0 +1,27 @@ +.syntax unified +.cpu cortex-m4 +.thumb +.text +.align 2 + +@ uint cpu_get_regs_and_sp(r0=uint regs[10]) +.global cpu_get_regs_and_sp +.thumb +.thumb_func +.type cpu_get_regs_and_sp, %function +cpu_get_regs_and_sp: +@ store registers into given array +str r4, [r0], #4 +str r5, [r0], #4 +str r6, [r0], #4 +str r7, [r0], #4 +str r8, [r0], #4 +str r9, [r0], #4 +str r10, [r0], #4 +str r11, [r0], #4 +str r12, [r0], #4 +str r13, [r0], #4 + +@ return the sp +mov r0, sp +bx lr diff --git a/ports/cxd56/supervisor/internal_flash.c b/ports/cxd56/supervisor/internal_flash.c new file mode 100644 index 000000000..0c9a61e06 --- /dev/null +++ b/ports/cxd56/supervisor/internal_flash.c @@ -0,0 +1,114 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 "supervisor/flash.h" + +/* Prototypes for Remote API */ + +int FM_RawWrite(uint32_t offset, const void *buf, uint32_t size); +int FM_RawVerifyWrite(uint32_t offset, const void *buf, uint32_t size); +int FM_RawRead(uint32_t offset, void *buf, uint32_t size); +int FM_RawEraseSector(uint32_t sector); + +#define CXD56_SPIFLASHSIZE (16 * 1024 * 1024) + +#define SECTOR_SHIFT 12 +#define SECTOR_SIZE (1 << SECTOR_SHIFT) + +#define PAGE_SHIFT 9 +#define PAGE_SIZE (1 << PAGE_SHIFT) + +#define NO_SECTOR 0xffffffff + +uint8_t flash_cache[SECTOR_SIZE]; +uint32_t flash_sector = NO_SECTOR; + +void supervisor_flash_init(void) { +} + +uint32_t supervisor_flash_get_block_size(void) { + return FILESYSTEM_BLOCK_SIZE; +} + +uint32_t supervisor_flash_get_block_count(void) { + return CXD56_SPIFLASHSIZE >> PAGE_SHIFT; +} + +void supervisor_flash_flush(void) { + if (flash_sector == NO_SECTOR) { + return; + } + + FM_RawEraseSector(flash_sector); + FM_RawWrite(flash_sector << SECTOR_SHIFT, flash_cache, SECTOR_SIZE); + + flash_sector = NO_SECTOR; +} + +mp_uint_t supervisor_flash_read_blocks(uint8_t *dest, uint32_t block, uint32_t num_blocks) { + supervisor_flash_flush(); + + if (FM_RawRead(block << PAGE_SHIFT, dest, num_blocks << PAGE_SHIFT) < 0) { + return 1; + } + + return 0; // success +} + +mp_uint_t supervisor_flash_write_blocks(const uint8_t *src, uint32_t lba, uint32_t num_blocks) { + uint32_t sector; + uint32_t block_position; + uint32_t current_block = 0; + + while (num_blocks--) { + sector = (lba << PAGE_SHIFT) >> SECTOR_SHIFT; + block_position = lba - 8 * sector; + + if (sector != flash_sector) { + supervisor_flash_flush(); + + if (FM_RawRead(sector << SECTOR_SHIFT, flash_cache, SECTOR_SIZE) < 0) { + return 1; + } + + if (memcmp(&flash_cache[block_position << PAGE_SHIFT], &src[current_block << PAGE_SHIFT], PAGE_SIZE) != 0) { + flash_sector = sector; + } + } + + memcpy(&flash_cache[block_position << PAGE_SHIFT], &src[current_block << PAGE_SHIFT], PAGE_SIZE); + + lba++; + current_block++; + } + + return 0; // success +} + +void supervisor_flash_release_cache(void) { +} diff --git a/ports/cxd56/supervisor/internal_flash.h b/ports/cxd56/supervisor/internal_flash.h new file mode 100644 index 000000000..1580ad3e1 --- /dev/null +++ b/ports/cxd56/supervisor/internal_flash.h @@ -0,0 +1,30 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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. + */ + +#ifndef MICROPY_INCLUDED_CXD56_INTERNAL_FLASH_H +#define MICROPY_INCLUDED_CXD56_INTERNAL_FLASH_H + +#endif // MICROPY_INCLUDED_CXD56_INTERNAL_FLASH_H diff --git a/ports/cxd56/supervisor/internal_flash_root_pointers.h b/ports/cxd56/supervisor/internal_flash_root_pointers.h new file mode 100644 index 000000000..126c6af09 --- /dev/null +++ b/ports/cxd56/supervisor/internal_flash_root_pointers.h @@ -0,0 +1,31 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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. + */ +#ifndef MICROPY_INCLUDED_CXD56_INTERNAL_FLASH_ROOT_POINTERS_H +#define MICROPY_INCLUDED_CXD56_INTERNAL_FLASH_ROOT_POINTERS_H + +#define FLASH_ROOT_POINTERS + +#endif // MICROPY_INCLUDED_CXD56_INTERNAL_FLASH_ROOT_POINTERS_H diff --git a/ports/cxd56/supervisor/port.c b/ports/cxd56/supervisor/port.c new file mode 100644 index 000000000..ab1ea5273 --- /dev/null +++ b/ports/cxd56/supervisor/port.c @@ -0,0 +1,73 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 "supervisor/port.h" + +#include "common-hal/microcontroller/Pin.h" +#include "common-hal/analogio/AnalogIn.h" +#include "common-hal/pulseio/PulseOut.h" +#include "common-hal/pulseio/PWMOut.h" + +safe_mode_t port_init(void) { + boardctl(BOARDIOC_INIT, 0); + return NO_SAFE_MODE; +} + +void reset_cpu(void) { + boardctl(BOARDIOC_RESET, 0); +} + +void reset_port(void) { +#if CIRCUITPY_ANALOGIO + analogin_reset(); +#endif +#if CIRCUITPY_PULSEIO + pulseout_reset(); + pwmout_reset(); +#endif + + reset_all_pins(); +} + +void reset_board(void) { +} + +void reset_to_bootloader(void) { +} + +extern uint32_t _ebss; + +// Place the word to save just after our BSS section that gets blanked. +void port_set_saved_word(uint32_t value) { + _ebss = value; +} + +uint32_t port_get_saved_word(void) { + return _ebss; +} diff --git a/ports/cxd56/supervisor/usb.c b/ports/cxd56/supervisor/usb.c new file mode 100644 index 000000000..6ad253c6d --- /dev/null +++ b/ports/cxd56/supervisor/usb.c @@ -0,0 +1,30 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 "supervisor/usb.h" + +void init_usb_hardware(void) { +} diff --git a/ports/cxd56/tick.c b/ports/cxd56/tick.c new file mode 100644 index 000000000..6529db790 --- /dev/null +++ b/ports/cxd56/tick.c @@ -0,0 +1,45 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 "tick.h" + +#include "supervisor/shared/autoreload.h" +#include "supervisor/filesystem.h" + +// Global millisecond tick count +volatile uint64_t ticks_ms = 0; + +void board_timerhook(void) +{ + ticks_ms += 1; + +#if CIRCUITPY_FILESYSTEM_FLUSH_INTERVAL_MS > 0 + filesystem_tick(); +#endif +#ifdef CIRCUITPY_AUTORELOAD_DELAY_MS + autoreload_tick(); +#endif +} diff --git a/ports/cxd56/tick.h b/ports/cxd56/tick.h new file mode 100644 index 000000000..a0d9ee526 --- /dev/null +++ b/ports/cxd56/tick.h @@ -0,0 +1,34 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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. + */ + +#ifndef MICROPY_INCLUDED_CXD56_TICK_H +#define MICROPY_INCLUDED_CXD56_TICK_H + +#include "py/mpconfig.h" + +extern volatile uint64_t ticks_ms; + +#endif // MICROPY_INCLUDED_CXD56_TICK_H diff --git a/ports/spresense/Makefile b/ports/spresense/Makefile deleted file mode 100644 index d8f809635..000000000 --- a/ports/spresense/Makefile +++ /dev/null @@ -1,186 +0,0 @@ -# This file is part of the MicroPython project, http://micropython.org/ -# -# The MIT License (MIT) -# -# Copyright 2019 Sony Semiconductor Solutions 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 ../../py/mkenv.mk - -# Port-specific -include mpconfigport.mk - -# CircuitPython-specific -include $(TOP)/py/circuitpy_mpconfig.mk - -# qstr definitions (must come before including py.mk) -QSTR_DEFS = qstrdefsport.h - -# include py core make definitions -include $(TOP)/py/py.mk - -include $(TOP)/supervisor/supervisor.mk - -# Include make rules and variables common across CircuitPython builds. -include $(TOP)/py/circuitpy_defns.mk - -CROSS_COMPILE = arm-none-eabi- - -SPRESENSE_SDK = spresense-exported-sdk - -FIRMWARE = $(SPRESENSE_SDK)/firmware - -INC += \ - -I. \ - -I../.. \ - -I../lib/mp-readline \ - -I../lib/timeutils \ - -I../../lib/tinyusb/src \ - -I../../supervisor/shared/usb \ - -I$(BUILD) \ - -I$(SPRESENSE_SDK)/nuttx/include \ - -I$(SPRESENSE_SDK)/nuttx/arch \ - -I$(SPRESENSE_SDK)/nuttx/arch/chip \ - -I$(SPRESENSE_SDK)/sdk/bsp/include \ - -I$(SPRESENSE_SDK)/sdk/bsp/include/sdk \ - -CFLAGS += \ - $(INC) \ - -DCONFIG_WCHAR_BUILTIN \ - -DCONFIG_HAVE_DOUBLE \ - -Dmain=spresense_main \ - -D_estack=__stack \ - -c \ - -Os \ - -pipe \ - -std=gnu11 \ - -mcpu=cortex-m4 \ - -mthumb \ - -mfpu=fpv4-sp-d16 \ - -mfloat-abi=hard \ - -mabi=aapcs \ - -fno-builtin \ - -fno-strict-aliasing \ - -fno-strength-reduce \ - -fomit-frame-pointer \ - -ffunction-sections \ - -fdata-sections \ - -Wall \ - -LIBM = "${shell "$(CC)" $(CFLAGS) -print-file-name=libm.a}" - -LIBGCC = "${shell "$(CC)" $(CFLAGS) -print-libgcc-file-name}" - -LDFLAGS = \ - --entry=__start \ - -nostartfiles \ - -nodefaultlibs \ - --defsym __stack=_vectors+786432 \ - -T$(SPRESENSE_SDK)/nuttx/build/ramconfig.ld \ - --gc-sections \ - -Map=$(BUILD)/output.map \ - -o $(BUILD)/circuitpython.elf \ - --start-group \ - -u spresense_main \ - $(BUILD)/libmpy.a \ - $(SPRESENSE_SDK)/sdk/libs/libapps.a \ - $(SPRESENSE_SDK)/sdk/libs/libsdk.a \ - $(LIBM) \ - $(LIBGCC) \ - --end-group \ - -L$(BUILD) \ - -CFLAGS += -DCFG_TUSB_MCU=OPT_MCU_CXD56 -DCFG_TUD_MIDI_RX_BUFSIZE=128 -DCFG_TUD_CDC_RX_BUFSIZE=256 -DCFG_TUD_MIDI_TX_BUFSIZE=128 -DCFG_TUD_CDC_TX_BUFSIZE=256 -DCFG_TUD_MSC_BUFSIZE=512 $(CFLAGS_MOD) - -SRC_COMMON_HAL_EXPANDED = $(addprefix shared-bindings/, $(SRC_COMMON_HAL)) \ - $(addprefix shared-bindings/, $(SRC_BINDINGS_ENUMS)) \ - $(addprefix common-hal/, $(SRC_COMMON_HAL)) - -SRC_SHARED_MODULE_EXPANDED = $(addprefix shared-bindings/, $(SRC_SHARED_MODULE)) \ - $(addprefix shared-module/, $(SRC_SHARED_MODULE)) - -SRC_S = supervisor/cpu.s - -SRC_C = \ - tick.c \ - background.c \ - fatfs_port.c \ - mphalport.c \ - lib/utils/stdout_helpers.c \ - lib/utils/pyexec.c \ - lib/libc/string0.c \ - lib/mp-readline/readline.c \ - lib/timeutils/timeutils.c \ - lib/oofatfs/ff.c \ - lib/oofatfs/option/ccsbcs.c \ - lib/utils/interrupt_char.c \ - lib/utils/sys_stdio_mphal.c \ - lib/utils/context_manager_helpers.c \ - lib/utils/buffer_helper.c \ - supervisor/shared/memory.c \ - lib/tinyusb/src/portable/sony/cxd56/dcd_cxd56.c \ - -OBJ = $(PY_O) $(SUPERVISOR_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) -OBJ += $(addprefix $(BUILD)/, $(SRC_S:.s=.o)) -OBJ += $(addprefix $(BUILD)/, $(SRC_COMMON_HAL_EXPANDED:.c=.o)) -OBJ += $(addprefix $(BUILD)/, $(SRC_SHARED_MODULE_EXPANDED:.c=.o)) - -# List of sources for qstr extraction -SRC_QSTR += $(SRC_C) $(SRC_SUPERVISOR) $(SRC_COMMON_HAL_EXPANDED) $(SRC_SHARED_MODULE_EXPANDED) -# Sources that only hold QSTRs after pre-processing. -SRC_QSTR_PREPROCESSOR += - -all: $(BUILD)/circuitpython.spk - -$(FIRMWARE): - $(ECHO) "" - $(ECHO) "Download the spresense binaries zip archive from:" - $(ECHO) "https://developer.sony.com/file/download/download-spresense-firmware-v1-4-000" - $(ECHO) "Extract spresense binaries to $(FIRMWARE)" - $(ECHO) "" - $(ECHO) "run make flash-bootloader again to flash bootloader." - exit 1 - -$(BUILD)/libmpy.a: $(SPRESENSE_SDK) $(OBJ) - $(ECHO) "AR $@" - $(Q)$(AR) rcs $(BUILD)/libmpy.a $(OBJ) - -$(BUILD)/circuitpython.elf: $(BUILD)/libmpy.a - $(ECHO) "LD $@" - $(Q)$(LD) $(LDFLAGS) - -$(BUILD)/circuitpython.spk: $(BUILD)/circuitpython.elf - $(ECHO) "Creating $@" - $(SPRESENSE_SDK)/sdk/tools/linux/mkspk -c 2 $(BUILD)/circuitpython.elf nuttx $(BUILD)/circuitpython.spk - -flash: $(BUILD)/circuitpython.spk - $(ECHO) "Writing $< to the board" - $(SPRESENSE_SDK)/sdk/tools/flash.sh -c /dev/ttyUSB0 $(BUILD)/circuitpython.spk - -flash-bootloader: $(SPRESENSE_SDK) $(FIRMWARE) - $(ECHO) "Writing loader to the board" - $(SPRESENSE_SDK)/sdk/tools/flash.sh -l $(FIRMWARE) -c /dev/ttyUSB0 - -include $(TOP)/py/mkrules.mk - -# Print out the value of a make variable. -# https://stackoverflow.com/questions/16467718/how-to-print-out-a-variable-in-makefile -print-%: - @echo $* = $($*) diff --git a/ports/spresense/README.md b/ports/spresense/README.md deleted file mode 100644 index af4a6dea1..000000000 --- a/ports/spresense/README.md +++ /dev/null @@ -1,86 +0,0 @@ -CircuitPython port to Spresense -============================== - -This directory contains the port of CircuitPython to Spresense. It is a compact -development board based on Sony’s power-efficient multicore microcontroller -CXD5602. - -Board features: - -* Integrated GPS - * The embedded GNSS with support for GPS, QZSS and GLONASS enables applications - where tracking is required. -* Hi-res audio output and multi mic inputs - * Advanced 192kHz/24 bit audio codec and amplifier for audio output, and - support for up to 8 mic input channels. -* Multicore microcontroller - * Spresense is powered by Sony's CXD5602 microcontroller (ARM® Cortex®-M4F × 6 - cores), with a clock speed of 156 MHz. - -Currently, Spresense port does not support GNSS, Audio and Multicore. - -Refer to [developer.sony.com/develop/spresense/](https://developer.sony.com/develop/spresense/) -for further information about this board. - -Build instructions ------------------- - -Pull all submodules into your clone: - - $ git submodule update --init --recursive - -Build the MicroPython cross-compiler: - - $ make -C mpy-cross - -Change directory to spresense: - - $ cd ports/spresense - -To build circuitpython image run: - - $ make - -USB connection --------------- - -Add user to `dialout` group: - - $ sudo usermod -a -G dialout - -Connect the Spresense main board to the PC via the USB cable. - -Flash the bootloader --------------------- - -The correct bootloader is required for the Spresense board to function. - -Bootloader information: - -* The bootloader has to be flashed the very first time the board is used. - -* You have to accept the End User License Agreement to be able to download and use the Spresense bootloader binary. - -Download the spresense binaries zip archive from: - -Extract spresense binaries in your PC to ports/spresense/spresense-exported-sdk/firmware/ - -To flash the bootloader run the command: - - $ make flash-bootloader - -Flash the circuitpython.spk image ---------------------------------- - -To flash the firmware run the command: - - $ make flash - -Accessing the board -------------------- - -Connect the Spresense extension board to the PC via the USB cable. - -Once built and deployed, access the CircuitPython REPL (the Python prompt) via USB. You can run: - - $ screen /dev/ttyACM0 115200 diff --git a/ports/spresense/alloca.h b/ports/spresense/alloca.h deleted file mode 100644 index aad2f8b5b..000000000 --- a/ports/spresense/alloca.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef _ALLOCA_H -#define _ALLOCA_H - -#define alloca __builtin_alloca - -#endif /* _ALLOCA_H */ diff --git a/ports/spresense/background.c b/ports/spresense/background.c deleted file mode 100644 index ade257dd2..000000000 --- a/ports/spresense/background.c +++ /dev/null @@ -1,53 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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 "background.h" - -#include "supervisor/usb.h" -#include "supervisor/filesystem.h" -#include "supervisor/shared/stack.h" - -static bool running_background_tasks = false; - -void background_tasks_reset(void) { - running_background_tasks = false; -} - -void run_background_tasks(void) { - // Don't call ourselves recursively. - if (running_background_tasks) { - return; - } - - assert_heap_ok(); - running_background_tasks = true; - - usb_background(); - filesystem_background(); - - running_background_tasks = false; - assert_heap_ok(); -} diff --git a/ports/spresense/background.h b/ports/spresense/background.h deleted file mode 100644 index 39e7c4fc8..000000000 --- a/ports/spresense/background.h +++ /dev/null @@ -1,33 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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. - */ - -#ifndef MICROPY_INCLUDED_SPRESENSE_BACKGROUND_H -#define MICROPY_INCLUDED_SPRESENSE_BACKGROUND_H - -void background_tasks_reset(void); -void run_background_tasks(void); - -#endif // MICROPY_INCLUDED_SPRESENSE_BACKGROUND_H diff --git a/ports/spresense/common-hal/analogio/AnalogIn.c b/ports/spresense/common-hal/analogio/AnalogIn.c deleted file mode 100644 index 438fbbcae..000000000 --- a/ports/spresense/common-hal/analogio/AnalogIn.c +++ /dev/null @@ -1,131 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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 - -#include -#include - -#include "py/runtime.h" - -#include "shared-bindings/analogio/AnalogIn.h" - -typedef struct { - const char* devpath; - const mcu_pin_obj_t *pin; - int fd; -} analogin_dev_t; - -STATIC analogin_dev_t analogin_dev[] = { - {"/dev/lpadc0", &pin_A0, -1}, - {"/dev/lpadc1", &pin_A1, -1}, - {"/dev/lpadc2", &pin_A2, -1}, - {"/dev/lpadc3", &pin_A3, -1}, - {"/dev/hpadc0", &pin_A4, -1}, - {"/dev/hpadc1", &pin_A5, -1}, -}; - -void common_hal_analogio_analogin_construct(analogio_analogin_obj_t *self, const mcu_pin_obj_t *pin) { - if (!pin->analog) { - mp_raise_ValueError(translate("AnalogIn not supported on given pin")); - } - - self->number = -1; - - for (int i = 0; i < MP_ARRAY_SIZE(analogin_dev); i++) { - if (pin->number == analogin_dev[i].pin->number) { - self->number = i; - break; - } - } - - if (self->number < 0) { - mp_raise_ValueError(translate("Pin does not have ADC capabilities")); - } - - analogin_dev[self->number].fd = open(analogin_dev[pin->number].devpath, O_RDONLY); - if (analogin_dev[self->number].fd < 0) { - mp_raise_ValueError(translate("Pin does not have ADC capabilities")); - } - - // SCU FIFO overwrite - ioctl(analogin_dev[self->number].fd, SCUIOC_SETFIFOMODE, 1); - - // ADC FIFO size - ioctl(analogin_dev[self->number].fd, ANIOC_CXD56_FIFOSIZE, 2); - - // start ADC - ioctl(analogin_dev[self->number].fd, ANIOC_CXD56_START, 0); - - self->pin = pin; -} - -void common_hal_analogio_analogin_deinit(analogio_analogin_obj_t *self) { - if (common_hal_analogio_analogin_deinited(self)) { - return; - } - - // stop ADC - ioctl(analogin_dev[self->number].fd, ANIOC_CXD56_STOP, 0); - close(analogin_dev[self->number].fd); - analogin_dev[self->number].fd = -1; - - self->pin = mp_const_none; -} - -bool common_hal_analogio_analogin_deinited(analogio_analogin_obj_t *self) { - return self->pin == mp_const_none; -} - -uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) { - uint16_t value = 0; - - read(analogin_dev[self->number].fd, &value, sizeof(value)); - - return value; -} - -// Reference voltage is a fixed value which is depending on the board. -// e.g.) -// - Reference Voltage of A4 and A5 pins on Main Board is 0.7V. -// - Reference Voltage of A0 ~ A5 pins on External Interface board -// is selected 3.3V or 5.0V by a IO Volt jumper pin. -float common_hal_analogio_analogin_get_reference_voltage(analogio_analogin_obj_t *self) { - return 3.3f; -} - -void analogin_reset(void) { - for (int i = 0; i < MP_ARRAY_SIZE(analogin_dev); i++) { - if (analogin_dev[i].fd >= 0) { - // stop ADC - ioctl(analogin_dev[i].fd, ANIOC_CXD56_STOP, 0); - close(analogin_dev[i].fd); - analogin_dev[i].fd = -1; - } - } -} diff --git a/ports/spresense/common-hal/analogio/AnalogIn.h b/ports/spresense/common-hal/analogio/AnalogIn.h deleted file mode 100644 index 7fc197f98..000000000 --- a/ports/spresense/common-hal/analogio/AnalogIn.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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. - */ - -#ifndef MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_ANALOGIO_ANALOGIN_H -#define MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_ANALOGIO_ANALOGIN_H - -#include "py/obj.h" - -#include "common-hal/microcontroller/Pin.h" - -typedef struct { - mp_obj_base_t base; - const mcu_pin_obj_t *pin; - uint8_t number; -} analogio_analogin_obj_t; - -void analogin_reset(void); - -#endif // MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_ANALOGIO_ANALOGIN_H diff --git a/ports/spresense/common-hal/analogio/AnalogOut.c b/ports/spresense/common-hal/analogio/AnalogOut.c deleted file mode 100644 index 3f1abe80d..000000000 --- a/ports/spresense/common-hal/analogio/AnalogOut.c +++ /dev/null @@ -1,43 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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 "py/runtime.h" - -#include "shared-bindings/analogio/AnalogOut.h" - -void common_hal_analogio_analogout_construct(analogio_analogout_obj_t *self, const mcu_pin_obj_t *pin) { - mp_raise_RuntimeError(translate("AnalogOut functionality not supported")); -} - -void common_hal_analogio_analogout_deinit(analogio_analogout_obj_t *self) { -} - -bool common_hal_analogio_analogout_deinited(analogio_analogout_obj_t *self) { - return true; -} - -void common_hal_analogio_analogout_set_value(analogio_analogout_obj_t *self, uint16_t value) { -} diff --git a/ports/spresense/common-hal/analogio/AnalogOut.h b/ports/spresense/common-hal/analogio/AnalogOut.h deleted file mode 100644 index 60d3cf0a1..000000000 --- a/ports/spresense/common-hal/analogio/AnalogOut.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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. - */ - -#ifndef MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_ANALOGIO_ANALOGOUT_H -#define MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_ANALOGIO_ANALOGOUT_H - -#include "py/obj.h" - -typedef struct { - mp_obj_base_t base; -} analogio_analogout_obj_t; - -#endif // MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_ANALOGIO_ANALOGOUT_H diff --git a/ports/spresense/common-hal/analogio/__init__.c b/ports/spresense/common-hal/analogio/__init__.c deleted file mode 100644 index eea58c77d..000000000 --- a/ports/spresense/common-hal/analogio/__init__.c +++ /dev/null @@ -1 +0,0 @@ -// No analogio module functions. diff --git a/ports/spresense/common-hal/board/__init__.c b/ports/spresense/common-hal/board/__init__.c deleted file mode 100644 index d123969fe..000000000 --- a/ports/spresense/common-hal/board/__init__.c +++ /dev/null @@ -1,80 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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 "shared-bindings/board/__init__.h" - -STATIC const mp_rom_map_elem_t board_module_globals_table[] = { - { MP_ROM_QSTR(MP_QSTR_D0), MP_ROM_PTR(&pin_D0) }, - { MP_ROM_QSTR(MP_QSTR_D1), MP_ROM_PTR(&pin_D1) }, - { MP_ROM_QSTR(MP_QSTR_D2), MP_ROM_PTR(&pin_D2) }, - { MP_ROM_QSTR(MP_QSTR_D3), MP_ROM_PTR(&pin_D3) }, - { MP_ROM_QSTR(MP_QSTR_D4), MP_ROM_PTR(&pin_D4) }, - { MP_ROM_QSTR(MP_QSTR_D5), MP_ROM_PTR(&pin_D5) }, - { MP_ROM_QSTR(MP_QSTR_D6), MP_ROM_PTR(&pin_D6) }, - { MP_ROM_QSTR(MP_QSTR_D7), MP_ROM_PTR(&pin_D7) }, - { MP_ROM_QSTR(MP_QSTR_D8), MP_ROM_PTR(&pin_D8) }, - { MP_ROM_QSTR(MP_QSTR_D9), MP_ROM_PTR(&pin_D9) }, - { MP_ROM_QSTR(MP_QSTR_D10), MP_ROM_PTR(&pin_D10) }, - { MP_ROM_QSTR(MP_QSTR_D11), MP_ROM_PTR(&pin_D11) }, - { MP_ROM_QSTR(MP_QSTR_D12), MP_ROM_PTR(&pin_D12) }, - { MP_ROM_QSTR(MP_QSTR_D13), MP_ROM_PTR(&pin_D13) }, - { MP_ROM_QSTR(MP_QSTR_D14), MP_ROM_PTR(&pin_D14) }, - { MP_ROM_QSTR(MP_QSTR_D15), MP_ROM_PTR(&pin_D15) }, - { MP_ROM_QSTR(MP_QSTR_D16), MP_ROM_PTR(&pin_D16) }, - { MP_ROM_QSTR(MP_QSTR_D17), MP_ROM_PTR(&pin_D17) }, - { MP_ROM_QSTR(MP_QSTR_D18), MP_ROM_PTR(&pin_D18) }, - { MP_ROM_QSTR(MP_QSTR_D19), MP_ROM_PTR(&pin_D19) }, - { MP_ROM_QSTR(MP_QSTR_D20), MP_ROM_PTR(&pin_D20) }, - { MP_ROM_QSTR(MP_QSTR_D21), MP_ROM_PTR(&pin_D21) }, - { MP_ROM_QSTR(MP_QSTR_D22), MP_ROM_PTR(&pin_D22) }, - { MP_ROM_QSTR(MP_QSTR_D23), MP_ROM_PTR(&pin_D23) }, - { MP_ROM_QSTR(MP_QSTR_D24), MP_ROM_PTR(&pin_D24) }, - { MP_ROM_QSTR(MP_QSTR_D25), MP_ROM_PTR(&pin_D25) }, - { MP_ROM_QSTR(MP_QSTR_D26), MP_ROM_PTR(&pin_D26) }, - { MP_ROM_QSTR(MP_QSTR_D27), MP_ROM_PTR(&pin_D27) }, - { MP_ROM_QSTR(MP_QSTR_D28), MP_ROM_PTR(&pin_D28) }, - { MP_ROM_QSTR(MP_QSTR_LED0), MP_ROM_PTR(&pin_LED0) }, - { MP_ROM_QSTR(MP_QSTR_LED1), MP_ROM_PTR(&pin_LED1) }, - { MP_ROM_QSTR(MP_QSTR_LED2), MP_ROM_PTR(&pin_LED2) }, - { MP_ROM_QSTR(MP_QSTR_LED3), MP_ROM_PTR(&pin_LED3) }, - { MP_ROM_QSTR(MP_QSTR_A0), MP_ROM_PTR(&pin_A0) }, - { MP_ROM_QSTR(MP_QSTR_A1), MP_ROM_PTR(&pin_A1) }, - { MP_ROM_QSTR(MP_QSTR_A2), MP_ROM_PTR(&pin_A2) }, - { MP_ROM_QSTR(MP_QSTR_A3), MP_ROM_PTR(&pin_A3) }, - { MP_ROM_QSTR(MP_QSTR_A4), MP_ROM_PTR(&pin_A4) }, - { MP_ROM_QSTR(MP_QSTR_A5), MP_ROM_PTR(&pin_A5) }, - { MP_ROM_QSTR(MP_QSTR_SDA), MP_ROM_PTR(&pin_D14) }, - { MP_ROM_QSTR(MP_QSTR_SCL), MP_ROM_PTR(&pin_D15) }, - { MP_ROM_QSTR(MP_QSTR_SCK), MP_ROM_PTR(&pin_D13) }, - { MP_ROM_QSTR(MP_QSTR_MISO), MP_ROM_PTR(&pin_D12) }, - { MP_ROM_QSTR(MP_QSTR_MOSI), MP_ROM_PTR(&pin_D11) }, - { MP_ROM_QSTR(MP_QSTR_RX), MP_ROM_PTR(&pin_D0) }, - { MP_ROM_QSTR(MP_QSTR_TX), MP_ROM_PTR(&pin_D1) }, - { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, - { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, - { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&board_uart_obj) }, -}; -MP_DEFINE_CONST_DICT(board_module_globals, board_module_globals_table); diff --git a/ports/spresense/common-hal/busio/I2C.c b/ports/spresense/common-hal/busio/I2C.c deleted file mode 100644 index c163c183a..000000000 --- a/ports/spresense/common-hal/busio/I2C.c +++ /dev/null @@ -1,123 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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 - -#include "py/runtime.h" - -#include "shared-bindings/busio/I2C.h" - -void common_hal_busio_i2c_construct(busio_i2c_obj_t *self, const mcu_pin_obj_t *scl, - const mcu_pin_obj_t *sda, uint32_t frequency, uint32_t timeout) { - if (frequency != I2C_SPEED_STANDARD && frequency != I2C_SPEED_FAST) { - mp_raise_ValueError(translate("Unsupported baudrate")); - } - - if (scl->number != PIN_I2C0_BCK || sda->number != PIN_I2C0_BDT) { - mp_raise_ValueError(translate("Invalid pins")); - } - - claim_pin(scl); - claim_pin(sda); - - self->scl_pin = scl; - self->sda_pin = sda; - self->frequency = frequency; - self->i2c_dev = cxd56_i2cbus_initialize(0); -} - -void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) { - if (common_hal_busio_i2c_deinited(self)) { - return; - } - - cxd56_i2cbus_uninitialize(self->i2c_dev); - self->i2c_dev = NULL; - - reset_pin_number(self->scl_pin->number); - reset_pin_number(self->sda_pin->number); -} - -bool common_hal_busio_i2c_deinited(busio_i2c_obj_t *self) { - return self->i2c_dev == NULL; -} - -bool common_hal_busio_i2c_try_lock(busio_i2c_obj_t *self) { - bool grabbed_lock = false; - if (!self->has_lock) { - grabbed_lock = true; - self->has_lock = true; - } - return grabbed_lock; -} - -bool common_hal_busio_i2c_has_lock(busio_i2c_obj_t *self) { - return self->has_lock; -} - -void common_hal_busio_i2c_unlock(busio_i2c_obj_t *self) { - self->has_lock = false; -} - -bool common_hal_busio_i2c_probe(busio_i2c_obj_t *self, uint8_t addr) { - struct i2c_msg_s msg; - - msg.frequency = self->frequency; - msg.addr = addr; - msg.flags = 0; - msg.buffer = NULL; - msg.length = 0; - return I2C_TRANSFER(self->i2c_dev, &msg, 1) < 0 ? false : true; -} - -uint8_t common_hal_busio_i2c_write(busio_i2c_obj_t *self, uint16_t address, const uint8_t *data, size_t len, bool stop) { - struct i2c_msg_s msg; - - msg.frequency = self->frequency; - msg.addr = address; - msg.flags = (stop ? 0 : I2C_M_NOSTOP); - msg.buffer = (uint8_t *) data; - msg.length = len; - return I2C_TRANSFER(self->i2c_dev, &msg, 1); -} - -uint8_t common_hal_busio_i2c_read(busio_i2c_obj_t *self, uint16_t address, uint8_t *data, size_t len) { - struct i2c_msg_s msg; - - msg.frequency = self->frequency; - msg.addr = address; - msg.flags = I2C_M_READ; - msg.buffer = data; - msg.length = len; - return I2C_TRANSFER(self->i2c_dev, &msg, 1); -} - -void common_hal_busio_i2c_never_reset(busio_i2c_obj_t *self) { - never_reset_pin_number(self->scl_pin->number); - never_reset_pin_number(self->sda_pin->number); -} diff --git a/ports/spresense/common-hal/busio/I2C.h b/ports/spresense/common-hal/busio/I2C.h deleted file mode 100644 index ec2acfb53..000000000 --- a/ports/spresense/common-hal/busio/I2C.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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. - */ - -#ifndef MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_BUSIO_I2C_H -#define MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_BUSIO_I2C_H - -#include "py/obj.h" - -#include "common-hal/microcontroller/Pin.h" - -typedef struct { - mp_obj_base_t base; - struct i2c_master_s* i2c_dev; - uint32_t frequency; - bool has_lock; - const mcu_pin_obj_t *scl_pin; - const mcu_pin_obj_t *sda_pin; -} busio_i2c_obj_t; - -#endif // MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_BUSIO_I2C_H diff --git a/ports/spresense/common-hal/busio/OneWire.h b/ports/spresense/common-hal/busio/OneWire.h deleted file mode 100644 index a76ee8c77..000000000 --- a/ports/spresense/common-hal/busio/OneWire.h +++ /dev/null @@ -1,33 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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. - */ - -#ifndef MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_BUSIO_ONEWIRE_H -#define MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_BUSIO_ONEWIRE_H - -// Use bitbangio. -#include "shared-module/busio/OneWire.h" - -#endif // MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_BUSIO_ONEWIRE_H diff --git a/ports/spresense/common-hal/busio/SPI.c b/ports/spresense/common-hal/busio/SPI.c deleted file mode 100644 index 9a41011f2..000000000 --- a/ports/spresense/common-hal/busio/SPI.c +++ /dev/null @@ -1,155 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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/runtime.h" - -#include "shared-bindings/busio/SPI.h" - -void common_hal_busio_spi_construct(busio_spi_obj_t *self, const mcu_pin_obj_t *clock, - const mcu_pin_obj_t *mosi, const mcu_pin_obj_t *miso) { - int port = -1; - - if (clock->number == PIN_SPI4_SCK && mosi->number == PIN_SPI4_MOSI && miso->number == PIN_SPI4_MISO) { - port = 4; - } else if (clock->number == PIN_EMMC_CLK && mosi->number == PIN_EMMC_DATA0 && miso->number == PIN_EMMC_DATA1) { - port = 5; - } - - if (port < 0) { - mp_raise_ValueError(translate("Invalid pins")); - } - - claim_pin(clock); - claim_pin(mosi); - claim_pin(miso); - - self->clock_pin = clock; - self->mosi_pin = mosi; - self->miso_pin = miso; - self->spi_dev = cxd56_spibus_initialize(port); -} - -void common_hal_busio_spi_deinit(busio_spi_obj_t *self) { - if (common_hal_busio_spi_deinited(self)) { - return; - } - - self->spi_dev = NULL; - - reset_pin_number(self->clock_pin->number); - reset_pin_number(self->mosi_pin->number); - reset_pin_number(self->miso_pin->number); -} - -bool common_hal_busio_spi_deinited(busio_spi_obj_t *self) { - return self->spi_dev == NULL; -} - -bool common_hal_busio_spi_configure(busio_spi_obj_t *self, uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t bits) { - uint8_t mode; - - self->frequency = baudrate; - SPI_SETFREQUENCY(self->spi_dev, baudrate); - - if (polarity == 0) { - if (phase == 0) { - mode = SPIDEV_MODE0; - } else { - mode = SPIDEV_MODE1; - } - } else { - if (phase == 0) { - mode = SPIDEV_MODE2; - } else { - mode = SPIDEV_MODE3; - } - } - - self->polarity = polarity; - self->phase = phase; - SPI_SETMODE(self->spi_dev, mode); - - self->bits = bits; - SPI_SETBITS(self->spi_dev, bits); - - return true; -} - -bool common_hal_busio_spi_try_lock(busio_spi_obj_t *self) { - bool grabbed_lock = false; - if (!self->has_lock) { - grabbed_lock = true; - self->has_lock = true; - } - return grabbed_lock; -} - -bool common_hal_busio_spi_has_lock(busio_spi_obj_t *self) { - return self->has_lock; -} - -void common_hal_busio_spi_unlock(busio_spi_obj_t *self) { - self->has_lock = false; -} - -bool common_hal_busio_spi_write(busio_spi_obj_t *self, const uint8_t *data, size_t len) { - SPI_EXCHANGE(self->spi_dev, data, NULL, len); - - return true; -} - -bool common_hal_busio_spi_read(busio_spi_obj_t *self, uint8_t *data, size_t len, uint8_t write_value) { - SPI_EXCHANGE(self->spi_dev, NULL, data, len); - - return true; -} - -bool common_hal_busio_spi_transfer(busio_spi_obj_t *self, uint8_t *data_out, uint8_t *data_in, size_t len) { - SPI_EXCHANGE(self->spi_dev, data_out, data_in, len); - - return true; -} - -uint32_t common_hal_busio_spi_get_frequency(busio_spi_obj_t *self) { - return self->frequency; -} - -uint8_t common_hal_busio_spi_get_phase(busio_spi_obj_t* self) { - return self->phase; -} - -uint8_t common_hal_busio_spi_get_polarity(busio_spi_obj_t *self) { - return self->polarity; -} - -void common_hal_busio_spi_never_reset(busio_spi_obj_t *self) { - never_reset_pin_number(self->clock_pin->number); - never_reset_pin_number(self->mosi_pin->number); - never_reset_pin_number(self->miso_pin->number); -} diff --git a/ports/spresense/common-hal/busio/SPI.h b/ports/spresense/common-hal/busio/SPI.h deleted file mode 100644 index 90c2f6b81..000000000 --- a/ports/spresense/common-hal/busio/SPI.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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. - */ - -#ifndef MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_BUSIO_SPI_H -#define MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_BUSIO_SPI_H - -#include - -#include "py/obj.h" - -#include "common-hal/microcontroller/Pin.h" - -typedef struct { - mp_obj_base_t base; - struct spi_dev_s* spi_dev; - uint32_t frequency; - uint8_t phase; - uint8_t polarity; - uint8_t bits; - bool has_lock; - const mcu_pin_obj_t *clock_pin; - const mcu_pin_obj_t *mosi_pin; - const mcu_pin_obj_t *miso_pin; -} busio_spi_obj_t; - -#endif // MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_BUSIO_SPI_H diff --git a/ports/spresense/common-hal/busio/UART.c b/ports/spresense/common-hal/busio/UART.c deleted file mode 100644 index 2389cbf96..000000000 --- a/ports/spresense/common-hal/busio/UART.c +++ /dev/null @@ -1,168 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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 -#include - -#include -#include -#include -#include -#include -#include - -#include "py/mperrno.h" -#include "py/stream.h" -#include "py/runtime.h" - -#include "shared-bindings/busio/UART.h" - -void common_hal_busio_uart_construct(busio_uart_obj_t *self, - const mcu_pin_obj_t *tx, const mcu_pin_obj_t *rx, uint32_t baudrate, - uint8_t bits, uart_parity_t parity, uint8_t stop, mp_float_t timeout, - uint16_t receiver_buffer_size) { - struct termios tio; - - self->uart_fd = open("/dev/ttyS2", O_RDWR); - if (self->uart_fd < 0) { - mp_raise_ValueError(translate("Could not initialize UART")); - } - - ioctl(self->uart_fd, TCGETS, (long unsigned int)&tio); - tio.c_speed = baudrate; - ioctl(self->uart_fd, TCSETS, (long unsigned int)&tio); - ioctl(self->uart_fd, TCFLSH, (long unsigned int)NULL); - - if (bits != 8) { - mp_raise_ValueError(translate("Could not initialize UART")); - } - - if (parity != PARITY_NONE) { - mp_raise_ValueError(translate("Could not initialize UART")); - } - - if (stop != 1) { - mp_raise_ValueError(translate("Could not initialize UART")); - } - - if (tx->number != PIN_UART2_TXD || rx->number != PIN_UART2_RXD) { - mp_raise_ValueError(translate("Invalid pins")); - } - - claim_pin(tx); - claim_pin(rx); - - self->tx_pin = tx; - self->rx_pin = rx; - self->baudrate = baudrate; - self->timeout = timeout; -} - -void common_hal_busio_uart_deinit(busio_uart_obj_t *self) { - if (common_hal_busio_uart_deinited(self)) { - return; - } - - close(self->uart_fd); - self->uart_fd = -1; - - reset_pin_number(self->tx_pin->number); - reset_pin_number(self->rx_pin->number); -} - -bool common_hal_busio_uart_deinited(busio_uart_obj_t *self) { - return self->uart_fd < 0; -} - -size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t len, int *errcode) { - fd_set rfds; - struct timeval tv; - int retval, bytes_read; - - // make sure we want at least 1 char - if (len == 0) { - return 0; - } - - FD_ZERO(&rfds); - FD_SET(self->uart_fd, &rfds); - - tv.tv_sec = 0; - tv.tv_usec = self->timeout * 1000; - - retval = select(self->uart_fd + 1, &rfds, NULL, NULL, &tv); - - if (retval) { - bytes_read = read(self->uart_fd, data, len); - } else { - *errcode = EAGAIN; - return MP_STREAM_ERROR; - } - - return bytes_read; -} - -size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data, size_t len, int *errcode) { - int bytes_written = write(self->uart_fd, data, len); - - if (bytes_written < 0) { - *errcode = MP_EAGAIN; - return MP_STREAM_ERROR; - } - - return bytes_written; -} - -uint32_t common_hal_busio_uart_get_baudrate(busio_uart_obj_t *self) { - return self->baudrate; -} - -void common_hal_busio_uart_set_baudrate(busio_uart_obj_t *self, uint32_t baudrate) { - struct termios tio; - - ioctl(self->uart_fd, TCGETS, (long unsigned int)&tio); - tio.c_speed = baudrate; - ioctl(self->uart_fd, TCSETS, (long unsigned int)&tio); - ioctl(self->uart_fd, TCFLSH, (long unsigned int)NULL); -} - -uint32_t common_hal_busio_uart_rx_characters_available(busio_uart_obj_t *self) { - int count = 0; - - ioctl(self->uart_fd, FIONREAD, (long unsigned int)&count); - - return count; -} - -void common_hal_busio_uart_clear_rx_buffer(busio_uart_obj_t *self) { -} - -bool common_hal_busio_uart_ready_to_tx(busio_uart_obj_t *self) { - ioctl(self->uart_fd, TCFLSH, (long unsigned int)NULL); - return true; -} diff --git a/ports/spresense/common-hal/busio/UART.h b/ports/spresense/common-hal/busio/UART.h deleted file mode 100644 index 7cdbe4ff3..000000000 --- a/ports/spresense/common-hal/busio/UART.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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. - */ - -#ifndef MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_BUSIO_UART_H -#define MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_BUSIO_UART_H - -#include "py/obj.h" - -#include "common-hal/microcontroller/Pin.h" - -typedef struct { - mp_obj_base_t base; - int uart_fd; - const mcu_pin_obj_t *tx_pin; - const mcu_pin_obj_t *rx_pin; - uint32_t baudrate; - uint32_t timeout; -} busio_uart_obj_t; - -#endif // MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_BUSIO_UART_H diff --git a/ports/spresense/common-hal/busio/__init__.c b/ports/spresense/common-hal/busio/__init__.c deleted file mode 100644 index 41761b674..000000000 --- a/ports/spresense/common-hal/busio/__init__.c +++ /dev/null @@ -1 +0,0 @@ -// No busio module functions. diff --git a/ports/spresense/common-hal/digitalio/DigitalInOut.c b/ports/spresense/common-hal/digitalio/DigitalInOut.c deleted file mode 100644 index 7b1f6cc03..000000000 --- a/ports/spresense/common-hal/digitalio/DigitalInOut.c +++ /dev/null @@ -1,136 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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 "py/runtime.h" - -#include "shared-bindings/digitalio/DigitalInOut.h" - -digitalinout_result_t common_hal_digitalio_digitalinout_construct(digitalio_digitalinout_obj_t *self, const mcu_pin_obj_t *pin) { - if (pin->analog) { - mp_raise_ValueError(translate("DigitalInOut not supported on given pin")); - } - - claim_pin(pin); - - self->pin = pin; - self->input = true; - self->open_drain = false; - - board_gpio_write(self->pin->number, -1); - board_gpio_config(self->pin->number, 0, true, true, PIN_FLOAT); - - return DIGITALINOUT_OK; -} - -void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t *self) { - if (common_hal_digitalio_digitalinout_deinited(self)) { - return; - } - - board_gpio_write(self->pin->number, -1); - board_gpio_config(self->pin->number, 0, false, true, PIN_FLOAT); - - reset_pin_number(self->pin->number); - self->pin = mp_const_none; -} - -bool common_hal_digitalio_digitalinout_deinited(digitalio_digitalinout_obj_t *self) { - return self->pin == mp_const_none; -} - -void common_hal_digitalio_digitalinout_switch_to_input(digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) { - self->input = true; - self->pull = pull; - board_gpio_write(self->pin->number, -1); - board_gpio_config(self->pin->number, 0, true, true, pull); -} - -void common_hal_digitalio_digitalinout_switch_to_output(digitalio_digitalinout_obj_t *self, bool value, digitalio_drive_mode_t drive_mode) { - self->input = false; - self->open_drain = drive_mode == DRIVE_MODE_OPEN_DRAIN; - board_gpio_write(self->pin->number, -1); - board_gpio_config(self->pin->number, 0, false, true, PIN_FLOAT); - - if (self->open_drain) { - board_gpio_write(self->pin->number, 0); - } - common_hal_digitalio_digitalinout_set_value(self, value); -} - -digitalio_direction_t common_hal_digitalio_digitalinout_get_direction(digitalio_digitalinout_obj_t *self) { - return self->input ? DIRECTION_INPUT : DIRECTION_OUTPUT; -} - -void common_hal_digitalio_digitalinout_set_value(digitalio_digitalinout_obj_t *self, bool value) { - if (self->open_drain) { - if (value) { - board_gpio_write(self->pin->number, -1); - board_gpio_config(self->pin->number, 0, true, true, PIN_PULLUP); - } else { - board_gpio_config(self->pin->number, 0, false, true, PIN_FLOAT); - board_gpio_write(self->pin->number, 0); - } - } else { - board_gpio_write(self->pin->number, value); - } -} - -bool common_hal_digitalio_digitalinout_get_value(digitalio_digitalinout_obj_t *self) { - return board_gpio_read(self->pin->number); -} - -void common_hal_digitalio_digitalinout_set_drive_mode(digitalio_digitalinout_obj_t *self, digitalio_drive_mode_t drive_mode) { - if (drive_mode == DRIVE_MODE_PUSH_PULL) { - board_gpio_write(self->pin->number, -1); - board_gpio_config(self->pin->number, 0, false, true, PIN_FLOAT); - self->open_drain = false; - } else { - board_gpio_write(self->pin->number, -1); - board_gpio_config(self->pin->number, 0, false, true, PIN_FLOAT); - board_gpio_write(self->pin->number, 0); - self->open_drain = true; - } -} - -digitalio_drive_mode_t common_hal_digitalio_digitalinout_get_drive_mode(digitalio_digitalinout_obj_t *self) { - return self->open_drain ? DRIVE_MODE_OPEN_DRAIN : DRIVE_MODE_PUSH_PULL; -} - -void common_hal_digitalio_digitalinout_set_pull(digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) { - self->pull = pull; - board_gpio_write(self->pin->number, -1); - board_gpio_config(self->pin->number, 0, true, true, pull); -} - -digitalio_pull_t common_hal_digitalio_digitalinout_get_pull(digitalio_digitalinout_obj_t *self) { - return self->pull; -} - -void common_hal_digitalio_digitalinout_never_reset(digitalio_digitalinout_obj_t *self) { - never_reset_pin_number(self->pin->number); -} diff --git a/ports/spresense/common-hal/digitalio/DigitalInOut.h b/ports/spresense/common-hal/digitalio/DigitalInOut.h deleted file mode 100644 index a5bfd22bd..000000000 --- a/ports/spresense/common-hal/digitalio/DigitalInOut.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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. - */ - -#ifndef MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_DIGITALIO_DIGITALINOUT_H -#define MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_DIGITALIO_DIGITALINOUT_H - -#include "py/obj.h" - -#include "common-hal/microcontroller/Pin.h" - -typedef struct { - mp_obj_base_t base; - const mcu_pin_obj_t *pin; - bool input; - bool open_drain; - uint8_t pull; -} digitalio_digitalinout_obj_t; - -#endif // MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_DIGITALIO_DIGITALINOUT_H diff --git a/ports/spresense/common-hal/digitalio/__init__.c b/ports/spresense/common-hal/digitalio/__init__.c deleted file mode 100644 index 20fad4595..000000000 --- a/ports/spresense/common-hal/digitalio/__init__.c +++ /dev/null @@ -1 +0,0 @@ -// No digitalio module functions. diff --git a/ports/spresense/common-hal/microcontroller/Pin.c b/ports/spresense/common-hal/microcontroller/Pin.c deleted file mode 100644 index 005defa84..000000000 --- a/ports/spresense/common-hal/microcontroller/Pin.c +++ /dev/null @@ -1,162 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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 -#include -#include - -#include "shared-bindings/microcontroller/Pin.h" - -typedef struct { - const mcu_pin_obj_t *pin; - bool reset; - bool free; -} pin_status_t; - -const mcu_pin_obj_t pin_D0 = PIN(PIN_UART2_RXD, false); -const mcu_pin_obj_t pin_D1 = PIN(PIN_UART2_TXD, false); -const mcu_pin_obj_t pin_D2 = PIN(PIN_HIF_IRQ_OUT, false); -const mcu_pin_obj_t pin_D3 = PIN(PIN_PWM3, false); -const mcu_pin_obj_t pin_D4 = PIN(PIN_SPI2_MOSI, false); -const mcu_pin_obj_t pin_D5 = PIN(PIN_PWM1, false); -const mcu_pin_obj_t pin_D6 = PIN(PIN_PWM0, false); -const mcu_pin_obj_t pin_D7 = PIN(PIN_SPI3_CS1_X, false); -const mcu_pin_obj_t pin_D8 = PIN(PIN_SPI2_MISO, false); -const mcu_pin_obj_t pin_D9 = PIN(PIN_PWM2, false); -const mcu_pin_obj_t pin_D10 = PIN(PIN_SPI4_CS_X, false); -const mcu_pin_obj_t pin_D11 = PIN(PIN_SPI4_MOSI, false); -const mcu_pin_obj_t pin_D12 = PIN(PIN_SPI4_MISO, false); -const mcu_pin_obj_t pin_D13 = PIN(PIN_SPI4_SCK, false); -const mcu_pin_obj_t pin_D14 = PIN(PIN_I2C0_BDT, false); -const mcu_pin_obj_t pin_D15 = PIN(PIN_I2C0_BCK, false); -const mcu_pin_obj_t pin_D16 = PIN(PIN_EMMC_DATA0, false); -const mcu_pin_obj_t pin_D17 = PIN(PIN_EMMC_DATA1, false); -const mcu_pin_obj_t pin_D18 = PIN(PIN_I2S0_DATA_OUT, false); -const mcu_pin_obj_t pin_D19 = PIN(PIN_I2S0_DATA_IN, false); -const mcu_pin_obj_t pin_D20 = PIN(PIN_EMMC_DATA2, false); -const mcu_pin_obj_t pin_D21 = PIN(PIN_EMMC_DATA3, false); -const mcu_pin_obj_t pin_D22 = PIN(PIN_SEN_IRQ_IN, false); -const mcu_pin_obj_t pin_D23 = PIN(PIN_EMMC_CLK, false); -const mcu_pin_obj_t pin_D24 = PIN(PIN_EMMC_CMD, false); -const mcu_pin_obj_t pin_D25 = PIN(PIN_I2S0_LRCK, false); -const mcu_pin_obj_t pin_D26 = PIN(PIN_I2S0_BCK, false); -const mcu_pin_obj_t pin_D27 = PIN(PIN_UART2_CTS, false); -const mcu_pin_obj_t pin_D28 = PIN(PIN_UART2_RTS, false); -const mcu_pin_obj_t pin_LED0 = PIN(PIN_I2S1_BCK, false); -const mcu_pin_obj_t pin_LED1 = PIN(PIN_I2S1_LRCK, false); -const mcu_pin_obj_t pin_LED2 = PIN(PIN_I2S1_DATA_IN, false); -const mcu_pin_obj_t pin_LED3 = PIN(PIN_I2S1_DATA_OUT, false); -const mcu_pin_obj_t pin_A0 = PIN(0, true); -const mcu_pin_obj_t pin_A1 = PIN(1, true); -const mcu_pin_obj_t pin_A2 = PIN(2, true); -const mcu_pin_obj_t pin_A3 = PIN(3, true); -const mcu_pin_obj_t pin_A4 = PIN(4, true); -const mcu_pin_obj_t pin_A5 = PIN(5, true); - -STATIC pin_status_t pins[] = { - { &pin_D0, true, true }, - { &pin_D1, true, true }, - { &pin_D2, true, true }, - { &pin_D3, true, true }, - { &pin_D4, true, true }, - { &pin_D5, true, true }, - { &pin_D6, true, true }, - { &pin_D7, true, true }, - { &pin_D8, true, true }, - { &pin_D9, true, true }, - { &pin_D10, true, true }, - { &pin_D11, true, true }, - { &pin_D12, true, true }, - { &pin_D13, true, true }, - { &pin_D14, true, true }, - { &pin_D15, true, true }, - { &pin_D16, true, true }, - { &pin_D17, true, true }, - { &pin_D18, true, true }, - { &pin_D19, true, true }, - { &pin_D20, true, true }, - { &pin_D21, true, true }, - { &pin_D22, true, true }, - { &pin_D23, true, true }, - { &pin_D24, true, true }, - { &pin_D25, true, true }, - { &pin_D26, true, true }, - { &pin_D27, true, true }, - { &pin_D28, true, true }, - { &pin_LED0, true, true }, - { &pin_LED1, true, true }, - { &pin_LED2, true, true }, - { &pin_LED3, true, true }, -}; - -bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t *pin) { - for (int i = 0; i < MP_ARRAY_SIZE(pins); i++) { - if (pins[i].pin->number == pin->number) { - return pins[i].free; - } - } - - return true; -} - -void never_reset_pin_number(uint8_t pin_number) { - for (int i = 0; i < MP_ARRAY_SIZE(pins); i++) { - if (pins[i].pin->number == pin_number) { - pins[i].reset = false; - } - } -} - -void reset_pin_number(uint8_t pin_number) { - for (int i = 0; i < MP_ARRAY_SIZE(pins); i++) { - if (pins[i].pin->number == pin_number) { - pins[i].free = true; - } - } -} - -void reset_all_pins(void) { - for (int i = 0; i < MP_ARRAY_SIZE(pins); i++) { - if (!pins[i].free && pins[i].reset) { - board_gpio_write(pins[i].pin->number, -1); - board_gpio_config(pins[i].pin->number, 0, false, true, PIN_FLOAT); - board_gpio_int(pins[i].pin->number, false); - board_gpio_intconfig(pins[i].pin->number, 0, false, NULL); - pins[i].free = true; - } - } -} - -void claim_pin(const mcu_pin_obj_t *pin) { - for (int i = 0; i < MP_ARRAY_SIZE(pins); i++) { - if (pins[i].pin->number == pin->number) { - pins[i].free = false; - } - } -} diff --git a/ports/spresense/common-hal/microcontroller/Pin.h b/ports/spresense/common-hal/microcontroller/Pin.h deleted file mode 100644 index 2d995d0dd..000000000 --- a/ports/spresense/common-hal/microcontroller/Pin.h +++ /dev/null @@ -1,92 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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. - */ - -#ifndef MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_MICROCONTROLLER_PIN_H -#define MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_MICROCONTROLLER_PIN_H - -#include "py/obj.h" - -extern const mp_obj_type_t mcu_pin_type; - -#define PIN(pin, a) \ -{ \ - { &mcu_pin_type }, \ - .number = (pin), \ - .analog = (a) \ -} - -typedef struct { - mp_obj_base_t base; - uint8_t number; - bool analog; -} mcu_pin_obj_t; - -extern const mcu_pin_obj_t pin_D0; -extern const mcu_pin_obj_t pin_D1; -extern const mcu_pin_obj_t pin_D2; -extern const mcu_pin_obj_t pin_D3; -extern const mcu_pin_obj_t pin_D4; -extern const mcu_pin_obj_t pin_D5; -extern const mcu_pin_obj_t pin_D6; -extern const mcu_pin_obj_t pin_D7; -extern const mcu_pin_obj_t pin_D8; -extern const mcu_pin_obj_t pin_D9; -extern const mcu_pin_obj_t pin_D10; -extern const mcu_pin_obj_t pin_D11; -extern const mcu_pin_obj_t pin_D12; -extern const mcu_pin_obj_t pin_D13; -extern const mcu_pin_obj_t pin_D14; -extern const mcu_pin_obj_t pin_D15; -extern const mcu_pin_obj_t pin_D16; -extern const mcu_pin_obj_t pin_D17; -extern const mcu_pin_obj_t pin_D18; -extern const mcu_pin_obj_t pin_D19; -extern const mcu_pin_obj_t pin_D20; -extern const mcu_pin_obj_t pin_D21; -extern const mcu_pin_obj_t pin_D22; -extern const mcu_pin_obj_t pin_D23; -extern const mcu_pin_obj_t pin_D24; -extern const mcu_pin_obj_t pin_D25; -extern const mcu_pin_obj_t pin_D26; -extern const mcu_pin_obj_t pin_D27; -extern const mcu_pin_obj_t pin_D28; -extern const mcu_pin_obj_t pin_LED0; -extern const mcu_pin_obj_t pin_LED1; -extern const mcu_pin_obj_t pin_LED2; -extern const mcu_pin_obj_t pin_LED3; -extern const mcu_pin_obj_t pin_A0; -extern const mcu_pin_obj_t pin_A1; -extern const mcu_pin_obj_t pin_A2; -extern const mcu_pin_obj_t pin_A3; -extern const mcu_pin_obj_t pin_A4; -extern const mcu_pin_obj_t pin_A5; - -void never_reset_pin_number(uint8_t pin_number); -void reset_pin_number(uint8_t pin_number); -void reset_all_pins(void); -void claim_pin(const mcu_pin_obj_t* pin); - -#endif // MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_MICROCONTROLLER_PIN_H diff --git a/ports/spresense/common-hal/microcontroller/Processor.c b/ports/spresense/common-hal/microcontroller/Processor.c deleted file mode 100644 index 355ee01df..000000000 --- a/ports/spresense/common-hal/microcontroller/Processor.c +++ /dev/null @@ -1,41 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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 "py/mphal.h" - -uint32_t common_hal_mcu_processor_get_frequency(void) { - return mp_hal_ticks_cpu(); -} - -float common_hal_mcu_processor_get_temperature(void) { - return 0; -} - -void common_hal_mcu_processor_get_uid(uint8_t raw_id[]) { - boardctl(BOARDIOC_UNIQUEID, (uintptr_t) raw_id); -} diff --git a/ports/spresense/common-hal/microcontroller/Processor.h b/ports/spresense/common-hal/microcontroller/Processor.h deleted file mode 100644 index 6c4c17b03..000000000 --- a/ports/spresense/common-hal/microcontroller/Processor.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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. - */ - -#ifndef MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_MICROCONTROLLER_PROCESSOR_H -#define MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_MICROCONTROLLER_PROCESSOR_H - -#define COMMON_HAL_MCU_PROCESSOR_UID_LENGTH CONFIG_BOARDCTL_UNIQUEID_SIZE - -#include "py/obj.h" - -typedef struct { - mp_obj_base_t base; -} mcu_processor_obj_t; - -const mp_obj_type_t mcu_processor_type; - -#endif // MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_MICROCONTROLLER_PROCESSOR_H diff --git a/ports/spresense/common-hal/microcontroller/__init__.c b/ports/spresense/common-hal/microcontroller/__init__.c deleted file mode 100644 index 576896eb2..000000000 --- a/ports/spresense/common-hal/microcontroller/__init__.c +++ /dev/null @@ -1,111 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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 "py/mphal.h" -#include "py/runtime.h" - -#include "shared-bindings/microcontroller/__init__.h" -#include "common-hal/microcontroller/Pin.h" -#include "supervisor/filesystem.h" -#include "supervisor/shared/safe_mode.h" - -// The singleton microcontroller.Processor object, bound to microcontroller.cpu -// It currently only has properties, and no state. -const mcu_processor_obj_t common_hal_mcu_processor_obj = { - .base = { - .type = &mcu_processor_type, - }, -}; - -void common_hal_mcu_delay_us(uint32_t delay) { - mp_hal_delay_us(delay); -} - -void common_hal_mcu_disable_interrupts(void) { - __asm volatile ("cpsid i" : : : "memory"); -} - -void common_hal_mcu_enable_interrupts(void) { - __asm volatile ("cpsie i" : : : "memory"); -} - -void common_hal_mcu_on_next_reset(mcu_runmode_t runmode) { - if(runmode == RUNMODE_BOOTLOADER) { - mp_raise_ValueError(translate("Cannot reset into bootloader because no bootloader is present.")); - } else if(runmode == RUNMODE_SAFE_MODE) { - safe_mode_on_next_reset(PROGRAMMATIC_SAFE_MODE); - } -} - -void common_hal_mcu_reset(void) { - filesystem_flush(); - boardctl(BOARDIOC_RESET, 0); -} - -STATIC const mp_rom_map_elem_t mcu_pin_globals_table[] = { - { MP_ROM_QSTR(MP_QSTR_D0), MP_ROM_PTR(&pin_D0) }, - { MP_ROM_QSTR(MP_QSTR_D1), MP_ROM_PTR(&pin_D1) }, - { MP_ROM_QSTR(MP_QSTR_D2), MP_ROM_PTR(&pin_D2) }, - { MP_ROM_QSTR(MP_QSTR_D3), MP_ROM_PTR(&pin_D3) }, - { MP_ROM_QSTR(MP_QSTR_D4), MP_ROM_PTR(&pin_D4) }, - { MP_ROM_QSTR(MP_QSTR_D5), MP_ROM_PTR(&pin_D5) }, - { MP_ROM_QSTR(MP_QSTR_D6), MP_ROM_PTR(&pin_D6) }, - { MP_ROM_QSTR(MP_QSTR_D7), MP_ROM_PTR(&pin_D7) }, - { MP_ROM_QSTR(MP_QSTR_D8), MP_ROM_PTR(&pin_D8) }, - { MP_ROM_QSTR(MP_QSTR_D9), MP_ROM_PTR(&pin_D9) }, - { MP_ROM_QSTR(MP_QSTR_D10), MP_ROM_PTR(&pin_D10) }, - { MP_ROM_QSTR(MP_QSTR_D11), MP_ROM_PTR(&pin_D11) }, - { MP_ROM_QSTR(MP_QSTR_D12), MP_ROM_PTR(&pin_D12) }, - { MP_ROM_QSTR(MP_QSTR_D13), MP_ROM_PTR(&pin_D13) }, - { MP_ROM_QSTR(MP_QSTR_D14), MP_ROM_PTR(&pin_D14) }, - { MP_ROM_QSTR(MP_QSTR_D15), MP_ROM_PTR(&pin_D15) }, - { MP_ROM_QSTR(MP_QSTR_D16), MP_ROM_PTR(&pin_D16) }, - { MP_ROM_QSTR(MP_QSTR_D17), MP_ROM_PTR(&pin_D17) }, - { MP_ROM_QSTR(MP_QSTR_D18), MP_ROM_PTR(&pin_D18) }, - { MP_ROM_QSTR(MP_QSTR_D19), MP_ROM_PTR(&pin_D19) }, - { MP_ROM_QSTR(MP_QSTR_D20), MP_ROM_PTR(&pin_D20) }, - { MP_ROM_QSTR(MP_QSTR_D21), MP_ROM_PTR(&pin_D21) }, - { MP_ROM_QSTR(MP_QSTR_D22), MP_ROM_PTR(&pin_D22) }, - { MP_ROM_QSTR(MP_QSTR_D23), MP_ROM_PTR(&pin_D23) }, - { MP_ROM_QSTR(MP_QSTR_D24), MP_ROM_PTR(&pin_D24) }, - { MP_ROM_QSTR(MP_QSTR_D25), MP_ROM_PTR(&pin_D25) }, - { MP_ROM_QSTR(MP_QSTR_D26), MP_ROM_PTR(&pin_D26) }, - { MP_ROM_QSTR(MP_QSTR_D27), MP_ROM_PTR(&pin_D27) }, - { MP_ROM_QSTR(MP_QSTR_D28), MP_ROM_PTR(&pin_D28) }, - { MP_ROM_QSTR(MP_QSTR_LED0), MP_ROM_PTR(&pin_LED0) }, - { MP_ROM_QSTR(MP_QSTR_LED1), MP_ROM_PTR(&pin_LED1) }, - { MP_ROM_QSTR(MP_QSTR_LED2), MP_ROM_PTR(&pin_LED2) }, - { MP_ROM_QSTR(MP_QSTR_LED3), MP_ROM_PTR(&pin_LED3) }, - { MP_ROM_QSTR(MP_QSTR_A0), MP_ROM_PTR(&pin_A0) }, - { MP_ROM_QSTR(MP_QSTR_A1), MP_ROM_PTR(&pin_A1) }, - { MP_ROM_QSTR(MP_QSTR_A2), MP_ROM_PTR(&pin_A2) }, - { MP_ROM_QSTR(MP_QSTR_A3), MP_ROM_PTR(&pin_A3) }, - { MP_ROM_QSTR(MP_QSTR_A4), MP_ROM_PTR(&pin_A4) }, - { MP_ROM_QSTR(MP_QSTR_A5), MP_ROM_PTR(&pin_A5) }, -}; -MP_DEFINE_CONST_DICT(mcu_pin_globals, mcu_pin_globals_table); diff --git a/ports/spresense/common-hal/os/__init__.c b/ports/spresense/common-hal/os/__init__.c deleted file mode 100644 index d4b0e23be..000000000 --- a/ports/spresense/common-hal/os/__init__.c +++ /dev/null @@ -1,72 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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 "genhdr/mpversion.h" -#include "py/objstr.h" -#include "py/objtuple.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, "spresense"); -STATIC const MP_DEFINE_STR_OBJ(os_uname_info_nodename_obj, "spresense"); -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); -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 -); - -mp_obj_t common_hal_os_uname(void) { - return (mp_obj_t)&os_uname_info_obj; -} - -bool common_hal_os_urandom(uint8_t* buffer, mp_uint_t length) { - uint32_t i = 0; - - while (i < length) { - uint32_t new_random = rand(); - for (int j = 0; j < 4 && i < length; j++) { - buffer[i] = new_random & 0xff; - i++; - new_random >>= 8; - } - } - - return true; -} diff --git a/ports/spresense/common-hal/pulseio/PWMOut.c b/ports/spresense/common-hal/pulseio/PWMOut.c deleted file mode 100644 index fae49aed3..000000000 --- a/ports/spresense/common-hal/pulseio/PWMOut.c +++ /dev/null @@ -1,157 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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 - -#include "py/runtime.h" - -#include "shared-bindings/pulseio/PWMOut.h" - -typedef struct { - const char* devpath; - const mcu_pin_obj_t *pin; - int fd; - bool reset; -} pwmout_dev_t; - -STATIC pwmout_dev_t pwmout_dev[] = { - {"/dev/pwm0", &pin_D6, -1, true}, - {"/dev/pwm1", &pin_D5, -1, true}, - {"/dev/pwm2", &pin_D9, -1, true}, - {"/dev/pwm3", &pin_D3, -1, true} -}; - -pwmout_result_t common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t *self, - const mcu_pin_obj_t *pin, uint16_t duty, uint32_t frequency, - bool variable_frequency) { - self->number = -1; - - for (int i = 0; i < MP_ARRAY_SIZE(pwmout_dev); i++) { - if (pin->number == pwmout_dev[i].pin->number) { - self->number = i; - break; - } - } - - if (self->number < 0) { - return PWMOUT_INVALID_PIN; - } - - pwmout_dev[self->number].fd = open(pwmout_dev[self->number].devpath, O_RDONLY); - if (pwmout_dev[self->number].fd < 0) { - return PWMOUT_INVALID_PIN; - } - - self->info.frequency = frequency; - self->info.duty = duty; - self->variable_frequency = variable_frequency; - - if (ioctl(pwmout_dev[self->number].fd, PWMIOC_SETCHARACTERISTICS, (unsigned long)((uintptr_t)&self->info)) < 0) { - mp_raise_ValueError(translate("Invalid PWM frequency")); - } - ioctl(pwmout_dev[self->number].fd, PWMIOC_START, 0); - - claim_pin(pin); - - self->pin = pin; - - return PWMOUT_OK; -} - -void common_hal_pulseio_pwmout_deinit(pulseio_pwmout_obj_t *self) { - if (common_hal_pulseio_pwmout_deinited(self)) { - return; - } - - ioctl(pwmout_dev[self->number].fd, PWMIOC_STOP, 0); - close(pwmout_dev[self->number].fd); - pwmout_dev[self->number].fd = -1; - - reset_pin_number(self->pin->number); - self->pin = mp_const_none; -} - -bool common_hal_pulseio_pwmout_deinited(pulseio_pwmout_obj_t *self) { - return self->pin == mp_const_none; -} - -void common_hal_pulseio_pwmout_set_duty_cycle(pulseio_pwmout_obj_t *self, uint16_t duty) { - self->info.duty = duty; - - ioctl(pwmout_dev[self->number].fd, PWMIOC_SETCHARACTERISTICS, (unsigned long)((uintptr_t)&self->info)); -} - -uint16_t common_hal_pulseio_pwmout_get_duty_cycle(pulseio_pwmout_obj_t *self) { - return self->info.duty; -} - -void common_hal_pulseio_pwmout_set_frequency(pulseio_pwmout_obj_t *self, uint32_t frequency) { - self->info.frequency = frequency; - - if (ioctl(pwmout_dev[self->number].fd, PWMIOC_SETCHARACTERISTICS, (unsigned long)((uintptr_t)&self->info)) < 0) { - mp_raise_ValueError(translate("Invalid PWM frequency")); - } -} - -uint32_t common_hal_pulseio_pwmout_get_frequency(pulseio_pwmout_obj_t *self) { - return self->info.frequency; -} - -bool common_hal_pulseio_pwmout_get_variable_frequency(pulseio_pwmout_obj_t *self) { - return self->variable_frequency; -} - -void common_hal_pulseio_pwmout_never_reset(pulseio_pwmout_obj_t *self) { - never_reset_pin_number(self->pin->number); - - pwmout_dev[self->number].reset = false; -} - -void common_hal_pulseio_pwmout_reset_ok(pulseio_pwmout_obj_t *self) { - pwmout_dev[self->number].reset = true; -} - -void pwmout_reset(void) { - for (int i = 0; i < MP_ARRAY_SIZE(pwmout_dev); i++) { - if (pwmout_dev[i].fd >= 0 && pwmout_dev[i].reset) { - ioctl(pwmout_dev[i].fd, PWMIOC_STOP, 0); - close(pwmout_dev[i].fd); - pwmout_dev[i].fd = -1; - - reset_pin_number(pwmout_dev[i].pin->number); - } - } -} - -void pwmout_start(uint8_t pwm_num) { - ioctl(pwmout_dev[pwm_num].fd, PWMIOC_START, 0); -} - -void pwmout_stop(uint8_t pwm_num) { - ioctl(pwmout_dev[pwm_num].fd, PWMIOC_STOP, 0); -} diff --git a/ports/spresense/common-hal/pulseio/PWMOut.h b/ports/spresense/common-hal/pulseio/PWMOut.h deleted file mode 100644 index 486d5add9..000000000 --- a/ports/spresense/common-hal/pulseio/PWMOut.h +++ /dev/null @@ -1,48 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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. - */ - -#ifndef MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_PULSEIO_PWMOUT_H -#define MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_PULSEIO_PWMOUT_H - -#include - -#include "common-hal/microcontroller/Pin.h" - -#include "py/obj.h" - -typedef struct { - mp_obj_base_t base; - const mcu_pin_obj_t *pin; - struct pwm_info_s info; - bool variable_frequency; - uint8_t number; -} pulseio_pwmout_obj_t; - -void pwmout_reset(void); -void pwmout_start(uint8_t pwm_num); -void pwmout_stop(uint8_t pwm_num); - -#endif // MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_PULSEIO_PWMOUT_H diff --git a/ports/spresense/common-hal/pulseio/PulseIn.c b/ports/spresense/common-hal/pulseio/PulseIn.c deleted file mode 100644 index dd2773d1d..000000000 --- a/ports/spresense/common-hal/pulseio/PulseIn.c +++ /dev/null @@ -1,196 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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 "py/runtime.h" -#include "py/mphal.h" - -#include "shared-bindings/pulseio/PulseIn.h" -#include "shared-bindings/microcontroller/__init__.h" - -static pulseio_pulsein_obj_t *pulsein_objects[12]; - -static int pulsein_interrupt_handler(int irq, FAR void *context, FAR void *arg); - -static int pulsein_set_config(pulseio_pulsein_obj_t *self, bool first_edge) { - int mode; - - if (!first_edge) { - mode = INT_BOTH_EDGE; - } else if (self->idle_state) { - mode = INT_FALLING_EDGE; - } else { - mode = INT_RISING_EDGE; - } - return board_gpio_intconfig(self->pin->number, mode, false, pulsein_interrupt_handler); -} - -static int pulsein_interrupt_handler(int irq, FAR void *context, FAR void *arg) { - // Grab the current time first. - uint32_t current_us = mp_hal_ticks_us(); - - pulseio_pulsein_obj_t *self = pulsein_objects[irq - CXD56_IRQ_EXDEVICE_0]; - - if (self->first_edge) { - self->first_edge = false; - pulsein_set_config(self, false); - board_gpio_int(self->pin->number, true); - } else { - uint32_t us_diff = current_us - self->last_us; - - uint16_t duration = 0xffff; - if (us_diff < duration) { - duration = us_diff; - } - - uint16_t i = (self->start + self->len) % self->maxlen; - self->buffer[i] = duration; - if (self->len < self->maxlen) { - self->len++; - } else { - self->start++; - } - } - self->last_us = current_us; - - return 0; -} - -void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self, - const mcu_pin_obj_t *pin, uint16_t maxlen, bool idle_state) { - self->buffer = (uint16_t *) m_malloc(maxlen * sizeof(uint16_t), false); - if (self->buffer == NULL) { - mp_raise_msg_varg(&mp_type_MemoryError, translate("Failed to allocate RX buffer of %d bytes"), maxlen * sizeof(uint16_t)); - } - - self->pin = pin; - self->maxlen = maxlen; - self->idle_state = idle_state; - self->start = 0; - self->len = 0; - self->first_edge = true; - self->paused = false; - - int irq = pulsein_set_config(self, true); - if (irq < 0) { - mp_raise_RuntimeError(translate("EXTINT channel already in use")); - } else { - pulsein_objects[irq - CXD56_IRQ_EXDEVICE_0] = self; - } - - claim_pin(pin); - - board_gpio_int(self->pin->number, true); -} - -void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t *self) { - if (common_hal_pulseio_pulsein_deinited(self)) { - return; - } - - board_gpio_int(self->pin->number, false); - board_gpio_intconfig(self->pin->number, 0, false, NULL); - - reset_pin_number(self->pin->number); - self->pin = mp_const_none; -} - -bool common_hal_pulseio_pulsein_deinited(pulseio_pulsein_obj_t *self) { - return self->pin == mp_const_none; -} - -void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t *self) { - board_gpio_int(self->pin->number, false); - self->paused = true; -} - -void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t *self, uint16_t trigger_duration) { - // Make sure we're paused. - common_hal_pulseio_pulsein_pause(self); - - // Send the trigger pulse. - if (trigger_duration > 0) { - board_gpio_config(self->pin->number, 0, false, true, PIN_FLOAT); - board_gpio_write(self->pin->number, !self->idle_state); - common_hal_mcu_delay_us((uint32_t)trigger_duration); - board_gpio_write(self->pin->number, self->idle_state); - } - - // Reconfigure the pin and make sure its set to detect the first edge. - self->first_edge = true; - self->paused = false; - - pulsein_set_config(self, true); - board_gpio_int(self->pin->number, true); -} - -void common_hal_pulseio_pulsein_clear(pulseio_pulsein_obj_t *self) { - common_hal_mcu_disable_interrupts(); - self->start = 0; - self->len = 0; - common_hal_mcu_enable_interrupts(); -} - -uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t *self) { - if (self->len == 0) { - mp_raise_IndexError(translate("pop from an empty PulseIn")); - } - common_hal_mcu_disable_interrupts(); - uint16_t value = self->buffer[self->start]; - self->start = (self->start + 1) % self->maxlen; - self->len--; - common_hal_mcu_enable_interrupts(); - - return value; -} - -uint16_t common_hal_pulseio_pulsein_get_maxlen(pulseio_pulsein_obj_t *self) { - return self->maxlen; -} - -bool common_hal_pulseio_pulsein_get_paused(pulseio_pulsein_obj_t *self) { - return self->paused; -} - -uint16_t common_hal_pulseio_pulsein_get_len(pulseio_pulsein_obj_t *self) { - return self->len; -} - -uint16_t common_hal_pulseio_pulsein_get_item(pulseio_pulsein_obj_t *self, int16_t index) { - common_hal_mcu_disable_interrupts(); - if (index < 0) { - index += self->len; - } - if (index < 0 || index >= self->len) { - common_hal_mcu_enable_interrupts(); - mp_raise_IndexError(translate("index out of range")); - } - uint16_t value = self->buffer[(self->start + index) % self->maxlen]; - common_hal_mcu_enable_interrupts(); - return value; -} - \ No newline at end of file diff --git a/ports/spresense/common-hal/pulseio/PulseIn.h b/ports/spresense/common-hal/pulseio/PulseIn.h deleted file mode 100644 index 5f2f31811..000000000 --- a/ports/spresense/common-hal/pulseio/PulseIn.h +++ /dev/null @@ -1,47 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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. - */ - -#ifndef MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_PULSEIO_PULSEIN_H -#define MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_PULSEIO_PULSEIN_H - -#include "common-hal/microcontroller/Pin.h" - -#include "py/obj.h" - -typedef struct { - mp_obj_base_t base; - const mcu_pin_obj_t *pin; - uint16_t *buffer; - uint16_t maxlen; - uint16_t start; - uint16_t len; - uint32_t last_us; - bool idle_state; - bool first_edge; - bool paused; -} pulseio_pulsein_obj_t; - -#endif // MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_PULSEIO_PULSEIN_H diff --git a/ports/spresense/common-hal/pulseio/PulseOut.c b/ports/spresense/common-hal/pulseio/PulseOut.c deleted file mode 100644 index 5e1d5a2ed..000000000 --- a/ports/spresense/common-hal/pulseio/PulseOut.c +++ /dev/null @@ -1,126 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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 -#include - -#include "py/runtime.h" - -#include "shared-bindings/pulseio/PulseOut.h" - -static uint16_t *pulse_buffer = NULL; -static uint16_t pulse_index = 0; -static uint16_t pulse_length; -static int pulse_fd = -1; - -static bool pulseout_timer_handler(unsigned int *next_interval_us, void *arg) -{ - uint8_t pwm_num = (uint8_t)(int)arg; - pulse_index++; - - if (pulse_index >= pulse_length) { - return false; - } - - *next_interval_us = pulse_buffer[pulse_index] * 1000; - - if (pulse_index % 2 == 0) { - pwmout_start(pwm_num); - } else { - pwmout_stop(pwm_num); - } - - return true; -} - -void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t *self, - const pulseio_pwmout_obj_t *carrier) { - if (pulse_fd < 0) { - pulse_fd = open("/dev/timer0", O_RDONLY); - } - - if (pulse_fd < 0) { - mp_raise_RuntimeError(translate("All timers in use")); - } - - self->pwm_num = carrier->number; -} - -void common_hal_pulseio_pulseout_deinit(pulseio_pulseout_obj_t *self) { - if (common_hal_pulseio_pulseout_deinited(self)) { - return; - } - - ioctl(pulse_fd, TCIOC_STOP, 0); - close(pulse_fd); - pulse_fd = -1; - - pulse_buffer = NULL; -} - -bool common_hal_pulseio_pulseout_deinited(pulseio_pulseout_obj_t *self) { - return pulse_fd < 0; -} - -void common_hal_pulseio_pulseout_send(pulseio_pulseout_obj_t *self, uint16_t *pulses, uint16_t len) { - if (pulse_buffer != NULL) { - mp_raise_RuntimeError(translate("Another send is already active")); - } - - struct timer_sethandler_s sethandler; - - pulse_buffer = pulses; - pulse_index = 0; - pulse_length = len; - - unsigned long timeout = pulse_buffer[0] * 1000; - - ioctl(pulse_fd, TCIOC_SETTIMEOUT, timeout); - - sethandler.handler = pulseout_timer_handler; - sethandler.arg = (void *)(int)self->pwm_num; - - ioctl(pulse_fd, TCIOC_SETHANDLER, (unsigned long)&sethandler); - ioctl(pulse_fd, TCIOC_START, 0); - - while(pulse_index < len) { - // Do other things while we wait. The interrupts will handle sending the - // signal. - RUN_BACKGROUND_TASKS; - } - - pulse_buffer = NULL; -} - -void pulseout_reset(void) { - ioctl(pulse_fd, TCIOC_STOP, 0); - close(pulse_fd); - pulse_fd = -1; - - pulse_buffer = NULL; -} diff --git a/ports/spresense/common-hal/pulseio/PulseOut.h b/ports/spresense/common-hal/pulseio/PulseOut.h deleted file mode 100644 index 652a1197e..000000000 --- a/ports/spresense/common-hal/pulseio/PulseOut.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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. - */ - -#ifndef MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_PULSEIO_PULSEOUT_H -#define MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_PULSEIO_PULSEOUT_H - -#include "py/obj.h" - -typedef struct { - mp_obj_base_t base; - uint8_t pwm_num; -} pulseio_pulseout_obj_t; - -void pulseout_reset(void); - -#endif // MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_PULSEIO_PULSEOUT_H diff --git a/ports/spresense/common-hal/pulseio/__init__.c b/ports/spresense/common-hal/pulseio/__init__.c deleted file mode 100644 index 2bee925bc..000000000 --- a/ports/spresense/common-hal/pulseio/__init__.c +++ /dev/null @@ -1 +0,0 @@ -// No pulseio module functions. diff --git a/ports/spresense/common-hal/rtc/RTC.c b/ports/spresense/common-hal/rtc/RTC.c deleted file mode 100644 index ce65e6acd..000000000 --- a/ports/spresense/common-hal/rtc/RTC.c +++ /dev/null @@ -1,54 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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 "py/obj.h" -#include "py/runtime.h" - -#include "shared-bindings/rtc/RTC.h" - -void common_hal_rtc_get_time(timeutils_struct_time_t *tm) { - struct timeval tv; - - gettimeofday(&tv, NULL); - timeutils_seconds_since_2000_to_struct_time(tv.tv_sec, tm); -} - -void common_hal_rtc_set_time(timeutils_struct_time_t *tm) { - struct timeval tv = {0}; - - tv.tv_sec = timeutils_seconds_since_2000(tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec); - settimeofday(&tv, NULL); -} - -int common_hal_rtc_get_calibration(void) { - return 0; -} - -void common_hal_rtc_set_calibration(int calibration) { - mp_raise_NotImplementedError(translate("RTC calibration is not supported on this board")); -} diff --git a/ports/spresense/common-hal/rtc/RTC.h b/ports/spresense/common-hal/rtc/RTC.h deleted file mode 100644 index 7a3ef18bd..000000000 --- a/ports/spresense/common-hal/rtc/RTC.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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. - */ - -#ifndef MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_RTC_RTC_H -#define MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_RTC_RTC_H - -#endif // MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_RTC_RTC_H diff --git a/ports/spresense/common-hal/rtc/__init__.c b/ports/spresense/common-hal/rtc/__init__.c deleted file mode 100644 index 92d25d563..000000000 --- a/ports/spresense/common-hal/rtc/__init__.c +++ /dev/null @@ -1 +0,0 @@ -// No rtc module functions. diff --git a/ports/spresense/common-hal/supervisor/Runtime.c b/ports/spresense/common-hal/supervisor/Runtime.c deleted file mode 100755 index a0d9e70ab..000000000 --- a/ports/spresense/common-hal/supervisor/Runtime.c +++ /dev/null @@ -1,36 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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 "shared-bindings/supervisor/Runtime.h" -#include "supervisor/serial.h" - -bool common_hal_get_serial_connected(void) { - return (bool) serial_connected(); -} - -bool common_hal_get_serial_bytes_available(void) { - return (bool) serial_bytes_available(); -} diff --git a/ports/spresense/common-hal/supervisor/Runtime.h b/ports/spresense/common-hal/supervisor/Runtime.h deleted file mode 100755 index c3ed62a25..000000000 --- a/ports/spresense/common-hal/supervisor/Runtime.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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. - */ - -#ifndef MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_SUPERVISOR_RUNTIME_H -#define MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_SUPERVISOR_RUNTIME_H - -#include "py/obj.h" - -typedef struct { - mp_obj_base_t base; -} super_runtime_obj_t; - -#endif // MICROPY_INCLUDED_SPRESENSE_COMMON_HAL_SUPERVISOR_RUNTIME_H diff --git a/ports/spresense/common-hal/supervisor/__init__.c b/ports/spresense/common-hal/supervisor/__init__.c deleted file mode 100755 index e240525f2..000000000 --- a/ports/spresense/common-hal/supervisor/__init__.c +++ /dev/null @@ -1,36 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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 "shared-bindings/supervisor/__init__.h" -#include "shared-bindings/supervisor/Runtime.h" - -// The singleton supervisor.Runtime object, bound to supervisor.runtime -// It currently only has properties, and no state. -const super_runtime_obj_t common_hal_supervisor_runtime_obj = { - .base = { - .type = &supervisor_runtime_type, - }, -}; diff --git a/ports/spresense/common-hal/time/__init__.c b/ports/spresense/common-hal/time/__init__.c deleted file mode 100644 index 8f7326b62..000000000 --- a/ports/spresense/common-hal/time/__init__.c +++ /dev/null @@ -1,37 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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 "py/mphal.h" - -#include "tick.h" - -uint64_t common_hal_time_monotonic(void) { - return ticks_ms; -} - -void common_hal_time_delay_ms(uint32_t delay) { - mp_hal_delay_ms(delay); -} diff --git a/ports/spresense/fatfs_port.c b/ports/spresense/fatfs_port.c deleted file mode 100644 index e986b4c6e..000000000 --- a/ports/spresense/fatfs_port.c +++ /dev/null @@ -1,46 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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 "py/mphal.h" -#include "py/runtime.h" -#include "lib/oofatfs/ff.h" /* FatFs lower layer API */ -#include "lib/oofatfs/diskio.h" /* FatFs lower layer API */ -#include "lib/timeutils/timeutils.h" - -#if CIRCUITPY_RTC -#include "shared-bindings/rtc/RTC.h" -#endif - -DWORD get_fattime(void) { -#if CIRCUITPY_RTC - timeutils_struct_time_t tm; - common_hal_rtc_get_time(&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 >> 1); -#else - return ((2016 - 1980) << 25) | ((9) << 21) | ((1) << 16) | ((16) << 11) | ((43) << 5) | (35 / 2); -#endif -} diff --git a/ports/spresense/mpconfigboard.h b/ports/spresense/mpconfigboard.h deleted file mode 100644 index e0fef60f0..000000000 --- a/ports/spresense/mpconfigboard.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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. - */ - -#define MICROPY_HW_BOARD_NAME "SPRESENSE" -#define MICROPY_HW_MCU_NAME "CXD5602" -#define MICROPY_PY_SYS_PLATFORM "SPRESENSE" - -#define DEFAULT_I2C_BUS_SCL (&pin_D15) -#define DEFAULT_I2C_BUS_SDA (&pin_D14) - -#define DEFAULT_SPI_BUS_SCK (&pin_D13) -#define DEFAULT_SPI_BUS_MISO (&pin_D12) -#define DEFAULT_SPI_BUS_MOSI (&pin_D11) - -#define DEFAULT_UART_BUS_RX (&pin_D0) -#define DEFAULT_UART_BUS_TX (&pin_D1) diff --git a/ports/spresense/mpconfigport.h b/ports/spresense/mpconfigport.h deleted file mode 100644 index 233b7b9a7..000000000 --- a/ports/spresense/mpconfigport.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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. - */ - -#ifndef __INCLUDED_MPCONFIGPORT_H -#define __INCLUDED_MPCONFIGPORT_H - -// 24kiB stack -#define CIRCUITPY_DEFAULT_STACK_SIZE 0x6000 - -#include "py/circuitpy_mpconfig.h" - -#define MICROPY_PORT_ROOT_POINTERS \ - CIRCUITPY_COMMON_ROOT_POINTERS \ - -#endif // __INCLUDED_MPCONFIGPORT_H diff --git a/ports/spresense/mpconfigport.mk b/ports/spresense/mpconfigport.mk deleted file mode 100644 index 53dba2840..000000000 --- a/ports/spresense/mpconfigport.mk +++ /dev/null @@ -1,24 +0,0 @@ -USB_VID = 0x054c -USB_PID = 0x0bc2 -USB_PRODUCT = "Spresense" -USB_MANUFACTURER = "Sony" -USB_SERIAL_NUMBER_LENGTH = 10 -USB_DEVICES = "CDC,MSC" -USB_MSC_MAX_PACKET_SIZE = 512 -USB_RENUMBER_ENDPOINTS = 0 -USB_CDC_EP_NUM_NOTIFICATION = 3 -USB_CDC_EP_NUM_DATA_OUT = 2 -USB_CDC_EP_NUM_DATA_IN = 1 -USB_MSC_EP_NUM_OUT = 5 -USB_MSC_EP_NUM_IN = 4 - -CIRCUITPY_AUDIOIO = 0 -CIRCUITPY_AUDIOBUSIO = 0 -CIRCUITPY_I2CSLAVE = 0 -CIRCUITPY_ROTARYIO = 0 -CIRCUITPY_TOUCHIO = 0 -CIRCUITPY_GAMEPAD = 0 -CIRCUITPY_NEOPIXEL_WRITE = 0 -CIRCUITPY_NVM = 0 -CIRCUITPY_DISPLAYIO = 0 -CIRCUITPY_FREQUENCYIO = 0 diff --git a/ports/spresense/mphalport.c b/ports/spresense/mphalport.c deleted file mode 100644 index 79d93f975..000000000 --- a/ports/spresense/mphalport.c +++ /dev/null @@ -1,88 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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 -#include - -#include "py/mpstate.h" - -#include "tick.h" - -#define DELAY_CORRECTION (700) -#define DELAY_INTERVAL (50) - -void mp_hal_init(void) { - boardctl(BOARDIOC_INIT, 0); -} - -mp_uint_t mp_hal_ticks_ms(void) { - struct timeval tv; - gettimeofday(&tv, NULL); - return tv.tv_sec * 1000 + tv.tv_usec / 1000; -} - -mp_uint_t mp_hal_ticks_us(void) { - struct timeval tv; - gettimeofday(&tv, NULL); - return tv.tv_sec * 1000000 + tv.tv_usec; -} - -mp_uint_t mp_hal_ticks_cpu(void) { - return cxd56_get_cpu_baseclk(); -} - -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)) || - MP_STATE_VM(mp_pending_exception) == MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_reload_exception))) { - break; - } - duration = (ticks_ms - start_tick); - // TODO(tannewt): Go to sleep for a little while while we wait. - } -} - -void mp_hal_delay_us(uint32_t us) { - if (us) { - unsigned long long ticks = mp_hal_ticks_cpu() / 1000000L * us; - if (ticks < DELAY_CORRECTION) return; // delay time already used in calculation - - ticks -= DELAY_CORRECTION; - ticks /= 6; - // following loop takes 6 cycles - do { - __asm__ __volatile__("nop"); - } while(--ticks); - } -} diff --git a/ports/spresense/mphalport.h b/ports/spresense/mphalport.h deleted file mode 100644 index 8b397d41e..000000000 --- a/ports/spresense/mphalport.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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. - */ - -#ifndef MICROPY_INCLUDED_SPRESENSE_MPHALPORT_H -#define MICROPY_INCLUDED_SPRESENSE_MPHALPORT_H - -#include - -#include "lib/utils/interrupt_char.h" - -extern volatile uint64_t ticks_ms; - -#endif // MICROPY_INCLUDED_SPRESENSE_MPHALPORT_H diff --git a/ports/spresense/qstrdefsport.h b/ports/spresense/qstrdefsport.h deleted file mode 100644 index 3ba897069..000000000 --- a/ports/spresense/qstrdefsport.h +++ /dev/null @@ -1 +0,0 @@ -// qstrs specific to this port diff --git a/ports/spresense/spresense-exported-sdk b/ports/spresense/spresense-exported-sdk deleted file mode 160000 index b473b28a1..000000000 --- a/ports/spresense/spresense-exported-sdk +++ /dev/null @@ -1 +0,0 @@ -Subproject commit b473b28a14a03f3d416b6e2c071bcfd4fb92cb63 diff --git a/ports/spresense/supervisor/cpu.s b/ports/spresense/supervisor/cpu.s deleted file mode 100755 index 9e6807a5e..000000000 --- a/ports/spresense/supervisor/cpu.s +++ /dev/null @@ -1,27 +0,0 @@ -.syntax unified -.cpu cortex-m4 -.thumb -.text -.align 2 - -@ uint cpu_get_regs_and_sp(r0=uint regs[10]) -.global cpu_get_regs_and_sp -.thumb -.thumb_func -.type cpu_get_regs_and_sp, %function -cpu_get_regs_and_sp: -@ store registers into given array -str r4, [r0], #4 -str r5, [r0], #4 -str r6, [r0], #4 -str r7, [r0], #4 -str r8, [r0], #4 -str r9, [r0], #4 -str r10, [r0], #4 -str r11, [r0], #4 -str r12, [r0], #4 -str r13, [r0], #4 - -@ return the sp -mov r0, sp -bx lr diff --git a/ports/spresense/supervisor/internal_flash.c b/ports/spresense/supervisor/internal_flash.c deleted file mode 100644 index 0c9a61e06..000000000 --- a/ports/spresense/supervisor/internal_flash.c +++ /dev/null @@ -1,114 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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 "supervisor/flash.h" - -/* Prototypes for Remote API */ - -int FM_RawWrite(uint32_t offset, const void *buf, uint32_t size); -int FM_RawVerifyWrite(uint32_t offset, const void *buf, uint32_t size); -int FM_RawRead(uint32_t offset, void *buf, uint32_t size); -int FM_RawEraseSector(uint32_t sector); - -#define CXD56_SPIFLASHSIZE (16 * 1024 * 1024) - -#define SECTOR_SHIFT 12 -#define SECTOR_SIZE (1 << SECTOR_SHIFT) - -#define PAGE_SHIFT 9 -#define PAGE_SIZE (1 << PAGE_SHIFT) - -#define NO_SECTOR 0xffffffff - -uint8_t flash_cache[SECTOR_SIZE]; -uint32_t flash_sector = NO_SECTOR; - -void supervisor_flash_init(void) { -} - -uint32_t supervisor_flash_get_block_size(void) { - return FILESYSTEM_BLOCK_SIZE; -} - -uint32_t supervisor_flash_get_block_count(void) { - return CXD56_SPIFLASHSIZE >> PAGE_SHIFT; -} - -void supervisor_flash_flush(void) { - if (flash_sector == NO_SECTOR) { - return; - } - - FM_RawEraseSector(flash_sector); - FM_RawWrite(flash_sector << SECTOR_SHIFT, flash_cache, SECTOR_SIZE); - - flash_sector = NO_SECTOR; -} - -mp_uint_t supervisor_flash_read_blocks(uint8_t *dest, uint32_t block, uint32_t num_blocks) { - supervisor_flash_flush(); - - if (FM_RawRead(block << PAGE_SHIFT, dest, num_blocks << PAGE_SHIFT) < 0) { - return 1; - } - - return 0; // success -} - -mp_uint_t supervisor_flash_write_blocks(const uint8_t *src, uint32_t lba, uint32_t num_blocks) { - uint32_t sector; - uint32_t block_position; - uint32_t current_block = 0; - - while (num_blocks--) { - sector = (lba << PAGE_SHIFT) >> SECTOR_SHIFT; - block_position = lba - 8 * sector; - - if (sector != flash_sector) { - supervisor_flash_flush(); - - if (FM_RawRead(sector << SECTOR_SHIFT, flash_cache, SECTOR_SIZE) < 0) { - return 1; - } - - if (memcmp(&flash_cache[block_position << PAGE_SHIFT], &src[current_block << PAGE_SHIFT], PAGE_SIZE) != 0) { - flash_sector = sector; - } - } - - memcpy(&flash_cache[block_position << PAGE_SHIFT], &src[current_block << PAGE_SHIFT], PAGE_SIZE); - - lba++; - current_block++; - } - - return 0; // success -} - -void supervisor_flash_release_cache(void) { -} diff --git a/ports/spresense/supervisor/internal_flash.h b/ports/spresense/supervisor/internal_flash.h deleted file mode 100644 index bc4e98baf..000000000 --- a/ports/spresense/supervisor/internal_flash.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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. - */ - -#ifndef MICROPY_INCLUDED_SPRESENSE_INTERNAL_FLASH_H -#define MICROPY_INCLUDED_SPRESENSE_INTERNAL_FLASH_H - -#endif // MICROPY_INCLUDED_SPRESENSE_INTERNAL_FLASH_H diff --git a/ports/spresense/supervisor/internal_flash_root_pointers.h b/ports/spresense/supervisor/internal_flash_root_pointers.h deleted file mode 100644 index 87f478d13..000000000 --- a/ports/spresense/supervisor/internal_flash_root_pointers.h +++ /dev/null @@ -1,31 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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. - */ -#ifndef MICROPY_INCLUDED_SPRESENSE_INTERNAL_FLASH_ROOT_POINTERS_H -#define MICROPY_INCLUDED_SPRESENSE_INTERNAL_FLASH_ROOT_POINTERS_H - -#define FLASH_ROOT_POINTERS - -#endif // MICROPY_INCLUDED_SPRESENSE_INTERNAL_FLASH_ROOT_POINTERS_H diff --git a/ports/spresense/supervisor/port.c b/ports/spresense/supervisor/port.c deleted file mode 100644 index ab1ea5273..000000000 --- a/ports/spresense/supervisor/port.c +++ /dev/null @@ -1,73 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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 "supervisor/port.h" - -#include "common-hal/microcontroller/Pin.h" -#include "common-hal/analogio/AnalogIn.h" -#include "common-hal/pulseio/PulseOut.h" -#include "common-hal/pulseio/PWMOut.h" - -safe_mode_t port_init(void) { - boardctl(BOARDIOC_INIT, 0); - return NO_SAFE_MODE; -} - -void reset_cpu(void) { - boardctl(BOARDIOC_RESET, 0); -} - -void reset_port(void) { -#if CIRCUITPY_ANALOGIO - analogin_reset(); -#endif -#if CIRCUITPY_PULSEIO - pulseout_reset(); - pwmout_reset(); -#endif - - reset_all_pins(); -} - -void reset_board(void) { -} - -void reset_to_bootloader(void) { -} - -extern uint32_t _ebss; - -// Place the word to save just after our BSS section that gets blanked. -void port_set_saved_word(uint32_t value) { - _ebss = value; -} - -uint32_t port_get_saved_word(void) { - return _ebss; -} diff --git a/ports/spresense/supervisor/usb.c b/ports/spresense/supervisor/usb.c deleted file mode 100644 index 6ad253c6d..000000000 --- a/ports/spresense/supervisor/usb.c +++ /dev/null @@ -1,30 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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 "supervisor/usb.h" - -void init_usb_hardware(void) { -} diff --git a/ports/spresense/tick.c b/ports/spresense/tick.c deleted file mode 100644 index 6529db790..000000000 --- a/ports/spresense/tick.c +++ /dev/null @@ -1,45 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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 "tick.h" - -#include "supervisor/shared/autoreload.h" -#include "supervisor/filesystem.h" - -// Global millisecond tick count -volatile uint64_t ticks_ms = 0; - -void board_timerhook(void) -{ - ticks_ms += 1; - -#if CIRCUITPY_FILESYSTEM_FLUSH_INTERVAL_MS > 0 - filesystem_tick(); -#endif -#ifdef CIRCUITPY_AUTORELOAD_DELAY_MS - autoreload_tick(); -#endif -} diff --git a/ports/spresense/tick.h b/ports/spresense/tick.h deleted file mode 100644 index 7c4d5725f..000000000 --- a/ports/spresense/tick.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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. - */ - -#ifndef MICROPY_INCLUDED_SPRESENSE_TICK_H -#define MICROPY_INCLUDED_SPRESENSE_TICK_H - -#include "py/mpconfig.h" - -extern volatile uint64_t ticks_ms; - -#endif // MICROPY_INCLUDED_SPRESENSE_TICK_H -- cgit v1.2.3 From 5f09609d41349741b57b8a52415bad3a8662bf20 Mon Sep 17 00:00:00 2001 From: Kamil Tomaszewski Date: Fri, 11 Oct 2019 08:54:36 +0200 Subject: Add Spresense board folder --- ports/cxd56/Makefile | 18 ++++++ ports/cxd56/README.md | 6 +- ports/cxd56/boards/board.h | 45 +++++++++++++++ ports/cxd56/boards/spresense/board.c | 38 +++++++++++++ ports/cxd56/boards/spresense/mpconfigboard.h | 39 +++++++++++++ ports/cxd56/boards/spresense/mpconfigboard.mk | 4 ++ ports/cxd56/boards/spresense/pins.c | 80 ++++++++++++++++++++++++++ ports/cxd56/common-hal/board/__init__.c | 81 +-------------------------- ports/cxd56/mpconfigboard.h | 39 ------------- ports/cxd56/mpconfigport.mk | 4 -- ports/cxd56/supervisor/port.c | 12 +++- 11 files changed, 237 insertions(+), 129 deletions(-) create mode 100644 ports/cxd56/boards/board.h create mode 100644 ports/cxd56/boards/spresense/board.c create mode 100644 ports/cxd56/boards/spresense/mpconfigboard.h create mode 100644 ports/cxd56/boards/spresense/mpconfigboard.mk create mode 100644 ports/cxd56/boards/spresense/pins.c delete mode 100644 ports/cxd56/mpconfigboard.h diff --git a/ports/cxd56/Makefile b/ports/cxd56/Makefile index d8f809635..69c8e1bef 100644 --- a/ports/cxd56/Makefile +++ b/ports/cxd56/Makefile @@ -22,8 +22,23 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. +# Select the board to build for. +ifeq ($(BOARD),) + $(error You must provide a BOARD parameter) +else + ifeq ($(wildcard boards/$(BOARD)/.),) + $(error Invalid BOARD specified) + endif +endif + +# If the build directory is not given, make it reflect the board name. +BUILD ?= build-$(BOARD) + include ../../py/mkenv.mk +# Board-specific +include boards/$(BOARD)/mpconfigboard.mk + # Port-specific include mpconfigport.mk @@ -54,6 +69,7 @@ INC += \ -I../lib/timeutils \ -I../../lib/tinyusb/src \ -I../../supervisor/shared/usb \ + -Iboards/$(BOARD) \ -I$(BUILD) \ -I$(SPRESENSE_SDK)/nuttx/include \ -I$(SPRESENSE_SDK)/nuttx/arch \ @@ -123,6 +139,8 @@ SRC_C = \ background.c \ fatfs_port.c \ mphalport.c \ + boards/$(BOARD)/board.c \ + boards/$(BOARD)/pins.c \ lib/utils/stdout_helpers.c \ lib/utils/pyexec.c \ lib/libc/string0.c \ diff --git a/ports/cxd56/README.md b/ports/cxd56/README.md index d66ea40b4..99a9032fe 100644 --- a/ports/cxd56/README.md +++ b/ports/cxd56/README.md @@ -39,7 +39,7 @@ Change directory to cxd56: To build circuitpython image run: - $ make + $ make BOARD=spresense USB connection -------------- @@ -67,14 +67,14 @@ Extract spresense binaries in your PC to ports/spresense/spresense-exported-sdk/ To flash the bootloader run the command: - $ make flash-bootloader + $ make BOARD=spresense flash-bootloader Flash the circuitpython.spk image --------------------------------- To flash the firmware run the command: - $ make flash + $ make BOARD=spresense flash Accessing the board ------------------- diff --git a/ports/cxd56/boards/board.h b/ports/cxd56/boards/board.h new file mode 100644 index 000000000..597ae72e6 --- /dev/null +++ b/ports/cxd56/boards/board.h @@ -0,0 +1,45 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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. + */ + +// This file defines board specific functions. + +#ifndef MICROPY_INCLUDED_CXD56_BOARDS_BOARD_H +#define MICROPY_INCLUDED_CXD56_BOARDS_BOARD_H + +#include + +// Initializes board related state once on start up. +void board_init(void); + +// Returns true if the user initiates safe mode in a board specific way. +// Also add BOARD_USER_SAFE_MODE in mpconfigboard.h to explain the board specific +// way. +bool board_requests_safe_mode(void); + +// Reset the state of off MCU components such as neopixels. +void reset_board(void); + +#endif // MICROPY_INCLUDED_CXD56_BOARDS_BOARD_H diff --git a/ports/cxd56/boards/spresense/board.c b/ports/cxd56/boards/spresense/board.c new file mode 100644 index 000000000..2af7cfdcf --- /dev/null +++ b/ports/cxd56/boards/spresense/board.c @@ -0,0 +1,38 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 "boards/board.h" + +void board_init(void) +{ +} + +bool board_requests_safe_mode(void) { + return false; +} + +void reset_board(void) { +} diff --git a/ports/cxd56/boards/spresense/mpconfigboard.h b/ports/cxd56/boards/spresense/mpconfigboard.h new file mode 100644 index 000000000..e0fef60f0 --- /dev/null +++ b/ports/cxd56/boards/spresense/mpconfigboard.h @@ -0,0 +1,39 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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. + */ + +#define MICROPY_HW_BOARD_NAME "SPRESENSE" +#define MICROPY_HW_MCU_NAME "CXD5602" +#define MICROPY_PY_SYS_PLATFORM "SPRESENSE" + +#define DEFAULT_I2C_BUS_SCL (&pin_D15) +#define DEFAULT_I2C_BUS_SDA (&pin_D14) + +#define DEFAULT_SPI_BUS_SCK (&pin_D13) +#define DEFAULT_SPI_BUS_MISO (&pin_D12) +#define DEFAULT_SPI_BUS_MOSI (&pin_D11) + +#define DEFAULT_UART_BUS_RX (&pin_D0) +#define DEFAULT_UART_BUS_TX (&pin_D1) diff --git a/ports/cxd56/boards/spresense/mpconfigboard.mk b/ports/cxd56/boards/spresense/mpconfigboard.mk new file mode 100644 index 000000000..a2d4e5d88 --- /dev/null +++ b/ports/cxd56/boards/spresense/mpconfigboard.mk @@ -0,0 +1,4 @@ +USB_VID = 0x054c +USB_PID = 0x0bc2 +USB_PRODUCT = "Spresense" +USB_MANUFACTURER = "Sony" diff --git a/ports/cxd56/boards/spresense/pins.c b/ports/cxd56/boards/spresense/pins.c new file mode 100644 index 000000000..d123969fe --- /dev/null +++ b/ports/cxd56/boards/spresense/pins.c @@ -0,0 +1,80 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright 2019 Sony Semiconductor Solutions 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 "shared-bindings/board/__init__.h" + +STATIC const mp_rom_map_elem_t board_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR_D0), MP_ROM_PTR(&pin_D0) }, + { MP_ROM_QSTR(MP_QSTR_D1), MP_ROM_PTR(&pin_D1) }, + { MP_ROM_QSTR(MP_QSTR_D2), MP_ROM_PTR(&pin_D2) }, + { MP_ROM_QSTR(MP_QSTR_D3), MP_ROM_PTR(&pin_D3) }, + { MP_ROM_QSTR(MP_QSTR_D4), MP_ROM_PTR(&pin_D4) }, + { MP_ROM_QSTR(MP_QSTR_D5), MP_ROM_PTR(&pin_D5) }, + { MP_ROM_QSTR(MP_QSTR_D6), MP_ROM_PTR(&pin_D6) }, + { MP_ROM_QSTR(MP_QSTR_D7), MP_ROM_PTR(&pin_D7) }, + { MP_ROM_QSTR(MP_QSTR_D8), MP_ROM_PTR(&pin_D8) }, + { MP_ROM_QSTR(MP_QSTR_D9), MP_ROM_PTR(&pin_D9) }, + { MP_ROM_QSTR(MP_QSTR_D10), MP_ROM_PTR(&pin_D10) }, + { MP_ROM_QSTR(MP_QSTR_D11), MP_ROM_PTR(&pin_D11) }, + { MP_ROM_QSTR(MP_QSTR_D12), MP_ROM_PTR(&pin_D12) }, + { MP_ROM_QSTR(MP_QSTR_D13), MP_ROM_PTR(&pin_D13) }, + { MP_ROM_QSTR(MP_QSTR_D14), MP_ROM_PTR(&pin_D14) }, + { MP_ROM_QSTR(MP_QSTR_D15), MP_ROM_PTR(&pin_D15) }, + { MP_ROM_QSTR(MP_QSTR_D16), MP_ROM_PTR(&pin_D16) }, + { MP_ROM_QSTR(MP_QSTR_D17), MP_ROM_PTR(&pin_D17) }, + { MP_ROM_QSTR(MP_QSTR_D18), MP_ROM_PTR(&pin_D18) }, + { MP_ROM_QSTR(MP_QSTR_D19), MP_ROM_PTR(&pin_D19) }, + { MP_ROM_QSTR(MP_QSTR_D20), MP_ROM_PTR(&pin_D20) }, + { MP_ROM_QSTR(MP_QSTR_D21), MP_ROM_PTR(&pin_D21) }, + { MP_ROM_QSTR(MP_QSTR_D22), MP_ROM_PTR(&pin_D22) }, + { MP_ROM_QSTR(MP_QSTR_D23), MP_ROM_PTR(&pin_D23) }, + { MP_ROM_QSTR(MP_QSTR_D24), MP_ROM_PTR(&pin_D24) }, + { MP_ROM_QSTR(MP_QSTR_D25), MP_ROM_PTR(&pin_D25) }, + { MP_ROM_QSTR(MP_QSTR_D26), MP_ROM_PTR(&pin_D26) }, + { MP_ROM_QSTR(MP_QSTR_D27), MP_ROM_PTR(&pin_D27) }, + { MP_ROM_QSTR(MP_QSTR_D28), MP_ROM_PTR(&pin_D28) }, + { MP_ROM_QSTR(MP_QSTR_LED0), MP_ROM_PTR(&pin_LED0) }, + { MP_ROM_QSTR(MP_QSTR_LED1), MP_ROM_PTR(&pin_LED1) }, + { MP_ROM_QSTR(MP_QSTR_LED2), MP_ROM_PTR(&pin_LED2) }, + { MP_ROM_QSTR(MP_QSTR_LED3), MP_ROM_PTR(&pin_LED3) }, + { MP_ROM_QSTR(MP_QSTR_A0), MP_ROM_PTR(&pin_A0) }, + { MP_ROM_QSTR(MP_QSTR_A1), MP_ROM_PTR(&pin_A1) }, + { MP_ROM_QSTR(MP_QSTR_A2), MP_ROM_PTR(&pin_A2) }, + { MP_ROM_QSTR(MP_QSTR_A3), MP_ROM_PTR(&pin_A3) }, + { MP_ROM_QSTR(MP_QSTR_A4), MP_ROM_PTR(&pin_A4) }, + { MP_ROM_QSTR(MP_QSTR_A5), MP_ROM_PTR(&pin_A5) }, + { MP_ROM_QSTR(MP_QSTR_SDA), MP_ROM_PTR(&pin_D14) }, + { MP_ROM_QSTR(MP_QSTR_SCL), MP_ROM_PTR(&pin_D15) }, + { MP_ROM_QSTR(MP_QSTR_SCK), MP_ROM_PTR(&pin_D13) }, + { MP_ROM_QSTR(MP_QSTR_MISO), MP_ROM_PTR(&pin_D12) }, + { MP_ROM_QSTR(MP_QSTR_MOSI), MP_ROM_PTR(&pin_D11) }, + { MP_ROM_QSTR(MP_QSTR_RX), MP_ROM_PTR(&pin_D0) }, + { MP_ROM_QSTR(MP_QSTR_TX), MP_ROM_PTR(&pin_D1) }, + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, + { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, + { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&board_uart_obj) }, +}; +MP_DEFINE_CONST_DICT(board_module_globals, board_module_globals_table); diff --git a/ports/cxd56/common-hal/board/__init__.c b/ports/cxd56/common-hal/board/__init__.c index d123969fe..7a409d503 100644 --- a/ports/cxd56/common-hal/board/__init__.c +++ b/ports/cxd56/common-hal/board/__init__.c @@ -1,80 +1 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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 "shared-bindings/board/__init__.h" - -STATIC const mp_rom_map_elem_t board_module_globals_table[] = { - { MP_ROM_QSTR(MP_QSTR_D0), MP_ROM_PTR(&pin_D0) }, - { MP_ROM_QSTR(MP_QSTR_D1), MP_ROM_PTR(&pin_D1) }, - { MP_ROM_QSTR(MP_QSTR_D2), MP_ROM_PTR(&pin_D2) }, - { MP_ROM_QSTR(MP_QSTR_D3), MP_ROM_PTR(&pin_D3) }, - { MP_ROM_QSTR(MP_QSTR_D4), MP_ROM_PTR(&pin_D4) }, - { MP_ROM_QSTR(MP_QSTR_D5), MP_ROM_PTR(&pin_D5) }, - { MP_ROM_QSTR(MP_QSTR_D6), MP_ROM_PTR(&pin_D6) }, - { MP_ROM_QSTR(MP_QSTR_D7), MP_ROM_PTR(&pin_D7) }, - { MP_ROM_QSTR(MP_QSTR_D8), MP_ROM_PTR(&pin_D8) }, - { MP_ROM_QSTR(MP_QSTR_D9), MP_ROM_PTR(&pin_D9) }, - { MP_ROM_QSTR(MP_QSTR_D10), MP_ROM_PTR(&pin_D10) }, - { MP_ROM_QSTR(MP_QSTR_D11), MP_ROM_PTR(&pin_D11) }, - { MP_ROM_QSTR(MP_QSTR_D12), MP_ROM_PTR(&pin_D12) }, - { MP_ROM_QSTR(MP_QSTR_D13), MP_ROM_PTR(&pin_D13) }, - { MP_ROM_QSTR(MP_QSTR_D14), MP_ROM_PTR(&pin_D14) }, - { MP_ROM_QSTR(MP_QSTR_D15), MP_ROM_PTR(&pin_D15) }, - { MP_ROM_QSTR(MP_QSTR_D16), MP_ROM_PTR(&pin_D16) }, - { MP_ROM_QSTR(MP_QSTR_D17), MP_ROM_PTR(&pin_D17) }, - { MP_ROM_QSTR(MP_QSTR_D18), MP_ROM_PTR(&pin_D18) }, - { MP_ROM_QSTR(MP_QSTR_D19), MP_ROM_PTR(&pin_D19) }, - { MP_ROM_QSTR(MP_QSTR_D20), MP_ROM_PTR(&pin_D20) }, - { MP_ROM_QSTR(MP_QSTR_D21), MP_ROM_PTR(&pin_D21) }, - { MP_ROM_QSTR(MP_QSTR_D22), MP_ROM_PTR(&pin_D22) }, - { MP_ROM_QSTR(MP_QSTR_D23), MP_ROM_PTR(&pin_D23) }, - { MP_ROM_QSTR(MP_QSTR_D24), MP_ROM_PTR(&pin_D24) }, - { MP_ROM_QSTR(MP_QSTR_D25), MP_ROM_PTR(&pin_D25) }, - { MP_ROM_QSTR(MP_QSTR_D26), MP_ROM_PTR(&pin_D26) }, - { MP_ROM_QSTR(MP_QSTR_D27), MP_ROM_PTR(&pin_D27) }, - { MP_ROM_QSTR(MP_QSTR_D28), MP_ROM_PTR(&pin_D28) }, - { MP_ROM_QSTR(MP_QSTR_LED0), MP_ROM_PTR(&pin_LED0) }, - { MP_ROM_QSTR(MP_QSTR_LED1), MP_ROM_PTR(&pin_LED1) }, - { MP_ROM_QSTR(MP_QSTR_LED2), MP_ROM_PTR(&pin_LED2) }, - { MP_ROM_QSTR(MP_QSTR_LED3), MP_ROM_PTR(&pin_LED3) }, - { MP_ROM_QSTR(MP_QSTR_A0), MP_ROM_PTR(&pin_A0) }, - { MP_ROM_QSTR(MP_QSTR_A1), MP_ROM_PTR(&pin_A1) }, - { MP_ROM_QSTR(MP_QSTR_A2), MP_ROM_PTR(&pin_A2) }, - { MP_ROM_QSTR(MP_QSTR_A3), MP_ROM_PTR(&pin_A3) }, - { MP_ROM_QSTR(MP_QSTR_A4), MP_ROM_PTR(&pin_A4) }, - { MP_ROM_QSTR(MP_QSTR_A5), MP_ROM_PTR(&pin_A5) }, - { MP_ROM_QSTR(MP_QSTR_SDA), MP_ROM_PTR(&pin_D14) }, - { MP_ROM_QSTR(MP_QSTR_SCL), MP_ROM_PTR(&pin_D15) }, - { MP_ROM_QSTR(MP_QSTR_SCK), MP_ROM_PTR(&pin_D13) }, - { MP_ROM_QSTR(MP_QSTR_MISO), MP_ROM_PTR(&pin_D12) }, - { MP_ROM_QSTR(MP_QSTR_MOSI), MP_ROM_PTR(&pin_D11) }, - { MP_ROM_QSTR(MP_QSTR_RX), MP_ROM_PTR(&pin_D0) }, - { MP_ROM_QSTR(MP_QSTR_TX), MP_ROM_PTR(&pin_D1) }, - { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, - { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, - { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&board_uart_obj) }, -}; -MP_DEFINE_CONST_DICT(board_module_globals, board_module_globals_table); +// No board module functions. diff --git a/ports/cxd56/mpconfigboard.h b/ports/cxd56/mpconfigboard.h deleted file mode 100644 index e0fef60f0..000000000 --- a/ports/cxd56/mpconfigboard.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright 2019 Sony Semiconductor Solutions 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. - */ - -#define MICROPY_HW_BOARD_NAME "SPRESENSE" -#define MICROPY_HW_MCU_NAME "CXD5602" -#define MICROPY_PY_SYS_PLATFORM "SPRESENSE" - -#define DEFAULT_I2C_BUS_SCL (&pin_D15) -#define DEFAULT_I2C_BUS_SDA (&pin_D14) - -#define DEFAULT_SPI_BUS_SCK (&pin_D13) -#define DEFAULT_SPI_BUS_MISO (&pin_D12) -#define DEFAULT_SPI_BUS_MOSI (&pin_D11) - -#define DEFAULT_UART_BUS_RX (&pin_D0) -#define DEFAULT_UART_BUS_TX (&pin_D1) diff --git a/ports/cxd56/mpconfigport.mk b/ports/cxd56/mpconfigport.mk index 53dba2840..a418ae8e9 100644 --- a/ports/cxd56/mpconfigport.mk +++ b/ports/cxd56/mpconfigport.mk @@ -1,7 +1,3 @@ -USB_VID = 0x054c -USB_PID = 0x0bc2 -USB_PRODUCT = "Spresense" -USB_MANUFACTURER = "Sony" USB_SERIAL_NUMBER_LENGTH = 10 USB_DEVICES = "CDC,MSC" USB_MSC_MAX_PACKET_SIZE = 512 diff --git a/ports/cxd56/supervisor/port.c b/ports/cxd56/supervisor/port.c index ab1ea5273..3fddfe52c 100644 --- a/ports/cxd56/supervisor/port.c +++ b/ports/cxd56/supervisor/port.c @@ -27,6 +27,8 @@ #include #include +#include "boards/board.h" + #include "supervisor/port.h" #include "common-hal/microcontroller/Pin.h" @@ -36,6 +38,13 @@ safe_mode_t port_init(void) { boardctl(BOARDIOC_INIT, 0); + + board_init(); + + if (board_requests_safe_mode()) { + return USER_SAFE_MODE; + } + return NO_SAFE_MODE; } @@ -55,9 +64,6 @@ void reset_port(void) { reset_all_pins(); } -void reset_board(void) { -} - void reset_to_bootloader(void) { } -- cgit v1.2.3 From 8f653d62384fd9463d1c4df5c9ded4fe8546c6d4 Mon Sep 17 00:00:00 2001 From: Kamil Tomaszewski Date: Fri, 11 Oct 2019 09:34:00 +0200 Subject: Use CXD56 pin names --- ports/cxd56/boards/spresense/mpconfigboard.h | 14 +-- ports/cxd56/boards/spresense/pins.c | 92 +++++++------- ports/cxd56/common-hal/analogio/AnalogIn.c | 12 +- ports/cxd56/common-hal/microcontroller/Pin.c | 144 +++++++++++----------- ports/cxd56/common-hal/microcontroller/Pin.h | 78 ++++++------ ports/cxd56/common-hal/microcontroller/__init__.c | 78 ++++++------ ports/cxd56/common-hal/pulseio/PWMOut.c | 8 +- 7 files changed, 213 insertions(+), 213 deletions(-) diff --git a/ports/cxd56/boards/spresense/mpconfigboard.h b/ports/cxd56/boards/spresense/mpconfigboard.h index e0fef60f0..b20d00658 100644 --- a/ports/cxd56/boards/spresense/mpconfigboard.h +++ b/ports/cxd56/boards/spresense/mpconfigboard.h @@ -28,12 +28,12 @@ #define MICROPY_HW_MCU_NAME "CXD5602" #define MICROPY_PY_SYS_PLATFORM "SPRESENSE" -#define DEFAULT_I2C_BUS_SCL (&pin_D15) -#define DEFAULT_I2C_BUS_SDA (&pin_D14) +#define DEFAULT_I2C_BUS_SCL (&pin_I2C0_BCK) +#define DEFAULT_I2C_BUS_SDA (&pin_I2C0_BDT) -#define DEFAULT_SPI_BUS_SCK (&pin_D13) -#define DEFAULT_SPI_BUS_MISO (&pin_D12) -#define DEFAULT_SPI_BUS_MOSI (&pin_D11) +#define DEFAULT_SPI_BUS_SCK (&pin_SPI4_SCK) +#define DEFAULT_SPI_BUS_MISO (&pin_SPI4_MISO) +#define DEFAULT_SPI_BUS_MOSI (&pin_SPI4_MOSI) -#define DEFAULT_UART_BUS_RX (&pin_D0) -#define DEFAULT_UART_BUS_TX (&pin_D1) +#define DEFAULT_UART_BUS_RX (&pin_UART2_RXD) +#define DEFAULT_UART_BUS_TX (&pin_UART2_TXD) diff --git a/ports/cxd56/boards/spresense/pins.c b/ports/cxd56/boards/spresense/pins.c index d123969fe..fcc854590 100644 --- a/ports/cxd56/boards/spresense/pins.c +++ b/ports/cxd56/boards/spresense/pins.c @@ -27,52 +27,52 @@ #include "shared-bindings/board/__init__.h" STATIC const mp_rom_map_elem_t board_module_globals_table[] = { - { MP_ROM_QSTR(MP_QSTR_D0), MP_ROM_PTR(&pin_D0) }, - { MP_ROM_QSTR(MP_QSTR_D1), MP_ROM_PTR(&pin_D1) }, - { MP_ROM_QSTR(MP_QSTR_D2), MP_ROM_PTR(&pin_D2) }, - { MP_ROM_QSTR(MP_QSTR_D3), MP_ROM_PTR(&pin_D3) }, - { MP_ROM_QSTR(MP_QSTR_D4), MP_ROM_PTR(&pin_D4) }, - { MP_ROM_QSTR(MP_QSTR_D5), MP_ROM_PTR(&pin_D5) }, - { MP_ROM_QSTR(MP_QSTR_D6), MP_ROM_PTR(&pin_D6) }, - { MP_ROM_QSTR(MP_QSTR_D7), MP_ROM_PTR(&pin_D7) }, - { MP_ROM_QSTR(MP_QSTR_D8), MP_ROM_PTR(&pin_D8) }, - { MP_ROM_QSTR(MP_QSTR_D9), MP_ROM_PTR(&pin_D9) }, - { MP_ROM_QSTR(MP_QSTR_D10), MP_ROM_PTR(&pin_D10) }, - { MP_ROM_QSTR(MP_QSTR_D11), MP_ROM_PTR(&pin_D11) }, - { MP_ROM_QSTR(MP_QSTR_D12), MP_ROM_PTR(&pin_D12) }, - { MP_ROM_QSTR(MP_QSTR_D13), MP_ROM_PTR(&pin_D13) }, - { MP_ROM_QSTR(MP_QSTR_D14), MP_ROM_PTR(&pin_D14) }, - { MP_ROM_QSTR(MP_QSTR_D15), MP_ROM_PTR(&pin_D15) }, - { MP_ROM_QSTR(MP_QSTR_D16), MP_ROM_PTR(&pin_D16) }, - { MP_ROM_QSTR(MP_QSTR_D17), MP_ROM_PTR(&pin_D17) }, - { MP_ROM_QSTR(MP_QSTR_D18), MP_ROM_PTR(&pin_D18) }, - { MP_ROM_QSTR(MP_QSTR_D19), MP_ROM_PTR(&pin_D19) }, - { MP_ROM_QSTR(MP_QSTR_D20), MP_ROM_PTR(&pin_D20) }, - { MP_ROM_QSTR(MP_QSTR_D21), MP_ROM_PTR(&pin_D21) }, - { MP_ROM_QSTR(MP_QSTR_D22), MP_ROM_PTR(&pin_D22) }, - { MP_ROM_QSTR(MP_QSTR_D23), MP_ROM_PTR(&pin_D23) }, - { MP_ROM_QSTR(MP_QSTR_D24), MP_ROM_PTR(&pin_D24) }, - { MP_ROM_QSTR(MP_QSTR_D25), MP_ROM_PTR(&pin_D25) }, - { MP_ROM_QSTR(MP_QSTR_D26), MP_ROM_PTR(&pin_D26) }, - { MP_ROM_QSTR(MP_QSTR_D27), MP_ROM_PTR(&pin_D27) }, - { MP_ROM_QSTR(MP_QSTR_D28), MP_ROM_PTR(&pin_D28) }, - { MP_ROM_QSTR(MP_QSTR_LED0), MP_ROM_PTR(&pin_LED0) }, - { MP_ROM_QSTR(MP_QSTR_LED1), MP_ROM_PTR(&pin_LED1) }, - { MP_ROM_QSTR(MP_QSTR_LED2), MP_ROM_PTR(&pin_LED2) }, - { MP_ROM_QSTR(MP_QSTR_LED3), MP_ROM_PTR(&pin_LED3) }, - { MP_ROM_QSTR(MP_QSTR_A0), MP_ROM_PTR(&pin_A0) }, - { MP_ROM_QSTR(MP_QSTR_A1), MP_ROM_PTR(&pin_A1) }, - { MP_ROM_QSTR(MP_QSTR_A2), MP_ROM_PTR(&pin_A2) }, - { MP_ROM_QSTR(MP_QSTR_A3), MP_ROM_PTR(&pin_A3) }, - { MP_ROM_QSTR(MP_QSTR_A4), MP_ROM_PTR(&pin_A4) }, - { MP_ROM_QSTR(MP_QSTR_A5), MP_ROM_PTR(&pin_A5) }, - { MP_ROM_QSTR(MP_QSTR_SDA), MP_ROM_PTR(&pin_D14) }, - { MP_ROM_QSTR(MP_QSTR_SCL), MP_ROM_PTR(&pin_D15) }, - { MP_ROM_QSTR(MP_QSTR_SCK), MP_ROM_PTR(&pin_D13) }, - { MP_ROM_QSTR(MP_QSTR_MISO), MP_ROM_PTR(&pin_D12) }, - { MP_ROM_QSTR(MP_QSTR_MOSI), MP_ROM_PTR(&pin_D11) }, - { MP_ROM_QSTR(MP_QSTR_RX), MP_ROM_PTR(&pin_D0) }, - { MP_ROM_QSTR(MP_QSTR_TX), MP_ROM_PTR(&pin_D1) }, + { MP_ROM_QSTR(MP_QSTR_D0), MP_ROM_PTR(&pin_UART2_RXD) }, + { MP_ROM_QSTR(MP_QSTR_D1), MP_ROM_PTR(&pin_UART2_TXD) }, + { MP_ROM_QSTR(MP_QSTR_D2), MP_ROM_PTR(&pin_HIF_IRQ_OUT) }, + { MP_ROM_QSTR(MP_QSTR_D3), MP_ROM_PTR(&pin_PWM3) }, + { MP_ROM_QSTR(MP_QSTR_D4), MP_ROM_PTR(&pin_SPI2_MOSI) }, + { MP_ROM_QSTR(MP_QSTR_D5), MP_ROM_PTR(&pin_PWM1) }, + { MP_ROM_QSTR(MP_QSTR_D6), MP_ROM_PTR(&pin_PWM0) }, + { MP_ROM_QSTR(MP_QSTR_D7), MP_ROM_PTR(&pin_SPI3_CS1_X) }, + { MP_ROM_QSTR(MP_QSTR_D8), MP_ROM_PTR(&pin_SPI2_MISO) }, + { MP_ROM_QSTR(MP_QSTR_D9), MP_ROM_PTR(&pin_PWM2) }, + { MP_ROM_QSTR(MP_QSTR_D10), MP_ROM_PTR(&pin_SPI4_CS_X) }, + { MP_ROM_QSTR(MP_QSTR_D11), MP_ROM_PTR(&pin_SPI4_MOSI) }, + { MP_ROM_QSTR(MP_QSTR_D12), MP_ROM_PTR(&pin_SPI4_MISO) }, + { MP_ROM_QSTR(MP_QSTR_D13), MP_ROM_PTR(&pin_SPI4_SCK) }, + { MP_ROM_QSTR(MP_QSTR_D14), MP_ROM_PTR(&pin_I2C0_BDT) }, + { MP_ROM_QSTR(MP_QSTR_D15), MP_ROM_PTR(&pin_I2C0_BCK) }, + { MP_ROM_QSTR(MP_QSTR_D16), MP_ROM_PTR(&pin_EMMC_DATA0) }, + { MP_ROM_QSTR(MP_QSTR_D17), MP_ROM_PTR(&pin_EMMC_DATA1) }, + { MP_ROM_QSTR(MP_QSTR_D18), MP_ROM_PTR(&pin_I2S0_DATA_OUT) }, + { MP_ROM_QSTR(MP_QSTR_D19), MP_ROM_PTR(&pin_I2S0_DATA_IN) }, + { MP_ROM_QSTR(MP_QSTR_D20), MP_ROM_PTR(&pin_EMMC_DATA2) }, + { MP_ROM_QSTR(MP_QSTR_D21), MP_ROM_PTR(&pin_EMMC_DATA3) }, + { MP_ROM_QSTR(MP_QSTR_D22), MP_ROM_PTR(&pin_SEN_IRQ_IN) }, + { MP_ROM_QSTR(MP_QSTR_D23), MP_ROM_PTR(&pin_EMMC_CLK) }, + { MP_ROM_QSTR(MP_QSTR_D24), MP_ROM_PTR(&pin_EMMC_CMD) }, + { MP_ROM_QSTR(MP_QSTR_D25), MP_ROM_PTR(&pin_I2S0_LRCK) }, + { MP_ROM_QSTR(MP_QSTR_D26), MP_ROM_PTR(&pin_I2S0_BCK) }, + { MP_ROM_QSTR(MP_QSTR_D27), MP_ROM_PTR(&pin_UART2_CTS) }, + { MP_ROM_QSTR(MP_QSTR_D28), MP_ROM_PTR(&pin_UART2_RTS) }, + { MP_ROM_QSTR(MP_QSTR_LED0), MP_ROM_PTR(&pin_I2S1_BCK) }, + { MP_ROM_QSTR(MP_QSTR_LED1), MP_ROM_PTR(&pin_I2S1_LRCK) }, + { MP_ROM_QSTR(MP_QSTR_LED2), MP_ROM_PTR(&pin_I2S1_DATA_IN) }, + { MP_ROM_QSTR(MP_QSTR_LED3), MP_ROM_PTR(&pin_I2S1_DATA_OUT) }, + { MP_ROM_QSTR(MP_QSTR_A0), MP_ROM_PTR(&pin_LPADC0) }, + { MP_ROM_QSTR(MP_QSTR_A1), MP_ROM_PTR(&pin_LPADC1) }, + { MP_ROM_QSTR(MP_QSTR_A2), MP_ROM_PTR(&pin_LPADC2) }, + { MP_ROM_QSTR(MP_QSTR_A3), MP_ROM_PTR(&pin_LPADC3) }, + { MP_ROM_QSTR(MP_QSTR_A4), MP_ROM_PTR(&pin_HPADC0) }, + { MP_ROM_QSTR(MP_QSTR_A5), MP_ROM_PTR(&pin_HPADC1) }, + { MP_ROM_QSTR(MP_QSTR_SDA), MP_ROM_PTR(&pin_I2C0_BDT) }, + { MP_ROM_QSTR(MP_QSTR_SCL), MP_ROM_PTR(&pin_I2C0_BCK) }, + { MP_ROM_QSTR(MP_QSTR_SCK), MP_ROM_PTR(&pin_SPI4_SCK) }, + { MP_ROM_QSTR(MP_QSTR_MISO), MP_ROM_PTR(&pin_SPI4_MISO) }, + { MP_ROM_QSTR(MP_QSTR_MOSI), MP_ROM_PTR(&pin_SPI4_MOSI) }, + { MP_ROM_QSTR(MP_QSTR_RX), MP_ROM_PTR(&pin_UART2_RXD) }, + { MP_ROM_QSTR(MP_QSTR_TX), MP_ROM_PTR(&pin_UART2_TXD) }, { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&board_uart_obj) }, diff --git a/ports/cxd56/common-hal/analogio/AnalogIn.c b/ports/cxd56/common-hal/analogio/AnalogIn.c index 438fbbcae..6f3b7a1ba 100644 --- a/ports/cxd56/common-hal/analogio/AnalogIn.c +++ b/ports/cxd56/common-hal/analogio/AnalogIn.c @@ -42,12 +42,12 @@ typedef struct { } analogin_dev_t; STATIC analogin_dev_t analogin_dev[] = { - {"/dev/lpadc0", &pin_A0, -1}, - {"/dev/lpadc1", &pin_A1, -1}, - {"/dev/lpadc2", &pin_A2, -1}, - {"/dev/lpadc3", &pin_A3, -1}, - {"/dev/hpadc0", &pin_A4, -1}, - {"/dev/hpadc1", &pin_A5, -1}, + {"/dev/lpadc0", &pin_LPADC0, -1}, + {"/dev/lpadc1", &pin_LPADC1, -1}, + {"/dev/lpadc2", &pin_LPADC2, -1}, + {"/dev/lpadc3", &pin_LPADC3, -1}, + {"/dev/hpadc0", &pin_HPADC0, -1}, + {"/dev/hpadc1", &pin_HPADC1, -1}, }; void common_hal_analogio_analogin_construct(analogio_analogin_obj_t *self, const mcu_pin_obj_t *pin) { diff --git a/ports/cxd56/common-hal/microcontroller/Pin.c b/ports/cxd56/common-hal/microcontroller/Pin.c index 005defa84..23377197c 100644 --- a/ports/cxd56/common-hal/microcontroller/Pin.c +++ b/ports/cxd56/common-hal/microcontroller/Pin.c @@ -39,80 +39,80 @@ typedef struct { bool free; } pin_status_t; -const mcu_pin_obj_t pin_D0 = PIN(PIN_UART2_RXD, false); -const mcu_pin_obj_t pin_D1 = PIN(PIN_UART2_TXD, false); -const mcu_pin_obj_t pin_D2 = PIN(PIN_HIF_IRQ_OUT, false); -const mcu_pin_obj_t pin_D3 = PIN(PIN_PWM3, false); -const mcu_pin_obj_t pin_D4 = PIN(PIN_SPI2_MOSI, false); -const mcu_pin_obj_t pin_D5 = PIN(PIN_PWM1, false); -const mcu_pin_obj_t pin_D6 = PIN(PIN_PWM0, false); -const mcu_pin_obj_t pin_D7 = PIN(PIN_SPI3_CS1_X, false); -const mcu_pin_obj_t pin_D8 = PIN(PIN_SPI2_MISO, false); -const mcu_pin_obj_t pin_D9 = PIN(PIN_PWM2, false); -const mcu_pin_obj_t pin_D10 = PIN(PIN_SPI4_CS_X, false); -const mcu_pin_obj_t pin_D11 = PIN(PIN_SPI4_MOSI, false); -const mcu_pin_obj_t pin_D12 = PIN(PIN_SPI4_MISO, false); -const mcu_pin_obj_t pin_D13 = PIN(PIN_SPI4_SCK, false); -const mcu_pin_obj_t pin_D14 = PIN(PIN_I2C0_BDT, false); -const mcu_pin_obj_t pin_D15 = PIN(PIN_I2C0_BCK, false); -const mcu_pin_obj_t pin_D16 = PIN(PIN_EMMC_DATA0, false); -const mcu_pin_obj_t pin_D17 = PIN(PIN_EMMC_DATA1, false); -const mcu_pin_obj_t pin_D18 = PIN(PIN_I2S0_DATA_OUT, false); -const mcu_pin_obj_t pin_D19 = PIN(PIN_I2S0_DATA_IN, false); -const mcu_pin_obj_t pin_D20 = PIN(PIN_EMMC_DATA2, false); -const mcu_pin_obj_t pin_D21 = PIN(PIN_EMMC_DATA3, false); -const mcu_pin_obj_t pin_D22 = PIN(PIN_SEN_IRQ_IN, false); -const mcu_pin_obj_t pin_D23 = PIN(PIN_EMMC_CLK, false); -const mcu_pin_obj_t pin_D24 = PIN(PIN_EMMC_CMD, false); -const mcu_pin_obj_t pin_D25 = PIN(PIN_I2S0_LRCK, false); -const mcu_pin_obj_t pin_D26 = PIN(PIN_I2S0_BCK, false); -const mcu_pin_obj_t pin_D27 = PIN(PIN_UART2_CTS, false); -const mcu_pin_obj_t pin_D28 = PIN(PIN_UART2_RTS, false); -const mcu_pin_obj_t pin_LED0 = PIN(PIN_I2S1_BCK, false); -const mcu_pin_obj_t pin_LED1 = PIN(PIN_I2S1_LRCK, false); -const mcu_pin_obj_t pin_LED2 = PIN(PIN_I2S1_DATA_IN, false); -const mcu_pin_obj_t pin_LED3 = PIN(PIN_I2S1_DATA_OUT, false); -const mcu_pin_obj_t pin_A0 = PIN(0, true); -const mcu_pin_obj_t pin_A1 = PIN(1, true); -const mcu_pin_obj_t pin_A2 = PIN(2, true); -const mcu_pin_obj_t pin_A3 = PIN(3, true); -const mcu_pin_obj_t pin_A4 = PIN(4, true); -const mcu_pin_obj_t pin_A5 = PIN(5, true); +const mcu_pin_obj_t pin_UART2_RXD = PIN(PIN_UART2_RXD, false); +const mcu_pin_obj_t pin_UART2_TXD = PIN(PIN_UART2_TXD, false); +const mcu_pin_obj_t pin_HIF_IRQ_OUT = PIN(PIN_HIF_IRQ_OUT, false); +const mcu_pin_obj_t pin_PWM3 = PIN(PIN_PWM3, false); +const mcu_pin_obj_t pin_SPI2_MOSI = PIN(PIN_SPI2_MOSI, false); +const mcu_pin_obj_t pin_PWM1 = PIN(PIN_PWM1, false); +const mcu_pin_obj_t pin_PWM0 = PIN(PIN_PWM0, false); +const mcu_pin_obj_t pin_SPI3_CS1_X = PIN(PIN_SPI3_CS1_X, false); +const mcu_pin_obj_t pin_SPI2_MISO = PIN(PIN_SPI2_MISO, false); +const mcu_pin_obj_t pin_PWM2 = PIN(PIN_PWM2, false); +const mcu_pin_obj_t pin_SPI4_CS_X = PIN(PIN_SPI4_CS_X, false); +const mcu_pin_obj_t pin_SPI4_MOSI = PIN(PIN_SPI4_MOSI, false); +const mcu_pin_obj_t pin_SPI4_MISO = PIN(PIN_SPI4_MISO, false); +const mcu_pin_obj_t pin_SPI4_SCK = PIN(PIN_SPI4_SCK, false); +const mcu_pin_obj_t pin_I2C0_BDT = PIN(PIN_I2C0_BDT, false); +const mcu_pin_obj_t pin_I2C0_BCK = PIN(PIN_I2C0_BCK, false); +const mcu_pin_obj_t pin_EMMC_DATA0 = PIN(PIN_EMMC_DATA0, false); +const mcu_pin_obj_t pin_EMMC_DATA1 = PIN(PIN_EMMC_DATA1, false); +const mcu_pin_obj_t pin_I2S0_DATA_OUT = PIN(PIN_I2S0_DATA_OUT, false); +const mcu_pin_obj_t pin_I2S0_DATA_IN = PIN(PIN_I2S0_DATA_IN, false); +const mcu_pin_obj_t pin_EMMC_DATA2 = PIN(PIN_EMMC_DATA2, false); +const mcu_pin_obj_t pin_EMMC_DATA3 = PIN(PIN_EMMC_DATA3, false); +const mcu_pin_obj_t pin_SEN_IRQ_IN = PIN(PIN_SEN_IRQ_IN, false); +const mcu_pin_obj_t pin_EMMC_CLK = PIN(PIN_EMMC_CLK, false); +const mcu_pin_obj_t pin_EMMC_CMD = PIN(PIN_EMMC_CMD, false); +const mcu_pin_obj_t pin_I2S0_LRCK = PIN(PIN_I2S0_LRCK, false); +const mcu_pin_obj_t pin_I2S0_BCK = PIN(PIN_I2S0_BCK, false); +const mcu_pin_obj_t pin_UART2_CTS = PIN(PIN_UART2_CTS, false); +const mcu_pin_obj_t pin_UART2_RTS = PIN(PIN_UART2_RTS, false); +const mcu_pin_obj_t pin_I2S1_BCK = PIN(PIN_I2S1_BCK, false); +const mcu_pin_obj_t pin_I2S1_LRCK = PIN(PIN_I2S1_LRCK, false); +const mcu_pin_obj_t pin_I2S1_DATA_IN = PIN(PIN_I2S1_DATA_IN, false); +const mcu_pin_obj_t pin_I2S1_DATA_OUT = PIN(PIN_I2S1_DATA_OUT, false); +const mcu_pin_obj_t pin_LPADC0 = PIN(0, true); +const mcu_pin_obj_t pin_LPADC1 = PIN(1, true); +const mcu_pin_obj_t pin_LPADC2 = PIN(2, true); +const mcu_pin_obj_t pin_LPADC3 = PIN(3, true); +const mcu_pin_obj_t pin_HPADC0 = PIN(4, true); +const mcu_pin_obj_t pin_HPADC1 = PIN(5, true); STATIC pin_status_t pins[] = { - { &pin_D0, true, true }, - { &pin_D1, true, true }, - { &pin_D2, true, true }, - { &pin_D3, true, true }, - { &pin_D4, true, true }, - { &pin_D5, true, true }, - { &pin_D6, true, true }, - { &pin_D7, true, true }, - { &pin_D8, true, true }, - { &pin_D9, true, true }, - { &pin_D10, true, true }, - { &pin_D11, true, true }, - { &pin_D12, true, true }, - { &pin_D13, true, true }, - { &pin_D14, true, true }, - { &pin_D15, true, true }, - { &pin_D16, true, true }, - { &pin_D17, true, true }, - { &pin_D18, true, true }, - { &pin_D19, true, true }, - { &pin_D20, true, true }, - { &pin_D21, true, true }, - { &pin_D22, true, true }, - { &pin_D23, true, true }, - { &pin_D24, true, true }, - { &pin_D25, true, true }, - { &pin_D26, true, true }, - { &pin_D27, true, true }, - { &pin_D28, true, true }, - { &pin_LED0, true, true }, - { &pin_LED1, true, true }, - { &pin_LED2, true, true }, - { &pin_LED3, true, true }, + { &pin_UART2_RXD, true, true }, + { &pin_UART2_TXD, true, true }, + { &pin_HIF_IRQ_OUT, true, true }, + { &pin_PWM3, true, true }, + { &pin_SPI2_MOSI, true, true }, + { &pin_PWM1, true, true }, + { &pin_PWM0, true, true }, + { &pin_SPI3_CS1_X, true, true }, + { &pin_SPI2_MISO, true, true }, + { &pin_PWM2, true, true }, + { &pin_SPI4_CS_X, true, true }, + { &pin_SPI4_MOSI, true, true }, + { &pin_SPI4_MISO, true, true }, + { &pin_SPI4_SCK, true, true }, + { &pin_I2C0_BDT, true, true }, + { &pin_I2C0_BCK, true, true }, + { &pin_EMMC_DATA0, true, true }, + { &pin_EMMC_DATA1, true, true }, + { &pin_I2S0_DATA_OUT, true, true }, + { &pin_I2S0_DATA_IN, true, true }, + { &pin_EMMC_DATA2, true, true }, + { &pin_EMMC_DATA3, true, true }, + { &pin_SEN_IRQ_IN, true, true }, + { &pin_EMMC_CLK, true, true }, + { &pin_EMMC_CMD, true, true }, + { &pin_I2S0_LRCK, true, true }, + { &pin_I2S0_BCK, true, true }, + { &pin_UART2_CTS, true, true }, + { &pin_UART2_RTS, true, true }, + { &pin_I2S1_BCK, true, true }, + { &pin_I2S1_LRCK, true, true }, + { &pin_I2S1_DATA_IN, true, true }, + { &pin_I2S1_DATA_OUT, true, true }, }; bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t *pin) { diff --git a/ports/cxd56/common-hal/microcontroller/Pin.h b/ports/cxd56/common-hal/microcontroller/Pin.h index 113387cef..fe6524edb 100644 --- a/ports/cxd56/common-hal/microcontroller/Pin.h +++ b/ports/cxd56/common-hal/microcontroller/Pin.h @@ -44,45 +44,45 @@ typedef struct { bool analog; } mcu_pin_obj_t; -extern const mcu_pin_obj_t pin_D0; -extern const mcu_pin_obj_t pin_D1; -extern const mcu_pin_obj_t pin_D2; -extern const mcu_pin_obj_t pin_D3; -extern const mcu_pin_obj_t pin_D4; -extern const mcu_pin_obj_t pin_D5; -extern const mcu_pin_obj_t pin_D6; -extern const mcu_pin_obj_t pin_D7; -extern const mcu_pin_obj_t pin_D8; -extern const mcu_pin_obj_t pin_D9; -extern const mcu_pin_obj_t pin_D10; -extern const mcu_pin_obj_t pin_D11; -extern const mcu_pin_obj_t pin_D12; -extern const mcu_pin_obj_t pin_D13; -extern const mcu_pin_obj_t pin_D14; -extern const mcu_pin_obj_t pin_D15; -extern const mcu_pin_obj_t pin_D16; -extern const mcu_pin_obj_t pin_D17; -extern const mcu_pin_obj_t pin_D18; -extern const mcu_pin_obj_t pin_D19; -extern const mcu_pin_obj_t pin_D20; -extern const mcu_pin_obj_t pin_D21; -extern const mcu_pin_obj_t pin_D22; -extern const mcu_pin_obj_t pin_D23; -extern const mcu_pin_obj_t pin_D24; -extern const mcu_pin_obj_t pin_D25; -extern const mcu_pin_obj_t pin_D26; -extern const mcu_pin_obj_t pin_D27; -extern const mcu_pin_obj_t pin_D28; -extern const mcu_pin_obj_t pin_LED0; -extern const mcu_pin_obj_t pin_LED1; -extern const mcu_pin_obj_t pin_LED2; -extern const mcu_pin_obj_t pin_LED3; -extern const mcu_pin_obj_t pin_A0; -extern const mcu_pin_obj_t pin_A1; -extern const mcu_pin_obj_t pin_A2; -extern const mcu_pin_obj_t pin_A3; -extern const mcu_pin_obj_t pin_A4; -extern const mcu_pin_obj_t pin_A5; +extern const mcu_pin_obj_t pin_UART2_RXD; +extern const mcu_pin_obj_t pin_UART2_TXD; +extern const mcu_pin_obj_t pin_HIF_IRQ_OUT; +extern const mcu_pin_obj_t pin_PWM3; +extern const mcu_pin_obj_t pin_SPI2_MOSI; +extern const mcu_pin_obj_t pin_PWM1; +extern const mcu_pin_obj_t pin_PWM0; +extern const mcu_pin_obj_t pin_SPI3_CS1_X; +extern const mcu_pin_obj_t pin_SPI2_MISO; +extern const mcu_pin_obj_t pin_PWM2; +extern const mcu_pin_obj_t pin_SPI4_CS_X; +extern const mcu_pin_obj_t pin_SPI4_MOSI; +extern const mcu_pin_obj_t pin_SPI4_MISO; +extern const mcu_pin_obj_t pin_SPI4_SCK; +extern const mcu_pin_obj_t pin_I2C0_BDT; +extern const mcu_pin_obj_t pin_I2C0_BCK; +extern const mcu_pin_obj_t pin_EMMC_DATA0; +extern const mcu_pin_obj_t pin_EMMC_DATA1; +extern const mcu_pin_obj_t pin_I2S0_DATA_OUT; +extern const mcu_pin_obj_t pin_I2S0_DATA_IN; +extern const mcu_pin_obj_t pin_EMMC_DATA2; +extern const mcu_pin_obj_t pin_EMMC_DATA3; +extern const mcu_pin_obj_t pin_SEN_IRQ_IN; +extern const mcu_pin_obj_t pin_EMMC_CLK; +extern const mcu_pin_obj_t pin_EMMC_CMD; +extern const mcu_pin_obj_t pin_I2S0_LRCK; +extern const mcu_pin_obj_t pin_I2S0_BCK; +extern const mcu_pin_obj_t pin_UART2_CTS; +extern const mcu_pin_obj_t pin_UART2_RTS; +extern const mcu_pin_obj_t pin_I2S1_BCK; +extern const mcu_pin_obj_t pin_I2S1_LRCK; +extern const mcu_pin_obj_t pin_I2S1_DATA_IN; +extern const mcu_pin_obj_t pin_I2S1_DATA_OUT; +extern const mcu_pin_obj_t pin_LPADC0; +extern const mcu_pin_obj_t pin_LPADC1; +extern const mcu_pin_obj_t pin_LPADC2; +extern const mcu_pin_obj_t pin_LPADC3; +extern const mcu_pin_obj_t pin_HPADC0; +extern const mcu_pin_obj_t pin_HPADC1; void never_reset_pin_number(uint8_t pin_number); void reset_pin_number(uint8_t pin_number); diff --git a/ports/cxd56/common-hal/microcontroller/__init__.c b/ports/cxd56/common-hal/microcontroller/__init__.c index 576896eb2..2be74b006 100644 --- a/ports/cxd56/common-hal/microcontroller/__init__.c +++ b/ports/cxd56/common-hal/microcontroller/__init__.c @@ -68,44 +68,44 @@ void common_hal_mcu_reset(void) { } STATIC const mp_rom_map_elem_t mcu_pin_globals_table[] = { - { MP_ROM_QSTR(MP_QSTR_D0), MP_ROM_PTR(&pin_D0) }, - { MP_ROM_QSTR(MP_QSTR_D1), MP_ROM_PTR(&pin_D1) }, - { MP_ROM_QSTR(MP_QSTR_D2), MP_ROM_PTR(&pin_D2) }, - { MP_ROM_QSTR(MP_QSTR_D3), MP_ROM_PTR(&pin_D3) }, - { MP_ROM_QSTR(MP_QSTR_D4), MP_ROM_PTR(&pin_D4) }, - { MP_ROM_QSTR(MP_QSTR_D5), MP_ROM_PTR(&pin_D5) }, - { MP_ROM_QSTR(MP_QSTR_D6), MP_ROM_PTR(&pin_D6) }, - { MP_ROM_QSTR(MP_QSTR_D7), MP_ROM_PTR(&pin_D7) }, - { MP_ROM_QSTR(MP_QSTR_D8), MP_ROM_PTR(&pin_D8) }, - { MP_ROM_QSTR(MP_QSTR_D9), MP_ROM_PTR(&pin_D9) }, - { MP_ROM_QSTR(MP_QSTR_D10), MP_ROM_PTR(&pin_D10) }, - { MP_ROM_QSTR(MP_QSTR_D11), MP_ROM_PTR(&pin_D11) }, - { MP_ROM_QSTR(MP_QSTR_D12), MP_ROM_PTR(&pin_D12) }, - { MP_ROM_QSTR(MP_QSTR_D13), MP_ROM_PTR(&pin_D13) }, - { MP_ROM_QSTR(MP_QSTR_D14), MP_ROM_PTR(&pin_D14) }, - { MP_ROM_QSTR(MP_QSTR_D15), MP_ROM_PTR(&pin_D15) }, - { MP_ROM_QSTR(MP_QSTR_D16), MP_ROM_PTR(&pin_D16) }, - { MP_ROM_QSTR(MP_QSTR_D17), MP_ROM_PTR(&pin_D17) }, - { MP_ROM_QSTR(MP_QSTR_D18), MP_ROM_PTR(&pin_D18) }, - { MP_ROM_QSTR(MP_QSTR_D19), MP_ROM_PTR(&pin_D19) }, - { MP_ROM_QSTR(MP_QSTR_D20), MP_ROM_PTR(&pin_D20) }, - { MP_ROM_QSTR(MP_QSTR_D21), MP_ROM_PTR(&pin_D21) }, - { MP_ROM_QSTR(MP_QSTR_D22), MP_ROM_PTR(&pin_D22) }, - { MP_ROM_QSTR(MP_QSTR_D23), MP_ROM_PTR(&pin_D23) }, - { MP_ROM_QSTR(MP_QSTR_D24), MP_ROM_PTR(&pin_D24) }, - { MP_ROM_QSTR(MP_QSTR_D25), MP_ROM_PTR(&pin_D25) }, - { MP_ROM_QSTR(MP_QSTR_D26), MP_ROM_PTR(&pin_D26) }, - { MP_ROM_QSTR(MP_QSTR_D27), MP_ROM_PTR(&pin_D27) }, - { MP_ROM_QSTR(MP_QSTR_D28), MP_ROM_PTR(&pin_D28) }, - { MP_ROM_QSTR(MP_QSTR_LED0), MP_ROM_PTR(&pin_LED0) }, - { MP_ROM_QSTR(MP_QSTR_LED1), MP_ROM_PTR(&pin_LED1) }, - { MP_ROM_QSTR(MP_QSTR_LED2), MP_ROM_PTR(&pin_LED2) }, - { MP_ROM_QSTR(MP_QSTR_LED3), MP_ROM_PTR(&pin_LED3) }, - { MP_ROM_QSTR(MP_QSTR_A0), MP_ROM_PTR(&pin_A0) }, - { MP_ROM_QSTR(MP_QSTR_A1), MP_ROM_PTR(&pin_A1) }, - { MP_ROM_QSTR(MP_QSTR_A2), MP_ROM_PTR(&pin_A2) }, - { MP_ROM_QSTR(MP_QSTR_A3), MP_ROM_PTR(&pin_A3) }, - { MP_ROM_QSTR(MP_QSTR_A4), MP_ROM_PTR(&pin_A4) }, - { MP_ROM_QSTR(MP_QSTR_A5), MP_ROM_PTR(&pin_A5) }, + { MP_ROM_QSTR(MP_QSTR_UART2_RXD), MP_ROM_PTR(&pin_UART2_RXD) }, + { MP_ROM_QSTR(MP_QSTR_UART2_TXD), MP_ROM_PTR(&pin_UART2_TXD) }, + { MP_ROM_QSTR(MP_QSTR_HIF_IRQ_OUT), MP_ROM_PTR(&pin_HIF_IRQ_OUT) }, + { MP_ROM_QSTR(MP_QSTR_PWM3), MP_ROM_PTR(&pin_PWM3) }, + { MP_ROM_QSTR(MP_QSTR_SPI2_MOSI), MP_ROM_PTR(&pin_SPI2_MOSI) }, + { MP_ROM_QSTR(MP_QSTR_PWM1), MP_ROM_PTR(&pin_PWM1) }, + { MP_ROM_QSTR(MP_QSTR_PWM0), MP_ROM_PTR(&pin_PWM0) }, + { MP_ROM_QSTR(MP_QSTR_SPI3_CS1_X), MP_ROM_PTR(&pin_SPI3_CS1_X) }, + { MP_ROM_QSTR(MP_QSTR_SPI2_MISO), MP_ROM_PTR(&pin_SPI2_MISO) }, + { MP_ROM_QSTR(MP_QSTR_PWM2), MP_ROM_PTR(&pin_PWM2) }, + { MP_ROM_QSTR(MP_QSTR_SPI4_CS_X), MP_ROM_PTR(&pin_SPI4_CS_X) }, + { MP_ROM_QSTR(MP_QSTR_SPI4_MOSI), MP_ROM_PTR(&pin_SPI4_MOSI) }, + { MP_ROM_QSTR(MP_QSTR_SPI4_MISO), MP_ROM_PTR(&pin_SPI4_MISO) }, + { MP_ROM_QSTR(MP_QSTR_SPI4_SCK), MP_ROM_PTR(&pin_SPI4_SCK) }, + { MP_ROM_QSTR(MP_QSTR_I2C0_BDT), MP_ROM_PTR(&pin_I2C0_BDT) }, + { MP_ROM_QSTR(MP_QSTR_I2C0_BCK), MP_ROM_PTR(&pin_I2C0_BCK) }, + { MP_ROM_QSTR(MP_QSTR_EMMC_DATA0), MP_ROM_PTR(&pin_EMMC_DATA0) }, + { MP_ROM_QSTR(MP_QSTR_EMMC_DATA1), MP_ROM_PTR(&pin_EMMC_DATA1) }, + { MP_ROM_QSTR(MP_QSTR_I2S0_DATA_OUT), MP_ROM_PTR(&pin_I2S0_DATA_OUT) }, + { MP_ROM_QSTR(MP_QSTR_I2S0_DATA_IN), MP_ROM_PTR(&pin_I2S0_DATA_IN) }, + { MP_ROM_QSTR(MP_QSTR_EMMC_DATA2), MP_ROM_PTR(&pin_EMMC_DATA2) }, + { MP_ROM_QSTR(MP_QSTR_EMMC_DATA3), MP_ROM_PTR(&pin_EMMC_DATA3) }, + { MP_ROM_QSTR(MP_QSTR_SEN_IRQ_IN), MP_ROM_PTR(&pin_SEN_IRQ_IN) }, + { MP_ROM_QSTR(MP_QSTR_EMMC_CLK), MP_ROM_PTR(&pin_EMMC_CLK) }, + { MP_ROM_QSTR(MP_QSTR_EMMC_CMD), MP_ROM_PTR(&pin_EMMC_CMD) }, + { MP_ROM_QSTR(MP_QSTR_I2S0_LRCK), MP_ROM_PTR(&pin_I2S0_LRCK) }, + { MP_ROM_QSTR(MP_QSTR_I2S0_BCK), MP_ROM_PTR(&pin_I2S0_BCK) }, + { MP_ROM_QSTR(MP_QSTR_UART2_CTS), MP_ROM_PTR(&pin_UART2_CTS) }, + { MP_ROM_QSTR(MP_QSTR_UART2_RTS), MP_ROM_PTR(&pin_UART2_RTS) }, + { MP_ROM_QSTR(MP_QSTR_I2S1_BCK), MP_ROM_PTR(&pin_I2S1_BCK) }, + { MP_ROM_QSTR(MP_QSTR_I2S1_LRCK), MP_ROM_PTR(&pin_I2S1_LRCK) }, + { MP_ROM_QSTR(MP_QSTR_I2S1_DATA_IN), MP_ROM_PTR(&pin_I2S1_DATA_IN) }, + { MP_ROM_QSTR(MP_QSTR_I2S1_DATA_OUT), MP_ROM_PTR(&pin_I2S1_DATA_OUT) }, + { MP_ROM_QSTR(MP_QSTR_LPADC0), MP_ROM_PTR(&pin_LPADC0) }, + { MP_ROM_QSTR(MP_QSTR_LPADC1), MP_ROM_PTR(&pin_LPADC1) }, + { MP_ROM_QSTR(MP_QSTR_LPADC2), MP_ROM_PTR(&pin_LPADC2) }, + { MP_ROM_QSTR(MP_QSTR_LPADC3), MP_ROM_PTR(&pin_LPADC3) }, + { MP_ROM_QSTR(MP_QSTR_HPADC0), MP_ROM_PTR(&pin_HPADC0) }, + { MP_ROM_QSTR(MP_QSTR_HPADC1), MP_ROM_PTR(&pin_HPADC1) }, }; MP_DEFINE_CONST_DICT(mcu_pin_globals, mcu_pin_globals_table); diff --git a/ports/cxd56/common-hal/pulseio/PWMOut.c b/ports/cxd56/common-hal/pulseio/PWMOut.c index fae49aed3..38692146c 100644 --- a/ports/cxd56/common-hal/pulseio/PWMOut.c +++ b/ports/cxd56/common-hal/pulseio/PWMOut.c @@ -40,10 +40,10 @@ typedef struct { } pwmout_dev_t; STATIC pwmout_dev_t pwmout_dev[] = { - {"/dev/pwm0", &pin_D6, -1, true}, - {"/dev/pwm1", &pin_D5, -1, true}, - {"/dev/pwm2", &pin_D9, -1, true}, - {"/dev/pwm3", &pin_D3, -1, true} + {"/dev/pwm0", &pin_PWM0, -1, true}, + {"/dev/pwm1", &pin_PWM1, -1, true}, + {"/dev/pwm2", &pin_PWM2, -1, true}, + {"/dev/pwm3", &pin_PWM3, -1, true} }; pwmout_result_t common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t *self, -- cgit v1.2.3 From c062990830891cd4193563a4a11eabbec25b1b57 Mon Sep 17 00:00:00 2001 From: Kamil Tomaszewski Date: Fri, 11 Oct 2019 11:17:28 +0200 Subject: Make Makefile work across OS --- ports/cxd56/Makefile | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/ports/cxd56/Makefile b/ports/cxd56/Makefile index 69c8e1bef..c7350a652 100644 --- a/ports/cxd56/Makefile +++ b/ports/cxd56/Makefile @@ -62,6 +62,20 @@ SPRESENSE_SDK = spresense-exported-sdk FIRMWARE = $(SPRESENSE_SDK)/firmware +# Platforms are: Linux, Darwin, MSYS, CYGWIN +PLATFORM := $(firstword $(subst _, ,$(shell uname -s 2>/dev/null))) + +ifeq ($(PLATFORM),Darwin) + # macOS + MKSPK = $(SPRESENSE_SDK)/sdk/tools/macos/mkspk +else ifeq ($(PLATFORM),Linux) + # Linux + MKSPK = $(SPRESENSE_SDK)/sdk/tools/linux/mkspk +else + # Cygwin/MSYS2 + MKSPK = $(SPRESENSE_SDK)/sdk/tools/windows/mkspk.exe +endif + INC += \ -I. \ -I../.. \ @@ -186,7 +200,7 @@ $(BUILD)/circuitpython.elf: $(BUILD)/libmpy.a $(BUILD)/circuitpython.spk: $(BUILD)/circuitpython.elf $(ECHO) "Creating $@" - $(SPRESENSE_SDK)/sdk/tools/linux/mkspk -c 2 $(BUILD)/circuitpython.elf nuttx $(BUILD)/circuitpython.spk + $(MKSPK) -c 2 $(BUILD)/circuitpython.elf nuttx $(BUILD)/circuitpython.spk flash: $(BUILD)/circuitpython.spk $(ECHO) "Writing $< to the board" -- cgit v1.2.3 From 0d139e4eca0a992137a4ec93c86aa56518ed1095 Mon Sep 17 00:00:00 2001 From: Kamil Tomaszewski Date: Fri, 11 Oct 2019 11:31:32 +0200 Subject: Add a way to change serial device name --- ports/cxd56/Makefile | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ports/cxd56/Makefile b/ports/cxd56/Makefile index c7350a652..19d8ad1e9 100644 --- a/ports/cxd56/Makefile +++ b/ports/cxd56/Makefile @@ -76,6 +76,8 @@ else MKSPK = $(SPRESENSE_SDK)/sdk/tools/windows/mkspk.exe endif +SERIAL ?= /dev/ttyUSB0 + INC += \ -I. \ -I../.. \ @@ -204,11 +206,11 @@ $(BUILD)/circuitpython.spk: $(BUILD)/circuitpython.elf flash: $(BUILD)/circuitpython.spk $(ECHO) "Writing $< to the board" - $(SPRESENSE_SDK)/sdk/tools/flash.sh -c /dev/ttyUSB0 $(BUILD)/circuitpython.spk + $(SPRESENSE_SDK)/sdk/tools/flash.sh -c $(SERIAL) $(BUILD)/circuitpython.spk flash-bootloader: $(SPRESENSE_SDK) $(FIRMWARE) $(ECHO) "Writing loader to the board" - $(SPRESENSE_SDK)/sdk/tools/flash.sh -l $(FIRMWARE) -c /dev/ttyUSB0 + $(SPRESENSE_SDK)/sdk/tools/flash.sh -l $(FIRMWARE) -c $(SERIAL) include $(TOP)/py/mkrules.mk -- cgit v1.2.3 From 5cf66dfec278ba6e34bfb3041ef98e2d23c0eb1b Mon Sep 17 00:00:00 2001 From: Kamil Tomaszewski Date: Fri, 11 Oct 2019 12:00:58 +0200 Subject: Add cxd56 to build_board_info --- tools/build_board_info.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tools/build_board_info.py b/tools/build_board_info.py index 7d0d2996c..efa481a63 100644 --- a/tools/build_board_info.py +++ b/tools/build_board_info.py @@ -12,18 +12,20 @@ from sh.contrib import git sys.path.append("adabot") import adabot.github_requests as github -SUPPORTED_PORTS = ["nrf", "atmel-samd", "stm32f4"] +SUPPORTED_PORTS = ["nrf", "atmel-samd", "stm32f4", "cxd56"] BIN = ('bin',) UF2 = ('uf2',) BIN_UF2 = ('bin', 'uf2') HEX = ('hex',) +SPK = ('spk',) # Default extensions extension_by_port = { "nrf": UF2, "atmel-samd": UF2, "stm32f4": BIN, + "cxd56": SPK, } # Per board overrides -- cgit v1.2.3 From 954c1942f671aefb60cbef29e356c2763af39d53 Mon Sep 17 00:00:00 2001 From: Kamil Tomaszewski Date: Fri, 11 Oct 2019 12:05:47 +0200 Subject: Add Spresense to build.yml --- .github/workflows/build.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 8c1f46f6f..1bf7bf9ad 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -145,6 +145,7 @@ jobs: - "sparkfun_redboard_turbo" - "sparkfun_samd21_dev" - "sparkfun_samd21_mini" + - "spresense" - "stm32f411ve_discovery" - "stm32f412zg_discovery" - "trellis_m4_express" -- cgit v1.2.3 From eb1dfd2312ea643404c8f1c9a5232a9b8353c5f4 Mon Sep 17 00:00:00 2001 From: Kamil Tomaszewski Date: Fri, 11 Oct 2019 12:11:48 +0200 Subject: Add .gitignore --- ports/cxd56/.gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 ports/cxd56/.gitignore diff --git a/ports/cxd56/.gitignore b/ports/cxd56/.gitignore new file mode 100644 index 000000000..414487d53 --- /dev/null +++ b/ports/cxd56/.gitignore @@ -0,0 +1 @@ +build-*/ -- cgit v1.2.3 From 771f43591a41dc45e3f034f3ec4ee313a5dc2922 Mon Sep 17 00:00:00 2001 From: Kamil Tomaszewski Date: Fri, 11 Oct 2019 12:57:43 +0200 Subject: Add links to serial drivers --- ports/cxd56/README.md | 48 ++++++++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/ports/cxd56/README.md b/ports/cxd56/README.md index 99a9032fe..5b3c61726 100644 --- a/ports/cxd56/README.md +++ b/ports/cxd56/README.md @@ -1,5 +1,4 @@ -CircuitPython port to Spresense -============================== +# CircuitPython port to Spresense # This directory contains the port of CircuitPython to Spresense. It is a compact development board based on Sony’s power-efficient multicore microcontroller @@ -22,8 +21,29 @@ Currently, Spresense port does not support GNSS, Audio and Multicore. Refer to [developer.sony.com/develop/spresense/](https://developer.sony.com/develop/spresense/) for further information about this board. -Build instructions ------------------- +## Prerequisites ## + +### Linux ### + +Add user to `dialout` group: + + $ sudo usermod -a -G dialout + +### Windows ### + +Download and install USB serial driver + +* [CP210x USB to serial driver for Windows 7/8/8.1](https://www.silabs.com/documents/public/software/CP210x_Windows_Drivers.zip) + +* [CP210x USB to serial driver for Windows 10](https://www.silabs.com/documents/public/software/CP210x_Universal_Windows_Driver.zip) + +### macOS ### + +Download and install USB serial driver + +* [CP210x USB to serial driver for Mac OS X](https://www.silabs.com/documents/public/software/Mac_OSX_VCP_Driver.zip) + +## Build instructions ## Pull all submodules into your clone: @@ -41,17 +61,11 @@ To build circuitpython image run: $ make BOARD=spresense -USB connection --------------- - -Add user to `dialout` group: - - $ sudo usermod -a -G dialout +## USB connection ## -Connect the Spresense main board to the PC via the USB cable. +Connect the `Spresense main board` to the PC via the USB cable. -Flash the bootloader --------------------- +## Flash the bootloader ## The correct bootloader is required for the Spresense board to function. @@ -69,17 +83,15 @@ To flash the bootloader run the command: $ make BOARD=spresense flash-bootloader -Flash the circuitpython.spk image ---------------------------------- +## Flash the circuitpython.spk image ## To flash the firmware run the command: $ make BOARD=spresense flash -Accessing the board -------------------- +## Accessing the board ## -Connect the Spresense extension board to the PC via the USB cable. +Connect the `Spresense extension board` to the PC via the USB cable. Once built and deployed, access the CircuitPython REPL (the Python prompt) via USB. You can run: -- cgit v1.2.3 From 6b9375804c6ef0c56fe3bfa83c5780317dc5f4aa Mon Sep 17 00:00:00 2001 From: Kamil Tomaszewski Date: Fri, 11 Oct 2019 17:15:25 +0200 Subject: Rename circuitpython.spk to firmware.spk --- ports/cxd56/Makefile | 14 +++++++------- ports/cxd56/README.md | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/ports/cxd56/Makefile b/ports/cxd56/Makefile index 19d8ad1e9..38ebd8305 100644 --- a/ports/cxd56/Makefile +++ b/ports/cxd56/Makefile @@ -128,7 +128,7 @@ LDFLAGS = \ -T$(SPRESENSE_SDK)/nuttx/build/ramconfig.ld \ --gc-sections \ -Map=$(BUILD)/output.map \ - -o $(BUILD)/circuitpython.elf \ + -o $(BUILD)/firmware.elf \ --start-group \ -u spresense_main \ $(BUILD)/libmpy.a \ @@ -181,7 +181,7 @@ SRC_QSTR += $(SRC_C) $(SRC_SUPERVISOR) $(SRC_COMMON_HAL_EXPANDED) $(SRC_SHARED_M # Sources that only hold QSTRs after pre-processing. SRC_QSTR_PREPROCESSOR += -all: $(BUILD)/circuitpython.spk +all: $(BUILD)/firmware.spk $(FIRMWARE): $(ECHO) "" @@ -196,17 +196,17 @@ $(BUILD)/libmpy.a: $(SPRESENSE_SDK) $(OBJ) $(ECHO) "AR $@" $(Q)$(AR) rcs $(BUILD)/libmpy.a $(OBJ) -$(BUILD)/circuitpython.elf: $(BUILD)/libmpy.a +$(BUILD)/firmware.elf: $(BUILD)/libmpy.a $(ECHO) "LD $@" $(Q)$(LD) $(LDFLAGS) -$(BUILD)/circuitpython.spk: $(BUILD)/circuitpython.elf +$(BUILD)/firmware.spk: $(BUILD)/firmware.elf $(ECHO) "Creating $@" - $(MKSPK) -c 2 $(BUILD)/circuitpython.elf nuttx $(BUILD)/circuitpython.spk + $(MKSPK) -c 2 $(BUILD)/firmware.elf nuttx $(BUILD)/firmware.spk -flash: $(BUILD)/circuitpython.spk +flash: $(BUILD)/firmware.spk $(ECHO) "Writing $< to the board" - $(SPRESENSE_SDK)/sdk/tools/flash.sh -c $(SERIAL) $(BUILD)/circuitpython.spk + $(SPRESENSE_SDK)/sdk/tools/flash.sh -c $(SERIAL) $(BUILD)/firmware.spk flash-bootloader: $(SPRESENSE_SDK) $(FIRMWARE) $(ECHO) "Writing loader to the board" diff --git a/ports/cxd56/README.md b/ports/cxd56/README.md index 5b3c61726..b98cf35f2 100644 --- a/ports/cxd56/README.md +++ b/ports/cxd56/README.md @@ -83,7 +83,7 @@ To flash the bootloader run the command: $ make BOARD=spresense flash-bootloader -## Flash the circuitpython.spk image ## +## Flash the circuitpython image ## To flash the firmware run the command: -- cgit v1.2.3 From ef459326cbd00188a047680ad2d80518cf396ea3 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Sat, 12 Oct 2019 13:57:08 +0900 Subject: nrf: PWMAudioOut: coding style --- ports/nrf/common-hal/audiopwmio/PWMAudioOut.c | 54 ++++++++++++++------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/ports/nrf/common-hal/audiopwmio/PWMAudioOut.c b/ports/nrf/common-hal/audiopwmio/PWMAudioOut.c index 0321b751c..2a284b362 100644 --- a/ports/nrf/common-hal/audiopwmio/PWMAudioOut.c +++ b/ports/nrf/common-hal/audiopwmio/PWMAudioOut.c @@ -64,23 +64,25 @@ STATIC uint32_t calculate_pwm_parameters(uint32_t sample_rate, uint32_t *top_out } STATIC void activate_audiopwmout_obj(audiopwmio_pwmaudioout_obj_t *self) { - for(size_t i=0; i < MP_ARRAY_SIZE(active_audio); i++) { - if(!active_audio[i]) { + for (size_t i=0; i < MP_ARRAY_SIZE(active_audio); i++) { + if (!active_audio[i]) { active_audio[i] = self; break; } } } STATIC void deactivate_audiopwmout_obj(audiopwmio_pwmaudioout_obj_t *self) { - for(size_t i=0; i < MP_ARRAY_SIZE(active_audio); i++) { - if(active_audio[i] == self) + for (size_t i=0; i < MP_ARRAY_SIZE(active_audio); i++) { + if (active_audio[i] == self) { active_audio[i] = NULL; + } } } void audiopwmout_reset() { - for(size_t i=0; i < MP_ARRAY_SIZE(active_audio); i++) + for (size_t i=0; i < MP_ARRAY_SIZE(active_audio); i++) { active_audio[i] = NULL; + } } STATIC void fill_buffers(audiopwmio_pwmaudioout_obj_t *self, int buf) { @@ -97,25 +99,25 @@ STATIC void fill_buffers(audiopwmio_pwmaudioout_obj_t *self, int buf) { } uint32_t num_samples = buffer_length / self->bytes_per_sample / self->spacing; - if(self->bytes_per_sample == 1) { + if (self->bytes_per_sample == 1) { uint8_t offset = self->signed_to_unsigned ? 0x80 : 0; uint16_t scale = self->scale; - for(uint32_t i=0; ispacing; i++) { + for (uint32_t i=0; ispacing; i++) { uint8_t rawval = (*buffer++ + offset); uint16_t val = (uint16_t)(((uint32_t)rawval * (uint32_t)scale) >> 8); *dev_buffer++ = val; - if(self->spacing == 1) + if (self->spacing == 1) *dev_buffer++ = val; } } else { uint16_t offset = self->signed_to_unsigned ? 0x8000 : 0; uint16_t scale = self->scale; uint16_t *buffer16 = (uint16_t*)buffer; - for(uint32_t i=0; ispacing; i++) { + for (uint32_t i=0; ispacing; i++) { uint16_t rawval = (*buffer16++ + offset); uint16_t val = (uint16_t)((rawval * (uint32_t)scale) >> 16); *dev_buffer++ = val; - if(self->spacing == 1) + if (self->spacing == 1) *dev_buffer++ = val; } } @@ -124,30 +126,30 @@ STATIC void fill_buffers(audiopwmio_pwmaudioout_obj_t *self, int buf) { if (self->loop && get_buffer_result == GET_BUFFER_DONE) { audiosample_reset_buffer(self->sample, false, 0); - } else if(get_buffer_result == GET_BUFFER_DONE) { + } else if (get_buffer_result == GET_BUFFER_DONE) { self->pwm->SHORTS = NRF_PWM_SHORT_SEQEND0_STOP_MASK | NRF_PWM_SHORT_SEQEND1_STOP_MASK; self->stopping = true; } } STATIC void audiopwmout_background_obj(audiopwmio_pwmaudioout_obj_t *self) { - if(!common_hal_audiopwmio_pwmaudioout_get_playing(self)) + if (!common_hal_audiopwmio_pwmaudioout_get_playing(self)) return; - if(self->stopping) { + if (self->stopping) { bool stopped = (self->pwm->EVENTS_SEQEND[0] || !self->pwm->EVENTS_SEQSTARTED[0]) && (self->pwm->EVENTS_SEQEND[1] || !self->pwm->EVENTS_SEQSTARTED[1]); - if(stopped) + if (stopped) self->pwm->TASKS_STOP = 1; - } else if(!self->paused && !self->single_buffer) { - if(self->pwm->EVENTS_SEQSTARTED[0]) fill_buffers(self, 1); - if(self->pwm->EVENTS_SEQSTARTED[1]) fill_buffers(self, 0); + } else if (!self->paused && !self->single_buffer) { + if (self->pwm->EVENTS_SEQSTARTED[0]) fill_buffers(self, 1); + if (self->pwm->EVENTS_SEQSTARTED[1]) fill_buffers(self, 0); } } void audiopwmout_background() { - for(size_t i=0; i < MP_ARRAY_SIZE(active_audio); i++) { - if(!active_audio[i]) continue; + for (size_t i=0; i < MP_ARRAY_SIZE(active_audio); i++) { + if (!active_audio[i]) continue; audiopwmout_background_obj(active_audio[i]); } } @@ -157,7 +159,7 @@ void common_hal_audiopwmio_pwmaudioout_construct(audiopwmio_pwmaudioout_obj_t* s assert_pin_free(left_channel); assert_pin_free(right_channel); self->pwm = pwmout_allocate(256, PWM_PRESCALER_PRESCALER_DIV_1, true, NULL, NULL); - if(!self->pwm) { + if (!self->pwm) { mp_raise_RuntimeError(translate("All timers in use")); } @@ -172,7 +174,7 @@ void common_hal_audiopwmio_pwmaudioout_construct(audiopwmio_pwmaudioout_obj_t* s self->pwm->PSEL.OUT[0] = self->left_channel_number = left_channel->number; claim_pin(left_channel); - if(right_channel) + if (right_channel) { self->pwm->PSEL.OUT[2] = self->right_channel_number = right_channel->number; claim_pin(right_channel); @@ -195,9 +197,9 @@ void common_hal_audiopwmio_pwmaudioout_deinit(audiopwmio_pwmaudioout_obj_t* self // TODO: ramp the pwm down from quiescent value to 0 self->pwm->ENABLE = 0; - if(self->left_channel_number) + if (self->left_channel_number) reset_pin_number(self->left_channel_number); - if(self->right_channel_number) + if (self->right_channel_number) reset_pin_number(self->right_channel_number); pwmout_free_channel(self->pwm, 0); @@ -230,12 +232,12 @@ void common_hal_audiopwmio_pwmaudioout_play(audiopwmio_pwmaudioout_obj_t* self, audiosample_get_buffer_structure(sample, /* single channel */ false, &self->single_buffer, &self->signed_to_unsigned, &max_buffer_length, &self->spacing); - if(max_buffer_length > UINT16_MAX) { + if (max_buffer_length > UINT16_MAX) { mp_raise_ValueError_varg(translate("Buffer length %d too big. It must be less than %d"), max_buffer_length, UINT16_MAX); } self->buffer_length = (uint16_t)max_buffer_length; self->buffers[0] = m_malloc(self->buffer_length * 2 * sizeof(uint16_t), false); - if(!self->single_buffer) + if (!self->single_buffer) self->buffers[1] = m_malloc(self->buffer_length * 2 * sizeof(uint16_t), false); @@ -274,7 +276,7 @@ void common_hal_audiopwmio_pwmaudioout_stop(audiopwmio_pwmaudioout_obj_t* self) } bool common_hal_audiopwmio_pwmaudioout_get_playing(audiopwmio_pwmaudioout_obj_t* self) { - if(!self->paused && self->pwm->EVENTS_STOPPED) { + if (!self->paused && self->pwm->EVENTS_STOPPED) { self->playing = false; self->pwm->EVENTS_STOPPED = 0; } -- cgit v1.2.3 From fae6e295460208c18bb2293c8d311ee8942e10d7 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Sat, 12 Oct 2019 14:00:04 +0900 Subject: nrf: PWMAudioOut: deactivate PWM when deinitting self .. otherwise, when an AudioPWMOut object was deinitted without being explicitly stop()ped, it would use up a slot in active_audio[]; the 5th iteration would create a non-working audio object which would just buzz instead of playing the right thing. Closes: #2203 --- ports/nrf/common-hal/audiopwmio/PWMAudioOut.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ports/nrf/common-hal/audiopwmio/PWMAudioOut.c b/ports/nrf/common-hal/audiopwmio/PWMAudioOut.c index 2a284b362..9496277b1 100644 --- a/ports/nrf/common-hal/audiopwmio/PWMAudioOut.c +++ b/ports/nrf/common-hal/audiopwmio/PWMAudioOut.c @@ -194,6 +194,8 @@ void common_hal_audiopwmio_pwmaudioout_deinit(audiopwmio_pwmaudioout_obj_t* self if (common_hal_audiopwmio_pwmaudioout_deinited(self)) { return; } + deactivate_audiopwmout_obj(self); + // TODO: ramp the pwm down from quiescent value to 0 self->pwm->ENABLE = 0; -- cgit v1.2.3 From c67b2c16619c456935dd93675b478668aeac0352 Mon Sep 17 00:00:00 2001 From: Kamil Tomaszewski Date: Mon, 14 Oct 2019 08:18:17 +0200 Subject: Return None if reference_voltage is 0.0 --- shared-bindings/analogio/AnalogIn.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/shared-bindings/analogio/AnalogIn.c b/shared-bindings/analogio/AnalogIn.c index a8bbbf59a..de48ba082 100644 --- a/shared-bindings/analogio/AnalogIn.c +++ b/shared-bindings/analogio/AnalogIn.c @@ -138,7 +138,13 @@ const mp_obj_property_t analogio_analogin_value_obj = { STATIC mp_obj_t analogio_analogin_obj_get_reference_voltage(mp_obj_t self_in) { analogio_analogin_obj_t *self = MP_OBJ_TO_PTR(self_in); check_for_deinit(self); - return mp_obj_new_float(common_hal_analogio_analogin_get_reference_voltage(self)); + + float reference_voltage = common_hal_analogio_analogin_get_reference_voltage(self); + if (reference_voltage == 0.0f) { + return mp_const_none; + } else { + return mp_obj_new_float(reference_voltage); + } } MP_DEFINE_CONST_FUN_OBJ_1(analogio_analogin_get_reference_voltage_obj, analogio_analogin_obj_get_reference_voltage); -- cgit v1.2.3 From 8dfc8b584fa962231719a489994d28897890ab37 Mon Sep 17 00:00:00 2001 From: Kamil Tomaszewski Date: Mon, 14 Oct 2019 08:19:52 +0200 Subject: Change Spresense reference_voltage to 0.0 (None) --- ports/cxd56/common-hal/analogio/AnalogIn.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/cxd56/common-hal/analogio/AnalogIn.c b/ports/cxd56/common-hal/analogio/AnalogIn.c index 6f3b7a1ba..ebe291582 100644 --- a/ports/cxd56/common-hal/analogio/AnalogIn.c +++ b/ports/cxd56/common-hal/analogio/AnalogIn.c @@ -116,7 +116,7 @@ uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) { // - Reference Voltage of A0 ~ A5 pins on External Interface board // is selected 3.3V or 5.0V by a IO Volt jumper pin. float common_hal_analogio_analogin_get_reference_voltage(analogio_analogin_obj_t *self) { - return 3.3f; + return 0.0f; } void analogin_reset(void) { -- cgit v1.2.3 From b750ed3a0fbd010b1256ab26f945a3bbf851773b Mon Sep 17 00:00:00 2001 From: Kamil Tomaszewski Date: Mon, 14 Oct 2019 08:42:10 +0200 Subject: Change Spresense firmware download link --- ports/cxd56/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/cxd56/README.md b/ports/cxd56/README.md index b98cf35f2..ec284e742 100644 --- a/ports/cxd56/README.md +++ b/ports/cxd56/README.md @@ -75,7 +75,7 @@ Bootloader information: * You have to accept the End User License Agreement to be able to download and use the Spresense bootloader binary. -Download the spresense binaries zip archive from: +Download the spresense binaries zip archive from: [Spresense firmware v1-4-000](https://developer.sony.com/file/download/download-spresense-firmware-v1-4-000) Extract spresense binaries in your PC to ports/spresense/spresense-exported-sdk/firmware/ -- cgit v1.2.3 From 150f9041e32f8d0887378d6568db1f50bf2b9317 Mon Sep 17 00:00:00 2001 From: Kamil Tomaszewski Date: Mon, 14 Oct 2019 09:05:55 +0200 Subject: Fix unsafe floating point comparison --- shared-bindings/analogio/AnalogIn.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared-bindings/analogio/AnalogIn.c b/shared-bindings/analogio/AnalogIn.c index de48ba082..9a9b525d8 100644 --- a/shared-bindings/analogio/AnalogIn.c +++ b/shared-bindings/analogio/AnalogIn.c @@ -140,7 +140,7 @@ STATIC mp_obj_t analogio_analogin_obj_get_reference_voltage(mp_obj_t self_in) { check_for_deinit(self); float reference_voltage = common_hal_analogio_analogin_get_reference_voltage(self); - if (reference_voltage == 0.0f) { + if (reference_voltage <= 0.0f) { return mp_const_none; } else { return mp_obj_new_float(reference_voltage); -- cgit v1.2.3 From 051670038eadc00c46da62f4020ee752bfab1659 Mon Sep 17 00:00:00 2001 From: Jerry Needell Date: Mon, 14 Oct 2019 07:02:32 -0400 Subject: restructure nlr.h for udefined archtectures --- py/nlr.h | 49 +++++++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/py/nlr.h b/py/nlr.h index 802f5f39a..b442aaf8a 100644 --- a/py/nlr.h +++ b/py/nlr.h @@ -36,33 +36,38 @@ // If MICROPY_NLR_SETJMP is not enabled then auto-detect the machine arch #if !defined(MICROPY_NLR_SETJMP) || !MICROPY_NLR_SETJMP -#define MICROPY_NLR_SETJMP (0) // A lot of nlr-related things need different treatment on Windows -#if defined(_WIN32) || defined(__CYGWIN__) -#define MICROPY_NLR_OS_WINDOWS 1 -#else -#define MICROPY_NLR_OS_WINDOWS 0 -#endif -#if defined(__i386__) - #define MICROPY_NLR_X86 (1) - #define MICROPY_NLR_NUM_REGS (6) -#elif defined(__x86_64__) - #define MICROPY_NLR_X64 (1) - #if MICROPY_NLR_OS_WINDOWS - #define MICROPY_NLR_NUM_REGS (10) + #if defined(_WIN32) || defined(__CYGWIN__) + #define MICROPY_NLR_OS_WINDOWS 1 #else - #define MICROPY_NLR_NUM_REGS (8) + #define MICROPY_NLR_OS_WINDOWS 0 #endif -#elif defined(__thumb2__) || defined(__thumb__) || defined(__arm__) - #define MICROPY_NLR_THUMB (1) - #define MICROPY_NLR_NUM_REGS (10) -#elif defined(__xtensa__) - #define MICROPY_NLR_XTENSA (1) - #define MICROPY_NLR_NUM_REGS (10) -#else - #define MICROPY_NLR_SETJMP (1) + #if defined(__i386__) + #define MICROPY_NLR_X86 (1) + #define MICROPY_NLR_NUM_REGS (6) + #elif defined(__x86_64__) + #define MICROPY_NLR_X64 (1) + #if MICROPY_NLR_OS_WINDOWS + #define MICROPY_NLR_NUM_REGS (10) + #else + #define MICROPY_NLR_NUM_REGS (8) + #endif + #elif defined(__thumb2__) || defined(__thumb__) || defined(__arm__) + #define MICROPY_NLR_THUMB (1) + #define MICROPY_NLR_NUM_REGS (10) + #elif defined(__xtensa__) + #define MICROPY_NLR_XTENSA (1) + #define MICROPY_NLR_NUM_REGS (10) + #else + #define MICROPY_NLR_SETJMP (1) //#warning "No native NLR support for this arch, using setjmp implementation" + #endif #endif + +// If MICROPY_NLR_SETJMP is not defined above - define/disable it here + +#if !defined(MICROPY_NLR_SETJMP) + #define MICROPY_NLR_SETJMP (0) #endif #if MICROPY_NLR_SETJMP -- cgit v1.2.3 From e0f60d0c173f889199e431de3838cd1d93376500 Mon Sep 17 00:00:00 2001 From: arturo182 Date: Mon, 14 Oct 2019 23:42:53 +0200 Subject: Add IS25LP128F flash device definition --- supervisor/shared/external_flash/devices.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/supervisor/shared/external_flash/devices.h b/supervisor/shared/external_flash/devices.h index 4de22719a..2e9dfabf5 100644 --- a/supervisor/shared/external_flash/devices.h +++ b/supervisor/shared/external_flash/devices.h @@ -443,4 +443,22 @@ typedef struct { .single_status_byte = false, \ } +// Settings for the ISSI IS25LP128F 16MiB SPI flash. +// Datasheet: http://www.issi.com/WW/pdf/25LP-WP128F.pdf +#define IS25LP128F {\ + .total_size = (1 << 24), /* 16 MiB */ \ + .start_up_time_us = 10000, \ + .manufacturer_id = 0x9d, \ + .memory_type = 0x60, \ + .capacity = 0x18, \ + .max_clock_speed_mhz = 133, \ + .quad_enable_bit_mask = 0x02, \ + .has_sector_protection = true, \ + .supports_fast_read = true, \ + .supports_qspi = true, \ + .supports_qspi_writes = true, \ + .write_status_register_split = false, \ + .single_status_byte = true, \ +} + #endif // MICROPY_INCLUDED_ATMEL_SAMD_EXTERNAL_FLASH_DEVICES_H -- cgit v1.2.3 From 9435e01f9e46ed212adf592b008f3036e16c0fbd Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Mon, 14 Oct 2019 16:05:17 -0700 Subject: Support __bytes Fixes #1763 --- py/objstr.c | 16 ++++++++++++---- tests/basics/class_bytes.py | 9 +++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 tests/basics/class_bytes.py diff --git a/py/objstr.c b/py/objstr.c index 6ef9a15b5..853868d21 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -31,6 +31,7 @@ #include "py/unicode.h" #include "py/objstr.h" #include "py/objlist.h" +#include "py/objtype.h" #include "py/runtime.h" #include "py/stackctrl.h" @@ -211,6 +212,10 @@ STATIC mp_obj_t bytes_make_new(const mp_obj_type_t *type_in, size_t n_args, cons return mp_const_empty_bytes; } + if (n_args > 1) { + goto wrong_args; + } + if (MP_OBJ_IS_STR(args[0])) { if (n_args < 2 || n_args > 3) { goto wrong_args; @@ -226,10 +231,6 @@ STATIC mp_obj_t bytes_make_new(const mp_obj_type_t *type_in, size_t n_args, cons return MP_OBJ_FROM_PTR(o); } - if (n_args > 1) { - goto wrong_args; - } - if (MP_OBJ_IS_SMALL_INT(args[0])) { mp_int_t len = MP_OBJ_SMALL_INT_VALUE(args[0]); if (len < 0) { @@ -241,6 +242,13 @@ STATIC mp_obj_t bytes_make_new(const mp_obj_type_t *type_in, size_t n_args, cons return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); } + // check if __bytes__ exists, and if so delegate to it + mp_obj_t dest[2]; + mp_load_method_maybe(args[0], MP_QSTR___bytes__, dest); + if (dest[0] != MP_OBJ_NULL) { + return mp_call_method_n_kw(0, 0, dest); + } + // check if argument has the buffer protocol mp_buffer_info_t bufinfo; if (mp_get_buffer(args[0], &bufinfo, MP_BUFFER_READ)) { diff --git a/tests/basics/class_bytes.py b/tests/basics/class_bytes.py new file mode 100644 index 000000000..75e2e7244 --- /dev/null +++ b/tests/basics/class_bytes.py @@ -0,0 +1,9 @@ +class C1: + def __init__(self, value): + self.value = value + + def __bytes__(self): + return self.value + +c1 = C1(b"class 1") +print(bytes(c1)) -- cgit v1.2.3 From 1610d06bb438961722c558bd695d6964c76cc080 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Mon, 14 Oct 2019 19:59:23 -0700 Subject: Switch arg check back to allow ignored args for strings --- py/objstr.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/py/objstr.c b/py/objstr.c index 853868d21..fde264681 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -212,10 +212,6 @@ STATIC mp_obj_t bytes_make_new(const mp_obj_type_t *type_in, size_t n_args, cons return mp_const_empty_bytes; } - if (n_args > 1) { - goto wrong_args; - } - if (MP_OBJ_IS_STR(args[0])) { if (n_args < 2 || n_args > 3) { goto wrong_args; @@ -242,6 +238,10 @@ STATIC mp_obj_t bytes_make_new(const mp_obj_type_t *type_in, size_t n_args, cons return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); } + if (n_args > 1) { + goto wrong_args; + } + // check if __bytes__ exists, and if so delegate to it mp_obj_t dest[2]; mp_load_method_maybe(args[0], MP_QSTR___bytes__, dest); -- cgit v1.2.3