Merge branch 'master' into test
[romboot.git] / main.cpp
blob45bfdf53b6e61caae49721332dc8f2a2e6c5d191
1 //*----------------------------------------------------------------------------
2 //* ATMEL Microcontroller Software Support - ROUSSET -
3 //*----------------------------------------------------------------------------
4 //* The software is delivered "AS IS" without warranty or condition of any
5 //* kind, either express, implied or statutory. This includes without
6 //* limitation any warranty or condition with respect to merchantability or
7 //* fitness for any particular purpose, or against the infringements of
8 //* intellectual property rights of others.
9 //*----------------------------------------------------------------------------
10 //* File Name : main.c
11 //* Object :
12 //* Creation : HIi 10/10/2003
13 //*----------------------------------------------------------------------------
14 #include <AT91RM9200.h>
15 #include <lib_AT91RM9200.h>
17 #include "com.h"
18 #include "main.h"
19 #include "dataflash.h"
21 #define AT91C_UBOOT_ADDR 0x21F00000
22 #define AT91C_UBOOT_SIZE 512*1024
23 #define AT91C_UBOOT_DATAFLASH_ADDR 0xC1000000
25 // crystal= 18.432MHz
26 #define AT91C_PLLA_VALUE 0x20263E04 // -> 179.712MHz
27 #define AT91C_PLLA_MCK 0x0000202
29 // crystal= 20.000MHz
30 // #define AT91C_PLLA_VALUE 0x2023BE04 // -> 180MHz
31 // #define AT91C_PLLA_MCK 0x0000202
33 #define DELAY_MAIN_FREQ 1000
34 #define DISP_LINE_LEN 16
36 //* prototypes
37 extern void AT91F_DBGU_Printk(char *);
38 extern "C" void AT91F_ST_ASM_Handler(void);
39 extern "C" void Jump(unsigned int addr);
41 const char *menu_separ = "*----------------------------------------*\n\r";
43 const char *menu_dataflash = {
44 "1: Download Dataflash [addr]\n\r"
45 "2: Read Dataflash [addr]\n\r"
46 "3: Start U-BOOT\n\r"
47 "4: Clear bootloader section in Dataflash\n\r"
48 "5: Read Dram [addr]\n\r"
49 "6: Lock the flash\n\r"
50 "7: Unlock the flash\r\n"
51 "8: Read the lock register status of the flash\r\n"
54 //* Globales variables
55 char message[40];
56 volatile char XmodemComplete = 0;
57 unsigned int StTick;
59 AT91S_RomBoot const *pAT91;
60 AT91S_SBuffer sXmBuffer;
61 AT91S_SvcXmodem svcXmodem;
62 AT91S_Pipe xmodemPipe;
63 AT91S_CtlTempo ctlTempo;
64 AT91S_SvcTempo svcTempo; // Link to a AT91S_Tempo object
66 void switch_led();
68 //*--------------------------------------------------------------------------------------
69 //* Function Name : GetTickCount()
70 //* Object : Return the number of systimer tick
71 //* Input Parameters :
72 //* Output Parameters :
73 //*--------------------------------------------------------------------------------------
74 unsigned int GetTickCount(void)
76 return StTick;
80 //*--------------------------------------------------------------------------------------
81 //* Function Name : AT91_XmodemComplete()
82 //* Object : Perform the remap and jump to appli in RAM
83 //* Input Parameters :
84 //* Output Parameters :
85 //*--------------------------------------------------------------------------------------
86 void AT91_XmodemComplete(AT91S_PipeStatus status, void *pVoid)
88 // stop the Xmodem tempo
89 svcXmodem.tempo.Stop(&(svcXmodem.tempo));
90 XmodemComplete = 1;
94 //*--------------------------------------------------------------------------------------
95 //* Function Name : AT91F_XmodemProtocol(AT91S_PipeStatus status, void *pVoid)
96 //* Object : Xmodem dispatcher
97 //* Input Parameters :
98 //* Output Parameters :
99 //*--------------------------------------------------------------------------------------
100 void XmodemProtocol(AT91S_PipeStatus status, void *pVoid)
102 AT91PS_SBuffer pSBuffer = (AT91PS_SBuffer) xmodemPipe.pBuffer->pChild;
103 AT91PS_USART pUsart = svcXmodem.pUsart;
105 if (pSBuffer->szRdBuffer == 0) {
106 // Start a tempo to wait the Xmodem protocol complete
107 svcXmodem.tempo.Start(&(svcXmodem.tempo), 10, 0, AT91_XmodemComplete, pUsart);
112 //*-------------------------- Interrupt handlers ----------------------------------------
113 //*--------------------------------------------------------------------------------------
114 //* Function Name : irq1_c_handler()
115 //* Object : C Interrupt handler for Interrutp source 1
116 //* Input Parameters : none
117 //* Output Parameters : none
118 //*--------------------------------------------------------------------------------------
119 extern "C" void AT91F_ST_Handler(void);
121 void AT91F_ST_Handler(void)
123 volatile unsigned int csr = *AT91C_DBGU_CSR;
124 unsigned int error;
126 /* ========== Systimer interrupt ============== */
127 if (AT91C_BASE_ST->ST_SR & 0x01) {
128 StTick++;
129 switch_led();
130 ctlTempo.CtlTempoTick(&ctlTempo);
131 return;
134 error = AT91F_US_Error((AT91PS_USART)AT91C_BASE_DBGU);
135 if (csr & error) {
136 // Stop previous Xmodem transmition
137 *(AT91C_DBGU_CR) = AT91C_US_RSTSTA;
138 AT91F_US_DisableIt((AT91PS_USART)AT91C_BASE_DBGU, AT91C_US_ENDRX);
139 AT91F_US_EnableIt((AT91PS_USART)AT91C_BASE_DBGU, AT91C_US_RXRDY);
143 else if (csr & (AT91C_US_TXRDY | AT91C_US_ENDTX | AT91C_US_TXEMPTY |
144 AT91C_US_RXRDY | AT91C_US_ENDRX | AT91C_US_TIMEOUT |
145 AT91C_US_RXBUFF)) {
146 if ( !(svcXmodem.eot) )
147 svcXmodem.Handler(&svcXmodem, csr);
152 //*-----------------------------------------------------------------------------
153 //* Function Name : AT91F_DisplayMenu()
154 //* Object :
155 //* Input Parameters :
156 //* Return value :
157 //*-----------------------------------------------------------------------------
158 void AT91F_DisplayMenu(void)
160 printf("\n\rATMEL LOADER %s %s %s\n\r", AT91C_VERSION, __DATE__, __TIME__);
161 printf(menu_separ);
162 AT91F_DataflashPrintInfo();
163 printf(menu_separ);
164 printf(menu_dataflash);
165 printf(menu_separ);
168 //*-----------------------------------------------------------------------------
169 //* Function Name : AsciiToHex()
170 //* Object : ascii to hexa conversion
171 //* Input Parameters :
172 //* Return value :
173 //*-----------------------------------------------------------------------------
174 unsigned int AsciiToHex(char *s, unsigned int *val)
176 int n;
178 *val=0;
180 if(s[0] == '0' && ((s[1] == 'x') || (s[1] == 'X')))
181 s+=2;
182 n = 0;
183 while((n < 8) && (s[n] !=0))
185 *val <<= 4;
186 if ( (s[n] >= '0') && (s[n] <='9'))
187 *val += (s[n] - '0');
188 else
189 if ((s[n] >= 'a') && (s[n] <='f'))
190 *val += (s[n] - 0x57);
191 else
192 if ((s[n] >= 'A') && (s[n] <='F'))
193 *val += (s[n] - 0x37);
194 else
195 return 0;
196 n++;
199 return 1;
203 //*-----------------------------------------------------------------------------
204 //* Function Name : AT91F_MemoryDisplay()
205 //* Object : Display the content of the dataflash
206 //* Input Parameters :
207 //* Return value :
208 //*-----------------------------------------------------------------------------
209 int AT91F_MemoryDisplay(unsigned int addr, unsigned int size, unsigned int length)
211 unsigned long i, nbytes, linebytes;
212 char *cp;
213 unsigned int *uip;
214 unsigned short *usp;
215 unsigned char *ucp;
216 char linebuf[DISP_LINE_LEN];
218 nbytes = length * size;
221 uip = (unsigned int *)linebuf;
222 usp = (unsigned short *)linebuf;
223 ucp = (unsigned char *)linebuf;
225 printf("%08x:", addr);
226 linebytes = (nbytes > DISP_LINE_LEN)?DISP_LINE_LEN:nbytes;
228 read_dataflash(addr, (linebytes/size)*size, linebuf);
229 for (i=0; i<linebytes; i+= size)
231 if (size == 4)
232 printf(" %08x", *uip++);
233 else if (size == 2)
234 printf(" %04x", *usp++);
235 else
236 printf(" %02x", *ucp++);
237 addr += size;
239 printf(" ");
240 cp = linebuf;
241 for (i=0; i<linebytes; i++) {
242 if ((*cp < 0x20) || (*cp > 0x7e))
243 printf(".");
244 else
245 printf("%c", *cp);
246 cp++;
248 printf("\n\r");
249 nbytes -= linebytes;
250 } while (nbytes > 0);
251 return 0;
254 //*-----------------------------------------------------------------------------
255 //* Function Name : AT91F_DramDisplay()
256 //* Object : Display the content of the sdram
257 //* Input Parameters :
258 //* Return value :
259 //*-----------------------------------------------------------------------------
260 int AT91F_SdramDisplay(unsigned int addr, unsigned int size, unsigned int length)
262 unsigned long i, nbytes, linebytes;
263 char *cp;
264 unsigned int *uip;
265 unsigned short *usp;
266 unsigned char *ucp;
268 nbytes = length * size;
271 uip = (unsigned int *)addr;
272 usp = (unsigned short *)addr;
273 ucp = (unsigned char *)addr;
275 printf("%08x:", addr);
276 linebytes = (nbytes > DISP_LINE_LEN)?DISP_LINE_LEN:nbytes;
278 for (i=0; i<linebytes; i+= size)
280 if (size == 4)
281 printf(" %08x", *uip++);
282 else if (size == 2)
283 printf(" %04x", *usp++);
284 else
285 printf(" %02x", *ucp++);
286 addr += size;
288 printf(" ");
289 for (i=0; i<linebytes; i++) {
290 if ((*cp < 0x20) || (*cp > 0x7e))
291 printf(".");
292 else
293 printf("%c", *cp);
294 cp++;
296 printf("\n\r");
297 nbytes -= linebytes;
298 } while (nbytes > 0);
299 return 0;
303 //*--------------------------------------------------------------------------------------
304 //* Function Name : AT91F_SetPLL
305 //* Object : Set the PLLA to 180Mhz and Master clock to 60 Mhz
306 //* Input Parameters :
307 //* Output Parameters :
308 //*--------------------------------------------------------------------------------------
309 void AT91F_SetPLL(void)
311 volatile int tmp = 0;
313 AT91PS_PMC pPmc = AT91C_BASE_PMC;
314 AT91PS_CKGR pCkgr = AT91C_BASE_CKGR;
316 pPmc->PMC_IDR = 0xFFFFFFFF;
318 //* -Setup the PLL A
319 pCkgr->CKGR_PLLAR = AT91C_PLLA_VALUE;
321 while(!(pPmc->PMC_SR & AT91C_PMC_MCKRDY) && (tmp++ < DELAY_MAIN_FREQ));
323 //* - Commuting Master Clock from PLLB to PLLA/3
324 pPmc->PMC_MCKR = AT91C_PLLA_MCK;
328 //*--------------------------------------------------------------------------------------
329 //* Function Name : AT91F_ResetRegisters
330 //* Object : Restore the initial state to registers
331 //* Input Parameters :
332 //* Output Parameters :
333 //*--------------------------------------------------------------------------------------
334 static void AT91F_ResetRegisters(void)
336 volatile int i = 0;
338 //* set the PIOs in input
339 *AT91C_PIOA_ODR = 0xFFFFFFFF; /* Disables all the output pins */
340 *AT91C_PIOA_PER = 0xFFFFFFFF; /* Enables the PIO to control all the pins */
342 AT91F_AIC_DisableIt (AT91C_BASE_AIC, AT91C_ID_SYS);
344 /* close all peripheral clocks */
345 AT91C_BASE_PMC->PMC_PCDR = 0xFFFFFFFC;
347 //* Disable core interrupts and set supervisor mode
348 __asm__ ("msr CPSR_c, #0xDF"); //* ARM_MODE_SYS(0x1F) | I_BIT(0x80) | F_BIT(0x40)
350 //* Clear all the interrupts
351 *AT91C_AIC_ICCR = 0xffffffff;
353 /* read the AIC_IVR and AIC_FVR */
354 i = *AT91C_AIC_IVR;
355 i = *AT91C_AIC_FVR;
357 /* write the end of interrupt control register */
358 *AT91C_AIC_EOICR = 0;
360 AT91F_SetPLL();
363 void AT91F_StartUboot(unsigned int dummy, void *pvoid)
365 printf("Load U-BOOT from dataflash[%x] to SDRAM[%x]\n\r", AT91C_UBOOT_DATAFLASH_ADDR, AT91C_UBOOT_ADDR);
366 read_dataflash(AT91C_UBOOT_DATAFLASH_ADDR, AT91C_UBOOT_SIZE, (char *)(AT91C_UBOOT_ADDR));
367 printf("Set PLLA to 180Mhz and Master clock to 60Mhz and start U-BOOT\n\r");
368 //* Reset registers
369 AT91F_ResetRegisters();
370 Jump(AT91C_UBOOT_ADDR);
371 while(1);
374 void turn_led_off()
376 *AT91C_PIOD_ODR = AT91C_PIO_PD6; /* Manage PD6 io */
377 *AT91C_PIOD_PER = AT91C_PIO_PD6; /* Enable PD6 PIO */
378 *AT91C_PIOD_OER = AT91C_PIO_PD6; /* Enables PD6 ouput */
379 *AT91C_PIOD_SODR = AT91C_PIO_PD6; /* Set PD6 */
382 void turn_led_on()
384 *AT91C_PIOD_ODR = AT91C_PIO_PD6;
385 *AT91C_PIOD_PER = AT91C_PIO_PD6;
386 *AT91C_PIOD_OER = AT91C_PIO_PD6;
387 *AT91C_PIOD_CODR = AT91C_PIO_PD6;
390 void reset_flash_lock()
392 *AT91C_PIOB_ODR = AT91C_PIO_PC15; /* Manage PB15 io */
393 *AT91C_PIOB_PER = AT91C_PIO_PC15; /* Enables PB15 io */
394 *AT91C_PIOB_OER = AT91C_PIO_PC15; /* Enables PB15 ouput */
395 *AT91C_PIOB_CODR = AT91C_PIO_PC15; /* Clear PB 15 */
398 #define ILIM0 0
399 #define ILIM1 100
400 #define ILIM2 200
401 int cnt=0;
402 void switch_led()
404 switch ( cnt)
406 case ILIM0 :
408 turn_led_on();
409 cnt++;
410 break;
413 case ILIM1:
415 turn_led_off();
416 cnt++;
417 break;
419 case ILIM2:
421 cnt = ILIM0;
422 break;
424 default:
426 cnt++;
427 break;
432 AT91S_DataflashDesc desc;
434 unsigned char read_lock_cmd[4]=
436 0x32,0x00,0x00,0x00
439 void clear_desc( AT91S_DataflashDesc *d)
441 d->tx_cmd_pt = 0;
442 d->rx_cmd_pt = 0;
443 d->tx_cmd_size = 0;
444 d->rx_cmd_size = 0;
445 d->tx_data_size = 0;
446 d->rx_data_size = 0;
447 d->tx_data_pt = 0;
448 d->rx_data_pt = 0;
449 d->state = 0;
450 d->DataFlash_state = 0;
451 d->command[0] = 0;
452 d->command[1] = 0;
453 d->command[2] = 0;
454 d->command[3] = 0;
455 d->command[4] = 0;
456 d->command[5] = 0;
457 d->command[6] = 0;
458 d->command[7] = 0;
461 void read_lock_reg()
463 int i;
464 unsigned char rx[33];
466 clear_desc(&desc);
467 rx[0] = 0x8;
468 desc.tx_cmd_pt = read_lock_cmd;
469 desc.rx_cmd_pt = rx;
470 desc.tx_cmd_size = 4;
471 desc.rx_cmd_size = 4;
472 desc.tx_data_size = 32;
473 desc.rx_data_size = 32;
474 desc.tx_data_pt = rx;
475 desc.rx_data_pt = rx;
477 AT91F_SpiWrite(&desc);
478 printf("Lock status = \r\n");
479 for ( i = 0 ; i < 16 ; i++ )
481 printf("0x%02x:",rx[i]);
483 for ( i = 16 ; i < 32 ; i++ )
485 printf("0x%02x:",rx[i]);
489 unsigned char clear_lock_cmd[4]=
491 0x3D,0x2A,0x7F,0xCF
494 void clear_lock_reg()
496 int i;
497 unsigned char rx[33];
498 for ( i = 0 ; i < 32 ; i++)
500 rx[i] = 0;
503 clear_desc(&desc);
505 desc.tx_cmd_pt = clear_lock_cmd;
506 desc.rx_cmd_pt = rx;
507 desc.tx_cmd_size = 4;
508 desc.rx_cmd_size = 4;
509 AT91F_SpiWrite(&desc);
510 read_lock_reg();
513 unsigned char lock_unlock_cmd[4]=
515 0x3D,0x2A,0x7F,0xFC
518 unsigned char enable_lock_unlock_cmd[4]=
520 0x3D,0x2A,0x7F,0xA9
523 unsigned char disable_lock_unlock_cmd[4]=
525 0x3D,0x2A,0x7F,0x9A
527 void flash_lock_reg()
529 unsigned char rxf[4];
530 unsigned char rx[33];
531 int i;
533 clear_lock_reg();
535 clear_desc(&desc);
537 desc.tx_cmd_pt = enable_lock_unlock_cmd;
538 desc.rx_cmd_pt = rxf;
539 desc.tx_cmd_size = 4;
540 desc.rx_cmd_size = 4;
542 AT91F_SpiWrite(&desc);
543 clear_desc(&desc);
545 for ( i = 0 ; i < 32 ; i++)
547 rx[i] = 0xff;
549 desc.tx_cmd_pt = lock_unlock_cmd;
550 desc.rx_cmd_pt = rxf;
551 desc.tx_cmd_size = 4;
552 desc.rx_cmd_size = 4;
553 desc.tx_data_size = 32;
554 desc.rx_data_size = 32;
555 desc.tx_data_pt = rx;
556 desc.rx_data_pt = rx;
558 AT91F_SpiWrite(&desc);
563 void flash_unlock_reg()
565 int i;
566 unsigned char rxf[4];
567 unsigned char rx[33];
569 clear_desc(&desc);
571 for ( i = 0 ; i < 32 ; i++)
573 rx[i] = 0;
576 desc.tx_cmd_pt = lock_unlock_cmd;
577 desc.rx_cmd_pt = rxf;
578 desc.tx_cmd_size = 4;
579 desc.rx_cmd_size = 4;
580 desc.tx_data_size = 32;
581 desc.rx_data_size = 32;
582 desc.tx_data_pt = rx;
583 desc.rx_data_pt = rx;
585 AT91F_SpiWrite(&desc);
587 clear_desc(&desc);
589 desc.tx_cmd_pt = disable_lock_unlock_cmd;
590 desc.rx_cmd_pt = rxf;
591 desc.tx_cmd_size = 4;
592 desc.rx_cmd_size = 4;
594 AT91F_SpiWrite(&desc);
598 //*----------------------------------------------------------------------------
599 //* Function Name : main
600 //* Object : Main function
601 //* Input Parameters : none
602 //* Output Parameters : True
603 //*----------------------------------------------------------------------------
604 int main(void)
606 AT91PS_Buffer pXmBuffer;
607 AT91PS_SvcComm pSvcXmodem;
608 AT91S_SvcTempo svcUbootTempo; // Link to a AT91S_Tempo object
610 unsigned int AddressToDownload, SizeToDownload;
611 unsigned int DeviceAddress = 0;
612 volatile int i = 0;
613 char command = 0;
614 unsigned int crc1 = 0, crc2 = 0;
615 volatile int device;
616 int NbPage;
618 reset_flash_lock();
620 stdin = fopen(0, at91_dbgu_getc);
621 stdout = fopen(at91_dbgu_putc, 0);
623 pAT91 = AT91C_ROM_BOOT_ADDRESS;
625 // Tempo Initialisation
626 pAT91->OpenCtlTempo(&ctlTempo, (void *) &(pAT91->SYSTIMER_DESC));
627 ctlTempo.CtlTempoStart((void *) &(pAT91->SYSTIMER_DESC));
629 // Attach the tempo to a tempo controler
630 ctlTempo.CtlTempoCreate(&ctlTempo, &svcUbootTempo);
632 // Xmodem Initialisation
633 pXmBuffer = pAT91->OpenSBuffer(&sXmBuffer);
634 pSvcXmodem = pAT91->OpenSvcXmodem(&svcXmodem, (AT91PS_USART)AT91C_BASE_DBGU, &ctlTempo);
635 pAT91->OpenPipe(&xmodemPipe, pSvcXmodem, pXmBuffer);
637 //* System Timer initialization
638 AT91F_AIC_ConfigureIt (
639 AT91C_BASE_AIC, // AIC base address
640 AT91C_ID_SYS, // System peripheral ID
641 AT91C_AIC_PRIOR_HIGHEST, // Max priority
642 AT91C_AIC_SRCTYPE_INT_LEVEL_SENSITIVE, // Level sensitive
643 AT91F_ST_ASM_Handler );
644 //* Enable ST interrupt
645 AT91F_AIC_EnableIt(AT91C_BASE_AIC, AT91C_ID_SYS);
647 // DataFlash on SPI Configuration
648 AT91F_DataflashInit ();
650 // start tempo to start Uboot in a delay of 1 sec if no key pressed
651 svcUbootTempo.Start(&svcUbootTempo, 200, 0, AT91F_StartUboot, (void *)0);
653 printf("press any key to enter bootloader\n\r");
654 getc();
656 // stop tempo
657 svcUbootTempo.Stop(&svcUbootTempo);
659 while(1)
661 while(command == 0)
663 AddressToDownload = AT91C_DOWNLOAD_BASE_ADDRESS;
664 SizeToDownload = AT91C_DOWNLOAD_MAX_SIZE;
665 DeviceAddress = 0;
667 AT91F_DisplayMenu();
668 message[0] = 0;
669 message[2] = 0;
670 AT91F_ReadLine("Enter: ", message);
672 command = message[0];
673 if(command == '1' || command == '2' || command == '5' )
674 if(AsciiToHex(&message[2], &DeviceAddress) == 0)
675 command = 0;
677 switch(command)
679 case '1':
680 printf("Download Dataflash [0x%x]\n\r", DeviceAddress);
682 switch(DeviceAddress & 0xFF000000)
684 case CFG_DATAFLASH_LOGIC_ADDR_CS0:
685 printf("Download Dataflash 0 [0x%x]\n\r", DeviceAddress & 0xFF000000);
686 device = 0;
687 break;
689 case CFG_DATAFLASH_LOGIC_ADDR_CS1:
690 printf("Download Dataflash 1 [0x%x]\n\r", DeviceAddress & 0xFF000000);
691 device = 1;
692 break;
694 case CFG_DATAFLASH_LOGIC_ADDR_CS2:
695 printf("Download Dataflash 2 [0x%x]\n\r", DeviceAddress & 0xFF000000);
696 device = 2;
697 break;
699 case CFG_DATAFLASH_LOGIC_ADDR_CS3:
700 printf("Download Dataflash 3 [0x%x]\n\r", DeviceAddress & 0xFF000000);
701 device = 3;
702 break;
704 default:
705 command = 0;
706 break;
708 break;
710 case '2':
713 AT91F_MemoryDisplay(DeviceAddress, 4, 64);
714 AT91F_ReadLine ((char *)0, message);
715 DeviceAddress += 0x100;
717 while(message[0] == '\0');
718 command = 0;
719 break;
721 case '3':
722 AT91F_StartUboot(0, (void *)0);
723 command = 0;
724 break;
725 case '4':
727 int *i;
728 for(i = (int *)0x20000000; i < (int *)0x20004000; i++)
729 *i = 0;
731 write_dataflash(0xc0000000, 0x20000000, 0x4000);
732 printf("Bootsection cleared\r\n");
733 command = 0;
734 break;
735 case '5':
738 AT91F_SdramDisplay(DeviceAddress, 4, 64);
739 AT91F_ReadLine ((char *)0, message);
740 DeviceAddress += 0x100;
742 while(message[0] == '\0');
743 command = 0;
744 break;
745 case '6':
747 flash_lock_reg();
748 command = 0;
749 break;
751 case '7':
753 flash_unlock_reg();
754 command = 0;
755 break;
757 case '8':
759 read_lock_reg();
760 command = 0;
761 break;
763 case '9':
765 clear_lock_reg();
766 command = 0;
767 break;
770 default:
771 command = 0;
772 break;
776 xmodemPipe.Read(&xmodemPipe, (char *)AddressToDownload, SizeToDownload, XmodemProtocol, 0);
777 while(XmodemComplete !=1);
778 SizeToDownload = (unsigned int)(svcXmodem.pData) - (unsigned int)AddressToDownload;
780 // Modification of vector 6
781 NbPage = 0;
782 i = dataflash_info[device].Device.pages_number;
783 while(i >>= 1)
784 NbPage++;
785 i = (SizeToDownload / 512) + 1 + (NbPage << 13) + (dataflash_info[device].Device.pages_size << 17);
786 *(int *)(AddressToDownload + AT91C_OFFSET_VECT6) = i;
788 printf("\n\rModification of Arm Vector 6 :%x\n\r", i);
790 printf("\n\rWrite %d bytes in DataFlash [0x%x]\n\r",SizeToDownload, DeviceAddress);
791 crc1 = 0;
792 pAT91->CRC32((const unsigned char *)AddressToDownload, SizeToDownload , &crc1);
794 // write the dataflash
795 write_dataflash (DeviceAddress, AddressToDownload, SizeToDownload);
796 // clear the buffer before read
797 for(i=0; i < SizeToDownload; i++)
798 *(unsigned char *)(AddressToDownload + i) = 0;
800 //* Read dataflash page in TestBuffer
801 read_dataflash (DeviceAddress, SizeToDownload, (char *)(AddressToDownload));
803 printf("Verify Dataflash: ");
804 crc2 = 0;
806 pAT91->CRC32((const unsigned char *)AddressToDownload, SizeToDownload , &crc2);
807 if (crc1 != crc2)
808 printf("Failed\r\n");
809 else
810 printf("OK\r\n");
812 command = 0;
813 XmodemComplete = 0;
814 AT91F_WaitKeyPressed();