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
parent
9321828158
commit
ccbb5e84f9
@ -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__
|
@ -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);
|
@ -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 ( |