Skip to content

Commit f1248dc

Browse files
authored
Merge pull request adafruit#973 from dhalbert/3.x_onewire_fix
fix OneWire timing and DigitalInOut.switch_to_input()
2 parents 6908eed + 7c9a0e2 commit f1248dc

File tree

7 files changed

+72
-19
lines changed

7 files changed

+72
-19
lines changed

ports/atmel-samd/common-hal/digitalio/DigitalInOut.c

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t* self
6060
void common_hal_digitalio_digitalinout_switch_to_input(
6161
digitalio_digitalinout_obj_t* self, digitalio_pull_t pull) {
6262
self->output = false;
63-
63+
// This also sets direction to input.
6464
common_hal_digitalio_digitalinout_set_pull(self, pull);
6565
}
6666

@@ -69,13 +69,13 @@ void common_hal_digitalio_digitalinout_switch_to_output(
6969
digitalio_drive_mode_t drive_mode) {
7070
const uint8_t pin = self->pin->pin;
7171
gpio_set_pin_pull_mode(pin, GPIO_PULL_OFF);
72-
gpio_set_pin_direction(pin, GPIO_DIRECTION_OUT);
73-
7472
// Turn on "strong" pin driving (more current available). See DRVSTR doc in datasheet.
7573
hri_port_set_PINCFG_DRVSTR_bit(PORT, (enum gpio_port)GPIO_PORT(pin), GPIO_PIN(pin));
7674

7775
self->output = true;
7876
self->open_drain = drive_mode == DRIVE_MODE_OPEN_DRAIN;
77+
78+
// Direction is set in set_value. We don't need to do it here.
7979
common_hal_digitalio_digitalinout_set_value(self, value);
8080
}
8181

