📄 loader_f02x.lst
字号:
211 //
212 // This routine erases the FLASH page located at 0x1000
213 //
214 void erase_flash_page(void)
215 {
216 1 bit EA_state;
217 1 char xdata* data pagePointer = 0x1000; // pointer to xdata space located
218 1 // in data space
219 1
220 1 EA_state = EA; // holds interrupt state
221 1
222 1 EA = 0; // disable interrupts
223 1 FLSCL |= 0x01; // enable FLASH write/erase
224 1 PSCTL = 0x03; // MOVX erases FLASH
225 1
226 1 // Erase the FLASH page at 0x1000
227 1 *pagePointer = 0; // initiate the erase
228 1
229 1 PSCTL = 0x00; // MOVX writes target XRAM
230 1 FLSCL &= ~0x01; // disable FLASH write/erase
231 1
232 1 EA = EA_state; // restore interrupt state
233 1
234 1 f_valid = FALSE; // indicate that code is no longer valid
235 1 code_erased = TRUE; // indicate that FLASH has been erased
236 1 }
237
238 //-----------------------------------------------------------------------------
239 // receive_code
240 //-----------------------------------------------------------------------------
241 //
C51 COMPILER V7.50 LOADER_F02X 09/07/2007 14:28:22 PAGE 5
242 // This routine receives HEX records through the UART and writes the
243 // function located at 0x1000.
244 //
245 // Hex Record Format:
246 //
247 // +--------+--------+------+-------+--------+------(n bytes)------+----------+
248 // | RECORD | RECLEN | OFFSET | RECORD | | CHECKSUM |
249 // | MARK | (n) | (2 BYTES) | TYPE | DATA | |
250 // | ':' | | | | | |
251 // +--------+--------+------+-------+--------+------(n bytes)------+----------+
252 //
253 void receive_code(void)
254 {
255 1
256 1 char xdata* data pwrite; // pointer used for writing FLASH
257 1 char code* data pread; // pointer used for reading FLASH
258 1 unsigned int len; // holds the HEX record length field
259 1 char record_type; // holds the HEX record type field
260 1 unsigned int offset; // holds the HEX record offset field
261 1 // this is the starting address of
262 1 // the code image contained in the
263 1 // record
264 1
265 1 char checksum; // holds the HEX record checksum field
266 1 char flash_checksum; // holds the checksum calculated after
267 1 // the FLASH has been programmed
268 1 bit EA_state; // temporary holder used to restore
269 1 // interrupts to their previous state
270 1
271 1 char c; // temporary char
272 1 int i; // temporary int
273 1
274 1 // make sure the FLASH page has been erased
275 1 if(!code_erased){
276 2 printf("\n*** At least one FLASH page must be erased prior");
277 2 printf(" to this operation.\n");
278 2 return;
279 2 }
280 1
281 1 // hex2char:2字节ASCII码转换为单字节数程序;
282 1 // UART串口通信已经使能,PC通过终端程序发送代码
283 1
284 1 do{
285 2
286 2 while( c = _getkey() != ':' ); //等待帧开始标记
287 2
288 2
289 2 len = hex2char(); //获取数据域长度
290 2
291 2
292 2 offset = hex2char(); // 获取数据起始地址
293 2 offset <<= 8;
294 2 offset |= hex2char();
295 2
296 2 // get the record type
297 2 record_type = hex2char();
298 2 if( record_type != 0 && record_type != 1 ){
299 3 printf("\n*** Cannot decode HEX file.\n");
300 3 return;
301 3 }
302 2
303 2 EA_state = EA; // save the interrupt enable bit state
C51 COMPILER V7.50 LOADER_F02X 09/07/2007 14:28:22 PAGE 6
304 2
305 2 EA = 0; // disable interrupts (precautionary)
306 2 FLSCL |= 0x01; // enable FLASH write/erase
307 2 PSCTL = 0x01; // MOVX writes FLASH
308 2
309 2 pwrite = (char xdata*) offset; // initialize the write pointer
310 2
311 2 code_erased = FALSE; // clear the code_erased flag
312 2
313 2 // write the record into FLASH
314 2 for( i = 0; i < len; i++){
315 3 *pwrite = hex2char(); // write one byte to FLASH
316 3 pwrite++; // increment FLASH write pointer
317 3
318 3 }
319 2
320 2 PSCTL = 0x00; // MOVX writes target XRAM
321 2 FLSCL &= ~0x01; // disable FLASH write/erase
322 2 EA = EA_state; // restore interrupts to previous state
323 2
324 2 // verify the checksum
325 2 pread = (char code*) offset; // initialize the read pointer
326 2 checksum = hex2char(); // get the HEX record checksum field
327 2 flash_checksum = 0; // set the flash_checksum to zero
328 2
329 2 // add the data field stored in FLASH to the checksum
330 2 for( i = 0; i < len; i++){
331 3 flash_checksum += *pread++;
332 3 }
333 2
334 2 // add the remaining fields
335 2 flash_checksum += len;
336 2 flash_checksum += (char) (offset >> 8);
337 2 flash_checksum += (char) (offset & 0x00FF);
338 2 flash_checksum += record_type;
339 2 flash_checksum += checksum;
340 2
341 2 // verify the checksum (the flash_checksum should equal zero)
342 2 if(flash_checksum != 0){
343 3 printf("*** Checksum failed, try again.");
344 3 return;
345 3 }
346 2
347 2 } while(record_type != 1);
348 1
349 1 f_valid = TRUE; // flag that the "f()" function is valid
350 1
351 1 _getkey(); // remove carriage return from input
352 1 // stream
353 1
354 1 printf("\nReceived OK\n");
355 1 }
356
357 //-----------------------------------------------------------------------------
358 // Initialization Subroutines
359 //-----------------------------------------------------------------------------
360
361 //-----------------------------------------------------------------------------
362 // SYSCLK_Init
363 //-----------------------------------------------------------------------------
364 //
365 // This routine initializes the system clock to use an 22.1184MHz crystal
C51 COMPILER V7.50 LOADER_F02X 09/07/2007 14:28:22 PAGE 7
366 // as its clock source.
367 //
368 void SYSCLK_Init (void)
369 {
370 1 int i; // delay counter
371 1
372 1 OSCXCN = 0x67; // start external oscillator with
373 1 // 22.1184MHz crystal
374 1
375 1 for (i=0; i < 256; i++) ; // wait for osc to start
376 1
377 1 while (!(OSCXCN & 0x80)) ; // Wait for crystal osc. to settle
378 1
379 1 OSCICN = 0x88; // select external oscillator as SYSCLK
380 1 // source and enable missing clock
381 1 // detector
382 1 }
383
384 //-----------------------------------------------------------------------------
385 // PORT_Init
386 //-----------------------------------------------------------------------------
387 //
388 // Configure the Crossbar and GPIO ports
389 //
390 void PORT_Init (void)
391 {
392 1 XBR0 = 0x04; // Enable UART0
393 1 XBR1 = 0x00;
394 1 XBR2 = 0x40; // Enable crossbar and weak pull-ups
395 1 P0MDOUT |= 0x01; // enable TX0 as a push-pull output
396 1 P1MDOUT |= 0x40; // enable P1.6 (LED) as push-pull output
397 1 }
398
399 //-----------------------------------------------------------------------------
400 // UART0_Init
401 //-----------------------------------------------------------------------------
402 //
403 // Configure the UART0 using Timer1, for <baudrate> and 8-N-1.
404 //
405 void UART0_Init (void)
406 {
407 1 SCON0 = 0x50; // SCON0: mode 1, 8-bit UART, enable RX
408 1 TMOD = 0x20; // TMOD: timer 1, mode 2, 8-bit reload
409 1 TH1 = -(SYSCLK/BAUDRATE/16); // set Timer1 reload value for baudrate
410 1 TR1 = 1; // start Timer1
411 1 CKCON |= 0x10; // Timer1 uses SYSCLK as time base
412 1 PCON |= 0x80; // SMOD00 = 1
413 1 TI0 = 1; // Indicate TX0 ready
414 1 }
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 584 ----
CONSTANT SIZE = 497 ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = 7 15
IDATA SIZE = ---- ----
BIT SIZE = 2 2
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -