This introduces an alternative hardware API called nativeio structured around different functions that are typically accelerated by native hardware. Its not meant to reflect the structure of the hardware.

Docs are here: http://tannewt-micropython.readthedocs.io/en/microcontroller/

It differs from upstream's machine in the following ways:

* Python API is identical across ports due to code structure. (Lives in shared-bindings)
* Focuses on abstracting common functionality (AnalogIn) and not representing structure (ADC).
* Documentation lives with code making it easy to ensure they match.
* Pin is split into references (board.D13 and microcontroller.pin.PA17) and functionality (DigitalInOut).
* All nativeio classes claim underlying hardware resources when inited on construction, support Context Managers (aka with statements) and have deinit methods which release the claimed hardware.
* All constructors take pin references rather than peripheral ids. Its up to the implementation to find hardware or throw and exception.
crypto-aes
Scott Shawcroft 7 years ago committed by Scott Shawcroft
parent 9321828158
commit ccbb5e84f9

@ -31,7 +31,6 @@ INC += -I.
INC += -I..
INC += -I../lib/mp-readline
INC += -I../lib/timeutils
INC += -Icommon-hal/modules/
INC += -Iasf_conf/
INC += -Iasf/common/boards/
INC += -Iasf/common/services/sleepmgr/
@ -79,7 +78,23 @@ CFLAGS_CORTEX_M0 = \
-msoft-float \
-mfloat-abi=soft \
-fsingle-precision-constant \
-fno-strict-aliasing \
-Wdouble-promotion \
-Wno-endif-labels \
-Wstrict-prototypes \
-Werror-implicit-function-declaration \
-Wpointer-arith \
-Wfloat-equal \
-Wundef \
-Wshadow \
-Wwrite-strings \
-Wsign-compare \
-Wmissing-format-attribute \
-Wno-deprecated-declarations \
-Wpacked \
-Wnested-externs \
-Wunreachable-code \
-Wcast-align \
-D__SAMD21G18A__ \
-DUSB_DEVICE_PRODUCT_ID=$(USB_PID) \
-DUSB_DEVICE_VENDOR_ID=$(USB_VID) \
@ -92,9 +107,15 @@ CFLAGS_CORTEX_M0 = \
-DEXTINT_CALLBACK_MODE=true \
-DUDD_ENABLE \
-DUSART_CALLBACK_MODE=true \
-DSPI_CALLBACK_MODE=false \
-DI2C_MASTER_CALLBACK_MODE=false \
-DDAC_CALLBACK_MODE=false \
-DTCC_ASYNC=false \
-DADC_CALLBACK_MODE=false \
-DTC_ASYNC=true \
-DUSB_DEVICE_LPM_SUPPORT
CFLAGS = $(INC) -Wall -Werror -ansi -std=gnu99 -nostdlib $(CFLAGS_CORTEX_M0) $(COPT)
-DUSB_DEVICE_LPM_SUPPORT \
--param max-inline-insns-single=500
CFLAGS = $(INC) -Wall -Werror -std=gnu11 -nostdlib $(CFLAGS_CORTEX_M0) $(COPT)
#Debugging/Optimization
# TODO(tannewt): Figure out what NDEBUG does. Adding it to the debug build
@ -154,17 +175,10 @@ SRC_C = \
builtin_open.c \
fatfs_port.c \
main.c \
modmachine.c \
modmachine_adc.c \
modmachine_dac.c \
modmachine_pin.c \
modmachine_pwm.c \
modneopixel_write.c \
moduos.c \
modutime.c \
mphalport.c \
pin_named_pins.c \
samdneopixel.c \
samd21_pins.c \
neopixel_status.c \
tick.c \
$(FLASH_IMPL) \
asf/common/services/sleepmgr/samd/sleepmgr.c \
@ -178,7 +192,6 @@ SRC_C = \
asf/sam0/utils/cmsis/samd21/source/gcc/startup_samd21.c \
asf/sam0/utils/cmsis/samd21/source/system_samd21.c \
asf/sam0/utils/syscalls/gcc/syscalls.c \
boards/samd21_pins.c \
boards/$(BOARD)/init.c \
boards/$(BOARD)/pins.c \
lib/fatfs/ff.c \
@ -194,12 +207,19 @@ STM_SRC_C = $(addprefix stmhal/,\
input.c \
)
# TODO(tannewt): Use this sed line to extract the RST docs from these sources:
# sed': sed -n 's+^//|++p' ../api/machine.c
#
# RST lines are prefixed with //|
SRC_BINDINGS = \
modules/machine.c
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 \
neopixel_write/__init__.c \
time/__init__.c
SRC_BINDINGS_EXPANDED = $(addprefix shared-bindings/, $(SRC_BINDINGS)) \
$(addprefix common-hal/, $(SRC_BINDINGS))

@ -31,11 +31,11 @@
extern volatile bool reset_next_character;
void autoreset_tick();
void autoreset_tick(void);
void autoreset_start();
void autoreset_stop();
void autoreset_enable();
void autoreset_disable();
void autoreset_start(void);
void autoreset_stop(void);
void autoreset_enable(void);
void autoreset_disable(void);
#endif // __MICROPY_INCLUDED_ATMEL_SAMD_AUTORESET_H__

@ -87,9 +87,9 @@ void usb_rts_notify(uint8_t port, bool set);
//! Interface callback definition
#define UDI_MSC_ENABLE_EXT() mp_msc_enable()
extern bool mp_msc_enable();
extern bool mp_msc_enable(void);
#define UDI_MSC_DISABLE_EXT() mp_msc_disable()
extern void mp_msc_disable();
extern void mp_msc_disable(void);
//! Enable id string of interface to add an extra USB string
#define UDI_MSC_STRING_ID 5

@ -7,6 +7,8 @@
#define MICROPY_HW_LED_TX PIN_PA27
#define MICROPY_HW_LED_RX PIN_PB03
#define MICROPY_HW_NEOPIXEL PIN_PB22
#define MICROPY_HW_NEOPIXEL &pin_PB22
#define AUTORESET_DELAY_MS 500
#define FLASH_INCLUDE "internal_flash.h"

@ -1,6 +1,8 @@
#include "boards/samd21_pins.h"
#include "shared-bindings/board/__init__.h"
STATIC const mp_map_elem_t pin_board_pins_locals_dict_table[] = {
#include "samd21_pins.h"
STATIC const mp_map_elem_t board_global_dict_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_A0), (mp_obj_t)&pin_PA02 },
{ MP_OBJ_NEW_QSTR(MP_QSTR_A1), (mp_obj_t)&pin_PB08 },
{ MP_OBJ_NEW_QSTR(MP_QSTR_A2), (mp_obj_t)&pin_PB09 },
@ -27,4 +29,4 @@ STATIC const mp_map_elem_t pin_board_pins_locals_dict_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_MOSI), (mp_obj_t)&pin_PB10 },
{ MP_OBJ_NEW_QSTR(MP_QSTR_MISO), (mp_obj_t)&pin_PA12 },
};
MP_DEFINE_CONST_DICT(pin_board_pins_locals_dict, pin_board_pins_locals_dict_table);
MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table);

@ -87,9 +87,9 @@ void usb_rts_notify(uint8_t port, bool set);
//! Interface callback definition
#define UDI_MSC_ENABLE_EXT() mp_msc_enable()
extern bool mp_msc_enable();
extern bool mp_msc_enable(void);
#define UDI_MSC_DISABLE_EXT() mp_msc_disable()
extern void mp_msc_disable();
extern void mp_msc_disable(void);
//! Enable id string of interface to add an extra USB string
#define UDI_MSC_STRING_ID 5

@ -7,3 +7,5 @@
#define MICROPY_HW_MCU_NAME "samd21g18"
#define AUTORESET_DELAY_MS 500
#define FLASH_INCLUDE "internal_flash.h"

@ -1,6 +1,6 @@
#include "boards/samd21_pins.h"
#include "samd21_pins.h"
STATIC const mp_map_elem_t pin_board_pins_locals_dict_table[] = {
STATIC const mp_map_elem_t board_global_dict_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_A0), (mp_obj_t)&pin_PA02 },
{ MP_OBJ_NEW_QSTR(MP_QSTR_A1), (mp_obj_t)&pin_PB08 },
{ MP_OBJ_NEW_QSTR(MP_QSTR_A2), (mp_obj_t)&pin_PB09 },
@ -23,4 +23,4 @@ STATIC const mp_map_elem_t pin_board_pins_locals_dict_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_D12), (mp_obj_t)&pin_PA19 },
{ MP_OBJ_NEW_QSTR(MP_QSTR_D13), (mp_obj_t)&pin_PA17 },
};
MP_DEFINE_CONST_DICT(pin_board_pins_locals_dict, pin_board_pins_locals_dict_table);
MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table);

@ -87,9 +87,9 @@ void usb_rts_notify(uint8_t port, bool set);
//! Interface callback definition
#define UDI_MSC_ENABLE_EXT() mp_msc_enable()
extern bool mp_msc_enable();
extern bool mp_msc_enable(void);
#define UDI_MSC_DISABLE_EXT() mp_msc_disable()
extern void mp_msc_disable();
extern void mp_msc_disable(void);
//! Enable id string of interface to add an extra USB string
#define UDI_MSC_STRING_ID 5

@ -7,3 +7,5 @@
#define MICROPY_HW_MCU_NAME "samd21g18"
#define AUTORESET_DELAY_MS 500
#define FLASH_INCLUDE "internal_flash.h"

@ -1,6 +1,6 @@
#include "boards/samd21_pins.h"
#include "samd21_pins.h"
STATIC const mp_map_elem_t pin_board_pins_locals_dict_table[] = {
STATIC const mp_map_elem_t board_global_dict_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_A0), (mp_obj_t)&pin_PA02 },
{ MP_OBJ_NEW_QSTR(MP_QSTR_A1), (mp_obj_t)&pin_PB08 },
{ MP_OBJ_NEW_QSTR(MP_QSTR_A2), (mp_obj_t)&pin_PB09 },
@ -22,4 +22,4 @@ STATIC const mp_map_elem_t pin_board_pins_locals_dict_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_D12), (mp_obj_t)&pin_PA19 },
{ MP_OBJ_NEW_QSTR(MP_QSTR_D13), (mp_obj_t)&pin_PA17 },
};
MP_DEFINE_CONST_DICT(pin_board_pins_locals_dict, pin_board_pins_locals_dict_table);
MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table);

@ -87,9 +87,9 @@ void usb_rts_notify(uint8_t port, bool set);
//! Interface callback definition
#define UDI_MSC_ENABLE_EXT() mp_msc_enable()
extern bool mp_msc_enable();
extern bool mp_msc_enable(void);
#define UDI_MSC_DISABLE_EXT() mp_msc_disable()
extern void mp_msc_disable();
extern void mp_msc_disable(void);
//! Enable id string of interface to add an extra USB string
#define UDI_MSC_STRING_ID 5

@ -19,3 +19,5 @@
#define SPI_FLASH_SERCOM SERCOM4
#define AUTORESET_DELAY_MS 500
#define FLASH_INCLUDE "spi_flash.h"

@ -87,9 +87,9 @@ void usb_rts_notify(uint8_t port, bool set);
//! Interface callback definition
#define UDI_MSC_ENABLE_EXT() mp_msc_enable()
extern bool mp_msc_enable();
extern bool mp_msc_enable(void);
#define UDI_MSC_DISABLE_EXT() mp_msc_disable()
extern void mp_msc_disable();
extern void mp_msc_disable(void);
//! Enable id string of interface to add an extra USB string
#define UDI_MSC_STRING_ID 5

