From 2b1242aaae8713fc0b702f81539f21f49886b204 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Wed, 2 Aug 2017 13:44:16 +0200 Subject: [PATCH] global: add ms in us sleep utilities --- global.c | 45 +++++++++++++++++++++++++++++++++++++++++++-- global.h | 10 ++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/global.c b/global.c index 6e03c56..5febabe 100644 --- a/global.c +++ b/global.c @@ -15,23 +15,26 @@ /** global definitions and methods (code) * @file global.c * @author King Kévin - * @date 2016 + * @date 2016-2017 */ /* standard libraries */ #include // standard integer types #include // general utilities /* STM32 (including CM3) libraries */ +#include // Cortex M3 utilities +#include // interrupt handler +#include // SysTick library #include // real-time control clock library #include // general purpose input output library #include // timer library -#include // interrupt handler #include // external interrupt defines #include "global.h" // common methods #include "string.h" // memory utilities volatile bool button_flag = false; +volatile uint32_t sleep_duration = 0; /**< sleep duration count down (in SysTick interrupts) */ char* b2s(uint64_t binary, uint8_t rjust) { @@ -79,6 +82,44 @@ void led_toggle(void) gpio_toggle(GPIO(LED_PORT), GPIO(LED_PIN)); } +void sleep_us(uint32_t duration) +{ + systick_counter_disable(); // disable SysTick to reconfigure it + systick_clear(); // reset SysTick + systick_set_frequency(1000000,rcc_ahb_frequency); // set SysTick frequency to microseconds + systick_interrupt_enable(); // enable interrupt to count duration + sleep_duration = duration; // save sleep duration for count down + systick_counter_enable(); // start counting + while (sleep_duration) { // wait for count down to complete + __WFI(); // go to sleep + } +} + +void sleep_ms(uint32_t duration) +{ + systick_counter_disable(); // disable SysTick to reconfigure it + systick_clear(); // reset SysTick + systick_set_frequency(1000,rcc_ahb_frequency); // set SysTick frequency to milliseconds + systick_interrupt_enable(); // enable interrupt to count duration + sleep_duration = duration; // save sleep duration for count down + systick_counter_enable(); // start counting + while (sleep_duration) { // wait for count down to complete + __WFI(); // go to sleep + } +} + +/** SysTick interrupt handler */ +void sys_tick_handler(void) +{ + if (sleep_duration) { + sleep_duration--; // decrement duration + } + if (0==sleep_duration) { // sleep complete + systick_counter_disable(); // stop systick + systick_interrupt_disable(); // stop interrupting + } +} + void board_setup(void) { // setup LED diff --git a/global.h b/global.h index 2f5cb35..5d73604 100644 --- a/global.h +++ b/global.h @@ -362,6 +362,16 @@ void led_off(void); /** toggle board LED */ void led_toggle(void); +/** go to sleep for some microseconds + * @param[in] duration sleep duration in us + */ +void sleep_us(uint32_t duration); + +/** go to sleep for some milliseconds + * @param[in] duration sleep duration in ms + */ +void sleep_ms(uint32_t duration); + /** setup board peripherals */ void board_setup(void);