Skip to content

Commit

Permalink
Split up nativeio.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tannewt committed Apr 10, 2017
1 parent 4810722 commit f28f8ba
Show file tree
Hide file tree
Showing 120 changed files with 2,690 additions and 1,909 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
41 changes: 21 additions & 20 deletions atmel-samd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)) \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* THE SOFTWARE.
*/

#include "common-hal/nativeio/AnalogIn.h"
#include "common-hal/analogio/AnalogIn.h"

#include <string.h>

Expand All @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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;
}
49 changes: 49 additions & 0 deletions atmel-samd/common-hal/analogio/AnalogIn.h
Original file line number Diff line number Diff line change
@@ -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__
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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);
Expand Down
41 changes: 41 additions & 0 deletions atmel-samd/common-hal/analogio/AnalogOut.h
Original file line number Diff line number Diff line change
@@ -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__
1 change: 1 addition & 0 deletions atmel-samd/common-hal/analogio/__init__.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// No analogio module functions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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);
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
43 changes: 43 additions & 0 deletions atmel-samd/common-hal/busio/I2C.h
Original file line number Diff line number Diff line change
@@ -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__
Loading

0 comments on commit f28f8ba

Please sign in to comment.