Merge pull request #320 from dhalbert/master

merge 2.x changes into master; touch up Makefile CFLAGS logic
This commit is contained in:
Dan Halbert 2017-10-13 14:51:09 -04:00 committed by GitHub
commit 6e8fa8b3c9
94 changed files with 1496 additions and 52 deletions

9
.gitmodules vendored
View File

@ -24,3 +24,12 @@
path = atmel-samd/asf4 path = atmel-samd/asf4
url = https://github.com/adafruit/asf4.git url = https://github.com/adafruit/asf4.git
branch = circuitpython branch = circuitpython
[submodule "frozen/Adafruit_CircuitPython_Thermistor"]
path = frozen/Adafruit_CircuitPython_Thermistor
url = https://github.com/adafruit/Adafruit_CircuitPython_Thermistor.git
[submodule "frozen/Adafruit_CircuitPython_LIS3DH"]
path = frozen/Adafruit_CircuitPython_LIS3DH
url = https://github.com/adafruit/Adafruit_CircuitPython_LIS3DH.git
[submodule "frozen/Adafruit_CircuitPython_BusDevice"]
path = frozen/Adafruit_CircuitPython_BusDevice
url = https://github.com/adafruit/Adafruit_CircuitPython_BusDevice.git

View File

@ -80,28 +80,38 @@ BASE_CFLAGS = \
--param max-inline-insns-single=500 --param max-inline-insns-single=500
# NDEBUG disables assert() statements. This reduces code size pretty dramatically, per tannewt. # NDEBUG disables assert() statements. This reduces code size pretty dramatically, per tannewt.
ifeq ($(CHIP_FAMILY), samd21) ifeq ($(CHIP_FAMILY), samd21)
CFLAGS = -Os -DNDEBUG -finline-limit=49 CFLAGS = -Os -DNDEBUG
endif endif
ifeq ($(CHIP_FAMILY), samd51) ifeq ($(CHIP_FAMILY), samd51)
CFLAGS = -O2 -DNDEBUG CFLAGS = -O2 -DNDEBUG
endif endif
#Debugging/Optimization #Debugging/Optimization
ifeq ($(DEBUG), 1) ifeq ($(DEBUG), 1)
# Turn on Python modules useful for debugging (e.g. uheap, ustack). # Turn on Python modules useful for debugging (e.g. uheap, ustack).
CFLAGS += -ggdb CFLAGS += -ggdb
ifeq ($(CHIP_FAMILY), samd21) ifeq ($(CHIP_FAMILY), samd21)
CFLAGS += -DNDEBUG -DENABLE_MICRO_TRACE_BUFFER CFLAGS += -DENABLE_MICRO_TRACE_BUFFER
endif endif
else else
# -finline-limit can shrink the image size. -finline-limit=80 or so is similar to not having it on. # -finline-limit can shrink the image size.
# There is no simple default value, though. # -finline-limit=80 or so is similar to not having it on.
CFLAGS += -DNDEBUG -flto -finline-limit=49 # There is no simple default value, though.
ifeq ($(FLASH_IMPL),internal_flash.c)
ifeq ($(CHIP_FAMILY), samd21)
### TODO(dhalbert): disable for now; breaks things in 3.0.0 build(e.g. deinit checking is disabled)
# CFLAGS += -finline-limit=19
endif
endif
CFLAGS += -flto
endif endif
CFLAGS += $(INC) -Wall -Werror -std=gnu11 -nostdlib $(BASE_CFLAGS) $(CFLAGS_MOD) $(COPT) CFLAGS += $(INC) -Wall -Werror -std=gnu11 -nostdlib $(BASE_CFLAGS) $(CFLAGS_MOD) $(COPT)
ifeq ($(CHIP_FAMILY), samd21) ifeq ($(CHIP_FAMILY), samd21)
CFLAGS += \ CFLAGS += \
-mthumb \ -mthumb \
@ -262,7 +272,8 @@ SRC_BINDINGS_ENUMS = \
digitalio/Direction.c \ digitalio/Direction.c \
digitalio/DriveMode.c \ digitalio/DriveMode.c \
digitalio/Pull.c \ digitalio/Pull.c \
help.c help.c \
util.c
SRC_COMMON_HAL_EXPANDED = $(addprefix shared-bindings/, $(SRC_COMMON_HAL)) \ SRC_COMMON_HAL_EXPANDED = $(addprefix shared-bindings/, $(SRC_COMMON_HAL)) \
$(addprefix shared-bindings/, $(SRC_BINDINGS_ENUMS)) \ $(addprefix shared-bindings/, $(SRC_BINDINGS_ENUMS)) \
@ -274,6 +285,8 @@ SRC_SHARED_MODULE = \
bitbangio/OneWire.c \ bitbangio/OneWire.c \
bitbangio/SPI.c \ bitbangio/SPI.c \
busio/OneWire.c \ busio/OneWire.c \
gamepad/__init__.c \
gamepad/GamePad.c \
os/__init__.c \ os/__init__.c \
random/__init__.c \ random/__init__.c \
storage/__init__.c \ storage/__init__.c \

View File

@ -9,4 +9,9 @@ CHIP_VARIANT = SAMD21G18A
CHIP_FAMILY = samd21 CHIP_FAMILY = samd21
# Include these Python libraries in firmware. # Include these Python libraries in firmware.
### TODO(halbert): disable some of these frozen modules; they don't fit in 3.0.0 build while internalfs
### is in use
###FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice
###FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_LIS3DH
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_NeoPixel FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_NeoPixel
###FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_Thermistor

View File

@ -0,0 +1,51 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef MICROPY_INCLUDED_ATMEL_SAMD_BOARD_FLASH_W25Q32BV_H
#define MICROPY_INCLUDED_ATMEL_SAMD_BOARD_FLASH_W25Q32BV_H
// The total flash size in bytes.
#define SPI_FLASH_TOTAL_SIZE (1 << 21) // 2 MiB
// The size of the smallest erase unit thats erased with command 0x20.
#define SPI_FLASH_ERASE_SIZE (1 << 12) // 4 KiB
// The size of a page that is programmed with page program command 0x02.
#define SPI_FLASH_PAGE_SIZE (256) // 256 bytes
// These are the first three response bytes to the JEDEC ID command 0x9f that is
// used to confirm we're talking to the flash we expect.
#ifndef SPI_FLASH_JEDEC_MANUFACTURER
#define SPI_FLASH_JEDEC_MANUFACTURER 0xef
#define SPI_FLASH_SECTOR_PROTECTION false
#else
#define SPI_FLASH_JEDEC_MANUFACTURER_2 0xef
#define SPI_FLASH_SECTOR_PROTECTION_2 false
#endif
#define SPI_FLASH_JEDEC_MEMORY_TYPE 0x40
#define SPI_FLASH_JEDEC_CAPACITY 0x16
#endif // MICROPY_INCLUDED_ATMEL_SAMD_BOARD_FLASH_W25Q32BV_H

View File

@ -0,0 +1,37 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "boards/board.h"
void board_init(void) {
}
bool board_requests_safe_mode(void) {
return false;
}
void reset_board(void) {
}

View File

@ -0,0 +1,115 @@
/**
* \file
*
* \brief Memory access control configuration file.
*
* Copyright (c) 2014-2015 Atmel Corporation. All rights reserved.
*
* \asf_license_start
*
* \page License
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The name of Atmel may not be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* 4. This software may only be redistributed and used in connection with an
* Atmel microcontroller product.
*
* THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* \asf_license_stop
*
*/
/*
* Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
*/
#ifndef _CONF_ACCESS_H_
#define _CONF_ACCESS_H_
#include "compiler.h"
#include "board.h"
/*! \name Activation of Logical Unit Numbers
*/
//! @{
#define LUN_0 ENABLE //!< On-Chip Virtual Memory.
#define LUN_1 DISABLE //!< AT45DBX Data Flash.
#define LUN_2 DISABLE //!< SD/MMC Card over SPI.
#define LUN_3 DISABLE //!< SD/MMC Card over MCI Slot 0.
#define LUN_4 DISABLE
#define LUN_5 DISABLE
#define LUN_6 DISABLE
#define LUN_7 DISABLE
#define LUN_USB DISABLE //!< Host Mass-Storage Memory.
//! @}
/*! \name LUN 0 Definitions
*/
//! @{
#define LUN_0_INCLUDE "access_vfs.h"
#define Lun_0_test_unit_ready vfs_test_unit_ready
#define Lun_0_read_capacity vfs_read_capacity
#define Lun_0_unload NULL
#define Lun_0_wr_protect vfs_wr_protect
#define Lun_0_removal vfs_removal
#define Lun_0_usb_read_10 vfs_usb_read_10
#define Lun_0_usb_write_10 vfs_usb_write_10
#define LUN_0_NAME "\"CircuitPython VFS[0]\""
//! @}
#define MEM_USB LUN_USB
/*! \name Actions Associated with Memory Accesses
*
* Write here the action to associate with each memory access.
*
* \warning Be careful not to waste time in order not to disturb the functions.
*/
//! @{
#define memory_start_read_action(nb_sectors)
#define memory_stop_read_action()
#define memory_start_write_action(nb_sectors)
#define memory_stop_write_action()
//! @}
/*! \name Activation of Interface Features
*/
//! @{
#define ACCESS_USB true //!< MEM <-> USB interface.
#define ACCESS_MEM_TO_RAM false //!< MEM <-> RAM interface.
#define ACCESS_STREAM false //!< Streaming MEM <-> MEM interface.
#define ACCESS_STREAM_RECORD false //!< Streaming MEM <-> MEM interface in record mode.
#define ACCESS_MEM_TO_MEM false //!< MEM <-> MEM interface.
#define ACCESS_CODEC false //!< Codec interface.
//! @}
/*! \name Specific Options for Access Control
*/
//! @{
#define GLOBAL_WR_PROTECT false //!< Management of a global write protection.
//! @}
#endif // _CONF_ACCESS_H_

View File

@ -0,0 +1,14 @@
/**
* \file
*
* \brief User board configuration template
*
*/
/*
* Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
*/
#ifndef CONF_BOARD_H
#define CONF_BOARD_H
#endif // CONF_BOARD_H

View File

@ -0,0 +1 @@
#include "conf_clocks_crystalless.h"

View File

@ -0,0 +1,221 @@
#include <stdbool.h>
#include <stdint.h>
#include "asf/common/services/usb/class/cdc/usb_protocol_cdc.h"
#ifndef CONF_USB_H_INCLUDED
#define CONF_USB_H_INCLUDED
#define USB_DEVICE_MAJOR_VERSION 1
#define USB_DEVICE_MINOR_VERSION 0
#define USB_DEVICE_POWER 100 // Consumption on Vbus line (mA)
#define USB_DEVICE_ATTR \
(USB_CONFIG_ATTR_BUS_POWERED)
// (USB_CONFIG_ATTR_REMOTE_WAKEUP|USB_CONFIG_ATTR_SELF_POWERED)
// (USB_CONFIG_ATTR_REMOTE_WAKEUP|USB_CONFIG_ATTR_BUS_POWERED)
//! USB Device string definitions (Optional)
#ifndef USB_DEVICE_MANUFACTURE_NAME
# define USB_DEVICE_MANUFACTURE_NAME "Radomir Dopieralski"
#endif
#ifndef USB_DEVICE_PRODUCT_NAME
# define USB_DEVICE_PRODUCT_NAME "Trinket M0 Haxpress"
#endif
// #define USB_DEVICE_SERIAL_NAME "12...EF"
#define USB_DEVICE_GET_SERIAL_NAME_POINTER serial_number
#define USB_DEVICE_GET_SERIAL_NAME_LENGTH 32
extern char serial_number[USB_DEVICE_GET_SERIAL_NAME_LENGTH];
//! Control endpoint size
#define USB_DEVICE_EP_CTRL_SIZE 64
//! Interfaces for this device (CDC COM + CDC DATA + MSC + HID mouse + HID kbd)
#define USB_DEVICE_NB_INTERFACE 5
// (3 | USB_EP_DIR_IN) // CDC Notify endpoint
// (4 | USB_EP_DIR_IN) // CDC TX
// (5 | USB_EP_DIR_OUT) // CDC RX
// (1 | USB_EP_DIR_IN) // MSC IN
// (2 | USB_EP_DIR_OUT) // MSC OUT
// (6 | USB_EP_DIR_IN) // HID mouse report
// (7 | USB_EP_DIR_IN) // HID keyboard report
#define USB_DEVICE_MAX_EP 7
#define UDI_CDC_PORT_NB 1
#define UDI_CDC_ENABLE_EXT(port) mp_cdc_enable(port)
extern bool mp_cdc_enable(uint8_t port);
#define UDI_CDC_DISABLE_EXT(port) mp_cdc_disable(port)
extern void mp_cdc_disable(uint8_t port);
#define UDI_CDC_LOW_RATE
#define UDI_CDC_DEFAULT_RATE 115200
#define UDI_CDC_DEFAULT_STOPBITS CDC_STOP_BITS_1
#define UDI_CDC_DEFAULT_PARITY CDC_PAR_NONE
#define UDI_CDC_DEFAULT_DATABITS 8
#define UDI_CDC_RX_NOTIFY(port) usb_rx_notify()
void usb_rx_notify(void);
#define UDI_CDC_SET_CODING_EXT(port,cfg) usb_coding_notify(port, cfg)
void usb_coding_notify(uint8_t port, usb_cdc_line_coding_t* coding);
#define UDI_CDC_SET_DTR_EXT(port,set) usb_dtr_notify(port, set)
void usb_dtr_notify(uint8_t port, bool set);
#define UDI_CDC_SET_RTS_EXT(port,set) usb_rts_notify(port, set)
void usb_rts_notify(uint8_t port, bool set);
/**
* USB CDC low level configuration
* In standalone these configurations are defined by the CDC module.
* For composite device, these configuration must be defined here
* @{
*/
//! Endpoint numbers definition
#define UDI_CDC_COMM_EP_0 (3 | USB_EP_DIR_IN) // Notify endpoint
#define UDI_CDC_DATA_EP_IN_0 (4 | USB_EP_DIR_IN) // TX
#define UDI_CDC_DATA_EP_OUT_0 (5 | USB_EP_DIR_OUT) // RX
//! Interface numbers
#define UDI_CDC_COMM_IFACE_NUMBER_0 0
#define UDI_CDC_DATA_IFACE_NUMBER_0 1
/**
* Configuration of MSC interface
* @{
*/
//! Vendor name and Product version of MSC interface
#define UDI_MSC_GLOBAL_VENDOR_ID \
'A', 'T', 'M', 'E', 'L', ' ', ' ', ' '
#define UDI_MSC_GLOBAL_PRODUCT_VERSION \
'1', '.', '0', '0'
//! Interface callback definition
#define UDI_MSC_ENABLE_EXT() mp_msc_enable()
extern bool mp_msc_enable(void);
#define UDI_MSC_DISABLE_EXT() mp_msc_disable()
extern void mp_msc_disable(void);
//! Enable id string of interface to add an extra USB string
#define UDI_MSC_STRING_ID 5
/**
* USB MSC low level configuration
* In standalone these configurations are defined by the MSC module.
* For composite device, these configuration must be defined here
* @{
*/
//! Endpoint numbers definition
#define UDI_MSC_EP_IN (1 | USB_EP_DIR_IN)
#define UDI_MSC_EP_OUT (2 | USB_EP_DIR_OUT)
//! Interface number
#define UDI_MSC_IFACE_NUMBER 2
/**
* Configuration of HID Mouse interface
* @{
*/
//! Interface callback definition
#define UDI_HID_MOUSE_ENABLE_EXT() mp_mouse_enable()
extern bool mp_mouse_enable(void);
#define UDI_HID_MOUSE_DISABLE_EXT() mp_mouse_disable()
extern void mp_mouse_disable(void);
//! Enable id string of interface to add an extra USB string
#define UDI_HID_MOUSE_STRING_ID 6
/**
* USB HID Mouse low level configuration
* In standalone these configurations are defined by the HID Mouse module.
* For composite device, these configuration must be defined here
* @{
*/
//! Endpoint numbers definition
#define UDI_HID_MOUSE_EP_IN (6 | USB_EP_DIR_IN)
//! Interface number
#define UDI_HID_MOUSE_IFACE_NUMBER 3
//@}
//@}
/**
* Configuration of HID Keyboard interface
* @{
*/
//! Interface callback definition
#define UDI_HID_KBD_ENABLE_EXT() mp_keyboard_enable()
extern bool mp_keyboard_enable(void);
#define UDI_HID_KBD_DISABLE_EXT() mp_keyboard_disable()
extern void mp_keyboard_disable(void);
#define UDI_HID_KBD_CHANGE_LED(value) mp_keyboard_led(value)
extern void mp_keyboard_led(uint8_t);
//! Enable id string of interface to add an extra USB string
#define UDI_HID_KBD_STRING_ID 7
/**
* USB HID Keyboard low level configuration
* In standalone these configurations are defined by the HID Keyboard module.
* For composite device, these configuration must be defined here
* @{
*/
//! Endpoint numbers definition
#define UDI_HID_KBD_EP_IN (7 | USB_EP_DIR_IN)
//! Interface number
#define UDI_HID_KBD_IFACE_NUMBER 4
/**
* Description of Composite Device
* @{
*/
//! USB Interfaces descriptor structure
#define UDI_COMPOSITE_DESC_T \
usb_iad_desc_t udi_cdc_iad; \
udi_cdc_comm_desc_t udi_cdc_comm; \
udi_cdc_data_desc_t udi_cdc_data; \
udi_msc_desc_t udi_msc; \
udi_hid_mouse_desc_t udi_hid_mouse; \
udi_hid_kbd_desc_t udi_hid_kbd
//! USB Interfaces descriptor value for Full Speed
#define UDI_COMPOSITE_DESC_FS \
.udi_cdc_iad = UDI_CDC_IAD_DESC_0, \
.udi_cdc_comm = UDI_CDC_COMM_DESC_0, \
.udi_cdc_data = UDI_CDC_DATA_DESC_0_FS, \
.udi_msc = UDI_MSC_DESC_FS, \
.udi_hid_mouse = UDI_HID_MOUSE_DESC, \
.udi_hid_kbd = UDI_HID_KBD_DESC
//! USB Interfaces descriptor value for High Speed
#define UDI_COMPOSITE_DESC_HS \
.udi_cdc_iad = UDI_CDC_IAD_DESC_0, \
.udi_cdc_comm = UDI_CDC_COMM_DESC_0, \
.udi_cdc_data = UDI_CDC_DATA_DESC_0_HS, \
.udi_msc = UDI_MSC_DESC_HS, \
.udi_hid_mouse = UDI_HID_MOUSE_DESC, \
.udi_hid_kbd = UDI_HID_KBD_DESC
//! USB Interface APIs
#define UDI_COMPOSITE_API \
&udi_api_cdc_comm, \
&udi_api_cdc_data, \
&udi_api_msc, \
&udi_api_hid_mouse, \
&udi_api_hid_kbd
//@}
/**
* USB Device Driver Configuration
* @{
*/
//@}
//! The includes of classes and other headers must be done at the end of this file to avoid compile error
#include "udi_cdc.h"
#include "udi_msc.h"
#include "udi_hid_mouse.h"
#include "udi_hid_kbd.h"
#endif

View File

@ -0,0 +1,39 @@
#define USB_REPL
#define MICROPY_HW_BOARD_NAME "Trinket M0 Haxpress"
#define MICROPY_HW_MCU_NAME "samd21e18"
// Rev B - Black
#define MICROPY_HW_APA102_MOSI (&pin_PA00)
#define MICROPY_HW_APA102_SCK (&pin_PA01)
// Salae reads 12mhz which is the limit even though we set it to the
// safer 8mhz.
#define SPI_FLASH_BAUDRATE (8000000)
#define SPI_FLASH_MUX_SETTING SPI_SIGNAL_MUX_SETTING_D
#define SPI_FLASH_PAD0_PINMUX PINMUX_PA16D_SERCOM3_PAD0 // MOSI
#define SPI_FLASH_PAD1_PINMUX PINMUX_PA17D_SERCOM3_PAD1 // SCK
#define SPI_FLASH_PAD2_PINMUX PINMUX_UNUSED // Use default pinmux for the chip
// select since we manage it
// ourselves.
#define SPI_FLASH_PAD3_PINMUX PINMUX_PA19D_SERCOM3_PAD3 // MISO
#define SPI_FLASH_SERCOM SERCOM3
#define SPI_FLASH_CS PIN_PA11
#define MICROPY_PORT_A (PORT_PA00 | PORT_PA01 | PORT_PA11 | PORT_PA16 |\
PORT_PA17 | PORT_PA18 | PORT_PA19 | PORT_PA24 |\
PORT_PA25)
#define MICROPY_PORT_B (0)
#define MICROPY_PORT_C (0)
#define CALIBRATE_CRYSTALLESS 1
#include "spi_flash.h"
#define CIRCUITPY_INTERNAL_NVM_SIZE 256
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - CIRCUITPY_INTERNAL_NVM_SIZE)
#include "flash_W25Q32BV.h"

View File

@ -0,0 +1,8 @@
LD_FILE = boards/samd21x18-bootloader-external-flash-crystalless.ld
USB_VID = 0x239A
USB_PID = 0x801F
FLASH_IMPL = spi_flash.c
CHIP_VARIANT = SAMD21E18A
CHIP_FAMILY = samd21

View File

@ -0,0 +1,31 @@
#include "samd21_pins.h"
STATIC const mp_rom_map_elem_t board_global_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_D0), MP_ROM_PTR(&pin_PA08) },
{ MP_ROM_QSTR(MP_QSTR_A2), MP_ROM_PTR(&pin_PA08) },
{ MP_ROM_QSTR(MP_QSTR_SDA), MP_ROM_PTR(&pin_PA08) },
{ MP_ROM_QSTR(MP_QSTR_D1), MP_ROM_PTR(&pin_PA02) },
{ MP_ROM_QSTR(MP_QSTR_A0), MP_ROM_PTR(&pin_PA02) },
{ MP_ROM_QSTR(MP_QSTR_D2), MP_ROM_PTR(&pin_PA09) },
{ MP_ROM_QSTR(MP_QSTR_A1), MP_ROM_PTR(&pin_PA09) },
{ MP_ROM_QSTR(MP_QSTR_SCL), MP_ROM_PTR(&pin_PA09) },
{ MP_ROM_QSTR(MP_QSTR_MISO), MP_ROM_PTR(&pin_PA09) },
{ MP_ROM_QSTR(MP_QSTR_D4), MP_ROM_PTR(&pin_PA06) },
{ MP_ROM_QSTR(MP_QSTR_A4), MP_ROM_PTR(&pin_PA06) },
{ MP_ROM_QSTR(MP_QSTR_TX), MP_ROM_PTR(&pin_PA06) },
{ MP_ROM_QSTR(MP_QSTR_MOSI), MP_ROM_PTR(&pin_PA06) },
{ MP_ROM_QSTR(MP_QSTR_D3), MP_ROM_PTR(&pin_PA07) },
{ MP_ROM_QSTR(MP_QSTR_A3), MP_ROM_PTR(&pin_PA07) },
{ MP_ROM_QSTR(MP_QSTR_RX), MP_ROM_PTR(&pin_PA07) },
{ MP_ROM_QSTR(MP_QSTR_SCK), MP_ROM_PTR(&pin_PA07) },
{ MP_ROM_QSTR(MP_QSTR_D13),MP_ROM_PTR(&pin_PA10) },
{ MP_ROM_QSTR(MP_QSTR_APA102_MOSI), MP_ROM_PTR(&pin_PA00) },
{ MP_ROM_QSTR(MP_QSTR_APA102_SCK), MP_ROM_PTR(&pin_PA01) },
};
MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table);

View File

