Compare commits

..

No commits in common. "b7951f22118b42eeebebf9506b4adb1fa87bc83d" and "7e69bfd89c9c0b3659035871717d81ad9308a9ba" have entirely different histories.

6 changed files with 25 additions and 228 deletions

View File

@ -21,7 +21,6 @@ static volatile struct STK* regs = (struct STK*)STK_BASE_ADDRESS;
//--local variables-------------------------------------------------------------
static StkCallback callback;
static uint32_t prescaler;
//--public functions------------------------------------------------------------
@ -33,12 +32,10 @@ uint32_t stk_configure(uint32_t period_us, StkCallback cb)
struct RccClocks clocks;
rcc_get_clocks(&clocks);
uint32_t prescaler = (clocks.ahb_freq / 1000000 / 8);
uint32_t reload = period_us * prescaler;
uint32_t reload = period_us * (clocks.ahb_freq / 1000000 / 8);
if (reload < 1) {
//period is too small, try using the non-prescaled clock
prescaler *= 8;
reload = period_us * prescaler;
reload = period_us * (clocks.ahb_freq / 1000000);
if (reload < 1) {
//period is still too small
return 1;
@ -74,9 +71,9 @@ void stk_stop(void)
reg_reset(regs->CTRL, STK_CTRL_ENABLE);
}
uint32_t stk_read_us(void)
uint32_t stk_read(void)
{
return (regs->VAL.word & 0x00FFFFFF) / prescaler;
return regs->VAL.word & 0x00FFFFFF;
}

View File

@ -46,9 +46,9 @@ void stk_start(void);
void stk_stop(void);
/**
* Read the current value of the timer's counter in µs
* Read the current value of the timer's counter
*/
uint32_t stk_read_us(void);
uint32_t stk_read(void);
#endif //_STK_H_

View File

@ -1,23 +0,0 @@
/** @file delay.c
* Module handling delays definitions and operations
*
* The module provides an API to delay the code execution with the most
* accuraccy possible, putting the system in sleep whenever possible. The RTC is
* required when using delays superior to a 1.5s. Accuraccy is only garenteed up
* to the unit being used (to the millisecond when delaying in ms, to the second
* when delaying in seconds)
*/
//--includes--------------------------------------------------------------------
#include "delay.h"
//--local definitions-----------------------------------------------------------
//--local variables-------------------------------------------------------------
//--public functions------------------------------------------------------------
//--local functions-------------------------------------------------------------

View File

@ -1,23 +0,0 @@
/** @file delay.h
* Module handling delays definitions and operations
*
* The module provides an API to delay the code execution with the most
* accuraccy possible, putting the system in sleep whenever possible. The RTC is
* required when using delays superior to a 1.5s. Accuraccy is only garenteed up
* to the unit being used (to the millisecond when delaying in ms, to the second
* when delaying in seconds)
*/
#ifndef _DELAY_H_
#define _DELAY_H_
//--includes--------------------------------------------------------------------
//--type definitions------------------------------------------------------------
//--functions-------------------------------------------------------------------
//--internal_functions----------------------------------------------------------
#endif //_DELAY_H_

View File

@ -6,85 +6,3 @@
* see https://dunkels.com/adam/pt/index.html
*/
//--includes--------------------------------------------------------------------
#include "task.h"
#include "error.h"
//--local definitions-----------------------------------------------------------
#define MAX_TASK_NB 10
//--local variables-------------------------------------------------------------
static struct Task task_list[MAX_TASK_NB];
static uint8_t task_nb;
//--public functions------------------------------------------------------------
void task_schedule(uint32_t elapsed_ms)
{
for (uint8_t i = 0; i < task_nb; ++i) {
if (task_list[i].function != nullptr) {
if (task_list[i].state.is_active
|| task_list[i].state.delay <= 0) {
task_list[i].function(&task_list[i].state);
if (task_list[i].state.count == _TASK_COUNT_EXIT) {
task_list[i].function = nullptr;
}
} else {
task_list[i].state.delay -= elapsed_ms;
}
}
}
}
void task_start(TaskFunction function)
{
for (uint8_t i = 0; i < MAX_TASK_NB; ++i) {
if (task_list[i].function == nullptr) {
task_list[i].function = function;
task_list[i].state.delay = 0;
task_list[i].state.count = 0;
task_list[i].state.is_active = false;
++task_nb;
return;
}
}
error_trigger("task list is full");
}
void task_stop(TaskFunction function)
{
for (uint8_t i = 0; i < task_nb; ++i) {
if (task_list[i].function == function) {
task_list[i].state.count = _TASK_COUNT_CLEANUP & 0x7F;
task_list[i].function(&task_list[i].state);
task_list[i].function = nullptr;
return;
}
}
error_trigger("task does not exist");
}
bool task_is_running(TaskFunction function)
{
for (uint8_t i = 0; i < task_nb; ++i) {
if (task_list[i].function == function) {
return true;
}
}
return false;
}

View File

@ -12,113 +12,41 @@
//--includes--------------------------------------------------------------------
#include <stdint.h>
#include <stdbool.h>
//--type definitions------------------------------------------------------------
struct TaskState {
int32_t delay;
uint8_t count:7;
uint8_t is_active:1;
};
typedef void(*TaskFunction)(struct TaskState*);
struct Task {
TaskFunction function;
struct TaskState state;
};
//--functions-------------------------------------------------------------------
#define TASK(fct_name) void fct_name(struct TaskState* restrict __task_state)
#define TASK(fct_name) uint8_t fct_name(uint8_t __task_context)
#define TASK_ENTRY \
_TASK_COUNT_INIT; \
switch (__task_state->count) {
#define task_begin() do { \
_TASK_COUNT_INIT; \
switch (__task_context) { \
} while (0)
#define TASK_CLEANUP \
case (_TASK_COUNT_CLEANUP): \
/* fall through */
#define task_end() do { \
} \
return _TASK_COUNT_STOP; \
while (0)
#define TASK_EXIT \
} \
__task_state->count = _TASK_COUNT_EXIT; \
return;
#define TASK_TIMEOUT
#define TASK_YIELD() _TASK_YIELD(_TASK_COUNT_INCR)
#define TASK_SLEEP(delay_ms) _TASK_SLEEP(delay_ms, _TASK_COUNT_INCR)
#define TASK_YIELD_UNTIL(cond) _TASK_YIELD_UNTIL(cond, _TASK_COUNT_INCR)
#define TASK_SLEEP_UNTIL(cond, delay_ms) \
_TASK_SLEEP_UNTIL(cond, delay_ms, _TASK_COUNT_INCR)
#define TASK_EXECUTE(task) _TASK_EXECUTE(task, _TASK_COUNT_INCR)
void task_schedule(uint32_t elapsed_ms);
void task_declare();
void task_start(TaskFunction task);
void task_stop(TaskFunction task);
bool task_is_running(TaskFunction task);
#define task_wait_until(cond) _task_wait_until(cond, _TASK_COUNT_INCR)
//--internal_functions----------------------------------------------------------
#define _TASK_COUNT_INIT enum { TASK_COUNTER_BASE = __COUNTER__ }
#define _TASK_COUNT_INCR (uint8_t)(__COUNTER__ - TASK_COUNTER_BASE - 1)
#define _TASK_COUNT_EXIT (UINT8_MAX & 0x7F)
#define _TASK_COUNT_CLEANUP (UINT8_MAX - 1)
#define _TASK_COUNT_STOP UINT8_MAX
#define _TASK_YIELD(count_val) do { \
__task_state->count = count_val; \
return; \
case (count_val): \
/* fall through */ \
} while (0)
#define _task(fct_name)
#define _TASK_SLEEP(delay_ms, count_val) do { \
__task_state->count = count_val; \
__task_state->delay = delay_ms; \
return; \
case (count_val): \
/* fall through */ \
} while (0)
#define _task_wait_until(cond, count) do { \
case (count): \
if (!(cond)) { \
return count; \
} \
/* fall through */ \
while (0)
#define _TASK_YIELD_UNTIL(cond, count_val) do { \
__task_state->count = count_val; \
case (count_val): \
if (!(cond)) { \
return; \
} \
/* fall through */ \
} while (0)
#define _TASK_SLEEP_UNTIL(cond, delay_ms, count_val) do { \
__task_state->count = count_val; \
__task_state->delay = delay_ms; \
__task_state->is_active = true; \
case (count_val): \
if (!(cond) && __task_state->delay >= 0) { \
return; \
} \
__task_state->is_active = false; \
/* fall through */ \
} while (0)
#define _TASK_EXECUTE(task, count_val) do { \
__task_state->count = count_val; \
task_start(Task task); \
return; \
case (count_val): \
if (task_is_running(task)) { \
return; \
} \
/* fall through */ \
} while (0)
#endif //_task_h_