?? scu_bs.lst
字號:
C51 COMPILER V6.10 SCU_BS 04/19/2001 14:03:35 PAGE 1
C51 COMPILER V6.10, COMPILATION OF MODULE SCU_BS
OBJECT MODULE PLACED IN .\SCU_BS.OBJ
COMPILER INVOKED BY: C:\KEIL\C51\BIN\C51.EXE .\SCU_BS.C OPTIMIZE(6,SIZE) BROWSE DEBUG OBJECTEXTEND
stmt level source
1 /*------------------------------------------------------------------*-
2
3 SCU_Bs.c (v1.00)
4
5 ------------------------------------------------------------------
6
7 This is an implementation of SCU SCHEDULER (RS-485) for 8051/52.
8
9 --- See Chapter 27 ---
10
11 *** SLAVE / BACKUP NODE ***
12 *** MASTER CHECKS FOR SLAVE ACKNOWLEDEMENTS ***
13 *** Includes support for tranceiver enables ***
14
15 *** Uses 1232 watchdog timer ***
16
17 *** Assumes 12 MHz osc (same as Master) ***
18
19 *** Both Master and Slave share the same tick rate (5 ms) ***
20 *** - See Master code for details ***
21
22
23 COPYRIGHT
24 ---------
25
26 This code is from the book:
27
28 PATTERNS FOR TIME-TRIGGERED EMBEDDED SYSTEMS by Michael J. Pont
29 [Pearson Education, 2001; ISBN: 0-201-33138-1].
30
31 This code is copyright (c) 2001 by Michael J. Pont.
32
33 See book for copyright details and other information.
34
35 -*------------------------------------------------------------------*/
36
37 #include "Main.h"
38 #include "Port.h"
39
40 #include "SCU_Bs.h"
41 #include "TLight_B.h"
42
43 // ------ Public variable definitions ------------------------------
44
45 // Data sent from the master to this slave
46 tByte Tick_message_data_G;
47
48 // Data sent from this slave to the master
49 // - data may be sent on, by the master, to another slave
50 tByte Ack_message_data_G = '2';
51
52 // ------ Public variable declarations -----------------------------
53
54 // The array of tasks (see Sch51.c)
55 extern sTask SCH_tasks_G[SCH_MAX_TASKS];
C51 COMPILER V6.10 SCU_BS 04/19/2001 14:03:35 PAGE 2
56
57 // The error code variable (see Sch51.c)
58 extern tByte Error_code_G;
59
60 // ------ Private function prototypes ------------------------------
61
62 static void SCU_B_SLAVE_Enter_Safe_State(void);
63
64 static void SCU_B_SLAVE_Send_Ack_Message_To_Master(void);
65 static tByte SCU_B_SLAVE_Process_Tick_Message(void);
66
67 static void SCU_B_SLAVE_Watchdog_Init(void);
68 static void SCU_B_SLAVE_Watchdog_Refresh(void) reentrant;
69
70
71 // ------ Private constants ----------------------------------------
72
73 // Each slave must have a unique non-zero ID
74 #define SLAVE_ID 0x32
75
76 #define NO_NETWORK_ERROR (1)
77 #define NETWORK_ERROR (0)
78
79 // ------ Private variables ----------------------------------------
80
81 static bit Message_byte_G;
82 static bit WATCHDOG_state_G = 0;
83 static tByte Message_ID_G = 0;
84
85
86 /*------------------------------------------------------------------*-
87 SCU_B_SLAVE_Init_T1()
88
89 Scheduler initialisation function. Prepares scheduler
90 data structures and sets up timer interrupts at required rate.
91 Must call this function before using the scheduler.
92
93 BAUD_RATE - The required baud rate
94
95 -*------------------------------------------------------------------*/
96 void SCU_B_SLAVE_Init_T1(const tWord BAUD_RATE)
97 {
98 1 tByte i;
99 1
100 1 // Sort out the tasks
101 1 for (i = 0; i < SCH_MAX_TASKS; i++)
102 1 {
103 2 SCH_Delete_Task(i);
104 2 }
105 1
106 1 // Reset the global error variable
107 1 // - SCH_Delete_Task() will generate an error code,
108 1 // (because the task array is empty)
109 1 Error_code_G = 0;
110 1
111 1 // Set the network error pin (reset when tick message received)
112 1 Network_error_pin = NETWORK_ERROR;
113 1
114 1 // Set up RS-485 tranceiver
115 1 RS485_Rx_NOT_Enable = 0; // Receiver is (here) constantly enabled
116 1 // (NOTE - negative logic!)
117 1
C51 COMPILER V6.10 SCU_BS 04/19/2001 14:03:35 PAGE 3
118 1 RS485_Tx_Enable = 0; // Transmitter (in slave) is enabled
119 1 // only when data are to be transmitted
120 1 // by this slave
121 1
122 1 // Ready for first tick message
123 1 Message_byte_G = 1;
124 1
125 1 // ------ Set the baud rate (begin) -----------------------------
126 1 PCON &= 0x7F; // Set SMOD bit to 0 (don't double baud rates)
127 1
128 1 // receiver enabled
129 1 // 9-bit data, 1 start bit, 1 stop bit, variable baud rate (asynchronous)
130 1 SCON = 0xD2;
131 1
132 1 TMOD |= 0x20; // T1 in mode 2, 8-bit auto reload
133 1
134 1 TH1 = (256 - (tByte)((((tLong)OSC_FREQ / 100) * 3125)
135 1 / ((tLong) BAUD_RATE * OSC_PER_INST * 1000)));
136 1
137 1 TL1 = TH1;
138 1 TR1 = 1; // Run the timer
139 1 TI = 1; // Send first character (dummy)
140 1
141 1 // ------ Set the baud rate (end) -------------------------------
142 1
143 1 // Interrupt enabled
144 1 // (Both receiving and SENDING a byte will generate a serial interrupt)
145 1 // Global interrupts not yet enabled.
146 1 ES = 1;
147 1
148 1 // Start the watchdog
149 1 SCU_B_SLAVE_Watchdog_Init();
150 1 }
151
152 /*------------------------------------------------------------------*-
153 SCU_B_SLAVE_Start()
154
155 Starts the slave scheduler, by enabling interrupts.
156
157 NOTE: Usually called after all regular tasks are added,
158 to keep the tasks synchronised.
159
160 NOTE: ONLY THE SCHEDULER INTERRUPT SHOULD BE ENABLED!!!
161
162 -*------------------------------------------------------------------*/
163 void SCU_B_SLAVE_Start(void)
164 {
165 1 tByte Command = 0;
166 1 tByte Message_byte;
167 1 tByte Count = 0;
168 1 bit Slave_started = 0;
169 1
170 1 // Disable interrupts
171 1 EA = 0;
172 1
173 1 // We can be at this point because:
174 1 // 1. The network has just been powered up
175 1 // 2. An error has occurred in the Master, and it is not generating ticks
176 1 // 3. The network has been damaged and no ticks are being received by this slave
177 1 //
178 1 // Try to make sure the system is in a safe state...
179 1 SCU_B_SLAVE_Enter_Safe_State();
C51 COMPILER V6.10 SCU_BS 04/19/2001 14:03:35 PAGE 4
180 1
181 1 // NOTE: Interrupts are disabled here
182 1 Count = 0;
183 1
184 1 Error_code_G = ERROR_SCH_WAITING_FOR_START_COMMAND_FROM_MASTER;
185 1 SCH_Report_Status(); // Sch not yet running - do this manually
186 1
187 1 // Now wait (indefinitely) for appropriate signals from the master
188 1 do {
189 2 // Wait for tick messages (byte 1), all bits set to 0, to be received
190 2 do {
191 3 SCU_B_SLAVE_Watchdog_Refresh(); // Must keep feeding the watchdog
192 3 } while (RI == 0);
193 2
194 2 Message_byte = (tByte) SBUF;
195 2 RI = 0;
196 2
197 2 // Must get two ID messages in a row...
198 2 // (with command bit)
199 2 // Ack each one
200 2 if ((Message_byte == (tByte) SLAVE_ID) && (RB8 == 1))
201 2 {
202 3 Count++;
203 3
204 3 // Received message for this slave - send ack
205 3 // Must enable the slave RS-485 (Max489) hardware (Tx)
206 3 RS485_Tx_Enable = 1;
207 3
208 3 TI = 0;
209 3 TB8 = 1; // Set command bit
210 3 SBUF = (tByte) SLAVE_ID;
211 3
212 3 // Wait while data are sent
213 3 // (watchdog will trap UART failure...)
214 3 while (TI == 0);
215 3
216 3 // Now clear Tx enable pin
217 3 RS485_Tx_Enable = 0;
218 3 }
219 2 else
220 2 {
221 3 Count = 0;
222 3 }
223 2 } while (Count < 2);
224 1
225 1 // Start the scheduler
226 1 EA = 1;
227 1 }
228
229 /*------------------------------------------------------------------*-
230
231 SCU_B_SLAVE_Update
232
233 This is the scheduler ISR. It is called at a rate
234 determined by the timer settings in SCU_B_SLAVE_Init().
235
236 This Slave is triggered by USART interrupts.
237
238 -*------------------------------------------------------------------*/
239 void SCU_B_SLAVE_Update(void) interrupt INTERRUPT_UART_Rx_Tx
240 {
241 1 tByte Index;
C51 COMPILER V6.10 SCU_BS 04/19/2001 14:03:35 PAGE 5
242 1
243 1 if (RI == 1) // Must check this.
244 1 {
245 2 // Default
246 2 Network_error_pin = NO_NETWORK_ERROR;
247 2
248 2 // Two-byte messages are sent (Ack) and received (Tick)
249 2 // - it takes two sched ticks to process each message
250 2 //
251 2 // Keep track of the current byte
252 2 if (Message_byte_G == 0)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -