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
12 //* Creation : HIi 10/10/2003
13 //*----------------------------------------------------------------------------
14 #include <AT91RM9200.h>
15 #include <lib_AT91RM9200.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
26 #define AT91C_PLLA_VALUE 0x20263E04 // -> 179.712MHz
27 #define AT91C_PLLA_MCK 0x0000202
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
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"
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
56 volatile char XmodemComplete
= 0;
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
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)
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
));
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
;
126 /* ========== Systimer interrupt ============== */
127 if (AT91C_BASE_ST
->ST_SR
& 0x01) {
130 ctlTempo
.CtlTempoTick(&ctlTempo
);
134 error
= AT91F_US_Error((AT91PS_USART
)AT91C_BASE_DBGU
);
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
|
146 if ( !(svcXmodem
.eot
) )
147 svcXmodem
.Handler(&svcXmodem
, csr
);
152 //*-----------------------------------------------------------------------------
153 //* Function Name : AT91F_DisplayMenu()
155 //* Input Parameters :
157 //*-----------------------------------------------------------------------------
158 void AT91F_DisplayMenu(void)
160 printf("\n\rATMEL LOADER %s %s %s\n\r", AT91C_VERSION
, __DATE__
, __TIME__
);
162 AT91F_DataflashPrintInfo();
164 printf(menu_dataflash
);
168 //*-----------------------------------------------------------------------------
169 //* Function Name : AsciiToHex()
170 //* Object : ascii to hexa conversion
171 //* Input Parameters :
173 //*-----------------------------------------------------------------------------
174 unsigned int AsciiToHex(char *s
, unsigned int *val
)
180 if(s
[0] == '0' && ((s
[1] == 'x') || (s
[1] == 'X')))
183 while((n
< 8) && (s
[n
] !=0))
186 if ( (s
[n
] >= '0') && (s
[n
] <='9'))
187 *val
+= (s
[n
] - '0');
189 if ((s
[n
] >= 'a') && (s
[n
] <='f'))
190 *val
+= (s
[n
] - 0x57);
192 if ((s
[n
] >= 'A') && (s
[n
] <='F'))
193 *val
+= (s
[n
] - 0x37);
203 //*-----------------------------------------------------------------------------
204 //* Function Name : AT91F_MemoryDisplay()
205 //* Object : Display the content of the dataflash
206 //* Input Parameters :
208 //*-----------------------------------------------------------------------------
209 int AT91F_MemoryDisplay(unsigned int addr
, unsigned int size
, unsigned int length
)
211 unsigned long i
, nbytes
, linebytes
;
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
)
232 printf(" %08x", *uip
++);
234 printf(" %04x", *usp
++);
236 printf(" %02x", *ucp
++);
241 for (i
=0; i
<linebytes
; i
++) {
242 if ((*cp
< 0x20) || (*cp
> 0x7e))
250 } while (nbytes
> 0);
254 //*-----------------------------------------------------------------------------
255 //* Function Name : AT91F_DramDisplay()
256 //* Object : Display the content of the sdram
257 //* Input Parameters :
259 //*-----------------------------------------------------------------------------
260 int AT91F_SdramDisplay(unsigned int addr
, unsigned int size
, unsigned int length
)
262 unsigned long i
, nbytes
, linebytes
;
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
)
281 printf(" %08x", *uip
++);
283 printf(" %04x", *usp
++);
285 printf(" %02x", *ucp
++);
289 for (i
=0; i
<linebytes
; i
++) {
290 if ((*cp
< 0x20) || (*cp
> 0x7e))
298 } while (nbytes
> 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;
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)
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 */
357 /* write the end of interrupt control register */
358 *AT91C_AIC_EOICR
= 0;
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");
369 AT91F_ResetRegisters();
370 Jump(AT91C_UBOOT_ADDR
);
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 */
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 */
432 AT91S_DataflashDesc desc
;
434 unsigned char read_lock_cmd
[4]=
439 void clear_desc( AT91S_DataflashDesc
*d
)
450 d
->DataFlash_state
= 0;
464 unsigned char rx
[33];
468 desc
.tx_cmd_pt
= read_lock_cmd
;
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]=
494 void clear_lock_reg()
497 unsigned char rx
[33];
498 for ( i
= 0 ; i
< 32 ; i
++)
505 desc
.tx_cmd_pt
= clear_lock_cmd
;
507 desc
.tx_cmd_size
= 4;
508 desc
.rx_cmd_size
= 4;
509 AT91F_SpiWrite(&desc
);
513 unsigned char lock_unlock_cmd
[4]=
518 unsigned char enable_lock_unlock_cmd
[4]=
523 unsigned char disable_lock_unlock_cmd
[4]=
527 void flash_lock_reg()
529 unsigned char rxf
[4];
530 unsigned char rx
[33];
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
);
545 for ( i
= 0 ; i
< 32 ; i
++)
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()
566 unsigned char rxf
[4];
567 unsigned char rx
[33];
571 for ( i
= 0 ; i
< 32 ; i
++)
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
);
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 //*----------------------------------------------------------------------------
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;
614 unsigned int crc1
= 0, crc2
= 0;
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");
657 svcUbootTempo
.Stop(&svcUbootTempo
);
663 AddressToDownload
= AT91C_DOWNLOAD_BASE_ADDRESS
;
664 SizeToDownload
= AT91C_DOWNLOAD_MAX_SIZE
;
670 AT91F_ReadLine("Enter: ", message
);
672 command
= message
[0];
673 if(command
== '1' || command
== '2' || command
== '5' )
674 if(AsciiToHex(&message
[2], &DeviceAddress
) == 0)
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);
689 case CFG_DATAFLASH_LOGIC_ADDR_CS1
:
690 printf("Download Dataflash 1 [0x%x]\n\r", DeviceAddress
& 0xFF000000);
694 case CFG_DATAFLASH_LOGIC_ADDR_CS2
:
695 printf("Download Dataflash 2 [0x%x]\n\r", DeviceAddress
& 0xFF000000);
699 case CFG_DATAFLASH_LOGIC_ADDR_CS3
:
700 printf("Download Dataflash 3 [0x%x]\n\r", DeviceAddress
& 0xFF000000);
713 AT91F_MemoryDisplay(DeviceAddress
, 4, 64);
714 AT91F_ReadLine ((char *)0, message
);
715 DeviceAddress
+= 0x100;
717 while(message
[0] == '\0');
722 AT91F_StartUboot(0, (void *)0);
728 for(i
= (int *)0x20000000; i
< (int *)0x20004000; i
++)
731 write_dataflash(0xc0000000, 0x20000000, 0x4000);
732 printf("Bootsection cleared\r\n");
738 AT91F_SdramDisplay(DeviceAddress
, 4, 64);
739 AT91F_ReadLine ((char *)0, message
);
740 DeviceAddress
+= 0x100;
742 while(message
[0] == '\0');
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
782 i
= dataflash_info
[device
].Device
.pages_number
;
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
);
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: ");
806 pAT91
->CRC32((const unsigned char *)AddressToDownload
, SizeToDownload
, &crc2
);
808 printf("Failed\r\n");
814 AT91F_WaitKeyPressed();