@ -77,7 +77,14 @@ void common_hal_analogio_analogin_construct(analogio_analogin_obj_t* self,
active_channel_count++; active_channel_count++;
} }
bool common_hal_analogio_analogin_deinited(analogio_analogin_obj_t *self) {
return self->pin == mp_const_none;
}
void common_hal_analogio_analogin_deinit(analogio_analogin_obj_t *self) { void common_hal_analogio_analogin_deinit(analogio_analogin_obj_t *self) {
if (common_hal_analogio_analogin_deinited(self)) {
return;
}
active_channel_count--; active_channel_count--;
if (active_channel_count == 0) { if (active_channel_count == 0) {
adc_reset(adc_instance); adc_reset(adc_instance);
@ -89,6 +96,7 @@ void common_hal_analogio_analogin_deinit(analogio_analogin_obj_t *self) {
config_adc = NULL; config_adc = NULL;
} }
reset_pin(self->pin->pin); reset_pin(self->pin->pin);
self->pin = mp_const_none;
} }
void analogin_reset() { void analogin_reset() {

View File

@ -59,10 +59,18 @@ void common_hal_analogio_analogout_construct(analogio_analogout_obj_t* self,
dac_enable(&self->dac_instance); dac_enable(&self->dac_instance);
} }
bool common_hal_analogio_analogout_deinited(analogio_analogout_obj_t *self) {
return self->deinited;
}
void common_hal_analogio_analogout_deinit(analogio_analogout_obj_t *self) { void common_hal_analogio_analogout_deinit(analogio_analogout_obj_t *self) {
if (common_hal_analogio_analogout_deinited(self)) {
return;
}
dac_disable(&self->dac_instance); dac_disable(&self->dac_instance);
dac_chan_disable(&self->dac_instance, DAC_CHANNEL_0); dac_chan_disable(&self->dac_instance, DAC_CHANNEL_0);
reset_pin(PIN_PA02); reset_pin(PIN_PA02);
self->deinited = true;
} }
void common_hal_analogio_analogout_set_value(analogio_analogout_obj_t *self, void common_hal_analogio_analogout_set_value(analogio_analogout_obj_t *self,

View File

@ -36,6 +36,7 @@
typedef struct { typedef struct {
mp_obj_base_t base; mp_obj_base_t base;
struct dac_module dac_instance; struct dac_module dac_instance;
bool deinited;
} analogio_analogout_obj_t; } analogio_analogout_obj_t;
#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ANALOGIO_ANALOGOUT_H #endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ANALOGIO_ANALOGOUT_H

View File

@ -145,9 +145,20 @@ void common_hal_audiobusio_pdmin_construct(audiobusio_pdmin_obj_t* self,
self->bit_depth = bit_depth; self->bit_depth = bit_depth;
} }
bool common_hal_audiobusio_pdmin_deinited(audiobusio_pdmin_obj_t* self) {
return self->clock_pin == mp_const_none;
}
void common_hal_audiobusio_pdmin_deinit(audiobusio_pdmin_obj_t* self) { void common_hal_audiobusio_pdmin_deinit(audiobusio_pdmin_obj_t* self) {
if (common_hal_audiobusio_pdmin_deinited(self)) {
return;
}
i2s_disable(&self->i2s_instance); i2s_disable(&self->i2s_instance);
i2s_reset(&self->i2s_instance); i2s_reset(&self->i2s_instance);
reset_pin(self->clock_pin->pin);
reset_pin(self->data_pin->pin);
self->clock_pin = mp_const_none;
self->data_pin = mp_const_none;
} }
uint8_t common_hal_audiobusio_pdmin_get_bit_depth(audiobusio_pdmin_obj_t* self) { uint8_t common_hal_audiobusio_pdmin_get_bit_depth(audiobusio_pdmin_obj_t* self) {

View File

@ -381,7 +381,14 @@ void common_hal_audioio_audioout_construct_from_file(audioio_audioout_obj_t* sel
} }
} }
bool common_hal_audioio_audioout_deinited(audioio_audioout_obj_t* self) {
return self->pin == mp_const_none;
}
void common_hal_audioio_audioout_deinit(audioio_audioout_obj_t* self) { void common_hal_audioio_audioout_deinit(audioio_audioout_obj_t* self) {
if (common_hal_audioio_audioout_deinited(self)) {
return;
}
refcount--; refcount--;
if (refcount == 0) { if (refcount == 0) {
if (MP_STATE_VM(audioout_sample_timer) != NULL) { if (MP_STATE_VM(audioout_sample_timer) != NULL) {
@ -407,6 +414,8 @@ void common_hal_audioio_audioout_deinit(audioio_audioout_obj_t* self) {
} }
reset_pin(self->pin->pin); reset_pin(self->pin->pin);
} }
self->pin = mp_const_none;
} }
static void set_timer_frequency(uint32_t frequency) { static void set_timer_frequency(uint32_t frequency) {

View File

@ -95,10 +95,19 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
i2c_master_enable(&self->i2c_master_instance); i2c_master_enable(&self->i2c_master_instance);
} }
bool common_hal_busio_i2c_deinited(busio_i2c_obj_t *self) {
return self->sda_pin == NO_PIN;
}
void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) { void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) {
if (common_hal_busio_i2c_deinited(self)) {
return;
}
i2c_master_reset(&self->i2c_master_instance); i2c_master_reset(&self->i2c_master_instance);
reset_pin(self->sda_pin); reset_pin(self->sda_pin);
reset_pin(self->scl_pin); reset_pin(self->scl_pin);
self->sda_pin = NO_PIN;
self->scl_pin = NO_PIN;
} }
bool common_hal_busio_i2c_probe(busio_i2c_obj_t *self, uint8_t addr) { bool common_hal_busio_i2c_probe(busio_i2c_obj_t *self, uint8_t addr) {

View File

@ -159,11 +159,19 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
spi_enable(&self->spi_master_instance); spi_enable(&self->spi_master_instance);
} }
bool common_hal_busio_spi_deinited(busio_spi_obj_t *self) {
return self->clock_pin == NO_PIN;
}
void common_hal_busio_spi_deinit(busio_spi_obj_t *self) { void common_hal_busio_spi_deinit(busio_spi_obj_t *self) {
if (common_hal_busio_spi_deinited(self)) {
return;
}
spi_disable(&self->spi_master_instance); spi_disable(&self->spi_master_instance);
reset_pin(self->clock_pin); reset_pin(self->clock_pin);
reset_pin(self->MOSI_pin); reset_pin(self->MOSI_pin);
reset_pin(self->MISO_pin); reset_pin(self->MISO_pin);
self->clock_pin = NO_PIN;
} }
bool common_hal_busio_spi_configure(busio_spi_obj_t *self, bool common_hal_busio_spi_configure(busio_spi_obj_t *self,

View File

@ -243,7 +243,14 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
self->uart_instance.hw->USART.INTENSET.bit.RXC = true; self->uart_instance.hw->USART.INTENSET.bit.RXC = true;
} }
bool common_hal_busio_uart_deinited(busio_uart_obj_t *self) {
return self->rx_pin == NO_PIN && self->tx_pin == NO_PIN;
}
void common_hal_busio_uart_deinit(busio_uart_obj_t *self) { void common_hal_busio_uart_deinit(busio_uart_obj_t *self) {
if (common_hal_busio_uart_deinited(self)) {
return;
}
self->uart_instance.hw->USART.INTENCLR.bit.RXC = true; self->uart_instance.hw->USART.INTENCLR.bit.RXC = true;
uint8_t instance_index = _sercom_get_sercom_inst_index(self->uart_instance.hw); uint8_t instance_index = _sercom_get_sercom_inst_index(self->uart_instance.hw);
@ -256,6 +263,8 @@ void common_hal_busio_uart_deinit(busio_uart_obj_t *self) {
usart_disable(&self->uart_instance); usart_disable(&self->uart_instance);
reset_pin(self->rx_pin); reset_pin(self->rx_pin);
reset_pin(self->tx_pin); reset_pin(self->tx_pin);
self->rx_pin = NO_PIN;
self->tx_pin = NO_PIN;
} }
// Read characters. // Read characters.

View File

@ -46,8 +46,16 @@ digitalinout_result_t common_hal_digitalio_digitalinout_construct(
return DIGITALINOUT_OK; return DIGITALINOUT_OK;
} }
bool common_hal_digitalio_digitalinout_deinited(digitalio_digitalinout_obj_t* self) {
return self->pin == mp_const_none;
}
void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t* self) { void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t* self) {
if (common_hal_digitalio_digitalinout_deinited(self)) {
return;
}
reset_pin(self->pin->pin); reset_pin(self->pin->pin);
self->pin = mp_const_none;
} }
void common_hal_digitalio_digitalinout_switch_to_input( void common_hal_digitalio_digitalinout_switch_to_input(

View File

@ -238,7 +238,14 @@ void common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self,
common_hal_pulseio_pwmout_set_duty_cycle(self, duty); common_hal_pulseio_pwmout_set_duty_cycle(self, duty);
} }
extern void common_hal_pulseio_pwmout_deinit(pulseio_pwmout_obj_t* self) { bool common_hal_pulseio_pwmout_deinited(pulseio_pwmout_obj_t* self) {
return self->pin == mp_const_none;
}
void common_hal_pulseio_pwmout_deinit(pulseio_pwmout_obj_t* self) {
if (common_hal_pulseio_pwmout_deinited(self)) {
return;
}
const pin_timer_t* t = self->timer; const pin_timer_t* t = self->timer;
uint8_t index = (((uint32_t) t->tcc) - ((uint32_t) TCC0)) / 0x400; uint8_t index = (((uint32_t) t->tcc) - ((uint32_t) TCC0)) / 0x400;
timer_refcount[index]--; timer_refcount[index]--;
@ -260,6 +267,7 @@ extern void common_hal_pulseio_pwmout_deinit(pulseio_pwmout_obj_t* self) {
} }
} }
reset_pin(self->pin->pin); reset_pin(self->pin->pin);
self->pin = mp_const_none;
} }
extern void common_hal_pulseio_pwmout_set_duty_cycle(pulseio_pwmout_obj_t* self, uint16_t duty) { extern void common_hal_pulseio_pwmout_set_duty_cycle(pulseio_pwmout_obj_t* self, uint16_t duty) {

View File

@ -152,10 +152,18 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self,
extint_chan_enable_callback(self->channel, EXTINT_CALLBACK_TYPE_DETECT); extint_chan_enable_callback(self->channel, EXTINT_CALLBACK_TYPE_DETECT);
} }
bool common_hal_pulseio_pulsein_deinited(pulseio_pulsein_obj_t* self) {
return self->pin == NO_PIN;
}
void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t* self) { void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t* self) {
if (common_hal_pulseio_pulsein_deinited(self)) {
return;
}
extint_chan_disable_callback(self->channel, EXTINT_CALLBACK_TYPE_DETECT); extint_chan_disable_callback(self->channel, EXTINT_CALLBACK_TYPE_DETECT);
active_pulseins[self->channel] = NULL; active_pulseins[self->channel] = NULL;
reset_pin(self->pin); reset_pin(self->pin);
self->pin = NO_PIN;
} }
void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t* self) { void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t* self) {

View File

@ -34,6 +34,7 @@
#include "mpconfigport.h" #include "mpconfigport.h"
#include "py/gc.h" #include "py/gc.h"
#include "py/runtime.h" #include "py/runtime.h"
#include "samd21_pins.h"
#include "shared-bindings/pulseio/PulseOut.h" #include "shared-bindings/pulseio/PulseOut.h"
#undef ENABLE #undef ENABLE
@ -128,7 +129,14 @@ void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self,
turn_off(self->pincfg); turn_off(self->pincfg);
} }
bool common_hal_pulseio_pulseout_deinited(pulseio_pulseout_obj_t* self) {
return self->pin == NO_PIN;
}
void common_hal_pulseio_pulseout_deinit(pulseio_pulseout_obj_t* self) { void common_hal_pulseio_pulseout_deinit(pulseio_pulseout_obj_t* self) {
if (common_hal_pulseio_pulseout_deinited(self)) {
return;
}
PortGroup *const port_base = port_get_group_from_gpio_pin(self->pin); PortGroup *const port_base = port_get_group_from_gpio_pin(self->pin);
port_base->DIRCLR.reg = 1 << (self->pin % 32); port_base->DIRCLR.reg = 1 << (self->pin % 32);
@ -140,6 +148,7 @@ void common_hal_pulseio_pulseout_deinit(pulseio_pulseout_obj_t* self) {
gc_free(MP_STATE_VM(pulseout_tc_instance)); gc_free(MP_STATE_VM(pulseout_tc_instance));
MP_STATE_VM(pulseout_tc_instance) = NULL; MP_STATE_VM(pulseout_tc_instance) = NULL;
} }
self->pin = NO_PIN;
} }
void common_hal_pulseio_pulseout_send(pulseio_pulseout_obj_t* self, uint16_t* pulses, uint16_t length) { void common_hal_pulseio_pulseout_send(pulseio_pulseout_obj_t* self, uint16_t* pulses, uint16_t length) {

View File

@ -33,6 +33,7 @@
#include "py/mphal.h" #include "py/mphal.h"
#include "shared-bindings/touchio/TouchIn.h" #include "shared-bindings/touchio/TouchIn.h"
#include "samd21_pins.h"
#include "tick.h" #include "tick.h"
#include "adafruit_ptc.h" #include "adafruit_ptc.h"
@ -71,12 +72,27 @@ void common_hal_touchio_touchin_construct(touchio_touchin_obj_t* self,
adafruit_ptc_init(PTC, &self->config); adafruit_ptc_init(PTC, &self->config);
self->threshold = 2 * get_raw_reading(self); // Initial values for pins will vary, depending on what peripherals the pins
// share on-chip.
//
// Set a "touched" threshold not too far above the initial value.
// For simple finger touch, the values may vary as much as a factor of two,
// but for touches using fruit or other objects, the difference is much less.
self->threshold = get_raw_reading(self) + 100;
}
bool common_hal_touchio_touchin_deinited(touchio_touchin_obj_t* self) {
return self->config.pin == NO_PIN;
} }
void common_hal_touchio_touchin_deinit(touchio_touchin_obj_t* self) { void common_hal_touchio_touchin_deinit(touchio_touchin_obj_t* self) {
// TODO(tannewt): Reset the PTC. // TODO(tannewt): Reset the PTC.
reset_pin(self->pin->pin); if (common_hal_touchio_touchin_deinited(self)) {
return;
}
reset_pin(self->config.pin);
self->config.pin = NO_PIN;
} }
void touchin_reset() { void touchin_reset() {
@ -87,3 +103,15 @@ bool common_hal_touchio_touchin_get_value(touchio_touchin_obj_t *self) {
uint16_t reading = get_raw_reading(self); uint16_t reading = get_raw_reading(self);
return reading > self->threshold; return reading > self->threshold;
} }
uint16_t common_hal_touchio_touchin_get_raw_value(touchio_touchin_obj_t *self) {
return get_raw_reading(self);
}
uint16_t common_hal_touchio_touchin_get_threshold(touchio_touchin_obj_t *self) {
return self->threshold;
}
void common_hal_touchio_touchin_set_threshold(touchio_touchin_obj_t *self, uint16_t new_threshold) {
self->threshold = new_threshold;
}

View File

@ -36,7 +36,6 @@
typedef struct { typedef struct {
mp_obj_base_t base; mp_obj_base_t base;
const mcu_pin_obj_t * pin;
struct adafruit_ptc_config config; struct adafruit_ptc_config config;
uint16_t threshold; uint16_t threshold;
} touchio_touchin_obj_t; } touchio_touchin_obj_t;

View File

@ -163,6 +163,7 @@ extern const struct _mp_obj_module_t neopixel_write_module;
extern const struct _mp_obj_module_t uheap_module; extern const struct _mp_obj_module_t uheap_module;
extern const struct _mp_obj_module_t ustack_module; extern const struct _mp_obj_module_t ustack_module;
extern const struct _mp_obj_module_t samd_module; extern const struct _mp_obj_module_t samd_module;
extern const struct _mp_obj_module_t gamepad_module;
extern const struct _mp_obj_module_t touchio_module; extern const struct _mp_obj_module_t touchio_module;
extern const struct _mp_obj_module_t usb_hid_module; extern const struct _mp_obj_module_t usb_hid_module;
@ -182,6 +183,8 @@ extern const struct _mp_obj_module_t usb_hid_module;
#define MICROPY_PY_ARRAY_SLICE_ASSIGN (1) #define MICROPY_PY_ARRAY_SLICE_ASSIGN (1)
#define MICROPY_PY_SYS_MAXSIZE (1) #define MICROPY_PY_SYS_MAXSIZE (1)
#define MICROPY_CPYTHON_COMPAT (1) #define MICROPY_CPYTHON_COMPAT (1)
// Scan gamepad every 32ms
#define CIRCUITPY_GAMEPAD_TICKS 0x1f
#define EXTRA_BUILTIN_MODULES #define EXTRA_BUILTIN_MODULES
// { MP_OBJ_NEW_QSTR(MP_QSTR_audioio), (mp_obj_t)&audioio_module }, // { MP_OBJ_NEW_QSTR(MP_QSTR_audioio), (mp_obj_t)&audioio_module },
@ -203,6 +206,7 @@ extern const struct _mp_obj_module_t usb_hid_module;
// { MP_OBJ_NEW_QSTR(MP_QSTR_touchio), (mp_obj_t)&touchio_module }, // { MP_OBJ_NEW_QSTR(MP_QSTR_touchio), (mp_obj_t)&touchio_module },
// { MP_OBJ_NEW_QSTR(MP_QSTR_analogio), (mp_obj_t)&analogio_module }, // { MP_OBJ_NEW_QSTR(MP_QSTR_analogio), (mp_obj_t)&analogio_module },
// { MP_OBJ_NEW_QSTR(MP_QSTR_busio), (mp_obj_t)&busio_module }, // { MP_OBJ_NEW_QSTR(MP_QSTR_busio), (mp_obj_t)&busio_module },
// { MP_OBJ_NEW_QSTR(MP_QSTR_gamepad),(mp_obj_t)&gamepad_module },
// { MP_OBJ_NEW_QSTR(MP_QSTR_neopixel_write),(mp_obj_t)&neopixel_write_module }, // { MP_OBJ_NEW_QSTR(MP_QSTR_neopixel_write),(mp_obj_t)&neopixel_write_module },
// { MP_OBJ_NEW_QSTR(MP_QSTR_usb_hid),(mp_obj_t)&usb_hid_module }, // { MP_OBJ_NEW_QSTR(MP_QSTR_usb_hid),(mp_obj_t)&usb_hid_module },
// { MP_OBJ_NEW_QSTR(MP_QSTR_storage), (mp_obj_t)&storage_module }, // { MP_OBJ_NEW_QSTR(MP_QSTR_storage), (mp_obj_t)&storage_module },

View File

@ -44,7 +44,7 @@ mp_vfs_mount_t mp_vfs_mount_flash;
// we don't make this function static because it needs a lot of stack and we // we don't make this function static because it needs a lot of stack and we
// want it to be executed without using stack within main() function // want it to be executed without using stack within main() function
void filesystem_init(void) { void filesystem_init(bool create_allowed) {
// init the vfs object // init the vfs object
fs_user_mount_t *vfs_fat = &fs_user_mount_flash; fs_user_mount_t *vfs_fat = &fs_user_mount_flash;
vfs_fat->flags = 0; vfs_fat->flags = 0;
@ -53,7 +53,7 @@ void filesystem_init(void) {
// try to mount the flash // try to mount the flash
FRESULT res = f_mount(&vfs_fat->fatfs); FRESULT res = f_mount(&vfs_fat->fatfs);
if (res == FR_NO_FILESYSTEM) { if (res == FR_NO_FILESYSTEM && create_allowed) {
// no filesystem so create a fresh one // no filesystem so create a fresh one
uint8_t working_buf[_MAX_SS]; uint8_t working_buf[_MAX_SS];
res = f_mkfs(&vfs_fat->fatfs, FM_FAT, 0, working_buf, sizeof(working_buf)); res = f_mkfs(&vfs_fat->fatfs, FM_FAT, 0, working_buf, sizeof(working_buf));

View File

@ -29,6 +29,7 @@
#include "peripheral_clk_config.h" #include "peripheral_clk_config.h"
#include "supervisor/shared/autoreload.h" #include "supervisor/shared/autoreload.h"
#include "shared-module/gamepad/__init__.h"
#include "shared-bindings/microcontroller/Processor.h" #include "shared-bindings/microcontroller/Processor.h"
// Global millisecond tick count // Global millisecond tick count
@ -42,6 +43,11 @@ void SysTick_Handler(void) {
#ifdef CIRCUITPY_AUTORELOAD_DELAY_MS #ifdef CIRCUITPY_AUTORELOAD_DELAY_MS
autoreload_tick(); autoreload_tick();
#endif #endif
#ifdef CIRCUITPY_GAMEPAD_TICKS
if (!(ticks_ms & CIRCUITPY_GAMEPAD_TICKS)) {
gamepad_tick();
}
#endif
} }
void tick_init() { void tick_init() {

View File

@ -121,12 +121,14 @@ SRC_COMMON_HAL = \
time/__init__.c \ time/__init__.c \
board/__init__.c board/__init__.c
# These don't have corresponding files in each port but are still located in # These don't have corresponding files in each port but are still located in
# shared-bindings to make it clear what the contents of the modules are. # shared-bindings to make it clear what the contents of the modules are.
SRC_BINDINGS_ENUMS = \ SRC_BINDINGS_ENUMS = \
digitalio/Direction.c \ digitalio/Direction.c \
digitalio/DriveMode.c \ digitalio/DriveMode.c \
digitalio/Pull.c digitalio/Pull.c \
util.c
SRC_COMMON_HAL_EXPANDED = $(addprefix shared-bindings/, $(SRC_COMMON_HAL)) \ SRC_COMMON_HAL_EXPANDED = $(addprefix shared-bindings/, $(SRC_COMMON_HAL)) \
$(addprefix shared-bindings/, $(SRC_BINDINGS_ENUMS)) \ $(addprefix shared-bindings/, $(SRC_BINDINGS_ENUMS)) \
@ -141,7 +143,7 @@ SRC_SHARED_MODULE = \
multiterminal/__init__.c \ multiterminal/__init__.c \
os/__init__.c \ os/__init__.c \
random/__init__.c \ random/__init__.c \
storage/__init__.c \ storage/__init__.c
SRC_SHARED_MODULE_EXPANDED = $(addprefix shared-bindings/, $(SRC_SHARED_MODULE)) \ SRC_SHARED_MODULE_EXPANDED = $(addprefix shared-bindings/, $(SRC_SHARED_MODULE)) \
$(addprefix shared-module/, $(SRC_SHARED_MODULE)) $(addprefix shared-module/, $(SRC_SHARED_MODULE))

View File

@ -46,8 +46,16 @@ void common_hal_analogio_analogin_construct(analogio_analogin_obj_t* self,
adc_in_use = true; adc_in_use = true;
} }
bool common_hal_analogio_analogin_deinited(analogio_analogin_obj_t* self) {
return self->deinited;
}
void common_hal_analogio_analogin_deinit(analogio_analogin_obj_t* self) { void common_hal_analogio_analogin_deinit(analogio_analogin_obj_t* self) {
if (common_hal_analogio_analogin_deinited(self)) {
return;
}
adc_in_use = false; adc_in_use = false;
self->deinited = true;
} }
uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) { uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) {

View File

@ -33,7 +33,7 @@
typedef struct { typedef struct {
mp_obj_base_t base; mp_obj_base_t base;
const mcu_pin_obj_t * pin; bool deinited;
} analogio_analogin_obj_t; } analogio_analogin_obj_t;
#endif // MICROPY_INCLUDED_ESP8266_COMMON_HAL_ANALOGIO_ANALOGIN_H #endif // MICROPY_INCLUDED_ESP8266_COMMON_HAL_ANALOGIO_ANALOGIN_H

View File

@ -38,6 +38,10 @@ void common_hal_analogio_analogout_construct(analogio_analogout_obj_t* self,
"No hardware support for analog out.")); "No hardware support for analog out."));
} }
bool common_hal_analogio_analogout_deinited(analogio_analogout_obj_t *self) {
return true;
}
void common_hal_analogio_analogout_deinit(analogio_analogout_obj_t *self) { void common_hal_analogio_analogout_deinit(analogio_analogout_obj_t *self) {
} }

View File

@ -74,7 +74,15 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
CLEAR_PERI_REG_MASK(SPI_USER(HSPI), SPI_FLASH_MODE); CLEAR_PERI_REG_MASK(SPI_USER(HSPI), SPI_FLASH_MODE);
} }
bool common_hal_busio_spi_deinited(busio_spi_obj_t *self) {
return self->deinited;
}
void common_hal_busio_spi_deinit(busio_spi_obj_t *self) { void common_hal_busio_spi_deinit(busio_spi_obj_t *self) {
if (common_hal_busio_spi_deinited(self)) {
return;
}
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, 0); PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, 0);
PIN_PULLUP_DIS(PERIPHS_IO_MUX_MTDI_U); PIN_PULLUP_DIS(PERIPHS_IO_MUX_MTDI_U);
@ -86,6 +94,8 @@ void common_hal_busio_spi_deinit(busio_spi_obj_t *self) {
// Turn off outputs 12 - 14. // Turn off outputs 12 - 14.
gpio_output_set(0x0, 0x0, 0x0, 0x7 << 12); gpio_output_set(0x0, 0x0, 0x0, 0x7 << 12);
self->deinited = true;
} }
bool common_hal_busio_spi_configure(busio_spi_obj_t *self, bool common_hal_busio_spi_configure(busio_spi_obj_t *self,

View File

@ -34,6 +34,7 @@
typedef struct { typedef struct {
mp_obj_base_t base; mp_obj_base_t base;
bool locked; bool locked;
bool deinited;
} busio_spi_obj_t; } busio_spi_obj_t;
#endif // MICROPY_INCLUDED_ESP8266_COMMON_HAL_BUSIO_SPI_H #endif // MICROPY_INCLUDED_ESP8266_COMMON_HAL_BUSIO_SPI_H

View File

@ -92,8 +92,16 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
uart_setup(UART1); uart_setup(UART1);
} }
bool common_hal_busio_uart_deinited(busio_uart_obj_t *self) {
return self->deinited;
}
void common_hal_busio_uart_deinit(busio_uart_obj_t *self) { void common_hal_busio_uart_deinit(busio_uart_obj_t *self) {
if (common_hal_busio_uart_deinited(self)) {
return;
}
PIN_FUNC_SELECT(FUNC_U1TXD_BK, 0); PIN_FUNC_SELECT(FUNC_U1TXD_BK, 0);
self->deinited = true;
} }
size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t len, int *errcode) { size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t len, int *errcode) {

View File

@ -32,7 +32,8 @@
#include "py/obj.h" #include "py/obj.h"
typedef struct { typedef struct {
mp_obj_base_t base; mp_obj_base_t base;
bool deinited;
} busio_uart_obj_t; } busio_uart_obj_t;
#endif // MICROPY_INCLUDED_ESP8266_COMMON_HAL_BUSIO_UART_H #endif // MICROPY_INCLUDED_ESP8266_COMMON_HAL_BUSIO_UART_H

View File

@ -41,13 +41,21 @@ digitalinout_result_t common_hal_digitalio_digitalinout_construct(
return DIGITALINOUT_OK; return DIGITALINOUT_OK;
} }
bool common_hal_digitalio_digitalinout_deinited(digitalio_digitalinout_obj_t* self) {
return self->pin == mp_const_none;
}
void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t* self) { void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t* self) {
if (common_hal_digitalio_digitalinout_deinited(self)) {
return;
}
if (self->pin->gpio_number < 16) { if (self->pin->gpio_number < 16) {
uint32_t pin_mask = 1 << self->pin->gpio_number; uint32_t pin_mask = 1 << self->pin->gpio_number;
gpio_output_set(0x0, 0x0, 0x0, pin_mask); gpio_output_set(0x0, 0x0, 0x0, pin_mask);
PIN_FUNC_SELECT(self->pin->peripheral, 0); PIN_FUNC_SELECT(self->pin->peripheral, 0);
PIN_PULLUP_DIS(self->pin->peripheral); PIN_PULLUP_DIS(self->pin->peripheral);
} }
self->pin = mp_const_none;
} }
void common_hal_digitalio_digitalinout_switch_to_input( void common_hal_digitalio_digitalinout_switch_to_input(

View File

@ -77,7 +77,14 @@ void common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self, const mcu_p
} }
} }
extern void common_hal_pulseio_pwmout_deinit(pulseio_pwmout_obj_t* self) { bool common_hal_pulseio_pwmout_deinited(pulseio_pwmout_obj_t* self) {
return self->pin == mp_const_none;
}
void common_hal_pulseio_pwmout_deinit(pulseio_pwmout_obj_t* self) {
if (common_hal_pulseio_pwmout_deinited(self)) {
return;
}
pwm_delete(self->channel); pwm_delete(self->channel);
pwm_start(); pwm_start();
if (self->pin->gpio_number < 16) { if (self->pin->gpio_number < 16) {
@ -86,9 +93,10 @@ extern void common_hal_pulseio_pwmout_deinit(pulseio_pwmout_obj_t* self) {
PIN_FUNC_SELECT(self->pin->peripheral, 0); PIN_FUNC_SELECT(self->pin->peripheral, 0);
PIN_PULLUP_DIS(self->pin->peripheral); PIN_PULLUP_DIS(self->pin->peripheral);
} }
self->pin = mp_const_none;
} }
extern void common_hal_pulseio_pwmout_set_duty_cycle(pulseio_pwmout_obj_t* self, uint16_t duty) { void common_hal_pulseio_pwmout_set_duty_cycle(pulseio_pwmout_obj_t* self, uint16_t duty) {
// We get 16 bits of duty in but the underlying code is only ten bit. // We get 16 bits of duty in but the underlying code is only ten bit.
pwm_set_duty(duty >> 6, self->channel); pwm_set_duty(duty >> 6, self->channel);
pwm_start(); pwm_start();

View File

@ -37,8 +37,11 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self,
mp_raise_NotImplementedError(""); mp_raise_NotImplementedError("");
} }
void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t* self) { bool common_hal_pulseio_pulsein_deinited(pulseio_pulsein_obj_t* self) {
return true;
}
void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t* self) {
} }
void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t* self) { void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t* self) {

View File

@ -35,6 +35,10 @@ void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self,
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "No hardware support for PulseOut.")); nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "No hardware support for PulseOut."));
} }
bool common_hal_pulseio_pulseout_deinited(pulseio_pulseout_obj_t* self) {
return true;
}
void common_hal_pulseio_pulseout_deinit(pulseio_pulseout_obj_t* self) { void common_hal_pulseio_pulseout_deinit(pulseio_pulseout_obj_t* self) {
} }

