8000 implement mp_hal_delay_us() to not need interrupts, and use it · sparkfun/circuitpython@2a0b857 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2a0b857

Browse files
committed
implement mp_hal_delay_us() to not need interrupts, and use it
1 parent b4fd77b commit 2a0b857

File tree

7 files changed

+15
-38
lines changed

7 files changed

+15
-38
lines changed

ports/atmel-samd/mphalport.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@ 8000 @ -99,10 +99,7 @@ void mp_hal_delay_ms(mp_uint_t delay) {
9999
}
100100
}
101101

102-
void mp_hal_delay_us(mp_uint_t delay) {
103-
tick_delay(delay);
104-
}
105-
102+
// Use mp_hal_delay_us() for timing of less than 1ms.
106103
// Do a simple timing loop to wait for a certain number of microseconds.
107104
// Can be used when interrupts are disabled, which makes tick_delay() unreliable.
108105
//
@@ -115,8 +112,8 @@ void mp_hal_delay_us(mp_uint_t delay) {
115112
#define DELAY_LOOP_ITERATIONS_PER_US ( (30U*120000000U) / common_hal_mcu_processor_get_frequency())
116113
#endif
117114

118-
void mp_hal_delay_us_loop(uint32_t us) {
119-
for (uint32_t i = us*DELAY_LOOP_ITERATIONS_PER_US; i > 0; i--) {
115+
void mp_hal_delay_us(mp_uint_t delay) {
116+
for (uint32_t i = delay*DELAY_LOOP_ITERATIONS_PER_US; i > 0; i--) {
120117
asm volatile("nop");
121118
}
122119
}

ports/atmel-samd/mphalport.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,4 @@ void mp_hal_set_interrupt_char(int c);
5050
void mp_hal_disable_all_interrupts(void);
5151
void mp_hal_enable_all_interrupts(void);
5252

53-
void mp_hal_delay_us_loop(uint32_t us);
54-
5553
#endif // MICROPY_INCLUDED_ATMEL_SAMD_MPHALPORT_H

ports/esp8266/Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ SRC_C = \
9292
machine_hspi.c \
9393
modesp.c \
9494
modnetwork.c \
95-
mphalport.c \
9695
ets_alt_task.c \
9796
fatfs_port.c \
9897
posix_helpers.c \

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.c

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,3 @@ void mp_hal_stdout_tx_strn_cooked(const char *str, mp_uint_t len) {
8585
void mp_hal_stdout_tx_str(const char *str) {
8686
mp_hal_stdout_tx_strn(str, strlen(str));
8787
}
88-
89-
// Do a simple timing loop to wait for a certain number of microseconds.
90-
// Can be used when interrupts are disabled, which makes tick_delay() unreliable.
91-
//
92-
// Testing done at 48 MHz on SAMD21 and 120 MHz on SAMD51 (cache on).
93-
// TODO: Test on NRF. For now, use SAMD51 calibration, even though nRF52 runs slower.
94-
// Fraction should compensate.
95-
#define DELAY_LOOP_ITERATIONS_PER_US ( (30U*120000000U) / common_hal_mcu_processor_get_frequency())
96-
97-
void mp_hal_delay_us_loop(uint32_t us) {
98-
for (uint32_t i = us*DELAY_LOOP_ITERATIONS_PER_US; i > 0; i--) {
99-
asm volatile("nop");
100-
}
101-
}

ports/nrf/mphalport.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ static inline uint32_t hal_tick_fake(void) {
5252

5353
extern const unsigned char mp_hal_status_to_errno_table[4];
5454

55-
5655
NORETURN void mp_hal_raise(HAL_StatusTypeDef status);
5756
void mp_hal_set_interrupt_char(int c); // -1 to disable
5857

@@ -70,7 +69,6 @@ bool mp_hal_stdin_any(void);
7069
#define mp_hal_pin_od_high(p) mp_hal_pin_high(p)
7170
#define mp_hal_pin_open_drain(p) hal_gpio_cfg_pin(p->port, p->pin, HAL_GPIO_MODE_INPUT, HAL_GPIO_PULL_DISABLED)
7271

73-
void mp_hal_delay_us_loop(uint32_t us);
7472

7573
// TODO: empty implementation for now. Used by machine_spi.c:69
7674
#define mp_hal_delay_us_fast(p)

shared-module/bitbangio/OneWire.c

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

27-
#include "mphalport.h"
28-
2927
#include "common-hal/microcontroller/Pin.h"
3028
#include "shared-bindings/bitbangio/OneWire.h"
3129
#include "shared-bindings/microcontroller/__init__.h"
@@ -51,32 +49,32 @@ void shared_module_bitbangio_onewire_deinit(bitbangio_onewire_obj_t* self) {
5149
common_hal_digitalio_digitalinout_deinit(&self->pin);
5250
}
5351

54-
// We can't use common_hal_mcu_delay_us() here because it needs interrupts to be accurate
55-
// due to SysTick rollover checking, done by an interrupt.
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.
5654

5755
bool shared_module_bitbangio_onewire_reset(bitbangio_onewire_obj_t* self) {
5856
common_hal_mcu_disable_interrupts();
5957
common_hal_digitalio_digitalinout_switch_to_output(&self->pin, false, DRIVE_MODE_OPEN_DRAIN);
60-
mp_hal_delay_us_loop(480);
58+
common_hal_mcu_delay_us(480);
6159
common_hal_digitalio_digitalinout_switch_to_input(&self->pin, PULL_NONE);
62-
mp_hal_delay_us_loop(70);
60+
common_hal_mcu_delay_us(70);
6361
bool value = common_hal_digitalio_digitalinout_get_value(&self->pin);
64-
mp_hal_delay_us_loop(410);
62+
common_hal_mcu_delay_us(410);
6563
common_hal_mcu_enable_interrupts();
6664
return value;
6765
}
6866

6967
bool shared_module_bitbangio_onewire_read_bit(bitbangio_onewire_obj_t* self) {
7068
common_hal_mcu_disable_interrupts();
7169
common_hal_digitalio_digitalinout_switch_to_output(&self->pin, false, DRIVE_MODE_OPEN_DRAIN);
72-
mp_hal_delay_us_loop(6);
70+
common_hal_mcu_delay_us(6);
7371
common_hal_digitalio_digitalinout_switch_to_input(&self->pin, PULL_NONE);
7472
// TODO(tannewt): Test with more devices and maybe make the delays
7573
// configurable. This should be 9 by the datasheet but all bits read as 1
7674
// then.
77-
mp_hal_delay_us_loop(6);
75+
common_hal_mcu_delay_us(6);
7876
bool value = common_hal_digitalio_digitalinout_get_value(&self->pin);
79-
mp_hal_delay_us_loop(55);
77+
common_hal_mcu_delay_us(55);
8078
common_hal_mcu_enable_interrupts();
8179
return value;
8280
}
@@ -85,8 +83,8 @@ void shared_module_bitbangio_onewire_write_bit(bitbangio_onewire_obj_t* self,
8583
bool bit) {
8684
common_hal_mcu_disable_interrupts();
8785
common_hal_digitalio_digitalinout_switch_to_output(&self->pin, false, DRIVE_MODE_OPEN_DRAIN);
88-
mp_hal_delay_us_loop(bit? 6 : 60);
86+
common_hal_mcu_delay_us(bit? 6 : 60);
8987
common_hal_digitalio_digitalinout_switch_to_input(&self->pin, PULL_NONE);
90-
mp_hal_delay_us_loop(bit? 64 : 10);
88+
common_hal_mcu_delay_us(bit? 64 : 10);
9189
common_hal_mcu_enable_interrupts();
9290
}

0 commit comments

Comments
 (0)
0