BME554L -Fall 2025 - Palmeri
Duke University
Invalid Date
Main
threadBut we can define our own threads for a variety of purposes!
/* 1024 byte stack, handler, NULL, NULL, NULL, priority 5, no time slice, no delay */
K_THREAD_DEFINE(heartbeat_thread_id, 1024, error_thread, NULL, NULL, NULL, 5, 0, 0);
extern void heartbeat_thread(void *, void *, void *) {
while (1) {
k_msleep(250); // scheduler can run other tasks now
gpio_pin_toggle_dt(&heartbeat_led);
k_msleep(750); // scheduler can run other tasks now
gpio_pin_toggle_dt(&heartbeat_led);
}
}
k_msleep
) without blocking other threads.prj.conf
with CONFIG_EVENTS=y
.0/1
) indicates (False/True
) if an event has occured).k_event_post
).k_event_wait
) for an event (or events) to occur.k_event_clear
) after they are processed.k_event
structure./* 1024 byte stack, handler, NULL, NULL, NULL, priority 5, no time slice, no delay */
K_THREAD_DEFINE(temp_too_high_thread_id, 1024, temp_too_high_thread, NULL, NULL, NULL, 5, 0, 0);
extern void temp_too_high_thread(void *, void *, void *) {
// need to loop to keep the thread running after the first error occurs
while (1) {
/* &temp_events is a pointer to an event bit array
0xF is an example of a bit mask of events in the array to wait for - any, not all
true clears all of the events that may have previously been posted before waiting
K_FOREVER means wait indefinitely (this could be a finite period of time instead)
*/
uint32_t events = k_event_wait(&temp_events, 0xF, true, K_FOREVER);
// can also define the bit mask as a logical operation of the individual bits
// uint32_t events = k_event_wait(&temp_events, TEMP_TOO_HIGH_EVENT | TEMP_TOO_LOW EVENT, true, K_FOREVER);
// events is an int representation of the bit mask of the events that were posted
// if you want to wait for **ALL** events in the mask, use
// k_event_wait_all() instead of k_event_wait()
LOG_INF("Temperature Event Posted: %d", events); // bit array mask output as an int
shut_down_system(); // do something in response to the temperature event, like change states
}
}