Split up nativeio.

This was done to allow greatly granularity when deciding what functionality
is built into each board's build. For example, this way pulseio can be
omitted to allow for something else such as touchio.
This commit is contained in:
Scott Shawcroft 2017-04-10 13:32:19 -07:00
parent 4810722064
commit f28f8ba568
120 changed files with 2690 additions and 1909 deletions

View File

@ -32,7 +32,7 @@ project admins. Please join the [Gitter chat](https://gitter.im/adafruit/circuit
* Port for Atmel SAMD21 (Commonly known as M0 in product names.)
* No `machine` API on Atmel SAMD21 port.
* Only supports Atmel SAMD21 and ESP8266 ports.
* Unified hardware API: [`nativeio`](https://circuitpython.readthedocs.io/en/latest/shared-bindings/nativeio/__init__.html), [`microcontroller`](https://circuitpython.readthedocs.io/en/latest/shared-bindings/microcontroller/__init__.html), [`board`](https://circuitpython.readthedocs.io/en/latest/shared-bindings/board/__init__.html), [`bitbangio`](https://circuitpython.readthedocs.io/en/latest/shared-bindings/bitbangio/__init__.html) (Only available on atmel-samd21 and ESP8266 currently.)
* Unified hardware APIs: [`analogio`](https://circuitpython.readthedocs.io/en/latest/shared-bindings/analogio/__init__.html), [`busio`](https://circuitpython.readthedocs.io/en/latest/shared-bindings/busio/__init__.html), [`digitalio`](https://circuitpython.readthedocs.io/en/latest/shared-bindings/digitalio/__init__.html), [`pulseio`](https://circuitpython.readthedocs.io/en/latest/shared-bindings/pulseio/__init__.html), [`touchio`](https://circuitpython.readthedocs.io/en/latest/shared-bindings/touchio/__init__.html), [`microcontroller`](https://circuitpython.readthedocs.io/en/latest/shared-bindings/microcontroller/__init__.html), [`board`](https://circuitpython.readthedocs.io/en/latest/shared-bindings/board/__init__.html), [`bitbangio`](https://circuitpython.readthedocs.io/en/latest/shared-bindings/bitbangio/__init__.html) (Only available on atmel-samd21 and ESP8266 currently.)
* Tracks MicroPython's releases (not master).
* No module aliasing. (`uos` and `utime` are not available as `os` and `time` respectively.)
* Modules with a CPython counterpart, such as `time`, are strict [subsets](https://circuitpython.readthedocs.io/en/latest/shared-bindings/time/__init__.html) of their [CPython version](https://docs.python.org/3.4/library/time.html?highlight=time#module-time). Therefore, code from CircuitPython is runnable on CPython but not necessarily the reverse.

View File

@ -221,41 +221,42 @@ SRC_BINDINGS = \
board/__init__.c \
microcontroller/__init__.c \
microcontroller/Pin.c \
nativeio/__init__.c \
nativeio/AnalogIn.c \
nativeio/AnalogOut.c \
nativeio/DigitalInOut.c \
nativeio/I2C.c \
nativeio/PulseIn.c \
nativeio/PulseOut.c \
nativeio/PWMOut.c \
nativeio/SPI.c \
nativeio/UART.c \
analogio/__init__.c \
analogio/AnalogIn.c \
analogio/AnalogOut.c \
digitalio/__init__.c \
digitalio/DigitalInOut.c \
pulseio/__init__.c \
pulseio/PulseIn.c \
pulseio/PulseOut.c \
pulseio/PWMOut.c \
busio/__init__.c \
busio/I2C.c \
busio/SPI.c \
busio/UART.c \
neopixel_write/__init__.c \
time/__init__.c \
usb_hid/__init__.c \
usb_hid/Device.c
SRC_BINDINGS_EXPANDED = $(addprefix shared-bindings/, $(SRC_BINDINGS)) \
$(addprefix common-hal/, $(SRC_BINDINGS))
# Handle touch support on its own since it only fits in the image when external
# flash is used to store the file system.
SRC_BINDINGS_EXPANDED += shared-bindings/nativeio/TouchIn.c
ifeq ($(FLASH_IMPL),internal_flash.c)
SRC_BINDINGS_EXPANDED += common-hal/nativeio/TouchInStub.c
else
SRC_BINDINGS_EXPANDED += common-hal/nativeio/TouchIn.c
# TODO(tannewt): Remove this after we switch to freetouch and rely on the linker
# to drop classes we don't need.
ifneq ($(FLASH_IMPL),internal_flash.c)
SRC_BINDINGS += touchio/__init__.c touchio/TouchIn.c
endif
SRC_BINDINGS_EXPANDED = $(addprefix shared-bindings/, $(SRC_BINDINGS)) \
$(addprefix common-hal/, $(SRC_BINDINGS))
SRC_SHARED_MODULE = \
help.c \
bitbangio/__init__.c \
bitbangio/I2C.c \
bitbangio/OneWire.c \
bitbangio/SPI.c \
nativeio/OneWire.c \
busio/OneWire.c \
uheap/__init__.c \
SRC_SHARED_MODULE_EXPANDED = $(addprefix shared-bindings/, $(SRC_SHARED_MODULE)) \

View File

@ -24,7 +24,7 @@
* THE SOFTWARE.
*/
#include "common-hal/nativeio/AnalogIn.h"
#include "common-hal/analogio/AnalogIn.h"
#include <string.h>
@ -33,7 +33,7 @@
#include "py/runtime.h"
#include "py/binary.h"
#include "py/mphal.h"
#include "shared-bindings/nativeio/AnalogIn.h"
#include "shared-bindings/analogio/AnalogIn.h"
#include "asf/sam0/drivers/adc/adc.h"
#include "samd21_pins.h"
@ -42,7 +42,7 @@
volatile uint8_t active_channel_count;
struct adc_module *adc_instance = NULL;
void common_hal_nativeio_analogin_construct(nativeio_analogin_obj_t* self,
void common_hal_analogio_analogin_construct(analogio_analogin_obj_t* self,
const mcu_pin_obj_t *pin) {
if (!pin->has_adc) {
// No ADC function on that pin
@ -72,7 +72,7 @@ void common_hal_nativeio_analogin_construct(nativeio_analogin_obj_t* self,
active_channel_count++;
}
void common_hal_nativeio_analogin_deinit(nativeio_analogin_obj_t *self) {
void common_hal_analogio_analogin_deinit(analogio_analogin_obj_t *self) {
active_channel_count--;
if (active_channel_count == 0) {
adc_reset(adc_instance);
@ -92,7 +92,7 @@ void analogin_reset() {
active_channel_count = 0;
}
uint16_t common_hal_nativeio_analogin_get_value(nativeio_analogin_obj_t *self) {
uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) {
adc_set_positive_input(adc_instance, self->pin->adc_input);
adc_enable(adc_instance);
@ -111,6 +111,6 @@ uint16_t common_hal_nativeio_analogin_get_value(nativeio_analogin_obj_t *self) {
return data;
}
float common_hal_nativeio_analogin_get_reference_voltage(nativeio_analogin_obj_t *self) {
float common_hal_analogio_analogin_get_reference_voltage(analogio_analogin_obj_t *self) {
return 3.3f;
}

View File

@ -0,0 +1,49 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2016 Scott Shawcroft
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ANALOGIO_ANALOGIN_H__
#define __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ANALOGIO_ANALOGIN_H__
#include "common-hal/microcontroller/types.h"
// Don't reorder these includes because they are dependencies of adc_feature.h.
// They should really be included by adc_feature.h.
#include <compiler.h>
#include "asf/sam0/drivers/system/clock/gclk.h"
#include "asf/sam0/utils/cmsis/samd21/include/component/adc.h"
#include "asf/sam0/drivers/adc/adc_sam_d_r/adc_feature.h"
#include "py/obj.h"
typedef struct {
mp_obj_base_t base;
const mcu_pin_obj_t * pin;
struct adc_module * adc_instance;
} analogio_analogin_obj_t;
void analogin_reset(void);
#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ANALOGIO_ANALOGIN_H__

View File

@ -30,12 +30,12 @@
#include "py/mperrno.h"
#include "py/runtime.h"
#include "shared-bindings/nativeio/AnalogOut.h"
#include "shared-bindings/analogio/AnalogOut.h"
#include "asf/sam0/drivers/dac/dac.h"
#include "samd21_pins.h"
void common_hal_nativeio_analogout_construct(nativeio_analogout_obj_t* self,
void common_hal_analogio_analogout_construct(analogio_analogout_obj_t* self,
const mcu_pin_obj_t *pin) {
if (pin->pin != PIN_PA02) {
mp_raise_ValueError("AnalogOut not supported on given pin");
@ -58,13 +58,13 @@ void common_hal_nativeio_analogout_construct(nativeio_analogout_obj_t* self,
dac_enable(&self->dac_instance);
}
void common_hal_nativeio_analogout_deinit(nativeio_analogout_obj_t *self) {
void common_hal_analogio_analogout_deinit(analogio_analogout_obj_t *self) {
dac_disable(&self->dac_instance);
dac_chan_disable(&self->dac_instance, DAC_CHANNEL_0);
reset_pin(PIN_PA02);
}
void common_hal_nativeio_analogout_set_value(nativeio_analogout_obj_t *self,
void common_hal_analogio_analogout_set_value(analogio_analogout_obj_t *self,
uint16_t value) {
// Input is 16 bit but we only support 10 bit so we shift the input.
dac_chan_write(&self->dac_instance, DAC_CHANNEL_0, value >> 6);

View File

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

View File

@ -0,0 +1 @@
// No analogio module functions.

View File

@ -24,10 +24,7 @@
* THE SOFTWARE.
*/
// This file contains all of the port specific HAL functions for the machine
// module.
#include "shared-bindings/nativeio/I2C.h"
#include "shared-bindings/busio/I2C.h"
#include "py/mperrno.h"
#include "py/nlr.h"
#include "py/runtime.h"
@ -41,7 +38,7 @@
// Number of times to try to send packet if failed.
#define TIMEOUT 1
void common_hal_nativeio_i2c_construct(nativeio_i2c_obj_t *self,
void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
const mcu_pin_obj_t* scl, const mcu_pin_obj_t* sda, uint32_t frequency) {
struct i2c_master_config config_i2c_master;
i2c_master_get_config_defaults(&config_i2c_master);
@ -85,7 +82,7 @@ void common_hal_nativeio_i2c_construct(nativeio_i2c_obj_t *self,
sercom, &config_i2c_master);
if (status != STATUS_OK) {
common_hal_nativeio_i2c_deinit(self);
common_hal_busio_i2c_deinit(self);
if (status == STATUS_ERR_BAUDRATE_UNAVAILABLE) {
mp_raise_ValueError("Unsupported baudrate");
} else {
@ -96,13 +93,13 @@ void common_hal_nativeio_i2c_construct(nativeio_i2c_obj_t *self,
i2c_master_enable(&self->i2c_master_instance);
}
void common_hal_nativeio_i2c_deinit(nativeio_i2c_obj_t *self) {
void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) {
i2c_master_reset(&self->i2c_master_instance);
reset_pin(self->sda_pin);
reset_pin(self->scl_pin);
}
bool common_hal_nativeio_i2c_probe(nativeio_i2c_obj_t *self, uint8_t addr) {
bool common_hal_busio_i2c_probe(busio_i2c_obj_t *self, uint8_t addr) {
uint8_t buf;
struct i2c_master_packet packet = {
.address = addr,
@ -118,26 +115,26 @@ bool common_hal_nativeio_i2c_probe(nativeio_i2c_obj_t *self, uint8_t addr) {
return status == STATUS_OK;
}
void common_hal_nativeio_i2c_configure(nativeio_i2c_obj_t *self,
void common_hal_busio_i2c_configure(busio_i2c_obj_t *self,
uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t bits) {
return;
}
bool common_hal_nativeio_i2c_try_lock(nativeio_i2c_obj_t *self) {
bool common_hal_busio_i2c_try_lock(busio_i2c_obj_t *self) {
self->has_lock = i2c_master_lock(&self->i2c_master_instance) == STATUS_OK;
return self->has_lock;
}
bool common_hal_nativeio_i2c_has_lock(nativeio_i2c_obj_t *self) {
bool common_hal_busio_i2c_has_lock(busio_i2c_obj_t *self) {
return self->has_lock;
}
void common_hal_nativeio_i2c_unlock(nativeio_i2c_obj_t *self) {
void common_hal_busio_i2c_unlock(busio_i2c_obj_t *self) {
self->has_lock = false;
i2c_master_unlock(&self->i2c_master_instance);
}
uint8_t common_hal_nativeio_i2c_write(nativeio_i2c_obj_t *self, uint16_t addr,
uint8_t common_hal_busio_i2c_write(busio_i2c_obj_t *self, uint16_t addr,
const uint8_t *data, size_t len, bool transmit_stop_bit) {
struct i2c_master_packet packet = {
.address = addr,
@ -171,7 +168,7 @@ uint8_t common_hal_nativeio_i2c_write(nativeio_i2c_obj_t *self, uint16_t addr,
return MP_EIO;
}
uint8_t common_hal_nativeio_i2c_read(nativeio_i2c_obj_t *self, uint16_t addr,
uint8_t common_hal_busio_i2c_read(busio_i2c_obj_t *self, uint16_t addr,
uint8_t *data, size_t len) {
struct i2c_master_packet packet = {
.address = addr,

View File

@ -0,0 +1,43 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2016 Scott Shawcroft
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_BUSIO_I2C_H__
#define __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_BUSIO_I2C_H__
#include "common-hal/microcontroller/types.h"
#include "asf/sam0/drivers/sercom/i2c/i2c_master.h"
#include "py/obj.h"
typedef struct {
mp_obj_base_t base;
struct i2c_master_module i2c_master_instance;
bool has_lock;
uint8_t scl_pin;
uint8_t sda_pin;
} busio_i2c_obj_t;
#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_BUSIO_I2C_H__

View File

@ -3,7 +3,7 @@
*
* The MIT License (MIT)
*
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
* Copyright (c) 2016 Scott Shawcroft
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@ -24,9 +24,10 @@
* THE SOFTWARE.
*/
#ifndef __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_PULSEIN_H__
#define __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_PULSEIN_H__
#ifndef __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_BUSIO_ONEWIRE_H__
#define __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_BUSIO_ONEWIRE_H__
void pulsein_reset(void);
// Use bitbangio.
#include "shared-module/busio/OneWire.h"
#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_PULSEIN_H__
#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_BUSIO_ONEWIRE_H__

View File

@ -24,10 +24,7 @@
* THE SOFTWARE.
*/
// This file contains all of the port specific HAL functions for the machine
// module.
#include "shared-bindings/nativeio/SPI.h"
#include "shared-bindings/busio/SPI.h"
#include "py/nlr.h"
#include "py/runtime.h"
#include "samd21_pins.h"
@ -38,7 +35,7 @@
// Number of times to try to send packet if failed.
#define TIMEOUT 1
void common_hal_nativeio_spi_construct(nativeio_spi_obj_t *self,
void common_hal_busio_spi_construct(busio_spi_obj_t *self,
const mcu_pin_obj_t * clock, const mcu_pin_obj_t * mosi,
const mcu_pin_obj_t * miso) {
struct spi_config config_spi_master;
@ -146,14 +143,14 @@ void common_hal_nativeio_spi_construct(nativeio_spi_obj_t *self,
spi_enable(&self->spi_master_instance);
}
void common_hal_nativeio_spi_deinit(nativeio_spi_obj_t *self) {
void common_hal_busio_spi_deinit(busio_spi_obj_t *self) {
spi_disable(&self->spi_master_instance);
reset_pin(self->clock_pin);
reset_pin(self->MOSI_pin);
reset_pin(self->MISO_pin);
}
bool common_hal_nativeio_spi_configure(nativeio_spi_obj_t *self,
bool common_hal_busio_spi_configure(busio_spi_obj_t *self,
uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t bits) {
// TODO(tannewt): Check baudrate first before changing it.
if (baudrate != self->current_baudrate) {
@ -194,21 +191,21 @@ bool common_hal_nativeio_spi_configure(nativeio_spi_obj_t *self,
return true;
}
bool common_hal_nativeio_spi_try_lock(nativeio_spi_obj_t *self) {
bool common_hal_busio_spi_try_lock(busio_spi_obj_t *self) {
self->has_lock = spi_lock(&self->spi_master_instance) == STATUS_OK;
return self->has_lock;
}
bool common_hal_nativeio_spi_has_lock(nativeio_spi_obj_t *self) {
bool common_hal_busio_spi_has_lock(busio_spi_obj_t *self) {
return self->has_lock;
}
void common_hal_nativeio_spi_unlock(nativeio_spi_obj_t *self) {
void common_hal_busio_spi_unlock(busio_spi_obj_t *self) {
self->has_lock = false;
spi_unlock(&self->spi_master_instance);
}
bool common_hal_nativeio_spi_write(nativeio_spi_obj_t *self,
bool common_hal_busio_spi_write(busio_spi_obj_t *self,
const uint8_t *data, size_t len) {
if (len == 0) {
return true;
@ -220,7 +217,7 @@ bool common_hal_nativeio_spi_write(nativeio_spi_obj_t *self,
return status == STATUS_OK;
}
bool common_hal_nativeio_spi_read(nativeio_spi_obj_t *self,
bool common_hal_busio_spi_read(busio_spi_obj_t *self,
uint8_t *data, size_t len, uint8_t write_value) {
if (len == 0) {
return true;

View File

@ -0,0 +1,45 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2016 Scott Shawcroft
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_BUSIO_SPI_H__
#define __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_BUSIO_SPI_H__
#include "common-hal/microcontroller/types.h"
#include "asf/sam0/drivers/sercom/spi/spi.h"
#include "py/obj.h"
typedef struct {
mp_obj_base_t base;
struct spi_module spi_master_instance;
bool has_lock;
uint8_t clock_pin;
uint8_t MOSI_pin;
uint8_t MISO_pin;
uint32_t current_baudrate;
} busio_spi_obj_t;
#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_BUSIO_SPI_H__

View File

@ -25,7 +25,7 @@
*/
#include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/nativeio/UART.h"
#include "shared-bindings/busio/UART.h"
#include "mpconfigport.h"
#include "py/gc.h"
@ -40,7 +40,7 @@
#undef ENABLE
nativeio_uart_obj_t *_uart_instances[SERCOM_INST_NUM];
busio_uart_obj_t *_uart_instances[SERCOM_INST_NUM];
static void _sercom_default_handler(
const uint8_t instance)
@ -48,7 +48,7 @@ static void _sercom_default_handler(
Assert(false);
}
static void _nativeio_uart_interrupt_handler(uint8_t instance)
static void _busio_uart_interrupt_handler(uint8_t instance)
{
/* Temporary variables */
uint16_t interrupt_status;
@ -58,7 +58,7 @@ static void _nativeio_uart_interrupt_handler(uint8_t instance)
struct usart_module *module
= (struct usart_module *)_sercom_instances[instance];
nativeio_uart_obj_t *self = _uart_instances[instance];
busio_uart_obj_t *self = _uart_instances[instance];
/* Pointer to the hardware module instance */
SercomUsart *const usart_hw = &(module->hw->USART);
@ -122,7 +122,7 @@ static void _nativeio_uart_interrupt_handler(uint8_t instance)
}
}
void common_hal_nativeio_uart_construct(nativeio_uart_obj_t *self,
void common_hal_busio_uart_construct(busio_uart_obj_t *self,
const mcu_pin_obj_t * tx, const mcu_pin_obj_t * rx, uint32_t baudrate,
uint8_t bits, uart_parity_t parity, uint8_t stop, uint32_t timeout,
uint8_t receiver_buffer_size) {
@ -227,7 +227,7 @@ void common_hal_nativeio_uart_construct(nativeio_uart_obj_t *self,
// We use our own interrupt handler because we want a circular buffer
// instead of the jobs that ASF provides.
uint8_t instance_index = _sercom_get_sercom_inst_index(self->uart_instance.hw);
_sercom_set_handler(instance_index, _nativeio_uart_interrupt_handler);
_sercom_set_handler(instance_index, _busio_uart_interrupt_handler);
_sercom_instances[instance_index] = &self->uart_instance;
_uart_instances[instance_index] = self;
@ -238,7 +238,7 @@ void common_hal_nativeio_uart_construct(nativeio_uart_obj_t *self,
self->uart_instance.hw->USART.INTENSET.bit.RXC = true;
}
void common_hal_nativeio_uart_deinit(nativeio_uart_obj_t *self) {
void common_hal_busio_uart_deinit(busio_uart_obj_t *self) {
self->uart_instance.hw->USART.INTENCLR.bit.RXC = true;
uint8_t instance_index = _sercom_get_sercom_inst_index(self->uart_instance.hw);
@ -254,7 +254,7 @@ void common_hal_nativeio_uart_deinit(nativeio_uart_obj_t *self) {
}
// Read characters.
size_t common_hal_nativeio_uart_read(nativeio_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) {
size_t total_read = 0;
uint64_t start_ticks = ticks_ms;
while (total_read < len && ticks_ms - start_ticks < self->timeout_ms) {
@ -287,7 +287,7 @@ size_t common_hal_nativeio_uart_read(nativeio_uart_obj_t *self, uint8_t *data, s
}
// Write characters.
size_t common_hal_nativeio_uart_write(nativeio_uart_obj_t *self, const uint8_t *data, size_t len, int *errcode) {
size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data, size_t len, int *errcode) {
/* Check that the transmitter is enabled */
if (!(self->uart_instance.transmitter_enabled)) {
*errcode = MP_EIO;
@ -362,14 +362,14 @@ size_t common_hal_nativeio_uart_write(nativeio_uart_obj_t *self, const uint8_t *
return tx_pos;
}
uint32_t common_hal_nativeio_uart_rx_characters_available(nativeio_uart_obj_t *self) {
uint32_t common_hal_busio_uart_rx_characters_available(busio_uart_obj_t *self) {
if (self->uart_instance.character_size == USART_CHARACTER_SIZE_9BIT) {
return self->buffer_size / 2;
}
return self->buffer_size;
}
bool common_hal_nativeio_uart_ready_to_tx(nativeio_uart_obj_t *self) {
bool common_hal_busio_uart_ready_to_tx(busio_uart_obj_t *self) {
if (!(self->uart_instance.transmitter_enabled)) {
return false;
}

View File

@ -0,0 +1,50 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2016 Scott Shawcroft
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_BUSIO_UART_H__
#define __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_BUSIO_UART_H__
#include "common-hal/microcontroller/types.h"
#include "asf/sam0/drivers/sercom/usart/usart.h"
#include "py/obj.h"
typedef struct {
mp_obj_base_t base;
struct usart_module uart_instance;
uint8_t rx_pin;
uint8_t tx_pin;
uint32_t timeout_ms;
bool rx_error;
// Index of the oldest received character.
uint32_t buffer_start;
// Index of the next available spot to store a character.
uint32_t buffer_size;
uint32_t buffer_length;
uint8_t* buffer;
} busio_uart_obj_t;
#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_BUSIO_UART_H__

View File

@ -0,0 +1 @@
// No busio module functions.

View File

@ -31,13 +31,13 @@
#include "py/runtime.h"
#include "py/mphal.h"
#include "shared-bindings/nativeio/DigitalInOut.h"
#include "shared-bindings/digitalio/DigitalInOut.h"
#include "asf/sam0/drivers/port/port.h"
#include "asf/sam0/drivers/system/pinmux/pinmux.h"
digitalinout_result_t common_hal_nativeio_digitalinout_construct(
nativeio_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) {
self->pin = pin;
struct port_config pin_conf;
@ -49,7 +49,7 @@ digitalinout_result_t common_hal_nativeio_digitalinout_construct(
return DIGITALINOUT_OK;
}
void common_hal_nativeio_digitalinout_deinit(nativeio_digitalinout_obj_t* self) {
void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t* self) {
struct port_config pin_conf;
port_get_config_defaults(&pin_conf);
@ -57,15 +57,15 @@ void common_hal_nativeio_digitalinout_deinit(nativeio_digitalinout_obj_t* self)
port_pin_set_config(self->pin->pin, &pin_conf);
}
void common_hal_nativeio_digitalinout_switch_to_input(
nativeio_digitalinout_obj_t* self, enum digitalinout_pull_t pull) {
void common_hal_digitalio_digitalinout_switch_to_input(
digitalio_digitalinout_obj_t* self, enum digitalinout_pull_t pull) {
self->output = false;
common_hal_nativeio_digitalinout_set_pull(self, pull);
common_hal_digitalio_digitalinout_set_pull(self, pull);
}
void common_hal_nativeio_digitalinout_switch_to_output(
nativeio_digitalinout_obj_t* self, bool value,
void common_hal_digitalio_digitalinout_switch_to_output(
digitalio_digitalinout_obj_t* self, bool value,
enum digitalinout_drive_mode_t drive_mode) {
struct port_config pin_conf;
port_get_config_defaults(&pin_conf);
@ -76,16 +76,16 @@ void common_hal_nativeio_digitalinout_switch_to_output(
self->output = true;
self->open_drain = drive_mode == DRIVE_MODE_OPEN_DRAIN;
common_hal_nativeio_digitalinout_set_value(self, value);
common_hal_digitalio_digitalinout_set_value(self, value);
}
enum digitalinout_direction_t common_hal_nativeio_digitalinout_get_direction(
nativeio_digitalinout_obj_t* self) {
enum digitalinout_direction_t common_hal_digitalio_digitalinout_get_direction(
digitalio_digitalinout_obj_t* self) {
return self->output? DIRECTION_OUT : DIRECTION_IN;
}
void common_hal_nativeio_digitalinout_set_value(
nativeio_digitalinout_obj_t* self, bool value) {
void common_hal_digitalio_digitalinout_set_value(
digitalio_digitalinout_obj_t* self, bool value) {
uint32_t pin = self->pin->pin;
PortGroup *const port_base = port_get_group_from_gpio_pin(pin);
uint32_t pin_mask = (1UL << (pin % 32));
@ -104,8 +104,8 @@ void common_hal_nativeio_digitalinout_set_value(
}
}
bool common_hal_nativeio_digitalinout_get_value(
nativeio_digitalinout_obj_t* self) {
bool common_hal_digitalio_digitalinout_get_value(
digitalio_digitalinout_obj_t* self) {
uint32_t pin = self->pin->pin;
PortGroup *const port_base = port_get_group_from_gpio_pin(pin);
uint32_t pin_mask = (1UL << (pin % 32));
@ -120,20 +120,20 @@ bool common_hal_nativeio_digitalinout_get_value(
}
}
void common_hal_nativeio_digitalinout_set_drive_mode(
nativeio_digitalinout_obj_t* self,
void common_hal_digitalio_digitalinout_set_drive_mode(
digitalio_digitalinout_obj_t* self,
enum digitalinout_drive_mode_t drive_mode) {
bool value = common_hal_nativeio_digitalinout_get_value(self);
bool value = common_hal_digitalio_digitalinout_get_value(self);
self->open_drain = drive_mode == DRIVE_MODE_OPEN_DRAIN;
// True is implemented differently between modes so reset the value to make
// sure its correct for the new mode.
if (value) {
common_hal_nativeio_digitalinout_set_value(self, value);
common_hal_digitalio_digitalinout_set_value(self, value);
}
}
enum digitalinout_drive_mode_t common_hal_nativeio_digitalinout_get_drive_mode(
nativeio_digitalinout_obj_t* self) {
enum digitalinout_drive_mode_t common_hal_digitalio_digitalinout_get_drive_mode(
digitalio_digitalinout_obj_t* self) {
if (self->open_drain) {
return DRIVE_MODE_OPEN_DRAIN;
} else {
@ -141,8 +141,8 @@ enum digitalinout_drive_mode_t common_hal_nativeio_digitalinout_get_drive_mode(
}
}
void common_hal_nativeio_digitalinout_set_pull(
nativeio_digitalinout_obj_t* self, enum digitalinout_pull_t pull) {
void common_hal_digitalio_digitalinout_set_pull(
digitalio_digitalinout_obj_t* self, enum digitalinout_pull_t pull) {
enum port_pin_pull asf_pull = PORT_PIN_PULL_NONE;
switch (pull) {
case PULL_UP:
@ -163,8 +163,8 @@ void common_hal_nativeio_digitalinout_set_pull(
port_pin_set_config(self->pin->pin, &pin_conf);
}
enum digitalinout_pull_t common_hal_nativeio_digitalinout_get_pull(
nativeio_digitalinout_obj_t* self) {
enum digitalinout_pull_t common_hal_digitalio_digitalinout_get_pull(
digitalio_digitalinout_obj_t* self) {
uint32_t pin = self->pin->pin;
PortGroup *const port_base = port_get_group_from_gpio_pin(pin);
uint32_t pin_mask = (1UL << (pin % 32));

View File

@ -24,9 +24,10 @@
* THE SOFTWARE.
*/
#ifndef __MICROPY_INCLUDED_ESP8266_COMMON_HAL_NATIVEIO_DIGITALINOUT_H__
#define __MICROPY_INCLUDED_ESP8266_COMMON_HAL_NATIVEIO_DIGITALINOUT_H__
#ifndef __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_DIGITALIO_DIGITALINOUT_H__
#define __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_DIGITALIO_DIGITALINOUT_H__
#include "common-hal/microcontroller/types.h"
#include "py/obj.h"
typedef struct {
@ -34,6 +35,6 @@ typedef struct {
const mcu_pin_obj_t * pin;
bool output;
bool open_drain;
} nativeio_digitalinout_obj_t;
} digitalio_digitalinout_obj_t;
#endif // __MICROPY_INCLUDED_ESP8266_COMMON_HAL_NATIVEIO_DIGITALINOUT_H__
#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_DIGITALIO_DIGITALINOUT_H__

View File

@ -0,0 +1 @@
// No digitalio module functions.

View File

@ -1,32 +0,0 @@
/*
* 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_COMMON_HAL_NATIVEIO_PULSEOUT_H__
#define __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_PULSEOUT_H__
void pulseout_reset(void);
#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_PULSEOUT_H__

View File

@ -1,45 +0,0 @@
/*
* This file is part of the Micro Python 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.
*/
#include <string.h>
#include "py/nlr.h"
#include "py/runtime.h"
#include "py/binary.h"
#include "py/mphal.h"
#include "shared-bindings/nativeio/TouchIn.h"
void common_hal_nativeio_touchin_construct(nativeio_touchin_obj_t* self,
const mcu_pin_obj_t *pin) {
mp_raise_NotImplementedError("No capacitive touch support");
}
void common_hal_nativeio_touchin_deinit(nativeio_touchin_obj_t* self) {
}
bool common_hal_nativeio_touchin_get_value(nativeio_touchin_obj_t *self) {
return false;
}

View File

@ -1 +0,0 @@
// No nativeio module functions.

View File

@ -1,142 +0,0 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2016 Scott Shawcroft
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
// This defines the types used to underly the standard nativeio Python objects.
// The shared API is defined in terms of these types.
#ifndef __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_TYPES_H__
#define __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_TYPES_H__
#include "common-hal/microcontroller/types.h"
// Don't reorder these includes because they are dependencies of adc_feature.h.
// They should really be included by adc_feature.h.
#include <compiler.h>
#include "asf/sam0/drivers/system/clock/gclk.h"
#include "asf/sam0/utils/cmsis/samd21/include/component/adc.h"
#include "asf/sam0/drivers/adc/adc_sam_d_r/adc_feature.h"
#include "asf/sam0/drivers/dac/dac.h"
#include "asf/sam0/drivers/sercom/i2c/i2c_master.h"
#include "asf/sam0/drivers/sercom/spi/spi.h"
#include "asf/sam0/drivers/sercom/usart/usart.h"
#include "asf/sam0/drivers/tc/tc.h"
#include "asf/sam0/drivers/tcc/tcc.h"
// Only support TouchIn when external SPI flash is used.
#ifdef SPI_FLASH_SECTOR_SIZE
#include "QTouch/touch_api_ptc.h"
#endif
#include "py/obj.h"
// Import bitbang implemented types. Must be after DigitalInOut because they
// likely depend on it.
#include "shared-module/nativeio/OneWire.h"
typedef struct {
mp_obj_base_t base;
const mcu_pin_obj_t * pin;
struct adc_module * adc_instance;
} nativeio_analogin_obj_t;
typedef struct {
mp_obj_base_t base;
struct dac_module dac_instance;
} nativeio_analogout_obj_t;
typedef struct {
mp_obj_base_t base;
struct i2c_master_module i2c_master_instance;
bool has_lock;
uint8_t scl_pin;
uint8_t sda_pin;
} nativeio_i2c_obj_t;
typedef struct {
mp_obj_base_t base;
struct spi_module spi_master_instance;
bool has_lock;
uint8_t clock_pin;
uint8_t MOSI_pin;
uint8_t MISO_pin;
uint32_t current_baudrate;
} nativeio_spi_obj_t;
typedef struct {
mp_obj_base_t base;
uint8_t channel;
uint8_t pin;
uint16_t* buffer;
uint16_t maxlen;
bool idle_state;
volatile uint16_t start;
volatile uint16_t len;
volatile bool first_edge;
uint16_t ticks_per_ms;
} nativeio_pulsein_obj_t;
typedef struct {
mp_obj_base_t base;
__IO PORT_PINCFG_Type *pincfg;
uint8_t pin;
} nativeio_pulseout_obj_t;
typedef struct {
mp_obj_base_t base;
const mcu_pin_obj_t *pin;
const pin_timer_t* timer;
bool variable_frequency;
union {
struct tc_module tc_instance;
struct tcc_module tcc_instance;
};
} nativeio_pwmout_obj_t;
typedef struct {
mp_obj_base_t base;
// Only support TouchIn when external SPI flash is used.
#ifdef SPI_FLASH_SECTOR_SIZE
const mcu_pin_obj_t * pin;
sensor_id_t sensor_id;
#endif
} nativeio_touchin_obj_t;
typedef struct {
mp_obj_base_t base;
struct usart_module uart_instance;
uint8_t rx_pin;
uint8_t tx_pin;
uint32_t timeout_ms;
bool rx_error;
// Index of the oldest received character.
uint32_t buffer_start;
// Index of the next available spot to store a character.
uint32_t buffer_size;
uint32_t buffer_length;
uint8_t* buffer;
} nativeio_uart_obj_t;
#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_TYPES_H__

View File

@ -29,8 +29,9 @@
#include "shared-bindings/neopixel_write/__init__.h"
#include "asf/common2/services/delay/delay.h"
#include "asf/sam0/drivers/port/port.h"
void common_hal_neopixel_write(const nativeio_digitalinout_obj_t* digitalinout, uint8_t *pixels, uint32_t numBytes) {
void common_hal_neopixel_write(const digitalio_digitalinout_obj_t* digitalinout, uint8_t *pixels, uint32_t numBytes) {
// This is adapted directly from the Adafruit NeoPixel library SAMD21G18A code:
// https://github.com/adafruit/Adafruit_NeoPixel/blob/master/Adafruit_NeoPixel.cpp
uint8_t *ptr, *end, p, bitMask;

View File

@ -27,8 +27,8 @@
#include <stdint.h>
#include "py/runtime.h"
#include "common-hal/nativeio/PWMOut.h"
#include "shared-bindings/nativeio/PWMOut.h"
#include "common-hal/pulseio/PWMOut.h"
#include "shared-bindings/pulseio/PWMOut.h"
#include "samd21_pins.h"
@ -70,7 +70,7 @@ bool channel_ok(const pin_timer_t* t, uint8_t index) {
t->is_tc;
}
void common_hal_nativeio_pwmout_construct(nativeio_pwmout_obj_t* self,
void common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self,
const mcu_pin_obj_t* pin,
uint16_t duty,
uint32_t frequency,
@ -198,10 +198,10 @@ void common_hal_nativeio_pwmout_construct(nativeio_pwmout_obj_t* self,
pin_config.direction = SYSTEM_PINMUX_PIN_DIR_OUTPUT;
system_pinmux_pin_set_config(pin->pin, &pin_config);
common_hal_nativeio_pwmout_set_duty_cycle(self, duty);
common_hal_pulseio_pwmout_set_duty_cycle(self, duty);
}
extern void common_hal_nativeio_pwmout_deinit(nativeio_pwmout_obj_t* self) {
extern void common_hal_pulseio_pwmout_deinit(pulseio_pwmout_obj_t* self) {
const pin_timer_t* t = self->timer;
uint8_t index = (((uint32_t) t->tcc) - ((uint32_t) TCC0)) / 0x400;
timer_refcount[index]--;
@ -225,7 +225,7 @@ extern void common_hal_nativeio_pwmout_deinit(nativeio_pwmout_obj_t* self) {
reset_pin(self->pin->pin);
}
extern void common_hal_nativeio_pwmout_set_duty_cycle(nativeio_pwmout_obj_t* self, uint16_t duty) {
extern void common_hal_pulseio_pwmout_set_duty_cycle(pulseio_pwmout_obj_t* self, uint16_t duty) {
const pin_timer_t* t = self->timer;
if (t->is_tc) {
uint32_t top = ((uint32_t) t->tc->COUNT16.CC[0].reg + 1);
@ -238,7 +238,7 @@ extern void common_hal_nativeio_pwmout_set_duty_cycle(nativeio_pwmout_obj_t* sel
}
}
uint16_t common_hal_nativeio_pwmout_get_duty_cycle(nativeio_pwmout_obj_t* self) {
uint16_t common_hal_pulseio_pwmout_get_duty_cycle(pulseio_pwmout_obj_t* self) {
const pin_timer_t* t = self->timer;
if (t->is_tc) {
uint16_t top = t->tc->COUNT16.CC[0].reg;
@ -260,7 +260,7 @@ uint16_t common_hal_nativeio_pwmout_get_duty_cycle(nativeio_pwmout_obj_t* self)
}
void common_hal_nativeio_pwmout_set_frequency(nativeio_pwmout_obj_t* self,
void common_hal_pulseio_pwmout_set_frequency(pulseio_pwmout_obj_t* self,
uint32_t frequency) {
if (frequency == 0 || frequency > 6000000) {
mp_raise_ValueError("Invalid PWM frequency");
@ -281,7 +281,7 @@ void common_hal_nativeio_pwmout_set_frequency(nativeio_pwmout_obj_t* self,
break;
}
}
uint16_t old_duty = common_hal_nativeio_pwmout_get_duty_cycle(self);
uint16_t old_duty = common_hal_pulseio_pwmout_get_duty_cycle(self);
uint8_t old_divisor;
if (t->is_tc) {
old_divisor = t->tc->COUNT16.CTRLA.bit.PRESCALER;
@ -308,10 +308,10 @@ void common_hal_nativeio_pwmout_set_frequency(nativeio_pwmout_obj_t* self,
tcc_set_top_value(&self->tcc_instance, new_top);
}
common_hal_nativeio_pwmout_set_duty_cycle(self, old_duty);
common_hal_pulseio_pwmout_set_duty_cycle(self, old_duty);
}
uint32_t common_hal_nativeio_pwmout_get_frequency(nativeio_pwmout_obj_t* self) {
uint32_t common_hal_pulseio_pwmout_get_frequency(pulseio_pwmout_obj_t* self) {
uint32_t system_clock = system_cpu_clock_get_hz();
const pin_timer_t* t = self->timer;
uint32_t top;
@ -326,6 +326,6 @@ uint32_t common_hal_nativeio_pwmout_get_frequency(nativeio_pwmout_obj_t* self) {
return (system_clock / prescaler[divisor]) / (top + 1);
}
bool common_hal_nativeio_pwmout_get_variable_frequency(nativeio_pwmout_obj_t* self) {
bool common_hal_pulseio_pwmout_get_variable_frequency(pulseio_pwmout_obj_t* self) {
return self->variable_frequency;
}

View File

@ -0,0 +1,49 @@
/*
* 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_COMMON_HAL_PULSEIO_PWMOUT_H__
#define __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PULSEIO_PWMOUT_H__
#include "common-hal/microcontroller/types.h"
#include "asf/sam0/drivers/tc/tc.h"
#include "asf/sam0/drivers/tcc/tcc.h"
#include "py/obj.h"
typedef struct {
mp_obj_base_t base;
const mcu_pin_obj_t *pin;
const pin_timer_t* timer;
bool variable_frequency;
union {
struct tc_module tc_instance;
struct tcc_module tcc_instance;
};
} pulseio_pwmout_obj_t;
void pwmout_reset(void);
#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PULSEIO_PWMOUT_H__

View File

@ -24,24 +24,25 @@
* THE SOFTWARE.
*/
#include "common-hal/nativeio/PulseIn.h"
#include "common-hal/pulseio/PulseIn.h"
#include <stdint.h>
#include "asf/common2/services/delay/delay.h"
#include "asf/sam0/drivers/extint/extint.h"
#include "asf/sam0/drivers/extint/extint_callback.h"
#include "asf/sam0/drivers/port/port.h"
#include "mpconfigport.h"
#include "py/gc.h"
#include "py/runtime.h"
#include "samd21_pins.h"
#include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/nativeio/PulseIn.h"
#include "shared-bindings/pulseio/PulseIn.h"
#include "tick.h"
static nativeio_pulsein_obj_t *active_pulseins[EIC_NUMBER_OF_INTERRUPTS];
static pulseio_pulsein_obj_t *active_pulseins[EIC_NUMBER_OF_INTERRUPTS];
static uint64_t last_ms[EIC_NUMBER_OF_INTERRUPTS];
static uint16_t last_us[EIC_NUMBER_OF_INTERRUPTS];
@ -53,7 +54,7 @@ void pulsein_reset(void) {
}
}
static void pulsein_set_config(nativeio_pulsein_obj_t* self, bool first_edge) {
static void pulsein_set_config(pulseio_pulsein_obj_t* self, bool first_edge) {
struct extint_chan_conf config;
extint_chan_get_config_defaults(&config);
config.gpio_pin = self->pin;
@ -78,7 +79,7 @@ static void pulsein_callback(void) {
// Grab the current time first.
uint16_t current_us = tc_get_count_value(&ms_timer);
uint64_t current_ms = ticks_ms;
nativeio_pulsein_obj_t* self = active_pulseins[extint_get_current_channel()];
pulseio_pulsein_obj_t* self = active_pulseins[extint_get_current_channel()];
current_us = current_us * 1000 / self->ticks_per_ms;
if (self->first_edge) {
self->first_edge = false;
@ -110,7 +111,7 @@ static void pulsein_callback(void) {
last_us[self->channel] = current_us;
}
void common_hal_nativeio_pulsein_construct(nativeio_pulsein_obj_t* self,
void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self,
const mcu_pin_obj_t* pin, uint16_t maxlen, bool idle_state) {
if (!pin->has_extint) {
mp_raise_RuntimeError("No hardware support on pin");
@ -144,17 +145,17 @@ void common_hal_nativeio_pulsein_construct(nativeio_pulsein_obj_t* self,
extint_chan_enable_callback(self->channel, EXTINT_CALLBACK_TYPE_DETECT);
}
void common_hal_nativeio_pulsein_deinit(nativeio_pulsein_obj_t* self) {
void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t* self) {
extint_chan_disable_callback(self->channel, EXTINT_CALLBACK_TYPE_DETECT);
active_pulseins[self->channel] = NULL;
reset_pin(self->pin);
}
void common_hal_nativeio_pulsein_pause(nativeio_pulsein_obj_t* self) {
void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t* self) {
extint_chan_disable_callback(self->channel, EXTINT_CALLBACK_TYPE_DETECT);
}
void common_hal_nativeio_pulsein_resume(nativeio_pulsein_obj_t* self,
void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self,
uint16_t trigger_duration) {
// Send the trigger pulse.
if (trigger_duration > 0) {
@ -176,14 +177,14 @@ void common_hal_nativeio_pulsein_resume(nativeio_pulsein_obj_t* self,
pulsein_set_config(self, true);
}
void common_hal_nativeio_pulsein_clear(nativeio_pulsein_obj_t* self) {
void common_hal_pulseio_pulsein_clear(pulseio_pulsein_obj_t* self) {
common_hal_mcu_disable_interrupts();
self->start = 0;
self->len = 0;
common_hal_mcu_enable_interrupts();
}
uint16_t common_hal_nativeio_pulsein_popleft(nativeio_pulsein_obj_t* self) {
uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t* self) {
if (self->len == 0) {
mp_raise_IndexError("pop from an empty PulseIn");
}
@ -196,15 +197,15 @@ uint16_t common_hal_nativeio_pulsein_popleft(nativeio_pulsein_obj_t* self) {
return value;
}
uint16_t common_hal_nativeio_pulsein_get_maxlen(nativeio_pulsein_obj_t* self) {
uint16_t common_hal_pulseio_pulsein_get_maxlen(pulseio_pulsein_obj_t* self) {
return self->maxlen;
}
uint16_t common_hal_nativeio_pulsein_get_len(nativeio_pulsein_obj_t* self) {
uint16_t common_hal_pulseio_pulsein_get_len(pulseio_pulsein_obj_t* self) {
return self->len;
}
uint16_t common_hal_nativeio_pulsein_get_item(nativeio_pulsein_obj_t* self,
uint16_t common_hal_pulseio_pulsein_get_item(pulseio_pulsein_obj_t* self,
int16_t index) {
common_hal_mcu_disable_interrupts();
if (index < 0) {

View File

@ -0,0 +1,49 @@
/*
* 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_COMMON_HAL_PULSEIO_PULSEIN_H__
#define __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PULSEIO_PULSEIN_H__
#include "common-hal/microcontroller/types.h"
#include "py/obj.h"
typedef struct {
mp_obj_base_t base;
uint8_t channel;
uint8_t pin;
uint16_t* buffer;
uint16_t maxlen;
bool idle_state;
volatile uint16_t start;
volatile uint16_t len;
volatile bool first_edge;
uint16_t ticks_per_ms;
} pulseio_pulsein_obj_t;
void pulsein_reset(void);
#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PULSEIO_PULSEIN_H__

View File

@ -24,15 +24,16 @@
* THE SOFTWARE.
*/
#include "common-hal/nativeio/PulseOut.h"
#include "common-hal/pulseio/PulseOut.h"
#include <stdint.h>
#include "asf/sam0/drivers/tc/tc_interrupt.h"
#include "asf/sam0/drivers/port/port.h"
#include "mpconfigport.h"
#include "py/runtime.h"
#include "shared-bindings/nativeio/PulseOut.h"
#include "shared-bindings/pulseio/PulseOut.h"
#undef ENABLE
@ -78,8 +79,8 @@ void pulseout_reset() {
active_pincfg = NULL;
}
void common_hal_nativeio_pulseout_construct(nativeio_pulseout_obj_t* self,
const nativeio_pwmout_obj_t* carrier) {
void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self,
const pulseio_pwmout_obj_t* carrier) {
if (refcount == 0) {
// Find a spare timer.
Tc *t = NULL;
@ -121,7 +122,7 @@ void common_hal_nativeio_pulseout_construct(nativeio_pulseout_obj_t* self,
turn_off(self->pincfg);
}
void common_hal_nativeio_pulseout_deinit(nativeio_pulseout_obj_t* self) {
void common_hal_pulseio_pulseout_deinit(pulseio_pulseout_obj_t* self) {
PortGroup *const port_base = port_get_group_from_gpio_pin(self->pin);
port_base->DIRCLR.reg = 1 << (self->pin % 32);
@ -133,7 +134,7 @@ void common_hal_nativeio_pulseout_deinit(nativeio_pulseout_obj_t* self) {
}
}
void common_hal_nativeio_pulseout_send(nativeio_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) {
if (active_pincfg != NULL) {
mp_raise_RuntimeError("Another send is already active");
}

View File

@ -0,0 +1,44 @@
/*
* 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_COMMON_HAL_PULSEIO_PULSEOUT_H__
#define __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PULSEIO_PULSEOUT_H__
#include "common-hal/microcontroller/types.h"
#include "asf/sam0/drivers/tc/tc.h"
#include "asf/sam0/drivers/tcc/tcc.h"
#include "py/obj.h"
typedef struct {
mp_obj_base_t base;
__IO PORT_PINCFG_Type *pincfg;
uint8_t pin;
} pulseio_pulseout_obj_t;
void pulseout_reset(void);
#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_PULSEIO_PULSEOUT_H__

View File

@ -0,0 +1 @@
// No pulseio module functions.

View File

@ -31,7 +31,7 @@
#include "py/runtime.h"
#include "py/binary.h"
#include "py/mphal.h"
#include "shared-bindings/nativeio/TouchIn.h"
#include "shared-bindings/touchio/TouchIn.h"
#include "tick.h"
@ -182,9 +182,9 @@ touch_config_t touch_config = {
DEF_TOUCH_PTC_ISR_LVL, /* PTC interrupt level. */
};
nativeio_touchin_obj_t *active_touchin_obj[DEF_SELFCAP_NUM_CHANNELS];
touchio_touchin_obj_t *active_touchin_obj[DEF_SELFCAP_NUM_CHANNELS];
void common_hal_nativeio_touchin_construct(nativeio_touchin_obj_t* self,
void common_hal_touchio_touchin_construct(touchio_touchin_obj_t* self,
const mcu_pin_obj_t *pin) {
if (!pin->has_touch) {
mp_raise_ValueError("Invalid pin");
@ -245,13 +245,13 @@ void common_hal_nativeio_touchin_construct(nativeio_touchin_obj_t* self,
}
// Run a measurement to get calibrated.
common_hal_nativeio_touchin_get_value(self);
common_hal_touchio_touchin_get_value(self);
}
// This may get called twice, once on exit or deinit and once from the finaliser
// (__del__). We need the finaliser to reduce the chance of us writing to memory
// we no longer own because we keep track of Python pointers in active_touchin_obj.
void common_hal_nativeio_touchin_deinit(nativeio_touchin_obj_t* self) {
void common_hal_touchio_touchin_deinit(touchio_touchin_obj_t* self) {
touch_selfcap_sensor_disable(self->sensor_id);
// Remove ourselves from the list of active objects. We don't change the
// selfcap settings at all because we don't want to reinit now. Instead,
@ -273,7 +273,7 @@ void touch_selfcap_measure_complete_callback(void)
touch_acq_status = p_selfcap_measure_data->acq_status;
}
bool common_hal_nativeio_touchin_get_value(nativeio_touchin_obj_t *self) {
bool common_hal_touchio_touchin_get_value(touchio_touchin_obj_t *self) {
if (p_selfcap_measure_data->acq_status & TOUCH_CC_CALIB_ERROR) {
mp_raise_RuntimeError("Touch calibration error");
}

View File

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

View File

@ -0,0 +1 @@
// No touchio module functions.

View File

@ -24,13 +24,13 @@
#include "asf/sam0/drivers/system/system.h"
#include <board.h>
#include "common-hal/nativeio/AnalogIn.h"
#include "common-hal/nativeio/PulseOut.h"
#include "common-hal/nativeio/PWMOut.h"
#include "common-hal/analogio/AnalogIn.h"
#include "common-hal/pulseio/PulseOut.h"
#include "common-hal/pulseio/PWMOut.h"
#include "common-hal/usb_hid/__init__.h"
#ifdef EXPRESS_BOARD
#include "common-hal/nativeio/types.h"
#include "common-hal/touchio/TouchIn.h"
#include "QTouch/touch_api_ptc.h"
#define INTERNAL_CIRCUITPY_CONFIG_START_ADDR (0x00040000 - 0x100)
#else
@ -138,7 +138,7 @@ void reset_mp(void) {
}
#ifdef EXPRESS_BOARD
extern nativeio_touchin_obj_t *active_touchin_obj[DEF_SELFCAP_NUM_CHANNELS];
extern touchio_touchin_obj_t *active_touchin_obj[DEF_SELFCAP_NUM_CHANNELS];
extern touch_selfcap_config_t selfcap_config;
#endif
extern volatile bool mp_msc_enabled;

View File

@ -126,7 +126,10 @@ typedef long mp_off_t;
// extra built in modules to add to the list of known ones
extern const struct _mp_obj_module_t microcontroller_module;
extern const struct _mp_obj_module_t bitbangio_module;
extern const struct _mp_obj_module_t nativeio_module;
extern const struct _mp_obj_module_t analogio_module;
extern const struct _mp_obj_module_t digitalio_module;
extern const struct _mp_obj_module_t pulseio_module;
extern const struct _mp_obj_module_t busio_module;
extern const struct _mp_obj_module_t board_module;
extern const struct _mp_obj_module_t uos_module;
extern const struct _mp_obj_module_t time_module;
@ -137,8 +140,10 @@ extern const struct _mp_obj_module_t usb_hid_module;
// Internal flash size dependent settings.
#if BOARD_FLASH_SIZE > 192000
extern const struct _mp_obj_module_t touchio_module;
#define MICROPY_PY_MICROPYTHON_MEM_INFO (1)
#define EXTRA_BUILTIN_MODULES \
{ MP_OBJ_NEW_QSTR(MP_QSTR_touchio), (mp_obj_t)&touchio_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_bitbangio), (mp_obj_t)&bitbangio_module }
#define EXPRESS_BOARD
#else
@ -148,7 +153,10 @@ extern const struct _mp_obj_module_t usb_hid_module;
#define MICROPY_PORT_BUILTIN_MODULES \
{ MP_OBJ_NEW_QSTR(MP_QSTR_microcontroller), (mp_obj_t)&microcontroller_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_nativeio), (mp_obj_t)&nativeio_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_analogio), (mp_obj_t)&analogio_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_digitalio), (mp_obj_t)&digitalio_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_pulseio), (mp_obj_t)&pulseio_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_busio), (mp_obj_t)&busio_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_board), (mp_obj_t)&board_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_uos), (mp_obj_t)&uos_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&time_module }, \

View File

@ -4,7 +4,7 @@
#include "mphalport.h"
#include "shared-bindings/bitbangio/SPI.h"
#include "shared-bindings/nativeio/DigitalInOut.h"
#include "shared-bindings/digitalio/DigitalInOut.h"
#include "shared-bindings/neopixel_write/__init__.h"
#include "shared-module/bitbangio/types.h"
#include "rgb_led_status.h"
@ -12,7 +12,7 @@
#ifdef MICROPY_HW_NEOPIXEL
static uint8_t status_neopixel_color[3];
static nativeio_digitalinout_obj_t status_neopixel;
static digitalio_digitalinout_obj_t status_neopixel;
#endif
#if defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK)
@ -26,8 +26,8 @@ static uint32_t current_status_color = 0;
void rgb_led_status_init() {
#ifdef MICROPY_HW_NEOPIXEL
common_hal_nativeio_digitalinout_construct(&status_neopixel, MICROPY_HW_NEOPIXEL);
common_hal_nativeio_digitalinout_switch_to_output(&status_neopixel, false, DRIVE_MODE_PUSH_PULL);
common_hal_digitalio_digitalinout_construct(&status_neopixel, MICROPY_HW_NEOPIXEL);
common_hal_digitalio_digitalinout_switch_to_output(&status_neopixel, false, DRIVE_MODE_PUSH_PULL);
#endif
#if defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK)
shared_module_bitbangio_spi_construct(&status_apa102,

View File

@ -1,5 +1,7 @@
# Adding `nativeio` support to other ports
`nativeio` provides a well-defined, cross-port hardware abstraction layer built to support different devices and their drivers. It's backed by the Common HAL, a C api suitable for supporting different hardware in a similar manner. By sharing this C api, developers can support new hardware easily and cross-port functionality to the new hardware.
# Adding `digitalio` support to other ports
`digitalio` provides a well-defined, cross-port hardware abstraction layer built to support different devices and their drivers. It's backed by the Common HAL, a C api suitable for supporting different hardware in a similar manner. By sharing this C api, developers can support new hardware easily and cross-port functionality to the new hardware.
These instructions also apply to `analogio`, `busio`, `pulseio` and `touchio`. Most drivers depend on `analogio`, `digitalio` and `busio` so start with those.
## File layout
Common HAL related files are found in these locations:
@ -20,15 +22,23 @@ SRC_BINDINGS = \
board/__init__.c \
microcontroller/__init__.c \
microcontroller/Pin.c \
nativeio/__init__.c \
nativeio/AnalogIn.c \
nativeio/AnalogOut.c \
nativeio/DigitalInOut.c \
nativeio/I2C.c \
nativeio/PWMOut.c \
nativeio/SPI.c \
analogio/__init__.c \
analogio/AnalogIn.c \
analogio/AnalogOut.c \
digitalio/__init__.c \
digitalio/DigitalInOut.c \
pulseio/__init__.c \
pulseio/PulseIn.c \
pulseio/PulseOut.c \
pulseio/PWMOut.c \
busio/__init__.c \
busio/I2C.c \
busio/SPI.c \
busio/UART.c \
neopixel_write/__init__.c \
time/__init__.c
time/__init__.c \
usb_hid/__init__.c \
usb_hid/Device.c
SRC_BINDINGS_EXPANDED = $(addprefix shared-bindings/, $(SRC_BINDINGS)) \
$(addprefix common-hal/, $(SRC_BINDINGS))
@ -46,14 +56,20 @@ Built in modules are typically defined in `mpconfigport.h`. To add support you s
```
extern const struct _mp_obj_module_t microcontroller_module;
extern const struct _mp_obj_module_t nativeio_module;
extern const struct _mp_obj_module_t analogio_module;
extern const struct _mp_obj_module_t digitalio_module;
extern const struct _mp_obj_module_t pulseio_module;
extern const struct _mp_obj_module_t busio_module;
extern const struct _mp_obj_module_t board_module;
extern const struct _mp_obj_module_t time_module;
extern const struct _mp_obj_module_t neopixel_write_module;
#define MICROPY_PORT_BUILTIN_MODULES \
{ MP_OBJ_NEW_QSTR(MP_QSTR_microcontroller), (mp_obj_t)&microcontroller_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_nativeio), (mp_obj_t)&nativeio_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_analogio), (mp_obj_t)&analogio_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_digitalio), (mp_obj_t)&digitalio_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_pulseio), (mp_obj_t)&pulseio_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_busio), (mp_obj_t)&busio_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_board), (mp_obj_t)&board_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&time_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_neopixel_write),(mp_obj_t)&neopixel_write_module } \

View File

@ -267,8 +267,8 @@ you can because you never know how much something will be called. Fewer
allocations means less time spent cleaning up. So, where you can, prefer
bytearray buffers that are created in ``__init__`` and used throughout the
object with methods that read or write into the buffer instead of creating new
objects. `nativeio` classes are design to read and write to subsections of
buffers.
objects. Unified hardware API classes such as `busio.SPI` are design to read and
write to subsections of buffers.
Its ok to allocate an object to return to the user. Just beware of causing more
than one allocation per call due to internal logic.

View File

@ -79,7 +79,7 @@ SRC_C = \
esp_init_data.c \
gccollect.c \
lexerstr32.c \
uart.c \
espuart.c \
esppwm.c \
esponewire.c \
espneopixel.c \
@ -109,16 +109,18 @@ SRC_C = \
SRC_COMMON_HAL = \
microcontroller/__init__.c \
microcontroller/Pin.c \
nativeio/__init__.c \
nativeio/AnalogIn.c \
nativeio/AnalogOut.c \
nativeio/DigitalInOut.c \
nativeio/PulseIn.c \
nativeio/PulseOut.c \
nativeio/PWMOut.c \
nativeio/SPI.c \
nativeio/TouchIn.c \
nativeio/UART.c \
analogio/__init__.c \
analogio/AnalogIn.c \
analogio/AnalogOut.c \
digitalio/__init__.c \
digitalio/DigitalInOut.c \
pulseio/__init__.c \
pulseio/PulseIn.c \
pulseio/PulseOut.c \
pulseio/PWMOut.c \
busio/__init__.c \
busio/SPI.c \
busio/UART.c \
neopixel_write/__init__.c \
time/__init__.c \
board/__init__.c
@ -131,8 +133,8 @@ SRC_SHARED_MODULE = \
bitbangio/I2C.c \
bitbangio/OneWire.c \
bitbangio/SPI.c \
nativeio/I2C.c \
nativeio/OneWire.c \
busio/I2C.c \
busio/OneWire.c \
SRC_SHARED_MODULE_EXPANDED = $(addprefix shared-bindings/, $(SRC_SHARED_MODULE)) \
$(addprefix shared-module/, $(SRC_SHARED_MODULE))

View File

@ -32,13 +32,13 @@
#include "py/binary.h"
#include "py/mphal.h"
#include "common-hal/microcontroller/__init__.h"
#include "shared-bindings/nativeio/AnalogIn.h"
#include "shared-bindings/analogio/AnalogIn.h"
#include "user_interface.h"
volatile bool adc_in_use __attribute__((aligned(4))) = false;
void common_hal_nativeio_analogin_construct(nativeio_analogin_obj_t* self,
void common_hal_analogio_analogin_construct(analogio_analogin_obj_t* self,
const mcu_pin_obj_t *pin) {
if (pin != &pin_TOUT) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Pin %q does not have ADC capabilities", pin->name));
@ -46,15 +46,15 @@ void common_hal_nativeio_analogin_construct(nativeio_analogin_obj_t* self,
adc_in_use = true;
}
void common_hal_nativeio_analogin_deinit(nativeio_analogin_obj_t* self) {
void common_hal_analogio_analogin_deinit(analogio_analogin_obj_t* self) {
adc_in_use = false;
}
uint16_t common_hal_nativeio_analogin_get_value(nativeio_analogin_obj_t *self) {
uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) {
// ADC is 10 bit so shift by 6 to make it 16-bit.
return system_adc_read() << 6;
}
float common_hal_nativeio_analogin_get_reference_voltage(nativeio_analogin_obj_t *self) {
float common_hal_analogio_analogin_get_reference_voltage(analogio_analogin_obj_t *self) {
return 1.0f;
}

View File

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

View File

@ -30,18 +30,17 @@
#include "py/runtime.h"
#include "shared-bindings/nativeio/AnalogOut.h"
#include "shared-bindings/analogio/AnalogOut.h"
void common_hal_nativeio_analogout_construct(nativeio_analogout_obj_t* self,
void common_hal_analogio_analogout_construct(analogio_analogout_obj_t* self,
const mcu_pin_obj_t *pin) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError,
"No hardware support for analog out."));
}
void common_hal_nativeio_analogout_deinit(nativeio_analogout_obj_t *self) {
void common_hal_analogio_analogout_deinit(analogio_analogout_obj_t *self) {
}
void common_hal_nativeio_analogout_set_value(nativeio_analogout_obj_t *self,
void common_hal_analogio_analogout_set_value(analogio_analogout_obj_t *self,
uint16_t value) {
}

View File

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

View File

@ -0,0 +1 @@
// No analogio module functions.

View File

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

View File

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

View File

@ -27,7 +27,7 @@
#include "esp8266/ets_alt_task.h"
#include "esp8266/hspi.h"
#include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/nativeio/SPI.h"
#include "common-hal/busio/SPI.h"
#include "py/nlr.h"
#include "eagle_soc.h"
@ -38,7 +38,7 @@ extern const mcu_pin_obj_t pin_MTMS;
extern const mcu_pin_obj_t pin_MTCK;
extern const mcu_pin_obj_t pin_MTDI;
void common_hal_nativeio_spi_construct(nativeio_spi_obj_t *self,
void common_hal_busio_spi_construct(busio_spi_obj_t *self,
const mcu_pin_obj_t * clock, const mcu_pin_obj_t * mosi,
const mcu_pin_obj_t * miso) {
if (clock != &pin_MTMS || !((mosi == &pin_MTCK && miso == MP_OBJ_TO_PTR(mp_const_none)) ||
@ -73,7 +73,7 @@ void common_hal_nativeio_spi_construct(nativeio_spi_obj_t *self,
CLEAR_PERI_REG_MASK(SPI_USER(HSPI), SPI_FLASH_MODE);
}
void common_hal_nativeio_spi_deinit(nativeio_spi_obj_t *self) {
void common_hal_busio_spi_deinit(busio_spi_obj_t *self) {
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, 0);
PIN_PULLUP_DIS(PERIPHS_IO_MUX_MTDI_U);
@ -87,7 +87,7 @@ void common_hal_nativeio_spi_deinit(nativeio_spi_obj_t *self) {
gpio_output_set(0x0, 0x0, 0x0, 0x7 << 12);
}
bool common_hal_nativeio_spi_configure(nativeio_spi_obj_t *self,
bool common_hal_busio_spi_configure(busio_spi_obj_t *self,
uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t bits) {
if (bits != 8) {
return false;
@ -112,7 +112,7 @@ bool common_hal_nativeio_spi_configure(nativeio_spi_obj_t *self,
return true;
}
bool common_hal_nativeio_spi_try_lock(nativeio_spi_obj_t *self) {
bool common_hal_busio_spi_try_lock(busio_spi_obj_t *self) {
bool success = false;
common_hal_mcu_disable_interrupts();
if (!self->locked) {
@ -123,15 +123,15 @@ bool common_hal_nativeio_spi_try_lock(nativeio_spi_obj_t *self) {
return success;
}
bool common_hal_nativeio_spi_has_lock(nativeio_spi_obj_t *self) {
bool common_hal_busio_spi_has_lock(busio_spi_obj_t *self) {
return self->locked;
}
void common_hal_nativeio_spi_unlock(nativeio_spi_obj_t *self) {
void common_hal_busio_spi_unlock(busio_spi_obj_t *self) {
self->locked = false;
}
bool common_hal_nativeio_spi_write(nativeio_spi_obj_t *self,
bool common_hal_busio_spi_write(busio_spi_obj_t *self,
const uint8_t * data, size_t len) {
size_t chunk_size = 1024;
size_t count = len / chunk_size;
@ -151,7 +151,7 @@ bool common_hal_nativeio_spi_write(nativeio_spi_obj_t *self,
return true;
}
bool common_hal_nativeio_spi_read(nativeio_spi_obj_t *self,
bool common_hal_busio_spi_read(busio_spi_obj_t *self,
uint8_t * data, size_t len, uint8_t write_value) {
// Process data in chunks, let the pending tasks run in between
size_t chunk_size = 1024; // TODO this should depend on baudrate

View File

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

View File

@ -26,17 +26,17 @@
#include "common-hal/microcontroller/__init__.h"
#include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/nativeio/UART.h"
#include "shared-bindings/busio/UART.h"
#include "ets_sys.h"
#include "uart.h"
#include "espuart.h"
#include "py/nlr.h"
// UartDev is defined and initialized in rom code.
extern UartDevice UartDev;
void common_hal_nativeio_uart_construct(nativeio_uart_obj_t *self,
void common_hal_busio_uart_construct(busio_uart_obj_t *self,
const mcu_pin_obj_t * tx, const mcu_pin_obj_t * rx, uint32_t baudrate,
uint8_t bits, uart_parity_t parity, uint8_t stop, uint32_t timeout,
uint8_t receiver_buffer_size) {
@ -92,16 +92,16 @@ void common_hal_nativeio_uart_construct(nativeio_uart_obj_t *self,
uart_setup(UART1);
}
void common_hal_nativeio_uart_deinit(nativeio_uart_obj_t *self) {
void common_hal_busio_uart_deinit(busio_uart_obj_t *self) {
PIN_FUNC_SELECT(FUNC_U1TXD_BK, 0);
}
size_t common_hal_nativeio_uart_read(nativeio_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) {
return 0;
}
// Write characters.
size_t common_hal_nativeio_uart_write(nativeio_uart_obj_t *self, const uint8_t *data, size_t len, int *errcode) {
size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data, size_t len, int *errcode) {
// write the data
for (size_t i = 0; i < len; ++i) {
uart_tx_one_char(UART1, *data++);
@ -111,10 +111,10 @@ size_t common_hal_nativeio_uart_write(nativeio_uart_obj_t *self, const uint8_t *
return len;
}
uint32_t common_hal_nativeio_uart_rx_characters_available(nativeio_uart_obj_t *self) {
uint32_t common_hal_busio_uart_rx_characters_available(busio_uart_obj_t *self) {
return 0;
}
bool common_hal_nativeio_uart_ready_to_tx(nativeio_uart_obj_t *self) {
bool common_hal_busio_uart_ready_to_tx(busio_uart_obj_t *self) {
return true;
}

View File

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

View File

@ -0,0 +1 @@
// No busio module functions.

View File

@ -32,16 +32,16 @@
#include "py/runtime.h"
#include "py/mphal.h"
#include "shared-bindings/nativeio/DigitalInOut.h"
#include "shared-bindings/digitalio/DigitalInOut.h"
digitalinout_result_t common_hal_nativeio_digitalinout_construct(
nativeio_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) {
self->pin = pin;
PIN_FUNC_SELECT(self->pin->peripheral, self->pin->gpio_function);
return DIGITALINOUT_OK;
}
void common_hal_nativeio_digitalinout_deinit(nativeio_digitalinout_obj_t* self) {
void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t* self) {
if (self->pin->gpio_number < 16) {
uint32_t pin_mask = 1 << self->pin->gpio_number;
gpio_output_set(0x0, 0x0, 0x0, pin_mask);
@ -50,8 +50,8 @@ void common_hal_nativeio_digitalinout_deinit(nativeio_digitalinout_obj_t* self)
}
}
void common_hal_nativeio_digitalinout_switch_to_input(
nativeio_digitalinout_obj_t* self, enum digitalinout_pull_t pull) {
void common_hal_digitalio_digitalinout_switch_to_input(
digitalio_digitalinout_obj_t* self, enum digitalinout_pull_t pull) {
self->output = false;
if (self->pin->gpio_number == 16) {
@ -62,11 +62,11 @@ void common_hal_nativeio_digitalinout_switch_to_input(
PIN_PULLUP_DIS(self->pin->peripheral);
gpio_output_set(0, 0, 0, 1 << self->pin->gpio_number);
}
common_hal_nativeio_digitalinout_set_pull(self, pull);
common_hal_digitalio_digitalinout_set_pull(self, pull);
}
void common_hal_nativeio_digitalinout_switch_to_output(
nativeio_digitalinout_obj_t* self, bool value,
void common_hal_digitalio_digitalinout_switch_to_output(
digitalio_digitalinout_obj_t* self, bool value,
enum digitalinout_drive_mode_t drive_mode) {
self->output = true;
self->open_drain = drive_mode == DRIVE_MODE_OPEN_DRAIN;
@ -78,16 +78,16 @@ void common_hal_nativeio_digitalinout_switch_to_output(
gpio_output_set(0, 0, 1 << self->pin->gpio_number, 0);
PIN_PULLUP_DIS(self->pin->peripheral);
}
common_hal_nativeio_digitalinout_set_value(self, value);
common_hal_digitalio_digitalinout_set_value(self, value);
}
enum digitalinout_direction_t common_hal_nativeio_digitalinout_get_direction(
nativeio_digitalinout_obj_t* self) {
enum digitalinout_direction_t common_hal_digitalio_digitalinout_get_direction(
digitalio_digitalinout_obj_t* self) {
return self->output? DIRECTION_OUT : DIRECTION_IN;
}
void common_hal_nativeio_digitalinout_set_value(
nativeio_digitalinout_obj_t* self, bool value) {
void common_hal_digitalio_digitalinout_set_value(
digitalio_digitalinout_obj_t* self, bool value) {
if (value) {
if (self->open_drain) {
// Disable output.
@ -109,8 +109,8 @@ void common_hal_nativeio_digitalinout_set_value(
// Register addresses taken from: https://github.com/esp8266/esp8266-wiki/wiki/gpio-registers
volatile uint32_t* PIN_DIR = (uint32_t *) 0x6000030C;
volatile uint32_t* PIN_OUT = (uint32_t *) 0x60000300;
bool common_hal_nativeio_digitalinout_get_value(
nativeio_digitalinout_obj_t* self) {
bool common_hal_digitalio_digitalinout_get_value(
digitalio_digitalinout_obj_t* self) {
if (!self->output) {
if (self->pin->gpio_number == 16) {
return READ_PERI_REG(RTC_GPIO_IN_DATA) & 1;
@ -126,20 +126,20 @@ bool common_hal_nativeio_digitalinout_get_value(
}
}
void common_hal_nativeio_digitalinout_set_drive_mode(
nativeio_digitalinout_obj_t* self,
void common_hal_digitalio_digitalinout_set_drive_mode(
digitalio_digitalinout_obj_t* self,
enum digitalinout_drive_mode_t drive_mode) {
bool value = common_hal_nativeio_digitalinout_get_value(self);
bool value = common_hal_digitalio_digitalinout_get_value(self);
self->open_drain = drive_mode == DRIVE_MODE_OPEN_DRAIN;
// True is implemented differently between modes so reset the value to make
// sure its correct for the new mode.
if (value) {
common_hal_nativeio_digitalinout_set_value(self, value);
common_hal_digitalio_digitalinout_set_value(self, value);
}
}
enum digitalinout_drive_mode_t common_hal_nativeio_digitalinout_get_drive_mode(
nativeio_digitalinout_obj_t* self) {
enum digitalinout_drive_mode_t common_hal_digitalio_digitalinout_get_drive_mode(
digitalio_digitalinout_obj_t* self) {
if (self->open_drain) {
return DRIVE_MODE_OPEN_DRAIN;
} else {
@ -147,8 +147,8 @@ enum digitalinout_drive_mode_t common_hal_nativeio_digitalinout_get_drive_mode(
}
}
void common_hal_nativeio_digitalinout_set_pull(
nativeio_digitalinout_obj_t* self, enum digitalinout_pull_t pull) {
void common_hal_digitalio_digitalinout_set_pull(
digitalio_digitalinout_obj_t* self, enum digitalinout_pull_t pull) {
if (pull == PULL_DOWN) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError,
"ESP8266 does not support pull down."));
@ -166,8 +166,8 @@ void common_hal_nativeio_digitalinout_set_pull(
}
}
enum digitalinout_pull_t common_hal_nativeio_digitalinout_get_pull(
nativeio_digitalinout_obj_t* self) {
enum digitalinout_pull_t common_hal_digitalio_digitalinout_get_pull(
digitalio_digitalinout_obj_t* self) {
if (self->pin->gpio_number < 16 &&
(READ_PERI_REG(self->pin->peripheral) & PERIPHS_IO_MUX_PULLUP) != 0) {
return PULL_UP;

View File

@ -24,9 +24,10 @@
* THE SOFTWARE.
*/
#ifndef __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_DIGITALINOUT_H__
#define __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_DIGITALINOUT_H__
#ifndef __MICROPY_INCLUDED_ESP8266_COMMON_HAL_DIGITALIO_DIGITALINOUT_H__
#define __MICROPY_INCLUDED_ESP8266_COMMON_HAL_DIGITALIO_DIGITALINOUT_H__
#include "common-hal/microcontroller/types.h"
#include "py/obj.h"
typedef struct {
@ -34,6 +35,6 @@ typedef struct {
const mcu_pin_obj_t * pin;
bool output;
bool open_drain;
} nativeio_digitalinout_obj_t;
} digitalio_digitalinout_obj_t;
#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_DIGITALINOUT_H__
#endif // __MICROPY_INCLUDED_ESP8266_COMMON_HAL_DIGITALIO_DIGITALINOUT_H__

View File

@ -0,0 +1 @@
// No digitalio module functions.

View File

@ -1,46 +0,0 @@
/*
* This file is part of the Micro Python 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.
*/
#include <string.h>
#include "py/nlr.h"
#include "py/runtime.h"
#include "py/binary.h"
#include "py/mphal.h"
#include "shared-bindings/nativeio/TouchIn.h"
void common_hal_nativeio_touchin_construct(nativeio_touchin_obj_t* self,
const mcu_pin_obj_t *pin) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError,
"No hardware support for capacitive touch."));
}
void common_hal_nativeio_touchin_deinit(nativeio_touchin_obj_t* self) {
}
bool common_hal_nativeio_touchin_get_value(nativeio_touchin_obj_t *self) {
return false;
}

View File

@ -1 +0,0 @@
// No nativeio module functions.

View File

@ -1,78 +0,0 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2016 Scott Shawcroft
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
// This defines the types used to underly the standard nativeio Python objects.
// The shared API is defined in terms of these types.
#ifndef __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_TYPES_H__
#define __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_TYPES_H__
#include "common-hal/microcontroller/types.h"
#include "py/obj.h"
// Use the bitbang wrapper for I2C and OneWire
#include "shared-module/nativeio/I2C.h"
#include "shared-module/nativeio/OneWire.h"
typedef struct {
mp_obj_base_t base;
const mcu_pin_obj_t * pin;
} nativeio_analogin_obj_t;
// Not supported, throws error on construction.
typedef struct {
mp_obj_base_t base;
} nativeio_analogout_obj_t;
typedef struct {
mp_obj_base_t base;
bool locked;
} nativeio_spi_obj_t;
typedef struct {
mp_obj_base_t base;
} nativeio_pulsein_obj_t;
typedef struct {
mp_obj_base_t base;
} nativeio_pulseout_obj_t;
typedef struct {
mp_obj_base_t base;
int channel;
const mcu_pin_obj_t* pin;
} nativeio_pwmout_obj_t;
typedef struct {
mp_obj_base_t base;
} nativeio_touchin_obj_t;
typedef struct {
mp_obj_base_t base;
} nativeio_uart_obj_t;
#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_TYPES_H__

View File

@ -28,6 +28,6 @@
#include "espneopixel.h"
void common_hal_neopixel_write(const nativeio_digitalinout_obj_t* digitalinout, uint8_t *pixels, uint32_t numBytes) {
void common_hal_neopixel_write(const digitalio_digitalinout_obj_t* digitalinout, uint8_t *pixels, uint32_t numBytes) {
esp_neopixel_write(digitalinout->pin->gpio_number, pixels, numBytes);
}

View File

@ -30,7 +30,7 @@
#include "esppwm.h"
#include "py/runtime.h"
#include "shared-bindings/nativeio/PWMOut.h"
#include "shared-bindings/pulseio/PWMOut.h"
#include "eagle_soc.h"
#include "c_types.h"
@ -46,7 +46,7 @@ void pwmout_reset(void) {
first_channel_variable = false;
}
void common_hal_nativeio_pwmout_construct(nativeio_pwmout_obj_t* self, const mcu_pin_obj_t* pin, uint16_t duty, uint32_t frequency,
void common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self, const mcu_pin_obj_t* pin, uint16_t duty, uint32_t frequency,
bool variable_frequency) {
if (frequency > PWM_FREQ_MAX) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
@ -77,7 +77,7 @@ void common_hal_nativeio_pwmout_construct(nativeio_pwmout_obj_t* self, const mcu
}
}
extern void common_hal_nativeio_pwmout_deinit(nativeio_pwmout_obj_t* self) {
extern void common_hal_pulseio_pwmout_deinit(pulseio_pwmout_obj_t* self) {
pwm_delete(self->channel);
pwm_start();
if (self->pin->gpio_number < 16) {
@ -88,17 +88,17 @@ extern void common_hal_nativeio_pwmout_deinit(nativeio_pwmout_obj_t* self) {
}
}
extern void common_hal_nativeio_pwmout_set_duty_cycle(nativeio_pwmout_obj_t* self, uint16_t duty) {
extern 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();
}
uint16_t common_hal_nativeio_pwmout_get_duty_cycle(nativeio_pwmout_obj_t* self) {
uint16_t common_hal_pulseio_pwmout_get_duty_cycle(pulseio_pwmout_obj_t* self) {
return pwm_get_duty(self->channel) << 6;
}
void common_hal_nativeio_pwmout_set_frequency(nativeio_pwmout_obj_t* self, uint32_t frequency) {
void common_hal_pulseio_pwmout_set_frequency(pulseio_pwmout_obj_t* self, uint32_t frequency) {
if (frequency > PWM_FREQ_MAX) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
"Maximum PWM frequency is %dhz.", PWM_FREQ_MAX));
@ -109,10 +109,10 @@ void common_hal_nativeio_pwmout_set_frequency(nativeio_pwmout_obj_t* self, uint3
pwm_set_freq(frequency, 0);
}
uint32_t common_hal_nativeio_pwmout_get_frequency(nativeio_pwmout_obj_t* self) {
uint32_t common_hal_pulseio_pwmout_get_frequency(pulseio_pwmout_obj_t* self) {
return pwm_get_freq(0);
}
bool common_hal_nativeio_pwmout_get_variable_frequency(nativeio_pwmout_obj_t* self) {
bool common_hal_pulseio_pwmout_get_variable_frequency(pulseio_pwmout_obj_t* self) {
return first_channel_variable;
}

View File

@ -0,0 +1,40 @@
/*
* 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_ESP8266_COMMON_HAL_PULSEIO_PWMOUT_H__
#define __MICROPY_INCLUDED_ESP8266_COMMON_HAL_PULSEIO_PWMOUT_H__
#include "common-hal/microcontroller/types.h"
typedef struct {
mp_obj_base_t base;
int channel;
const mcu_pin_obj_t* pin;
} pulseio_pwmout_obj_t;
void pwmout_reset(void);
#endif // __MICROPY_INCLUDED_ESP8266_COMMON_HAL_PULSEIO_PWMOUT_H__

View File

@ -30,40 +30,40 @@
#include "py/gc.h"
#include "py/runtime.h"
#include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/nativeio/PulseIn.h"
#include "shared-bindings/pulseio/PulseIn.h"
void common_hal_nativeio_pulsein_construct(nativeio_pulsein_obj_t* self,
void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self,
const mcu_pin_obj_t* pin, uint16_t maxlen, bool idle_state) {
mp_raise_NotImplementedError("");
}
void common_hal_nativeio_pulsein_deinit(nativeio_pulsein_obj_t* self) {
void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t* self) {
}
void common_hal_nativeio_pulsein_pause(nativeio_pulsein_obj_t* self) {
void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t* self) {
}
void common_hal_nativeio_pulsein_resume(nativeio_pulsein_obj_t* self,
void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self,
uint16_t trigger_duration) {
}
void common_hal_nativeio_pulsein_clear(nativeio_pulsein_obj_t* self) {
void common_hal_pulseio_pulsein_clear(pulseio_pulsein_obj_t* self) {
}
uint16_t common_hal_nativeio_pulsein_popleft(nativeio_pulsein_obj_t* self) {
uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t* self) {
return 0;
}
uint16_t common_hal_nativeio_pulsein_get_maxlen(nativeio_pulsein_obj_t* self) {
uint16_t common_hal_pulseio_pulsein_get_maxlen(pulseio_pulsein_obj_t* self) {
return 0;
}
uint16_t common_hal_nativeio_pulsein_get_len(nativeio_pulsein_obj_t* self) {
uint16_t common_hal_pulseio_pulsein_get_len(pulseio_pulsein_obj_t* self) {
return 0;
}
uint16_t common_hal_nativeio_pulsein_get_item(nativeio_pulsein_obj_t* self,
uint16_t common_hal_pulseio_pulsein_get_item(pulseio_pulsein_obj_t* self,
int16_t index) {
return 0;
}

View File

@ -24,9 +24,15 @@
* THE SOFTWARE.
*/
#ifndef __MICROPY_INCLUDED_ESP8266_COMMON_HAL_NATIVEIO_PWMOUT_H__
#define __MICROPY_INCLUDED_ESP8266_COMMON_HAL_NATIVEIO_PWMOUT_H__
#ifndef __MICROPY_INCLUDED_ESP8266_COMMON_HAL_PULSEIO_PULSEIN_H__
#define __MICROPY_INCLUDED_ESP8266_COMMON_HAL_PULSEIO_PULSEIN_H__
#include "py/obj.h"
typedef struct {
mp_obj_base_t base;
} pulseio_pulsein_obj_t;
void pwmout_reset(void);
#endif // __MICROPY_INCLUDED_ESP8266_COMMON_HAL_NATIVEIO_PWMOUT_H__
#endif // __MICROPY_INCLUDED_ESP8266_COMMON_HAL_PULSEIO_PULSEIN_H__

View File

@ -28,15 +28,15 @@
#include <stdint.h>
#include "py/runtime.h"
#include "shared-bindings/nativeio/PulseOut.h"
#include "shared-bindings/pulseio/PulseOut.h"
void common_hal_nativeio_pulseout_construct(nativeio_pulseout_obj_t* self,
const nativeio_pwmout_obj_t* carrier) {
void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self,
const pulseio_pwmout_obj_t* carrier) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "No hardware support for PulseOut."));
}
void common_hal_nativeio_pulseout_deinit(nativeio_pulseout_obj_t* self) {
void common_hal_pulseio_pulseout_deinit(pulseio_pulseout_obj_t* self) {
}
void common_hal_nativeio_pulseout_send(nativeio_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

@ -24,9 +24,15 @@
* THE SOFTWARE.
*/
#ifndef __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_PWMOUT_H__
#define __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_PWMOUT_H__
#ifndef __MICROPY_INCLUDED_ESP8266_COMMON_HAL_PULSEIO_PULSEOUT_H__
#define __MICROPY_INCLUDED_ESP8266_COMMON_HAL_PULSEIO_PULSEOUT_H__
#include "py/obj.h"
typedef struct {
mp_obj_base_t base;
} pulseio_pulseout_obj_t;
void pwmout_reset(void);
#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_PWMOUT_H__
#endif // __MICROPY_INCLUDED_ESP8266_COMMON_HAL_PULSEIO_PULSEOUT_H__

View File

@ -0,0 +1 @@
// No pulseio module functions.

View File

@ -27,7 +27,7 @@
#include <stdio.h>
#include "ets_sys.h"
#include "etshal.h"
#include "uart.h"
#include "espuart.h"
#include "esp_mphal.h"
#include "user_interface.h"
#include "ets_alt_task.h"

View File

@ -11,7 +11,7 @@
*******************************************************************************/
#include "ets_sys.h"
#include "osapi.h"
#include "uart.h"
#include "espuart.h"
#include "osapi.h"
#include "uart_register.h"
#include "etshal.h"

View File

@ -29,7 +29,7 @@
#include <string.h>
#include "ets_sys.h"
#include "uart.h"
#include "espuart.h"
#include "py/runtime.h"
#include "py/stream.h"

View File

@ -40,7 +40,7 @@
#include "gccollect.h"
#include "user_interface.h"
#include "common-hal/microcontroller/Pin.h"
#include "common-hal/nativeio/PWMOut.h"
#include "common-hal/pulseio/PWMOut.h"
STATIC char heap[36 * 1024];

View File

@ -38,7 +38,7 @@
#include "netutils.h"
#include "queue.h"
#include "ets_sys.h"
#include "uart.h"
#include "espuart.h"
#include "user_interface.h"
#include "espconn.h"
#include "spi_flash.h"

View File

@ -156,7 +156,10 @@ extern const struct _mp_obj_module_t mp_module_machine;
extern const struct _mp_obj_module_t onewire_module;
extern const struct _mp_obj_module_t microcontroller_module;
extern const struct _mp_obj_module_t board_module;
extern const struct _mp_obj_module_t nativeio_module;
extern const struct _mp_obj_module_t analogio_module;
extern const struct _mp_obj_module_t digitalio_module;
extern const struct _mp_obj_module_t pulseio_module;
extern const struct _mp_obj_module_t busio_module;
extern const struct _mp_obj_module_t bitbangio_module;
extern const struct _mp_obj_module_t time_module;
@ -172,7 +175,10 @@ extern const struct _mp_obj_module_t time_module;
{ MP_OBJ_NEW_QSTR(MP_QSTR__onewire), (mp_obj_t)&onewire_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_microcontroller), (mp_obj_t)&microcontroller_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_board), (mp_obj_t)&board_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_nativeio), (mp_obj_t)&nativeio_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_analogio), (mp_obj_t)&analogio_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_digitalio), (mp_obj_t)&digitalio_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_pulseio), (mp_obj_t)&pulseio_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_busio), (mp_obj_t)&busio_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_bitbangio), (mp_obj_t)&bitbangio_module }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&time_module }, \

View File

@ -33,19 +33,19 @@
#include "py/objproperty.h"
#include "py/runtime.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/nativeio/AnalogIn.h"
#include "shared-bindings/analogio/AnalogIn.h"
//| .. currentmodule:: nativeio
//| .. currentmodule:: analogio
//|
//| :class:`AnalogIn` -- read analog voltage
//| ============================================
//|
//| Usage::
//|
//| import nativeio
//| import analogio
//| from board import *
//|
//| with nativeio.AnalogIn(A1) as adc:
//| with analogio.AnalogIn(A1) as adc:
//| val = adc.value
//|
@ -56,7 +56,7 @@
//|
//| :param ~microcontroller.Pin pin: the pin to read from
//|
STATIC mp_obj_t nativeio_analogin_make_new(const mp_obj_type_t *type,
STATIC mp_obj_t analogio_analogin_make_new(const mp_obj_type_t *type,
mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
// check number of arguments
mp_arg_check_num(n_args, n_kw, 1, 1, false);
@ -65,11 +65,11 @@ STATIC mp_obj_t nativeio_analogin_make_new(const mp_obj_type_t *type,
mp_obj_t pin_obj = args[0];
assert_pin(pin_obj, false);
nativeio_analogin_obj_t *self = m_new_obj(nativeio_analogin_obj_t);
self->base.type = &nativeio_analogin_type;
analogio_analogin_obj_t *self = m_new_obj(analogio_analogin_obj_t);
self->base.type = &analogio_analogin_type;
const mcu_pin_obj_t *pin = MP_OBJ_TO_PTR(pin_obj);
assert_pin_free(pin);
common_hal_nativeio_analogin_construct(self, pin);
common_hal_analogio_analogin_construct(self, pin);
return (mp_obj_t) self;
}
@ -78,12 +78,12 @@ STATIC mp_obj_t nativeio_analogin_make_new(const mp_obj_type_t *type,
//|
//| Turn off the AnalogIn and release the pin for other use.
//|
STATIC mp_obj_t nativeio_analogin_deinit(mp_obj_t self_in) {
nativeio_analogin_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_nativeio_analogin_deinit(self);
STATIC mp_obj_t analogio_analogin_deinit(mp_obj_t self_in) {
analogio_analogin_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_analogio_analogin_deinit(self);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(nativeio_analogin_deinit_obj, nativeio_analogin_deinit);
MP_DEFINE_CONST_FUN_OBJ_1(analogio_analogin_deinit_obj, analogio_analogin_deinit);
//| .. method:: __enter__()
//|
@ -95,12 +95,12 @@ MP_DEFINE_CONST_FUN_OBJ_1(nativeio_analogin_deinit_obj, nativeio_analogin_deinit
//|
//| Automatically deinitializes the hardware when exiting a context.
//|
STATIC mp_obj_t nativeio_analogin___exit__(size_t n_args, const mp_obj_t *args) {
STATIC mp_obj_t analogio_analogin___exit__(size_t n_args, const mp_obj_t *args) {
(void)n_args;
common_hal_nativeio_analogin_deinit(args[0]);
common_hal_analogio_analogin_deinit(args[0]);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_analogin___exit___obj, 4, 4, nativeio_analogin___exit__);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(analogio_analogin___exit___obj, 4, 4, analogio_analogin___exit__);
//| .. attribute:: value
//|
@ -112,15 +112,15 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_analogin___exit___obj, 4, 4,
//| :return: the data read
//| :rtype: int
//|
STATIC mp_obj_t nativeio_analogin_obj_get_value(mp_obj_t self_in) {
nativeio_analogin_obj_t *self = MP_OBJ_TO_PTR(self_in);
return MP_OBJ_NEW_SMALL_INT(common_hal_nativeio_analogin_get_value(self));
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));
}
MP_DEFINE_CONST_FUN_OBJ_1(nativeio_analogin_get_value_obj, nativeio_analogin_obj_get_value);
MP_DEFINE_CONST_FUN_OBJ_1(analogio_analogin_get_value_obj, analogio_analogin_obj_get_value);
const mp_obj_property_t nativeio_analogin_value_obj = {
const mp_obj_property_t analogio_analogin_value_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&nativeio_analogin_get_value_obj,
.proxy = {(mp_obj_t)&analogio_analogin_get_value_obj,
(mp_obj_t)&mp_const_none_obj,
(mp_obj_t)&mp_const_none_obj},
};
@ -132,33 +132,33 @@ const mp_obj_property_t nativeio_analogin_value_obj = {
//| :return: the reference voltage
//| :rtype: float
//|
STATIC mp_obj_t nativeio_analogin_obj_get_reference_voltage(mp_obj_t self_in) {
nativeio_analogin_obj_t *self = MP_OBJ_TO_PTR(self_in);
return mp_obj_new_float(common_hal_nativeio_analogin_get_reference_voltage(self));
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));
}
MP_DEFINE_CONST_FUN_OBJ_1(nativeio_analogin_get_reference_voltage_obj,
nativeio_analogin_obj_get_reference_voltage);
MP_DEFINE_CONST_FUN_OBJ_1(analogio_analogin_get_reference_voltage_obj,
analogio_analogin_obj_get_reference_voltage);
const mp_obj_property_t nativeio_analogin_reference_voltage_obj = {
const mp_obj_property_t analogio_analogin_reference_voltage_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&nativeio_analogin_get_reference_voltage_obj,
.proxy = {(mp_obj_t)&analogio_analogin_get_reference_voltage_obj,
(mp_obj_t)&mp_const_none_obj,
(mp_obj_t)&mp_const_none_obj},
};
STATIC const mp_rom_map_elem_t nativeio_analogin_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_analogin_deinit_obj) },
STATIC const mp_rom_map_elem_t analogio_analogin_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&analogio_analogin_deinit_obj) },
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_analogin___exit___obj) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_value), MP_ROM_PTR(&nativeio_analogin_value_obj)},
{ MP_OBJ_NEW_QSTR(MP_QSTR_reference_voltage), MP_ROM_PTR(&nativeio_analogin_reference_voltage_obj)},
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&analogio_analogin___exit___obj) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_value), MP_ROM_PTR(&analogio_analogin_value_obj)},
{ MP_OBJ_NEW_QSTR(MP_QSTR_reference_voltage), MP_ROM_PTR(&analogio_analogin_reference_voltage_obj)},
};
STATIC MP_DEFINE_CONST_DICT(nativeio_analogin_locals_dict, nativeio_analogin_locals_dict_table);
STATIC MP_DEFINE_CONST_DICT(analogio_analogin_locals_dict, analogio_analogin_locals_dict_table);
const mp_obj_type_t nativeio_analogin_type = {
const mp_obj_type_t analogio_analogin_type = {
{ &mp_type_type },
.name = MP_QSTR_AnalogIn,
.make_new = nativeio_analogin_make_new,
.locals_dict = (mp_obj_t)&nativeio_analogin_locals_dict,
.make_new = analogio_analogin_make_new,
.locals_dict = (mp_obj_t)&analogio_analogin_locals_dict,
};

View File

@ -24,17 +24,17 @@
* THE SOFTWARE.
*/
#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ANALOGIN_H__
#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ANALOGIN_H__
#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGIO_ANALOGIN_H__
#define __MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGIO_ANALOGIN_H__
#include "common-hal/microcontroller/types.h"
#include "common-hal/nativeio/types.h"
#include "common-hal/analogio/AnalogIn.h"
extern const mp_obj_type_t nativeio_analogin_type;
extern const mp_obj_type_t analogio_analogin_type;
void common_hal_nativeio_analogin_construct(nativeio_analogin_obj_t* self, const mcu_pin_obj_t *pin);
void common_hal_nativeio_analogin_deinit(nativeio_analogin_obj_t* self);
uint16_t common_hal_nativeio_analogin_get_value(nativeio_analogin_obj_t *self);
float common_hal_nativeio_analogin_get_reference_voltage(nativeio_analogin_obj_t *self);
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);
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);
#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ANALOGIN_H__
#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGIO_ANALOGIN_H__

View File

@ -31,9 +31,9 @@
#include "py/objproperty.h"
#include "py/runtime.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/nativeio/AnalogOut.h"
#include "shared-bindings/analogio/AnalogOut.h"
//| .. currentmodule:: nativeio
//| .. currentmodule:: analogio
//|
//| :class:`AnalogOut` -- output analog voltage
//| ============================================
@ -42,10 +42,10 @@
//|
//| Example usage::
//|
//| import nativeio
//| import analogio
//| from microcontroller import pin
//|
//| with nativeio.AnalogOut(pin.PA02) as dac: # output on pin PA02
//| with analogio.AnalogOut(pin.PA02) as dac: # output on pin PA02
//| dac.value = 32768 # makes PA02 1.65V
//|
@ -55,17 +55,17 @@
//|
//| :param ~microcontroller.Pin pin: the pin to output to
//|
STATIC mp_obj_t nativeio_analogout_make_new(const mp_obj_type_t *type, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
STATIC mp_obj_t analogio_analogout_make_new(const mp_obj_type_t *type, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
// check arguments
mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
assert_pin(args[0], false);
const mcu_pin_obj_t *pin = MP_OBJ_TO_PTR(args[0]);
nativeio_analogout_obj_t *self = m_new_obj(nativeio_analogout_obj_t);
self->base.type = &nativeio_analogout_type;
analogio_analogout_obj_t *self = m_new_obj(analogio_analogout_obj_t);
self->base.type = &analogio_analogout_type;
assert_pin_free(pin);
common_hal_nativeio_analogout_construct(self, pin);
common_hal_analogio_analogout_construct(self, pin);
return self;
}
@ -74,14 +74,14 @@ STATIC mp_obj_t nativeio_analogout_make_new(const mp_obj_type_t *type, mp_uint_t
//|
//| Turn off the AnalogOut and release the pin for other use.
//|
STATIC mp_obj_t nativeio_analogout_deinit(mp_obj_t self_in) {
nativeio_analogout_obj_t *self = self_in;
STATIC mp_obj_t analogio_analogout_deinit(mp_obj_t self_in) {
analogio_analogout_obj_t *self = self_in;
common_hal_nativeio_analogout_deinit(self);
common_hal_analogio_analogout_deinit(self);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_analogout_deinit_obj, nativeio_analogout_deinit);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(analogio_analogout_deinit_obj, analogio_analogout_deinit);
//| .. method:: __enter__()
//|
@ -93,12 +93,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_analogout_deinit_obj, nativeio_analogo
//|
//| Automatically deinitializes the hardware when exiting a context.
//|
STATIC mp_obj_t nativeio_analogout___exit__(size_t n_args, const mp_obj_t *args) {
STATIC mp_obj_t analogio_analogout___exit__(size_t n_args, const mp_obj_t *args) {
(void)n_args;
common_hal_nativeio_analogout_deinit(args[0]);
common_hal_analogio_analogout_deinit(args[0]);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_analogout___exit___obj, 4, 4, nativeio_analogout___exit__);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(analogio_analogout___exit___obj, 4, 4, analogio_analogout___exit__);
//| .. attribute:: value
//|
@ -109,39 +109,39 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_analogout___exit___obj, 4, 4
//| :return: the last value written
//| :rtype: int
//|
STATIC mp_obj_t nativeio_analogout_obj_set_value(mp_obj_t self_in, mp_obj_t value) {
nativeio_analogout_obj_t *self = MP_OBJ_TO_PTR(self_in);
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);
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.");
}
common_hal_nativeio_analogout_set_value(self, v);
common_hal_analogio_analogout_set_value(self, v);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(nativeio_analogout_set_value_obj, nativeio_analogout_obj_set_value);
MP_DEFINE_CONST_FUN_OBJ_2(analogio_analogout_set_value_obj, analogio_analogout_obj_set_value);
const mp_obj_property_t nativeio_analogout_value_obj = {
const mp_obj_property_t analogio_analogout_value_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&mp_const_none_obj,
(mp_obj_t)&nativeio_analogout_set_value_obj,
(mp_obj_t)&analogio_analogout_set_value_obj,
(mp_obj_t)&mp_const_none_obj},
};
STATIC const mp_rom_map_elem_t nativeio_analogout_locals_dict_table[] = {
STATIC const mp_rom_map_elem_t analogio_analogout_locals_dict_table[] = {
// instance methods
{ MP_OBJ_NEW_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_analogout_deinit_obj) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&analogio_analogout_deinit_obj) },
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_analogout___exit___obj) },
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&analogio_analogout___exit___obj) },
// Properties
{ MP_OBJ_NEW_QSTR(MP_QSTR_value), (mp_obj_t)&nativeio_analogout_value_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_value), (mp_obj_t)&analogio_analogout_value_obj },
};
STATIC MP_DEFINE_CONST_DICT(nativeio_analogout_locals_dict, nativeio_analogout_locals_dict_table);
STATIC MP_DEFINE_CONST_DICT(analogio_analogout_locals_dict, analogio_analogout_locals_dict_table);
const mp_obj_type_t nativeio_analogout_type = {
const mp_obj_type_t analogio_analogout_type = {
{ &mp_type_type },
.name = MP_QSTR_AnalogOut,
.make_new = nativeio_analogout_make_new,
.locals_dict = (mp_obj_t)&nativeio_analogout_locals_dict,
.make_new = analogio_analogout_make_new,
.locals_dict = (mp_obj_t)&analogio_analogout_locals_dict,
};

View File

@ -24,16 +24,16 @@
* THE SOFTWARE.
*/
#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ANALOGOUT_H__
#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ANALOGOUT_H__
#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGIO_ANALOGOUT_H__
#define __MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGIO_ANALOGOUT_H__
#include "common-hal/microcontroller/types.h"
#include "common-hal/nativeio/types.h"
#include "common-hal/analogio/AnalogOut.h"
extern const mp_obj_type_t nativeio_analogout_type;
extern const mp_obj_type_t analogio_analogout_type;
void common_hal_nativeio_analogout_construct(nativeio_analogout_obj_t* self, const mcu_pin_obj_t *pin);
void common_hal_nativeio_analogout_deinit(nativeio_analogout_obj_t *self);
void common_hal_nativeio_analogout_set_value(nativeio_analogout_obj_t *self, uint16_t value);
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_set_value(analogio_analogout_obj_t *self, uint16_t value);
#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ANALOGOUT_H__
#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGIO_ANALOGOUT_H__

View File

@ -0,0 +1,84 @@
/*
* 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.
*/
#include <stdint.h>
#include "py/obj.h"
#include "py/runtime.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/analogio/__init__.h"
#include "shared-bindings/analogio/AnalogIn.h"
#include "shared-bindings/analogio/AnalogOut.h"
//| :mod:`analogio` --- Analog hardware support
//| =================================================
//|
//| .. module:: analogio
//| :synopsis: Analog hardware support
//| :platform: SAMD21, ESP8266
//|
//| The `analogio` module contains classes to provide access to analog IO
//| typically implemented with digital-to-analog (DAC) and analog-to-digital
//| (ADC) converters.
//|
//| Libraries
//|
//| .. toctree::
//| :maxdepth: 3
//|
//| AnalogIn
//| AnalogOut
//|
//| All libraries change hardware state and should be deinitialized when they
//| are no longer needed. To do so, either call :py:meth:`!deinit` or use a
//| context manager.
//|
//| For example::
//|
//| import analogio
//| from board import *
//|
//| with analogio.AnalogIn(A0) as pin:
//| print(pin.value)
//|
//| This example will initialize the the device, read
//| :py:data:`~analogio.AnalogIn.value` and then
//| :py:meth:`~analogio.AnalogIn.deinit` the hardware.
//|
STATIC const mp_rom_map_elem_t analogio_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_analogio) },
{ MP_ROM_QSTR(MP_QSTR_AnalogIn), MP_ROM_PTR(&analogio_analogin_type) },
{ MP_ROM_QSTR(MP_QSTR_AnalogOut), MP_ROM_PTR(&analogio_analogout_type) },
};
STATIC MP_DEFINE_CONST_DICT(analogio_module_globals, analogio_module_globals_table);
const mp_obj_module_t analogio_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&analogio_module_globals,
};

View File

@ -24,11 +24,11 @@
* THE SOFTWARE.
*/
#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO___INIT___H__
#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO___INIT___H__
#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGIO___INIT___H__
#define __MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGIO___INIT___H__
#include "py/obj.h"
// Nothing now.
#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO___INIT___H__
#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_ANALOGIO___INIT___H__

View File

@ -24,8 +24,8 @@
* THE SOFTWARE.
*/
#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ONEWIRE_H__
#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ONEWIRE_H__
#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_BITBANGIO_ONEWIRE_H__
#define __MICROPY_INCLUDED_SHARED_BINDINGS_BITBANGIO_ONEWIRE_H__
#include "common-hal/microcontroller/types.h"
#include "shared-module/bitbangio/types.h"
@ -39,4 +39,4 @@ 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);
#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ONEWIRE_H__
#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_BITBANGIO_ONEWIRE_H__

View File

@ -25,7 +25,7 @@
*/
// bitbangio implements some standard protocols in the processor. Its only
// dependency is nativeio.DigitalInOut.
// dependency is digitalio.
#include <stdint.h>
@ -45,12 +45,13 @@
//|
//| .. module:: bitbangio
//| :synopsis: Digital protocols implemented by the CPU
//| :platform: SAMD21
//| :platform: SAMD21, ESP8266
//|
//| The `bitbangio` module contains classes to provide digital protocol support
//| regardless of whether the underlying hardware exists to use the protocol.
//| The `bitbangio` module contains classes to provide digital bus protocol
//| support regardless of whether the underlying hardware exists to use the
//| protocol.
//|
//| First try to use `nativeio` module instead which may utilize peripheral
//| First try to use `busio` module instead which may utilize peripheral
//| hardware to implement the protocols. Native implementations will be faster
//| than bitbanged versions and have more capabilities.
//|

View File

@ -25,14 +25,14 @@
*/
// This file contains all of the Python API definitions for the
// nativeio.I2C class.
// busio.I2C class.
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/nativeio/I2C.h"
#include "shared-bindings/busio/I2C.h"
#include "lib/utils/context_manager_helpers.h"
#include "py/runtime.h"
//| .. currentmodule:: nativeio
//| .. currentmodule:: busio
//|
//| :class:`I2C` --- Two wire serial protocol
//| ------------------------------------------
@ -55,10 +55,10 @@
//| :param ~microcontroller.Pin sda: The data pin
//| :param int frequency: The clock frequency in Hertz
//|
STATIC mp_obj_t nativeio_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 busio_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);
nativeio_i2c_obj_t *self = m_new_obj(nativeio_i2c_obj_t);
self->base.type = &nativeio_i2c_type;
busio_i2c_obj_t *self = m_new_obj(busio_i2c_obj_t);
self->base.type = &busio_i2c_type;
mp_map_t kw_args;
mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args);
enum { ARG_scl, ARG_sda, ARG_frequency };
@ -75,7 +75,7 @@ STATIC mp_obj_t nativeio_i2c_make_new(const mp_obj_type_t *type, size_t n_args,
assert_pin_free(scl);
const mcu_pin_obj_t* sda = MP_OBJ_TO_PTR(args[ARG_sda].u_obj);
assert_pin_free(sda);
common_hal_nativeio_i2c_construct(self, scl, sda, args[ARG_frequency].u_int);
common_hal_busio_i2c_construct(self, scl, sda, args[ARG_frequency].u_int);
return (mp_obj_t)self;
}
@ -83,12 +83,12 @@ STATIC mp_obj_t nativeio_i2c_make_new(const mp_obj_type_t *type, size_t n_args,
//|
//| Releases control of the underlying hardware so other classes can use it.
//|
STATIC mp_obj_t nativeio_i2c_obj_deinit(mp_obj_t self_in) {
nativeio_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_nativeio_i2c_deinit(self);
STATIC mp_obj_t busio_i2c_obj_deinit(mp_obj_t self_in) {
busio_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_busio_i2c_deinit(self);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(nativeio_i2c_deinit_obj, nativeio_i2c_obj_deinit);
MP_DEFINE_CONST_FUN_OBJ_1(busio_i2c_deinit_obj, busio_i2c_obj_deinit);
//| .. method:: I2C.__enter__()
//|
@ -100,15 +100,15 @@ MP_DEFINE_CONST_FUN_OBJ_1(nativeio_i2c_deinit_obj, nativeio_i2c_obj_deinit);
//|
//| Automatically deinitializes the hardware on context exit.
//|
STATIC mp_obj_t nativeio_i2c_obj___exit__(size_t n_args, const mp_obj_t *args) {
STATIC mp_obj_t busio_i2c_obj___exit__(size_t n_args, const mp_obj_t *args) {
(void)n_args;
common_hal_nativeio_i2c_deinit(args[0]);
common_hal_busio_i2c_deinit(args[0]);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_i2c___exit___obj, 4, 4, nativeio_i2c_obj___exit__);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(busio_i2c___exit___obj, 4, 4, busio_i2c_obj___exit__);
static void check_lock(nativeio_i2c_obj_t *self) {
if (!common_hal_nativeio_i2c_has_lock(self)) {
static void check_lock(busio_i2c_obj_t *self) {
if (!common_hal_busio_i2c_has_lock(self)) {
mp_raise_RuntimeError("Function requires lock.");
}
}
@ -121,20 +121,20 @@ static void check_lock(nativeio_i2c_obj_t *self) {
//| :return: List of device ids on the I2C bus
//| :rtype: list
//|
STATIC mp_obj_t nativeio_i2c_scan(mp_obj_t self_in) {
nativeio_i2c_obj_t *self = MP_OBJ_TO_PTR(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);
check_lock(self);
mp_obj_t list = mp_obj_new_list(0, NULL);
// 7-bit addresses 0b0000xxx and 0b1111xxx are reserved
for (int addr = 0x08; addr < 0x78; ++addr) {
bool success = common_hal_nativeio_i2c_probe(self, addr);
bool success = common_hal_busio_i2c_probe(self, addr);
if (success) {
mp_obj_list_append(list, MP_OBJ_NEW_SMALL_INT(addr));
}
}
return list;
}
MP_DEFINE_CONST_FUN_OBJ_1(nativeio_i2c_scan_obj, nativeio_i2c_scan);
MP_DEFINE_CONST_FUN_OBJ_1(busio_i2c_scan_obj, busio_i2c_scan);
//| .. method:: I2C.try_lock()
//|
@ -143,20 +143,20 @@ MP_DEFINE_CONST_FUN_OBJ_1(nativeio_i2c_scan_obj, nativeio_i2c_scan);
//| :return: True when lock has been grabbed
//| :rtype: bool
//|
STATIC mp_obj_t nativeio_i2c_obj_try_lock(mp_obj_t self_in) {
return mp_obj_new_bool(common_hal_nativeio_i2c_try_lock(MP_OBJ_TO_PTR(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)));
}
MP_DEFINE_CONST_FUN_OBJ_1(nativeio_i2c_try_lock_obj, nativeio_i2c_obj_try_lock);
MP_DEFINE_CONST_FUN_OBJ_1(busio_i2c_try_lock_obj, busio_i2c_obj_try_lock);
//| .. method:: I2C.unlock()
//|
//| Releases the I2C lock.
//|
STATIC mp_obj_t nativeio_i2c_obj_unlock(mp_obj_t self_in) {
common_hal_nativeio_i2c_unlock(MP_OBJ_TO_PTR(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));
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(nativeio_i2c_unlock_obj, nativeio_i2c_obj_unlock);
MP_DEFINE_CONST_FUN_OBJ_1(busio_i2c_unlock_obj, busio_i2c_obj_unlock);
//| .. method:: I2C.readfrom_into(address, buffer, \*, start=0, end=len(buffer))
//|
@ -172,7 +172,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(nativeio_i2c_unlock_obj, nativeio_i2c_obj_unlock);
//| :param int start: Index to start writing at
//| :param int end: Index to write up to but not include
//|
STATIC mp_obj_t nativeio_i2c_readfrom_into(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
STATIC mp_obj_t busio_i2c_readfrom_into(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_address, ARG_buffer, ARG_start, ARG_end };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_address, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} },
@ -180,7 +180,7 @@ STATIC mp_obj_t nativeio_i2c_readfrom_into(size_t n_args, const mp_obj_t *pos_ar
{ MP_QSTR_start, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
{ MP_QSTR_end, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = INT_MAX} },
};
nativeio_i2c_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
busio_i2c_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
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);
@ -198,14 +198,14 @@ STATIC mp_obj_t nativeio_i2c_readfrom_into(size_t n_args, const mp_obj_t *pos_ar
} else if (len > bufinfo.len) {
len = bufinfo.len;
}
uint8_t status = common_hal_nativeio_i2c_read(self, args[ARG_address].u_int, ((uint8_t*)bufinfo.buf) + start, len);
uint8_t status = common_hal_busio_i2c_read(self, args[ARG_address].u_int, ((uint8_t*)bufinfo.buf) + start, len);
if (status != 0) {
mp_raise_OSError(status);
}
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_KW(nativeio_i2c_readfrom_into_obj, 3, nativeio_i2c_readfrom_into);
MP_DEFINE_CONST_FUN_OBJ_KW(busio_i2c_readfrom_into_obj, 3, busio_i2c_readfrom_into);
//| .. method:: I2C.writeto(address, buffer, \*, start=0, end=len(buffer), stop=True)
//|
@ -223,7 +223,7 @@ MP_DEFINE_CONST_FUN_OBJ_KW(nativeio_i2c_readfrom_into_obj, 3, nativeio_i2c_readf
//| :param bool stop: If true, output an I2C stop condition after the
//| buffer is written
//|
STATIC mp_obj_t nativeio_i2c_writeto(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
STATIC mp_obj_t busio_i2c_writeto(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_address, ARG_buffer, ARG_start, ARG_end, ARG_stop };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_address, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} },
@ -232,7 +232,7 @@ STATIC mp_obj_t nativeio_i2c_writeto(size_t n_args, const mp_obj_t *pos_args, mp
{ MP_QSTR_end, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = INT_MAX} },
{ MP_QSTR_stop, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} },
};
nativeio_i2c_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
busio_i2c_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
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);
@ -254,33 +254,33 @@ STATIC mp_obj_t nativeio_i2c_writeto(size_t n_args, const mp_obj_t *pos_args, mp
}
// do the transfer
uint8_t status = common_hal_nativeio_i2c_write(self, args[ARG_address].u_int,
uint8_t status = common_hal_busio_i2c_write(self, args[ARG_address].u_int,
((uint8_t*) bufinfo.buf) + start, len, args[ARG_stop].u_bool);
if (status != 0) {
mp_raise_OSError(status);
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(nativeio_i2c_writeto_obj, 1, nativeio_i2c_writeto);
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(busio_i2c_writeto_obj, 1, busio_i2c_writeto);
STATIC const mp_rom_map_elem_t nativeio_i2c_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_i2c_deinit_obj) },
STATIC const mp_rom_map_elem_t busio_i2c_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&busio_i2c_deinit_obj) },
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_i2c___exit___obj) },
{ MP_ROM_QSTR(MP_QSTR_scan), MP_ROM_PTR(&nativeio_i2c_scan_obj) },
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&busio_i2c___exit___obj) },
{ MP_ROM_QSTR(MP_QSTR_scan), MP_ROM_PTR(&busio_i2c_scan_obj) },
{ MP_ROM_QSTR(MP_QSTR_try_lock), MP_ROM_PTR(&nativeio_i2c_try_lock_obj) },
{ MP_ROM_QSTR(MP_QSTR_unlock), MP_ROM_PTR(&nativeio_i2c_unlock_obj) },
{ MP_ROM_QSTR(MP_QSTR_try_lock), MP_ROM_PTR(&busio_i2c_try_lock_obj) },
{ MP_ROM_QSTR(MP_QSTR_unlock), MP_ROM_PTR(&busio_i2c_unlock_obj) },
{ MP_ROM_QSTR(MP_QSTR_readfrom_into), MP_ROM_PTR(&nativeio_i2c_readfrom_into_obj) },
{ MP_ROM_QSTR(MP_QSTR_writeto), MP_ROM_PTR(&nativeio_i2c_writeto_obj) },
{ MP_ROM_QSTR(MP_QSTR_readfrom_into), MP_ROM_PTR(&busio_i2c_readfrom_into_obj) },
{ MP_ROM_QSTR(MP_QSTR_writeto), MP_ROM_PTR(&busio_i2c_writeto_obj) },
};
STATIC MP_DEFINE_CONST_DICT(nativeio_i2c_locals_dict, nativeio_i2c_locals_dict_table);
STATIC MP_DEFINE_CONST_DICT(busio_i2c_locals_dict, busio_i2c_locals_dict_table);
const mp_obj_type_t nativeio_i2c_type = {
const mp_obj_type_t busio_i2c_type = {
{ &mp_type_type },
.name = MP_QSTR_I2C,
.make_new = nativeio_i2c_make_new,
.locals_dict = (mp_obj_dict_t*)&nativeio_i2c_locals_dict,
.make_new = busio_i2c_make_new,
.locals_dict = (mp_obj_dict_t*)&busio_i2c_locals_dict,
};

View File

@ -31,40 +31,40 @@
// This file includes externs for all functions a port should implement to
// support the machine module.
#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_I2C_H__
#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_I2C_H__
#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO_I2C_H__
#define __MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO_I2C_H__
#include "py/obj.h"
#include "common-hal/microcontroller/types.h"
#include "common-hal/nativeio/types.h"
#include "common-hal/busio/I2C.h"
// Type object used in Python. Should be shared between ports.
extern const mp_obj_type_t nativeio_i2c_type;
extern const mp_obj_type_t busio_i2c_type;
// Initializes the hardware peripheral.
extern void common_hal_nativeio_i2c_construct(nativeio_i2c_obj_t *self,
extern void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
const mcu_pin_obj_t * scl,
const mcu_pin_obj_t * sda,
uint32_t frequency);
extern void common_hal_nativeio_i2c_deinit(nativeio_i2c_obj_t *self);
extern void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self);
extern bool common_hal_nativeio_i2c_try_lock(nativeio_i2c_obj_t *self);
extern bool common_hal_nativeio_i2c_has_lock(nativeio_i2c_obj_t *self);
extern void common_hal_nativeio_i2c_unlock(nativeio_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 void common_hal_busio_i2c_unlock(busio_i2c_obj_t *self);
// Probe the bus to see if a device acknowledges the given address.
extern bool common_hal_nativeio_i2c_probe(nativeio_i2c_obj_t *self, uint8_t addr);
extern bool common_hal_busio_i2c_probe(busio_i2c_obj_t *self, uint8_t addr);
// Write to the device and return 0 on success or an appropriate error code from mperrno.h
extern uint8_t common_hal_nativeio_i2c_write(nativeio_i2c_obj_t *self, uint16_t address,
extern uint8_t common_hal_busio_i2c_write(busio_i2c_obj_t *self, uint16_t address,
const uint8_t * data, size_t len,
bool stop);
// Reads memory of the i2c device picking up where it left off and return 0 on
// success or an appropriate error code from mperrno.h
extern uint8_t common_hal_nativeio_i2c_read(nativeio_i2c_obj_t *self, uint16_t address,
extern uint8_t common_hal_busio_i2c_read(busio_i2c_obj_t *self, uint16_t address,
uint8_t * data, size_t len);
#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_I2C_H__
#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO_I2C_H__

View File

@ -31,14 +31,14 @@
#include "py/runtime.h"
#include "py/runtime0.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/nativeio/OneWire.h"
#include "shared-bindings/busio/OneWire.h"
//| .. currentmodule:: nativeio
//| .. currentmodule:: busio
//|
//| :class:`OneWire` -- Lowest-level of the Maxim OneWire protocol
//| =================================================================
//|
//| :class:`~nativeio.OneWire` implements the timing-sensitive foundation of the Maxim
//| :class:`~busio.OneWire` implements the timing-sensitive foundation of the Maxim
//| (formerly Dallas Semi) OneWire protocol.
//|
//| Protocol definition is here: https://www.maximintegrated.com/en/app-notes/index.mvp/id/126
@ -52,16 +52,16 @@
//|
//| Read a short series of pulses::
//|
//| import nativeio
//| import busio
//| import board
//|
//| with nativeio.OneWire(board.D7) as onewire:
//| with busio.OneWire(board.D7) as onewire:
//| onewire.reset()
//| onewire.write_bit(True)
//| onewire.write_bit(False)
//| print(onewire.read_bit())
//|
STATIC mp_obj_t nativeio_onewire_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 busio_onewire_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, 1, MP_OBJ_FUN_ARGS_MAX, true);
mp_map_t kw_args;
mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args);
@ -75,10 +75,10 @@ STATIC mp_obj_t nativeio_onewire_make_new(const mp_obj_type_t *type, size_t n_ar
const mcu_pin_obj_t* pin = MP_OBJ_TO_PTR(args[ARG_pin].u_obj);
assert_pin_free(pin);
nativeio_onewire_obj_t *self = m_new_obj(nativeio_onewire_obj_t);
self->base.type = &nativeio_onewire_type;
busio_onewire_obj_t *self = m_new_obj(busio_onewire_obj_t);
self->base.type = &busio_onewire_type;
common_hal_nativeio_onewire_construct(self, pin);
common_hal_busio_onewire_construct(self, pin);
return MP_OBJ_FROM_PTR(self);
}
@ -86,12 +86,12 @@ STATIC mp_obj_t nativeio_onewire_make_new(const mp_obj_type_t *type, size_t n_ar
//|
//| Deinitialize the OneWire bus and release any hardware resources for reuse.
//|
STATIC mp_obj_t nativeio_onewire_deinit(mp_obj_t self_in) {
nativeio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_nativeio_onewire_deinit(self);
STATIC mp_obj_t busio_onewire_deinit(mp_obj_t self_in) {
busio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_busio_onewire_deinit(self);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_onewire_deinit_obj, nativeio_onewire_deinit);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(busio_onewire_deinit_obj, busio_onewire_deinit);
//| .. method:: __enter__()
//|
@ -103,12 +103,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_onewire_deinit_obj, nativeio_onewire_d
//|
//| Automatically deinitializes the hardware when exiting a context.
//|
STATIC mp_obj_t nativeio_onewire_obj___exit__(size_t n_args, const mp_obj_t *args) {
STATIC mp_obj_t busio_onewire_obj___exit__(size_t n_args, const mp_obj_t *args) {
(void)n_args;
common_hal_nativeio_onewire_deinit(args[0]);
common_hal_busio_onewire_deinit(args[0]);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_onewire___exit___obj, 4, 4, nativeio_onewire_obj___exit__);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(busio_onewire___exit___obj, 4, 4, busio_onewire_obj___exit__);
//| .. method:: reset()
//|
@ -117,12 +117,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_onewire___exit___obj, 4, 4,
//| :returns: False when at least one device is present
//| :rtype: bool
//|
STATIC mp_obj_t nativeio_onewire_obj_reset(mp_obj_t self_in) {
nativeio_onewire_obj_t *self = MP_OBJ_TO_PTR(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);
return mp_obj_new_bool(common_hal_nativeio_onewire_reset(self));
return mp_obj_new_bool(common_hal_busio_onewire_reset(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(nativeio_onewire_reset_obj, nativeio_onewire_obj_reset);
MP_DEFINE_CONST_FUN_OBJ_1(busio_onewire_reset_obj, busio_onewire_obj_reset);
//| .. method:: read_bit()
//|
@ -131,39 +131,39 @@ MP_DEFINE_CONST_FUN_OBJ_1(nativeio_onewire_reset_obj, nativeio_onewire_obj_reset
//| :returns: bit state read
//| :rtype: bool
//|
STATIC mp_obj_t nativeio_onewire_obj_read_bit(mp_obj_t self_in) {
nativeio_onewire_obj_t *self = MP_OBJ_TO_PTR(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);
return mp_obj_new_bool(common_hal_nativeio_onewire_read_bit(self));
return mp_obj_new_bool(common_hal_busio_onewire_read_bit(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(nativeio_onewire_read_bit_obj, nativeio_onewire_obj_read_bit);
MP_DEFINE_CONST_FUN_OBJ_1(busio_onewire_read_bit_obj, busio_onewire_obj_read_bit);
//| .. method:: write_bit(value)
//|
//| Write out a bit based on value.
//|
STATIC mp_obj_t nativeio_onewire_obj_write_bit(mp_obj_t self_in, mp_obj_t bool_obj) {
nativeio_onewire_obj_t *self = MP_OBJ_TO_PTR(self_in);
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);
common_hal_nativeio_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;
}
MP_DEFINE_CONST_FUN_OBJ_2(nativeio_onewire_write_bit_obj, nativeio_onewire_obj_write_bit);
MP_DEFINE_CONST_FUN_OBJ_2(busio_onewire_write_bit_obj, busio_onewire_obj_write_bit);
STATIC const mp_rom_map_elem_t nativeio_onewire_locals_dict_table[] = {
STATIC const mp_rom_map_elem_t busio_onewire_locals_dict_table[] = {
// Methods
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_onewire_deinit_obj) },
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&busio_onewire_deinit_obj) },
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_onewire___exit___obj) },
{ MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&nativeio_onewire_reset_obj) },
{ MP_ROM_QSTR(MP_QSTR_read_bit), MP_ROM_PTR(&nativeio_onewire_read_bit_obj) },
{ MP_ROM_QSTR(MP_QSTR_write_bit), MP_ROM_PTR(&nativeio_onewire_write_bit_obj) },
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&busio_onewire___exit___obj) },
{ MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&busio_onewire_reset_obj) },
{ MP_ROM_QSTR(MP_QSTR_read_bit), MP_ROM_PTR(&busio_onewire_read_bit_obj) },
{ MP_ROM_QSTR(MP_QSTR_write_bit), MP_ROM_PTR(&busio_onewire_write_bit_obj) },
};
STATIC MP_DEFINE_CONST_DICT(nativeio_onewire_locals_dict, nativeio_onewire_locals_dict_table);
STATIC MP_DEFINE_CONST_DICT(busio_onewire_locals_dict, busio_onewire_locals_dict_table);
const mp_obj_type_t nativeio_onewire_type = {
const mp_obj_type_t busio_onewire_type = {
{ &mp_type_type },
.name = MP_QSTR_OneWire,
.make_new = nativeio_onewire_make_new,
.locals_dict = (mp_obj_dict_t*)&nativeio_onewire_locals_dict,
.make_new = busio_onewire_make_new,
.locals_dict = (mp_obj_dict_t*)&busio_onewire_locals_dict,
};

View File

@ -24,19 +24,19 @@
* THE SOFTWARE.
*/
#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ONEWIRE_H__
#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ONEWIRE_H__
#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO_ONEWIRE_H__
#define __MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO_ONEWIRE_H__
#include "common-hal/microcontroller/types.h"
#include "common-hal/nativeio/types.h"
#include "common-hal/busio/OneWire.h"
extern const mp_obj_type_t nativeio_onewire_type;
extern const mp_obj_type_t busio_onewire_type;
extern void common_hal_nativeio_onewire_construct(nativeio_onewire_obj_t* self,
extern void common_hal_busio_onewire_construct(busio_onewire_obj_t* self,
const mcu_pin_obj_t* pin);
extern void common_hal_nativeio_onewire_deinit(nativeio_onewire_obj_t* self);
extern bool common_hal_nativeio_onewire_reset(nativeio_onewire_obj_t* self);
extern bool common_hal_nativeio_onewire_read_bit(nativeio_onewire_obj_t* self);
extern void common_hal_nativeio_onewire_write_bit(nativeio_onewire_obj_t* self, bool bit);
extern void common_hal_busio_onewire_deinit(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);
#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_ONEWIRE_H__
#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO_ONEWIRE_H__

View File

@ -25,25 +25,25 @@
*/
// This file contains all of the Python API definitions for the
// nativeio.SPI class.
// busio.SPI class.
#include <string.h>
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/nativeio/SPI.h"
#include "shared-bindings/busio/SPI.h"
#include "lib/utils/context_manager_helpers.h"
#include "py/mperrno.h"
#include "py/nlr.h"
#include "py/runtime.h"
//| .. currentmodule:: nativeio
//| .. currentmodule:: busio
//|
//| :class:`SPI` -- a 3-4 wire serial protocol
//| -----------------------------------------------
//|
//| SPI is a serial protocol that has exclusive pins for data in and out of the
//| master. It is typically faster than :py:class:`~nativeio.I2C` because a
//| master. It is typically faster than :py:class:`~busio.I2C` because a
//| separate pin is used to control the active slave rather than a transitted
//| address. This class only manages three of the four SPI lines: `!clock`,
//| `!MOSI`, `!MISO`. Its up to the client to manage the appropriate slave
@ -68,10 +68,10 @@
//|
// TODO(tannewt): Support LSB SPI.
STATIC mp_obj_t nativeio_spi_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 busio_spi_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);
nativeio_spi_obj_t *self = m_new_obj(nativeio_spi_obj_t);
self->base.type = &nativeio_spi_type;
busio_spi_obj_t *self = m_new_obj(busio_spi_obj_t);
self->base.type = &busio_spi_type;
mp_map_t kw_args;
mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args);
enum { ARG_clock, ARG_MOSI, ARG_MISO };
@ -91,7 +91,7 @@ STATIC mp_obj_t nativeio_spi_make_new(const mp_obj_type_t *type, size_t n_args,
assert_pin_free(mosi);
const mcu_pin_obj_t* miso = MP_OBJ_TO_PTR(args[ARG_MISO].u_obj);
assert_pin_free(miso);
common_hal_nativeio_spi_construct(self, clock, mosi, miso);
common_hal_busio_spi_construct(self, clock, mosi, miso);
return (mp_obj_t)self;
}
@ -99,12 +99,12 @@ STATIC mp_obj_t nativeio_spi_make_new(const mp_obj_type_t *type, size_t n_args,
//|
//| Turn off the SPI bus.
//|
STATIC mp_obj_t nativeio_spi_obj_deinit(mp_obj_t self_in) {
nativeio_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_nativeio_spi_deinit(self);
STATIC mp_obj_t busio_spi_obj_deinit(mp_obj_t self_in) {
busio_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_busio_spi_deinit(self);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(nativeio_spi_deinit_obj, nativeio_spi_obj_deinit);
MP_DEFINE_CONST_FUN_OBJ_1(busio_spi_deinit_obj, busio_spi_obj_deinit);
//| .. method:: SPI.__enter__()
//|
@ -116,15 +116,15 @@ MP_DEFINE_CONST_FUN_OBJ_1(nativeio_spi_deinit_obj, nativeio_spi_obj_deinit);
//|
//| Automatically deinitializes the hardware when exiting a context.
//|
STATIC mp_obj_t nativeio_spi_obj___exit__(size_t n_args, const mp_obj_t *args) {
STATIC mp_obj_t busio_spi_obj___exit__(size_t n_args, const mp_obj_t *args) {
(void)n_args;
common_hal_nativeio_spi_deinit(args[0]);
common_hal_busio_spi_deinit(args[0]);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_spi_obj___exit___obj, 4, 4, nativeio_spi_obj___exit__);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(busio_spi_obj___exit___obj, 4, 4, busio_spi_obj___exit__);
static void check_lock(nativeio_spi_obj_t *self) {
if (!common_hal_nativeio_spi_has_lock(self)) {
static void check_lock(busio_spi_obj_t *self) {
if (!common_hal_busio_spi_has_lock(self)) {
mp_raise_RuntimeError("Function requires lock");
}
}
@ -139,7 +139,7 @@ static void check_lock(nativeio_spi_obj_t *self) {
//| or second (1). Rising or falling depends on clock polarity.
//| :param int bits: the number of bits per word
//|
STATIC mp_obj_t nativeio_spi_configure(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
STATIC mp_obj_t busio_spi_configure(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_baudrate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 100000} },
@ -147,7 +147,7 @@ STATIC mp_obj_t nativeio_spi_configure(size_t n_args, const mp_obj_t *pos_args,
{ MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
{ MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
};
nativeio_spi_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
busio_spi_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
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);
@ -165,13 +165,13 @@ STATIC mp_obj_t nativeio_spi_configure(size_t n_args, const mp_obj_t *pos_args,
mp_raise_ValueError("Invalid number of bits");
}
if (!common_hal_nativeio_spi_configure(self, args[ARG_baudrate].u_int,
if (!common_hal_busio_spi_configure(self, args[ARG_baudrate].u_int,
polarity, phase, bits)) {
mp_raise_OSError(MP_EIO);
}
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_KW(nativeio_spi_configure_obj, 1, nativeio_spi_configure);
MP_DEFINE_CONST_FUN_OBJ_KW(busio_spi_configure_obj, 1, busio_spi_configure);
//| .. method:: SPI.try_lock()
//|
@ -180,20 +180,20 @@ MP_DEFINE_CONST_FUN_OBJ_KW(nativeio_spi_configure_obj, 1, nativeio_spi_configure
//| :return: True when lock has been grabbed
//| :rtype: bool
//|
STATIC mp_obj_t nativeio_spi_obj_try_lock(mp_obj_t self_in) {
return mp_obj_new_bool(common_hal_nativeio_spi_try_lock(MP_OBJ_TO_PTR(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)));
}
MP_DEFINE_CONST_FUN_OBJ_1(nativeio_spi_try_lock_obj, nativeio_spi_obj_try_lock);
MP_DEFINE_CONST_FUN_OBJ_1(busio_spi_try_lock_obj, busio_spi_obj_try_lock);
//| .. method:: SPI.unlock()
//|
//| Releases the SPI lock.
//|
STATIC mp_obj_t nativeio_spi_obj_unlock(mp_obj_t self_in) {
common_hal_nativeio_spi_unlock(MP_OBJ_TO_PTR(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));
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(nativeio_spi_unlock_obj, nativeio_spi_obj_unlock);
MP_DEFINE_CONST_FUN_OBJ_1(busio_spi_unlock_obj, busio_spi_obj_unlock);
//| .. method:: SPI.write(buffer, \*, start=0, end=len(buffer))
//|
@ -203,14 +203,14 @@ MP_DEFINE_CONST_FUN_OBJ_1(nativeio_spi_unlock_obj, nativeio_spi_obj_unlock);
//| :param int start: Index to start writing from
//| :param int end: Index to read up to but not include
//|
STATIC mp_obj_t nativeio_spi_write(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
STATIC mp_obj_t busio_spi_write(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_buffer, ARG_start, ARG_end };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_start, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
{ MP_QSTR_end, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = INT_MAX} },
};
nativeio_spi_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
busio_spi_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
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,13 +229,13 @@ STATIC mp_obj_t nativeio_spi_write(size_t n_args, const mp_obj_t *pos_args, mp_m
len = bufinfo.len;
}
bool ok = common_hal_nativeio_spi_write(self, ((uint8_t*)bufinfo.buf) + start, len);
bool ok = common_hal_busio_spi_write(self, ((uint8_t*)bufinfo.buf) + start, len);
if (!ok) {
mp_raise_OSError(MP_EIO);
}
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_KW(nativeio_spi_write_obj, 2, nativeio_spi_write);
MP_DEFINE_CONST_FUN_OBJ_KW(busio_spi_write_obj, 2, busio_spi_write);
//| .. method:: SPI.readinto(buffer, \*, start=0, end=len(buffer), write_value=0)
@ -247,7 +247,7 @@ MP_DEFINE_CONST_FUN_OBJ_KW(nativeio_spi_write_obj, 2, nativeio_spi_write);
//| :param int end: Index to write up to but not include
//| :param int write_value: Value to write reading. (Usually ignored.)
//|
STATIC mp_obj_t nativeio_spi_readinto(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
STATIC mp_obj_t busio_spi_readinto(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_buffer, ARG_start, ARG_end, ARG_write_value };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
@ -255,7 +255,7 @@ STATIC mp_obj_t nativeio_spi_readinto(size_t n_args, const mp_obj_t *pos_args, m
{ MP_QSTR_end, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = INT_MAX} },
{ MP_QSTR_write_value,MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
};
nativeio_spi_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
busio_spi_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
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);
@ -274,31 +274,31 @@ STATIC mp_obj_t nativeio_spi_readinto(size_t n_args, const mp_obj_t *pos_args, m
len = bufinfo.len;
}
bool ok = common_hal_nativeio_spi_read(self, ((uint8_t*)bufinfo.buf) + start, len, args[ARG_write_value].u_int);
bool ok = common_hal_busio_spi_read(self, ((uint8_t*)bufinfo.buf) + start, len, args[ARG_write_value].u_int);
if (!ok) {
mp_raise_OSError(MP_EIO);
}
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_KW(nativeio_spi_readinto_obj, 2, nativeio_spi_readinto);
MP_DEFINE_CONST_FUN_OBJ_KW(busio_spi_readinto_obj, 2, busio_spi_readinto);
STATIC const mp_rom_map_elem_t nativeio_spi_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_spi_deinit_obj) },
STATIC const mp_rom_map_elem_t busio_spi_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&busio_spi_deinit_obj) },
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_spi_obj___exit___obj) },
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&busio_spi_obj___exit___obj) },
{ MP_ROM_QSTR(MP_QSTR_configure), MP_ROM_PTR(&nativeio_spi_configure_obj) },
{ MP_ROM_QSTR(MP_QSTR_try_lock), MP_ROM_PTR(&nativeio_spi_try_lock_obj) },
{ MP_ROM_QSTR(MP_QSTR_unlock), MP_ROM_PTR(&nativeio_spi_unlock_obj) },
{ MP_ROM_QSTR(MP_QSTR_configure), MP_ROM_PTR(&busio_spi_configure_obj) },
{ MP_ROM_QSTR(MP_QSTR_try_lock), MP_ROM_PTR(&busio_spi_try_lock_obj) },
{ MP_ROM_QSTR(MP_QSTR_unlock), MP_ROM_PTR(&busio_spi_unlock_obj) },
{ MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&nativeio_spi_readinto_obj) },
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&nativeio_spi_write_obj) },
{ MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&busio_spi_readinto_obj) },
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&busio_spi_write_obj) },
};
STATIC MP_DEFINE_CONST_DICT(nativeio_spi_locals_dict, nativeio_spi_locals_dict_table);
STATIC MP_DEFINE_CONST_DICT(busio_spi_locals_dict, busio_spi_locals_dict_table);
const mp_obj_type_t nativeio_spi_type = {
const mp_obj_type_t busio_spi_type = {
{ &mp_type_type },
.name = MP_QSTR_SPI,
.make_new = nativeio_spi_make_new,
.locals_dict = (mp_obj_dict_t*)&nativeio_spi_locals_dict,
.make_new = busio_spi_make_new,
.locals_dict = (mp_obj_dict_t*)&busio_spi_locals_dict,
};

View File

@ -24,34 +24,34 @@
* THE SOFTWARE.
*/
#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_SPI_H__
#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_SPI_H__
#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO_SPI_H__
#define __MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO_SPI_H__
#include "py/obj.h"
#include "common-hal/microcontroller/types.h"
#include "common-hal/nativeio/types.h"
#include "common-hal/busio/SPI.h"
// Type object used in Python. Should be shared between ports.
extern const mp_obj_type_t nativeio_spi_type;
extern const mp_obj_type_t busio_spi_type;
// Construct an underlying SPI object.
extern void common_hal_nativeio_spi_construct(nativeio_spi_obj_t *self,
extern void common_hal_busio_spi_construct(busio_spi_obj_t *self,
const mcu_pin_obj_t * clock, const mcu_pin_obj_t * mosi,
const mcu_pin_obj_t * miso);
extern void common_hal_nativeio_spi_deinit(nativeio_spi_obj_t *self);
extern void common_hal_busio_spi_deinit(busio_spi_obj_t *self);
extern bool common_hal_nativeio_spi_configure(nativeio_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);
extern bool common_hal_nativeio_spi_try_lock(nativeio_spi_obj_t *self);
extern bool common_hal_nativeio_spi_has_lock(nativeio_spi_obj_t *self);
extern void common_hal_nativeio_spi_unlock(nativeio_spi_obj_t *self);
extern bool common_hal_busio_spi_try_lock(busio_spi_obj_t *self);
extern bool common_hal_busio_spi_has_lock(busio_spi_obj_t *self);
extern void common_hal_busio_spi_unlock(busio_spi_obj_t *self);
// Writes out the given data.
extern bool common_hal_nativeio_spi_write(nativeio_spi_obj_t *self, const uint8_t *data, size_t len);
extern bool common_hal_busio_spi_write(busio_spi_obj_t *self, const uint8_t *data, size_t len);
// Reads in len bytes while outputting zeroes.
extern bool common_hal_nativeio_spi_read(nativeio_spi_obj_t *self, uint8_t *data, size_t len, uint8_t write_value);
extern bool common_hal_busio_spi_read(busio_spi_obj_t *self, uint8_t *data, size_t len, uint8_t write_value);
#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_SPI_H__
#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO_SPI_H__

View File

@ -26,7 +26,7 @@
#include <stdint.h>
#include "shared-bindings/nativeio/UART.h"
#include "shared-bindings/busio/UART.h"
#include "lib/utils/context_manager_helpers.h"
@ -36,7 +36,7 @@
#include "shared-bindings/microcontroller/Pin.h"
//| .. currentmodule:: nativeio
//| .. currentmodule:: busio
//|
//| :class:`UART` -- a bidirectional serial protocol
//| =================================================
@ -58,14 +58,14 @@
//|
typedef struct {
mp_obj_base_t base;
} nativeio_uart_parity_obj_t;
extern const nativeio_uart_parity_obj_t nativeio_uart_parity_even_obj;
extern const nativeio_uart_parity_obj_t nativeio_uart_parity_odd_obj;
} busio_uart_parity_obj_t;
extern const busio_uart_parity_obj_t busio_uart_parity_even_obj;
extern const busio_uart_parity_obj_t busio_uart_parity_odd_obj;
STATIC mp_obj_t nativeio_uart_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 busio_uart_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);
nativeio_uart_obj_t *self = m_new_obj(nativeio_uart_obj_t);
self->base.type = &nativeio_uart_type;
busio_uart_obj_t *self = m_new_obj(busio_uart_obj_t);
self->base.type = &busio_uart_type;
mp_map_t kw_args;
mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args);
enum { ARG_tx, ARG_rx, ARG_baudrate, ARG_bits, ARG_parity, ARG_stop, ARG_timeout, ARG_receiver_buffer_size};
@ -96,9 +96,9 @@ STATIC mp_obj_t nativeio_uart_make_new(const mp_obj_type_t *type, size_t n_args,
}
uart_parity_t parity = PARITY_NONE;
if (args[ARG_parity].u_obj == &nativeio_uart_parity_even_obj) {
if (args[ARG_parity].u_obj == &busio_uart_parity_even_obj) {
parity = PARITY_EVEN;
} else if (args[ARG_parity].u_obj == &nativeio_uart_parity_odd_obj) {
} else if (args[ARG_parity].u_obj == &busio_uart_parity_odd_obj) {
parity = PARITY_ODD;
}
@ -107,7 +107,7 @@ STATIC mp_obj_t nativeio_uart_make_new(const mp_obj_type_t *type, size_t n_args,
mp_raise_ValueError("stop must be 1 or 2");
}
common_hal_nativeio_uart_construct(self, tx, rx,
common_hal_busio_uart_construct(self, tx, rx,
args[ARG_baudrate].u_int, bits, parity, stop, args[ARG_timeout].u_int,
args[ARG_receiver_buffer_size].u_int);
return (mp_obj_t)self;
@ -117,12 +117,12 @@ STATIC mp_obj_t nativeio_uart_make_new(const mp_obj_type_t *type, size_t n_args,
//|
//| Deinitialises the UART and releases any hardware resources for reuse.
//|
STATIC mp_obj_t nativeio_uart_obj_deinit(mp_obj_t self_in) {
nativeio_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_nativeio_uart_deinit(self);
STATIC mp_obj_t busio_uart_obj_deinit(mp_obj_t self_in) {
busio_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_busio_uart_deinit(self);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_uart_deinit_obj, nativeio_uart_obj_deinit);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(busio_uart_deinit_obj, busio_uart_obj_deinit);
//| .. method:: __enter__()
//|
@ -134,12 +134,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_uart_deinit_obj, nativeio_uart_obj_dei
//|
//| Automatically deinitializes the hardware when exiting a context.
//|
STATIC mp_obj_t nativeio_uart_obj___exit__(size_t n_args, const mp_obj_t *args) {
STATIC mp_obj_t busio_uart_obj___exit__(size_t n_args, const mp_obj_t *args) {
(void)n_args;
common_hal_nativeio_uart_deinit(args[0]);
common_hal_busio_uart_deinit(args[0]);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_uart___exit___obj, 4, 4, nativeio_uart_obj___exit__);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(busio_uart___exit___obj, 4, 4, busio_uart_obj___exit__);
// These are standard stream methods. Code is in py/stream.c.
//
@ -175,8 +175,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_uart___exit___obj, 4, 4, nat
//|
// These three methods are used by the shared stream methods.
STATIC mp_uint_t nativeio_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, int *errcode) {
nativeio_uart_obj_t *self = self_in;
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;
byte *buf = buf_in;
// make sure we want at least 1 char
@ -184,26 +184,26 @@ STATIC mp_uint_t nativeio_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t si
return 0;
}
return common_hal_nativeio_uart_read(self, buf, size, errcode);
return common_hal_busio_uart_read(self, buf, size, errcode);
}
STATIC mp_uint_t nativeio_uart_write(mp_obj_t self_in, const void *buf_in, mp_uint_t size, int *errcode) {
nativeio_uart_obj_t *self = self_in;
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;
const byte *buf = buf_in;
return common_hal_nativeio_uart_write(self, buf, size, errcode);
return common_hal_busio_uart_write(self, buf, size, errcode);
}
STATIC mp_uint_t nativeio_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t arg, int *errcode) {
nativeio_uart_obj_t *self = self_in;
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;
mp_uint_t ret;
if (request == MP_IOCTL_POLL) {
mp_uint_t flags = arg;
ret = 0;
if ((flags & MP_IOCTL_POLL_RD) && common_hal_nativeio_uart_rx_characters_available(self) > 0) {
if ((flags & MP_IOCTL_POLL_RD) && common_hal_busio_uart_rx_characters_available(self) > 0) {
ret |= MP_IOCTL_POLL_RD;
}
if ((flags & MP_IOCTL_POLL_WR) && common_hal_nativeio_uart_ready_to_tx(self)) {
if ((flags & MP_IOCTL_POLL_WR) && common_hal_busio_uart_ready_to_tx(self)) {
ret |= MP_IOCTL_POLL_WR;
}
} else {
@ -213,7 +213,7 @@ STATIC mp_uint_t nativeio_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uin
return ret;
}
//| .. class:: nativeio.UART.Parity
//| .. class:: busio.UART.Parity
//|
//| Enum-like class to define the parity used to verify correct data transfer.
//|
@ -225,41 +225,41 @@ STATIC mp_uint_t nativeio_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uin
//|
//| Total number of ones should be even.
//|
const mp_obj_type_t nativeio_uart_parity_type;
const mp_obj_type_t busio_uart_parity_type;
const nativeio_uart_parity_obj_t nativeio_uart_parity_odd_obj = {
{ &nativeio_uart_parity_type },
const busio_uart_parity_obj_t busio_uart_parity_odd_obj = {
{ &busio_uart_parity_type },
};
const nativeio_uart_parity_obj_t nativeio_uart_parity_even_obj = {
{ &nativeio_uart_parity_type },
const busio_uart_parity_obj_t busio_uart_parity_even_obj = {
{ &busio_uart_parity_type },
};
STATIC const mp_rom_map_elem_t nativeio_uart_parity_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_ODD), MP_ROM_PTR(&nativeio_uart_parity_odd_obj) },
{ MP_ROM_QSTR(MP_QSTR_EVEN), MP_ROM_PTR(&nativeio_uart_parity_even_obj) },
STATIC const mp_rom_map_elem_t busio_uart_parity_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_ODD), MP_ROM_PTR(&busio_uart_parity_odd_obj) },
{ MP_ROM_QSTR(MP_QSTR_EVEN), MP_ROM_PTR(&busio_uart_parity_even_obj) },
};
STATIC MP_DEFINE_CONST_DICT(nativeio_uart_parity_locals_dict, nativeio_uart_parity_locals_dict_table);
STATIC MP_DEFINE_CONST_DICT(busio_uart_parity_locals_dict, busio_uart_parity_locals_dict_table);
STATIC void nativeio_uart_parity_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
STATIC void busio_uart_parity_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
qstr parity = MP_QSTR_ODD;
if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&nativeio_uart_parity_even_obj)) {
if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&busio_uart_parity_even_obj)) {
parity = MP_QSTR_EVEN;
}
mp_printf(print, "%q.%q.%q.%q", MP_QSTR_nativeio, MP_QSTR_UART, MP_QSTR_Parity, parity);
mp_printf(print, "%q.%q.%q.%q", MP_QSTR_busio, MP_QSTR_UART, MP_QSTR_Parity, parity);
}
const mp_obj_type_t nativeio_uart_parity_type = {
const mp_obj_type_t busio_uart_parity_type = {
{ &mp_type_type },
.name = MP_QSTR_Parity,
.print = nativeio_uart_parity_print,
.locals_dict = (mp_obj_t)&nativeio_uart_parity_locals_dict,
.print = busio_uart_parity_print,
.locals_dict = (mp_obj_t)&busio_uart_parity_locals_dict,
};
STATIC const mp_rom_map_elem_t nativeio_uart_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_uart_deinit_obj) },
STATIC const mp_rom_map_elem_t busio_uart_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&busio_uart_deinit_obj) },
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_uart___exit___obj) },
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&busio_uart___exit___obj) },
// Standard stream methods.
{ MP_OBJ_NEW_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },
@ -268,23 +268,23 @@ STATIC const mp_rom_map_elem_t nativeio_uart_locals_dict_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
// Nested Enum-like Classes.
{ MP_ROM_QSTR(MP_QSTR_Parity), MP_ROM_PTR(&nativeio_uart_parity_type) },
{ MP_ROM_QSTR(MP_QSTR_Parity), MP_ROM_PTR(&busio_uart_parity_type) },
};
STATIC MP_DEFINE_CONST_DICT(nativeio_uart_locals_dict, nativeio_uart_locals_dict_table);
STATIC MP_DEFINE_CONST_DICT(busio_uart_locals_dict, busio_uart_locals_dict_table);
STATIC const mp_stream_p_t uart_stream_p = {
.read = nativeio_uart_read,
.write = nativeio_uart_write,
.ioctl = nativeio_uart_ioctl,
.read = busio_uart_read,
.write = busio_uart_write,
.ioctl = busio_uart_ioctl,
.is_text = false,
};
const mp_obj_type_t nativeio_uart_type = {
const mp_obj_type_t busio_uart_type = {
{ &mp_type_type },
.name = MP_QSTR_UART,
.make_new = nativeio_uart_make_new,
.make_new = busio_uart_make_new,
.getiter = mp_identity,
.iternext = mp_stream_unbuffered_iter,
.protocol = &uart_stream_p,
.locals_dict = (mp_obj_dict_t*)&nativeio_uart_locals_dict,
.locals_dict = (mp_obj_dict_t*)&busio_uart_locals_dict,
};

View File

@ -24,13 +24,13 @@
* THE SOFTWARE.
*/
#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_UART_H__
#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_UART_H__
#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO_UART_H__
#define __MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO_UART_H__
#include "common-hal/microcontroller/types.h"
#include "common-hal/nativeio/types.h"
#include "common-hal/busio/UART.h"
extern const mp_obj_type_t nativeio_uart_type;
extern const mp_obj_type_t busio_uart_type;
typedef enum {
PARITY_NONE,
@ -39,22 +39,22 @@ typedef enum {
} uart_parity_t;
// Construct an underlying UART object.
extern void common_hal_nativeio_uart_construct(nativeio_uart_obj_t *self,
extern void common_hal_busio_uart_construct(busio_uart_obj_t *self,
const mcu_pin_obj_t * tx, const mcu_pin_obj_t * rx, uint32_t baudrate,
uint8_t bits, uart_parity_t parity, uint8_t stop, uint32_t timeout,
uint8_t receiver_buffer_size);
extern void common_hal_nativeio_uart_deinit(nativeio_uart_obj_t *self);
extern void common_hal_busio_uart_deinit(busio_uart_obj_t *self);
// Read characters. len is in characters NOT bytes!
extern size_t common_hal_nativeio_uart_read(nativeio_uart_obj_t *self,
extern size_t common_hal_busio_uart_read(busio_uart_obj_t *self,
uint8_t *data, size_t len, int *errcode);
// Write characters. len is in characters NOT bytes!
extern size_t common_hal_nativeio_uart_write(nativeio_uart_obj_t *self,
extern size_t common_hal_busio_uart_write(busio_uart_obj_t *self,
const uint8_t *data, size_t len, int *errcode);
extern uint32_t common_hal_nativeio_uart_rx_characters_available(nativeio_uart_obj_t *self);
extern bool common_hal_nativeio_uart_ready_to_tx(nativeio_uart_obj_t *self);
extern uint32_t common_hal_busio_uart_rx_characters_available(busio_uart_obj_t *self);
extern bool common_hal_busio_uart_ready_to_tx(busio_uart_obj_t *self);
#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_UART_H__
#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO_UART_H__

View File

@ -0,0 +1,98 @@
/*
* 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.
*/
#include <stdint.h>
#include "py/obj.h"
#include "py/runtime.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/busio/__init__.h"
#include "shared-bindings/busio/I2C.h"
#include "shared-bindings/busio/OneWire.h"
#include "shared-bindings/busio/SPI.h"
#include "shared-bindings/busio/UART.h"
#include "shared-bindings/busio/__init__.h"
#include "py/runtime.h"
//| :mod:`busio` --- Hardware accelerated behavior
//| =================================================
//|
//| .. module:: busio
//| :synopsis: Hardware accelerated behavior
//| :platform: SAMD21
//|
//| The `busio` module contains classes to support a variety of serial
//| protocols.
//|
//| When the microcontroller does not support the behavior in a hardware
//| accelerated fashion it may internally use a bitbang routine. However, if
//| hardware support is available on a subset of pins but not those provided,
//| then a RuntimeError will be raised. Use the `bitbangio` module to explicitly
//| bitbang a serial protocol on any general purpose pins.
//|
//| Libraries
//|
//| .. toctree::
//| :maxdepth: 3
//|
//| I2C
//| OneWire
//| SPI
//| UART
//|
//| All libraries change hardware state and should be deinitialized when they
//| are no longer needed. To do so, either call :py:meth:`!deinit` or use a
//| context manager.
//|
//| For example::
//|
//| import busio
//| from board import *
//|
//| with busio.I2C(SCL, SDA) as i2c:
//| i2c.scan()
//|
//| This example will initialize the the device, run
//| :py:meth:`~busio.I2C.scan` and then :py:meth:`~busio.I2C.deinit` the
//| hardware.
//|
STATIC const mp_rom_map_elem_t busio_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_busio) },
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&busio_i2c_type) },
{ MP_ROM_QSTR(MP_QSTR_OneWire), MP_ROM_PTR(&busio_onewire_type) },
{ MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&busio_spi_type) },
{ MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&busio_uart_type) },
};
STATIC MP_DEFINE_CONST_DICT(busio_module_globals, busio_module_globals_table);
const mp_obj_module_t busio_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&busio_module_globals,
};

View File

@ -24,9 +24,11 @@
* THE SOFTWARE.
*/
#ifndef __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_ANALOGIN_H__
#define __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_ANALOGIN_H__
#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO___INIT___H__
#define __MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO___INIT___H__
void analogin_reset(void);
#include "py/obj.h"
#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_ANALOGIN_H__
// Nothing now.
#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO___INIT___H__

View File

@ -0,0 +1,486 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdint.h>
#include <string.h>
#include "lib/utils/context_manager_helpers.h"
#include "py/nlr.h"
#include "py/objtype.h"
#include "py/objproperty.h"
#include "py/runtime.h"
#include "py/mphal.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/digitalio/DigitalInOut.h"
//| .. currentmodule:: digitalio
//|
//| :class:`DigitalInOut` -- digital input and output
//| =========================================================
//|
//| A DigitalInOut is used to digitally control I/O pins. For analog control of
//| a pin, see the :py:class:`~digitalio.AnalogIn` and
//| :py:class:`~digitalio.AnalogOut` classes.
//|
//| .. class:: DigitalInOut(pin)
//|
//| Create a new DigitalInOut object associated with the pin. Defaults to input
//| with no pull. Use :py:meth:`switch_to_input` and
//| :py:meth:`switch_to_output` to change the direction.
//|
//| :param ~microcontroller.Pin pin: The pin to control
//|
STATIC mp_obj_t digitalio_digitalinout_make_new(const mp_obj_type_t *type,
mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
digitalio_digitalinout_obj_t *self = m_new_obj(digitalio_digitalinout_obj_t);
self->base.type = &digitalio_digitalinout_type;
assert_pin(args[0], false);
mcu_pin_obj_t *pin = MP_OBJ_TO_PTR(args[0]);
assert_pin_free(pin);
common_hal_digitalio_digitalinout_construct(self, pin);
return (mp_obj_t)self;
}
//| .. method:: deinit()
//|
//| Turn off the DigitalInOut and release the pin for other use.
//|
STATIC mp_obj_t digitalio_digitalinout_obj_deinit(mp_obj_t self_in) {
digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_digitalio_digitalinout_deinit(self);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(digitalio_digitalinout_deinit_obj, digitalio_digitalinout_obj_deinit);
//| .. method:: __enter__()
//|
//| No-op used by Context Managers.
//|
// Provided by context manager helper.
//| .. method:: __exit__()
//|
//| Automatically deinitializes the hardware when exiting a context.
//|
STATIC mp_obj_t digitalio_digitalinout_obj___exit__(size_t n_args, const mp_obj_t *args) {
(void)n_args;
common_hal_digitalio_digitalinout_deinit(args[0]);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(digitalio_digitalinout_obj___exit___obj, 4, 4, digitalio_digitalinout_obj___exit__);
//|
//| .. method:: switch_to_output(value=False, drive_mode=DriveMode.PUSH_PULL)
//|
//| Switch to writing out digital values.
//|
//| :param bool value: default value to set upon switching
//| :param DriveMode drive_mode: drive mode for the output
//|
typedef struct {
mp_obj_base_t base;
} digitalio_digitalinout_drive_mode_obj_t;
extern const digitalio_digitalinout_drive_mode_obj_t digitalio_digitalinout_drive_mode_push_pull_obj;
extern const digitalio_digitalinout_drive_mode_obj_t digitalio_digitalinout_drive_mode_open_drain_obj;
STATIC mp_obj_t digitalio_digitalinout_switch_to_output(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_value, ARG_drive_mode };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_value, MP_ARG_BOOL, {.u_bool = false} },
{ MP_QSTR_drive_mode, MP_ARG_OBJ, {.u_rom_obj = &digitalio_digitalinout_drive_mode_push_pull_obj} },
};
digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
enum digitalinout_drive_mode_t drive_mode = DRIVE_MODE_PUSH_PULL;
if (args[ARG_drive_mode].u_rom_obj == &digitalio_digitalinout_drive_mode_open_drain_obj) {
drive_mode = DRIVE_MODE_OPEN_DRAIN;
}
// do the transfer
common_hal_digitalio_digitalinout_switch_to_output(self, args[ARG_value].u_bool, drive_mode);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_KW(digitalio_digitalinout_switch_to_output_obj, 1, digitalio_digitalinout_switch_to_output);
//| .. method:: switch_to_input(pull=None)
//|
//| Switch to read in digital values.
//|
//| :param Pull pull: pull configuration for the input
//|
//| Example usage::
//|
//| import digitalio
//| import board
//|
//| with digitalio.DigitalInOut(board.SLIDE_SWITCH) as switch:
//| switch.switch_to_input(pull=digitalio.DigitalInOut.Pull.UP)
//| # Or, after switch_to_input
//| switch.pull = digitalio.DigitalInOut.Pull.UP
//| print(switch.value)
//|
typedef struct {
mp_obj_base_t base;
} digitalio_digitalinout_pull_obj_t;
extern const digitalio_digitalinout_pull_obj_t digitalio_digitalinout_pull_up_obj;
extern const digitalio_digitalinout_pull_obj_t digitalio_digitalinout_pull_down_obj;
STATIC mp_obj_t digitalio_digitalinout_switch_to_input(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_pull };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_pull, MP_ARG_OBJ, {.u_rom_obj = mp_const_none} },
};
digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
enum digitalinout_pull_t pull = PULL_NONE;
if (args[ARG_pull].u_rom_obj == &digitalio_digitalinout_pull_up_obj) {
pull = PULL_UP;
}else if (args[ARG_pull].u_rom_obj == &digitalio_digitalinout_pull_down_obj) {
pull = PULL_DOWN;
}
// do the transfer
common_hal_digitalio_digitalinout_switch_to_input(self, pull);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_KW(digitalio_digitalinout_switch_to_input_obj, 1, digitalio_digitalinout_switch_to_input);
//| .. attribute:: direction
//|
//| Get the direction of the pin.
//|
//| :raises AttributeError: when set. Use :py:meth:`switch_to_input` and :py:meth:`switch_to_output` to change the direction.
//|
typedef struct {
mp_obj_base_t base;
} digitalio_digitalinout_direction_obj_t;
extern const digitalio_digitalinout_direction_obj_t digitalio_digitalinout_direction_in_obj;
extern const digitalio_digitalinout_direction_obj_t digitalio_digitalinout_direction_out_obj;
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);
enum digitalinout_direction_t direction = common_hal_digitalio_digitalinout_get_direction(self);
if (direction == DIRECTION_IN) {
return (mp_obj_t)&digitalio_digitalinout_direction_in_obj;
}
return (mp_obj_t)&digitalio_digitalinout_direction_out_obj;
}
MP_DEFINE_CONST_FUN_OBJ_1(digitalio_digitalinout_get_direction_obj, digitalio_digitalinout_obj_get_direction);
const mp_obj_property_t digitalio_digitalinout_direction_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&digitalio_digitalinout_get_direction_obj,
(mp_obj_t)&mp_const_none_obj,
(mp_obj_t)&mp_const_none_obj},
};
//| .. attribute:: value
//|
//| The digital logic level of the pin.
//|
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);
bool value = common_hal_digitalio_digitalinout_get_value(self);
return mp_obj_new_bool(value);
}
MP_DEFINE_CONST_FUN_OBJ_1(digitalio_digitalinout_get_value_obj, digitalio_digitalinout_obj_get_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);
if (common_hal_digitalio_digitalinout_get_direction(self) == DIRECTION_IN) {
mp_raise_AttributeError("Cannot set value when direction is input.");
return mp_const_none;
}
common_hal_digitalio_digitalinout_set_value(self, mp_obj_is_true(value));
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(digitalio_digitalinout_set_value_obj, digitalio_digitalinout_obj_set_value);
const mp_obj_property_t digitalio_digitalinout_value_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&digitalio_digitalinout_get_value_obj,
(mp_obj_t)&digitalio_digitalinout_set_value_obj,
(mp_obj_t)&mp_const_none_obj},
};
//| .. attribute:: drive_mode
//|
//| Get or set the pin drive mode.
//|
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);
if (common_hal_digitalio_digitalinout_get_direction(self) == DIRECTION_IN) {
mp_raise_AttributeError("Drive mode not used when direction is input.");
return mp_const_none;
}
enum digitalinout_drive_mode_t drive_mode = common_hal_digitalio_digitalinout_get_drive_mode(self);
if (drive_mode == DRIVE_MODE_PUSH_PULL) {
return (mp_obj_t)&digitalio_digitalinout_drive_mode_push_pull_obj;
}
return (mp_obj_t)&digitalio_digitalinout_drive_mode_open_drain_obj;
}
MP_DEFINE_CONST_FUN_OBJ_1(digitalio_digitalinout_get_drive_mode_obj, digitalio_digitalinout_obj_get_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);
if (common_hal_digitalio_digitalinout_get_direction(self) == DIRECTION_IN) {
mp_raise_AttributeError("Drive mode not used when direction is input.");
return mp_const_none;
}
enum digitalinout_drive_mode_t c_drive_mode = DRIVE_MODE_PUSH_PULL;
if (drive_mode == &digitalio_digitalinout_drive_mode_open_drain_obj) {
c_drive_mode = DRIVE_MODE_OPEN_DRAIN;
}
common_hal_digitalio_digitalinout_set_drive_mode(self, c_drive_mode);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(digitalio_digitalinout_set_drive_mode_obj, digitalio_digitalinout_obj_set_drive_mode);
const mp_obj_property_t digitalio_digitalinout_drive_mode_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&digitalio_digitalinout_get_drive_mode_obj,
(mp_obj_t)&digitalio_digitalinout_set_drive_mode_obj,
(mp_obj_t)&mp_const_none_obj},
};
//| .. attribute:: pull
//|
//| Get or set the pin pull.
//|
//| :raises AttributeError: if the direction is ~`Direction.OUT`.
//|
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);
if (common_hal_digitalio_digitalinout_get_direction(self) == DIRECTION_OUT) {
mp_raise_AttributeError("Pull not used when direction is output.");
return mp_const_none;
}
enum digitalinout_pull_t pull = common_hal_digitalio_digitalinout_get_pull(self);
if (pull == PULL_UP) {
return (mp_obj_t)&digitalio_digitalinout_pull_up_obj;
} else if (pull == PULL_DOWN) {
return (mp_obj_t)&digitalio_digitalinout_pull_down_obj;
}
return (mp_obj_t)&mp_const_none_obj;
}
MP_DEFINE_CONST_FUN_OBJ_1(digitalio_digitalinout_get_pull_obj, digitalio_digitalinout_obj_get_pull);
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);
if (common_hal_digitalio_digitalinout_get_direction(self) == DIRECTION_OUT) {
mp_raise_AttributeError("Pull not used when direction is output.");
return mp_const_none;
}
enum digitalinout_pull_t pull = PULL_NONE;
if (pull_obj == &digitalio_digitalinout_pull_up_obj) {
pull = PULL_UP;
} else if (pull_obj == &digitalio_digitalinout_pull_down_obj) {
pull = PULL_DOWN;
}
common_hal_digitalio_digitalinout_set_pull(self, pull);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(digitalio_digitalinout_set_pull_obj, digitalio_digitalinout_obj_set_pull);
const mp_obj_property_t digitalio_digitalinout_pull_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&digitalio_digitalinout_get_pull_obj,
(mp_obj_t)&digitalio_digitalinout_set_pull_obj,
(mp_obj_t)&mp_const_none_obj},
};
//| .. class:: digitalio.DigitalInOut.Direction
//|
//| Enum-like class to define which direction the digital values are
//| going.
//|
//| .. data:: IN
//|
//| Read digital data in
//|
//| .. data:: OUT
//|
//| Write digital data out
//|
const mp_obj_type_t digitalio_digitalinout_direction_type;
const digitalio_digitalinout_direction_obj_t digitalio_digitalinout_direction_in_obj = {
{ &digitalio_digitalinout_direction_type },
};
const digitalio_digitalinout_direction_obj_t digitalio_digitalinout_direction_out_obj = {
{ &digitalio_digitalinout_direction_type },
};
STATIC const mp_rom_map_elem_t digitalio_digitalinout_direction_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_IN), MP_ROM_PTR(&digitalio_digitalinout_direction_in_obj) },
{ MP_ROM_QSTR(MP_QSTR_OUT), MP_ROM_PTR(&digitalio_digitalinout_direction_out_obj) },
};
STATIC MP_DEFINE_CONST_DICT(digitalio_digitalinout_direction_locals_dict, digitalio_digitalinout_direction_locals_dict_table);
STATIC void digitalio_digitalinout_direction_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
qstr direction = MP_QSTR_IN;
if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&digitalio_digitalinout_direction_out_obj)) {
direction = MP_QSTR_OUT;
}
mp_printf(print, "%q.%q.%q.%q", MP_QSTR_digitalio, MP_QSTR_DigitalInOut, MP_QSTR_Direction, direction);
}
const mp_obj_type_t digitalio_digitalinout_direction_type = {
{ &mp_type_type },
.name = MP_QSTR_Direction,
.print = digitalio_digitalinout_direction_print,
.locals_dict = (mp_obj_t)&digitalio_digitalinout_direction_locals_dict,
};
//| .. class:: digitalio.DigitalInOut.DriveMode
//|
//| Enum-like class to define the drive mode used when outputting
//| digital values.
//|
//| .. data:: PUSH_PULL
//|
//| Output both high and low digital values
//|
//| .. data:: OPEN_DRAIN
//|
//| Output low digital values but go into high z for digital high. This is
//| useful for i2c and other protocols that share a digital line.
//|
const mp_obj_type_t digitalio_digitalinout_drive_mode_type;
const digitalio_digitalinout_drive_mode_obj_t digitalio_digitalinout_drive_mode_push_pull_obj = {
{ &digitalio_digitalinout_drive_mode_type },
};
const digitalio_digitalinout_drive_mode_obj_t digitalio_digitalinout_drive_mode_open_drain_obj = {
{ &digitalio_digitalinout_drive_mode_type },
};
STATIC const mp_rom_map_elem_t digitalio_digitalinout_drive_mode_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_PUSH_PULL), MP_ROM_PTR(&digitalio_digitalinout_drive_mode_push_pull_obj) },
{ MP_ROM_QSTR(MP_QSTR_OPEN_DRAIN), MP_ROM_PTR(&digitalio_digitalinout_drive_mode_open_drain_obj) },
};
STATIC MP_DEFINE_CONST_DICT(digitalio_digitalinout_drive_mode_locals_dict, digitalio_digitalinout_drive_mode_locals_dict_table);
STATIC void digitalio_digitalinout_drive_mode_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
qstr drive_mode = MP_QSTR_PUSH_PULL;
if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&digitalio_digitalinout_drive_mode_open_drain_obj)) {
drive_mode = MP_QSTR_OPEN_DRAIN;
}
mp_printf(print, "%q.%q.%q.%q", MP_QSTR_digitalio, MP_QSTR_DigitalInOut, MP_QSTR_DriveMode, drive_mode);
}
const mp_obj_type_t digitalio_digitalinout_drive_mode_type = {
{ &mp_type_type },
.name = MP_QSTR_DriveMode,
.print = digitalio_digitalinout_drive_mode_print,
.locals_dict = (mp_obj_t)&digitalio_digitalinout_drive_mode_locals_dict,
};
//| .. class:: digitalio.DigitalInOut.Pull
//|
//| Enum-like class to define the pull value, if any, used while reading
//| digital values in.
//|
//| .. data:: UP
//|
//| When the input line isn't being driven the pull up can pull the state
//| of the line high so it reads as true.
//|
//| .. data:: DOWN
//|
//| When the input line isn't being driven the pull down can pull the
//| state of the line low so it reads as false.
//|
const mp_obj_type_t digitalio_digitalinout_pull_type;
const digitalio_digitalinout_pull_obj_t digitalio_digitalinout_pull_up_obj = {
{ &digitalio_digitalinout_pull_type },
};
const digitalio_digitalinout_pull_obj_t digitalio_digitalinout_pull_down_obj = {
{ &digitalio_digitalinout_pull_type },
};
STATIC const mp_rom_map_elem_t digitalio_digitalinout_pull_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_UP), MP_ROM_PTR(&digitalio_digitalinout_pull_up_obj) },
{ MP_ROM_QSTR(MP_QSTR_DOWN), MP_ROM_PTR(&digitalio_digitalinout_pull_down_obj) },
};
STATIC MP_DEFINE_CONST_DICT(digitalio_digitalinout_pull_locals_dict, digitalio_digitalinout_pull_locals_dict_table);
STATIC void digitalio_digitalinout_pull_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
qstr pull = MP_QSTR_UP;
if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&digitalio_digitalinout_pull_down_obj)) {
pull = MP_QSTR_DOWN;
}
mp_printf(print, "%q.%q.%q.%q", MP_QSTR_digitalio, MP_QSTR_DigitalInOut, MP_QSTR_Pull, pull);
}
const mp_obj_type_t digitalio_digitalinout_pull_type = {
{ &mp_type_type },
.name = MP_QSTR_Pull,
.print = digitalio_digitalinout_pull_print,
.locals_dict = (mp_obj_t)&digitalio_digitalinout_pull_locals_dict,
};
STATIC const mp_rom_map_elem_t digitalio_digitalinout_locals_dict_table[] = {
// instance methods
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&digitalio_digitalinout_deinit_obj) },
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&digitalio_digitalinout_obj___exit___obj) },
{ MP_ROM_QSTR(MP_QSTR_switch_to_output), MP_ROM_PTR(&digitalio_digitalinout_switch_to_output_obj) },
{ MP_ROM_QSTR(MP_QSTR_switch_to_input), MP_ROM_PTR(&digitalio_digitalinout_switch_to_input_obj) },
// Properties
{ MP_ROM_QSTR(MP_QSTR_direction), MP_ROM_PTR(&digitalio_digitalinout_direction_obj) },
{ MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&digitalio_digitalinout_value_obj) },
{ MP_ROM_QSTR(MP_QSTR_drive_mode), MP_ROM_PTR(&digitalio_digitalinout_drive_mode_obj) },
{ MP_ROM_QSTR(MP_QSTR_pull), MP_ROM_PTR(&digitalio_digitalinout_pull_obj) },
// Nested Enum-like Classes.
{ MP_ROM_QSTR(MP_QSTR_Direction), MP_ROM_PTR(&digitalio_digitalinout_direction_type) },
{ MP_ROM_QSTR(MP_QSTR_DriveMode), MP_ROM_PTR(&digitalio_digitalinout_drive_mode_type) },
{ MP_ROM_QSTR(MP_QSTR_Pull), MP_ROM_PTR(&digitalio_digitalinout_pull_type) },
};
STATIC MP_DEFINE_CONST_DICT(digitalio_digitalinout_locals_dict, digitalio_digitalinout_locals_dict_table);
const mp_obj_type_t digitalio_digitalinout_type = {
{ &mp_type_type },
.name = MP_QSTR_DigitalInOut,
.make_new = digitalio_digitalinout_make_new,
.locals_dict = (mp_obj_t)&digitalio_digitalinout_locals_dict,
};

View File

@ -24,13 +24,13 @@
* THE SOFTWARE.
*/
#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_DIGITALINOUT_H__
#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_DIGITALINOUT_H__
#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_DIGITALIO_DIGITALINOUT_H__
#define __MICROPY_INCLUDED_SHARED_BINDINGS_DIGITALIO_DIGITALINOUT_H__
#include "common-hal/microcontroller/types.h"
#include "common-hal/nativeio/types.h"
#include "common-hal/digitalio/DigitalInOut.h"
extern const mp_obj_type_t nativeio_digitalinout_type;
extern const mp_obj_type_t digitalio_digitalinout_type;
enum digitalinout_direction_t {
DIRECTION_IN,
@ -53,16 +53,16 @@ typedef enum {
DIGITALINOUT_PIN_BUSY
} digitalinout_result_t;
digitalinout_result_t common_hal_nativeio_digitalinout_construct(nativeio_digitalinout_obj_t* self, const mcu_pin_obj_t* pin);
void common_hal_nativeio_digitalinout_deinit(nativeio_digitalinout_obj_t* self);
void common_hal_nativeio_digitalinout_switch_to_input(nativeio_digitalinout_obj_t* self, enum digitalinout_pull_t pull);
void common_hal_nativeio_digitalinout_switch_to_output(nativeio_digitalinout_obj_t* self, bool value, enum digitalinout_drive_mode_t drive_mode);
enum digitalinout_direction_t common_hal_nativeio_digitalinout_get_direction(nativeio_digitalinout_obj_t* self);
void common_hal_nativeio_digitalinout_set_value(nativeio_digitalinout_obj_t* self, bool value);
bool common_hal_nativeio_digitalinout_get_value(nativeio_digitalinout_obj_t* self);
void common_hal_nativeio_digitalinout_set_drive_mode(nativeio_digitalinout_obj_t* self, enum digitalinout_drive_mode_t drive_mode);
enum digitalinout_drive_mode_t common_hal_nativeio_digitalinout_get_drive_mode(nativeio_digitalinout_obj_t* self);
void common_hal_nativeio_digitalinout_set_pull(nativeio_digitalinout_obj_t* self, enum digitalinout_pull_t pull);
enum digitalinout_pull_t common_hal_nativeio_digitalinout_get_pull(nativeio_digitalinout_obj_t* self);
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_switch_to_input(digitalio_digitalinout_obj_t* self, enum digitalinout_pull_t pull);
void common_hal_digitalio_digitalinout_switch_to_output(digitalio_digitalinout_obj_t* self, bool value, enum digitalinout_drive_mode_t drive_mode);
enum digitalinout_direction_t common_hal_digitalio_digitalinout_get_direction(digitalio_digitalinout_obj_t* self);
void common_hal_digitalio_digitalinout_set_value(digitalio_digitalinout_obj_t* self, bool value);
bool common_hal_digitalio_digitalinout_get_value(digitalio_digitalinout_obj_t* self);
void common_hal_digitalio_digitalinout_set_drive_mode(digitalio_digitalinout_obj_t* self, enum digitalinout_drive_mode_t drive_mode);
enum digitalinout_drive_mode_t common_hal_digitalio_digitalinout_get_drive_mode(digitalio_digitalinout_obj_t* self);
void common_hal_digitalio_digitalinout_set_pull(digitalio_digitalinout_obj_t* self, enum digitalinout_pull_t pull);
enum digitalinout_pull_t common_hal_digitalio_digitalinout_get_pull(digitalio_digitalinout_obj_t* self);
#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_DIGITALINOUT_H__
#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_DIGITALIO_DIGITALINOUT_H__

View File

@ -0,0 +1,95 @@
/*
* 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.
*/
#include <stdint.h>
#include "py/obj.h"
#include "py/runtime.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/digitalio/__init__.h"
#include "shared-bindings/digitalio/DigitalInOut.h"
#include "py/runtime.h"
//| :mod:`digitalio` --- Basic digital pin support
//| =================================================
//|
//| .. module:: digitalio
//| :synopsis: Basic digital pin support
//| :platform: SAMD21, ESP8266
//|
//| The `digitalio` module contains classes to provide access to basic digital IO.
//|
//| Libraries
//|
//| .. toctree::
//| :maxdepth: 3
//|
//| DigitalInOut
//|
//| All classes change hardware state and should be deinitialized when they
//| are no longer needed. To do so, either call :py:meth:`!deinit` or use a
//| context manager.
//|
//| For example::
//|
//| import digitalio
//| from board import *
//|
//| with digitalio.DigitalInOut(D13) as pin:
//| print(pin.value)
//|
//| This example will initialize the the device, read
//| :py:data:`~digitalio.DigitalInOut.value` and then
//| :py:meth:`~digitalio.DigitalInOut.deinit` the hardware.
//|
//| Here is blinky::
//|
//| import digitalio
//| from board import *
//| import time
//|
//| with digitalio.DigitalInOut(D13) as led:
//| led.switch_to_output()
//| while True:
//| led.value = True
//| time.sleep(0.1)
//| led.value = False
//| time.sleep(0.1)
//|
STATIC const mp_rom_map_elem_t digitalio_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_digitalio) },
{ MP_ROM_QSTR(MP_QSTR_DigitalInOut), MP_ROM_PTR(&digitalio_digitalinout_type) },
};
STATIC MP_DEFINE_CONST_DICT(digitalio_module_globals, digitalio_module_globals_table);
const mp_obj_module_t digitalio_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&digitalio_module_globals,
};

View File

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

View File

@ -1,486 +0,0 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdint.h>
#include <string.h>
#include "lib/utils/context_manager_helpers.h"
#include "py/nlr.h"
#include "py/objtype.h"
#include "py/objproperty.h"
#include "py/runtime.h"
#include "py/mphal.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/nativeio/DigitalInOut.h"
//| .. currentmodule:: nativeio
//|
//| :class:`DigitalInOut` -- digital input and output
//| =========================================================
//|
//| A DigitalInOut is used to digitally control I/O pins. For analog control of
//| a pin, see the :py:class:`~nativeio.AnalogIn` and
//| :py:class:`~nativeio.AnalogOut` classes.
//|
//| .. class:: DigitalInOut(pin)
//|
//| Create a new DigitalInOut object associated with the pin. Defaults to input
//| with no pull. Use :py:meth:`switch_to_input` and
//| :py:meth:`switch_to_output` to change the direction.
//|
//| :param ~microcontroller.Pin pin: The pin to control
//|
STATIC mp_obj_t nativeio_digitalinout_make_new(const mp_obj_type_t *type,
mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
nativeio_digitalinout_obj_t *self = m_new_obj(nativeio_digitalinout_obj_t);
self->base.type = &nativeio_digitalinout_type;
assert_pin(args[0], false);
mcu_pin_obj_t *pin = MP_OBJ_TO_PTR(args[0]);
assert_pin_free(pin);
common_hal_nativeio_digitalinout_construct(self, pin);
return (mp_obj_t)self;
}
//| .. method:: deinit()
//|
//| Turn off the DigitalInOut and release the pin for other use.
//|
STATIC mp_obj_t nativeio_digitalinout_obj_deinit(mp_obj_t self_in) {
nativeio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_nativeio_digitalinout_deinit(self);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(nativeio_digitalinout_deinit_obj, nativeio_digitalinout_obj_deinit);
//| .. method:: __enter__()
//|
//| No-op used by Context Managers.
//|
// Provided by context manager helper.
//| .. method:: __exit__()
//|
//| Automatically deinitializes the hardware when exiting a context.
//|
STATIC mp_obj_t nativeio_digitalinout_obj___exit__(size_t n_args, const mp_obj_t *args) {
(void)n_args;
common_hal_nativeio_digitalinout_deinit(args[0]);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_digitalinout_obj___exit___obj, 4, 4, nativeio_digitalinout_obj___exit__);
//|
//| .. method:: switch_to_output(value=False, drive_mode=DriveMode.PUSH_PULL)
//|
//| Switch to writing out digital values.
//|
//| :param bool value: default value to set upon switching
//| :param DriveMode drive_mode: drive mode for the output
//|
typedef struct {
mp_obj_base_t base;
} nativeio_digitalinout_drive_mode_obj_t;
extern const nativeio_digitalinout_drive_mode_obj_t nativeio_digitalinout_drive_mode_push_pull_obj;
extern const nativeio_digitalinout_drive_mode_obj_t nativeio_digitalinout_drive_mode_open_drain_obj;
STATIC mp_obj_t nativeio_digitalinout_switch_to_output(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_value, ARG_drive_mode };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_value, MP_ARG_BOOL, {.u_bool = false} },
{ MP_QSTR_drive_mode, MP_ARG_OBJ, {.u_rom_obj = &nativeio_digitalinout_drive_mode_push_pull_obj} },
};
nativeio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
enum digitalinout_drive_mode_t drive_mode = DRIVE_MODE_PUSH_PULL;
if (args[ARG_drive_mode].u_rom_obj == &nativeio_digitalinout_drive_mode_open_drain_obj) {
drive_mode = DRIVE_MODE_OPEN_DRAIN;
}
// do the transfer
common_hal_nativeio_digitalinout_switch_to_output(self, args[ARG_value].u_bool, drive_mode);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_KW(nativeio_digitalinout_switch_to_output_obj, 1, nativeio_digitalinout_switch_to_output);
//| .. method:: switch_to_input(pull=None)
//|
//| Switch to read in digital values.
//|
//| :param Pull pull: pull configuration for the input
//|
//| Example usage::
//|
//| import nativeio
//| import board
//|
//| with nativeio.DigitalInOut(board.SLIDE_SWITCH) as switch:
//| switch.switch_to_input(pull=nativeio.DigitalInOut.Pull.UP)
//| # Or, after switch_to_input
//| switch.pull = nativeio.DigitalInOut.Pull.UP
//| print(switch.value)
//|
typedef struct {
mp_obj_base_t base;
} nativeio_digitalinout_pull_obj_t;
extern const nativeio_digitalinout_pull_obj_t nativeio_digitalinout_pull_up_obj;
extern const nativeio_digitalinout_pull_obj_t nativeio_digitalinout_pull_down_obj;
STATIC mp_obj_t nativeio_digitalinout_switch_to_input(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_pull };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_pull, MP_ARG_OBJ, {.u_rom_obj = mp_const_none} },
};
nativeio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
enum digitalinout_pull_t pull = PULL_NONE;
if (args[ARG_pull].u_rom_obj == &nativeio_digitalinout_pull_up_obj) {
pull = PULL_UP;
}else if (args[ARG_pull].u_rom_obj == &nativeio_digitalinout_pull_down_obj) {
pull = PULL_DOWN;
}
// do the transfer
common_hal_nativeio_digitalinout_switch_to_input(self, pull);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_KW(nativeio_digitalinout_switch_to_input_obj, 1, nativeio_digitalinout_switch_to_input);
//| .. attribute:: direction
//|
//| Get the direction of the pin.
//|
//| :raises AttributeError: when set. Use :py:meth:`switch_to_input` and :py:meth:`switch_to_output` to change the direction.
//|
typedef struct {
mp_obj_base_t base;
} nativeio_digitalinout_direction_obj_t;
extern const nativeio_digitalinout_direction_obj_t nativeio_digitalinout_direction_in_obj;
extern const nativeio_digitalinout_direction_obj_t nativeio_digitalinout_direction_out_obj;
STATIC mp_obj_t nativeio_digitalinout_obj_get_direction(mp_obj_t self_in) {
nativeio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in);
enum digitalinout_direction_t direction = common_hal_nativeio_digitalinout_get_direction(self);
if (direction == DIRECTION_IN) {
return (mp_obj_t)&nativeio_digitalinout_direction_in_obj;
}
return (mp_obj_t)&nativeio_digitalinout_direction_out_obj;
}
MP_DEFINE_CONST_FUN_OBJ_1(nativeio_digitalinout_get_direction_obj, nativeio_digitalinout_obj_get_direction);
const mp_obj_property_t nativeio_digitalinout_direction_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&nativeio_digitalinout_get_direction_obj,
(mp_obj_t)&mp_const_none_obj,
(mp_obj_t)&mp_const_none_obj},
};
//| .. attribute:: value
//|
//| The digital logic level of the pin.
//|
STATIC mp_obj_t nativeio_digitalinout_obj_get_value(mp_obj_t self_in) {
nativeio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in);
bool value = common_hal_nativeio_digitalinout_get_value(self);
return mp_obj_new_bool(value);
}
MP_DEFINE_CONST_FUN_OBJ_1(nativeio_digitalinout_get_value_obj, nativeio_digitalinout_obj_get_value);
STATIC mp_obj_t nativeio_digitalinout_obj_set_value(mp_obj_t self_in, mp_obj_t value) {
nativeio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in);
if (common_hal_nativeio_digitalinout_get_direction(self) == DIRECTION_IN) {
mp_raise_AttributeError("Cannot set value when direction is input.");
return mp_const_none;
}
common_hal_nativeio_digitalinout_set_value(self, mp_obj_is_true(value));
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(nativeio_digitalinout_set_value_obj, nativeio_digitalinout_obj_set_value);
const mp_obj_property_t nativeio_digitalinout_value_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&nativeio_digitalinout_get_value_obj,
(mp_obj_t)&nativeio_digitalinout_set_value_obj,
(mp_obj_t)&mp_const_none_obj},
};
//| .. attribute:: drive_mode
//|
//| Get or set the pin drive mode.
//|
STATIC mp_obj_t nativeio_digitalinout_obj_get_drive_mode(mp_obj_t self_in) {
nativeio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in);
if (common_hal_nativeio_digitalinout_get_direction(self) == DIRECTION_IN) {
mp_raise_AttributeError("Drive mode not used when direction is input.");
return mp_const_none;
}
enum digitalinout_drive_mode_t drive_mode = common_hal_nativeio_digitalinout_get_drive_mode(self);
if (drive_mode == DRIVE_MODE_PUSH_PULL) {
return (mp_obj_t)&nativeio_digitalinout_drive_mode_push_pull_obj;
}
return (mp_obj_t)&nativeio_digitalinout_drive_mode_open_drain_obj;
}
MP_DEFINE_CONST_FUN_OBJ_1(nativeio_digitalinout_get_drive_mode_obj, nativeio_digitalinout_obj_get_drive_mode);
STATIC mp_obj_t nativeio_digitalinout_obj_set_drive_mode(mp_obj_t self_in, mp_obj_t drive_mode) {
nativeio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in);
if (common_hal_nativeio_digitalinout_get_direction(self) == DIRECTION_IN) {
mp_raise_AttributeError("Drive mode not used when direction is input.");
return mp_const_none;
}
enum digitalinout_drive_mode_t c_drive_mode = DRIVE_MODE_PUSH_PULL;
if (drive_mode == &nativeio_digitalinout_drive_mode_open_drain_obj) {
c_drive_mode = DRIVE_MODE_OPEN_DRAIN;
}
common_hal_nativeio_digitalinout_set_drive_mode(self, c_drive_mode);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(nativeio_digitalinout_set_drive_mode_obj, nativeio_digitalinout_obj_set_drive_mode);
const mp_obj_property_t nativeio_digitalinout_drive_mode_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&nativeio_digitalinout_get_drive_mode_obj,
(mp_obj_t)&nativeio_digitalinout_set_drive_mode_obj,
(mp_obj_t)&mp_const_none_obj},
};
//| .. attribute:: pull
//|
//| Get or set the pin pull.
//|
//| :raises AttributeError: if the direction is ~`Direction.OUT`.
//|
STATIC mp_obj_t nativeio_digitalinout_obj_get_pull(mp_obj_t self_in) {
nativeio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in);
if (common_hal_nativeio_digitalinout_get_direction(self) == DIRECTION_OUT) {
mp_raise_AttributeError("Pull not used when direction is output.");
return mp_const_none;
}
enum digitalinout_pull_t pull = common_hal_nativeio_digitalinout_get_pull(self);
if (pull == PULL_UP) {
return (mp_obj_t)&nativeio_digitalinout_pull_up_obj;
} else if (pull == PULL_DOWN) {
return (mp_obj_t)&nativeio_digitalinout_pull_down_obj;
}
return (mp_obj_t)&mp_const_none_obj;
}
MP_DEFINE_CONST_FUN_OBJ_1(nativeio_digitalinout_get_pull_obj, nativeio_digitalinout_obj_get_pull);
STATIC mp_obj_t nativeio_digitalinout_obj_set_pull(mp_obj_t self_in, mp_obj_t pull_obj) {
nativeio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in);
if (common_hal_nativeio_digitalinout_get_direction(self) == DIRECTION_OUT) {
mp_raise_AttributeError("Pull not used when direction is output.");
return mp_const_none;
}
enum digitalinout_pull_t pull = PULL_NONE;
if (pull_obj == &nativeio_digitalinout_pull_up_obj) {
pull = PULL_UP;
} else if (pull_obj == &nativeio_digitalinout_pull_down_obj) {
pull = PULL_DOWN;
}
common_hal_nativeio_digitalinout_set_pull(self, pull);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(nativeio_digitalinout_set_pull_obj, nativeio_digitalinout_obj_set_pull);
const mp_obj_property_t nativeio_digitalinout_pull_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&nativeio_digitalinout_get_pull_obj,
(mp_obj_t)&nativeio_digitalinout_set_pull_obj,
(mp_obj_t)&mp_const_none_obj},
};
//| .. class:: microcontroller.DigitalInOut.Direction
//|
//| Enum-like class to define which direction the digital values are
//| going.
//|
//| .. data:: IN
//|
//| Read digital data in
//|
//| .. data:: OUT
//|
//| Write digital data out
//|
const mp_obj_type_t nativeio_digitalinout_direction_type;
const nativeio_digitalinout_direction_obj_t nativeio_digitalinout_direction_in_obj = {
{ &nativeio_digitalinout_direction_type },
};
const nativeio_digitalinout_direction_obj_t nativeio_digitalinout_direction_out_obj = {
{ &nativeio_digitalinout_direction_type },
};
STATIC const mp_rom_map_elem_t nativeio_digitalinout_direction_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_IN), MP_ROM_PTR(&nativeio_digitalinout_direction_in_obj) },
{ MP_ROM_QSTR(MP_QSTR_OUT), MP_ROM_PTR(&nativeio_digitalinout_direction_out_obj) },
};
STATIC MP_DEFINE_CONST_DICT(nativeio_digitalinout_direction_locals_dict, nativeio_digitalinout_direction_locals_dict_table);
STATIC void nativeio_digitalinout_direction_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
qstr direction = MP_QSTR_IN;
if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&nativeio_digitalinout_direction_out_obj)) {
direction = MP_QSTR_OUT;
}
mp_printf(print, "%q.%q.%q.%q", MP_QSTR_nativeio, MP_QSTR_DigitalInOut, MP_QSTR_Direction, direction);
}
const mp_obj_type_t nativeio_digitalinout_direction_type = {
{ &mp_type_type },
.name = MP_QSTR_Direction,
.print = nativeio_digitalinout_direction_print,
.locals_dict = (mp_obj_t)&nativeio_digitalinout_direction_locals_dict,
};
//| .. class:: nativeio.DigitalInOut.DriveMode
//|
//| Enum-like class to define the drive mode used when outputting
//| digital values.
//|
//| .. data:: PUSH_PULL
//|
//| Output both high and low digital values
//|
//| .. data:: OPEN_DRAIN
//|
//| Output low digital values but go into high z for digital high. This is
//| useful for i2c and other protocols that share a digital line.
//|
const mp_obj_type_t nativeio_digitalinout_drive_mode_type;
const nativeio_digitalinout_drive_mode_obj_t nativeio_digitalinout_drive_mode_push_pull_obj = {
{ &nativeio_digitalinout_drive_mode_type },
};
const nativeio_digitalinout_drive_mode_obj_t nativeio_digitalinout_drive_mode_open_drain_obj = {
{ &nativeio_digitalinout_drive_mode_type },
};
STATIC const mp_rom_map_elem_t nativeio_digitalinout_drive_mode_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_PUSH_PULL), MP_ROM_PTR(&nativeio_digitalinout_drive_mode_push_pull_obj) },
{ MP_ROM_QSTR(MP_QSTR_OPEN_DRAIN), MP_ROM_PTR(&nativeio_digitalinout_drive_mode_open_drain_obj) },
};
STATIC MP_DEFINE_CONST_DICT(nativeio_digitalinout_drive_mode_locals_dict, nativeio_digitalinout_drive_mode_locals_dict_table);
STATIC void nativeio_digitalinout_drive_mode_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
qstr drive_mode = MP_QSTR_PUSH_PULL;
if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&nativeio_digitalinout_drive_mode_open_drain_obj)) {
drive_mode = MP_QSTR_OPEN_DRAIN;
}
mp_printf(print, "%q.%q.%q.%q", MP_QSTR_nativeio, MP_QSTR_DigitalInOut, MP_QSTR_DriveMode, drive_mode);
}
const mp_obj_type_t nativeio_digitalinout_drive_mode_type = {
{ &mp_type_type },
.name = MP_QSTR_DriveMode,
.print = nativeio_digitalinout_drive_mode_print,
.locals_dict = (mp_obj_t)&nativeio_digitalinout_drive_mode_locals_dict,
};
//| .. class:: nativeio.DigitalInOut.Pull
//|
//| Enum-like class to define the pull value, if any, used while reading
//| digital values in.
//|
//| .. data:: UP
//|
//| When the input line isn't being driven the pull up can pull the state
//| of the line high so it reads as true.
//|
//| .. data:: DOWN
//|
//| When the input line isn't being driven the pull down can pull the
//| state of the line low so it reads as false.
//|
const mp_obj_type_t nativeio_digitalinout_pull_type;
const nativeio_digitalinout_pull_obj_t nativeio_digitalinout_pull_up_obj = {
{ &nativeio_digitalinout_pull_type },
};
const nativeio_digitalinout_pull_obj_t nativeio_digitalinout_pull_down_obj = {
{ &nativeio_digitalinout_pull_type },
};
STATIC const mp_rom_map_elem_t nativeio_digitalinout_pull_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_UP), MP_ROM_PTR(&nativeio_digitalinout_pull_up_obj) },
{ MP_ROM_QSTR(MP_QSTR_DOWN), MP_ROM_PTR(&nativeio_digitalinout_pull_down_obj) },
};
STATIC MP_DEFINE_CONST_DICT(nativeio_digitalinout_pull_locals_dict, nativeio_digitalinout_pull_locals_dict_table);
STATIC void nativeio_digitalinout_pull_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
qstr pull = MP_QSTR_UP;
if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&nativeio_digitalinout_pull_down_obj)) {
pull = MP_QSTR_DOWN;
}
mp_printf(print, "%q.%q.%q.%q", MP_QSTR_nativeio, MP_QSTR_DigitalInOut, MP_QSTR_Pull, pull);
}
const mp_obj_type_t nativeio_digitalinout_pull_type = {
{ &mp_type_type },
.name = MP_QSTR_Pull,
.print = nativeio_digitalinout_pull_print,
.locals_dict = (mp_obj_t)&nativeio_digitalinout_pull_locals_dict,
};
STATIC const mp_rom_map_elem_t nativeio_digitalinout_locals_dict_table[] = {
// instance methods
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_digitalinout_deinit_obj) },
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_digitalinout_obj___exit___obj) },
{ MP_ROM_QSTR(MP_QSTR_switch_to_output), MP_ROM_PTR(&nativeio_digitalinout_switch_to_output_obj) },
{ MP_ROM_QSTR(MP_QSTR_switch_to_input), MP_ROM_PTR(&nativeio_digitalinout_switch_to_input_obj) },
// Properties
{ MP_ROM_QSTR(MP_QSTR_direction), MP_ROM_PTR(&nativeio_digitalinout_direction_obj) },
{ MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&nativeio_digitalinout_value_obj) },
{ MP_ROM_QSTR(MP_QSTR_drive_mode), MP_ROM_PTR(&nativeio_digitalinout_drive_mode_obj) },
{ MP_ROM_QSTR(MP_QSTR_pull), MP_ROM_PTR(&nativeio_digitalinout_pull_obj) },
// Nested Enum-like Classes.
{ MP_ROM_QSTR(MP_QSTR_Direction), MP_ROM_PTR(&nativeio_digitalinout_direction_type) },
{ MP_ROM_QSTR(MP_QSTR_DriveMode), MP_ROM_PTR(&nativeio_digitalinout_drive_mode_type) },
{ MP_ROM_QSTR(MP_QSTR_Pull), MP_ROM_PTR(&nativeio_digitalinout_pull_type) },
};
STATIC MP_DEFINE_CONST_DICT(nativeio_digitalinout_locals_dict, nativeio_digitalinout_locals_dict_table);
const mp_obj_type_t nativeio_digitalinout_type = {
{ &mp_type_type },
.name = MP_QSTR_DigitalInOut,
.make_new = nativeio_digitalinout_make_new,
.locals_dict = (mp_obj_t)&nativeio_digitalinout_locals_dict,
};

View File

@ -1,140 +0,0 @@
/*
* 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.
*/
// nativeio is the HAL for low-level, hardware accelerated classes. It
// is not meant to simplify APIs, its only meant to unify them so that other
// libraries do not require port specific logic.
#include <stdint.h>
#include "py/obj.h"
#include "py/runtime.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/nativeio/__init__.h"
#include "shared-bindings/nativeio/AnalogIn.h"
#include "shared-bindings/nativeio/AnalogOut.h"
#include "shared-bindings/nativeio/DigitalInOut.h"
#include "shared-bindings/nativeio/I2C.h"
#include "shared-bindings/nativeio/OneWire.h"
#include "shared-bindings/nativeio/PulseIn.h"
#include "shared-bindings/nativeio/PulseOut.h"
#include "shared-bindings/nativeio/PWMOut.h"
#include "shared-bindings/nativeio/SPI.h"
#include "shared-bindings/nativeio/TouchIn.h"
#include "shared-bindings/nativeio/UART.h"
#include "common-hal/nativeio/types.h"
#include "shared-bindings/nativeio/__init__.h"
#include "py/runtime.h"
//| :mod:`nativeio` --- Hardware accelerated behavior
//| =================================================
//|
//| .. module:: nativeio
//| :synopsis: Hardware accelerated behavior
//| :platform: SAMD21
//|
//| The `nativeio` module contains classes to provide access to IO typically
//| accelerated by hardware on the onboard microcontroller. The classes are
//| meant to align with commonly hardware accelerated IO and not necessarily
//| match up with microcontroller structure (because it varies).
//|
//| When the microcontroller does not support the behavior in a hardware
//| accelerated fashion it may internally use a bitbang routine. However, if
//| hardware support is available on a subset of pins but not those provided,
//| then a RuntimeError will be raised. Use the `bitbangio` module to explicitly
//| bitbang a protocol on any general purpose pins.
//|
//| Libraries
//|
//| .. toctree::
//| :maxdepth: 3
//|
//| AnalogIn
//| AnalogOut
//| DigitalInOut
//| I2C
//| OneWire
//| PulseIn
//| PulseOut
//| PWMOut
//| SPI
//| TouchIn
//| UART
//|
//| All libraries change hardware state and should be deinitialized when they
//| are no longer needed. To do so, either call :py:meth:`!deinit` or use a
//| context manager.
//|
//| For example::
//|
//| import nativeio
//| from board import *
//|
//| with nativeio.I2C(SCL, SDA) as i2c:
//| i2c.scan()
//|
//| This example will initialize the the device, run
//| :py:meth:`~nativeio.I2C.scan` and then :py:meth:`~nativeio.I2C.deinit` the
//| hardware.
//|
//| Here is blinky::
//|
//| import nativeio
//| from board import *
//| import time
//|
//| with nativeio.DigitalInOut(D13) as led:
//| led.switch_to_output()
//| while True:
//| led.value = True
//| time.sleep(0.1)
//| led.value = False
//| time.sleep(0.1)
//|
STATIC const mp_rom_map_elem_t nativeio_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_nativeio) },
{ MP_ROM_QSTR(MP_QSTR_AnalogIn), MP_ROM_PTR(&nativeio_analogin_type) },
{ MP_ROM_QSTR(MP_QSTR_AnalogOut), MP_ROM_PTR(&nativeio_analogout_type) },
{ MP_ROM_QSTR(MP_QSTR_DigitalInOut), MP_ROM_PTR(&nativeio_digitalinout_type) },
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&nativeio_i2c_type) },
{ MP_ROM_QSTR(MP_QSTR_OneWire), MP_ROM_PTR(&nativeio_onewire_type) },
{ MP_ROM_QSTR(MP_QSTR_PulseIn), MP_ROM_PTR(&nativeio_pulsein_type) },
{ MP_ROM_QSTR(MP_QSTR_PulseOut), MP_ROM_PTR(&nativeio_pulseout_type) },
{ MP_ROM_QSTR(MP_QSTR_PWMOut), MP_ROM_PTR(&nativeio_pwmout_type) },
{ MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&nativeio_spi_type) },
{ MP_ROM_QSTR(MP_QSTR_TouchIn), MP_ROM_PTR(&nativeio_touchin_type) },
{ MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&nativeio_uart_type) },
};
STATIC MP_DEFINE_CONST_DICT(nativeio_module_globals, nativeio_module_globals_table);
const mp_obj_module_t nativeio_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&nativeio_module_globals,
};

View File

@ -28,9 +28,7 @@
#include "py/mphal.h"
#include "py/runtime.h"
#include "common-hal/nativeio/types.h"
#include "shared-bindings/nativeio/DigitalInOut.h"
#include "shared-bindings/digitalio/DigitalInOut.h"
#include "shared-bindings/neopixel_write/__init__.h"
//| :mod:`neopixel_write` --- Low-level neopixel implementation
@ -47,15 +45,15 @@
//|
//| Write buf out on the given DigitalInOut.
//|
//| :param ~nativeio.DigitalInOut gpio: the DigitalInOut to output with
//| :param ~digitalio.DigitalInOut gpio: the DigitalInOut to output with
//| :param bytearray buf: The bytes to clock out. No assumption is made about color order
//|
STATIC mp_obj_t neopixel_write_neopixel_write_(mp_obj_t digitalinout_obj, mp_obj_t buf) {
if (!MP_OBJ_IS_TYPE(digitalinout_obj, &nativeio_digitalinout_type)) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "Expected a %q", nativeio_digitalinout_type.name));
if (!MP_OBJ_IS_TYPE(digitalinout_obj, &digitalio_digitalinout_type)) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "Expected a %q", digitalio_digitalinout_type.name));
}
// Convert parameters into expected types.
const nativeio_digitalinout_obj_t *digitalinout = MP_OBJ_TO_PTR(digitalinout_obj);
const digitalio_digitalinout_obj_t *digitalinout = MP_OBJ_TO_PTR(digitalinout_obj);
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_READ);
// Call platform's neopixel write function with provided buffer and options.

View File

@ -30,8 +30,8 @@
#include <stdint.h>
#include <stdbool.h>
#include "common-hal/nativeio/types.h"
#include "common-hal/digitalio/DigitalInOut.h"
extern void common_hal_neopixel_write(const nativeio_digitalinout_obj_t* gpio, uint8_t *pixels, uint32_t numBytes);
extern void common_hal_neopixel_write(const digitalio_digitalinout_obj_t* gpio, uint8_t *pixels, uint32_t numBytes);
#endif

View File

@ -30,9 +30,9 @@
#include "py/objproperty.h"
#include "py/runtime.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/nativeio/PWMOut.h"
#include "shared-bindings/pulseio/PWMOut.h"
//| .. currentmodule:: nativeio
//| .. currentmodule:: pulseio
//|
//| :class:`PWMOut` -- Output a Pulse Width Modulated signal
//| ========================================================
@ -57,32 +57,32 @@
//|
//| Simple LED fade::
//|
//| import nativeio
//| import pulseio
//| import board
//|
//| with nativeio.PWMOut(board.D13) as pwm: # output on D13
//| with pulseio.PWMOut(board.D13) as pwm: # output on D13
//| pwm.duty_cycle = 2 ** 15 # Cycles the pin with 50% duty cycle (half of 2 ** 16) at the default 500hz
//|
//| PWM at specific frequency (servos and motors)::
//|
//| import nativeio
//| import pulseio
//| import board
//|
//| with nativeio.PWMOut(board.D13, frequency=50) as pwm:
//| with pulseio.PWMOut(board.D13, frequency=50) as pwm:
//| pwm.duty_cycle = 2 ** 15 # Cycles the pin with 50% duty cycle (half of 2 ** 16) at 50hz
//|
//| Variable frequency (usually tones)::
//|
//| import nativeio
//| import pulseio
//| import board
//| import time
//|
//| with nativeio.PWMOut(board.D13, duty_cycle=2 ** 15, frequency=440, variable_frequency=True) as pwm:
//| with pulseio.PWMOut(board.D13, duty_cycle=2 ** 15, frequency=440, variable_frequency=True) as pwm:
//| time.sleep(0.2)
//| pwm.frequency = 880
//| time.sleep(0.1)
//|
STATIC mp_obj_t nativeio_pwmout_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
STATIC mp_obj_t pulseio_pwmout_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
mp_obj_t pin_obj = args[0];
assert_pin(pin_obj, false);
@ -90,8 +90,8 @@ STATIC mp_obj_t nativeio_pwmout_make_new(const mp_obj_type_t *type, size_t n_arg
assert_pin_free(pin);
// create PWM object from the given pin
nativeio_pwmout_obj_t *self = m_new_obj(nativeio_pwmout_obj_t);
self->base.type = &nativeio_pwmout_type;
pulseio_pwmout_obj_t *self = m_new_obj(pulseio_pwmout_obj_t);
self->base.type = &pulseio_pwmout_type;
mp_map_t kw_args;
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
@ -107,7 +107,7 @@ STATIC mp_obj_t nativeio_pwmout_make_new(const mp_obj_type_t *type, size_t n_arg
uint32_t frequency = parsed_args[ARG_frequency].u_int;
bool variable_frequency = parsed_args[ARG_variable_frequency].u_int;
common_hal_nativeio_pwmout_construct(self, pin, duty_cycle, frequency, variable_frequency);
common_hal_pulseio_pwmout_construct(self, pin, duty_cycle, frequency, variable_frequency);
return MP_OBJ_FROM_PTR(self);
}
@ -116,12 +116,12 @@ STATIC mp_obj_t nativeio_pwmout_make_new(const mp_obj_type_t *type, size_t n_arg
//|
//| Deinitialises the PWMOut and releases any hardware resources for reuse.
//|
STATIC mp_obj_t nativeio_pwmout_deinit(mp_obj_t self_in) {
nativeio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_nativeio_pwmout_deinit(self);
STATIC mp_obj_t pulseio_pwmout_deinit(mp_obj_t self_in) {
pulseio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_pulseio_pwmout_deinit(self);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pwmout_deinit_obj, nativeio_pwmout_deinit);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pwmout_deinit_obj, pulseio_pwmout_deinit);
//| .. method:: __enter__()
//|
@ -133,39 +133,39 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pwmout_deinit_obj, nativeio_pwmout_dei
//|
//| Automatically deinitializes the hardware when exiting a context.
//|
STATIC mp_obj_t nativeio_pwmout_obj___exit__(size_t n_args, const mp_obj_t *args) {
STATIC mp_obj_t pulseio_pwmout_obj___exit__(size_t n_args, const mp_obj_t *args) {
(void)n_args;
common_hal_nativeio_pwmout_deinit(args[0]);
common_hal_pulseio_pwmout_deinit(args[0]);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(nativeio_pwmout___exit___obj, 4, 4, nativeio_pwmout_obj___exit__);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pulseio_pwmout___exit___obj, 4, 4, pulseio_pwmout_obj___exit__);
//| .. attribute:: duty_cycle
//|
//| 16 bit value that dictates how much of one cycle is high (1) versus low
//| (0). 0xffff will always be high, 0 will always be low and 0x7fff will
//| be half high and then half low.
STATIC mp_obj_t nativeio_pwmout_obj_get_duty_cycle(mp_obj_t self_in) {
nativeio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in);
return MP_OBJ_NEW_SMALL_INT(common_hal_nativeio_pwmout_get_duty_cycle(self));
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);
return MP_OBJ_NEW_SMALL_INT(common_hal_pulseio_pwmout_get_duty_cycle(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pwmout_get_duty_cycle_obj, nativeio_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 nativeio_pwmout_obj_set_duty_cycle(mp_obj_t self_in, mp_obj_t duty_cycle) {
nativeio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in);
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);
mp_int_t duty = mp_obj_get_int(duty_cycle);
if (duty < 0 || duty > 0xffff) {
mp_raise_ValueError("PWM duty must be between 0 and 65536 (16 bit resolution)");
}
common_hal_nativeio_pwmout_set_duty_cycle(self, duty);
common_hal_pulseio_pwmout_set_duty_cycle(self, duty);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(nativeio_pwmout_set_duty_cycle_obj, nativeio_pwmout_obj_set_duty_cycle);
MP_DEFINE_CONST_FUN_OBJ_2(pulseio_pwmout_set_duty_cycle_obj, pulseio_pwmout_obj_set_duty_cycle);
const mp_obj_property_t nativeio_pwmout_duty_cycle_obj = {
const mp_obj_property_t pulseio_pwmout_duty_cycle_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&nativeio_pwmout_get_duty_cycle_obj,
(mp_obj_t)&nativeio_pwmout_set_duty_cycle_obj,
.proxy = {(mp_obj_t)&pulseio_pwmout_get_duty_cycle_obj,
(mp_obj_t)&pulseio_pwmout_set_duty_cycle_obj,
(mp_obj_t)&mp_const_none_obj},
};
@ -174,48 +174,48 @@ const mp_obj_property_t nativeio_pwmout_duty_cycle_obj = {
//| 32 bit value that dictates the PWM frequency in Hertz (cycles per
//| second). Only writeable when constructed with ``variable_frequency=True``.
//|
STATIC mp_obj_t nativeio_pwmout_obj_get_frequency(mp_obj_t self_in) {
nativeio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in);
return MP_OBJ_NEW_SMALL_INT(common_hal_nativeio_pwmout_get_frequency(self));
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);
return MP_OBJ_NEW_SMALL_INT(common_hal_pulseio_pwmout_get_frequency(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(nativeio_pwmout_get_frequency_obj, nativeio_pwmout_obj_get_frequency);
MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pwmout_get_frequency_obj, pulseio_pwmout_obj_get_frequency);
STATIC mp_obj_t nativeio_pwmout_obj_set_frequency(mp_obj_t self_in, mp_obj_t frequency) {
nativeio_pwmout_obj_t *self = MP_OBJ_TO_PTR(self_in);
if (!common_hal_nativeio_pwmout_get_variable_frequency(self)) {
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);
if (!common_hal_pulseio_pwmout_get_variable_frequency(self)) {
mp_raise_AttributeError(
"PWM frequency not writeable when variable_frequency is False on "
"construction.");
}
common_hal_nativeio_pwmout_set_frequency(self, mp_obj_get_int(frequency));
common_hal_pulseio_pwmout_set_frequency(self, mp_obj_get_int(frequency));
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(nativeio_pwmout_set_frequency_obj, nativeio_pwmout_obj_set_frequency);
MP_DEFINE_CONST_FUN_OBJ_2(pulseio_pwmout_set_frequency_obj, pulseio_pwmout_obj_set_frequency);
const mp_obj_property_t nativeio_pwmout_frequency_obj = {
const mp_obj_property_t pulseio_pwmout_frequency_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&nativeio_pwmout_get_frequency_obj,
(mp_obj_t)&nativeio_pwmout_set_frequency_obj,
.proxy = {(mp_obj_t)&pulseio_pwmout_get_frequency_obj,
(mp_obj_t)&pulseio_pwmout_set_frequency_obj,
(mp_obj_t)&mp_const_none_obj},
};
STATIC const mp_rom_map_elem_t nativeio_pwmout_locals_dict_table[] = {
STATIC const mp_rom_map_elem_t pulseio_pwmout_locals_dict_table[] = {
// Methods
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&nativeio_pwmout_deinit_obj) },
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&pulseio_pwmout_deinit_obj) },
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&nativeio_pwmout___exit___obj) },
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&pulseio_pwmout___exit___obj) },
// Properties
{ MP_ROM_QSTR(MP_QSTR_duty_cycle), MP_ROM_PTR(&nativeio_pwmout_duty_cycle_obj) },
{ MP_ROM_QSTR(MP_QSTR_frequency), MP_ROM_PTR(&nativeio_pwmout_frequency_obj) },
{ MP_ROM_QSTR(MP_QSTR_duty_cycle), MP_ROM_PTR(&pulseio_pwmout_duty_cycle_obj) },
{ MP_ROM_QSTR(MP_QSTR_frequency), MP_ROM_PTR(&pulseio_pwmout_frequency_obj) },
// TODO(tannewt): Add enabled to determine whether the signal is output
// without giving up the resources. Useful for IR output.
};
STATIC MP_DEFINE_CONST_DICT(nativeio_pwmout_locals_dict, nativeio_pwmout_locals_dict_table);
STATIC MP_DEFINE_CONST_DICT(pulseio_pwmout_locals_dict, pulseio_pwmout_locals_dict_table);
const mp_obj_type_t nativeio_pwmout_type = {
const mp_obj_type_t pulseio_pwmout_type = {
{ &mp_type_type },
.name = MP_QSTR_PWMOut,
.make_new = nativeio_pwmout_make_new,
.locals_dict = (mp_obj_dict_t*)&nativeio_pwmout_locals_dict,
.make_new = pulseio_pwmout_make_new,
.locals_dict = (mp_obj_dict_t*)&pulseio_pwmout_locals_dict,
};

View File

@ -24,22 +24,22 @@
* THE SOFTWARE.
*/
#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_PWMOUT_H__
#define __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_PWMOUT_H__
#ifndef __MICROPY_INCLUDED_SHARED_BINDINGS_PULSEIO_PWMOUT_H__
#define __MICROPY_INCLUDED_SHARED_BINDINGS_PULSEIO_PWMOUT_H__
#include "common-hal/microcontroller/types.h"
#include "common-hal/nativeio/types.h"
#include "common-hal/pulseio/PWMOut.h"
extern const mp_obj_type_t nativeio_pwmout_type;
extern const mp_obj_type_t pulseio_pwmout_type;
extern void common_hal_nativeio_pwmout_construct(nativeio_pwmout_obj_t* self,
extern void common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self,
const mcu_pin_obj_t* pin, uint16_t duty, uint32_t frequency,
bool variable_frequency);
extern void common_hal_nativeio_pwmout_deinit(nativeio_pwmout_obj_t* self);
extern void common_hal_nativeio_pwmout_set_duty_cycle(nativeio_pwmout_obj_t* self, uint16_t duty);
extern uint16_t common_hal_nativeio_pwmout_get_duty_cycle(nativeio_pwmout_obj_t* self);
extern void common_hal_nativeio_pwmout_set_frequency(nativeio_pwmout_obj_t* self, uint32_t frequency);
extern uint32_t common_hal_nativeio_pwmout_get_frequency(nativeio_pwmout_obj_t* self);
extern bool common_hal_nativeio_pwmout_get_variable_frequency(nativeio_pwmout_obj_t* self);
extern void common_hal_pulseio_pwmout_deinit(pulseio_pwmout_obj_t* self);
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 void common_hal_pulseio_pwmout_set_frequency(pulseio_pwmout_obj_t* self, uint32_t frequency);
extern uint32_t common_hal_pulseio_pwmout_get_frequency(pulseio_pwmout_obj_t* self);
extern bool common_hal_pulseio_pwmout_get_variable_frequency(pulseio_pwmout_obj_t* self);
#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_NATIVEIO_PWMOUT_H__
#endif // __MICROPY_INCLUDED_SHARED_BINDINGS_PULSEIO_PWMOUT_H__

Some files were not shown because too many files have changed in this diff Show More