File size: 4,543 Bytes
7b6bf43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/******************************************************************************

 * FILE NAME:    Scheduler.c

 * DESCRIPTION:

 * 

 * DATE BEGUN:  

 * BY:          

 * PRODUCT NAME:

 * APPLICATION:

 * TARGET H/W:

 * DOC REF:

 *****************************************************************************

 */

/*****************************************************************************

** #include 

*****************************************************************************/
#include "stdint.h"
#include "stdbool.h"


//! \name finit state machine state
//! @{
typedef enum {
    fsm_rt_err          = -1,    //!< fsm error, error code can be get from other interface
    fsm_rt_cpl          = 0,     //!< fsm complete
    fsm_rt_on_going     = 1,     //!< fsm on-going
} fsm_rt_t;
//! @}

static fsm_rt_t task_print(void);
static fsm_rt_t task_delay(void);
static fsm_rt_t print(void);
static fsm_rt_t delay(void);
static bool s_bFlag = false;
/****************************************************************************/
/**

 * Function Name: void scheduler( void )

 * Description: none

 *

 * Param:   none

 * Return:  none

 * Author:  

 ****************************************************************************/
void scheduler( void )
{	
    while(!serial_out('z'));
    while(!serial_out('t'));
    while(!serial_out('2'));
    while(!serial_out('0'));
    while(!serial_out('1'));
    while(!serial_out('2'));
    while(!serial_out('\r'));
    while(!serial_out('\n'));
    
    /* loop here forever */
	while(1){    
        task_print();     
        task_delay();     
	}
		
}

#define TASK_PRINT_RESET_FSM()	\
	do{	\
		s_tTaskPrintState = TASK_PRINT_START;	\
	}while(0)	
static fsm_rt_t task_print(void)
{
	static enum{
		TASK_PRINT_START=0,
		TASK_PRINT_ING
	}s_tTaskPrintState = TASK_PRINT_START;
	switch (s_tTaskPrintState){
		case TASK_PRINT_START:
			if(s_bFlag){
				s_tTaskPrintState = TASK_PRINT_ING;
			}
			break;
			
		case TASK_PRINT_ING:
			if(fsm_rt_cpl == print()){
				TASK_PRINT_RESET_FSM();
				return fsm_rt_cpl;
			}
			break;
	}
	return fsm_rt_on_going;
}

#define TASK_DELAY_RESET_FSM()	\
	do{	\
		s_tTaskDelayState = TASK_DELAY_START;	\
	}while(0)	
static  fsm_rt_t task_delay(void)
{
	static enum{
		TASK_DELAY_START=0,
		TASK_DELAY_ING
	}s_tTaskDelayState = TASK_DELAY_START;
	switch(s_tTaskDelayState){
		case TASK_DELAY_START:
			s_bFlag = false;
		
		case TASK_DELAY_ING:
			if(fsm_rt_cpl == delay()){
				s_bFlag = true;
				TASK_DELAY_RESET_FSM();
				return fsm_rt_on_going;
			}
	}
	return fsm_rt_on_going;
}

#define PRINT_RESET_FSM()	\
	do{	\
		s_tPrintState = PRINT_START;	\
	}while(0)
	
static fsm_rt_t print(void)
{
	static enum{
		PRINT_START = 0,
		PRINT_OUT_H,
		PRINT_OUT_E,
		PRINT_OUT_L1,
		PRINT_OUT_L2,
		PRINT_OUT_O,
		PRINT_OUT_R,
		PRINT_OUT_N
	}s_tPrintState = PRINT_START;
	switch(s_tPrintState){
		case PRINT_START:
			s_tPrintState = PRINT_OUT_H;
		
		case PRINT_OUT_H:
			if(serial_out('h')){
				s_tPrintState = PRINT_OUT_E;
			}
			break;
		case PRINT_OUT_E:
			if(serial_out('e')){
				s_tPrintState = PRINT_OUT_L1;
			}
			break;
		case PRINT_OUT_L1:
			if(serial_out('l')){
				s_tPrintState = PRINT_OUT_L2;
			}
			break;
		case PRINT_OUT_L2:
			if(serial_out('l')){
				s_tPrintState = PRINT_OUT_O;
			}
			break;
		case PRINT_OUT_O:
			if(serial_out('o')){
				s_tPrintState = PRINT_OUT_R;
			}
			break;
		case PRINT_OUT_R:
			if(serial_out('\r')){
				s_tPrintState = PRINT_OUT_N;
			}
			break;
		case PRINT_OUT_N:
			if(serial_out('\n')){
				PRINT_RESET_FSM();
				return fsm_rt_cpl;
			}
			break;
	}
	return fsm_rt_on_going;
}

#define DELAY_RESET_FSM()	\
	do{	\
		s_tDelaytState = DELAY_START;	\
	}while(0)
#define DEALY_TIME (0x2ffff)
	
static fsm_rt_t delay(void)
{
	static enum{
		DELAY_START = 0,
		DELAY
	}s_tDelaytState = DELAY_START;
	static uint32_t s_wDelayCounter = 0;
	switch(s_tDelaytState){
		case DELAY_START:
			s_wDelayCounter = 0;
			s_tDelaytState = DELAY;

		case DELAY:
			s_wDelayCounter ++;
			if(s_wDelayCounter > DEALY_TIME){	
				s_wDelayCounter = 0;				
				DELAY_RESET_FSM();
				return fsm_rt_cpl;
			}
			break;
	}			
	return fsm_rt_on_going;
}

/*****************************************************************************

** End File

*****************************************************************************/