@ -0,0 +1 @@
Subproject commit 39f28ed4e0e5a06fc33fd01c1efc8c52c4140f03

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

@ -0,0 +1 @@
Subproject commit 2d57c0ba9a09d6d30f0ae2b98aba9567c25e6fb6

5
main.c
View File

@ -219,7 +219,10 @@ int __attribute__((used)) main(void) {
mp_stack_fill_with_sentinel(); mp_stack_fill_with_sentinel();
#endif #endif
filesystem_init(); // Create a new filesystem only if we're not in a safe mode.
// A power brownout here could make it appear as if there's
// no SPI flash filesystem, and we might erase the existing one.
filesystem_init(safe_mode == NO_SAFE_MODE);
// Reset everything and prep MicroPython to run boot.py. // Reset everything and prep MicroPython to run boot.py.
reset_port(); reset_port();

View File

@ -46,6 +46,12 @@ vpath %.c . $(TOP)
$(BUILD)/%.o: %.c $(BUILD)/%.o: %.c
$(call compile_c) $(call compile_c)
# frozen.c and frozen_mpy.c are created in $(BUILD), so use our rule
# for those as well.
vpath %.c . $(BUILD)
$(BUILD)/%.o: %.c
$(call compile_c)
# List all native flags since the current build system doesn't have # List all native flags since the current build system doesn't have
# the MicroPython configuration available. However, these flags are # the MicroPython configuration available. However, these flags are
# needed to extract all qstrings # needed to extract all qstrings

View File

@ -34,6 +34,7 @@
#include "py/runtime.h" #include "py/runtime.h"
#include "shared-bindings/microcontroller/Pin.h" #include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/analogio/AnalogIn.h" #include "shared-bindings/analogio/AnalogIn.h"
#include "shared-bindings/util.h"
//| .. currentmodule:: analogio //| .. currentmodule:: analogio
//| //|
@ -114,8 +115,9 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(analogio_analogin___exit___obj, 4, 4,
//| :rtype: int //| :rtype: int
//| //|
STATIC mp_obj_t analogio_analogin_obj_get_value(mp_obj_t self_in) { STATIC mp_obj_t analogio_analogin_obj_get_value(mp_obj_t self_in) {
analogio_analogin_obj_t *self = MP_OBJ_TO_PTR(self_in); analogio_analogin_obj_t *self = MP_OBJ_TO_PTR(self_in);
return MP_OBJ_NEW_SMALL_INT(common_hal_analogio_analogin_get_value(self)); raise_error_if_deinited(common_hal_analogio_analogin_deinited(self));
return MP_OBJ_NEW_SMALL_INT(common_hal_analogio_analogin_get_value(self));
} }
MP_DEFINE_CONST_FUN_OBJ_1(analogio_analogin_get_value_obj, analogio_analogin_obj_get_value); MP_DEFINE_CONST_FUN_OBJ_1(analogio_analogin_get_value_obj, analogio_analogin_obj_get_value);
@ -134,8 +136,9 @@ const mp_obj_property_t analogio_analogin_value_obj = {
//| :rtype: float //| :rtype: float
//| //|
STATIC mp_obj_t analogio_analogin_obj_get_reference_voltage(mp_obj_t self_in) { 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); analogio_analogin_obj_t *self = MP_OBJ_TO_PTR(self_in);
return mp_obj_new_float(common_hal_analogio_analogin_get_reference_voltage(self)); raise_error_if_deinited(common_hal_analogio_analogin_deinited(self));
return mp_obj_new_float(common_hal_analogio_analogin_get_reference_voltage(self));
} }
MP_DEFINE_CONST_FUN_OBJ_1(analogio_analogin_get_reference_voltage_obj, MP_DEFINE_CONST_FUN_OBJ_1(analogio_analogin_get_reference_voltage_obj,
analogio_analogin_obj_get_reference_voltage); analogio_analogin_obj_get_reference_voltage);

View File

@ -34,6 +34,7 @@ extern const mp_obj_type_t analogio_analogin_type;
void common_hal_analogio_analogin_construct(analogio_analogin_obj_t* self, const mcu_pin_obj_t *pin); void common_hal_analogio_analogin_construct(analogio_analogin_obj_t* self, const mcu_pin_obj_t *pin);
void common_hal_analogio_analogin_deinit(analogio_analogin_obj_t* self); void common_hal_analogio_analogin_deinit(analogio_analogin_obj_t* self);
bool common_hal_analogio_analogin_deinited(analogio_analogin_obj_t* self);
uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t* self); uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t* self);
float common_hal_analogio_analogin_get_reference_voltage(analogio_analogin_obj_t* self); float common_hal_analogio_analogin_get_reference_voltage(analogio_analogin_obj_t* self);

View File

@ -30,8 +30,10 @@
#include "lib/utils/context_manager_helpers.h" #include "lib/utils/context_manager_helpers.h"
#include "py/objproperty.h" #include "py/objproperty.h"
#include "py/runtime.h" #include "py/runtime.h"
#include "shared-bindings/microcontroller/Pin.h" #include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/analogio/AnalogOut.h" #include "shared-bindings/analogio/AnalogOut.h"
#include "shared-bindings/util.h"
//| .. currentmodule:: analogio //| .. currentmodule:: analogio
//| //|
@ -112,6 +114,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(analogio_analogout___exit___obj, 4, 4
//| //|
STATIC mp_obj_t analogio_analogout_obj_set_value(mp_obj_t self_in, mp_obj_t value) { STATIC mp_obj_t analogio_analogout_obj_set_value(mp_obj_t self_in, mp_obj_t value) {
analogio_analogout_obj_t *self = MP_OBJ_TO_PTR(self_in); analogio_analogout_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(common_hal_analogio_analogout_deinited(self));
uint32_t v = mp_obj_get_int(value); uint32_t v = mp_obj_get_int(value);
if (v >= (1 << 16)) { if (v >= (1 << 16)) {
mp_raise_ValueError("AnalogOut is only 16 bits. Value must be less than 65536."); mp_raise_ValueError("AnalogOut is only 16 bits. Value must be less than 65536.");

View File

@ -34,6 +34,7 @@ extern const mp_obj_type_t analogio_analogout_type;
void common_hal_analogio_analogout_construct(analogio_analogout_obj_t* self, const mcu_pin_obj_t *pin); void common_hal_analogio_analogout_construct(analogio_analogout_obj_t* self, const mcu_pin_obj_t *pin);
void common_hal_analogio_analogout_deinit(analogio_analogout_obj_t *self); void common_hal_analogio_analogout_deinit(analogio_analogout_obj_t *self);
bool common_hal_analogio_analogout_deinited(analogio_analogout_obj_t *self);
void common_hal_analogio_analogout_set_value(analogio_analogout_obj_t *self, uint16_t value); void common_hal_analogio_analogout_set_value(analogio_analogout_obj_t *self, uint16_t value);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGIO_ANALOGOUT_H #endif // MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGIO_ANALOGOUT_H

View File

@ -32,6 +32,7 @@
#include "py/runtime.h" #include "py/runtime.h"
#include "shared-bindings/microcontroller/Pin.h" #include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/audiobusio/PDMIn.h" #include "shared-bindings/audiobusio/PDMIn.h"
#include "shared-bindings/util.h"
//| .. currentmodule:: audiobusio //| .. currentmodule:: audiobusio
//| //|
@ -161,7 +162,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(audiobusio_pdmin___exit___obj, 4, 4,
//| //|
STATIC mp_obj_t audiobusio_pdmin_obj_record(mp_obj_t self_obj, mp_obj_t destination, mp_obj_t destination_length) { STATIC mp_obj_t audiobusio_pdmin_obj_record(mp_obj_t self_obj, mp_obj_t destination, mp_obj_t destination_length) {
audiobusio_pdmin_obj_t *self = MP_OBJ_TO_PTR(self_obj); audiobusio_pdmin_obj_t *self = MP_OBJ_TO_PTR(self_obj);
raise_error_if_deinited(common_hal_audiobusio_pdmin_deinited(self));
if (!MP_OBJ_IS_SMALL_INT(destination_length)) { if (!MP_OBJ_IS_SMALL_INT(destination_length)) {
mp_raise_TypeError("destination_length must be int"); mp_raise_TypeError("destination_length must be int");
} }
@ -198,6 +199,7 @@ MP_DEFINE_CONST_FUN_OBJ_3(audiobusio_pdmin_record_obj, audiobusio_pdmin_obj_reco
//| //|
STATIC mp_obj_t audiobusio_pdmin_obj_get_frequency(mp_obj_t self_in) { STATIC mp_obj_t audiobusio_pdmin_obj_get_frequency(mp_obj_t self_in) {
audiobusio_pdmin_obj_t *self = MP_OBJ_TO_PTR(self_in); audiobusio_pdmin_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(common_hal_audiobusio_pdmin_deinited(self));
return MP_OBJ_NEW_SMALL_INT(common_hal_audiobusio_pdmin_get_frequency(self)); return MP_OBJ_NEW_SMALL_INT(common_hal_audiobusio_pdmin_get_frequency(self));
} }
MP_DEFINE_CONST_FUN_OBJ_1(audiobusio_pdmin_get_frequency_obj, audiobusio_pdmin_obj_get_frequency); MP_DEFINE_CONST_FUN_OBJ_1(audiobusio_pdmin_get_frequency_obj, audiobusio_pdmin_obj_get_frequency);

View File

@ -37,6 +37,7 @@ void common_hal_audiobusio_pdmin_construct(audiobusio_pdmin_obj_t* self,
const mcu_pin_obj_t* clock_pin, const mcu_pin_obj_t* data_pin, const mcu_pin_obj_t* clock_pin, const mcu_pin_obj_t* data_pin,
uint32_t frequency, uint8_t bit_depth, bool mono, uint8_t oversample); uint32_t frequency, uint8_t bit_depth, bool mono, uint8_t oversample);
void common_hal_audiobusio_pdmin_deinit(audiobusio_pdmin_obj_t* self); void common_hal_audiobusio_pdmin_deinit(audiobusio_pdmin_obj_t* self);
bool common_hal_audiobusio_pdmin_deinited(audiobusio_pdmin_obj_t* self);
uint32_t common_hal_audiobusio_pdmin_record_to_buffer(audiobusio_pdmin_obj_t* self, uint32_t common_hal_audiobusio_pdmin_record_to_buffer(audiobusio_pdmin_obj_t* self,
uint16_t* buffer, uint32_t length); uint16_t* buffer, uint32_t length);
uint8_t common_hal_audiobusio_pdmin_get_bit_depth(audiobusio_pdmin_obj_t* self); uint8_t common_hal_audiobusio_pdmin_get_bit_depth(audiobusio_pdmin_obj_t* self);

View File

@ -32,6 +32,7 @@
#include "py/runtime.h" #include "py/runtime.h"
#include "shared-bindings/microcontroller/Pin.h" #include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/audioio/AudioOut.h" #include "shared-bindings/audioio/AudioOut.h"
#include "shared-bindings/util.h"
//| .. currentmodule:: audioio //| .. currentmodule:: audioio
//| //|
@ -162,6 +163,7 @@ STATIC mp_obj_t audioio_audioout_obj_play(size_t n_args, const mp_obj_t *pos_arg
{ MP_QSTR_loop, MP_ARG_BOOL, {.u_bool = false} }, { MP_QSTR_loop, MP_ARG_BOOL, {.u_bool = false} },
}; };
audioio_audioout_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); audioio_audioout_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
raise_error_if_deinited(common_hal_audioio_audioout_deinited(self));
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
@ -177,6 +179,7 @@ MP_DEFINE_CONST_FUN_OBJ_KW(audioio_audioout_play_obj, 1, audioio_audioout_obj_pl
//| //|
STATIC mp_obj_t audioio_audioout_obj_stop(mp_obj_t self_in) { STATIC mp_obj_t audioio_audioout_obj_stop(mp_obj_t self_in) {
audioio_audioout_obj_t *self = MP_OBJ_TO_PTR(self_in); audioio_audioout_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(common_hal_audioio_audioout_deinited(self));
common_hal_audioio_audioout_stop(self); common_hal_audioio_audioout_stop(self);
return mp_const_none; return mp_const_none;
} }
@ -188,6 +191,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(audioio_audioout_stop_obj, audioio_audioout_obj_stop);
//| //|
STATIC mp_obj_t audioio_audioout_obj_get_playing(mp_obj_t self_in) { STATIC mp_obj_t audioio_audioout_obj_get_playing(mp_obj_t self_in) {
audioio_audioout_obj_t *self = MP_OBJ_TO_PTR(self_in); audioio_audioout_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(common_hal_audioio_audioout_deinited(self));
return mp_obj_new_bool(common_hal_audioio_audioout_get_playing(self)); return mp_obj_new_bool(common_hal_audioio_audioout_get_playing(self));
} }
MP_DEFINE_CONST_FUN_OBJ_1(audioio_audioout_get_playing_obj, audioio_audioout_obj_get_playing); MP_DEFINE_CONST_FUN_OBJ_1(audioio_audioout_get_playing_obj, audioio_audioout_obj_get_playing);
@ -207,12 +211,14 @@ const mp_obj_property_t audioio_audioout_playing_obj = {
//| //|
STATIC mp_obj_t audioio_audioout_obj_get_frequency(mp_obj_t self_in) { STATIC mp_obj_t audioio_audioout_obj_get_frequency(mp_obj_t self_in) {
audioio_audioout_obj_t *self = MP_OBJ_TO_PTR(self_in); audioio_audioout_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(common_hal_audioio_audioout_deinited(self));
return MP_OBJ_NEW_SMALL_INT(common_hal_audioio_audioout_get_frequency(self)); return MP_OBJ_NEW_SMALL_INT(common_hal_audioio_audioout_get_frequency(self));
} }
MP_DEFINE_CONST_FUN_OBJ_1(audioio_audioout_get_frequency_obj, audioio_audioout_obj_get_frequency); MP_DEFINE_CONST_FUN_OBJ_1(audioio_audioout_get_frequency_obj, audioio_audioout_obj_get_frequency);
STATIC mp_obj_t audioio_audioout_obj_set_frequency(mp_obj_t self_in, mp_obj_t frequency) { STATIC mp_obj_t audioio_audioout_obj_set_frequency(mp_obj_t self_in, mp_obj_t frequency) {
audioio_audioout_obj_t *self = MP_OBJ_TO_PTR(self_in); audioio_audioout_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(common_hal_audioio_audioout_deinited(self));
common_hal_audioio_audioout_set_frequency(self, mp_obj_get_int(frequency)); common_hal_audioio_audioout_set_frequency(self, mp_obj_get_int(frequency));
return mp_const_none; return mp_const_none;
} }

View File

@ -39,6 +39,7 @@ void common_hal_audioio_audioout_construct_from_file(audioio_audioout_obj_t* sel
const mcu_pin_obj_t* pin, pyb_file_obj_t* file); const mcu_pin_obj_t* pin, pyb_file_obj_t* file);
void common_hal_audioio_audioout_deinit(audioio_audioout_obj_t* self); void common_hal_audioio_audioout_deinit(audioio_audioout_obj_t* self);
bool common_hal_audioio_audioout_deinited(audioio_audioout_obj_t* self);
void common_hal_audioio_audioout_play(audioio_audioout_obj_t* self, bool loop); void common_hal_audioio_audioout_play(audioio_audioout_obj_t* self, bool loop);
void common_hal_audioio_audioout_stop(audioio_audioout_obj_t* self); void common_hal_audioio_audioout_stop(audioio_audioout_obj_t* self);
bool common_hal_audioio_audioout_get_playing(audioio_audioout_obj_t* self); bool common_hal_audioio_audioout_get_playing(audioio_audioout_obj_t* self);

View File

@ -29,6 +29,7 @@
#include "shared-bindings/bitbangio/I2C.h" #include "shared-bindings/bitbangio/I2C.h"
#include "shared-bindings/microcontroller/Pin.h" #include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/util.h"
#include "lib/utils/buffer_helper.h" #include "lib/utils/buffer_helper.h"
#include "lib/utils/context_manager_helpers.h" #include "lib/utils/context_manager_helpers.h"
@ -52,6 +53,7 @@
STATIC mp_obj_t bitbangio_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) { STATIC mp_obj_t bitbangio_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) {
mp_arg_check_num(n_args, n_kw, 0, MP_OBJ_FUN_ARGS_MAX, true); mp_arg_check_num(n_args, n_kw, 0, MP_OBJ_FUN_ARGS_MAX, true);
bitbangio_i2c_obj_t *self = m_new_obj(bitbangio_i2c_obj_t); bitbangio_i2c_obj_t *self = m_new_obj(bitbangio_i2c_obj_t);
raise_error_if_deinited(shared_module_bitbangio_i2c_deinited(self));
self->base.type = &bitbangio_i2c_type; self->base.type = &bitbangio_i2c_type;
mp_map_t kw_args; mp_map_t kw_args;
mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args); mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args);
@ -114,6 +116,7 @@ static void check_lock(bitbangio_i2c_obj_t *self) {
//| //|
STATIC mp_obj_t bitbangio_i2c_scan(mp_obj_t self_in) { STATIC mp_obj_t bitbangio_i2c_scan(mp_obj_t self_in) {
bitbangio_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in); bitbangio_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(shared_module_bitbangio_i2c_deinited(self));
check_lock(self); check_lock(self);
mp_obj_t list = mp_obj_new_list(0, NULL); mp_obj_t list = mp_obj_new_list(0, NULL);
// 7-bit addresses 0b0000xxx and 0b1111xxx are reserved // 7-bit addresses 0b0000xxx and 0b1111xxx are reserved
@ -132,7 +135,9 @@ MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_i2c_scan_obj, bitbangio_i2c_scan);
//| Attempts to grab the I2C lock. Returns True on success. //| Attempts to grab the I2C lock. Returns True on success.
//| //|
STATIC mp_obj_t bitbangio_i2c_obj_try_lock(mp_obj_t self_in) { STATIC mp_obj_t bitbangio_i2c_obj_try_lock(mp_obj_t self_in) {
return mp_obj_new_bool(shared_module_bitbangio_i2c_try_lock(MP_OBJ_TO_PTR(self_in))); bitbangio_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(shared_module_bitbangio_i2c_deinited(self));
return mp_obj_new_bool(shared_module_bitbangio_i2c_try_lock(self));
} }
MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_i2c_try_lock_obj, bitbangio_i2c_obj_try_lock); MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_i2c_try_lock_obj, bitbangio_i2c_obj_try_lock);
@ -141,7 +146,9 @@ MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_i2c_try_lock_obj, bitbangio_i2c_obj_try_lock
//| Releases the I2C lock. //| Releases the I2C lock.
//| //|
STATIC mp_obj_t bitbangio_i2c_obj_unlock(mp_obj_t self_in) { STATIC mp_obj_t bitbangio_i2c_obj_unlock(mp_obj_t self_in) {
shared_module_bitbangio_i2c_unlock(MP_OBJ_TO_PTR(self_in)); bitbangio_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(shared_module_bitbangio_i2c_deinited(self));
shared_module_bitbangio_i2c_unlock(self);
return mp_const_none; return mp_const_none;
} }
MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_i2c_unlock_obj, bitbangio_i2c_obj_unlock); MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_i2c_unlock_obj, bitbangio_i2c_obj_unlock);
@ -169,6 +176,7 @@ STATIC mp_obj_t bitbangio_i2c_readfrom_into(size_t n_args, const mp_obj_t *pos_a
{ MP_QSTR_end, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = INT_MAX} }, { MP_QSTR_end, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = INT_MAX} },
}; };
bitbangio_i2c_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); bitbangio_i2c_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
raise_error_if_deinited(shared_module_bitbangio_i2c_deinited(self));
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
check_lock(self); check_lock(self);
@ -215,6 +223,7 @@ STATIC mp_obj_t bitbangio_i2c_writeto(size_t n_args, const mp_obj_t *pos_args, m
{ MP_QSTR_stop, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} }, { MP_QSTR_stop, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} },
}; };
bitbangio_i2c_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); bitbangio_i2c_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
raise_error_if_deinited(shared_module_bitbangio_i2c_deinited(self));
check_lock(self); check_lock(self);
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);

View File

@ -42,6 +42,7 @@ extern void shared_module_bitbangio_i2c_construct(bitbangio_i2c_obj_t *self,
uint32_t frequency); uint32_t frequency);
extern void shared_module_bitbangio_i2c_deinit(bitbangio_i2c_obj_t *self); extern void shared_module_bitbangio_i2c_deinit(bitbangio_i2c_obj_t *self);
extern bool shared_module_bitbangio_i2c_deinited(bitbangio_i2c_obj_t *self);
extern bool shared_module_bitbangio_i2c_try_lock(bitbangio_i2c_obj_t *self); extern bool shared_module_bitbangio_i2c_try_lock(bitbangio_i2c_obj_t *self);
extern bool shared_module_bitbangio_i2c_has_lock(bitbangio_i2c_obj_t *self); extern bool shared_module_bitbangio_i2c_has_lock(bitbangio_i2c_obj_t *self);

View File

@ -32,6 +32,7 @@
#include "py/runtime0.h" #include "py/runtime0.h"
#include "shared-bindings/microcontroller/Pin.h" #include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/bitbangio/OneWire.h" #include "shared-bindings/bitbangio/OneWire.h"
#include "shared-bindings/util.h"
//| .. currentmodule:: bitbangio //| .. currentmodule:: bitbangio
//| //|
@ -117,6 +118,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bitbangio_onewire___exit___obj, 4, 4,
//| //|
STATIC mp_obj_t bitbangio_onewire_obj_reset(mp_obj_t self_in) { STATIC mp_obj_t bitbangio_onewire_obj_reset(mp_obj_t self_in) {
bitbangio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in); bitbangio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(shared_module_bitbangio_onewire_deinited(self));
return mp_obj_new_bool(shared_module_bitbangio_onewire_reset(self)); return mp_obj_new_bool(shared_module_bitbangio_onewire_reset(self));
} }
@ -131,6 +133,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_onewire_reset_obj, bitbangio_onewire_obj_res
//| //|
STATIC mp_obj_t bitbangio_onewire_obj_read_bit(mp_obj_t self_in) { STATIC mp_obj_t bitbangio_onewire_obj_read_bit(mp_obj_t self_in) {
bitbangio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in); bitbangio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(shared_module_bitbangio_onewire_deinited(self));
return mp_obj_new_bool(shared_module_bitbangio_onewire_read_bit(self)); return mp_obj_new_bool(shared_module_bitbangio_onewire_read_bit(self));
} }
@ -142,6 +145,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_onewire_read_bit_obj, bitbangio_onewire_obj_
//| //|
STATIC mp_obj_t bitbangio_onewire_obj_write_bit(mp_obj_t self_in, mp_obj_t bool_obj) { STATIC mp_obj_t bitbangio_onewire_obj_write_bit(mp_obj_t self_in, mp_obj_t bool_obj) {
bitbangio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in); bitbangio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(shared_module_bitbangio_onewire_deinited(self));
shared_module_bitbangio_onewire_write_bit(self, mp_obj_is_true(bool_obj)); shared_module_bitbangio_onewire_write_bit(self, mp_obj_is_true(bool_obj));
return mp_const_none; return mp_const_none;

View File

@ -35,6 +35,7 @@ extern const mp_obj_type_t bitbangio_onewire_type;
extern void shared_module_bitbangio_onewire_construct(bitbangio_onewire_obj_t* self, extern void shared_module_bitbangio_onewire_construct(bitbangio_onewire_obj_t* self,
const mcu_pin_obj_t* pin); const mcu_pin_obj_t* pin);
extern void shared_module_bitbangio_onewire_deinit(bitbangio_onewire_obj_t* self); extern void shared_module_bitbangio_onewire_deinit(bitbangio_onewire_obj_t* self);
extern bool shared_module_bitbangio_onewire_deinited(bitbangio_onewire_obj_t* self);
extern bool shared_module_bitbangio_onewire_reset(bitbangio_onewire_obj_t* self); extern bool shared_module_bitbangio_onewire_reset(bitbangio_onewire_obj_t* self);
extern bool shared_module_bitbangio_onewire_read_bit(bitbangio_onewire_obj_t* self); extern bool shared_module_bitbangio_onewire_read_bit(bitbangio_onewire_obj_t* self);
extern void shared_module_bitbangio_onewire_write_bit(bitbangio_onewire_obj_t* self, bool bit); extern void shared_module_bitbangio_onewire_write_bit(bitbangio_onewire_obj_t* self, bool bit);

View File

@ -31,6 +31,7 @@
#include "shared-bindings/bitbangio/SPI.h" #include "shared-bindings/bitbangio/SPI.h"
#include "shared-bindings/microcontroller/Pin.h" #include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/util.h"
#include "lib/utils/context_manager_helpers.h" #include "lib/utils/context_manager_helpers.h"
#include "py/mperrno.h" #include "py/mperrno.h"
@ -138,6 +139,7 @@ STATIC mp_obj_t bitbangio_spi_configure(size_t n_args, const mp_obj_t *pos_args,
{ MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} }, { MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
}; };
bitbangio_spi_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); bitbangio_spi_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
raise_error_if_deinited(shared_module_bitbangio_spi_deinited(self));
check_lock(self); check_lock(self);
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
@ -168,7 +170,9 @@ MP_DEFINE_CONST_FUN_OBJ_KW(bitbangio_spi_configure_obj, 1, bitbangio_spi_configu
//| :rtype: bool //| :rtype: bool
//| //|
STATIC mp_obj_t bitbangio_spi_obj_try_lock(mp_obj_t self_in) { STATIC mp_obj_t bitbangio_spi_obj_try_lock(mp_obj_t self_in) {
return mp_obj_new_bool(shared_module_bitbangio_spi_try_lock(MP_OBJ_TO_PTR(self_in))); bitbangio_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(shared_module_bitbangio_spi_deinited(self));
return mp_obj_new_bool(shared_module_bitbangio_spi_try_lock(self));
} }
MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_spi_try_lock_obj, bitbangio_spi_obj_try_lock); MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_spi_try_lock_obj, bitbangio_spi_obj_try_lock);
@ -177,7 +181,9 @@ MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_spi_try_lock_obj, bitbangio_spi_obj_try_lock
//| Releases the SPI lock. //| Releases the SPI lock.
//| //|
STATIC mp_obj_t bitbangio_spi_obj_unlock(mp_obj_t self_in) { STATIC mp_obj_t bitbangio_spi_obj_unlock(mp_obj_t self_in) {
shared_module_bitbangio_spi_unlock(MP_OBJ_TO_PTR(self_in)); bitbangio_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(shared_module_bitbangio_spi_deinited(self));
shared_module_bitbangio_spi_unlock(self);
return mp_const_none; return mp_const_none;
} }
MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_spi_unlock_obj, bitbangio_spi_obj_unlock); MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_spi_unlock_obj, bitbangio_spi_obj_unlock);
@ -188,9 +194,10 @@ MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_spi_unlock_obj, bitbangio_spi_obj_unlock);
//| //|
// TODO(tannewt): Add support for start and end kwargs. // TODO(tannewt): Add support for start and end kwargs.
STATIC mp_obj_t bitbangio_spi_write(mp_obj_t self_in, mp_obj_t wr_buf) { STATIC mp_obj_t bitbangio_spi_write(mp_obj_t self_in, mp_obj_t wr_buf) {
bitbangio_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(shared_module_bitbangio_spi_deinited(self));
mp_buffer_info_t src; mp_buffer_info_t src;
mp_get_buffer_raise(wr_buf, &src, MP_BUFFER_READ); mp_get_buffer_raise(wr_buf, &src, MP_BUFFER_READ);
bitbangio_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_lock(self); check_lock(self);
bool ok = shared_module_bitbangio_spi_write(self, src.buf, src.len); bool ok = shared_module_bitbangio_spi_write(self, src.buf, src.len);
if (!ok) { if (!ok) {
@ -207,10 +214,12 @@ MP_DEFINE_CONST_FUN_OBJ_2(bitbangio_spi_write_obj, bitbangio_spi_write);
//| //|
// TODO(tannewt): Add support for start and end kwargs. // TODO(tannewt): Add support for start and end kwargs.
STATIC mp_obj_t bitbangio_spi_readinto(size_t n_args, const mp_obj_t *args) { STATIC mp_obj_t bitbangio_spi_readinto(size_t n_args, const mp_obj_t *args) {
bitbangio_spi_obj_t *self = MP_OBJ_TO_PTR(args[0]);
raise_error_if_deinited(shared_module_bitbangio_spi_deinited(self));
mp_buffer_info_t bufinfo; mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE); mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE);
check_lock(args[0]); check_lock(args[0]);
bool ok = shared_module_bitbangio_spi_read(args[0], bufinfo.buf, bufinfo.len); bool ok = shared_module_bitbangio_spi_read(self, bufinfo.buf, bufinfo.len);
if (!ok) { if (!ok) {
mp_raise_OSError(MP_EIO); mp_raise_OSError(MP_EIO);
} }

View File

@ -41,6 +41,7 @@ extern void shared_module_bitbangio_spi_construct(bitbangio_spi_obj_t *self,
const mcu_pin_obj_t * miso); const mcu_pin_obj_t * miso);
extern void shared_module_bitbangio_spi_deinit(bitbangio_spi_obj_t *self); extern void shared_module_bitbangio_spi_deinit(bitbangio_spi_obj_t *self);
extern bool shared_module_bitbangio_spi_deinited(bitbangio_spi_obj_t *self);
extern void shared_module_bitbangio_spi_configure(bitbangio_spi_obj_t *self, extern void shared_module_bitbangio_spi_configure(bitbangio_spi_obj_t *self,
uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t bits); uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t bits);

View File

@ -29,6 +29,7 @@
#include "shared-bindings/microcontroller/Pin.h" #include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/busio/I2C.h" #include "shared-bindings/busio/I2C.h"
#include "shared-bindings/util.h"
#include "lib/utils/buffer_helper.h" #include "lib/utils/buffer_helper.h"
#include "lib/utils/context_manager_helpers.h" #include "lib/utils/context_manager_helpers.h"
@ -126,6 +127,7 @@ static void check_lock(busio_i2c_obj_t *self) {
//| //|
STATIC mp_obj_t busio_i2c_scan(mp_obj_t self_in) { STATIC mp_obj_t busio_i2c_scan(mp_obj_t self_in) {
busio_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in); busio_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(common_hal_busio_i2c_deinited(self));
check_lock(self); check_lock(self);
mp_obj_t list = mp_obj_new_list(0, NULL); mp_obj_t list = mp_obj_new_list(0, NULL);
// 7-bit addresses 0b0000xxx and 0b1111xxx are reserved // 7-bit addresses 0b0000xxx and 0b1111xxx are reserved
@ -147,7 +149,9 @@ MP_DEFINE_CONST_FUN_OBJ_1(busio_i2c_scan_obj, busio_i2c_scan);
//| :rtype: bool //| :rtype: bool
//| //|
STATIC mp_obj_t busio_i2c_obj_try_lock(mp_obj_t self_in) { STATIC mp_obj_t busio_i2c_obj_try_lock(mp_obj_t self_in) {
return mp_obj_new_bool(common_hal_busio_i2c_try_lock(MP_OBJ_TO_PTR(self_in))); busio_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(common_hal_busio_i2c_deinited(self));
return mp_obj_new_bool(common_hal_busio_i2c_try_lock(self));
} }
MP_DEFINE_CONST_FUN_OBJ_1(busio_i2c_try_lock_obj, busio_i2c_obj_try_lock); MP_DEFINE_CONST_FUN_OBJ_1(busio_i2c_try_lock_obj, busio_i2c_obj_try_lock);
@ -156,7 +160,9 @@ MP_DEFINE_CONST_FUN_OBJ_1(busio_i2c_try_lock_obj, busio_i2c_obj_try_lock);
//| Releases the I2C lock. //| Releases the I2C lock.
//| //|
STATIC mp_obj_t busio_i2c_obj_unlock(mp_obj_t self_in) { STATIC mp_obj_t busio_i2c_obj_unlock(mp_obj_t self_in) {
common_hal_busio_i2c_unlock(MP_OBJ_TO_PTR(self_in)); busio_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(common_hal_busio_i2c_deinited(self));
common_hal_busio_i2c_unlock(self);
return mp_const_none; return mp_const_none;
} }
MP_DEFINE_CONST_FUN_OBJ_1(busio_i2c_unlock_obj, busio_i2c_obj_unlock); MP_DEFINE_CONST_FUN_OBJ_1(busio_i2c_unlock_obj, busio_i2c_obj_unlock);
@ -184,6 +190,7 @@ STATIC mp_obj_t busio_i2c_readfrom_into(size_t n_args, const mp_obj_t *pos_args,
{ MP_QSTR_end, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = INT_MAX} }, { MP_QSTR_end, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = INT_MAX} },
}; };
busio_i2c_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); busio_i2c_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
raise_error_if_deinited(common_hal_busio_i2c_deinited(self));
check_lock(self); check_lock(self);
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
@ -229,6 +236,7 @@ STATIC mp_obj_t busio_i2c_writeto(size_t n_args, const mp_obj_t *pos_args, mp_ma
{ MP_QSTR_stop, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} }, { MP_QSTR_stop, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} },
}; };
busio_i2c_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); busio_i2c_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
raise_error_if_deinited(common_hal_busio_i2c_deinited(self));
check_lock(self); check_lock(self);
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);

View File

