Skip to content

Commit 0ae3448

Browse files
committed
atmel-samd & esp8266: Make sure pins are not already in use.
This prevents corrupting previous functional objects by stealing their pins out from under them. It prevents this by ensuring that pins are in default state before claiming them. It also verifies pins are released correctly and reset on soft reset. Fixes adafruit#4, instantiating a second class will fail. Fixes adafruit#29, pins are now reset too.
1 parent bb9c751 commit 0ae3448

35 files changed

Lines changed: 322 additions & 15 deletions

File tree

atmel-samd/boards/arduino_zero/mpconfigboard.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
#define MICROPY_HW_LED_TX PIN_PA27
88
#define MICROPY_HW_LED_RX PIN_PB03
99

10+
#define MICROPY_PORT_A (PORT_PA24 | PORT_PA25 | PORT_PA27)
11+
#define MICROPY_PORT_B (PORT_PB03)
12+
1013
#define AUTORESET_DELAY_MS 500
1114

1215
#include "internal_flash.h"

atmel-samd/boards/cplay_m0_flash/mpconfigboard.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
#define SPI_FLASH_CS PIN_PB08
1818
#define SPI_FLASH_SERCOM SERCOM4
1919

20+
#define MICROPY_PORT_A (PORT_PA12 | PORT_PA24 | PORT_PA25)
21+
#define MICROPY_PORT_B (PORT_PB08 | PORT_PB10 | PORT_PB11)
22+
2023
#define AUTORESET_DELAY_MS 500
2124

2225
#include "spi_flash.h"
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// LEDs
2-
#define MICROPY_HW_LED_MSC PIN_PA17 // red
2+
//#define MICROPY_HW_LED_MSC PIN_PA17 // red
33
// #define UART_REPL
44
#define USB_REPL
55

@@ -8,6 +8,9 @@
88

99
#define AUTORESET_DELAY_MS 500
1010

11+
#define MICROPY_PORT_A (PORT_PA24 | PORT_PA25)
12+
#define MICROPY_PORT_B (0)
13+
1114
#include "internal_flash.h"
1215

1316
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - 0x010000)
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// LEDs
2-
#define MICROPY_HW_LED_MSC PIN_PA17 // red
2+
//#define MICROPY_HW_LED_MSC PIN_PA17 // red
33
// #define UART_REPL
44
#define USB_REPL
55

@@ -8,6 +8,9 @@
88

99
#define AUTORESET_DELAY_MS 500
1010

11+
#define MICROPY_PORT_A (PORT_PA24 | PORT_PA25)
12+
#define MICROPY_PORT_B (0)
13+
1114
#include "internal_flash.h"
1215

1316
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - 0x010000)

atmel-samd/boards/feather_m0_flash/mpconfigboard.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
#define SPI_FLASH_CS PIN_PA13
1919
#define SPI_FLASH_SERCOM SERCOM4
2020

21+
#define MICROPY_PORT_A (PORT_PA12 | PORT_PA13 | PORT_PA14 | PORT_PA24 | PORT_PA25)
22+
#define MICROPY_PORT_B (PORT_PB10 | PORT_PB11)
23+
2124
#define AUTORESET_DELAY_MS 500
2225

2326
#include "spi_flash.h"

atmel-samd/boards/gemma_m0/mpconfigboard.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
#define MICROPY_HW_BOARD_NAME "Adafruit Gemma M0 (Experimental)"
44
#define MICROPY_HW_MCU_NAME "samd21e18"
55

6-
#define MICROPY_HW_APA102_MOSI &pin_PA04
7-
#define MICROPY_HW_APA102_SCK &pin_PA05
6+
#define MICROPY_HW_APA102_SERCOM SERCOM0
7+
#define MICROPY_HW_APA102_MOSI &pin_PA04
8+
#define MICROPY_HW_APA102_SCK &pin_PA05
9+
10+
#define MICROPY_PORT_A (PORT_PA04 | PORT_PA05 | PORT_PA24 | PORT_PA25)
11+
#define MICROPY_PORT_B (0)
812

913
#define AUTORESET_DELAY_MS 500
1014

atmel-samd/boards/metro_m0_flash/mpconfigboard.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
#define SPI_FLASH_CS PIN_PA13
3333
#define SPI_FLASH_SERCOM SERCOM4
3434

35+
#define MICROPY_PORT_A (PORT_PA12 | PORT_PA13 |PORT_PA24 | PORT_PA25 | PORT_PA27 | PORT_PA30)
36+
#define MICROPY_PORT_B (PORT_PB03 | PORT_PB10 | PORT_PB11)
37+
3538
#define AUTORESET_DELAY_MS 500
3639

3740
#include "spi_flash.h"

atmel-samd/boards/trinket_m0/mpconfigboard.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
#define MICROPY_HW_BOARD_NAME "Adafruit Trinket M0 (Experimental)"
44
#define MICROPY_HW_MCU_NAME "samd21e18"
55

6-
#define MICROPY_HW_APA102_MOSI &pin_PA04
7-
#define MICROPY_HW_APA102_SCK &pin_PA05
6+
#define MICROPY_HW_APA102_SERCOM SERCOM0
7+
#define MICROPY_HW_APA102_MOSI &pin_PA04
8+
#define MICROPY_HW_APA102_SCK &pin_PA05
9+
10+
#define MICROPY_PORT_A (PORT_PA04 | PORT_PA05 | PORT_PA24 | PORT_PA25)
11+
#define MICROPY_PORT_B (0)
812

913
#define AUTORESET_DELAY_MS 500
1014

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,36 @@
1-
// Pins have no behavior.
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 Scott Shawcroft for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "shared-bindings/microcontroller/Pin.h"
28+
29+
bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t* pin) {
30+
PortGroup *const port = system_pinmux_get_group_from_gpio_pin(pin->pin);
31+
uint32_t pin_index = (pin->pin);
32+
PORT_PINCFG_Type state = port->PINCFG[pin_index];
33+
34+
return state.bit.PMUXEN == 0 && state.bit.INEN == 0 &&
35+
state.bit.PULLEN == 0 && (port->DIR.reg & (1 << pin_index)) == 0;
36+
}

atmel-samd/common-hal/nativeio/AnalogIn.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "shared-bindings/nativeio/AnalogIn.h"
3434

3535
#include "asf/sam0/drivers/adc/adc.h"
36+
#include "samd21_pins.h"
3637

3738
void common_hal_nativeio_analogin_construct(nativeio_analogin_obj_t* self,
3839
const mcu_pin_obj_t *pin) {
@@ -55,6 +56,13 @@ void common_hal_nativeio_analogin_construct(nativeio_analogin_obj_t* self,
5556
adc_init(&self->adc_instance, ADC, &config_adc);
5657
}
5758

59+
void common_hal_nativeio_analogin_deinit(nativeio_analogin_obj_t *self) {
60+
// TODO(tannewt): Count how many pins are in use and only reset the ADC when
61+
// none are used.
62+
adc_reset(&self->adc_instance);
63+
reset_pin(self->pin->pin);
64+
}
65+
5866
// TODO(tannewt): Don't turn it all on just for one read. This simplifies
5967
// handling of reading multiple inputs and surviving sleep though so for now its
6068
// ok.

0 commit comments

Comments
 (0)