global: minor code improvement

This commit is contained in:
King Kévin 2018-03-01 17:44:18 +01:00
parent 013092166e
commit d8934dc4c5
1 changed files with 13 additions and 7 deletions

View File

@ -34,13 +34,14 @@
#include "global.h" // common methods
volatile bool button_flag = false;
volatile uint32_t sleep_duration = 0; /**< sleep duration count down (in SysTick interrupts) */
volatile bool user_input_available = false;
static volatile uint8_t user_input_buffer[64] = {0}; /**< ring buffer for received data */
static volatile uint8_t user_input_i = 0; /**< current position of read received data */
static volatile uint8_t user_input_used = 0; /**< how much data has been received and not red */
static volatile uint32_t sleep_duration = 0; /**< sleep duration count down (in SysTick interrupts) */
char* b2s(uint64_t binary, uint8_t rjust)
{
static char string[64+1] = {0}; // the string representation to return
@ -143,12 +144,14 @@ void led_red(void)
void sleep_us(uint32_t duration)
{
systick_counter_disable(); // disable SysTick to reconfigure it
if (!systick_set_frequency(1000000,rcc_ahb_frequency)) { // set SysTick frequency to microseconds
while (true); // unhandled error
}
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
while (sleep_duration>0) { // wait for count down to complete
__WFI(); // go to sleep
}
}
@ -156,25 +159,28 @@ void sleep_us(uint32_t duration)
void sleep_ms(uint32_t duration)
{
systick_counter_disable(); // disable SysTick to reconfigure it
if (!systick_set_frequency(1000,rcc_ahb_frequency)) { // set SysTick frequency to milliseconds
while (true); // unhandled error
}
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
while (sleep_duration>0) { // wait for count down to complete
// __WFI(); // go to sleep
}
}
/** SysTick interrupt handler */
void sys_tick_handler(void)
{
if (sleep_duration) {
if (sleep_duration>0) {
sleep_duration--; // decrement duration
}
if (0==sleep_duration) { // sleep complete
systick_counter_disable(); // stop systick
systick_interrupt_disable(); // stop interrupting
sleep_duration = 0; // ensure it still is at 0
}
}