@ -8,7 +8,7 @@
#define MICROPY_HW_LED_TX PIN_PA27
#define MICROPY_HW_LED_RX PIN_PB03
#define MICROPY_HW_NEOPIXEL PIN_PA30
#define MICROPY_HW_NEOPIXEL &pin_PA30
#define SPI_FLASH_BAUDRATE (1000000)
@ -33,3 +33,5 @@
#define SPI_FLASH_SERCOM SERCOM4
#define AUTORESET_DELAY_MS 500
#define FLASH_INCLUDE "spi_flash.h"

@ -1,6 +1,6 @@
#include "boards/samd21_pins.h"
#include "samd21_pins.h"
STATIC const mp_map_elem_t pin_board_pins_locals_dict_table[] = {
STATIC const mp_map_elem_t board_global_dict_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_A0), (mp_obj_t)&pin_PA02 },
{ MP_OBJ_NEW_QSTR(MP_QSTR_A1), (mp_obj_t)&pin_PB08 },
{ MP_OBJ_NEW_QSTR(MP_QSTR_A2), (mp_obj_t)&pin_PB09 },
@ -30,4 +30,4 @@ STATIC const mp_map_elem_t pin_board_pins_locals_dict_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_FLASH_MISO), (mp_obj_t)&pin_PA12 },
{ MP_OBJ_NEW_QSTR(MP_QSTR_FLASH_CS), (mp_obj_t)&pin_PA13 },
};
MP_DEFINE_CONST_DICT(pin_board_pins_locals_dict, pin_board_pins_locals_dict_table);
MP_DEFINE_CONST_DICT(board_module_globals, board_global_dict_table);

@ -1,166 +0,0 @@
#ifndef __MICROPY_INCLUDED_ATMEL_SAMD_BOARDS_SAMD21_PINS_H__
#define __MICROPY_INCLUDED_ATMEL_SAMD_BOARDS_SAMD21_PINS_H__
#include "modmachine_pin.h"
// Pins in datasheet order.
#ifdef PIN_PA00
extern const pin_obj_t pin_PA00;
#endif
#ifdef PIN_PA01
extern const pin_obj_t pin_PA01;
#endif
#ifdef PIN_PA02
extern const pin_obj_t pin_PA02;
#endif
#ifdef PIN_PA03
extern const pin_obj_t pin_PA03;
#endif
#ifdef PIN_PB04
extern const pin_obj_t pin_PB04;
#endif
#ifdef PIN_PB05
extern const pin_obj_t pin_PB05;
#endif
#ifdef PIN_PB06
extern const pin_obj_t pin_PB06;
#endif
#ifdef PIN_PB07
extern const pin_obj_t pin_PB07;
#endif
#ifdef PIN_PB08
extern const pin_obj_t pin_PB08;
#endif
#ifdef PIN_PB09
extern const pin_obj_t pin_PB09;
#endif
#ifdef PIN_PA04
extern const pin_obj_t pin_PA04;
#endif
#ifdef PIN_PA05
extern const pin_obj_t pin_PA05;
#endif
#ifdef PIN_PA06
extern const pin_obj_t pin_PA06;
#endif
#ifdef PIN_PA07
extern const pin_obj_t pin_PA07;
#endif
#ifdef PIN_PA08
extern const pin_obj_t pin_PA08;
#endif
#ifdef PIN_PA09
extern const pin_obj_t pin_PA09;
#endif
#ifdef PIN_PA10
extern const pin_obj_t pin_PA10;
#endif
#ifdef PIN_PA11
extern const pin_obj_t pin_PA11;
#endif
#ifdef PIN_PB10
extern const pin_obj_t pin_PB10;
#endif
#ifdef PIN_PB11
extern const pin_obj_t pin_PB11;
#endif
#ifdef PIN_PB12
extern const pin_obj_t pin_PB12;
#endif
#ifdef PIN_PB13
extern const pin_obj_t pin_PB13;
#endif
#ifdef PIN_PB14
extern const pin_obj_t pin_PB14;
#endif
// Second page.
#ifdef PIN_PB15
extern const pin_obj_t pin_PB15;
#endif
#ifdef PIN_PA12
extern const pin_obj_t pin_PA12;
#endif
#ifdef PIN_PA13
extern const pin_obj_t pin_PA13;
#endif
#ifdef PIN_PA14
extern const pin_obj_t pin_PA14;
#endif
#ifdef PIN_PA15
extern const pin_obj_t pin_PA15;
#endif
#ifdef PIN_PA16
extern const pin_obj_t pin_PA16;
#endif
#ifdef PIN_PA17
extern const pin_obj_t pin_PA17;
#endif
#ifdef PIN_PA18
extern const pin_obj_t pin_PA18;
#endif
#ifdef PIN_PA19
extern const pin_obj_t pin_PA19;
#endif
#ifdef PIN_PB16
extern const pin_obj_t pin_PB16;
#endif
#ifdef PIN_PB17
extern const pin_obj_t pin_PB17;
#endif
#ifdef PIN_PA20
extern const pin_obj_t pin_PA20;
#endif
#ifdef PIN_PA21
extern const pin_obj_t pin_PA21;
#endif
#ifdef PIN_PA22
extern const pin_obj_t pin_PA22;
#endif
#ifdef PIN_PA23
extern const pin_obj_t pin_PA23;
#endif
#ifdef PIN_PA24
extern const pin_obj_t pin_PA24;
#endif
#ifdef PIN_PA25
extern const pin_obj_t pin_PA25;
#endif
#ifdef PIN_PB22
extern const pin_obj_t pin_PB22;
#endif
#ifdef PIN_PB23
extern const pin_obj_t pin_PB23;
#endif
#ifdef PIN_PA27
extern const pin_obj_t pin_PA27;
#endif
#ifdef PIN_PA28
extern const pin_obj_t pin_PA28;
#endif
#ifdef PIN_PA30
extern const pin_obj_t pin_PA30;
#endif
#ifdef PIN_PA31
extern const pin_obj_t pin_PA31;
#endif
#ifdef PIN_PB30
extern const pin_obj_t pin_PB30;
#endif
#ifdef PIN_PB31
extern const pin_obj_t pin_PB31;
#endif
#ifdef PIN_PB00
extern const pin_obj_t pin_PB00;
#endif
#ifdef PIN_PB01
extern const pin_obj_t pin_PB01;
#endif
#ifdef PIN_PB02
extern const pin_obj_t pin_PB02;
#endif
#ifdef PIN_PB03
extern const pin_obj_t pin_PB03;
#endif
#endif // __MICROPY_INCLUDED_ATMEL_SAMD_BOARDS_SAMD21_PINS_H__

