272
|
1 module coro;
|
|
2
|
|
3 /*
|
|
4 Some co-routines doing some work.
|
|
5
|
|
6 This example demonstrates how to write co-operating
|
|
7 routines in a nice way.
|
|
8 */
|
|
9
|
|
10 int regbank[10];
|
|
11
|
|
12 function i2c_write(int address, int d)
|
|
13 {
|
|
14 regbank[2] = address;
|
|
15 regbank[0] |= 0x1; // Issue go command
|
|
16 while (regbank[1] != 0)
|
|
17 {
|
|
18 yield;
|
|
19 }
|
|
20
|
|
21 // Wait while busy:
|
|
22 while (regbank[1] != 11)
|
|
23 {
|
|
24 yield;
|
|
25 }
|
|
26 }
|
|
27
|
|
28 function int i2c_read(int address)
|
|
29 {
|
|
30 regbank[2] = address;
|
|
31 regbank[0] |= 0x1; // Issue go command
|
|
32 while (regbank[1] != 0)
|
|
33 {
|
|
34 yield;
|
|
35 }
|
|
36
|
|
37 // Wait while busy:
|
|
38 while (regbank[1] != 11)
|
|
39 {
|
|
40 yield;
|
|
41 }
|
|
42 }
|
|
43
|
|
44 function void eeprom_set(int address, int v)
|
|
45 {
|
|
46 i2c_write(address, v);
|
|
47 i2c_read(address);
|
|
48 }
|
|
49
|
|
50 function int calcX(int y)
|
|
51 {
|
|
52 var int x;
|
|
53 x = 2;
|
|
54 x = x + 2 + 9 * y;
|
|
55 return x;
|
|
56 }
|
|
57
|
|
58 var int counter = 0;
|
|
59
|
|
60 task task1()
|
|
61 {
|
|
62 start task3;
|
|
63 var int a = 200;
|
|
64 while (a > 0)
|
|
65 {
|
|
66 yield;
|
|
67 }
|
|
68 }
|
|
69
|
|
70 task task2()
|
|
71 {
|
|
72 while(true)
|
|
73 {
|
|
74 yield;
|
|
75 }
|
|
76 }
|
|
77
|
|
78 task task3()
|
|
79 {
|
|
80 eeprom_set(99, 1);
|
|
81 yield;
|
|
82 }
|
|
83
|
|
84 task main()
|
|
85 {
|
|
86 start task1;
|
|
87 start task2;
|
|
88 await task1;
|
|
89 await task2;
|
|
90 }
|
|
91
|