@ -49,6 +49,7 @@ extern void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
uint32_t frequency); uint32_t frequency);
extern void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self); extern void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self);
extern bool common_hal_busio_i2c_deinited(busio_i2c_obj_t *self);
extern bool common_hal_busio_i2c_try_lock(busio_i2c_obj_t *self); extern bool common_hal_busio_i2c_try_lock(busio_i2c_obj_t *self);
extern bool common_hal_busio_i2c_has_lock(busio_i2c_obj_t *self); extern bool common_hal_busio_i2c_has_lock(busio_i2c_obj_t *self);

View File

@ -32,6 +32,7 @@
#include "py/runtime0.h" #include "py/runtime0.h"
#include "shared-bindings/microcontroller/Pin.h" #include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/busio/OneWire.h" #include "shared-bindings/busio/OneWire.h"
#include "shared-bindings/util.h"
//| .. currentmodule:: busio //| .. currentmodule:: busio
//| //|
@ -120,6 +121,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(busio_onewire___exit___obj, 4, 4, bus
//| //|
STATIC mp_obj_t busio_onewire_obj_reset(mp_obj_t self_in) { STATIC mp_obj_t busio_onewire_obj_reset(mp_obj_t self_in) {
busio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in); busio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(common_hal_busio_onewire_deinited(self));
return mp_obj_new_bool(common_hal_busio_onewire_reset(self)); return mp_obj_new_bool(common_hal_busio_onewire_reset(self));
} }
@ -134,6 +136,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(busio_onewire_reset_obj, busio_onewire_obj_reset);
//| //|
STATIC mp_obj_t busio_onewire_obj_read_bit(mp_obj_t self_in) { STATIC mp_obj_t busio_onewire_obj_read_bit(mp_obj_t self_in) {
busio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in); busio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(common_hal_busio_onewire_deinited(self));
return mp_obj_new_bool(common_hal_busio_onewire_read_bit(self)); return mp_obj_new_bool(common_hal_busio_onewire_read_bit(self));
} }
@ -145,6 +148,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(busio_onewire_read_bit_obj, busio_onewire_obj_read_bit
//| //|
STATIC mp_obj_t busio_onewire_obj_write_bit(mp_obj_t self_in, mp_obj_t bool_obj) { STATIC mp_obj_t busio_onewire_obj_write_bit(mp_obj_t self_in, mp_obj_t bool_obj) {
busio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in); busio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(common_hal_busio_onewire_deinited(self));
common_hal_busio_onewire_write_bit(self, mp_obj_is_true(bool_obj)); common_hal_busio_onewire_write_bit(self, mp_obj_is_true(bool_obj));
return mp_const_none; return mp_const_none;

View File

@ -35,6 +35,7 @@ extern const mp_obj_type_t busio_onewire_type;
extern void common_hal_busio_onewire_construct(busio_onewire_obj_t* self, extern void common_hal_busio_onewire_construct(busio_onewire_obj_t* self,
const mcu_pin_obj_t* pin); const mcu_pin_obj_t* pin);
extern void common_hal_busio_onewire_deinit(busio_onewire_obj_t* self); extern void common_hal_busio_onewire_deinit(busio_onewire_obj_t* self);
extern bool common_hal_busio_onewire_deinited(busio_onewire_obj_t* self);
extern bool common_hal_busio_onewire_reset(busio_onewire_obj_t* self); extern bool common_hal_busio_onewire_reset(busio_onewire_obj_t* self);
extern bool common_hal_busio_onewire_read_bit(busio_onewire_obj_t* self); extern bool common_hal_busio_onewire_read_bit(busio_onewire_obj_t* self);
extern void common_hal_busio_onewire_write_bit(busio_onewire_obj_t* self, bool bit); extern void common_hal_busio_onewire_write_bit(busio_onewire_obj_t* self, bool bit);

View File

@ -31,6 +31,7 @@
#include "shared-bindings/microcontroller/Pin.h" #include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/busio/SPI.h" #include "shared-bindings/busio/SPI.h"
#include "shared-bindings/util.h"
#include "lib/utils/buffer_helper.h" #include "lib/utils/buffer_helper.h"
#include "lib/utils/context_manager_helpers.h" #include "lib/utils/context_manager_helpers.h"
@ -151,6 +152,7 @@ STATIC mp_obj_t busio_spi_configure(size_t n_args, const mp_obj_t *pos_args, mp_
{ MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} }, { MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
}; };
busio_spi_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); busio_spi_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
raise_error_if_deinited(common_hal_busio_spi_deinited(self));
check_lock(self); check_lock(self);
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
@ -184,7 +186,9 @@ MP_DEFINE_CONST_FUN_OBJ_KW(busio_spi_configure_obj, 1, busio_spi_configure);
//| :rtype: bool //| :rtype: bool
//| //|
STATIC mp_obj_t busio_spi_obj_try_lock(mp_obj_t self_in) { STATIC mp_obj_t busio_spi_obj_try_lock(mp_obj_t self_in) {
return mp_obj_new_bool(common_hal_busio_spi_try_lock(MP_OBJ_TO_PTR(self_in))); busio_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(common_hal_busio_spi_deinited(self));
return mp_obj_new_bool(common_hal_busio_spi_try_lock(self));
} }
MP_DEFINE_CONST_FUN_OBJ_1(busio_spi_try_lock_obj, busio_spi_obj_try_lock); MP_DEFINE_CONST_FUN_OBJ_1(busio_spi_try_lock_obj, busio_spi_obj_try_lock);
@ -193,7 +197,9 @@ MP_DEFINE_CONST_FUN_OBJ_1(busio_spi_try_lock_obj, busio_spi_obj_try_lock);
//| Releases the SPI lock. //| Releases the SPI lock.
//| //|
STATIC mp_obj_t busio_spi_obj_unlock(mp_obj_t self_in) { STATIC mp_obj_t busio_spi_obj_unlock(mp_obj_t self_in) {
common_hal_busio_spi_unlock(MP_OBJ_TO_PTR(self_in)); busio_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(common_hal_busio_spi_deinited(self));
common_hal_busio_spi_unlock(self);
return mp_const_none; return mp_const_none;
} }
MP_DEFINE_CONST_FUN_OBJ_1(busio_spi_unlock_obj, busio_spi_obj_unlock); MP_DEFINE_CONST_FUN_OBJ_1(busio_spi_unlock_obj, busio_spi_obj_unlock);
@ -214,6 +220,7 @@ STATIC mp_obj_t busio_spi_write(size_t n_args, const mp_obj_t *pos_args, mp_map_
{ MP_QSTR_end, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = INT_MAX} }, { MP_QSTR_end, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = INT_MAX} },
}; };
busio_spi_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); busio_spi_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
raise_error_if_deinited(common_hal_busio_spi_deinited(self));
check_lock(self); check_lock(self);
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
@ -251,6 +258,7 @@ STATIC mp_obj_t busio_spi_readinto(size_t n_args, const mp_obj_t *pos_args, mp_m
{ MP_QSTR_write_value,MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, { MP_QSTR_write_value,MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
}; };
busio_spi_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); busio_spi_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
raise_error_if_deinited(common_hal_busio_spi_deinited(self));
check_lock(self); check_lock(self);
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);

View File

@ -41,6 +41,7 @@ extern void common_hal_busio_spi_construct(busio_spi_obj_t *self,
const mcu_pin_obj_t * miso); const mcu_pin_obj_t * miso);
extern void common_hal_busio_spi_deinit(busio_spi_obj_t *self); extern void common_hal_busio_spi_deinit(busio_spi_obj_t *self);
extern bool common_hal_busio_spi_deinited(busio_spi_obj_t *self);
extern bool common_hal_busio_spi_configure(busio_spi_obj_t *self, uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t bits); extern bool common_hal_busio_spi_configure(busio_spi_obj_t *self, uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t bits);

View File

@ -27,6 +27,8 @@
#include <stdint.h> #include <stdint.h>
#include "shared-bindings/busio/UART.h" #include "shared-bindings/busio/UART.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/util.h"
#include "lib/utils/context_manager_helpers.h" #include "lib/utils/context_manager_helpers.h"
@ -34,7 +36,6 @@
#include "py/runtime.h" #include "py/runtime.h"
#include "py/stream.h" #include "py/stream.h"
#include "shared-bindings/microcontroller/Pin.h"
//| .. currentmodule:: busio //| .. currentmodule:: busio
//| //|
@ -179,7 +180,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(busio_uart___exit___obj, 4, 4, busio_
// These three methods are used by the shared stream methods. // These three methods are used by the shared stream methods.
STATIC mp_uint_t busio_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, int *errcode) { STATIC mp_uint_t busio_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, int *errcode) {
busio_uart_obj_t *self = self_in; busio_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(common_hal_busio_uart_deinited(self));
byte *buf = buf_in; byte *buf = buf_in;
// make sure we want at least 1 char // make sure we want at least 1 char
@ -191,14 +193,16 @@ STATIC mp_uint_t busio_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t size,
} }
STATIC mp_uint_t busio_uart_write(mp_obj_t self_in, const void *buf_in, mp_uint_t size, int *errcode) { STATIC mp_uint_t busio_uart_write(mp_obj_t self_in, const void *buf_in, mp_uint_t size, int *errcode) {
busio_uart_obj_t *self = self_in; busio_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(common_hal_busio_uart_deinited(self));
const byte *buf = buf_in; const byte *buf = buf_in;
return common_hal_busio_uart_write(self, buf, size, errcode); return common_hal_busio_uart_write(self, buf, size, errcode);
} }
STATIC mp_uint_t busio_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t arg, int *errcode) { STATIC mp_uint_t busio_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t arg, int *errcode) {
busio_uart_obj_t *self = self_in; busio_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(common_hal_busio_uart_deinited(self));
mp_uint_t ret; mp_uint_t ret;
if (request == MP_IOCTL_POLL) { if (request == MP_IOCTL_POLL) {
mp_uint_t flags = arg; mp_uint_t flags = arg;

View File

@ -45,6 +45,7 @@ extern void common_hal_busio_uart_construct(busio_uart_obj_t *self,
uint8_t receiver_buffer_size); uint8_t receiver_buffer_size);
extern void common_hal_busio_uart_deinit(busio_uart_obj_t *self); extern void common_hal_busio_uart_deinit(busio_uart_obj_t *self);
extern bool common_hal_busio_uart_deinited(busio_uart_obj_t *self);
// Read characters. len is in characters NOT bytes! // Read characters. len is in characters NOT bytes!
extern size_t common_hal_busio_uart_read(busio_uart_obj_t *self, extern size_t common_hal_busio_uart_read(busio_uart_obj_t *self,

View File

@ -40,6 +40,7 @@
#include "shared-bindings/digitalio/Direction.h" #include "shared-bindings/digitalio/Direction.h"
#include "shared-bindings/digitalio/DriveMode.h" #include "shared-bindings/digitalio/DriveMode.h"
#include "shared-bindings/digitalio/Pull.h" #include "shared-bindings/digitalio/Pull.h"
#include "shared-bindings/util.h"
//| .. currentmodule:: digitalio //| .. currentmodule:: digitalio
//| //|
@ -119,6 +120,7 @@ STATIC mp_obj_t digitalio_digitalinout_switch_to_output(size_t n_args, const mp_
{ MP_QSTR_drive_mode, MP_ARG_OBJ, {.u_rom_obj = &digitalio_drive_mode_push_pull_obj} }, { MP_QSTR_drive_mode, MP_ARG_OBJ, {.u_rom_obj = &digitalio_drive_mode_push_pull_obj} },
}; };
digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
raise_error_if_deinited(common_hal_digitalio_digitalinout_deinited(self));
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
@ -155,6 +157,7 @@ STATIC mp_obj_t digitalio_digitalinout_switch_to_input(size_t n_args, const mp_o
{ MP_QSTR_pull, MP_ARG_OBJ, {.u_rom_obj = mp_const_none} }, { MP_QSTR_pull, MP_ARG_OBJ, {.u_rom_obj = mp_const_none} },
}; };
digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
raise_error_if_deinited(common_hal_digitalio_digitalinout_deinited(self));
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
@ -187,6 +190,7 @@ extern const digitalio_digitalio_direction_obj_t digitalio_digitalio_direction_o
STATIC mp_obj_t digitalio_digitalinout_obj_get_direction(mp_obj_t self_in) { STATIC mp_obj_t digitalio_digitalinout_obj_get_direction(mp_obj_t self_in) {
digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(common_hal_digitalio_digitalinout_deinited(self));
enum digitalio_direction_t direction = common_hal_digitalio_digitalinout_get_direction(self); enum digitalio_direction_t direction = common_hal_digitalio_digitalinout_get_direction(self);
if (direction == DIRECTION_INPUT) { if (direction == DIRECTION_INPUT) {
return (mp_obj_t)&digitalio_direction_input_obj; return (mp_obj_t)&digitalio_direction_input_obj;
@ -197,6 +201,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(digitalio_digitalinout_get_direction_obj, digitalio_di
STATIC mp_obj_t digitalio_digitalinout_obj_set_direction(mp_obj_t self_in, mp_obj_t value) { STATIC mp_obj_t digitalio_digitalinout_obj_set_direction(mp_obj_t self_in, mp_obj_t value) {
digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(common_hal_digitalio_digitalinout_deinited(self));
if (value == &digitalio_direction_input_obj) { if (value == &digitalio_direction_input_obj) {
common_hal_digitalio_digitalinout_switch_to_input(self, PULL_NONE); common_hal_digitalio_digitalinout_switch_to_input(self, PULL_NONE);
} else if (value == &digitalio_direction_output_obj) { } else if (value == &digitalio_direction_output_obj) {
@ -221,6 +226,7 @@ const mp_obj_property_t digitalio_digitalio_direction_obj = {
//| //|
STATIC mp_obj_t digitalio_digitalinout_obj_get_value(mp_obj_t self_in) { STATIC mp_obj_t digitalio_digitalinout_obj_get_value(mp_obj_t self_in) {
digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(common_hal_digitalio_digitalinout_deinited(self));
bool value = common_hal_digitalio_digitalinout_get_value(self); bool value = common_hal_digitalio_digitalinout_get_value(self);
return mp_obj_new_bool(value); return mp_obj_new_bool(value);
} }
@ -228,6 +234,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(digitalio_digitalinout_get_value_obj, digitalio_digita
STATIC mp_obj_t digitalio_digitalinout_obj_set_value(mp_obj_t self_in, mp_obj_t value) { STATIC mp_obj_t digitalio_digitalinout_obj_set_value(mp_obj_t self_in, mp_obj_t value) {
digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(common_hal_digitalio_digitalinout_deinited(self));
if (common_hal_digitalio_digitalinout_get_direction(self) == DIRECTION_INPUT) { if (common_hal_digitalio_digitalinout_get_direction(self) == DIRECTION_INPUT) {
mp_raise_AttributeError("Cannot set value when direction is input."); mp_raise_AttributeError("Cannot set value when direction is input.");
return mp_const_none; return mp_const_none;
@ -250,6 +257,7 @@ const mp_obj_property_t digitalio_digitalinout_value_obj = {
//| //|
STATIC mp_obj_t digitalio_digitalinout_obj_get_drive_mode(mp_obj_t self_in) { STATIC mp_obj_t digitalio_digitalinout_obj_get_drive_mode(mp_obj_t self_in) {
digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(common_hal_digitalio_digitalinout_deinited(self));
if (common_hal_digitalio_digitalinout_get_direction(self) == DIRECTION_INPUT) { if (common_hal_digitalio_digitalinout_get_direction(self) == DIRECTION_INPUT) {
mp_raise_AttributeError("Drive mode not used when direction is input."); mp_raise_AttributeError("Drive mode not used when direction is input.");
return mp_const_none; return mp_const_none;
@ -264,6 +272,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(digitalio_digitalinout_get_drive_mode_obj, digitalio_d
STATIC mp_obj_t digitalio_digitalinout_obj_set_drive_mode(mp_obj_t self_in, mp_obj_t drive_mode) { STATIC mp_obj_t digitalio_digitalinout_obj_set_drive_mode(mp_obj_t self_in, mp_obj_t drive_mode) {
digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(common_hal_digitalio_digitalinout_deinited(self));
if (common_hal_digitalio_digitalinout_get_direction(self) == DIRECTION_INPUT) { if (common_hal_digitalio_digitalinout_get_direction(self) == DIRECTION_INPUT) {
mp_raise_AttributeError("Drive mode not used when direction is input."); mp_raise_AttributeError("Drive mode not used when direction is input.");
return mp_const_none; return mp_const_none;
@ -293,6 +302,7 @@ const mp_obj_property_t digitalio_digitalio_drive_mode_obj = {
//| //|
STATIC mp_obj_t digitalio_digitalinout_obj_get_pull(mp_obj_t self_in) { STATIC mp_obj_t digitalio_digitalinout_obj_get_pull(mp_obj_t self_in) {
digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(common_hal_digitalio_digitalinout_deinited(self));
if (common_hal_digitalio_digitalinout_get_direction(self) == DIRECTION_OUTPUT) { if (common_hal_digitalio_digitalinout_get_direction(self) == DIRECTION_OUTPUT) {
mp_raise_AttributeError("Pull not used when direction is output."); mp_raise_AttributeError("Pull not used when direction is output.");
return mp_const_none; return mp_const_none;
@ -309,6 +319,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(digitalio_digitalinout_get_pull_obj, digitalio_digital
STATIC mp_obj_t digitalio_digitalinout_obj_set_pull(mp_obj_t self_in, mp_obj_t pull_obj) { STATIC mp_obj_t digitalio_digitalinout_obj_set_pull(mp_obj_t self_in, mp_obj_t pull_obj) {
digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in); digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(common_hal_digitalio_digitalinout_deinited(self));
if (common_hal_digitalio_digitalinout_get_direction(self) == DIRECTION_OUTPUT) { if (common_hal_digitalio_digitalinout_get_direction(self) == DIRECTION_OUTPUT) {
mp_raise_AttributeError("Pull not used when direction is output."); mp_raise_AttributeError("Pull not used when direction is output.");
return mp_const_none; return mp_const_none;

View File

@ -42,6 +42,7 @@ typedef enum {
digitalinout_result_t common_hal_digitalio_digitalinout_construct(digitalio_digitalinout_obj_t* self, const mcu_pin_obj_t* pin); digitalinout_result_t common_hal_digitalio_digitalinout_construct(digitalio_digitalinout_obj_t* self, const mcu_pin_obj_t* pin);
void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t* self); void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t* self);
bool common_hal_digitalio_digitalinout_deinited(digitalio_digitalinout_obj_t* self);
void common_hal_digitalio_digitalinout_switch_to_input(digitalio_digitalinout_obj_t* self, enum digitalio_pull_t pull); void common_hal_digitalio_digitalinout_switch_to_input(digitalio_digitalinout_obj_t* self, enum digitalio_pull_t pull);
void common_hal_digitalio_digitalinout_switch_to_output(digitalio_digitalinout_obj_t* self, bool value, enum digitalio_drive_mode_t drive_mode); void common_hal_digitalio_digitalinout_switch_to_output(digitalio_digitalinout_obj_t* self, bool value, enum digitalio_drive_mode_t drive_mode);
enum digitalio_direction_t common_hal_digitalio_digitalinout_get_direction(digitalio_digitalinout_obj_t* self); enum digitalio_direction_t common_hal_digitalio_digitalinout_get_direction(digitalio_digitalinout_obj_t* self);

View File

@ -0,0 +1,146 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2016 Radomir Dopieralski for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "py/obj.h"
#include "py/runtime.h"
#include "py/mphal.h"
#include "shared-module/gamepad/GamePad.h"
#include "GamePad.h"
gamepad_obj_t* gamepad_singleton = NULL;
//| .. currentmodule:: gamepad
//|
//| :class:`GamePad` -- Scan buttons for presses
//| ============================================
//|
//| Usage::
//|
//| import board
//| import digitalio
//| import gamepad
//| import time
//|
//| B_UP = 1 << 0
//| B_DOWN = 1 << 1
//|
//|
//| pad = gamepad.GamePad(
//| digitalio.DigitalInOut(board.D0),
//| digitalio.DigitalInOut(board.D1),
//| )
//|
//| y = 0
//| while True:
//| buttons = pad.get_pressed()
//| if buttons & B_UP:
//| y -= 1
//| print(y)
//| elif buttons & B_DOWN:
//| y += 1
//| print(y)
//| time.sleep(0.1)
//| while pad.get_pressed():
//| # Wait for all buttons to be released.
//| time.sleep(0.1)
//|
//| .. class:: GamePad([b1[, b2[, b3[, b4[, b5[, b6[, b7[, b8]]]]]]]])
//|
//| Initializes button scanning routines.
//|
//| The ``b1``-``b8`` parameters are ``DigitalInOut`` objects, which
//| immediately get switched to input with a pull-up, and then scanned
//| regularly for button presses. The order is the same as the order of
//| bits returned by the ``get_pressed`` function. You can re-initialize
//| it with different keys, then the new object will replace the previous
//| one.
//|
//| The basic feature required here is the ability to poll the keys at
//| regular intervals (so that de-bouncing is consistent) and fast enough
//| (so that we don't miss short button presses) while at the same time
//| letting the user code run normally, call blocking functions and wait
//| on delays.
//|
//| They button presses are accumulated, until the ``get_pressed`` method
//| is called, at which point the button state is cleared, and the new
//| button presses start to be recorded.
//|
STATIC mp_obj_t gamepad_make_new(const mp_obj_type_t *type, size_t n_args,
size_t n_kw, const mp_obj_t *args) {
if (!gamepad_singleton) {
gamepad_singleton = m_new_obj(gamepad_obj_t);
gamepad_singleton->base.type = &gamepad_type;
}
gamepad_init(n_args, args);
return MP_OBJ_FROM_PTR(gamepad_singleton);
}
//| .. method:: get_pressed()
//|
//| Get the status of buttons pressed since the last call and clear it.
//|
//| Returns an 8-bit number, with bits that correspond to buttons,
//| which have been pressed (or held down) since the last call to this
//| function set to 1, and the remaining bits set to 0. Then it clears
//| the button state, so that new button presses (or buttons that are
//| held down) can be recorded for the next call.
//|
STATIC mp_obj_t gamepad_get_pressed(mp_obj_t self_in) {
gamepad_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_obj_t gamepad = MP_OBJ_NEW_SMALL_INT(self->pressed);
self->pressed = 0;
return gamepad;
}
MP_DEFINE_CONST_FUN_OBJ_1(gamepad_get_pressed_obj, gamepad_get_pressed);
//| .. method:: deinit()
//|
//| Disable button scanning.
//|
STATIC mp_obj_t gamepad_deinit(mp_obj_t self_in) {
gamepad_singleton = NULL;
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(gamepad_deinit_obj, gamepad_deinit);
STATIC mp_obj_t gamepad_make_new(const mp_obj_type_t *type, size_t n_args,
size_t n_kw, const mp_obj_t *args);
STATIC const mp_rom_map_elem_t gamepad_locals_dict_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_get_pressed), MP_ROM_PTR(&gamepad_get_pressed_obj)},
{ MP_OBJ_NEW_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&gamepad_deinit_obj)},
};
STATIC MP_DEFINE_CONST_DICT(gamepad_locals_dict, gamepad_locals_dict_table);
const mp_obj_type_t gamepad_type = {
{ &mp_type_type },
.name = MP_QSTR_GamePad,
.make_new = gamepad_make_new,
.locals_dict = (mp_obj_dict_t*)&gamepad_locals_dict,
};

View File

@ -0,0 +1,33 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2016 Radomir Dopieralski for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPAD_GAMEPAD_H
#define MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPAD_GAMEPAD_H
extern const mp_obj_type_t gamepad_type;
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_GAMEPAD_GAMEPAD_H

View File

@ -0,0 +1,54 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2016 Radomir Dopieralski for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "py/obj.h"
#include "py/runtime.h"
#include "py/mphal.h"
#include "GamePad.h"
//| :mod:`gamepad` --- Button handling
//| ==================================
//|
//| .. module:: gamepad
//| :synopsis: Button handling
//| :platform: SAMD21
//|
//| .. toctree::
//| :maxdepth: 3
//|
//| GamePad
//|
STATIC const mp_rom_map_elem_t gamepad_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_gamepad) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_GamePad), MP_ROM_PTR(&gamepad_type)},
};
STATIC MP_DEFINE_CONST_DICT(gamepad_module_globals,
gamepad_module_globals_table);
const mp_obj_module_t gamepad_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&gamepad_module_globals,
};

View File

@ -21,9 +21,10 @@ Module / Port SAMD21 SAMD21 Express ESP8266
`board` **Yes** **Yes** **Yes** `board` **Yes** **Yes** **Yes**
`busio` **Yes** **Yes** **Yes** `busio` **Yes** **Yes** **Yes**
`digitalio` **Yes** **Yes** **Yes** `digitalio` **Yes** **Yes** **Yes**
`microcontroller` **Yes** **Yes** **Yes** `gamepad` No **Yes** No
`microcontroller` **Yes** **Yes** **Yes**
`multiterminal` No No **Yes** `multiterminal` No No **Yes**
`neopixel_write` **Yes** **Yes** **Yes** `neopixel_write` **Yes** **Yes** **Yes**
`nvm` No **Yes** No `nvm` No **Yes** No
`os` **Yes** **Yes** **Yes** `os` **Yes** **Yes** **Yes**
`pulseio` No **Yes** No `pulseio` No **Yes** No

View File

@ -29,8 +29,10 @@
#include "lib/utils/context_manager_helpers.h" #include "lib/utils/context_manager_helpers.h"
#include "py/objproperty.h" #include "py/objproperty.h"
#include "py/runtime.h" #include "py/runtime.h"
#include "shared-bindings/microcontroller/Pin.h" #include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/pulseio/PWMOut.h" #include "shared-bindings/pulseio/PWMOut.h"
#include "shared-bindings/util.h"
//| .. currentmodule:: pulseio //| .. currentmodule:: pulseio
//| //|
@ -147,13 +149,15 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pulseio_pwmout___exit___obj, 4, 4, pu
//| (0). 0xffff will always be high, 0 will always be low and 0x7fff will //| (0). 0xffff will always be high, 0 will always be low and 0x7fff will
//| be half high and then half low. //| be half high and then half low.
STATIC mp_obj_t pulseio_pwmout_obj_get_duty_cycle(mp_obj_t self_in) { STATIC mp_obj_t pulseio_pwmout_obj_get_duty_cycle(mp_obj_t self_in) {
pulseio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in); pulseio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in);
return MP_OBJ_NEW_SMALL_INT(common_hal_pulseio_pwmout_get_duty_cycle(self)); raise_error_if_deinited(common_hal_pulseio_pwmout_deinited(self));
return MP_OBJ_NEW_SMALL_INT(common_hal_pulseio_pwmout_get_duty_cycle(self));
} }
MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pwmout_get_duty_cycle_obj, pulseio_pwmout_obj_get_duty_cycle); MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pwmout_get_duty_cycle_obj, pulseio_pwmout_obj_get_duty_cycle);
STATIC mp_obj_t pulseio_pwmout_obj_set_duty_cycle(mp_obj_t self_in, mp_obj_t duty_cycle) { STATIC mp_obj_t pulseio_pwmout_obj_set_duty_cycle(mp_obj_t self_in, mp_obj_t duty_cycle) {
pulseio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in); pulseio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(common_hal_pulseio_pwmout_deinited(self));
mp_int_t duty = mp_obj_get_int(duty_cycle); mp_int_t duty = mp_obj_get_int(duty_cycle);
if (duty < 0 || duty > 0xffff) { if (duty < 0 || duty > 0xffff) {
mp_raise_ValueError("PWM duty_cycle must be between 0 and 65535 inclusive (16 bit resolution)"); mp_raise_ValueError("PWM duty_cycle must be between 0 and 65535 inclusive (16 bit resolution)");
@ -176,13 +180,15 @@ const mp_obj_property_t pulseio_pwmout_duty_cycle_obj = {
//| second). Only writeable when constructed with ``variable_frequency=True``. //| second). Only writeable when constructed with ``variable_frequency=True``.
//| //|
STATIC mp_obj_t pulseio_pwmout_obj_get_frequency(mp_obj_t self_in) { STATIC mp_obj_t pulseio_pwmout_obj_get_frequency(mp_obj_t self_in) {
pulseio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in); pulseio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in);
return MP_OBJ_NEW_SMALL_INT(common_hal_pulseio_pwmout_get_frequency(self)); raise_error_if_deinited(common_hal_pulseio_pwmout_deinited(self));
return MP_OBJ_NEW_SMALL_INT(common_hal_pulseio_pwmout_get_frequency(self));
} }
MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pwmout_get_frequency_obj, pulseio_pwmout_obj_get_frequency); MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pwmout_get_frequency_obj, pulseio_pwmout_obj_get_frequency);
STATIC mp_obj_t pulseio_pwmout_obj_set_frequency(mp_obj_t self_in, mp_obj_t frequency) { STATIC mp_obj_t pulseio_pwmout_obj_set_frequency(mp_obj_t self_in, mp_obj_t frequency) {
pulseio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in); pulseio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(common_hal_pulseio_pwmout_deinited(self));
if (!common_hal_pulseio_pwmout_get_variable_frequency(self)) { if (!common_hal_pulseio_pwmout_get_variable_frequency(self)) {
mp_raise_AttributeError( mp_raise_AttributeError(
"PWM frequency not writeable when variable_frequency is False on " "PWM frequency not writeable when variable_frequency is False on "

View File

@ -36,6 +36,7 @@ extern void common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self,
const mcu_pin_obj_t* pin, uint16_t duty, uint32_t frequency, const mcu_pin_obj_t* pin, uint16_t duty, uint32_t frequency,
bool variable_frequency); bool variable_frequency);
extern void common_hal_pulseio_pwmout_deinit(pulseio_pwmout_obj_t* self); extern void common_hal_pulseio_pwmout_deinit(pulseio_pwmout_obj_t* self);
extern bool common_hal_pulseio_pwmout_deinited(pulseio_pwmout_obj_t* self);
extern void common_hal_pulseio_pwmout_set_duty_cycle(pulseio_pwmout_obj_t* self, uint16_t duty); extern void common_hal_pulseio_pwmout_set_duty_cycle(pulseio_pwmout_obj_t* self, uint16_t duty);
extern uint16_t common_hal_pulseio_pwmout_get_duty_cycle(pulseio_pwmout_obj_t* self); extern uint16_t common_hal_pulseio_pwmout_get_duty_cycle(pulseio_pwmout_obj_t* self);
extern void common_hal_pulseio_pwmout_set_frequency(pulseio_pwmout_obj_t* self, uint32_t frequency); extern void common_hal_pulseio_pwmout_set_frequency(pulseio_pwmout_obj_t* self, uint32_t frequency);

View File

@ -32,6 +32,7 @@
#include "py/runtime0.h" #include "py/runtime0.h"
#include "shared-bindings/microcontroller/Pin.h" #include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/pulseio/PulseIn.h" #include "shared-bindings/pulseio/PulseIn.h"
#include "shared-bindings/util.h"
//| .. currentmodule:: pulseio //| .. currentmodule:: pulseio
//| //|
@ -139,6 +140,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pulseio_pulsein___exit___obj, 4, 4, p
//| //|
STATIC mp_obj_t pulseio_pulsein_obj_pause(mp_obj_t self_in) { STATIC mp_obj_t pulseio_pulsein_obj_pause(mp_obj_t self_in) {
pulseio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); pulseio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(common_hal_pulseio_pulsein_deinited(self));
common_hal_pulseio_pulsein_pause(self); common_hal_pulseio_pulsein_pause(self);
return mp_const_none; return mp_const_none;
@ -162,6 +164,8 @@ STATIC mp_obj_t pulseio_pulsein_obj_resume(size_t n_args, const mp_obj_t *pos_ar
{ MP_QSTR_trigger_duration, MP_ARG_INT, {.u_int = 0} }, { MP_QSTR_trigger_duration, MP_ARG_INT, {.u_int = 0} },
}; };
pulseio_pulsein_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); pulseio_pulsein_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
raise_error_if_deinited(common_hal_pulseio_pulsein_deinited(self));
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
@ -176,6 +180,7 @@ MP_DEFINE_CONST_FUN_OBJ_KW(pulseio_pulsein_resume_obj, 1, pulseio_pulsein_obj_re
//| //|
STATIC mp_obj_t pulseio_pulsein_obj_clear(mp_obj_t self_in) { STATIC mp_obj_t pulseio_pulsein_obj_clear(mp_obj_t self_in) {
pulseio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); pulseio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(common_hal_pulseio_pulsein_deinited(self));
common_hal_pulseio_pulsein_clear(self); common_hal_pulseio_pulsein_clear(self);
return mp_const_none; return mp_const_none;
@ -188,6 +193,8 @@ MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pulsein_clear_obj, pulseio_pulsein_obj_clear);
//| //|
STATIC mp_obj_t pulseio_pulsein_obj_popleft(mp_obj_t self_in) { STATIC mp_obj_t pulseio_pulsein_obj_popleft(mp_obj_t self_in) {
pulseio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); pulseio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(common_hal_pulseio_pulsein_deinited(self));
return MP_OBJ_NEW_SMALL_INT(common_hal_pulseio_pulsein_popleft(self)); return MP_OBJ_NEW_SMALL_INT(common_hal_pulseio_pulsein_popleft(self));
} }
MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pulsein_popleft_obj, pulseio_pulsein_obj_popleft); MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pulsein_popleft_obj, pulseio_pulsein_obj_popleft);
@ -199,6 +206,8 @@ MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pulsein_popleft_obj, pulseio_pulsein_obj_pople
//| //|
STATIC mp_obj_t pulseio_pulsein_obj_get_maxlen(mp_obj_t self_in) { STATIC mp_obj_t pulseio_pulsein_obj_get_maxlen(mp_obj_t self_in) {
pulseio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); pulseio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(common_hal_pulseio_pulsein_deinited(self));
return MP_OBJ_NEW_SMALL_INT(common_hal_pulseio_pulsein_get_maxlen(self)); return MP_OBJ_NEW_SMALL_INT(common_hal_pulseio_pulsein_get_maxlen(self));
} }
MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pulsein_get_maxlen_obj, pulseio_pulsein_obj_get_maxlen); MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pulsein_get_maxlen_obj, pulseio_pulsein_obj_get_maxlen);
@ -221,6 +230,7 @@ const mp_obj_property_t pulseio_pulsein_maxlen_obj = {
//| //|
STATIC mp_obj_t pulsein_unary_op(mp_uint_t op, mp_obj_t self_in) { STATIC mp_obj_t pulsein_unary_op(mp_uint_t op, mp_obj_t self_in) {
pulseio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); pulseio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(common_hal_pulseio_pulsein_deinited(self));
uint16_t len = common_hal_pulseio_pulsein_get_len(self); uint16_t len = common_hal_pulseio_pulsein_get_len(self);
switch (op) { switch (op) {
case MP_UNARY_OP_BOOL: return mp_obj_new_bool(len != 0); case MP_UNARY_OP_BOOL: return mp_obj_new_bool(len != 0);
@ -244,6 +254,8 @@ STATIC mp_obj_t pulsein_subscr(mp_obj_t self_in, mp_obj_t index_obj, mp_obj_t va
mp_raise_AttributeError("Cannot delete values"); mp_raise_AttributeError("Cannot delete values");
} else { } else {
pulseio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in); pulseio_pulsein_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(common_hal_pulseio_pulsein_deinited(self));
if (MP_OBJ_IS_TYPE(index_obj, &mp_type_slice)) { if (MP_OBJ_IS_TYPE(index_obj, &mp_type_slice)) {
mp_raise_NotImplementedError("Slices not supported"); mp_raise_NotImplementedError("Slices not supported");
} else { } else {

View File

@ -35,6 +35,7 @@ extern const mp_obj_type_t pulseio_pulsein_type;
extern void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, extern void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self,
const mcu_pin_obj_t* pin, uint16_t maxlen, bool idle_state); const mcu_pin_obj_t* pin, uint16_t maxlen, bool idle_state);
extern void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t* self); extern void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t* self);
extern bool common_hal_pulseio_pulsein_deinited(pulseio_pulsein_obj_t* self);
extern void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t* self); extern void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t* self);
extern void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self, uint16_t trigger_duration); extern void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self, uint16_t trigger_duration);
extern void common_hal_pulseio_pulsein_clear(pulseio_pulsein_obj_t* self); extern void common_hal_pulseio_pulsein_clear(pulseio_pulsein_obj_t* self);

View File

@ -29,9 +29,11 @@
#include "lib/utils/context_manager_helpers.h" #include "lib/utils/context_manager_helpers.h"
#include "py/objproperty.h" #include "py/objproperty.h"
#include "py/runtime.h" #include "py/runtime.h"
#include "shared-bindings/microcontroller/Pin.h" #include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/pulseio/PulseOut.h" #include "shared-bindings/pulseio/PulseOut.h"
#include "shared-bindings/pulseio/PWMOut.h" #include "shared-bindings/pulseio/PWMOut.h"
#include "shared-bindings/util.h"
//| .. currentmodule:: pulseio //| .. currentmodule:: pulseio
//| //|
@ -123,6 +125,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pulseio_pulseout___exit___obj, 4, 4,
//| //|
STATIC mp_obj_t pulseio_pulseout_obj_send(mp_obj_t self_in, mp_obj_t pulses) { STATIC mp_obj_t pulseio_pulseout_obj_send(mp_obj_t self_in, mp_obj_t pulses) {
pulseio_pulseout_obj_t *self = MP_OBJ_TO_PTR(self_in); pulseio_pulseout_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(common_hal_pulseio_pulseout_deinited(self));
mp_buffer_info_t bufinfo; mp_buffer_info_t bufinfo;
mp_get_buffer_raise(pulses, &bufinfo, MP_BUFFER_READ); mp_get_buffer_raise(pulses, &bufinfo, MP_BUFFER_READ);

View File

@ -36,6 +36,7 @@ extern const mp_obj_type_t pulseio_pulseout_type;
extern void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self, extern void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self,
const pulseio_pwmout_obj_t* carrier); const pulseio_pwmout_obj_t* carrier);
extern void common_hal_pulseio_pulseout_deinit(pulseio_pulseout_obj_t* self); extern void common_hal_pulseio_pulseout_deinit(pulseio_pulseout_obj_t* self);
extern bool common_hal_pulseio_pulseout_deinited(pulseio_pulseout_obj_t* self);
extern void common_hal_pulseio_pulseout_send(pulseio_pulseout_obj_t* self, extern void common_hal_pulseio_pulseout_send(pulseio_pulseout_obj_t* self,
uint16_t* pulses, uint16_t len); uint16_t* pulses, uint16_t len);

View File

@ -24,6 +24,7 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
#include <stdint.h>
#include <string.h> #include <string.h>
#include "lib/utils/context_manager_helpers.h" #include "lib/utils/context_manager_helpers.h"
@ -34,6 +35,7 @@
#include "py/runtime.h" #include "py/runtime.h"
#include "shared-bindings/microcontroller/Pin.h" #include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/touchio/TouchIn.h" #include "shared-bindings/touchio/TouchIn.h"
#include "shared-bindings/util.h"
//| .. currentmodule:: touchio //| .. currentmodule:: touchio
//| //|
@ -106,13 +108,15 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(touchio_touchin___exit___obj, 4, 4, t
//| .. attribute:: value //| .. attribute:: value
//| //|
//| Whether the touch pad is being touched or not. //| Whether the touch pad is being touched or not.
//| True if `raw_value` > `threshold`.
//| //|
//| :return: True when touched, False otherwise. //| :return: True when touched, False otherwise.
//| :rtype: bool //| :rtype: bool
//| //|
STATIC mp_obj_t touchio_touchin_obj_get_value(mp_obj_t self_in) { STATIC mp_obj_t touchio_touchin_obj_get_value(mp_obj_t self_in) {
touchio_touchin_obj_t *self = MP_OBJ_TO_PTR(self_in); touchio_touchin_obj_t *self = MP_OBJ_TO_PTR(self_in);
return mp_obj_new_bool(common_hal_touchio_touchin_get_value(self)); raise_error_if_deinited(common_hal_touchio_touchin_deinited(self));
return mp_obj_new_bool(common_hal_touchio_touchin_get_value(self));
} }
MP_DEFINE_CONST_FUN_OBJ_1(touchio_touchin_get_value_obj, touchio_touchin_obj_get_value); MP_DEFINE_CONST_FUN_OBJ_1(touchio_touchin_get_value_obj, touchio_touchin_obj_get_value);
@ -123,12 +127,79 @@ const mp_obj_property_t touchio_touchin_value_obj = {
(mp_obj_t)&mp_const_none_obj}, (mp_obj_t)&mp_const_none_obj},
}; };
//| .. attribute:: raw_value
//|
//| The raw touch measurement. Not settable.
//|
//| :return: an integer >= 0
//| :rtype: int
//|
STATIC mp_obj_t touchio_touchin_obj_get_raw_value(mp_obj_t self_in) {
touchio_touchin_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(common_hal_touchio_touchin_deinited(self));
return MP_OBJ_NEW_SMALL_INT(common_hal_touchio_touchin_get_raw_value(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(touchio_touchin_get_raw_value_obj, touchio_touchin_obj_get_raw_value);
const mp_obj_property_t touchio_touchin_raw_value_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&touchio_touchin_get_raw_value_obj,
(mp_obj_t)&mp_const_none_obj,
(mp_obj_t)&mp_const_none_obj},
};
//| .. attribute:: threshold
//|
//| `value` will return True if `raw_value` is greater than than this threshold.
//| When the **TouchIn** object is created, an initial `raw_value` is read from the pin,
//| and then `threshold` is set to be 100 + that value.
//|
//| You can set the threshold to a different value to make the pin more or less sensitive.
//|
//| :return: an integer >= 0
//| :rtype: int
//|
STATIC mp_obj_t touchio_touchin_obj_get_threshold(mp_obj_t self_in) {
touchio_touchin_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(common_hal_touchio_touchin_deinited(self));
return MP_OBJ_NEW_SMALL_INT(common_hal_touchio_touchin_get_threshold(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(touchio_touchin_get_threshold_obj, touchio_touchin_obj_get_threshold);
STATIC mp_obj_t touchio_touchin_obj_set_threshold(mp_obj_t self_in, mp_obj_t threshold_obj) {
touchio_touchin_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(common_hal_touchio_touchin_deinited(self));
uint32_t new_threshold = mp_obj_get_int(threshold_obj);
if (new_threshold < 0 || new_threshold > UINT16_MAX) {
// I would use MP_STRINGIFY(UINT16_MAX), but that prints "0xffff" instead of 65536.
mp_raise_ValueError("threshold must be in the range 0-65536");
}
common_hal_touchio_touchin_set_threshold(self, new_threshold);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(touchio_touchin_set_threshold_obj, touchio_touchin_obj_set_threshold);
const mp_obj_property_t touchio_touchin_threshold_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&touchio_touchin_get_threshold_obj,
(mp_obj_t)&touchio_touchin_set_threshold_obj,
(mp_obj_t)&mp_const_none_obj},
};
STATIC const mp_rom_map_elem_t touchio_touchin_locals_dict_table[] = { STATIC const mp_rom_map_elem_t touchio_touchin_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&touchio_touchin___exit___obj) }, { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&touchio_touchin___exit___obj) },
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&touchio_touchin_deinit_obj) }, { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&touchio_touchin_deinit_obj) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_value), MP_ROM_PTR(&touchio_touchin_value_obj)}, { MP_OBJ_NEW_QSTR(MP_QSTR_value), MP_ROM_PTR(&touchio_touchin_value_obj)},
{ MP_OBJ_NEW_QSTR(MP_QSTR_raw_value), MP_ROM_PTR(&touchio_touchin_raw_value_obj)},
{ MP_OBJ_NEW_QSTR(MP_QSTR_threshold), MP_ROM_PTR(&touchio_touchin_threshold_obj)},
}; };
STATIC MP_DEFINE_CONST_DICT(touchio_touchin_locals_dict, touchio_touchin_locals_dict_table); STATIC MP_DEFINE_CONST_DICT(touchio_touchin_locals_dict, touchio_touchin_locals_dict_table);

View File

@ -34,6 +34,10 @@ extern const mp_obj_type_t touchio_touchin_type;
void common_hal_touchio_touchin_construct(touchio_touchin_obj_t* self, const mcu_pin_obj_t *pin); void common_hal_touchio_touchin_construct(touchio_touchin_obj_t* self, const mcu_pin_obj_t *pin);
void common_hal_touchio_touchin_deinit(touchio_touchin_obj_t* self); void common_hal_touchio_touchin_deinit(touchio_touchin_obj_t* self);
bool common_hal_touchio_touchin_deinited(touchio_touchin_obj_t* self);
bool common_hal_touchio_touchin_get_value(touchio_touchin_obj_t *self); bool common_hal_touchio_touchin_get_value(touchio_touchin_obj_t *self);
uint16_t common_hal_touchio_touchin_get_raw_value(touchio_touchin_obj_t *self);
uint16_t common_hal_touchio_touchin_get_threshold(touchio_touchin_obj_t *self);
void common_hal_touchio_touchin_set_threshold(touchio_touchin_obj_t *self, uint16_t new_threshold);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_TOUCHIO_TOUCHIN_H #endif // MICROPY_INCLUDED_SHARED_BINDINGS_TOUCHIO_TOUCHIN_H

42
shared-bindings/util.c Normal file
View File

@ -0,0 +1,42 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2017 Dan Halbert for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_UTIL_H
#define MICROPY_INCLUDED_SHARED_BINDINGS_UTIL_H
#include "py/runtime.h"
#include "shared-bindings/util.h"
// Check if pin is None. If so, deinit() has already been called on the object, so complain.
void raise_error_if_deinited(bool deinited) {
if (deinited) {
mp_raise_ValueError("Object has been deinitialized and can no longer be used. Create a new object.");
}
}
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_UTIL_H

33
shared-bindings/util.h Normal file
View File

@ -0,0 +1,33 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2017 Dan Halbert for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_UTIL_H
#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_UTIL_H
void raise_error_if_deinited(bool deinited);
#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_UTIL_H

View File

@ -162,7 +162,15 @@ void shared_module_bitbangio_i2c_construct(bitbangio_i2c_obj_t *self,
stop(self); stop(self);
} }
bool shared_module_bitbangio_i2c_deinited(bitbangio_i2c_obj_t *self) {
// If one is deinited, both will be.
return common_hal_digitalio_digitalinout_deinited(&self->scl);
}
void shared_module_bitbangio_i2c_deinit(bitbangio_i2c_obj_t *self) { void shared_module_bitbangio_i2c_deinit(bitbangio_i2c_obj_t *self) {
if (shared_module_bitbangio_i2c_deinited(self)) {
return;
}
common_hal_digitalio_digitalinout_deinit(&self->scl); common_hal_digitalio_digitalinout_deinit(&self->scl);
common_hal_digitalio_digitalinout_deinit(&self->sda); common_hal_digitalio_digitalinout_deinit(&self->sda);
} }

View File

@ -38,7 +38,14 @@ void shared_module_bitbangio_onewire_construct(bitbangio_onewire_obj_t* self,
common_hal_digitalio_digitalinout_construct(&self->pin, pin); common_hal_digitalio_digitalinout_construct(&self->pin, pin);
} }
bool shared_module_bitbangio_onewire_deinited(bitbangio_onewire_obj_t* self) {
return common_hal_digitalio_digitalinout_deinited(&self->pin);
}
void shared_module_bitbangio_onewire_deinit(bitbangio_onewire_obj_t* self) { void shared_module_bitbangio_onewire_deinit(bitbangio_onewire_obj_t* self) {
if (shared_module_bitbangio_onewire_deinited(self)) {
return;
}
common_hal_digitalio_digitalinout_deinit(&self->pin); common_hal_digitalio_digitalinout_deinit(&self->pin);
} }

View File

@ -70,7 +70,14 @@ void shared_module_bitbangio_spi_construct(bitbangio_spi_obj_t *self,
self->phase = 0; self->phase = 0;
} }
bool shared_module_bitbangio_spi_deinited(bitbangio_spi_obj_t *self) {
return common_hal_digitalio_digitalinout_deinited(&self->clock);
}
void shared_module_bitbangio_spi_deinit(bitbangio_spi_obj_t *self) { void shared_module_bitbangio_spi_deinit(bitbangio_spi_obj_t *self) {
if (shared_module_bitbangio_spi_deinited(self)) {
return;
}
common_hal_digitalio_digitalinout_deinit(&self->clock); common_hal_digitalio_digitalinout_deinit(&self->clock);
if (self->has_mosi) { if (self->has_mosi) {
common_hal_digitalio_digitalinout_deinit(&self->mosi); common_hal_digitalio_digitalinout_deinit(&self->mosi);

View File

@ -34,6 +34,10 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
shared_module_bitbangio_i2c_construct(&self->bitbang, scl, sda, freq); shared_module_bitbangio_i2c_construct(&self->bitbang, scl, sda, freq);
} }
bool common_hal_busio_i2c_deinited(busio_i2c_obj_t *self) {
return shared_module_bitbangio_i2c_deinited(&self->bitbang);
}
void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) { void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) {
shared_module_bitbangio_i2c_deinit(&self->bitbang); shared_module_bitbangio_i2c_deinit(&self->bitbang);
} }

View File

@ -34,7 +34,14 @@ void common_hal_busio_onewire_construct(busio_onewire_obj_t* self,
shared_module_bitbangio_onewire_construct(&self->bitbang, pin); shared_module_bitbangio_onewire_construct(&self->bitbang, pin);
} }
bool common_hal_busio_onewire_deinited(busio_onewire_obj_t* self) {
return shared_module_bitbangio_onewire_deinited(&self->bitbang);
}
void common_hal_busio_onewire_deinit(busio_onewire_obj_t* self) { void common_hal_busio_onewire_deinit(busio_onewire_obj_t* self) {
if (common_hal_busio_onewire_deinited(self)) {
return;
}
shared_module_bitbangio_onewire_deinit(&self->bitbang); shared_module_bitbangio_onewire_deinit(&self->bitbang);
} }

View File

@ -0,0 +1,46 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2016 Radomir Dopieralski for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdbool.h>
#include "__init__.h"
#include "GamePad.h"
#include "shared-bindings/digitalio/Pull.h"
#include "shared-bindings/digitalio/DigitalInOut.h"
void gamepad_init(size_t n_pins, const mp_obj_t* pins) {
for (size_t i=0; i<8; ++i) {
gamepad_singleton->pins[i] = NULL;
}
for (size_t i=0; i<n_pins; ++i) {
digitalio_digitalinout_obj_t *pin = MP_OBJ_TO_PTR(pins[i]);
gamepad_singleton->pins[i] = pin;
common_hal_digitalio_digitalinout_switch_to_input(pin, PULL_UP);
}
gamepad_singleton->last = 0;
}

View File

@ -0,0 +1,45 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2016 Radomir Dopieralski for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef MICROPY_INCLUDED_GAMEPAD_GAMEPAD_H
#define MICROPY_INCLUDED_GAMEPAD_GAMEPAD_H
#include <stdint.h>
#include "shared-bindings/digitalio/DigitalInOut.h"
typedef struct {
mp_obj_base_t base;
digitalio_digitalinout_obj_t* pins[8];
volatile uint8_t last;
volatile uint8_t pressed;
} gamepad_obj_t;
extern gamepad_obj_t* gamepad_singleton;
void gamepad_init(size_t n_pins, const mp_obj_t* pins);
#endif // MICROPY_INCLUDED_GAMEPAD_GAMEPAD_H

View File

@ -0,0 +1,51 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2016 Radomir Dopieralski for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdbool.h>
#include "__init__.h"
#include "GamePad.h"
#include "shared-bindings/digitalio/DigitalInOut.h"
void gamepad_tick(void) {
if (!gamepad_singleton) {
return;
}
uint8_t gamepad_current = 0;
for (int i=0; i<8; ++i) {
digitalio_digitalinout_obj_t* pin = gamepad_singleton->pins[i];
if (!pin) {
break;
}
if (!common_hal_digitalio_digitalinout_get_value(pin)) {
gamepad_current |= 1<<i;
}
}
gamepad_singleton->pressed |= gamepad_singleton->last & gamepad_current;
gamepad_singleton->last = gamepad_current;
}

View File

@ -0,0 +1,32 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2016 Scott Shawcroft for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef MICROPY_INCLUDED_GAMEPAD_H
#define MICROPY_INCLUDED_GAMEPAD_H
void gamepad_tick(void);
#endif // MICROPY_INCLUDED_GAMEPAD_H

View File

@ -29,7 +29,7 @@
#include <stdbool.h> #include <stdbool.h>
void filesystem_init(void); void filesystem_init(bool create_allowed);
void filesystem_flush(void); void filesystem_flush(void);
void filesystem_default_writeable(bool writeable); void filesystem_default_writeable(bool writeable);
bool filesystem_present(void); bool filesystem_present(void);