Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

configurable paths and support for custom targets #47

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions board/makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
INCLUDES += -Iboard/
INCLUDES += -I$(BOARDS_PATH)

SRCS += board/board_init.c
SRCS += $(BOARDS_PATH)board_init.c

# Include custom board makefile, where board specific source files can be added
# Does nothing, if file does not exist
Expand Down
3 changes: 3 additions & 0 deletions config.mk
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ arm_toolchain=
# and fallback to "python" cmd
python_interpreter=

# Override default board and source paths if needed here
BOARDS_PATH := ../board/
APPLICATIONS_PATH := ../source/
2 changes: 1 addition & 1 deletion libraries/positioning/poslib/poslib_da.c
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ static void stop_tag()
}
}

void PosLibDa_Stop()
void PosLibDa_stop()
{
stop_router();
stop_tag();
Expand Down
144 changes: 138 additions & 6 deletions libraries/scheduler/app_scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,23 @@ typedef struct
/** Structure of a task */
typedef struct
{
#if APP_SCHEDULER_ENABLE_ARGS_PASSING
union
{
task_cb_f func; /* Cb of this task */
const task_with_args_st * p_task_with_args; /* Cb of this task */
};
#else
task_cb_f func; /* Cb of this task */
#endif
timestamp_t next_ts;/* When is the next execution */
uint32_t exec_time_us; /* Time needed for execution */
bool updated; /* Updated in IRQ context? */
bool removed; /* Task removed, to be released */
#if APP_SCHEDULER_ENABLE_ARGS_PASSING
bool has_args;
#endif

} task_t;

/** List of tasks */
Expand Down Expand Up @@ -192,6 +204,10 @@ static bool is_timestamp_before(timestamp_t * ts1_p, timestamp_t * ts2_p)
*/
static void perform_task(task_t * task)
{

#ifdef APP_SCHEDULER_ENABLE_ARGS_PASSING
const task_with_args_st* p_task_with_args = NULL;
#endif
task_cb_f task_cb = NULL;
uint32_t next = APP_SCHEDULER_STOP_TASK;

Expand All @@ -201,13 +217,29 @@ static void perform_task(task_t * task)
return;
}

task_cb = task->func;
if (task_cb == NULL)
#ifdef APP_SCHEDULER_ENABLE_ARGS_PASSING
if(task->has_args)
{
return;
p_task_with_args = task->p_task_with_args;
if ((p_task_with_args == NULL) || (p_task_with_args->func == NULL))
{
// There is an issue, no next task
return;
}
// Execute the context task selected
next = p_task_with_args->func(p_task_with_args->p_args);
}
else
#endif
{
task_cb = task->func;
if (task_cb == NULL)
{
return;
}
// Execute the task selected
next = task_cb();
}
// Execute the task selected
next = task_cb();

// Update its next execution time under critical section
// to avoid overriding new value set by IRQ
Expand All @@ -220,7 +252,16 @@ static void perform_task(task_t * task)
{
// Task doesn't have to be executed again
// so safe to release it
task->func = NULL;
#ifdef APP_SCHEDULER_ENABLE_ARGS_PASSING
if(task->has_args)
{
task->p_task_with_args = NULL;
}
else
#endif
{
task->func = NULL;
}
}
else
{
Expand Down Expand Up @@ -351,6 +392,7 @@ static bool add_task_to_table_locked(task_t * task_p)
for (uint8_t i = 0; i < APP_SCHEDULER_ALL_TASKS; i++)
{
// First check if task already exist
// func is in union so compare in both cases is valid
if (m_tasks[i].func == task_p->func)
{
// Task found, just update the next timestamp and exit
Expand Down Expand Up @@ -437,6 +479,9 @@ app_scheduler_res_e App_Scheduler_addTask_execTime(task_cb_f cb,
.exec_time_us = exec_time_us,
.updated = false,
.removed = false,
#if APP_SCHEDULER_ENABLE_ARGS_PASSING
.has_args = false,
#endif
};
app_scheduler_res_e res;
get_timestamp(&new_task.next_ts, delay_ms);
Expand Down Expand Up @@ -509,3 +554,90 @@ app_scheduler_res_e App_Scheduler_cancelTask(task_cb_f cb)

return res;
}


#if APP_SCHEDULER_ENABLE_ARGS_PASSING
app_scheduler_res_e App_Scheduler_addTask_with_args_execTime(const task_with_args_st* p_task,
uint32_t delay_ms,
uint32_t exec_time_us)
{
task_t new_task = {
.p_task_with_args = p_task,
.exec_time_us = exec_time_us,
.updated = false,
.removed = false,
.has_args = true,
};
app_scheduler_res_e res;
get_timestamp(&new_task.next_ts, delay_ms);

if (!m_initialized)
{
return APP_SCHEDULER_RES_UNINITIALIZED;
}

Sys_enterCriticalSection();

if (!add_task_to_table_locked(&new_task))
{
res = APP_SCHEDULER_RES_NO_MORE_TASK;
}
else
{
// Check if reschedule is needed:
// - No next task
// - Updating current next scheduled task
// - New task is before next task
if (m_next_task_p == NULL
|| (m_next_task_p->p_task_with_args == p_task)
|| is_timestamp_before(&new_task.next_ts, &m_next_task_p->next_ts))
{
m_force_reschedule = true;
// Call our periodic work to update the next task ASAP
// with short exec time as no task will be executed
lib_system->setPeriodicCb(periodic_work,
0,
EXECUTION_TIME_NEEDED_FOR_SCHEDULING_US);
}
res = APP_SCHEDULER_RES_OK;
}

Sys_exitCriticalSection();

return res;
}

app_scheduler_res_e App_Scheduler_cancelTask_with_args(const task_with_args_st* p_task)
{
app_scheduler_res_e res;

if (!m_initialized)
{
return APP_SCHEDULER_RES_UNINITIALIZED;
}

Sys_enterCriticalSection();

// func and p_task_with_args are in union so casting will work here
task_t * removed_task = remove_task_from_table_locked((task_cb_f) p_task);
if (removed_task != NULL)
{
// Force our task to be reschedule asap to do the cleanup of the task
// and potentially change the next task to be schedule
m_force_reschedule = true;
lib_system->setPeriodicCb(periodic_work,
0,
EXECUTION_TIME_NEEDED_FOR_SCHEDULING_US);

res = APP_SCHEDULER_RES_OK;
}
else
{
res = APP_SCHEDULER_RES_UNKNOWN_TASK;
}

Sys_exitCriticalSection();

return res;
}
#endif
94 changes: 94 additions & 0 deletions libraries/scheduler/app_scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,27 @@
*/
typedef uint32_t (*task_cb_f)();


#if APP_SCHEDULER_ENABLE_ARGS_PASSING
/**
* \brief Task callback to be registered
*
* \param p_args user defined arguments variable. This can be useful
* if callback function execution depends on arguments passed
* \return Delay before being executed again in ms
* \note Return value in ms is aligned on system coarse timesatmp
* boundaries that has a 1/128s granularity. So asking 3 or 7 ms will
* result in same scheduling.
* If a better accuracy is needed, hardware timer must be used
*/
typedef uint32_t (*task_with_args_cb_f)(const void *p_args);

typedef struct {
const task_with_args_cb_f func;
const void * p_args;
} task_with_args_st;
#endif

/**
* \brief Value to return from task to remove it
*/
Expand Down Expand Up @@ -135,4 +156,77 @@ static inline app_scheduler_res_e App_Scheduler_addTask(task_cb_f cb, uint32_t d
*/
app_scheduler_res_e App_Scheduler_cancelTask(task_cb_f cb);

#if APP_SCHEDULER_ENABLE_ARGS_PASSING

/**
* \brief Add a task
*
* Example on use:
*
* @code
*
* static uint32_t periodic_task_50ms()
* {
* ...
* return 50;
* }
*
* static uint32_t periodic_task_500ms()
* {
* ...
* return 500;
* }
*
* void App_init(const app_global_functions_t * functions)
* {
* // Launch two periodic task with different period
* App_Scheduler_addTask_execTime(periodic_task_50ms, APP_SCHEDULER_SCHEDULE_ASAP, 10);
* App_Scheduler_addTask_execTime(periodic_task_500ms, APP_SCHEDULER_SCHEDULE_ASAP, 10);
* ...
* // Start the stack
* lib_state->startStack();
* }
* @endcode
*
*
* \param cb
* Callback to be called from main periodic task.
* Same cb can only be added once. Calling this function with an already
* registered cb will update the next scheduled time.
* \param delay_ms
* delay in ms to be scheduled (0 to be scheduled asap)
* \param exec_time_us
* Maximum execution time required for the task to be executed
* \return True if able to add, false otherwise
*/
app_scheduler_res_e App_Scheduler_addTask_with_args_execTime(const task_with_args_st* p_task, uint32_t delay_ms, uint32_t exec_time_us);

#ifdef APP_SCHEDULER_MAX_EXEC_TIME_US
/**
* \brief Add a task without execution time (deprecated)
* \param cb
* Callback to be called from main periodic task.
* Same cb can only be added once. Calling this function with an already
* registered cb will update the next scheduled time.
* \param delay_ms
* delay in ms to be scheduled (0 to be scheduled asap)
* \return True if able to add, false otherwise
*
* \note This call is deprecated and you should use @ref App_Scheduler_addTask_execTime instead
*/
static inline app_scheduler_res_e App_Scheduler_addTask_with_args(const task_with_args_st* p_task, uint32_t delay_ms)
{
return App_Scheduler_addTask_with_args_execTime(p_task, delay_ms, APP_SCHEDULER_MAX_EXEC_TIME_US);
}
#endif

/**
* \brief Cancel a task
* \param cb
* Callback already registered from App_Scheduler_addTask.
* \return True if able to cancel, false otherwise (not existing)
*/
app_scheduler_res_e App_Scheduler_cancelTask_with_args(const task_with_args_st* p_task);
#endif

#endif //_APP_SCHEDULER_H_
3 changes: 3 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,6 @@ clean: need_board
.PHONY: clean_all
clean_all:
rm -rf $(GLOBAL_BUILD)

# lets you create custom targets to do project specific post processing
-include $(APP_SRCS_PATH)custom_targets.mk
2 changes: 1 addition & 1 deletion makefile_app.mk
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ endif
CFLAGS += -DVER_MAJOR=$(app_major) -DVER_MINOR=$(app_minor) -DVER_MAINT=$(app_maintenance) -DVER_DEV=$(app_development)

# Include board init part
-include board/makefile
-include $(BOARDS_PATH)makefile

# Include app specific makefile
-include $(APP_SRCS_PATH)makefile
Expand Down
2 changes: 1 addition & 1 deletion makefile_bootloader.mk
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ BOOTLOADER_ELF := $(BL_BUILDPREFIX)bootloader.elf
-include bootloader/makefile

# Include board part (for BOARD_HW_xx defines)
-include board/makefile
-include $(BOARDS_PATH)makefile

# Include HAL drivers code (needed to build power.c (DCDC))
-include $(HAL_API_PATH)makefile
Expand Down
3 changes: 2 additions & 1 deletion makefile_common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ WP_LIB_PATH := libraries/
GLOBAL_BUILD := build/
BOARDS_PATH := board/
BOARDS_PATH_INTERNAL := board_internal/
APPLICATIONS_PATH := source/
MCU_PATH := mcu/

# General compiler flags (Define it before specific makefile in order to allow app to overwrite it)
Expand Down Expand Up @@ -135,7 +136,7 @@ INCLUDES += -I$(MCU_PATH)common/cmsis -I$(BOARD_FOLDER)

# Folder where the application sources are located (and config file)
# Can be in different folders, try them one by one
APP_POSSIBLE_FOLDER := source/*/$(app_name)/ source/$(app_name)/
APP_POSSIBLE_FOLDER := $(APPLICATIONS_PATH)*/$(app_name)/ $(APPLICATIONS_PATH)$(app_name)/

APP_SRCS_PATH := $(wildcard $(APP_POSSIBLE_FOLDER))
ifeq (,$(wildcard $(APP_SRCS_PATH)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ static int32_t lis2_spi_read(void * handle,
*/
static int32_t lis2_spi_write(void *handle,
uint8_t reg,
uint8_t *bufp,
const uint8_t *bufp,
uint16_t len)
{
spi_res_e res;
Expand Down
Loading