Skip to content

Instantly share code, notes, and snippets.

@arthurafarias
Created August 25, 2017 10:32
Show Gist options
  • Save arthurafarias/9e0593616a356154a101ba54575ba9b3 to your computer and use it in GitHub Desktop.
Save arthurafarias/9e0593616a356154a101ba54575ba9b3 to your computer and use it in GitHub Desktop.
Sample Code for Zephyr OS
#include <zephyr.h>
#include <misc/printk.h>
/* size of stack area used by each thread */
#define STACKSIZE 1024
K_SEM_DEFINE(sem, 0, 1);
K_MUTEX_DEFINE(mutex);
extern void t01_entry(void);
extern void t02_entry(void);
extern void t03_entry(void);
extern void t04_entry(void);
extern void print_counter_value(void);
K_THREAD_DEFINE(t01_id, STACKSIZE, t01_entry, NULL, NULL, NULL, 1, 0, K_NO_WAIT);
K_THREAD_DEFINE(t02_id, STACKSIZE, t02_entry, NULL, NULL, NULL, 1, 0, K_NO_WAIT);
K_THREAD_DEFINE(t03_id, STACKSIZE, t03_entry, NULL, NULL, NULL, 1, 0, K_NO_WAIT);
K_THREAD_DEFINE(t04_id, STACKSIZE, t04_entry, NULL, NULL, NULL, 1, 0, K_NO_WAIT);
void main(void) {
k_sched_time_slice_set(1000, 0);
}
void t01_entry(void) {
while(1) {
printk("T01\n");
k_sem_give(&sem);
// k_sleep(1500);
}
}
void t02_entry(void) {
while(1) {
if ( k_sem_take(&sem, K_FOREVER) == 0) {
printk("T02\n");
}
}
}
void t03_entry(void) {
while(1) {
printk("T03\n");
// k_mutex_lock(&mutex, K_FOREVER);
// print_counter_value();
// k_mutex_unlock(&mutex);
return ;
}
}
void t04_entry(void) {
while(1) {
printk("T04\n");
// k_mutex_lock(&mutex, K_FOREVER);
// print_counter_value();
// k_mutex_unlock(&mutex);
return ;
}
}
int counter = 0;
void print_counter_value(void) {
counter ++;
printk("Counter Value: %d\n", counter);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment