From 52a11547cbd3d8bf132bdfdd2fa3f804920665f3 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Thu, 28 Jun 2018 23:06:08 -0400 Subject: [PATCH 1/5] fix OneWire timing and DigitalInOut.switch_to_input() --- .../common-hal/digitalio/DigitalInOut.c | 34 ++++++++++++------- ports/atmel-samd/mphalport.c | 20 +++++++++++ ports/atmel-samd/mphalport.h | 3 +- ports/atmel-samd/tick.c | 4 +-- ports/nrf/mphalport.c | 14 ++++++++ ports/nrf/mphalport.h | 3 +- shared-module/bitbangio/OneWire.c | 21 +++++++----- 7 files changed, 74 insertions(+), 25 deletions(-) diff --git a/ports/atmel-samd/common-hal/digitalio/DigitalInOut.c b/ports/atmel-samd/common-hal/digitalio/DigitalInOut.c index cd0e3f91e57d3..03bd225b14738 100644 --- a/ports/atmel-samd/common-hal/digitalio/DigitalInOut.c +++ b/ports/atmel-samd/common-hal/digitalio/DigitalInOut.c @@ -60,7 +60,7 @@ void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t* self void common_hal_digitalio_digitalinout_switch_to_input( digitalio_digitalinout_obj_t* self, digitalio_pull_t pull) { self->output = false; - + // This also sets direction to input. common_hal_digitalio_digitalinout_set_pull(self, pull); } @@ -69,13 +69,13 @@ void common_hal_digitalio_digitalinout_switch_to_output( digitalio_drive_mode_t drive_mode) { const uint8_t pin = self->pin->pin; gpio_set_pin_pull_mode(pin, GPIO_PULL_OFF); - gpio_set_pin_direction(pin, GPIO_DIRECTION_OUT); - // Turn on "strong" pin driving (more current available). See DRVSTR doc in datasheet. hri_port_set_PINCFG_DRVSTR_bit(PORT, (enum gpio_port)GPIO_PORT(pin), GPIO_PIN(pin)); self->output = true; self->open_drain = drive_mode == DRIVE_MODE_OPEN_DRAIN; + + // Direction is set in set_value. We don't need to do it here. common_hal_digitalio_digitalinout_set_value(self, value); } @@ -86,16 +86,22 @@ digitalio_direction_t common_hal_digitalio_digitalinout_get_direction( void common_hal_digitalio_digitalinout_set_value( digitalio_digitalinout_obj_t* self, bool value) { + const uint8_t pin = self->pin->pin; + const uint8_t port = GPIO_PORT(pin); + const uint32_t pin_mask = 1U << GPIO_PIN(pin); if (value) { if (self->open_drain) { - gpio_set_pin_direction(self->pin->pin, GPIO_DIRECTION_IN); + // Assertion: pull is off, so the pin is floating in this case. + // We do open-drain high output (no sinking of current) + // by changing the direction to input with no pulls. + hri_port_clear_DIR_DIR_bf(PORT, port, pin_mask); } else { - gpio_set_pin_level(self->pin->pin, true); - gpio_set_pin_direction(self->pin->pin, GPIO_DIRECTION_OUT); + hri_port_set_DIR_DIR_bf(PORT, port, pin_mask); + hri_port_set_OUT_OUT_bf(PORT, port, pin_mask); } } else { - gpio_set_pin_level(self->pin->pin, false); - gpio_set_pin_direction(self->pin->pin, GPIO_DIRECTION_OUT); + hri_port_set_DIR_DIR_bf(PORT, port, pin_mask); + hri_port_clear_OUT_OUT_bf(PORT,port, pin_mask); } } @@ -105,10 +111,10 @@ bool common_hal_digitalio_digitalinout_get_value( if (!self->output) { return gpio_get_pin_level(pin); } else { - if (self->open_drain && hri_port_get_DIR_reg(PORT, (enum gpio_port)GPIO_PORT(pin), 1U << GPIO_PIN(pin)) == 0) { + if (self->open_drain && hri_port_get_DIR_reg(PORT, GPIO_PORT(pin), 1U << GPIO_PIN(pin)) == 0) { return true; } else { - return hri_port_get_OUT_reg(PORT, (enum gpio_port)GPIO_PORT(pin), 1U << GPIO_PIN(pin)); + return hri_port_get_OUT_reg(PORT, GPIO_PORT(pin), 1U << GPIO_PIN(pin)); } } } @@ -119,7 +125,7 @@ void common_hal_digitalio_digitalinout_set_drive_mode( bool value = common_hal_digitalio_digitalinout_get_value(self); self->open_drain = drive_mode == DRIVE_MODE_OPEN_DRAIN; // True is implemented differently between modes so reset the value to make - // sure its correct for the new mode. + // sure it's correct for the new mode. if (value) { common_hal_digitalio_digitalinout_set_value(self, value); } @@ -148,7 +154,9 @@ void common_hal_digitalio_digitalinout_set_pull( default: break; } + // Set pull first to avoid glitches. gpio_set_pin_pull_mode(self->pin->pin, asf_pull); + gpio_set_pin_direction(self->pin->pin, GPIO_DIRECTION_IN); } digitalio_pull_t common_hal_digitalio_digitalinout_get_pull( @@ -158,9 +166,9 @@ digitalio_pull_t common_hal_digitalio_digitalinout_get_pull( mp_raise_AttributeError("Cannot get pull while in output mode"); return PULL_NONE; } else { - if (hri_port_get_PINCFG_PULLEN_bit(PORT, (enum gpio_port)GPIO_PORT(pin), GPIO_PIN(pin)) == 0) { + if (hri_port_get_PINCFG_PULLEN_bit(PORT, GPIO_PORT(pin), GPIO_PIN(pin)) == 0) { return PULL_NONE; - } if (hri_port_get_OUT_reg(PORT, (enum gpio_port)GPIO_PORT(pin), 1U << GPIO_PIN(pin)) > 0) { + } if (hri_port_get_OUT_reg(PORT, GPIO_PORT(pin), 1U << GPIO_PIN(pin)) > 0) { return PULL_UP; } else { return PULL_DOWN; diff --git a/ports/atmel-samd/mphalport.c b/ports/atmel-samd/mphalport.c index c1b8ce5504462..dd6a1471349c1 100644 --- a/ports/atmel-samd/mphalport.c +++ b/ports/atmel-samd/mphalport.c @@ -14,6 +14,7 @@ #include "hal/include/hal_delay.h" #include "hal/include/hal_gpio.h" #include "hal/include/hal_sleep.h" +#include "sam.h" #include "mpconfigboard.h" #include "mphalport.h" @@ -22,6 +23,7 @@ #include "usb.h" extern struct usart_module usart_instance; +extern uint32_t common_hal_mcu_processor_get_frequency(void); int mp_hal_stdin_rx_chr(void) { for (;;) { @@ -75,6 +77,24 @@ void mp_hal_delay_us(mp_uint_t delay) { tick_delay(delay); } +// Do a simple timing loop to wait for a certain number of microseconds. +// Can be used when interrupts are disabled, which makes tick_delay() unreliable. +// +// Testing done at 48 MHz on SAMD21 and 120 MHz on SAMD51, multiplication and division cancel out. +// But get the frequency just in case. +#ifdef SAMD21 +#define DELAY_LOOP_ITERATIONS_PER_US ( (10U*48000000U) / common_hal_mcu_processor_get_frequency()) +#endif +#ifdef SAMD51 +#define DELAY_LOOP_ITERATIONS_PER_US ( (30U*120000000U) / common_hal_mcu_processor_get_frequency()) +#endif + +void mp_hal_delay_us_loop(uint32_t us) { + for (uint32_t i = us*DELAY_LOOP_ITERATIONS_PER_US; i > 0; i--) { + asm volatile("nop"); + } +} + void mp_hal_disable_all_interrupts(void) { common_hal_mcu_disable_interrupts(); } diff --git a/ports/atmel-samd/mphalport.h b/ports/atmel-samd/mphalport.h index 3b5955a4ffc65..31d34c8c392f8 100644 --- a/ports/atmel-samd/mphalport.h +++ b/ports/atmel-samd/mphalport.h @@ -48,7 +48,8 @@ int receive_usb(void); void mp_hal_set_interrupt_char(int c); void mp_hal_disable_all_interrupts(void); - void mp_hal_enable_all_interrupts(void); +void mp_hal_delay_us_loop(uint32_t us); + #endif // MICROPY_INCLUDED_ATMEL_SAMD_MPHALPORT_H diff --git a/ports/atmel-samd/tick.c b/ports/atmel-samd/tick.c index 9753a1a0c7494..149347793395a 100644 --- a/ports/atmel-samd/tick.c +++ b/ports/atmel-samd/tick.c @@ -64,8 +64,8 @@ void tick_init() { for (uint16_t i = 0; i < PERIPH_COUNT_IRQn; i++) { NVIC_SetPriority(i, (1UL << __NVIC_PRIO_BITS) - 1UL); } - // Bump up the systick interrupt. - NVIC_SetPriority(SysTick_IRQn, 1); + // Bump up the systick interrupt so nothing else interferes with timekeeping. + NVIC_SetPriority(SysTick_IRQn, 0); #ifdef SAMD21 NVIC_SetPriority(USB_IRQn, 1); #endif diff --git a/ports/nrf/mphalport.c b/ports/nrf/mphalport.c index 957d3535d296a..1f732e1889a19 100644 --- a/ports/nrf/mphalport.c +++ b/ports/nrf/mphalport.c @@ -83,3 +83,17 @@ void mp_hal_stdout_tx_strn_cooked(const char *str, mp_uint_t len) { void mp_hal_stdout_tx_str(const char *str) { mp_hal_stdout_tx_strn(str, strlen(str)); } + +// Do a simple timing loop to wait for a certain number of microseconds. +// Can be used when interrupts are disabled, which makes tick_delay() unreliable. +// +// Testing done at 48 MHz on SAMD21 and 120 MHz on SAMD51 (cache on). +// TODO: Test on NRF. For now, use SAMD51 calibration, even though nRF52 runs slower. +// Fraction should compensate. +#define DELAY_LOOP_ITERATIONS_PER_US (30U*120000000U) / common_hal_mcu_processor_get_frequency()) + +void mp_hal_delay_us_loop(uint32_t us) { + for (uint32_t i = us*DELAY_LOOP_ITERATIONS_PER_US; i > 0; i--) { + asm volatile("nop"); + } +} diff --git a/ports/nrf/mphalport.h b/ports/nrf/mphalport.h index 3334fd7fe96a7..79e1d2d58ee9d 100644 --- a/ports/nrf/mphalport.h +++ b/ports/nrf/mphalport.h @@ -52,6 +52,7 @@ static inline uint32_t hal_tick_fake(void) { extern const unsigned char mp_hal_status_to_errno_table[4]; + NORETURN void mp_hal_raise(HAL_StatusTypeDef status); void mp_hal_set_interrupt_char(int c); // -1 to disable @@ -69,6 +70,7 @@ bool mp_hal_stdin_any(void); #define mp_hal_pin_od_high(p) mp_hal_pin_high(p) #define mp_hal_pin_open_drain(p) hal_gpio_cfg_pin(p->port, p->pin, HAL_GPIO_MODE_INPUT, HAL_GPIO_PULL_DISABLED) +void mp_hal_delay_us_loop(uint32_t us); // TODO: empty implementation for now. Used by machine_spi.c:69 #define mp_hal_delay_us_fast(p) @@ -76,4 +78,3 @@ bool mp_hal_stdin_any(void); #define mp_hal_ticks_cpu() (0) #endif - diff --git a/shared-module/bitbangio/OneWire.c b/shared-module/bitbangio/OneWire.c index e77bdb3b2961f..b226b9d7847a8 100644 --- a/shared-module/bitbangio/OneWire.c +++ b/shared-module/bitbangio/OneWire.c @@ -24,6 +24,8 @@ * THE SOFTWARE. */ +#include "mphalport.h" + #include "common-hal/microcontroller/Pin.h" #include "shared-bindings/bitbangio/OneWire.h" #include "shared-bindings/microcontroller/__init__.h" @@ -49,14 +51,17 @@ void shared_module_bitbangio_onewire_deinit(bitbangio_onewire_obj_t* self) { common_hal_digitalio_digitalinout_deinit(&self->pin); } +// We can't use common_hal_mcu_delay_us() here because it needs interrupts to be accurate +// due to SysTick rollover checking, done by an interrupt. + bool shared_module_bitbangio_onewire_reset(bitbangio_onewire_obj_t* self) { common_hal_mcu_disable_interrupts(); common_hal_digitalio_digitalinout_switch_to_output(&self->pin, false, DRIVE_MODE_OPEN_DRAIN); - common_hal_mcu_delay_us(480); + mp_hal_delay_us_loop(480); common_hal_digitalio_digitalinout_switch_to_input(&self->pin, PULL_NONE); - common_hal_mcu_delay_us(70); + mp_hal_delay_us_loop(70); bool value = common_hal_digitalio_digitalinout_get_value(&self->pin); - common_hal_mcu_delay_us(410); + mp_hal_delay_us_loop(410); common_hal_mcu_enable_interrupts(); return value; } @@ -64,14 +69,14 @@ bool shared_module_bitbangio_onewire_reset(bitbangio_onewire_obj_t* self) { bool shared_module_bitbangio_onewire_read_bit(bitbangio_onewire_obj_t* self) { common_hal_mcu_disable_interrupts(); common_hal_digitalio_digitalinout_switch_to_output(&self->pin, false, DRIVE_MODE_OPEN_DRAIN); - common_hal_mcu_delay_us(6); + mp_hal_delay_us_loop(6); common_hal_digitalio_digitalinout_switch_to_input(&self->pin, PULL_NONE); // TODO(tannewt): Test with more devices and maybe make the delays // configurable. This should be 9 by the datasheet but all bits read as 1 // then. - common_hal_mcu_delay_us(6); + mp_hal_delay_us_loop(6); bool value = common_hal_digitalio_digitalinout_get_value(&self->pin); - common_hal_mcu_delay_us(55); + mp_hal_delay_us_loop(55); common_hal_mcu_enable_interrupts(); return value; } @@ -80,8 +85,8 @@ void shared_module_bitbangio_onewire_write_bit(bitbangio_onewire_obj_t* self, bool bit) { common_hal_mcu_disable_interrupts(); common_hal_digitalio_digitalinout_switch_to_output(&self->pin, false, DRIVE_MODE_OPEN_DRAIN); - common_hal_mcu_delay_us(bit? 6 : 60); + mp_hal_delay_us_loop(bit? 6 : 60); common_hal_digitalio_digitalinout_switch_to_input(&self->pin, PULL_NONE); - common_hal_mcu_delay_us(bit? 64 : 10); + mp_hal_delay_us_loop(bit? 64 : 10); common_hal_mcu_enable_interrupts(); } From b4fd77bb7c041d2855dba826f380ce66944c23d4 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Fri, 29 Jun 2018 00:00:05 -0400 Subject: [PATCH 2/5] fix nrf and esp8266 builds for OneWire fix --- ports/atmel-samd/mphalport.c | 26 ++++++++++++++++++++++++++ ports/esp8266/Makefile | 1 + ports/esp8266/mphalport.c | 33 +++++++++++++++++++++++++++++++++ ports/esp8266/mphalport.h | 34 ++++++++++++++++++++++++++++++++++ ports/nrf/mphalport.c | 4 +++- 5 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 ports/esp8266/mphalport.c create mode 100644 ports/esp8266/mphalport.h diff --git a/ports/atmel-samd/mphalport.c b/ports/atmel-samd/mphalport.c index dd6a1471349c1..7bb423a3e676f 100644 --- a/ports/atmel-samd/mphalport.c +++ b/ports/atmel-samd/mphalport.c @@ -1,3 +1,29 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Scott Shawcroft + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + #include #include "lib/mp-readline/readline.h" diff --git a/ports/esp8266/Makefile b/ports/esp8266/Makefile index 8fabd3412a5e2..02b75eadd5195 100644 --- a/ports/esp8266/Makefile +++ b/ports/esp8266/Makefile @@ -92,6 +92,7 @@ SRC_C = \ machine_hspi.c \ modesp.c \ modnetwork.c \ + mphalport.c \ ets_alt_task.c \ fatfs_port.c \ posix_helpers.c \ diff --git a/ports/esp8266/mphalport.c b/ports/esp8266/mphalport.c new file mode 100644 index 0000000000000..e22facef041ad --- /dev/null +++ b/ports/esp8266/mphalport.c @@ -0,0 +1,33 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 Dan Halbert for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "esp_mphal.h" + +#include + +void mp_hal_delay_us_loop(uint32_t us) { + mp_hal_delay_us(us); +} diff --git a/ports/esp8266/mphalport.h b/ports/esp8266/mphalport.h new file mode 100644 index 0000000000000..9cf2fcdd6f12a --- /dev/null +++ b/ports/esp8266/mphalport.h @@ -0,0 +1,34 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 Dan Halbert for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MICROPY_INCLUDED_ESP8266_MPHALPORT_H +#define MICROPY_INCLUDED_ESP8266_MPHALPORT_H + +#include + +void mp_hal_delay_us_loop(uint32_t us); + +#endif // MICROPY_INCLUDED_ESP8266_MPHALPORT_H diff --git a/ports/nrf/mphalport.c b/ports/nrf/mphalport.c index 1f732e1889a19..014d410ecfeff 100644 --- a/ports/nrf/mphalport.c +++ b/ports/nrf/mphalport.c @@ -32,6 +32,8 @@ #include "py/mperrno.h" #include "hal_uart.h" +extern uint32_t common_hal_mcu_processor_get_frequency(void); + #define UART_INSTANCE UART_BASE(0) FIL* boot_output_file; @@ -90,7 +92,7 @@ void mp_hal_stdout_tx_str(const char *str) { // Testing done at 48 MHz on SAMD21 and 120 MHz on SAMD51 (cache on). // TODO: Test on NRF. For now, use SAMD51 calibration, even though nRF52 runs slower. // Fraction should compensate. -#define DELAY_LOOP_ITERATIONS_PER_US (30U*120000000U) / common_hal_mcu_processor_get_frequency()) +#define DELAY_LOOP_ITERATIONS_PER_US ( (30U*120000000U) / common_hal_mcu_processor_get_frequency()) void mp_hal_delay_us_loop(uint32_t us) { for (uint32_t i = us*DELAY_LOOP_ITERATIONS_PER_US; i > 0; i--) { From 2a0b8576439b0cfefff909135dba424a2a179275 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Fri, 29 Jun 2018 16:01:46 -0400 Subject: [PATCH 3/5] implement mp_hal_delay_us() to not need interrupts, and use it --- ports/atmel-samd/mphalport.c | 9 +++----- ports/atmel-samd/mphalport.h | 2 -- ports/esp8266/Makefile | 1 - .../nrf/common-hal/microcontroller/__init__.c | 3 ++- ports/nrf/mphalport.c | 14 ------------ ports/nrf/mphalport.h | 2 -- shared-module/bitbangio/OneWire.c | 22 +++++++++---------- 7 files changed, 15 insertions(+), 38 deletions(-) diff --git a/ports/atmel-samd/mphalport.c b/ports/atmel-samd/mphalport.c index 7bb423a3e676f..a4eca1c748c9c 100644 --- a/ports/atmel-samd/mphalport.c +++ b/ports/atmel-samd/mphalport.c @@ -99,10 +99,7 @@ void mp_hal_delay_ms(mp_uint_t delay) { } } -void mp_hal_delay_us(mp_uint_t delay) { - tick_delay(delay); -} - +// Use mp_hal_delay_us() for timing of less than 1ms. // Do a simple timing loop to wait for a certain number of microseconds. // Can be used when interrupts are disabled, which makes tick_delay() unreliable. // @@ -115,8 +112,8 @@ void mp_hal_delay_us(mp_uint_t delay) { #define DELAY_LOOP_ITERATIONS_PER_US ( (30U*120000000U) / common_hal_mcu_processor_get_frequency()) #endif -void mp_hal_delay_us_loop(uint32_t us) { - for (uint32_t i = us*DELAY_LOOP_ITERATIONS_PER_US; i > 0; i--) { +void mp_hal_delay_us(mp_uint_t delay) { + for (uint32_t i = delay*DELAY_LOOP_ITERATIONS_PER_US; i > 0; i--) { asm volatile("nop"); } } diff --git a/ports/atmel-samd/mphalport.h b/ports/atmel-samd/mphalport.h index 31d34c8c392f8..3bc824d4e9baa 100644 --- a/ports/atmel-samd/mphalport.h +++ b/ports/atmel-samd/mphalport.h @@ -50,6 +50,4 @@ void mp_hal_set_interrupt_char(int c); void mp_hal_disable_all_interrupts(void); void mp_hal_enable_all_interrupts(void); -void mp_hal_delay_us_loop(uint32_t us); - #endif // MICROPY_INCLUDED_ATMEL_SAMD_MPHALPORT_H diff --git a/ports/esp8266/Makefile b/ports/esp8266/Makefile index 02b75eadd5195..8fabd3412a5e2 100644 --- a/ports/esp8266/Makefile +++ b/ports/esp8266/Makefile @@ -92,7 +92,6 @@ SRC_C = \ machine_hspi.c \ modesp.c \ modnetwork.c \ - mphalport.c \ ets_alt_task.c \ fatfs_port.c \ posix_helpers.c \ diff --git a/ports/nrf/common-hal/microcontroller/__init__.c b/ports/nrf/common-hal/microcontroller/__init__.c index e26acff1b4cbc..4399e7708c480 100644 --- a/ports/nrf/common-hal/microcontroller/__init__.c +++ b/ports/nrf/common-hal/microcontroller/__init__.c @@ -33,6 +33,8 @@ #include "shared-bindings/microcontroller/Processor.h" // TODO porting common_hal_mcu +// This routine should work even when interrupts are disabled. Used by OneWire +// for precise timing. void common_hal_mcu_delay_us(uint32_t delay) { // os_delay_us(delay); } @@ -58,4 +60,3 @@ const mcu_processor_obj_t common_hal_mcu_processor_obj = { .type = &mcu_processor_type, }, }; - diff --git a/ports/nrf/mphalport.c b/ports/nrf/mphalport.c index 014d410ecfeff..d0447f06c39ef 100644 --- a/ports/nrf/mphalport.c +++ b/ports/nrf/mphalport.c @@ -85,17 +85,3 @@ void mp_hal_stdout_tx_strn_cooked(const char *str, mp_uint_t len) { void mp_hal_stdout_tx_str(const char *str) { mp_hal_stdout_tx_strn(str, strlen(str)); } - -// Do a simple timing loop to wait for a certain number of microseconds. -// Can be used when interrupts are disabled, which makes tick_delay() unreliable. -// -// Testing done at 48 MHz on SAMD21 and 120 MHz on SAMD51 (cache on). -// TODO: Test on NRF. For now, use SAMD51 calibration, even though nRF52 runs slower. -// Fraction should compensate. -#define DELAY_LOOP_ITERATIONS_PER_US ( (30U*120000000U) / common_hal_mcu_processor_get_frequency()) - -void mp_hal_delay_us_loop(uint32_t us) { - for (uint32_t i = us*DELAY_LOOP_ITERATIONS_PER_US; i > 0; i--) { - asm volatile("nop"); - } -} diff --git a/ports/nrf/mphalport.h b/ports/nrf/mphalport.h index 79e1d2d58ee9d..a872477370221 100644 --- a/ports/nrf/mphalport.h +++ b/ports/nrf/mphalport.h @@ -52,7 +52,6 @@ static inline uint32_t hal_tick_fake(void) { extern const unsigned char mp_hal_status_to_errno_table[4]; - NORETURN void mp_hal_raise(HAL_StatusTypeDef status); void mp_hal_set_interrupt_char(int c); // -1 to disable @@ -70,7 +69,6 @@ bool mp_hal_stdin_any(void); #define mp_hal_pin_od_high(p) mp_hal_pin_high(p) #define mp_hal_pin_open_drain(p) hal_gpio_cfg_pin(p->port, p->pin, HAL_GPIO_MODE_INPUT, HAL_GPIO_PULL_DISABLED) -void mp_hal_delay_us_loop(uint32_t us); // TODO: empty implementation for now. Used by machine_spi.c:69 #define mp_hal_delay_us_fast(p) diff --git a/shared-module/bitbangio/OneWire.c b/shared-module/bitbangio/OneWire.c index b226b9d7847a8..f5f4790876fbc 100644 --- a/shared-module/bitbangio/OneWire.c +++ b/shared-module/bitbangio/OneWire.c @@ -24,8 +24,6 @@ * THE SOFTWARE. */ -#include "mphalport.h" - #include "common-hal/microcontroller/Pin.h" #include "shared-bindings/bitbangio/OneWire.h" #include "shared-bindings/microcontroller/__init__.h" @@ -51,17 +49,17 @@ void shared_module_bitbangio_onewire_deinit(bitbangio_onewire_obj_t* self) { common_hal_digitalio_digitalinout_deinit(&self->pin); } -// We can't use common_hal_mcu_delay_us() here because it needs interrupts to be accurate -// due to SysTick rollover checking, done by an interrupt. +// We use common_hal_mcu_delay_us(). It should not be dependent on interrupts +// to do accurate timekeeping, since we disable interrupts during the delays below. bool shared_module_bitbangio_onewire_reset(bitbangio_onewire_obj_t* self) { common_hal_mcu_disable_interrupts(); common_hal_digitalio_digitalinout_switch_to_output(&self->pin, false, DRIVE_MODE_OPEN_DRAIN); - mp_hal_delay_us_loop(480); + common_hal_mcu_delay_us(480); common_hal_digitalio_digitalinout_switch_to_input(&self->pin, PULL_NONE); - mp_hal_delay_us_loop(70); + common_hal_mcu_delay_us(70); bool value = common_hal_digitalio_digitalinout_get_value(&self->pin); - mp_hal_delay_us_loop(410); + common_hal_mcu_delay_us(410); common_hal_mcu_enable_interrupts(); return value; } @@ -69,14 +67,14 @@ bool shared_module_bitbangio_onewire_reset(bitbangio_onewire_obj_t* self) { bool shared_module_bitbangio_onewire_read_bit(bitbangio_onewire_obj_t* self) { common_hal_mcu_disable_interrupts(); common_hal_digitalio_digitalinout_switch_to_output(&self->pin, false, DRIVE_MODE_OPEN_DRAIN); - mp_hal_delay_us_loop(6); + common_hal_mcu_delay_us(6); common_hal_digitalio_digitalinout_switch_to_input(&self->pin, PULL_NONE); // TODO(tannewt): Test with more devices and maybe make the delays // configurable. This should be 9 by the datasheet but all bits read as 1 // then. - mp_hal_delay_us_loop(6); + common_hal_mcu_delay_us(6); bool value = common_hal_digitalio_digitalinout_get_value(&self->pin); - mp_hal_delay_us_loop(55); + common_hal_mcu_delay_us(55); common_hal_mcu_enable_interrupts(); return value; } @@ -85,8 +83,8 @@ void shared_module_bitbangio_onewire_write_bit(bitbangio_onewire_obj_t* self, bool bit) { common_hal_mcu_disable_interrupts(); common_hal_digitalio_digitalinout_switch_to_output(&self->pin, false, DRIVE_MODE_OPEN_DRAIN); - mp_hal_delay_us_loop(bit? 6 : 60); + common_hal_mcu_delay_us(bit? 6 : 60); common_hal_digitalio_digitalinout_switch_to_input(&self->pin, PULL_NONE); - mp_hal_delay_us_loop(bit? 64 : 10); + common_hal_mcu_delay_us(bit? 64 : 10); common_hal_mcu_enable_interrupts(); } From 7028a399c2f29ee71dafbbf1536244b1b01b8303 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Fri, 29 Jun 2018 16:04:18 -0400 Subject: [PATCH 4/5] remove previously added esp8266/mphalport.{c,h} --- ports/esp8266/mphalport.c | 33 --------------------------------- ports/esp8266/mphalport.h | 34 ---------------------------------- 2 files changed, 67 deletions(-) delete mode 100644 ports/esp8266/mphalport.c delete mode 100644 ports/esp8266/mphalport.h diff --git a/ports/esp8266/mphalport.c b/ports/esp8266/mphalport.c deleted file mode 100644 index e22facef041ad..0000000000000 --- a/ports/esp8266/mphalport.c +++ /dev/null @@ -1,33 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2018 Dan Halbert for Adafruit Industries - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include "esp_mphal.h" - -#include - -void mp_hal_delay_us_loop(uint32_t us) { - mp_hal_delay_us(us); -} diff --git a/ports/esp8266/mphalport.h b/ports/esp8266/mphalport.h deleted file mode 100644 index 9cf2fcdd6f12a..0000000000000 --- a/ports/esp8266/mphalport.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2018 Dan Halbert for Adafruit Industries - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#ifndef MICROPY_INCLUDED_ESP8266_MPHALPORT_H -#define MICROPY_INCLUDED_ESP8266_MPHALPORT_H - -#include - -void mp_hal_delay_us_loop(uint32_t us); - -#endif // MICROPY_INCLUDED_ESP8266_MPHALPORT_H From 7c9a0e29960948eaad4485481355a84059148df4 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Fri, 29 Jun 2018 16:06:51 -0400 Subject: [PATCH 5/5] remove unnecessary extern in nrf/mphalport.c --- ports/nrf/mphalport.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/ports/nrf/mphalport.c b/ports/nrf/mphalport.c index d0447f06c39ef..957d3535d296a 100644 --- a/ports/nrf/mphalport.c +++ b/ports/nrf/mphalport.c @@ -32,8 +32,6 @@ #include "py/mperrno.h" #include "hal_uart.h" -extern uint32_t common_hal_mcu_processor_get_frequency(void); - #define UART_INSTANCE UART_BASE(0) FIL* boot_output_file;