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.
crypto-aes
Scott Shawcroft 6 years ago
parent 4810722064
commit f28f8ba568

@ -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.

@ -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)) \

@ -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;
}

@ -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__

@ -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);

@ -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__

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

@ -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,

@ -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__

@ -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__

@ -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;

@ -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__

@ -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;
}

@ -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__

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

@ -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));

@ -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__

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

@ -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__

@ -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;
}

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

@ -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__

@ -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;

@ -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;
}

@ -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__

@ -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);
}