Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion lib/pbio/drv/charger/charger_mp2639a.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ pbio_error_t pbdrv_charger_mp2639a_process_thread(pbio_os_state_t *state, void *

void pbdrv_charger_init(void) {
pbdrv_init_busy_up();
pbio_os_start_process(&pbdrv_charger_mp2639a_process, pbdrv_charger_mp2639a_process_thread, NULL);
pbio_os_process_start(&pbdrv_charger_mp2639a_process, pbdrv_charger_mp2639a_process_thread, NULL);
}

#endif // PBDRV_CONFIG_CHARGER_MP2639A
29 changes: 28 additions & 1 deletion lib/pbio/include/pbio/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,19 @@ struct _pbio_os_process_t {
} \
} while (0)

/**
* Yields the protothread until the specified condition is true.
*
* @param [in] state Protothread state.
* @param [in] condition The condition.
*/
#define AWAIT_UNTIL(state, condition) \
do { \
PB_LC_SET(state); \
if (!(condition)) { \
return PBIO_ERROR_AGAIN; \
} \
} while (0)

#define AWAIT(state, child, statement) \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, this need to be an expression, not a statement. And the doc comment needs to say that the expression needs to evaluate to a pbio_error_t. Same applies to the AWAIT_RACE as well.

do { \
Expand All @@ -176,6 +189,16 @@ struct _pbio_os_process_t {
} \
} while (0)

#define AWAIT_RACE(state, child1, child2, statement1, statement2) \
do { \
ASYNC_INIT((child1)); \
ASYNC_INIT((child2)); \
PB_LC_SET(state); \
if ((statement1) == PBIO_ERROR_AGAIN && (statement2) == PBIO_ERROR_AGAIN) { \
return PBIO_ERROR_AGAIN; \
} \
} while (0)

#define AWAIT_MS(state, timer, duration) \
do { \
pbio_os_timer_set(timer, duration); \
Expand All @@ -191,7 +214,11 @@ void pbio_os_run_while_idle(void);

void pbio_os_request_poll(void);

void pbio_os_start_process(pbio_os_process_t *process, pbio_os_process_func_t func, void *context);
pbio_error_t pbio_port_process_none_thread(pbio_os_state_t *state, void *context);

void pbio_os_process_start(pbio_os_process_t *process, pbio_os_process_func_t func, void *context);

void pbio_os_process_reset(pbio_os_process_t *process, pbio_os_process_func_t func);

/**
* Disables interrupts and returns the previous interrupt state.
Expand Down
34 changes: 29 additions & 5 deletions lib/pbio/src/os.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,23 @@ void pbio_os_request_poll(void) {
static pbio_os_process_t *process_list = NULL;

/**
* Starts a process.
* Placeholder thread that does nothing.
*
* NB: Processes can be started only once. They cannot be restarted.
* @param [in] state The protothread state.
* @param [in] context The context.
*/
pbio_error_t pbio_port_process_none_thread(pbio_os_state_t *state, void *context) {
return PBIO_ERROR_AGAIN;
}

/**
* Adds a process to the list of processes to run and starts it soon.
*
* @param process The process to start.
* @param func The process thread function.
* @param context The context to pass to the process.
*/
void pbio_os_start_process(pbio_os_process_t *process, pbio_os_process_func_t func, void *context) {
void pbio_os_process_start(pbio_os_process_t *process, pbio_os_process_func_t func, void *context) {

// Add the new process to the end of the list.
pbio_os_process_t *last = process_list;
Expand All @@ -69,11 +77,27 @@ void pbio_os_start_process(pbio_os_process_t *process, pbio_os_process_func_t fu
}

// Initialize the process.
process->func = func;
process->context = context;
process->next = NULL;
process->context = context;

pbio_os_process_reset(process, func);
}

/**
* Resets an existing process to the initial state with a new function and context.
*
* @param process The process to start.
* @param func The process thread function. Choose NULL if it does not need changing.
*/
void pbio_os_process_reset(pbio_os_process_t *process, pbio_os_process_func_t func) {
process->err = PBIO_ERROR_AGAIN;
process->state = 0;
if (func) {
process->func = func;
}

// Request a poll to start the process soon, running to its first yield.
pbio_os_request_poll();
}

/**
Expand Down