Skip to content

Commit 7cb5486

Browse files
committed
Add PulseIn support which can be used to measure a series of pulse widths.
This is useful for infrared input and DHT sensors.
1 parent d200a62 commit 7cb5486

File tree

17 files changed

+802
-64
lines changed

17 files changed

+802
-64
lines changed

atmel-samd/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ LIBS = -larm_cortexM0l_math -lsamd21_qtouch_gcc -lm -lgcc -lc
150150
SRC_ASF = $(addprefix asf/sam0/,\
151151
drivers/adc/adc_sam_d_r/adc.c \
152152
drivers/dac/dac_sam_d_c/dac.c \
153+
drivers/extint/extint_callback.c \
154+
drivers/extint/extint_sam_d_r/extint.c \
153155
drivers/nvm/nvm.c \
154156
drivers/port/port.c \
155157
drivers/sercom/i2c/i2c_sam0/i2c_master.c \
@@ -224,6 +226,7 @@ SRC_BINDINGS = \
224226
nativeio/AnalogOut.c \
225227
nativeio/DigitalInOut.c \
226228
nativeio/I2C.c \
229+
nativeio/PulseIn.c \
227230
nativeio/PulseOut.c \
228231
nativeio/PWMOut.c \
229232
nativeio/SPI.c \

atmel-samd/asf_conf/conf_extint.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* \file
3+
*
4+
* \brief SAM D21 External Interrupt Driver Configuration Header
5+
*
6+
* Copyright (C) 2013-2015 Atmel Corporation. All rights reserved.
7+
*
8+
* \asf_license_start
9+
*
10+
* \page License
11+
*
12+
* Redistribution and use in source and binary forms, with or without
13+
* modification, are permitted provided that the following conditions are met:
14+
*
15+
* 1. Redistributions of source code must retain the above copyright notice,
16+
* this list of conditions and the following disclaimer.
17+
*
18+
* 2. Redistributions in binary form must reproduce the above copyright notice,
19+
* this list of conditions and the following disclaimer in the documentation
20+
* and/or other materials provided with the distribution.
21+
*
22+
* 3. The name of Atmel may not be used to endorse or promote products derived
23+
* from this software without specific prior written permission.
24+
*
25+
* 4. This software may only be redistributed and used in connection with an
26+
* Atmel microcontroller product.
27+
*
28+
* THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29+
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31+
* EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32+
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36+
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37+
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38+
* POSSIBILITY OF SUCH DAMAGE.
39+
*
40+
* \asf_license_stop
41+
*
42+
*/
43+
/*
44+
* Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45+
*/
46+
#ifndef CONF_EXTINT_H_INCLUDED
47+
#define CONF_EXTINT_H_INCLUDED
48+
49+
# define EXTINT_CLOCK_SOURCE GCLK_GENERATOR_0
50+
51+
#endif

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ typedef struct {
5959
mp_obj_base_t base;
6060
qstr name;
6161
uint8_t pin;
62+
bool has_extint:1;
63+
uint8_t extint_channel:7;
6264
bool has_adc:1;
6365
enum adc_positive_input adc_input:7;
6466
bool has_touch:1;
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 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/nativeio/PulseIn.h"
28+
29+
#include <stdint.h>
30+
31+
#include "asf/common2/services/delay/delay.h"
32+
#include "asf/sam0/drivers/extint/extint.h"
33+
#include "asf/sam0/drivers/extint/extint_callback.h"
34+
35+
#include "mpconfigport.h"
36+
#include "py/gc.h"
37+
#include "py/runtime.h"
38+
#include "samd21_pins.h"
39+
#include "shared-bindings/microcontroller/__init__.h"
40+
#include "shared-bindings/nativeio/PulseIn.h"
41+
42+
#include "tick.h"
43+
44+
static nativeio_pulsein_obj_t *active_pulseins[EIC_NUMBER_OF_INTERRUPTS];
45+
static uint64_t last_ms[EIC_NUMBER_OF_INTERRUPTS];
46+
static uint16_t last_us[EIC_NUMBER_OF_INTERRUPTS];
47+
48+
void pulsein_reset(void) {
49+
for (int i = 0; i < EIC_NUMBER_OF_INTERRUPTS; i++) {
50+
active_pulseins[i] = NULL;
51+
last_ms[i] = 0;
52+
last_us[i] = 0;
53+
}
54+
}
55+
56+
static void pulsein_set_config(nativeio_pulsein_obj_t* self, bool first_edge) {
57+
struct extint_chan_conf config;
58+
extint_chan_get_config_defaults(&config);
59+
config.gpio_pin = self->pin;
60+
config.gpio_pin_pull = EXTINT_PULL_NONE;
61+
config.filter_input_signal = true;
62+
63+
if (!first_edge) {
64+
config.detection_criteria = EXTINT_DETECT_BOTH;
65+
} else if (self->idle_state) {
66+
config.detection_criteria = EXTINT_DETECT_FALLING;
67+
} else {
68+
config.detection_criteria = EXTINT_DETECT_RISING;
69+
}
70+
extint_chan_disable_callback(self->channel, EXTINT_CALLBACK_TYPE_DETECT);
71+
extint_chan_set_config(self->channel, &config);
72+
// Clear any interrupts that may have triggered without notifying the CPU.
73+
EIC->INTFLAG.reg |= (1UL << self->channel);
74+
extint_chan_enable_callback(self->channel, EXTINT_CALLBACK_TYPE_DETECT);
75+
}
76+
77+
static void pulsein_callback(void) {
78+
// Grab the current time first.
79+
uint16_t current_us = tc_get_count_value(&ms_timer);
80+
uint64_t current_ms = ticks_ms;
81+
nativeio_pulsein_obj_t* self = active_pulseins[extint_get_current_channel()];
82+
current_us = current_us * 1000 / self->ticks_per_ms;
83+
if (self->first_edge) {
84+
self->first_edge = false;
85+
pulsein_set_config(self, false);
86+
} else {
87+
uint32_t ms_diff = current_ms - last_ms[self->channel];
88+
uint16_t us_diff = current_us - last_us[self->channel];
89+
if (last_us[self->channel] > current_us) {
90+
us_diff = 1000 + current_us - last_us[self->channel];
91+
}
92+
uint32_t total_diff = us_diff;
93+
if (ms_diff > 1) {
94+
total_diff += (ms_diff - 1) * 1000;
95+
}
96+
uint16_t duration = 0xffff;
97+
if (total_diff < duration) {
98+
duration = total_diff;
99+
}
100+
101+
uint16_t i = (self->start + self->len) % self->maxlen;
102+
self->buffer[i] = duration;
103+
if (self->len < self->maxlen) {
104+
self->len++;
105+
} else {
106+
self->start++;
107+
}
108+
}
109+
last_ms[self->channel] = current_ms;
110+
last_us[self->channel] = current_us;
111+
}
112+
113+
void common_hal_nativeio_pulsein_construct(nativeio_pulsein_obj_t* self,
114+
const mcu_pin_obj_t* pin, uint16_t maxlen, bool idle_state) {
115+
if (!pin->has_extint) {
116+
mp_raise_RuntimeError("No hardware support on pin");
117+
}
118+
// TODO(tannewt): Switch to checking actual extint peripheral state when other
119+
// classes use extints.
120+
if (active_pulseins[pin->extint_channel] != NULL) {
121+
mp_raise_RuntimeError("EXTINT channel already in use");
122+
}
123+
124+
self->buffer = (uint16_t *) gc_alloc(maxlen * sizeof(uint16_t), false);
125+
if (self->buffer == NULL) {
126+
mp_raise_msg_varg(&mp_type_MemoryError, "Failed to allocate RX buffer of %d bytes", maxlen * sizeof(uint16_t));
127+
}
128+
self->channel = pin->extint_channel;
129+
self->pin = pin->pin;
130+
self->maxlen = maxlen;
131+
self->idle_state = idle_state;
132+
self->start = 0;
133+
self->len = 0;
134+
self->first_edge = true;
135+
self->ticks_per_ms = (system_cpu_clock_get_hz() / 1000 - 1);
136+
137+
active_pulseins[pin->extint_channel] = self;
138+
139+
pulsein_set_config(self, true);
140+
extint_register_callback(
141+
pulsein_callback,
142+
self->channel,
143+
EXTINT_CALLBACK_TYPE_DETECT);
144+
extint_chan_enable_callback(self->channel, EXTINT_CALLBACK_TYPE_DETECT);
145+
}
146+
147+
void common_hal_nativeio_pulsein_deinit(nativeio_pulsein_obj_t* self) {
148+
extint_chan_disable_callback(self->channel, EXTINT_CALLBACK_TYPE_DETECT);
149+
active_pulseins[self->channel] = NULL;
150+
reset_pin(self->pin);
151+
}
152+
153+
void common_hal_nativeio_pulsein_pause(nativeio_pulsein_obj_t* self) {
154+
extint_chan_disable_callback(self->channel, EXTINT_CALLBACK_TYPE_DETECT);
155+
}
156+
157+
void common_hal_nativeio_pulsein_resume(nativeio_pulsein_obj_t* self,
158+
uint16_t trigger_duration) {
159+
// Send the trigger pulse.
160+
if (trigger_duration > 0) {
161+
struct port_config pin_conf;
162+
port_get_config_defaults(&pin_conf);
163+
164+
pin_conf.direction = PORT_PIN_DIR_OUTPUT;
165+
pin_conf.input_pull = PORT_PIN_PULL_NONE;
166+
port_pin_set_config(self->pin, &pin_conf);
167+
port_pin_set_output_level(self->pin, !self->idle_state);
168+
delay_us(trigger_duration);
169+
port_pin_set_output_level(self->pin, self->idle_state);
170+
}
171+
172+
// Reconfigure the pin and make sure its set to detect the first edge.
173+
last_ms[self->channel] = 0;
174+
last_us[self->channel] = 0;
175+
self->first_edge = true;
176+
pulsein_set_config(self, true);
177+
}
178+
179+
void common_hal_nativeio_pulsein_clear(nativeio_pulsein_obj_t* self) {
180+
common_hal_mcu_disable_interrupts();
181+
self->start = 0;
182+
self->len = 0;
183+
common_hal_mcu_enable_interrupts();
184+
}
185+
186+
uint16_t common_hal_nativeio_pulsein_popleft(nativeio_pulsein_obj_t* self) {
187+
if (self->len == 0) {
188+
mp_raise_IndexError("pop from an empty PulseIn");
189+
}
190+
common_hal_mcu_disable_interrupts();
191+
uint16_t value = self->buffer[self->start];
192+
self->start = (self->start + 1) % self->maxlen;
193+
self->len--;
194+
common_hal_mcu_enable_interrupts();
195+
196+
return value;
197+
}
198+
199+
uint16_t common_hal_nativeio_pulsein_get_maxlen(nativeio_pulsein_obj_t* self) {
200+
return self->maxlen;
201+
}
202+
203+
uint16_t common_hal_nativeio_pulsein_get_len(nativeio_pulsein_obj_t* self) {
204+
return self->len;
205+
}
206+
207+
uint16_t common_hal_nativeio_pulsein_get_item(nativeio_pulsein_obj_t* self,
208+
int16_t index) {
209+
common_hal_mcu_disable_interrupts();
210+
if (index < 0) {
211+
index += self->len;
212+
}
213+
if (index < 0 || index >= self->len) {
214+
common_hal_mcu_enable_interrupts();
215+
mp_raise_IndexError("index out of range");
216+
}
217+
uint16_t value = self->buffer[(self->start + index) % self->maxlen];
218+
common_hal_mcu_enable_interrupts();
219+
return value;
220+
}
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) 2017 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_ATMEL_SAMD_COMMON_HAL_NATIVEIO_PULSEIN_H__
28+
#define __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_PULSEIN_H__
29+
30+
void pulsein_reset(void);
31+
32+
#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NATIVEIO_PULSEIN_H__

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,25 @@ typedef struct {
8888
uint32_t current_baudrate;
8989
} nativeio_spi_obj_t;
9090

91+
typedef struct {
92+
mp_obj_base_t base;
93+
uint8_t channel;
94+
uint8_t pin;
95+
uint16_t* buffer;
96+
uint16_t maxlen;
97+
bool idle_state;
98+
volatile uint16_t start;
99+
volatile uint16_t len;
100+
volatile bool first_edge;
101+
uint16_t ticks_per_ms;
102+
} nativeio_pulsein_obj_t;
103+
104+
typedef struct {
105+
mp_obj_base_t base;
106+
__IO PORT_PINCFG_Type *pincfg;
107+
uint8_t pin;
108+
} nativeio_pulseout_obj_t;
109+
91110
typedef struct {
92111
mp_obj_base_t base;
93112
const mcu_pin_obj_t *pin;
@@ -99,12 +118,6 @@ typedef struct {
99118
};
100119
} nativeio_pwmout_obj_t;
101120

102-
typedef struct {
103-
mp_obj_base_t base;
104-
__IO PORT_PINCFG_Type *pincfg;
105-
uint8_t pin;
106-
} nativeio_pulseout_obj_t;
107-
108121
typedef struct {
109122
mp_obj_base_t base;
110123
// Only support TouchIn when external SPI flash is used.

0 commit comments

Comments
 (0)