📄 hsch51.lst
字号:
=2
=2 // Additional interrupts (used in shared-clock schedulers)
=2 #define INTERRUPT_EXTERNAL_0 0
=2 #define INTERRUPT_EXTERNAL_1 2
=2 #define INTERRUPT_UART_Rx_Tx 4
=2 #define INTERRUPT_CAN_c515c 17
=2
=2 //------------------------------------------------------------------
=2 // Error codes
=2 // - see Chapter 14.
=2 //------------------------------------------------------------------
=2
=2 #define ERROR_SCH_TOO_MANY_TASKS (1)
=2 #define ERROR_SCH_CANNOT_DELETE_TASK (2)
=2
=2 #define ERROR_SCH_WAITING_FOR_SLAVE_TO_ACK (3)
=2 #define ERROR_SCH_WAITING_FOR_START_COMMAND_FROM_MASTER (3)
=2
C51 COMPILER V6.10 HSCH51 04/19/2001 12:07:05 PAGE 10
=2 #define ERROR_SCH_ONE_OR_MORE_SLAVES_DID_NOT_START (4)
=2 #define ERROR_SCH_LOST_SLAVE (5)
=2
=2 #define ERROR_SCH_CAN_BUS_ERROR (6)
=2
=2 #define ERROR_I2C_WRITE_BYTE (10)
=2 #define ERROR_I2C_READ_BYTE (11)
=2 #define ERROR_I2C_WRITE_BYTE_AT24C64 (12)
=2 #define ERROR_I2C_READ_BYTE_AT24C64 (13)
=2 #define ERROR_I2C_DS1621 (14)
=2
=2 #define ERROR_USART_TI (21)
=2 #define ERROR_USART_WRITE_CHAR (22)
=2
=2 #define ERROR_SPI_EXCHANGE_BYTES_TIMEOUT (31)
=2 #define ERROR_SPI_X25_TIMEOUT (32)
=2 #define ERROR_SPI_MAX1110_TIMEOUT (33)
=2
=2 #define ERROR_ADC_MAX150_TIMEOUT (44)
=2
=2 #endif
115 =2
116 =2 /*------------------------------------------------------------------*-
117 =2 ---- END OF FILE -------------------------------------------------
118 =2 -*------------------------------------------------------------------*/
28 =1
29 =1 // ------ Public data type declarations ----------------------------
30 =1
31 =1 // Store in DATA area, if possible, for rapid access
32 =1 // Total memory per task is 8 bytes
33 =1 typedef data struct
34 =1 {
35 =1 // Pointer to the task (must be a 'void (void)' function)
36 =1 void (code * pTask)(void);
37 =1
38 =1 // Delay (ticks) until the function will (next) be run
39 =1 // - see SCH_Add_Task() for further details
40 =1 tWord Delay;
41 =1
42 =1 // Interval (ticks) between subsequent runs.
43 =1 // - see SCH_Add_Task() for further details
44 =1 tWord Period;
45 =1
46 =1 // Incremented (by scheduler) when task is due to execute
47 =1 tByte RunMe;
48 =1
49 =1 // Set to 1 if task is co-operative
50 =1 // Set to 0 if task is pre-emptive
51 =1 tByte Co_op;
52 =1 } sTaskH;
53 =1
54 =1 // ------ Public function prototypes -------------------------------
55 =1
56 =1 // Core scheduler functions
57 =1 void hSCH_Dispatch_Tasks(void);
58 =1 tByte hSCH_Add_Task(void (code *)(void), tWord, tWord, bit);
59 =1 bit hSCH_Delete_Task(tByte);
60 =1 void hSCH_Report_Status(void);
61 =1
62 =1 // ------ Public constants -----------------------------------------
63 =1
64 =1 // The maximum number of tasks required at any one time
C51 COMPILER V6.10 HSCH51 04/19/2001 12:07:05 PAGE 11
65 =1 // during the execution of the program
66 =1 //
67 =1 // MUST BE ADJUSTED FOR EACH NEW PROJECT
68 =1 #define hSCH_MAX_TASKS (2)
69 =1
70 =1 #endif
71 =1
72 =1 /*------------------------------------------------------------------*-
73 =1 ---- END OF FILE -------------------------------------------------
74 =1 -*------------------------------------------------------------------*/
75 =1
38
39 // ------ Public variable definitions ------------------------------
40
41 // The array of tasks
42 sTaskH hSCH_tasks_G[hSCH_MAX_TASKS];
43
44 // Used to display the error code
45 // See Main.H for details of error codes
46 // See Port.H for details of the error port
47 tByte Error_code_G = 0;
48
49 // ------ Private function prototypes ------------------------------
50
51 static void hSCH_Go_To_Sleep(void);
52
53 // ------ Private variables ----------------------------------------
54
55 // Keeps track of time since last error was recorded (see below)
56 static tWord Error_tick_count_G;
57
58 // The code of the last error (reset after ~1 minute)
59 static tByte Last_error_code_G;
60
61
62 /*------------------------------------------------------------------*-
63
64 hSCH_Dispatch_Tasks()
65
66 This is the 'dispatcher' function. When a task (function)
67 is due to run, hSCH_Dispatch_Tasks() will run it.
68 This function must be called (repeatedly) from the main loop.
69
70 -*------------------------------------------------------------------*/
71 void hSCH_Dispatch_Tasks(void)
72 {
73 1 tByte Index;
74 1
75 1 // Dispatches (runs) the next task (if one is ready)
76 1 for (Index = 0; Index < hSCH_MAX_TASKS; Index++)
77 1 {
78 2 // Only dispatching co-operative tasks
79 2 if ((hSCH_tasks_G[Index].Co_op) && (hSCH_tasks_G[Index].RunMe > 0))
80 2 {
81 3 (*hSCH_tasks_G[Index].pTask)(); // Run the task
82 3
83 3 hSCH_tasks_G[Index].RunMe -= 1; // Reset / reduce RunMe flag
84 3
85 3 // Periodic tasks will automatically run again
86 3 // - if this is a 'one shot' task, remove it from the array
87 3 if (hSCH_tasks_G[Index].Period == 0)
88 3 {
C51 COMPILER V6.10 HSCH51 04/19/2001 12:07:05 PAGE 12
89 4 // Faster than call to delete task
90 4 hSCH_tasks_G[Index].pTask = 0;
91 4 }
92 3 }
93 2 }
94 1
95 1 // Report system status
96 1 hSCH_Report_Status();
97 1
98 1 // The scheduler enters idle mode at this point
99 1 hSCH_Go_To_Sleep();
100 1 }
101
102 /*------------------------------------------------------------------*-
103
104 hSCH_Add_Task()
105
106 Causes a task (function) to be executed at regular intervals
107 or after a user-defined delay
108
109 Fn_P - The name of the function which is to be scheduled.
110 NOTE: All scheduled functions must be 'void, void' -
111 that is, they must take no parameters, and have
112 a void return type.
113
114 Del - The interval (TICKS) before the task is first executed
115
116 Rep - If 'Rep' is 0, the function is only called once,
117 at the time determined by 'Del'. If Rep is non-zero,
118 then the function is called repeatedly at an interval
119 determined by the vakue of Rep (see below for examples
120 that should help clarify this).
121
122 Co-op - Set to 1 if it a co-op task; 0 if pre-emptive
123
124 RETN: The position in the task array at which the task has been added.
125 If the return value is hSCH_MAX_TASKS then the task could not be
126 added to the array (there was insufficient space). If the
127 return value is < hSCH_MAX_TASKS, then the task was added
128 successfully.
129
130 Note: this return value may be required, if a task is
131 to be subsequently deleted - see hSCH_Delete_Task().
132
133
134 EXAMPLES:
135
136 Task_ID = hSCH_Add_Task(Do_X,1000,0,0);
137 Causes the function Do_X() to be executed once after 1000 ticks.
138 (Pre-emptive task)
139
140 Task_ID = hSCH_Add_Task(Do_X,0,1000,1);
141 Causes the function Do_X() to be executed regularly, every 1000 ticks.
142 (co-operative task)
143
144 Task_ID = hSCH_Add_Task(Do_X,300,1000,0);
145 Causes the function Do_X() to be executed regularly, every 1000 ticks.
146 Task will be first executed at T = 300 ticks, then 1300, 2300, etc.
147 (Pre-emptive task)
148
149 -*------------------------------------------------------------------*/
150 tByte hSCH_Add_Task(void (code* Fn_p)(), // Task function pointer
C51 COMPILER V6.10 HSCH51 04/19/2001 12:07:05 PAGE 13
151 tWord Del, // Num ticks 'til task first runs
152 tWord Per, // Num ticks between repeat runs
153 bit Co_op) // Co_op / pre_emp
154 {
155 1 tByte Index = 0;
156 1
157 1 // First find a gap in the array (if there is one)
158 1 while ((hSCH_tasks_G[Index].pTask != 0) && (Index < hSCH_MAX_TASKS))
159 1 {
160 2 Index++;
161 2 }
162 1
163 1 // Have we reached the end of the list?
164 1 if (Index == hSCH_MAX_TASKS)
165 1 {
166 2 // Task list is full
167 2 //
168 2 // Set the global error variable
169 2 Error_code_G = ERROR_SCH_TOO_MANY_TASKS;
170 2
171 2 // Also return an error code
172 2 return hSCH_MAX_TASKS;
173 2 }
174 1
175 1 // If we're here, there is a space in the task array
176 1 hSCH_tasks_G[Index].pTask = Fn_p;
177 1
178 1 hSCH_tasks_G[Index].Delay = Del;
179 1 hSCH_tasks_G[Index].Period = Per;
180 1
181 1 hSCH_tasks_G[Index].Co_op = Co_op;
182 1
183 1 hSCH_tasks_G[Index].RunMe = 0;
184 1
185 1 return Index; // return position of task (to allow later deletion)
186 1 }
187
188 /*------------------------------------------------------------------*-
189
190 hSCH_Delete_Task()
191
192 Removes a task from the scheduler. Note that this does
193 *not* delete the associated function from memory:
194 it simply means that it is no longer called by the scheduler.
195
196 PARAMS: Task_index - The task index. Provided by hSCH_Add_Task().
197
198 RETURNS: RETURN_ERROR or RETURN_NORMAL
199
200 -*------------------------------------------------------------------*/
201 bit hSCH_Delete_Task(tByte Task_index)
202 {
203 1 bit Return_code;
204 1
205 1 if (hSCH_tasks_G[Task_index].pTask == 0)
206 1 {
207 2 // No task at this location...
208 2 //
209 2 // Set the global error variable
210 2 Error_code_G = ERROR_SCH_CANNOT_DELETE_TASK;
211 2
212 2 // ...also return an error code
C51 COMPILER V6.10 HSCH51 04/19/2001 12:07:05 PAGE 14
213 2 Return_code = RETURN_ERROR;
214 2 }
215 1 else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -