Skip to content

Commit f74e477

Browse files
committed
atmel-samd: Fully reset the ADC state on reset to prevent a crash.
1 parent d29915c commit f74e477

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
#include "common-hal/nativeio/AnalogIn.h"
28+
2729
#include <string.h>
2830

2931
#include "py/gc.h"
@@ -82,6 +84,14 @@ void common_hal_nativeio_analogin_deinit(nativeio_analogin_obj_t *self) {
8284
reset_pin(self->pin->pin);
8385
}
8486

87+
void analogin_reset() {
88+
if (adc_instance != NULL) {
89+
adc_reset(adc_instance);
90+
adc_instance = NULL;
91+
}
92+
active_channel_count = 0;
93+
}
94+
8595
uint16_t common_hal_nativeio_analogin_get_value(nativeio_analogin_obj_t *self) {
8696
adc_set_positive_input(adc_instance, self->pin->adc_input);
8797

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
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_ATMEL_SAMD_COMMON_HAL_NATIVEIO_ANALOGIN_H__
28+
#define __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_ANALOGIN_H__
29+
30+
void analogin_reset(void);
31+
32+
#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_ANALOGIN_H__

atmel-samd/main.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include "asf/sam0/drivers/system/system.h"
2323
#include <board.h>
2424

25+
#include "common-hal/nativeio/AnalogIn.h"
26+
2527
#ifdef EXPRESS_BOARD
2628
#include "common-hal/nativeio/types.h"
2729
#include "QTouch/touch_api_ptc.h"
@@ -157,6 +159,8 @@ void reset_samd21(void) {
157159
selfcap_config.num_sensors = 0;
158160
#endif
159161

162+
analogin_reset();
163+
160164
struct system_pinmux_config config;
161165
system_pinmux_get_config_defaults(&config);
162166
config.powersave = true;

0 commit comments

Comments
 (0)