@ -24,4 +24,11 @@
* THE SOFTWARE.
*/
extern const mp_obj_type_t dac_type;
#include <string.h>
#include "py/runtime.h"
#include "py/mphal.h"
#include "common-hal/microcontroller/types.h"
// Pins aren't actually defined here. They are in the board specific directory
// such as boards/arduino_zero/pins.c.

@ -0,0 +1 @@
// Pins have no behavior.

@ -0,0 +1,197 @@
/*
* 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 "py/mphal.h"
#include "samd21_pins.h"
void common_hal_mcu_delay_us(uint32_t delay) {
mp_hal_delay_us(delay);
}
// This maps MCU pin names to pin objects.
STATIC const mp_map_elem_t mcu_pin_global_dict_table[] = {
// Pins in datasheet order.
#ifdef PIN_PA00
{ MP_OBJ_NEW_QSTR(MP_QSTR_PA00), (mp_obj_t)&pin_PA00 },
#endif
#ifdef PIN_PA01
{ MP_OBJ_NEW_QSTR(MP_QSTR_PA01), (mp_obj_t)&pin_PA01 },
#endif
#ifdef PIN_PA02
{ MP_OBJ_NEW_QSTR(MP_QSTR_PA02), (mp_obj_t)&pin_PA02 },
#endif
#ifdef PIN_PA03
{ MP_OBJ_NEW_QSTR(MP_QSTR_PA03), (mp_obj_t)&pin_PA03 },
#endif
#ifdef PIN_PB04
{ MP_OBJ_NEW_QSTR(MP_QSTR_PB04), (mp_obj_t)&pin_PB04 },
#endif
#ifdef PIN_PB05
{ MP_OBJ_NEW_QSTR(MP_QSTR_PB05), (mp_obj_t)&pin_PB05 },
#endif
#ifdef PIN_PB06
{ MP_OBJ_NEW_QSTR(MP_QSTR_PB06), (mp_obj_t)&pin_PB06 },
#endif
#ifdef PIN_PB07
{ MP_OBJ_NEW_QSTR(MP_QSTR_PB07), (mp_obj_t)&pin_PB07 },
#endif
#ifdef PIN_PB08
{ MP_OBJ_NEW_QSTR(MP_QSTR_PB08), (mp_obj_t)&pin_PB08 },
#endif
#ifdef PIN_PB09
{ MP_OBJ_NEW_QSTR(MP_QSTR_PB09), (mp_obj_t)&pin_PB09 },
#endif
#ifdef PIN_PA04
{ MP_OBJ_NEW_QSTR(MP_QSTR_PA04), (mp_obj_t)&pin_PA04 },
#endif
#ifdef PIN_PA05
{ MP_OBJ_NEW_QSTR(MP_QSTR_PA05), (mp_obj_t)&pin_PA05 },
#endif
#ifdef PIN_PA06
{ MP_OBJ_NEW_QSTR(MP_QSTR_PA06), (mp_obj_t)&pin_PA06 },
#endif
#ifdef PIN_PA07
{ MP_OBJ_NEW_QSTR(MP_QSTR_PA07), (mp_obj_t)&pin_PA07 },
#endif
#ifdef PIN_PA08
{ MP_OBJ_NEW_QSTR(MP_QSTR_PA08), (mp_obj_t)&pin_PA08 },
#endif
#ifdef PIN_PA09
{ MP_OBJ_NEW_QSTR(MP_QSTR_PA09), (mp_obj_t)&pin_PA09 },
#endif
#ifdef PIN_PA10
{ MP_OBJ_NEW_QSTR(MP_QSTR_PA10), (mp_obj_t)&pin_PA10 },
#endif
#ifdef PIN_PA11
{ MP_OBJ_NEW_QSTR(MP_QSTR_PA11), (mp_obj_t)&pin_PA11 },
#endif
#ifdef PIN_PB10
{ MP_OBJ_NEW_QSTR(MP_QSTR_PB10), (mp_obj_t)&pin_PB10 },
#endif
#ifdef PIN_PB11
{ MP_OBJ_NEW_QSTR(MP_QSTR_PB11), (mp_obj_t)&pin_PB11 },
#endif
#ifdef PIN_PB12
{ MP_OBJ_NEW_QSTR(MP_QSTR_PB12), (mp_obj_t)&pin_PB12 },
#endif
#ifdef PIN_PB13
{ MP_OBJ_NEW_QSTR(MP_QSTR_PB13), (mp_obj_t)&pin_PB13 },
#endif
#ifdef PIN_PB14
{ MP_OBJ_NEW_QSTR(MP_QSTR_PB14), (mp_obj_t)&pin_PB14 },
#endif
// Second page.
#ifdef PIN_PB15
{ MP_OBJ_NEW_QSTR(MP_QSTR_PB15), (mp_obj_t)&pin_PB15 },
#endif
#ifdef PIN_PA12
{ MP_OBJ_NEW_QSTR(MP_QSTR_PA12), (mp_obj_t)&pin_PA12 },
#endif
#ifdef PIN_PA13
{ MP_OBJ_NEW_QSTR(MP_QSTR_PA13), (mp_obj_t)&pin_PA13 },
#endif
#ifdef PIN_PA14
{ MP_OBJ_NEW_QSTR(MP_QSTR_PA14), (mp_obj_t)&pin_PA14 },
#endif
#ifdef PIN_PA15
{ MP_OBJ_NEW_QSTR(MP_QSTR_PA15), (mp_obj_t)&pin_PA15 },
#endif
#ifdef PIN_PA16
{ MP_OBJ_NEW_QSTR(MP_QSTR_PA16), (mp_obj_t)&pin_PA16 },
#endif
#ifdef PIN_PA17
{ MP_OBJ_NEW_QSTR(MP_QSTR_PA17), (mp_obj_t)&pin_PA17 },
#endif
#ifdef PIN_PA18
{ MP_OBJ_NEW_QSTR(MP_QSTR_PA18), (mp_obj_t)&pin_PA18 },
#endif
#ifdef PIN_PA19
{ MP_OBJ_NEW_QSTR(MP_QSTR_PA19), (mp_obj_t)&pin_PA19 },
#endif
#ifdef PIN_PB16
{ MP_OBJ_NEW_QSTR(MP_QSTR_PB16), (mp_obj_t)&pin_PB16 },
#endif
#ifdef PIN_PB17
{ MP_OBJ_NEW_QSTR(MP_QSTR_PB17), (mp_obj_t)&pin_PB17 },
#endif
#ifdef PIN_PA20
{ MP_OBJ_NEW_QSTR(MP_QSTR_PA20), (mp_obj_t)&pin_PA20 },
#endif
#ifdef PIN_PA21
{ MP_OBJ_NEW_QSTR(MP_QSTR_PA21), (mp_obj_t)&pin_PA21 },
#endif
#ifdef PIN_PA22
{ MP_OBJ_NEW_QSTR(MP_QSTR_PA22), (mp_obj_t)&pin_PA22 },
#endif
#ifdef PIN_PA23
{ MP_OBJ_NEW_QSTR(MP_QSTR_PA23), (mp_obj_t)&pin_PA23 },
#endif
#ifdef PIN_PA24
{ MP_OBJ_NEW_QSTR(MP_QSTR_PA24), (mp_obj_t)&pin_PA24 },
#endif
#ifdef PIN_PA25
{ MP_OBJ_NEW_QSTR(MP_QSTR_PA25), (mp_obj_t)&pin_PA25 },
#endif
#ifdef PIN_PB22
{ MP_OBJ_NEW_QSTR(MP_QSTR_PB22), (mp_obj_t)&pin_PB22 },
#endif
#ifdef PIN_PB23
{ MP_OBJ_NEW_QSTR(MP_QSTR_PB23), (mp_obj_t)&pin_PB23 },
#endif
#ifdef PIN_PA27
{ MP_OBJ_NEW_QSTR(MP_QSTR_PA27), (mp_obj_t)&pin_PA27 },
#endif
#ifdef PIN_PA28
{ MP_OBJ_NEW_QSTR(MP_QSTR_PA28), (mp_obj_t)&pin_PA28 },
#endif
#ifdef PIN_PA30
{ MP_OBJ_NEW_QSTR(MP_QSTR_PA30), (mp_obj_t)&pin_PA30 },
#endif
#ifdef PIN_PA31
{ MP_OBJ_NEW_QSTR(MP_QSTR_PA31), (mp_obj_t)&pin_PA31 },
#endif
#ifdef PIN_PB30
{ MP_OBJ_NEW_QSTR(MP_QSTR_PB30), (mp_obj_t)&pin_PB30 },
#endif
#ifdef PIN_PB31
{ MP_OBJ_NEW_QSTR(MP_QSTR_PB31), (mp_obj_t)&pin_PB31 },
#endif
#ifdef PIN_PB00
{ MP_OBJ_NEW_QSTR(MP_QSTR_PB00), (mp_obj_t)&pin_PB00 },
#endif
#ifdef PIN_PB01
{ MP_OBJ_NEW_QSTR(MP_QSTR_PB01), (mp_obj_t)&pin_PB01 },
#endif
#ifdef PIN_PB02
{ MP_OBJ_NEW_QSTR(MP_QSTR_PB02), (mp_obj_t)&pin_PB02 },
#endif
#ifdef PIN_PB03
{ MP_OBJ_NEW_QSTR(MP_QSTR_PB03), (mp_obj_t)&pin_PB03 }
#endif
};
MP_DEFINE_CONST_DICT(mcu_pin_globals, mcu_pin_global_dict_table);

@ -24,25 +24,18 @@
* THE SOFTWARE.
*/
// Machine is the HAL for low-level, hardware accelerated functions. It is not
// meant to simplify APIs, its only meant to unify them so that other modules
// do not require port specific logic.
//
// This file defines core data structures for machine classes. They are port
// specific and passed through the Python layer untouched.
#ifndef __MICROPY_INCLUDED_ATMEL_SAMD_API_MACHINE_TYPES_H__
#define __MICROPY_INCLUDED_ATMEL_SAMD_API_MACHINE_TYPES_H__
#ifndef __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_MICROCONTROLLER_TYPES_H__
#define __MICROPY_INCLUDED_ATMEL_SAMD_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/adc/adc_sam_d_r/adc_feature.h" // for adc_positive_input
#include "asf/sam0/drivers/sercom/i2c/i2c_master.h"
#include "asf/sam0/drivers/sercom/spi/spi.h"
#include "asf/sam0/drivers/tc/tc.h"
#include "asf/sam0/drivers/tcc/tcc.h"
#include "py/obj.h"
@ -63,24 +56,14 @@ typedef struct {
#define NUM_SERCOMS_PER_PIN 2
typedef struct {
mp_obj_base_t base;
qstr name;
uint32_t pin;
bool has_adc;
enum adc_positive_input adc_input;
pin_timer_t primary_timer;
pin_timer_t secondary_timer;
pin_sercom_t sercom[NUM_SERCOMS_PER_PIN];
} pin_obj_t;
typedef struct _machine_i2c_obj_t {
mp_obj_base_t base;
struct i2c_master_module i2c_master_instance;
} machine_i2c_obj_t;
typedef struct _machine_spi_obj_t {
mp_obj_base_t base;
struct spi_module spi_master_instance;
} machine_spi_obj_t;
mp_obj_base_t base;
qstr name;
uint32_t pin;
bool has_adc;
enum adc_positive_input adc_input;
pin_timer_t primary_timer;
pin_timer_t secondary_timer;
pin_sercom_t sercom[NUM_SERCOMS_PER_PIN];
} mcu_pin_obj_t;
#endif // __MICROPY_INCLUDED_ATMEL_SAMD_API_MACHINE_TYPES_H__
#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_MICROCONTROLLER_TYPES_H__

@ -1,317 +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 file contains all of the port specific HAL functions for the machine
// module.
#include "shared-bindings/modules/machine.h"
#include "py/nlr.h"
#include "asf/sam0/drivers/sercom/i2c/i2c_master.h"
// We use ENABLE registers below we don't want to treat as a macro.
#undef ENABLE
// Number of times to try to send packet if failed.
#define TIMEOUT 1
void mp_hal_i2c_construct(machine_i2c_obj_t *self, const pin_obj_t* scl,
const pin_obj_t* sda, uint32_t freq) {
struct i2c_master_config config_i2c_master;
i2c_master_get_config_defaults(&config_i2c_master);
// Struct takes the argument in Khz not Hz.
config_i2c_master.baud_rate = freq / 1000;
Sercom* sercom = NULL;
uint32_t sda_pinmux = 0;
uint32_t scl_pinmux = 0;
for (int i = 0; i < NUM_SERCOMS_PER_PIN; i++) {
Sercom* potential_sercom = sda->sercom[i].sercom;
if (potential_sercom == NULL ||
potential_sercom->I2CM.CTRLA.bit.ENABLE != 0 ||
sda->sercom[i].pad != 0) {
continue;
}
sda_pinmux = sda->sercom[i].pinmux;
for (int j = 0; j < NUM_SERCOMS_PER_PIN; j++) {
if (potential_sercom == scl->sercom[j].sercom &&
scl->sercom[j].pad == 1) {
scl_pinmux = scl->sercom[j].pinmux;
sercom = potential_sercom;
break;
}
}
if (sercom != NULL) {
break;
}
}
if (sercom == NULL) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError,
"No hardware support available with those pins."));
}
config_i2c_master.pinmux_pad0 = sda_pinmux; // SDA
config_i2c_master.pinmux_pad1 = scl_pinmux; // SCL
config_i2c_master.buffer_timeout = 10000;
enum status_code status = i2c_master_init(&self->i2c_master_instance,
sercom, &config_i2c_master);
if (status != STATUS_OK) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "I2C bus init error"));
}
i2c_master_enable(&self->i2c_master_instance);
}
void mp_hal_i2c_deinit(machine_i2c_obj_t *self) {
i2c_master_disable(&self->i2c_master_instance);
}
void mp_hal_i2c_write(machine_i2c_obj_t *self, uint8_t addr, uint8_t *data,
size_t len) {
struct i2c_master_packet packet = {
.address = addr,
.data_length = len,
.data = data,
.ten_bit_address = false,
.high_speed = false,
.hs_master_code = 0x0,
};
uint16_t timeout = 0;
enum status_code status = STATUS_BUSY;
while (status != STATUS_OK) {
status = i2c_master_write_packet_wait(&self->i2c_master_instance,
&packet);
/* Increment timeout counter and check if timed out. */
if (timeout++ == TIMEOUT) {
break;
}
}
if (status != STATUS_OK) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "I2C bus error"));
}
}
bool mp_hal_i2c_probe(machine_i2c_obj_t *self, uint8_t addr) {
uint8_t buf;
struct i2c_master_packet packet = {
.address = addr,
.data_length = 0,
.data = &buf,
.ten_bit_address = false,
.high_speed = false,
.hs_master_code = 0x0,
};
enum status_code status = i2c_master_write_packet_wait(
&self->i2c_master_instance, &packet);
return status == STATUS_OK;
}
void mp_hal_i2c_read(machine_i2c_obj_t *self, uint8_t addr, uint8_t *data,
size_t len) {
struct i2c_master_packet packet = {
.address = addr,
.data_length = len,
.data = data,
.ten_bit_address = false,
.high_speed = false,
.hs_master_code = 0x0,
};
uint16_t timeout = 0;
enum status_code status = STATUS_BUSY;
while (status != STATUS_OK) {
status = i2c_master_read_packet_wait(&self->i2c_master_instance,
&packet);
/* Increment timeout counter and check if timed out. */
if (timeout++ == TIMEOUT) {
break;
}
}
if (status != STATUS_OK) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "I2C bus error"));
}
}
void mp_hal_i2c_write_mem(machine_i2c_obj_t *self, uint8_t addr,
uint16_t memaddr, const uint8_t *src, size_t len) {
uint8_t buffer[len+1];
buffer[0] = (uint8_t) memaddr;
for (int i = 0; i < len; i++) {
buffer[i+1] = src[i];
}
struct i2c_master_packet packet = {
.address = addr,
.data_length = len + 1,
.data = buffer,
.ten_bit_address = false,
.high_speed = false,
.hs_master_code = 0x0,
};
uint16_t timeout = 0;
enum status_code status = STATUS_BUSY;
while (status != STATUS_OK) {
status = i2c_master_write_packet_wait(&self->i2c_master_instance,
&packet);
/* Increment timeout counter and check if timed out. */
if (timeout++ == TIMEOUT) {
break;
}
}
if (status != STATUS_OK) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "I2C bus error"));
}
}
void mp_hal_i2c_read_mem(machine_i2c_obj_t *self, uint8_t addr, uint16_t memaddr, uint8_t *dest, size_t len) {
// Write the memory address.
struct i2c_master_packet packet = {
.address = addr,
.data_length = 1,
.data = (uint8_t *)&memaddr,
.ten_bit_address = false,
.high_speed = false,
.hs_master_code = 0x0,
};
uint16_t timeout = 0;
enum status_code status = STATUS_BUSY;
while (status != STATUS_OK) {
status = i2c_master_write_packet_wait_no_stop(
&self->i2c_master_instance, &packet);
/* Increment timeout counter and check if timed out. */
if (timeout++ == TIMEOUT) {
break;
}
}
if (status != STATUS_OK) {
i2c_master_send_stop(&self->i2c_master_instance);
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "I2C bus error"));
}
// i2c_read will do a repeated start, and then read the I2C memory
mp_hal_i2c_read(self, addr, dest, len);
return;
}
void mp_hal_spi_construct(machine_spi_obj_t *self, const pin_obj_t * clock,
const pin_obj_t * mosi, const pin_obj_t * miso,
uint32_t baudrate) {
struct spi_config config_spi_master;
spi_get_config_defaults(&config_spi_master);
Sercom* sercom = NULL;
uint32_t clock_pinmux = 0;
uint32_t mosi_pinmux = 0;
uint32_t miso_pinmux = 0;
uint8_t clock_pad = 0;
uint8_t mosi_pad = 0;
uint8_t miso_pad = 0;
for (int i = 0; i < NUM_SERCOMS_PER_PIN; i++) {
Sercom* potential_sercom = clock->sercom[i].sercom;
if (potential_sercom == NULL ||
potential_sercom->SPI.CTRLA.bit.ENABLE != 0) {
continue;
}
clock_pinmux = clock->sercom[i].pinmux;
clock_pad = clock->sercom[i].pad;
for (int j = 0; j < NUM_SERCOMS_PER_PIN; j++) {
mosi_pinmux = mosi->sercom[j].pinmux;
mosi_pad = mosi->sercom[j].pad;
for (int k = 0; k < NUM_SERCOMS_PER_PIN; k++) {
if (potential_sercom == miso->sercom[k].sercom) {
miso_pinmux = miso->sercom[k].pinmux;
miso_pad = miso->sercom[k].pad;
sercom = potential_sercom;
break;
}
}
if (sercom != NULL) {
break;
}
}
if (sercom != NULL) {
break;
}
}
if (sercom == NULL) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError,
"No hardware support available with those pins."));
}
// Depends on where MOSI and CLK are.
uint8_t dopo = 8;
if (clock_pad == 1) {
if (mosi_pad == 0) {
dopo = 0;
} else if (mosi_pad == 3) {
dopo = 2;
}
} else if (clock_pad == 3) {
if (mosi_pad == 0) {
dopo = 3;
} else if (mosi_pad == 2) {
dopo = 1;
}
}
if (dopo == 8) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "SPI MOSI and clock pins incompatible."));
}
config_spi_master.mux_setting = (dopo << SERCOM_SPI_CTRLA_DOPO_Pos) |
(miso_pad << SERCOM_SPI_CTRLA_DIPO_Pos);
// Map pad to pinmux through a short array.
uint32_t *pinmuxes[4] = {&config_spi_master.pinmux_pad0,
&config_spi_master.pinmux_pad1,
&config_spi_master.pinmux_pad2,
&config_spi_master.pinmux_pad3};
*pinmuxes[clock_pad] = clock_pinmux;
*pinmuxes[mosi_pad] = mosi_pinmux;
*pinmuxes[miso_pad] = miso_pinmux;
config_spi_master.mode_specific.master.baudrate = baudrate;
spi_init(&self->spi_master_instance, sercom, &config_spi_master);
spi_enable(&self->spi_master_instance);
}
void mp_hal_spi_deinit(machine_spi_obj_t *self) {
spi_disable(&self->spi_master_instance);
}
void mp_hal_spi_transfer(machine_spi_obj_t *self, size_t len, const uint8_t *src,
uint8_t *dest) {
// TODO(tannewt): Don't cast away the const. Change ASF to respect it instead.
enum status_code status = spi_transceive_buffer_wait(
&self->spi_master_instance, (uint8_t *) src, dest, len);
if (status != STATUS_OK) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "SPI bus error"));
}
}

@ -0,0 +1,76 @@
/*
* 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/AnalogIn.h"
#include "asf/sam0/drivers/adc/adc.h"
void common_hal_nativeio_analogin_construct(nativeio_analogin_obj_t* self,
const mcu_pin_obj_t *pin) {
if (!pin->has_adc) {
// No ADC function on that pin
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "pin %q does not have ADC capabilities", pin->name));
}
self->pin = pin;
struct adc_config config_adc;
adc_get_config_defaults(&config_adc);
config_adc.positive_input = self->pin->adc_input;
config_adc.resolution = ADC_RESOLUTION_CUSTOM;
config_adc.accumulate_samples = ADC_ACCUMULATE_SAMPLES_16;
config_adc.divide_result = ADC_DIVIDE_RESULT_16;
config_adc.clock_prescaler = ADC_CLOCK_PRESCALER_DIV128;
adc_init(&self->adc_instance, ADC, &config_adc);
}
// TODO(tannewt): Don't turn it all on just for one read. This simplifies
// handling of reading multiple inputs and surviving sleep though so for now its
// ok.
uint16_t common_hal_nativeio_analogin_get_value(nativeio_analogin_obj_t *self) {
adc_enable(&self->adc_instance);
adc_start_conversion(&self->adc_instance);
uint16_t data;
enum status_code status = adc_read(&self->adc_instance, &data);
while (status == STATUS_BUSY) {
status = adc_read(&self->adc_instance, &data);
}
if (status == STATUS_ERR_OVERFLOW) {
// TODO(tannewt): Throw an error.
}
adc_disable(&self->adc_instance);
return data;
}

@ -0,0 +1,69 @@
/*
* 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 "py/runtime.h"
#include "shared-bindings/nativeio/AnalogOut.h"
#include "asf/sam0/drivers/dac/dac.h"
void common_hal_nativeio_analogout_construct(nativeio_analogout_obj_t* self,
const mcu_pin_obj_t *pin) {
if (pin->pin != PIN_PA02) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError,
"DAC only supported on pin PA02."));
return;
}
struct dac_config config_dac;
dac_get_config_defaults(&config_dac);
config_dac.reference = DAC_REFERENCE_AVCC;
enum status_code status = dac_init(&self->dac_instance, DAC, &config_dac);
if (status != STATUS_OK) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError,
"DAC init failed."));
return;
}
struct dac_chan_config config_analogout_chan;
dac_chan_get_config_defaults(&config_analogout_chan);
dac_chan_set_config(&self->dac_instance, DAC_CHANNEL_0, &config_analogout_chan);
dac_chan_enable(&self->dac_instance, DAC_CHANNEL_0);
dac_enable(&self->dac_instance);
}
void common_hal_nativeio_analogout_deinit(nativeio_analogout_obj_t *self) {
dac_disable(&self->dac_instance);
dac_chan_disable(&self->dac_instance, DAC_CHANNEL_0);
}
void common_hal_nativeio_analogout_set_value(nativeio_analogout_obj_t *self,
uint16_t value) {
dac_chan_write(&self->dac_instance, DAC_CHANNEL_0, value);
}

@ -0,0 +1,186 @@
/*
* 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 "py/nlr.h"
#include "py/runtime.h"
#include "py/mphal.h"
#include "shared-bindings/nativeio/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) {
self->pin = pin;
struct port_config pin_conf;
port_get_config_defaults(&pin_conf);
pin_conf.direction = PORT_PIN_DIR_INPUT;
pin_conf.input_pull = PORT_PIN_PULL_NONE;
port_pin_set_config(self->pin->pin, &pin_conf);
return DIGITALINOUT_OK;
}
void common_hal_nativeio_digitalinout_deinit(nativeio_digitalinout_obj_t* self) {
struct port_config pin_conf;
port_get_config_defaults(&pin_conf);
pin_conf.powersave = true;
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) {
self->output = false;
common_hal_nativeio_digitalinout_set_pull(self, pull);
}
void common_hal_nativeio_digitalinout_switch_to_output(
nativeio_digitalinout_obj_t* self, bool value,
enum digitalinout_drive_mode_t drive_mode) {
struct port_config pin_conf;
port_get_config_defaults(&pin_conf);
pin_conf.direction = PORT_PIN_DIR_INPUT;
pin_conf.input_pull = PORT_PIN_PULL_NONE;
port_pin_set_config(self->pin->pin, &pin_conf);
self->output = true;
self->open_drain = drive_mode == DRIVE_MODE_OPEN_DRAIN;
common_hal_nativeio_digitalinout_set_value(self, value);
}
enum digitalinout_direction_t common_hal_nativeio_digitalinout_get_direction(
nativeio_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) {
uint32_t pin = self->pin->pin;
PortGroup *const port_base = port_get_group_from_gpio_pin(pin);
uint32_t pin_mask = (1UL << (pin % 32));
/* Set the pin to high or low atomically based on the requested level */
if (value) {
if (self->open_drain) {
port_base->DIRCLR.reg = pin_mask;
} else {
port_base->DIRSET.reg = pin_mask;
port_base->OUTSET.reg = pin_mask;
}
} else {
if (!self->open_drain) {
port_base->DIRSET.reg = pin_mask;
}
port_base->OUTCLR.reg = pin_mask;
}
}
bool common_hal_nativeio_digitalinout_get_value(
nativeio_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));
if (!self->output) {
return (port_base->IN.reg & pin_mask);
} else {
if (self->open_drain && (port_base->DIR.reg & pin_mask) == 0) {
return true;
} else {
return (port_base->OUT.reg & pin_mask);
}
}
}
void common_hal_nativeio_digitalinout_set_drive_mode(
nativeio_digitalinout_obj_t* self,
enum digitalinout_drive_mode_t drive_mode) {
bool value = common_hal_nativeio_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);
}
}
enum digitalinout_drive_mode_t common_hal_nativeio_digitalinout_get_drive_mode(
nativeio_digitalinout_obj_t* self) {
if (self->open_drain) {
return DRIVE_MODE_OPEN_DRAIN;
} else {
return DRIVE_MODE_PUSH_PULL;
}
}
void common_hal_nativeio_digitalinout_set_pull(
nativeio_digitalinout_obj_t* self, enum digitalinout_pull_t pull) {
enum port_pin_pull asf_pull = PORT_PIN_PULL_NONE;
switch (pull) {
case PULL_UP:
asf_pull = PORT_PIN_PULL_UP;
break;
case PULL_DOWN:
asf_pull = PORT_PIN_PULL_DOWN;
break;
case PULL_NONE:
default:
break;
}
struct port_config pin_conf;
port_get_config_defaults(&pin_conf);
pin_conf.direction = PORT_PIN_DIR_INPUT;
pin_conf.input_pull = asf_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) {
uint32_t pin = self->pin->pin;
PortGroup *const port_base = port_get_group_from_gpio_pin(pin);
uint32_t pin_mask = (1UL << (pin % 32));
if (self->output) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError,
"Cannot get pull while in output mode."));
return PULL_NONE;
} else {
if (port_base->PINCFG[pin % 32].bit.PULLEN != 0) {
return PULL_NONE;
} if ((port_base->OUT.reg & pin_mask) > 0) {
return PULL_UP;
} else {
return PULL_DOWN;
}
}
}

@ -0,0 +1,159 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (