📄 uart_dv.lst
字号:
158 **/
159 void UART_TxByte()
160 {
161 1 if (UARTFlag & TX_FLAG) //check is TX flag setting
162 1 {
163 2 SBUF = Tx_Buf[Tx_Index++];
164 2 if (Tx_Index >= Tx_Length) //close the TX (send complete)
165 2 {
166 3 UARTFlag &= (TX_FLAG ^ CLEAN_UARTFlag); //clean TX flag
167 3 if (Tx_Buf[1] != SACK)
168 3 UARTFlag |= TX_WAITACK; //set wait ACK flag **
169 3 }
170 2 }
171 1 }
172
173 /**--------------------------------------------------------------------------
174 * Name void UART_SendOut(char cmd_length);
175 * char *sendbuf sending buffer
176 * char cmd_length length of command
177 *
178 * Description The Routine will
C51 COMPILER V7.50 UART_DV 12/14/2006 10:36:29 PAGE 4
179 * 1. wait last time send complete,
180 * 2. Send First byte
181 * 3. Set flag.
182 *
183 * Flow Chart
184 *
185 * Return
186 *
187 * DATE Author Description
188 * ===========================================================================
189 * 2003-01-16 K.M. Ho This is first time implement
190 * 2004-05-14 Eson W. Modify for 8051 component
191 */
192 void UART_SendOut(BYTE *sendbuf, BYTE send_length)
193 {
194 1 BYTE i;
195 1 BYTE send_times;
196 1
197 1 send_times = 0;
198 1
199 1 while(1)
200 1 {
201 2 if (UARTFlag & (TX_FLAG|TX_WAITACK))
202 2 { //UART is busy for last TX
203 3 for (i=0; i<120; i++);
204 3 ++send_times;
205 3 if (send_times>120) //send out fail times > 3
206 3 return;
207 3 }
208 2 else
209 2 { //Ready to send out first byte
210 3 UARTFlag |= TX_FLAG; //set flag for send
211 3 for (i=0; i<send_length; i++) //copy send data to TX buffer
212 3 // Tx_Buf[i] = ReTx_Buf[i] = sendbuf[i];
213 3 Tx_Buf[i] = sendbuf[i];
214 3 // Tx_Length = ReTx_Length = send_length; //copy sending data length
215 3 Tx_Length = send_length; //copy sending data length
216 3 Tx_Index = 1; //Trigger UART TX INT
217 3 SBUF=Tx_Buf[0]; //put first data to THR
218 3 return;
219 3 }
220 2 }
221 1 }
222
223 /**--------------------------------------------------------------------------
224 * Name void PrepareSendBuf(char buf_index, int addr, char data_length,
225 * char cmd_type);
226 * char *sendbuf sending buffer (MAX. 24bytes)
227 * int addr address of Read/Write/Fill
228 * char data_length data length
229 * char cmd_type type of command
230 *
231 * Description prepare the sending buffer
232 *
233 * Flow Chart
234 *
235 * Return None
236 *
237 * DATE Author Description
238 * ===========================================================================
239 * 2004-04-15 K.M. Ho This is first time implement
240 */
C51 COMPILER V7.50 UART_DV 12/14/2006 10:36:29 PAGE 5
241 void PrepareSendBuf(BYTE *sendbuf, int addr, BYTE data_length, BYTE cmd_type)
242 {
243 1 struct UART_PACK *tx_pack;
244 1 BYTE crc_byte;
245 1 BYTE i;
246 1
247 1 tx_pack = (struct UART_PACK *)sendbuf; //TX struct pointer to sending buffer
248 1 tx_pack->syn1 = SYN_BYTE; //SYN
249 1 tx_pack->cmd = cmd_type; //command
250 1 tx_pack->length = data_length; //data length
251 1 tx_pack->reserve=0x00; //reserve is always 0
252 1 // tx_pack->addr = addr;
253 1 sendbuf[4] = (BYTE) (addr &0x00ff); //addr low byte
254 1 sendbuf[5] = (BYTE)((addr>>8)&0x00ff); //addr high byte
255 1 tx_pack->crc = 0x00; //clean CRC
256 1 tx_pack->syn2 = SYN_BYTE; //put SYN to send buffe
257 1
258 1 crc_byte = 0; //clean CRC check byte
259 1 for (i=0; i<data_length+UART_PACK_SIZE; i++)
260 1 crc_byte ^= sendbuf[i]; //make CRC check byte
261 1
262 1 tx_pack->crc = crc_byte; //put in CRC byte
263 1 }
264
265 /**--------------------------------------------------------------------------
266 * Name void UART_ParseFun(void);
267 *
268 * Description Parse the recive buf when the PARSE flag is setting
269 * There are
270 * ACK device RX is OK
271 * NACK device RX in not OK, request resend
272 * MSGSHOW device request show message on PC screen
273 * DEVICEINF device respond information
274 * MEMRESPOND device respond memory content of address
275 * REGRESPOND register
276 *
277 * Flow Chart 1. copy RX buffer data to tmp and make CRC check byte
278 * 2. clean PARSE flag
279 * 3. is CRC right? No, send NACK command
280 * 4. parse COMMAND
281 * 5. send ACK command if need.
282 *
283 * Return
284 *
285 * DATE Author Description
286 * ===========================================================================
287 * 2003-01-22 KM Ho This is first time implement
288 * 2003-03-27 KM Ho Modify RX ACK command process
289 * 2004-05-19 Eson W.
290 * 2004-11-17 KMHo Add EEPROM Read/Write Command
291 **/
292
293 void UART_ParseFun()
294 {
295 1 struct UART_PACK *rx_pack;
296 1 BYTE crc_byte;
297 1 BYTE buf_index;
298 1 BYTE i, send_length;
299 1 BYTE sendtemp[32];
300 1
301 1 if ( ! (UARTFlag & RX_PARSE) )
302 1 return;
C51 COMPILER V7.50 UART_DV 12/14/2006 10:36:29 PAGE 6
303 1
304 1 LED_G = !LED_G;
305 1
306 1 UARTFlag &= (RX_PARSE^CLEAN_UARTFlag); //clean parse flag
307 1
308 1 buf_index = UART_RxInUse ^ 1; //chang to Rx complete buffer
309 1 //RX buf is double buffer
310 1 rx_pack = (struct UART_PACK *)Rx_Buf[buf_index];
311 1
312 1 crc_byte = 0x00; //make CRC check byte
313 1 for(i=0; i<Parse_Length; i++)
314 1 crc_byte ^= Rx_Buf[buf_index][i];
315 1
316 1 if (crc_byte) //CRC error sent NACK and return
317 1 {
318 2 PrepareSendBuf(sendtemp, 0, 0, SNACK);
319 2 UART_SendOut(sendtemp, UART_PACK_SIZE);
320 2 return;
321 2 }
322 1
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -