Do not allow a *io object to be used after deinit().

Fixes #278, #277, #276, #275.
crypto-aes
Dan Halbert 5 years ago committed by Scott Shawcroft
parent c2bb9e2eb5
commit c478c10923

@ -135,9 +135,13 @@ ifeq ($(DEBUG), 1)
# -DMICROPY_DEBUG_MODULES may also be added to an -flto build, if you wish.
CFLAGS += -Os -ggdb -DNDEBUG -DENABLE_MICRO_TRACE_BUFFER -DMICROPY_DEBUG_MODULES
else
# -finline-limit can shrink the image size. -finline-limit=80 or so is similar to not having it on.
# GCC_INLINE_LIMIT specifies -finline-limit, which can shrink the image size.
# -finline-limit=80 or so is similar to not having it on.
# There is no simple default value, though.
CFLAGS += -Os -DNDEBUG -flto -finline-limit=39
ifeq ($(FLASH_IMPL),internal_flash.c)
GCC_INLINE_LIMIT = -finline-limit=19
endif
CFLAGS += -Os -DNDEBUG -flto $(GCC_INLINE_LIMIT)
endif
ifneq ($(FROZEN_DIR),)
@ -272,7 +276,8 @@ SRC_BINDINGS_ENUMS = \
digitalio/Direction.c \
digitalio/DriveMode.c \
digitalio/Pull.c \
help.c
help.c \
util.c
SRC_COMMON_HAL_EXPANDED = $(addprefix shared-bindings/, $(SRC_COMMON_HAL)) \
$(addprefix shared-bindings/, $(SRC_BINDINGS_ENUMS)) \

@ -5,3 +5,4 @@ USB_PID = 0x8015
FLASH_IMPL = internal_flash.c
CHIP_VARIANT = SAMD21G18A

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

@ -59,10 +59,18 @@ void common_hal_analogio_analogout_construct(analogio_analogout_obj_t* self,
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) {
if (common_hal_analogio_analogout_deinited(self)) {
return;
}
dac_disable(&self->dac_instance);
dac_chan_disable(&self->dac_instance, DAC_CHANNEL_0);
reset_pin(PIN_PA02);
self->deinited = true;
}
void common_hal_analogio_analogout_set_value(analogio_analogout_obj_t *self,

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

@ -145,11 +145,20 @@ void common_hal_audiobusio_pdmin_construct(audiobusio_pdmin_obj_t* self,
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) {
if (common_hal_audiobusio_pdmin_deinited(self)) {
return;
}
i2s_disable(&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) {

@ -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) {
if (common_hal_audioio_audioout_deinited(self)) {
return;
}
refcount--;
if (refcount == 0) {
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);
}
self->pin = mp_const_none;
}
static void set_timer_frequency(uint32_t frequency) {

@ -95,10 +95,19 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
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) {
if (common_hal_busio_i2c_deinited(self)) {
return;
}
i2c_master_reset(&self->i2c_master_instance);
reset_pin(self->sda_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) {

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

@ -243,7 +243,14 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
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) {
if (common_hal_busio_uart_deinited(self)) {
return;
}
self->uart_instance.hw->USART.INTENCLR.bit.RXC = true;
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);
reset_pin(self->rx_pin);
reset_pin(self->tx_pin);
self->rx_pin = NO_PIN;
self->tx_pin = NO_PIN;
}
// Read characters.

@ -51,8 +51,16 @@ digitalinout_result_t common_hal_digitalio_digitalinout_construct(
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) {
if (common_hal_digitalio_digitalinout_deinited(self)) {
return;
}
reset_pin(self->pin->pin);
self->pin = mp_const_none;
}
void common_hal_digitalio_digitalinout_switch_to_input(

@ -238,7 +238,14 @@ void common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self,
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;
uint8_t index = (((uint32_t) t->tcc) - ((uint32_t) TCC0)) / 0x400;
timer_refcount[index]--;
@ -260,6 +267,7 @@ extern void common_hal_pulseio_pwmout_deinit(pulseio_pwmout_obj_t* self) {
}
}
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) {

@ -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);
}
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) {
if (common_hal_pulseio_pulsein_deinited(self)) {
return;
}
extint_chan_disable_callback(self->channel, EXTINT_CALLBACK_TYPE_DETECT);
active_pulseins[self->channel] = NULL;
reset_pin(self->pin);
self->pin = NO_PIN;
}
void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t* self) {

@ -34,6 +34,7 @@
#include "mpconfigport.h"
#include "py/gc.h"
#include "py/runtime.h"
#include "samd21_pins.h"
#include "shared-bindings/pulseio/PulseOut.h"
#undef ENABLE
@ -128,7 +129,14 @@ void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self,
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) {
if (common_hal_pulseio_pulseout_deinited(self)) {
return;
}
PortGroup *const port_base = port_get_group_from_gpio_pin(self->pin);
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));
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) {

@ -33,6 +33,7 @@
#include "py/mphal.h"
#include "shared-bindings/touchio/TouchIn.h"
#include "samd21_pins.h"
#include "tick.h"
#include "adafruit_ptc.h"
@ -81,9 +82,17 @@ void common_hal_touchio_touchin_construct(touchio_touchin_obj_t* self,
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) {
// 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() {

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

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

@ -46,8 +46,16 @@ void common_hal_analogio_analogin_construct(analogio_analogin_obj_t* self,
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) {
if (common_hal_analogio_analogin_deinited(self)) {
return;
}
adc_in_use = false;
self->deinited = true;
}
uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) {

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

@ -38,6 +38,10 @@ void common_hal_analogio_analogout_construct(analogio_analogout_obj_t* self,
"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) {
}

@ -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);
}
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) {
if (common_hal_busio_spi_deinited(self)) {
return;
}
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, 0);
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.
gpio_output_set(0x0, 0x0, 0x0, 0x7 << 12);
self->deinited = true;
}
bool common_hal_busio_spi_configure(busio_spi_obj_t *self,

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

@ -92,8 +92,16 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
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) {
if (common_hal_busio_uart_deinited(self)) {
return;
}
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) {

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

@ -41,13 +41,21 @@ digitalinout_result_t common_hal_digitalio_digitalinout_construct(
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) {
if (common_hal_digitalio_digitalinout_deinited(self)) {
return;
}
if (self->pin->gpio_number < 16) {
uint32_t pin_mask = 1 << self->pin->gpio_number;
gpio_output_set(0x0, 0x0, 0x0, pin_mask);
PIN_FUNC_SELECT(self->pin->peripheral, 0);
PIN_PULLUP_DIS(self->pin->peripheral);
}
self->pin = mp_const_none;
}
void common_hal_digitalio_digitalinout_switch_to_input(

@ -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_start();
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_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.
pwm_set_duty(duty >> 6, self->channel);
pwm_start();

@ -37,8 +37,11 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self,
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) {

@ -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."));
}
bool common_hal_pulseio_pulseout_deinited(pulseio_pulseout_obj_t* self) {
return true;
}
void common_hal_pulseio_pulseout_deinit(pulseio_pulseout_obj_t* self) {
}

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

@ -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_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);
float common_hal_analogio_analogin_get_reference_voltage(analogio_analogin_obj_t* self);