@@ -86,16 +86,22 @@ digitalio_direction_t common_hal_digitalio_digitalinout_get_direction(
8686

8787
void common_hal_digitalio_digitalinout_set_value(
8888
digitalio_digitalinout_obj_t* self, bool value) {
89+
const uint8_t pin = self->pin->pin;
90+
const uint8_t port = GPIO_PORT(pin);
91+
const uint32_t pin_mask = 1U << GPIO_PIN(pin);
8992
if (value) {
9093
if (self->open_drain) {
91-
gpio_set_pin_direction(self->pin->pin, GPIO_DIRECTION_IN);
94+
// Assertion: pull is off, so the pin is floating in this case.
95+
// We do open-drain high output (no sinking of current)
96+
// by changing the direction to input with no pulls.
97+
hri_port_clear_DIR_DIR_bf(PORT, port, pin_mask);
9298
} else {
93-
gpio_set_pin_level(self->pin->pin, true);
94-
gpio_set_pin_direction(self->pin->pin, GPIO_DIRECTION_OUT);
99+
hri_port_set_DIR_DIR_bf(PORT, port, pin_mask);
100+
hri_port_set_OUT_OUT_bf(PORT, port, pin_mask);
95101
}
96102
} else {
97-
gpio_set_pin_level(self->pin->pin, false);
98-
gpio_set_pin_direction(self->pin->pin, GPIO_DIRECTION_OUT);
103+
hri_port_set_DIR_DIR_bf(PORT, port, pin_mask);
104+
hri_port_clear_OUT_OUT_bf(PORT,port, pin_mask);
99105
}
100106
}
101107

@@ -105,10 +111,10 @@ bool common_hal_digitalio_digitalinout_get_value(
105111
if (!self->output) {
106112
return gpio_get_pin_level(pin);
107113
} else {
108-
if (self->open_drain && hri_port_get_DIR_reg(PORT, (enum gpio_port)GPIO_PORT(pin), 1U << GPIO_PIN(pin)) == 0) {
114+
if (self->open_drain && hri_port_get_DIR_reg(PORT, GPIO_PORT(pin), 1U << GPIO_PIN(pin)) == 0) {
109115
return true;
110116
} else {
111-
return hri_port_get_OUT_reg(PORT, (enum gpio_port)GPIO_PORT(pin), 1U << GPIO_PIN(pin));
117+
return hri_port_get_OUT_reg(PORT, GPIO_PORT(pin), 1U << GPIO_PIN(pin));
112118
}
113119
}
114120
}
@@ -119,7 +125,7 @@ void common_hal_digitalio_digitalinout_set_drive_mode(
119125
bool value = common_hal_digitalio_digitalinout_get_value(self);
120126
self->open_drain = drive_mode == DRIVE_MODE_OPEN_DRAIN;
121127
// True is implemented differently between modes so reset the value to make
122-
// sure its correct for the new mode.
128+
// sure it's correct for the new mode.
123129
if (value) {
124130
common_hal_digitalio_digitalinout_set_value(self, value);
125131
}
@@ -148,7 +154,9 @@ void common_hal_digitalio_digitalinout_set_pull(
148154
default:
149155
break;
150156
}
157+
// Set pull first to avoid glitches.
151158
gpio_set_pin_pull_mode(self->pin->pin, asf_pull);
159+
gpio_set_pin_direction(self->pin->pin, GPIO_DIRECTION_IN);
152160
}
153161

154162
digitalio_pull_t common_hal_digitalio_digitalinout_get_pull(
@@ -158,9 +166,9 @@ digitalio_pull_t common_hal_digitalio_digitalinout_get_pull(
158166
mp_raise_AttributeError("Cannot get pull while in output mode");
159167
return PULL_NONE;
160168
} else {
161-
if (hri_port_get_PINCFG_PULLEN_bit(PORT, (enum gpio_port)GPIO_PORT(pin), GPIO_PIN(pin)) == 0) {
169+
if (hri_port_get_PINCFG_PULLEN_bit(PORT, GPIO_PORT(pin), GPIO_PIN(pin)) == 0) {
162170
return PULL_NONE;
163-
} if (hri_port_get_OUT_reg(PORT, (enum gpio_port)GPIO_PORT(pin), 1U << GPIO_PIN(pin)) > 0) {
171+
} if (hri_port_get_OUT_reg(PORT, GPIO_PORT(pin), 1U << GPIO_PIN(pin)) > 0) {
164172
return PULL_UP;
165173
} else {
166174
return PULL_DOWN;

ports/atmel-samd/mphalport.c

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,29 @@
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+
127
#include <string.h>
228

329
#include "lib/mp-readline/readline.h"
@@ -14,6 +40,7 @@
1440
#include "hal/include/hal_delay.h"
1541
#include "hal/include/hal_gpio.h"
1642
#include "hal/include/hal_sleep.h"
43+
#include "sam.h"
1744

1845
#include "mpconfigboard.h"
1946
#include "mphalport.h"
@@ -22,6 +49,7 @@
2249
#include "usb.h"
2350

2451
extern struct usart_module usart_instance;
52+
extern uint32_t common_hal_mcu_processor_get_frequency(void);
2553

2654
int mp_hal_stdin_rx_chr(void) {
2755
for (;;) {
@@ -71,8 +99,23 @@ void mp_hal_delay_ms(mp_uint_t delay) {
7199
}
72100
}
73101

102+
// Use mp_hal_delay_us() for timing of less than 1ms.
103+
// Do a simple timing loop to wait for a certain number of microseconds.
104+
// Can be used when interrupts are disabled, which makes tick_delay() unreliable.
105+
//
106+
// Testing done at 48 MHz on SAMD21 and 120 MHz on SAMD51, multiplication and division cancel out.
107+
// But get the frequency just in case.
108+
#ifdef SAMD21
109+
#define DELAY_LOOP_ITERATIONS_PER_US ( (10U*48000000U) / common_hal_mcu_processor_get_frequency())
110+
#endif
111+
#ifdef SAMD51
112+
#define DELAY_LOOP_ITERATIONS_PER_US ( (30U*120000000U) / common_hal_mcu_processor_get_frequency())
113+
#endif
114+
74115
void mp_hal_delay_us(mp_uint_t delay) {
75-
tick_delay(delay);
116+
for (uint32_t i = delay*DELAY_LOOP_ITERATIONS_PER_US; i > 0; i--) {
117+
asm volatile("nop");
118+
}
76119
}
77120

78121
void mp_hal_disable_all_interrupts(void) {

ports/atmel-samd/mphalport.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ int receive_usb(void);
4848
void mp_hal_set_interrupt_char(int c);
4949

5050
void mp_hal_disable_all_interrupts(void);
51-
5251
void mp_hal_enable_all_interrupts(void);
5352

5453
#endif // MICROPY_INCLUDED_ATMEL_SAMD_MPHALPORT_H

ports/atmel-samd/tick.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ void tick_init() {
6464
for (uint16_t i = 0; i < PERIPH_COUNT_IRQn; i++) {
6565
NVIC_SetPriority(i, (1UL << __NVIC_PRIO_BITS) - 1UL);
6666
}
67-
// Bump up the systick interrupt.
68-
NVIC_SetPriority(SysTick_IRQn, 1);
67+
// Bump up the systick interrupt so nothing else interferes with timekeeping.
68+
NVIC_SetPriority(SysTick_IRQn, 0);
6969
#ifdef SAMD21
7070
NVIC_SetPriority(USB_IRQn, 1);
7171
#endif

ports/nrf/common-hal/microcontroller/__init__.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
#include "shared-bindings/microcontroller/Processor.h"
3434

3535
// TODO porting common_hal_mcu
36+
// This routine should work even when interrupts are disabled. Used by OneWire
37+
// for precise timing.
3638
void common_hal_mcu_delay_us(uint32_t delay) {
3739
// os_delay_us(delay);
3840
}
@@ -58,4 +60,3 @@ const mcu_processor_obj_t common_hal_mcu_processor_obj = {
5860
.type = &mcu_processor_type,
5961
},
6062
};
61-

ports/nrf/mphalport.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,3 @@ bool mp_hal_stdin_any(void);
7676
#define mp_hal_ticks_cpu() (0)
7777

7878
#endif
79-

shared-module/bitbangio/OneWire.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ void shared_module_bitbangio_onewire_deinit(bitbangio_onewire_obj_t* self) {
4949
common_hal_digitalio_digitalinout_deinit(&self->pin);
5050
}
5151

52+
// We use common_hal_mcu_delay_us(). It should not be dependent on interrupts
53+
// to do accurate timekeeping, since we disable interrupts during the delays below.
54+
5255
bool shared_module_bitbangio_onewire_reset(bitbangio_onewire_obj_t* self) {
5356
common_hal_mcu_disable_interrupts();
5457
common_hal_digitalio_digitalinout_switch_to_output(&self->pin, false, DRIVE_MODE_OPEN_DRAIN);

0 commit comments

Comments
 (0)