|
1 | 1 | /*============================================================================
|
2 |
| -* Super-Simple Tasker (SST/C) Example |
| 2 | +* Super-Simple Tasker (SST0/C) Example |
3 | 3 | *
|
4 | 4 | * Copyright (C) 2006-2023 Quantum Leaps, <state-machine.com>.
|
5 | 5 | *
|
|
27 | 27 | #include "bsp.h" /* Board Support Package interface */
|
28 | 28 | #include "blinky_button.h" /* application shared interface */
|
29 | 29 |
|
30 |
| -DBC_MODULE_NAME("blinky1") |
| 30 | +DBC_MODULE_NAME("blinky1") /* for DBC assertions in this module */ |
31 | 31 |
|
32 | 32 | /*..........................................................................*/
|
33 |
| -typedef struct { /* Blinky1 task */ |
34 |
| - SST_Task super; /* inherit SST_Task */ |
35 |
| - uint16_t toggles; |
36 |
| - uint8_t ticks; |
37 |
| - uint8_t tick_ctr; |
| 33 | +typedef struct { /* Blinky1 task */ |
| 34 | + SST_Task super; /* inherit SST_Task */ |
| 35 | + SST_TimeEvt te; /* time event for generating TIMEOUT events */ |
| 36 | + uint16_t toggles; /* number of toggles to perform for TIMEOUT event */ |
38 | 37 | } Blinky1;
|
39 | 38 |
|
| 39 | +static void Blinky1_ctor(Blinky1 * const me); |
40 | 40 | static void Blinky1_init(Blinky1 * const me, SST_Evt const * const ie);
|
41 | 41 | static void Blinky1_dispatch(Blinky1 * const me, SST_Evt const * const e);
|
42 | 42 |
|
43 | 43 | /*..........................................................................*/
|
44 | 44 | static Blinky1 Blinky1_inst; /* the Blinky instance */
|
45 | 45 | SST_Task * const AO_Blinky1 = &Blinky1_inst.super; /* opaque AO pointer */
|
46 | 46 |
|
| 47 | +void Blinky1_instantiate(void) { |
| 48 | + Blinky1_ctor(&Blinky1_inst); |
| 49 | +} |
| 50 | + |
47 | 51 | /*..........................................................................*/
|
48 |
| -void Blinky1_ctor(void) { |
49 |
| - Blinky1 * const me = &Blinky1_inst; |
| 52 | +static void Blinky1_ctor(Blinky1 * const me) { |
50 | 53 | SST_Task_ctor(
|
51 | 54 | &me->super,
|
52 | 55 | (SST_Handler)&Blinky1_init,
|
53 | 56 | (SST_Handler)&Blinky1_dispatch);
|
| 57 | + SST_TimeEvt_ctor(&me->te, TIMEOUT_SIG, &me->super); |
54 | 58 | }
|
55 |
| - |
56 | 59 | /*..........................................................................*/
|
57 | 60 | static void Blinky1_init(Blinky1 * const me, SST_Evt const * const ie) {
|
58 | 61 | /* the initial event must be provided and must be WORKLOAD_SIG */
|
59 | 62 | DBC_REQUIRE(300,
|
60 | 63 | (ie != (SST_Evt const *)0) && (ie->sig == BLINKY_WORK_SIG));
|
61 | 64 |
|
| 65 | + SST_TimeEvt_arm(&me->te, |
| 66 | + SST_EVT_DOWNCAST(BlinkyWorkEvt, ie)->ticks, |
| 67 | + SST_EVT_DOWNCAST(BlinkyWorkEvt, ie)->ticks); |
62 | 68 | me->toggles = SST_EVT_DOWNCAST(BlinkyWorkEvt, ie)->toggles;
|
63 |
| - me->ticks = SST_EVT_DOWNCAST(BlinkyWorkEvt, ie)->ticks; |
64 |
| - me->tick_ctr = me->ticks; |
65 | 69 | }
|
66 | 70 | /*..........................................................................*/
|
67 | 71 | static void Blinky1_dispatch(Blinky1 * const me, SST_Evt const * const e) {
|
68 | 72 | switch (e->sig) {
|
69 |
| - case TICK_SIG: { |
70 |
| - --me->tick_ctr; |
71 |
| - if (me->tick_ctr == 0U) { |
72 |
| - me->tick_ctr = me->ticks; |
73 |
| - for (uint16_t i = me->toggles; i > 0U; --i) { |
74 |
| - BSP_d5on(); |
75 |
| - BSP_d5off(); |
76 |
| - } |
| 73 | + case TIMEOUT_SIG: { |
| 74 | + for (uint16_t i = me->toggles; i > 0U; --i) { |
| 75 | + /* SST scheduler lock is not needed in non-preemptive SST0 */ |
| 76 | + //SST_LockKey key = SST_Task_lock(3U); |
| 77 | + BSP_d5on(); |
| 78 | + BSP_d5off(); |
| 79 | + //SST_Task_unlock(key); |
77 | 80 | }
|
78 | 81 | break;
|
79 | 82 | }
|
80 | 83 | case BLINKY_WORK_SIG: {
|
81 | 84 | BSP_d5on();
|
| 85 | + SST_TimeEvt_arm(&me->te, |
| 86 | + SST_EVT_DOWNCAST(BlinkyWorkEvt, e)->ticks, |
| 87 | + SST_EVT_DOWNCAST(BlinkyWorkEvt, e)->ticks); |
82 | 88 | me->toggles = SST_EVT_DOWNCAST(BlinkyWorkEvt, e)->toggles;
|
83 |
| - me->ticks = SST_EVT_DOWNCAST(BlinkyWorkEvt, e)->ticks; |
84 | 89 | BSP_d5off();
|
85 | 90 | break;
|
86 | 91 | }
|
|
0 commit comments