@ -30,8 +30,10 @@
#include "lib/utils/context_manager_helpers.h"
#include "py/objproperty.h"
#include "py/runtime.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/analogio/AnalogOut.h"
#include "shared-bindings/util.h"
//| .. 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) {
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);
if (v >= (1 << 16)) {
mp_raise_ValueError("AnalogOut is only 16 bits. Value must be less than 65536.");

@ -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_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);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGIO_ANALOGOUT_H

@ -32,6 +32,7 @@
#include "py/runtime.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/audiobusio/PDMIn.h"
#include "shared-bindings/util.h"
//| .. 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) {
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)) {
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) {
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));
}
MP_DEFINE_CONST_FUN_OBJ_1(audiobusio_pdmin_get_frequency_obj, audiobusio_pdmin_obj_get_frequency);

@ -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,
uint32_t frequency, uint8_t bit_depth, bool mono, uint8_t oversample);
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,
uint16_t* buffer, uint32_t length);
uint8_t common_hal_audiobusio_pdmin_get_bit_depth(audiobusio_pdmin_obj_t* self);

@ -32,6 +32,7 @@
#include "py/runtime.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/audioio/AudioOut.h"
#include "shared-bindings/util.h"
//| .. 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} },
};
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_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) {
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);
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) {
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));
}
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) {
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));
}
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) {
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));
return mp_const_none;
}

@ -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);
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_stop(audioio_audioout_obj_t* self);
bool common_hal_audioio_audioout_get_playing(audioio_audioout_obj_t* self);

@ -29,6 +29,7 @@
#include "shared-bindings/bitbangio/I2C.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/util.h"
#include "lib/utils/buffer_helper.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) {
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);
raise_error_if_deinited(shared_module_bitbangio_i2c_deinited(self));
self->base.type = &bitbangio_i2c_type;
mp_map_t kw_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) {
bitbangio_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(shared_module_bitbangio_i2c_deinited(self));
check_lock(self);
mp_obj_t list = mp_obj_new_list(0, NULL);
// 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.
//|
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);
@ -141,7 +146,9 @@ MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_i2c_try_lock_obj, bitbangio_i2c_obj_try_lock
//| Releases the I2C lock.
//|
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;
}
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} },
};
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_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
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} },
};
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);
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);

