summaryrefslogtreecommitdiffstatshomepage
path: root/stmhal/systick.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2017-03-02 15:32:32 +1100
committerDamien George <damien.p.george@gmail.com>2017-03-02 15:32:32 +1100
commit89738e8240b0ae6dcc24c48d8e9e5078da90e15a (patch)
tree94fe1425d6fda8e4bf9ed8d91256287828e763dd /stmhal/systick.c
parent6ab5512132d5b3d19a5bf029cb27829d41e76f8e (diff)
downloadmicropython-89738e8240b0ae6dcc24c48d8e9e5078da90e15a.tar.gz
micropython-89738e8240b0ae6dcc24c48d8e9e5078da90e15a.zip
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.
Diffstat (limited to 'stmhal/systick.c')
-rw-r--r--stmhal/systick.c42
1 files changed, 31 insertions, 11 deletions
diff --git a/stmhal/systick.c b/stmhal/systick.c
index ade05d74d7..eb11de9b74 100644
--- a/stmhal/systick.c
+++ b/stmhal/systick.c
@@ -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();