summaryrefslogtreecommitdiff
path: root/cros_ec/test/ec_os_test.c
blob: f34ccec4d3c59a228adb6b7417abb1e5bb76bf63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/* Basic test for EcOs objects */

#include <stdio.h>
#include <stdint.h>

#include "ec_os.h"

EcTask t1, t2, t3, t4;
EcSemaphore sem;
EcSwi swi;
EcTimer timer1, timer2;
EcEvent ev1, ev2;


void Thread1(void* arg) {
  int i;

  for (i = 0; i < 5; i++) {
    EcSemaphoreWait(&sem, EC_OS_FOREVER);
    /* Do some work */
    EcTaskSleep(5000);
    fprintf(stderr, "Hello from thread1: %s\n", (char*)arg);
    EcSemaphorePost(&sem);

    /* Two rapid posts to SWI, to see that they merge */
    EcSwiPost(&swi, 1 << i);
    EcSwiPost(&swi, 0x100 << i);

    EcTaskSleep(100);
  }

  EcTaskSleep(500000);
  fprintf(stderr, "Goodbye from thread1\n");
}


void Thread2(void* arg) {
  int i;

  for (i = 0; i < 5; i++) {
    EcSemaphoreWait(&sem, EC_OS_FOREVER);
    /* Do some work */
    EcTaskSleep(5000);
    fprintf(stderr, "Hello from thread2: %s\n", (char*)arg);
    EcSemaphorePost(&sem);

    /* Post events */
    EcEventPost(&ev1, 1 << i);
    EcEventPost(&ev2, 1 << i);

    EcTaskSleep(100);
  }

  EcTaskSleep(50000);
  fprintf(stderr, "Goodbye from thread2\n");
}


void Thread3(void* arg) {
  uint32_t got_bits = 0;

  while(got_bits != 0x10) {
    /* Wait for any of the bits to be set */

    EcEventWaitAny(&ev1, 0x1c, &got_bits, EC_OS_FOREVER);
    fprintf(stderr, "Event thread 3 got bits: 0x%x\n", got_bits);
  }
  fprintf(stderr, "Goodbye from event thread 3\n");
}


void Thread4(void* arg) {
  /* Wait on event bit from creation and a few posted bits. */
  EcEventWaitAll(&ev2, 0x10e, EC_OS_FOREVER);
  fprintf(stderr, "Event thread 4 got all bits\n");
  fprintf(stderr, "Goodbye from event thread 4\n");
}


void SwiFunc(void* arg, uint32_t bits) {
  fprintf(stderr, "Hello from SWI with bits=0x%x\n", bits);
}


void TimerFunc(void* arg) {
  fprintf(stderr, "Hello from timer: %s\n", (char*)arg);
  /* Start the one-shot timer. */
  EcTimerStart(&timer2);
}


void OneTimerFunc(void* arg) {
  fprintf(stderr, "Hello from one-shot timer: %s\n", (char*)arg);
  /* Stop the periodic timer */
  EcTimerStop(&timer1);
}


int main(void) {
  fprintf(stderr, "Hello, world.\n");

  EcOsInit();

  EcTaskCreate(&t1, EC_TASK_PRIORITY_DEFAULT, 0, Thread1, "Foo1");
  EcTaskCreate(&t2, EC_TASK_PRIORITY_DEFAULT, 0, Thread2, "Foo2");
  EcTaskCreate(&t3, EC_TASK_PRIORITY_DEFAULT, 0, Thread3, "EventTask1");
  EcTaskCreate(&t4, EC_TASK_PRIORITY_DEFAULT, 0, Thread4, "EventTask2");

  EcSwiCreate(&swi, EC_SWI_PRIORITY_DEFAULT, SwiFunc, "Swi1");
  EcTimerCreate(&timer1, 100000, EC_TIMER_PRIORITY_DEFAULT,
                EC_TIMER_FLAG_STARTED|EC_TIMER_FLAG_PERIODIC,
                TimerFunc, "Timer1");
  EcTimerCreate(&timer2, 150000, EC_TIMER_PRIORITY_DEFAULT,
                0, OneTimerFunc, "Timer2");
  EcSemaphoreCreate(&sem, 1);
  EcEventCreate(&ev1, 0);
  EcEventCreate(&ev2, 0x100);

  fprintf(stderr, "EcOs objects created.\n");

  EcOsStart();

  return 0;
}