@ -42,6 +42,7 @@ extern void shared_module_bitbangio_i2c_construct(bitbangio_i2c_obj_t *self,
uint32_t frequency);
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_has_lock(bitbangio_i2c_obj_t *self);

@ -32,6 +32,7 @@
#include "py/runtime0.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/bitbangio/OneWire.h"
#include "shared-bindings/util.h"
//| .. 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) {
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));
}
@ -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) {
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));
}
@ -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) {
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));
return mp_const_none;

@ -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,
const mcu_pin_obj_t* pin);
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_read_bit(bitbangio_onewire_obj_t* self);
extern void shared_module_bitbangio_onewire_write_bit(bitbangio_onewire_obj_t* self, bool bit);

@ -31,6 +31,7 @@
#include "shared-bindings/bitbangio/SPI.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/util.h"
#include "lib/utils/context_manager_helpers.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} },
};
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);
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);
@ -168,7 +170,9 @@ MP_DEFINE_CONST_FUN_OBJ_KW(bitbangio_spi_configure_obj, 1, bitbangio_spi_configu
//| :rtype: bool
//|
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);
@ -177,7 +181,9 @@ MP_DEFINE_CONST_FUN_OBJ_1(bitbangio_spi_try_lock_obj, bitbangio_spi_obj_try_lock
//| Releases the SPI lock.
//|
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;
}
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.
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_get_buffer_raise(wr_buf, &src, MP_BUFFER_READ);
bitbangio_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_lock(self);
bool ok = shared_module_bitbangio_spi_write(self, src.buf, src.len);
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.
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_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE);
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) {
mp_raise_OSError(MP_EIO);
}

@ -41,6 +41,7 @@ extern void shared_module_bitbangio_spi_construct(bitbangio_spi_obj_t *self,
const mcu_pin_obj_t * miso);
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,
uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t bits);

@ -29,6 +29,7 @@
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/busio/I2C.h"
#include "shared-bindings/util.h"
#include "lib/utils/buffer_helper.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) {
busio_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in);
raise_error_if_deinited(common_hal_busio_i2c_deinited(self));
check_lock(self);
mp_obj_t list = mp_obj_new_list(0, NULL);
// 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
//|
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);
@ -156,7 +160,9 @@ MP_DEFINE_CONST_FUN_OBJ_1(busio_i2c_try_lock_obj, busio_i2c_obj_try_lock);
//| Releases the I2C lock.
//|
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;
}
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} },
};
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);
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);
@ -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} },
};
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);
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);

@ -49,6 +49,7 @@ extern void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
uint32_t frequency);
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_has_lock(busio_i2c_obj_t *self);

@ -32,6 +32,7 @@
#include "py/runtime0.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/busio/OneWire.h"
#include "shared-bindings/util.h"
//| .. 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) {
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));
}
@ -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) {
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));
}
@ -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) {
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));
return mp_const_none;

@ -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,
const mcu_pin_obj_t* pin);
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_read_bit(busio_onewire_obj_t* self);
extern void common_hal_busio_onewire_write_bit(busio_onewire_obj_t* self, bool bit);

@ -31,6 +31,7 @@
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/busio/SPI.h"
#include "shared-bindings/util.h"
#include "lib/utils/buffer_helper.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} },
};
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);
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);
@ -184,7 +186,9 @@ MP_DEFINE_CONST_FUN_OBJ_KW(busio_spi_configure_obj, 1, busio_spi_configure);
//| :rtype: bool
//|
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);
@ -193,7 +197,9 @@ MP_DEFINE_CONST_FUN_OBJ_1(busio_spi_try_lock_obj, busio_spi_obj_try_lock);
//| Releases the SPI lock.
//|
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;
}
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} },
};
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);
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);
@ -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} },
};
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);
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);

@ -41,6 +41,7 @@ extern void common_hal_busio_spi_construct(busio_spi_obj_t *self,
const mcu_pin_obj_t * miso);
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);

@ -27,6 +27,8 @@
#include <stdint.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"
@ -34,7 +36,6 @@
#include "py/runtime.h"
#include "py/stream.h"
#include "shared-bindings/microcontroller/Pin.h"
//| .. 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.
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;
// 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) {
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;
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) {
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;
if (request == MP_IOCTL_POLL) {
mp_uint_t flags = arg;