3 struct common_area common_area
__attribute__((section(".commonarea")));
4 unsigned int start_addr
, end_addr
, bootrom_unlocked
;
5 extern char _bootrom_start
, _bootrom_end
, _flash_start
, _flash_end
;
7 static void ConfigClocks(void)
9 // we are using a 16 MHz crystal as the basis for everything
10 // slow clock runs at 32Khz typical regardless of crystal
12 // enable system clock and USB clock
13 PMC_SYS_CLK_ENABLE
= PMC_SYS_CLK_PROCESSOR_CLK
| PMC_SYS_CLK_UDP_CLK
;
15 // enable the clock to the following peripherals
16 PMC_PERIPHERAL_CLK_ENABLE
=
24 // worst case scenario, with 16Mhz xtal startup delay is 14.5ms
25 // with a slow clock running at it worst case (max) frequency of 42khz
26 // max startup delay = (14.5ms*42k)/8 = 76 = 0x4C round up to 0x50
28 // enable main oscillator and set startup delay
29 PMC_MAIN_OSCILLATOR
= PMC_MAIN_OSCILLATOR_ENABLE
|
30 PMC_MAIN_OSCILLATOR_STARTUP_DELAY(0x50);
32 // wait for main oscillator to stabilize
33 while ( !(PMC_INTERRUPT_STATUS
& PMC_MAIN_OSCILLATOR_STABILIZED
) )
36 // minimum PLL clock frequency is 80 MHz in range 00 (96 here so okay)
37 // frequency is crystal * multiplier / divisor = 16Mhz * 12 / 2 = 96Mhz
38 PMC_PLL
= PMC_PLL_DIVISOR(2) | PMC_PLL_COUNT_BEFORE_LOCK(0x50) |
39 PMC_PLL_FREQUENCY_RANGE(0) | PMC_PLL_MULTIPLIER(12) |
40 PMC_PLL_USB_DIVISOR(1);
42 // wait for PLL to lock
43 while ( !(PMC_INTERRUPT_STATUS
& PMC_MAIN_OSCILLATOR_PLL_LOCK
) )
46 // we want a master clock (MCK) to be PLL clock / 2 = 96Mhz / 2 = 48Mhz
47 // as per datasheet, this register must be programmed in two operations
48 // when changing to PLL, program the prescaler first then the source
49 PMC_MASTER_CLK
= PMC_CLK_PRESCALE_DIV_2
;
51 // wait for main clock ready signal
52 while ( !(PMC_INTERRUPT_STATUS
& PMC_MAIN_OSCILLATOR_MCK_READY
) )
55 // set the source to PLL
56 PMC_MASTER_CLK
= PMC_CLK_SELECTION_PLL_CLOCK
| PMC_CLK_PRESCALE_DIV_2
;
58 // wait for main clock ready signal
59 while ( !(PMC_INTERRUPT_STATUS
& PMC_MAIN_OSCILLATOR_MCK_READY
) )
63 static void Fatal(void)
68 void UsbPacketReceived(BYTE
*packet
, int len
)
71 UsbCommand
*c
= (UsbCommand
*)packet
;
74 if(len
!= sizeof(*c
)) {
81 c
->cmd
= CMD_DEVICE_INFO
;
82 c
->ext1
= DEVICE_INFO_FLAG_BOOTROM_PRESENT
| DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM
|
83 DEVICE_INFO_FLAG_UNDERSTANDS_START_FLASH
;
84 if(common_area
.flags
.osimage_present
) c
->ext1
|= DEVICE_INFO_FLAG_OSIMAGE_PRESENT
;
85 UsbSendPacket(packet
, len
);
89 /* The temporary write buffer of the embedded flash controller is mapped to the
90 * whole memory region, only the last 8 bits are decoded.
92 p
= (volatile DWORD
*)&_flash_start
;
93 for(i
= 0; i
< 12; i
++) {
94 p
[i
+c
->ext1
] = c
->d
.asDwords
[i
];
98 case CMD_FINISH_WRITE
:
99 p
= (volatile DWORD
*)&_flash_start
;
100 for(i
= 0; i
< 4; i
++) {
101 p
[i
+60] = c
->d
.asDwords
[i
];
104 /* Check that the address that we are supposed to write to is within our allowed region */
105 if( ((c
->ext1
+FLASH_PAGE_SIZE_BYTES
-1) >= end_addr
) || (c
->ext1
< start_addr
) ) {
109 UsbSendPacket(packet
, len
);
111 /* Translate address to flash page and do flash, update here for the 512k part */
112 MC_FLASH_COMMAND
= MC_FLASH_COMMAND_KEY
|
113 MC_FLASH_COMMAND_PAGEN((c
->ext1
-(int)&_flash_start
)/FLASH_PAGE_SIZE_BYTES
) |
116 while(!(MC_FLASH_STATUS
& MC_FLASH_STATUS_READY
))
120 case CMD_HARDWARE_RESET
:
121 USB_D_PLUS_PULLUP_OFF();
122 RSTC_CONTROL
= RST_CONTROL_KEY
| RST_CONTROL_PROCESSOR_RESET
;
125 case CMD_START_FLASH
:
126 if(c
->ext3
== START_FLASH_MAGIC
) bootrom_unlocked
= 1;
127 else bootrom_unlocked
= 0;
129 int prot_start
= (int)&_bootrom_start
;
130 int prot_end
= (int)&_bootrom_end
;
131 int allow_start
= (int)&_flash_start
;
132 int allow_end
= (int)&_flash_end
;
133 int cmd_start
= c
->ext1
;
134 int cmd_end
= c
->ext2
;
136 /* Only allow command if the bootrom is unlocked, or the parameters are outside of the protected
137 * bootrom area. In any case they must be within the flash area.
139 if( (bootrom_unlocked
|| ((cmd_start
>= prot_end
) || (cmd_end
< prot_start
)))
140 && (cmd_start
>= allow_start
) && (cmd_end
<= allow_end
) ) {
141 start_addr
= cmd_start
;
144 start_addr
= end_addr
= 0;
147 UsbSendPacket(packet
, len
);
159 UsbSendPacket(packet
, len
);
163 static void flash_mode(int externally_entered
)
167 bootrom_unlocked
= 0;
175 if(!externally_entered
&& !BUTTON_PRESS()) {
176 /* Perform a reset to leave flash mode */
177 USB_D_PLUS_PULLUP_OFF();
179 RSTC_CONTROL
= RST_CONTROL_KEY
| RST_CONTROL_PROCESSOR_RESET
;
182 if(externally_entered
&& BUTTON_PRESS()) {
183 /* Let the user's button press override the automatic leave */
184 externally_entered
= 0;
189 extern char _osimage_entry
;
193 // First set up all the I/O pins; GPIOs configured directly, other ones
194 // just need to be assigned to the appropriate peripheral.
196 // Kill all the pullups, especially the one on USB D+; leave them for
197 // the unused pins, though.
198 PIO_NO_PULL_UP_ENABLE
= (1 << GPIO_USB_PU
) |
203 (1 << GPIO_FPGA_DIN
) |
204 (1 << GPIO_FPGA_DOUT
) |
205 (1 << GPIO_FPGA_CCLK
) |
206 (1 << GPIO_FPGA_NINIT
) |
207 (1 << GPIO_FPGA_NPROGRAM
) |
208 (1 << GPIO_FPGA_DONE
) |
209 (1 << GPIO_MUXSEL_HIPKD
) |
210 (1 << GPIO_MUXSEL_HIRAW
) |
211 (1 << GPIO_MUXSEL_LOPKD
) |
212 (1 << GPIO_MUXSEL_LORAW
) |
215 // (and add GPIO_FPGA_ON)
216 // These pins are outputs
217 PIO_OUTPUT_ENABLE
= (1 << GPIO_LED_A
) |
223 // PIO controls the following pins
224 PIO_ENABLE
= (1 << GPIO_USB_PU
) |
230 USB_D_PLUS_PULLUP_OFF();
236 // if 512K FLASH part - TODO make some defines :)
237 if ((DBGU_CIDR
| 0xf00) == 0xa00) {
238 MC_FLASH_MODE0
= MC_FLASH_MODE_FLASH_WAIT_STATES(1) |
239 MC_FLASH_MODE_MASTER_CLK_IN_MHZ(0x48);
240 MC_FLASH_MODE1
= MC_FLASH_MODE_FLASH_WAIT_STATES(1) |
241 MC_FLASH_MODE_MASTER_CLK_IN_MHZ(0x48);
243 MC_FLASH_MODE0
= MC_FLASH_MODE_FLASH_WAIT_STATES(0) |
244 MC_FLASH_MODE_MASTER_CLK_IN_MHZ(48);
247 // Initialize all system clocks
252 int common_area_present
= 0;
253 switch(RSTC_STATUS
& RST_STATUS_TYPE_MASK
) {
254 case RST_STATUS_TYPE_WATCHDOG
:
255 case RST_STATUS_TYPE_SOFTWARE
:
256 case RST_STATUS_TYPE_USER
:
257 /* In these cases the common_area in RAM should be ok, retain it if it's there */
258 if(common_area
.magic
== COMMON_AREA_MAGIC
&& common_area
.version
== 1) {
259 common_area_present
= 1;
262 default: /* Otherwise, initialize it from scratch */
266 if(!common_area_present
){
267 /* Common area not ok, initialize it */
268 int i
; for(i
=0; i
<sizeof(common_area
); i
++) { /* Makeshift memset, no need to drag util.c into this */
269 ((char*)&common_area
)[i
] = 0;
271 common_area
.magic
= COMMON_AREA_MAGIC
;
272 common_area
.version
= 1;
273 common_area
.flags
.bootrom_present
= 1;
276 common_area
.flags
.bootrom_present
= 1;
277 if(common_area
.command
== COMMON_AREA_COMMAND_ENTER_FLASH_MODE
) {
278 common_area
.command
= COMMON_AREA_COMMAND_NONE
;
280 } else if(BUTTON_PRESS()) {
283 // jump to Flash address of the osimage entry point (LSBit set for thumb mode)
284 asm("bx %0\n" : : "r" ( ((int)&_osimage_entry
) | 0x1 ) );