stmhal: Rename sys_tick ticks/delay functions to corresp. mp_hal ones.

The renames are:
HAL_Delay -> mp_hal_delay_ms
sys_tick_udelay -> mp_hal_delay_us
sys_tick_get_microseconds -> mp_hal_ticks_us

And mp_hal_ticks_ms is added to provide the full set of timing functions.

Also, a separate HAL_Delay function is added which differs slightly from
mp_hal_delay_ms and is intended for use only by the ST HAL functions.
This commit is contained in:
Damien George 2017-03-02 15:32:32 +11:00
parent 6ab5512132
commit 89738e8240
7 changed files with 39 additions and 26 deletions

View File

@ -39,7 +39,7 @@ mp_uint_t gc_helper_get_regs_and_sp(mp_uint_t *regs);
void gc_collect(void) {
// get current time, in case we want to time the GC
#if 0
uint32_t start = sys_tick_get_microseconds();
uint32_t start = mp_hal_ticks_us();
#endif
// start the GC
@ -66,7 +66,7 @@ void gc_collect(void) {
#if 0
// print GC info
uint32_t ticks = sys_tick_get_microseconds() - start;
uint32_t ticks = mp_hal_ticks_us() - start;
gc_info_t info;
gc_info(&info);
printf("GC@%lu %lums\n", start, ticks);

View File

@ -34,6 +34,7 @@
#include "py/obj.h"
#include "py/gc.h"
#include "py/builtin.h"
#include "py/mphal.h"
#include "lib/utils/pyexec.h"
#include "lib/oofatfs/ff.h"
#include "lib/oofatfs/diskio.h"
@ -112,7 +113,7 @@ STATIC mp_obj_t pyb_micros(void) {
// We want to "cast" the 32 bit unsigned into a small-int. This means
// copying the MSB down 1 bit (extending the sign down), which is
// equivalent to just using the MP_OBJ_NEW_SMALL_INT macro.
return MP_OBJ_NEW_SMALL_INT(sys_tick_get_microseconds());
return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_us());
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_micros_obj, pyb_micros);
@ -128,7 +129,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_micros_obj, pyb_micros);
/// # Perform some operation
STATIC mp_obj_t pyb_elapsed_micros(mp_obj_t start) {
uint32_t startMicros = mp_obj_get_int(start);
uint32_t currMicros = sys_tick_get_microseconds();
uint32_t currMicros = mp_hal_ticks_us();
return MP_OBJ_NEW_SMALL_INT((currMicros - startMicros) & 0x3fffffff);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_elapsed_micros_obj, pyb_elapsed_micros);

View File

@ -27,13 +27,7 @@ void mp_hal_set_interrupt_char(int c); // -1 to disable
// timing functions
#include "stmhal/systick.h"
#define mp_hal_delay_ms HAL_Delay
#define mp_hal_delay_us(us) sys_tick_udelay(us)
#define mp_hal_delay_us_fast(us) sys_tick_udelay(us)
#define mp_hal_ticks_ms HAL_GetTick
#define mp_hal_ticks_us() sys_tick_get_microseconds()
#define mp_hal_delay_us_fast(us) mp_hal_delay_us(us)
extern bool mp_hal_ticks_cpu_enabled;
void mp_hal_ticks_cpu_enable(void);

View File

@ -28,9 +28,9 @@
#include "py/nlr.h"
#include "py/runtime.h"
#include "py/mphal.h"
#include "lib/oofatfs/ff.h"
#include "extmod/vfs_fat.h"
#include "mphalport.h"
#include "sdcard.h"
#include "pin.h"

View File

@ -298,7 +298,7 @@ void SysTick_Handler(void) {
uwTick += 1;
// Read the systick control regster. This has the side effect of clearing
// the COUNTFLAG bit, which makes the logic in sys_tick_get_microseconds
// the COUNTFLAG bit, which makes the logic in mp_hal_ticks_us
// work properly.
SysTick->CTRL;

View File

@ -24,20 +24,36 @@
* THE SOFTWARE.
*/
#include STM32_HAL_H
#include "py/obj.h"
#include "py/mphal.h"
#include "irq.h"
#include "systick.h"
#include "pybthread.h"
// We provide our own version of HAL_Delay that calls __WFI while waiting, in
// order to reduce power consumption.
// Note: Upon entering this function we may or may not have the GIL.
extern __IO uint32_t uwTick;
// We provide our own version of HAL_Delay that calls __WFI while waiting,
// and works when interrupts are disabled. This function is intended to be
// used only by the ST HAL functions.
void HAL_Delay(uint32_t Delay) {
if (query_irq() == IRQ_STATE_ENABLED) {
// IRQs enabled, so can use systick counter to do the delay
extern __IO uint32_t uwTick;
uint32_t start = uwTick;
// Wraparound of tick is taken care of by 2's complement arithmetic.
while (uwTick - start < Delay) {
// Enter sleep mode, waiting for (at least) the SysTick interrupt.
__WFI();
}
} else {
// IRQs disabled, use mp_hal_delay_ms routine.
mp_hal_delay_ms(Delay);
}
}
// Core delay function that does an efficient sleep and may switch thread context.
// Note: Upon entering this function we may or may not have the GIL.
void mp_hal_delay_ms(mp_uint_t Delay) {
if (query_irq() == IRQ_STATE_ENABLED) {
// IRQs enabled, so can use systick counter to do the delay
uint32_t start = uwTick;
// Wraparound of tick is taken care of by 2's complement arithmetic.
while (uwTick - start < Delay) {
@ -64,11 +80,11 @@ void HAL_Delay(uint32_t Delay) {
}
// delay for given number of microseconds
void sys_tick_udelay(uint32_t usec) {
void mp_hal_delay_us(mp_uint_t usec) {
if (query_irq() == IRQ_STATE_ENABLED) {
// IRQs enabled, so can use systick counter to do the delay
uint32_t start = sys_tick_get_microseconds();
while (sys_tick_get_microseconds() - start < usec) {
uint32_t start = mp_hal_ticks_us();
while (mp_hal_ticks_us() - start < usec) {
}
} else {
// IRQs disabled, so need to use a busy loop for the delay
@ -92,11 +108,15 @@ void sys_tick_wait_at_least(uint32_t start_tick, uint32_t delay_ms) {
}
}
mp_uint_t mp_hal_ticks_ms(void) {
return uwTick;
}
// The SysTick timer counts down at 168 MHz, so we can use that knowledge
// to grab a microsecond counter.
//
// We assume that HAL_GetTickis returns milliseconds.
uint32_t sys_tick_get_microseconds(void) {
mp_uint_t mp_hal_ticks_us(void) {
mp_uint_t irq_state = disable_irq();
uint32_t counter = SysTick->VAL;
uint32_t milliseconds = HAL_GetTick();

View File

@ -24,7 +24,5 @@
* THE SOFTWARE.
*/
void sys_tick_udelay(uint32_t usec);
void sys_tick_wait_at_least(uint32_t stc, uint32_t delay_ms);
bool sys_tick_has_passed(uint32_t stc, uint32_t delay_ms);
uint32_t sys_tick_get_microseconds(void);