diff options
author | Julian T <julian@jtle.dk> | 2020-06-07 23:40:14 +0200 |
---|---|---|
committer | Julian T <julian@jtle.dk> | 2020-06-07 23:40:14 +0200 |
commit | 6075c41624efa8260344dfd4b0c7fa563f4cfe5e (patch) | |
tree | 0ee0942543935c70ddd108f71562612cdd72daee /sem4/embedded/m5 | |
parent | 6cb9bec1e0241fe64afdad9a520f026a9c25ea91 (diff) |
Added solutions for many of the first exercises
Diffstat (limited to 'sem4/embedded/m5')
-rw-r--r-- | sem4/embedded/m5/m5.ino | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/sem4/embedded/m5/m5.ino b/sem4/embedded/m5/m5.ino new file mode 100644 index 0000000..496e33b --- /dev/null +++ b/sem4/embedded/m5/m5.ino @@ -0,0 +1,65 @@ +#include <krnl.h> +#define STACK 200 + +char stk1[STACK]; +char stk2[STACK]; +struct k_t *tsk1, *tsk2, *sem; + +unsigned long shared1 = 0; +unsigned long shared2 = 0; + +#define DELAY 100000 +void f1() { + for(;;) { + for (int i = 0; i < DELAY/4; i++); + unsigned long s1 = shared1; + + // Lock + k_wait(sem, 0); + unsigned long s2 = shared2; + // Unlock + k_signal(sem); + + if (s1 != 0) { + Serial.print("S1: "); Serial.println(s1); + } + if (s2 != 0) { + Serial.print("S2: "); Serial.println(s2); + } + } +} + +void f2() { + for(;;) { + shared1 += 1; + for (int i = 0; i < DELAY; i++); + shared1 -= 1; + + // Lock + k_wait(sem, 0); + shared2 += 1; + for (int i = 0; i < DELAY; i++); + shared2 -= 1; + // Unlock + k_signal(sem); + } +} + +void setup() { + pinMode(13, OUTPUT); + Serial.begin(115200); + Serial.println("Starting"); + + k_init(2, 1, 0); + + sem = k_crt_sem(1, 1); + + tsk1 = k_crt_task(f1, 10, stk1, STACK); + tsk2 = k_crt_task(f2, 10, stk2, STACK); + + k_start(1); +} + +void loop() { + +} |