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 #4, instantiating a second class will fail. Fixes #29, pins are now reset too.
1 parent bb9c751 commit 0ae3448

File tree

35 files changed

+322
-15
lines changed

35 files changed

+322
-15
lines changed

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.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "shared-bindings/nativeio/AnalogOut.h"
3333

3434
#include "asf/sam0/drivers/dac/dac.h"
35+
#include "samd21_pins.h"
3536

3637
void common_hal_nativeio_analogout_construct(nativeio_analogout_obj_t* self,
3738
const mcu_pin_obj_t *pin) {
@@ -61,6 +62,7 @@ void common_hal_nativeio_analogout_construct(nativeio_analogout_obj_t* self,
6162
void common_hal_nativeio_analogout_deinit(nativeio_analogout_obj_t *self) {
6263
dac_disable(&self->dac_instance);
6364
dac_chan_disable(&self->dac_instance, DAC_CHANNEL_0);
65+
reset_pin(PIN_PA02);
6466
}
6567

6668
void common_hal_nativeio_analogout_set_value(nativeio_analogout_obj_t *self,

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ void common_hal_nativeio_i2c_construct(nativeio_i2c_obj_t *self,
7777
config_i2c_master.pinmux_pad1 = scl_pinmux; // SCL
7878
config_i2c_master.buffer_timeout = 10000;
7979

80+
self->sda_pin = sda->pin;
81+
self->scl_pin = scl->pin;
82+
8083
enum status_code status = i2c_master_init(&self->i2c_master_instance,
8184
sercom, &config_i2c_master);
8285
if (status != STATUS_OK) {
@@ -88,6 +91,8 @@ void common_hal_nativeio_i2c_construct(nativeio_i2c_obj_t *self,
8891

8992
void common_hal_nativeio_i2c_deinit(nativeio_i2c_obj_t *self) {
9093
i2c_master_disable(&self->i2c_master_instance);
94+
reset_pin(self->sda_pin);
95+
reset_pin(self->scl_pin);
9196
}
9297

9398
bool common_hal_nativeio_i2c_probe(nativeio_i2c_obj_t *self, uint8_t addr) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ extern void common_hal_nativeio_pwmout_deinit(nativeio_pwmout_obj_t* self) {
8888
} else {
8989
tcc_disable(&self->tcc_instance);
9090
}
91+
reset_pin(self->pin->pin);
9192
}
9293

9394
extern void common_hal_nativeio_pwmout_set_duty_cycle(nativeio_pwmout_obj_t* self, uint16_t duty) {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,16 @@ void common_hal_nativeio_spi_construct(nativeio_spi_obj_t *self,
124124
&config_spi_master.pinmux_pad2,
125125
&config_spi_master.pinmux_pad3};
126126
*pinmuxes[clock_pad] = clock_pinmux;
127+
self->clock_pin = clock->pin;
128+
self->MOSI_pin = 0;
127129
if (!mosi_none) {
128130
*pinmuxes[mosi_pad] = mosi_pinmux;
131+
self->MOSI_pin = mosi->pin;
129132
}
133+
self->MISO_pin = 0;
130134
if (!miso_none) {
131135
*pinmuxes[miso_pad] = miso_pinmux;
136+
self->MISO_pin = miso->pin;
132137
}
133138

134139
spi_init(&self->spi_master_instance, sercom, &config_spi_master);
@@ -138,6 +143,9 @@ void common_hal_nativeio_spi_construct(nativeio_spi_obj_t *self,
138143

139144
void common_hal_nativeio_spi_deinit(nativeio_spi_obj_t *self) {
140145
spi_disable(&self->spi_master_instance);
146+
reset_pin(self->clock_pin);
147+
reset_pin(self->MOSI_pin);
148+
reset_pin(self->MISO_pin);
141149
}
142150

143151
bool common_hal_nativeio_spi_configure(nativeio_spi_obj_t *self,

atmel-samd/common-hal/nativeio/types.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,17 @@ typedef struct {
6969
mp_obj_base_t base;
7070
struct i2c_master_module i2c_master_instance;
7171
bool has_lock;
72+
uint8_t scl_pin;
73+
uint8_t sda_pin;
7274
} nativeio_i2c_obj_t;
7375

74-
typedef struct _machine_spi_obj_t {
76+
typedef struct {
7577
mp_obj_base_t base;
7678
struct spi_module spi_master_instance;
7779
bool has_lock;
80+
uint8_t clock_pin;
81+
uint8_t MOSI_pin;
82+
uint8_t MISO_pin;
7883
} nativeio_spi_obj_t;
7984

8085
typedef struct {

atmel-samd/main.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,22 @@ void reset_samd21(void) {
163163
continue;
164164
}
165165
#endif
166+
#ifdef MICROPY_HW_APA102_SERCOM
167+
if (sercom_instances[i] == MICROPY_HW_APA102_SERCOM) {
168+
continue;
169+
}
170+
#endif
166171
sercom_instances[i]->SPI.CTRLA.bit.SWRST = 1;
167172
}
168173

169-
// TODO(tannewt): Reset all of the pins too.
174+
struct system_pinmux_config config;
175+
system_pinmux_get_config_defaults(&config);
176+
config.powersave = true;
177+
178+
uint32_t pin_mask[2] = PORT_OUT_IMPLEMENTED;
179+
180+
system_pinmux_group_set_config(&(PORT->Group[0]), pin_mask[0] & ~MICROPY_PORT_A, &config);
181+
system_pinmux_group_set_config(&(PORT->Group[1]), pin_mask[1] & ~MICROPY_PORT_B, &config);
170182
}
171183

172184
bool maybe_run(const char* filename, int* ret) {

atmel-samd/samd21_pins.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ const mcu_pin_obj_t pin_## p_name = { \
4444

4545
#define NO_ADC_INPUT (0)
4646

47+
void reset_pin(uint8_t pin) {
48+
struct system_pinmux_config config;
49+
system_pinmux_get_config_defaults(&config);
50+
config.powersave = true;
51+
system_pinmux_pin_set_config(pin, &config);
52+
}
53+
4754
// Pins in datasheet order.
4855
#ifdef PIN_PA00
4956
PIN(PA00, false, NO_ADC_INPUT, \

atmel-samd/samd21_pins.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
#include "common-hal/microcontroller/types.h"
55

6+
void reset_pin(uint8_t pin);
7+
68
#define MUX_C 2
79
#define MUX_D 3
810
#define MUX_E 4
Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,51 @@
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 "common-hal/microcontroller/__init__.h"
28+
#include "shared-bindings/microcontroller/Pin.h"
29+
30+
#include "eagle_soc.h"
31+
32+
extern volatile bool adc_in_use;
33+
34+
bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t* pin) {
35+
return (pin == &pin_TOUT && adc_in_use) ||
36+
((READ_PERI_REG(pin->peripheral) &
37+
(PERIPHS_IO_MUX_FUNC<<PERIPHS_IO_MUX_FUNC_S)) == 0 &&
38+
(GPIO_REG_READ(GPIO_ENABLE_ADDRESS) & (1 << pin->gpio_number)) == 0 &&
39+
(READ_PERI_REG(pin->peripheral) & PERIPHS_IO_MUX_PULLUP) == 0 );
40+
}
41+
42+
void reset_pins(void) {
43+
for (int i = 0; i < 17; i++) {
44+
if (i == 0 || (i > 6 && i < 13) || i == 12) {
45+
continue;
46+
}
47+
uint32_t peripheral = PERIPHS_IO_MUX + i * 4;
48+
PIN_FUNC_SELECT(peripheral, 0);
49+
PIN_PULLUP_DIS(peripheral);
50+
}
51+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
#ifndef __MICROPY_INCLUDED_ESP8266_COMMON_HAL_MICROCONTROLLER_PIN_H__
28+
#define __MICROPY_INCLUDED_ESP8266_COMMON_HAL_MICROCONTROLLER_PIN_H__
29+
30+
void reset_pins(void);
31+
32+
#endif // __MICROPY_INCLUDED_ESP8266_COMMON_HAL_MICROCONTROLLER_PIN_H__

0 commit comments

Comments
 (0)