00001
00082 #include "board.h"
00083 #include "gpio.h"
00084 #include "scif_uc3l.h"
00085 #include "power_clocks_lib.h"
00086
00089
00090
00092 #define EXAMPLE_FDFLL_KHZ 50000
00093 #define EXAMPLE_FDFLL_HZ 50000000
00094
00095 volatile int i;
00096 volatile avr32_scif_t * SCIF = &AVR32_SCIF;
00097 volatile avr32_ast_t * AST = &AVR32_AST;
00098 U8 *avr32_sram=AVR32_SRAM;
00099
00100
00101 #define PM_SLEEP(sleep_mode) {__asm__ __volatile__ (\
00102 "MOV R2, 0\n\t"\
00103 "SBR R2, 28\n\t"\
00104 "sleep "STRINGZ(sleep_mode));}
00105
00106
00107
00108
00109
00111
00115 static void local_start_dfll_clock(void)
00116 {
00117 scif_dfll_closedloop_conf_t DfllConfig;
00118 scif_gclk_opt_t GcConf;
00119
00120
00121
00122
00123
00124 GcConf.clock_source = SCIF_GCCTRL_CLK32;
00125 GcConf.diven = OFF;
00126
00127
00128 scif_dfll0_closedloop_mainref_gc_enable(&GcConf);
00129
00130
00131
00132 DfllConfig.coarse = ((unsigned long long)(EXAMPLE_FDFLL_HZ - SCIF_DFLL_MINFREQ_HZ)*255)/(SCIF_DFLL_MAXFREQ_HZ - SCIF_DFLL_MINFREQ_HZ);
00133
00134
00135 DfllConfig.fmul = ((unsigned long long)EXAMPLE_FDFLL_HZ<<16)/SCIF_RC32K_FREQ_HZ;
00136
00137 DfllConfig.maxstep = 1;
00138 scif_dfll0_closedloop_start(&DfllConfig);
00139 }
00140
00141 void flashc_set_wait_state(void)
00142 {
00143 volatile avr32_flashcdw_t * AVR32_FLASHC = &AVR32_FLASHCDW;
00144 AVR32_FLASHC->fcr=AVR32_FLASHC->fcr|0x0040;
00145
00146
00147
00148 }
00149
00150 void flashc_hsben_command(void)
00151 {
00152 volatile avr32_flashcdw_t * AVR32_FLASHC = &AVR32_FLASHCDW;
00153
00154 while ((AVR32_FLASHC->fsr & 0x01)==0);
00155 AVR32_FLASHC->fcmd = 0xA5000010;
00156
00157 while ((AVR32_FLASHC->fsr & 0x01)==0);
00158 }
00159
00160
00161 void gpio_enable_port0_pull_up(void)
00162 {
00163 volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[0];
00164 gpio_port->puers =0x004FD7FF;
00165 }
00166
00167 void gpio_enable_port1_pull_up(void)
00168 {
00169 volatile avr32_gpio_port_t *gpio_port = &AVR32_GPIO.port[1];
00170 gpio_port->puers =0x00001BDF;
00171 }
00172
00173
00174 void gpio_configuration(void)
00175 {
00176 gpio_enable_port0_pull_up();
00177 gpio_enable_port1_pull_up();
00178
00179 gpio_clr_gpio_pin(AVR32_PIN_PA11);
00180 gpio_clr_gpio_pin(AVR32_PIN_PB05);
00181 gpio_clr_gpio_pin(AVR32_PIN_PA20);
00182 gpio_clr_gpio_pin(AVR32_PIN_PA13);
00183 }
00184
00185
00186 void enableOSC32crystal(int pinsel, int mode, int startup)
00187 {
00188 union {
00189 unsigned long oscctrl32;
00190 avr32_scif_oscctrl32_t OSCCTRL32;
00191 } oscctrl32 ;
00192 union {
00193 unsigned long icr;
00194 avr32_scif_icr_t ICR;
00195 } icr;
00196
00197
00198 oscctrl32.oscctrl32 = AVR32_SCIF.oscctrl32;
00199
00200 oscctrl32.OSCCTRL32.osc32en = 1;
00201 oscctrl32.OSCCTRL32.startup = startup;
00202 oscctrl32.OSCCTRL32.mode = mode;
00203 oscctrl32.OSCCTRL32.en32k = 1;
00204 oscctrl32.OSCCTRL32.en1k = 0;
00205 oscctrl32.OSCCTRL32.pinsel = pinsel;
00206 AVR32_SCIF.oscctrl32 = oscctrl32.oscctrl32;
00207 while(((AVR32_SCIF.isr)&AVR32_SCIF_ISR_OSC32RDY_MASK)!=AVR32_SCIF_ISR_OSC32RDY_MASK);
00208 icr.icr = 0;
00209 icr.ICR.osc32rdy = 1;
00210 AVR32_SCIF.icr = icr.icr;
00211 }
00212
00213 void enable_rtc32k_ast(void)
00214 {
00215 enableOSC32crystal(0, 1, 5);
00216 AVR32_AST.clock = 0x100;
00217 while((AVR32_AST.sr & 0x10000000));
00218 AVR32_AST.clock = 0x101;
00219 while((AVR32_AST.sr & 0x10000000));
00220 AVR32_AST.cv = 0;
00221 while((AVR32_AST.sr & 0x01000000));
00222 AVR32_AST.cr = 0x00000100;
00223 while((AVR32_AST.sr & 0x01000000));
00224 AVR32_AST.ar0 = 0x3FFFF;
00225 while((AVR32_AST.sr & 0x01000000));
00226 AVR32_AST.wer = 0x100;
00227 while((AVR32_AST.sr & 0x01000000));
00228 AVR32_AST.cr |= 1;
00229 while((AVR32_AST.sr & 0x01000000));
00230 }
00231
00232 void disable_ast(void)
00233 {
00234 AST->cr=AST->cr&0xFFFFFFFE;
00235 }
00236
00237
00238 void disable_por33_regulator(void)
00239 {
00240 SCIF->unlock=0xAA00006C;
00241 SCIF->rc32kcr=0x01;
00242
00243 SCIF->unlock=0xAA000044;
00244 SCIF->vregcr=0x00090807;
00245 while(SCIF->vregcr&0x00040000!=0x00040000);
00246
00247 SCIF->unlock=0xAA000044;
00248 SCIF->vregcr=0x00190807;
00249 while(SCIF->vregcr&0x00100000!=0x00100000);
00250 SCIF->unlock=0xAA00006C;
00251 SCIF->rc32kcr=0x00;
00252 }
00253
00254 void pm_ppcr_rstpun_mode(void)
00255 {
00256 AVR32_PM.unlock = 0xAA000000 | AVR32_PM_PPCR;
00257 AVR32_PM.ppcr = 0x0;
00258 }
00259
00260 void delayms(int ms)
00261 {
00262 int delay;
00263 delay=ms;
00264 for(delay=ms;delay>=0;delay--)
00265 {
00266 for(i=10; i>0; i--);
00267 }
00268 }
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278 int main(void)
00279 {
00280 long int ast_value=1;
00281 ast_value=*(avr32_sram+1);
00282 if(ast_value==0x00)
00283 {
00284 *(avr32_sram+1)=0x01;
00285
00286 enableOSC32crystal(0, 1, 5);
00287
00288 pm_ppcr_rstpun_mode();
00289
00290
00291
00292
00293 gpio_configuration();
00294
00295 gpio_set_gpio_pin(AVR32_PIN_PA11);
00296 delayms(500);
00297 gpio_clr_gpio_pin(AVR32_PIN_PA11);
00298
00299 disable_ast();
00300
00301
00302 local_start_dfll_clock();
00303
00304 flashc_set_wait_state();
00305
00306 flashc_hsben_command();
00307
00308 pm_set_clk_domain_div((pm_clk_domain_t)AVR32_PM_CLK_GRP_PBA, PM_CKSEL_DIVRATIO_8);
00309
00310 pm_set_mclk_source(PM_CLK_SRC_DFLL0);
00311
00312 disable_por33_regulator();
00313
00314 while(1);
00315 }
00316 if(ast_value==0x01)
00317 {
00318 *(avr32_sram+1)=0x02;
00319 enableOSC32crystal(0, 1, 5);
00320
00321 pm_ppcr_rstpun_mode();
00322
00323
00324
00325
00326 gpio_configuration();
00327
00328 gpio_set_gpio_pin(AVR32_PIN_PA13);
00329 delayms(500);
00330 gpio_clr_gpio_pin(AVR32_PIN_PA13);
00331
00332
00333 local_start_dfll_clock();
00334
00335 flashc_set_wait_state();
00336
00337 flashc_hsben_command();
00338
00339 pm_set_clk_domain_div((pm_clk_domain_t)AVR32_PM_CLK_GRP_PBA, PM_CKSEL_DIVRATIO_8);
00340
00341 pm_set_mclk_source(PM_CLK_SRC_DFLL0);
00342
00343 disable_por33_regulator();
00344
00345 PM_SLEEP(0);
00346
00347 while(1);
00348
00349 }
00350 if(ast_value==0x02)
00351 {
00352 *(avr32_sram+1)=0x03;
00353
00354 pm_ppcr_rstpun_mode();
00355
00356
00357
00358
00359 gpio_configuration();
00360
00361 gpio_set_gpio_pin(AVR32_PIN_PA20);
00362 delayms(500);
00363 gpio_clr_gpio_pin(AVR32_PIN_PA20);
00364
00365 disable_por33_regulator();
00366
00367 enable_rtc32k_ast();
00368
00369 PM_SLEEP(5);
00370
00371 while(1)
00372 {
00373 AVR32_AST.scr = 0x100;
00374 while((AVR32_AST.sr & 0x01000000));
00375
00376 PM_SLEEP(5);
00377 }
00378 }
00379
00380 if(ast_value==0x03)
00381
00382 {
00383 *(avr32_sram+1)=0x00;
00384
00385 pm_ppcr_rstpun_mode();
00386
00387
00388
00389
00390 gpio_configuration();
00391
00392 gpio_set_gpio_pin(AVR32_PIN_PB05);
00393 delayms(500);
00394 gpio_clr_gpio_pin(AVR32_PIN_PB05);
00395
00396 disable_por33_regulator();
00397
00398 disable_ast();
00399
00400 scif_stop_osc32();
00401
00402 PM_SLEEP(5);
00403 while(1);
00404 }
00405 else
00406 {
00407 *(avr32_sram+1)=0x01;
00408 enableOSC32crystal(0, 1, 5);
00409
00410 pm_ppcr_rstpun_mode();
00411
00412
00413
00414
00415 gpio_configuration();
00416
00417 gpio_set_gpio_pin(AVR32_PIN_PA11);
00418 delayms(500);
00419 gpio_clr_gpio_pin(AVR32_PIN_PA11);
00420
00421 disable_ast();
00422
00423
00424 local_start_dfll_clock();
00425
00426 flashc_set_wait_state();
00427
00428 flashc_hsben_command();
00429
00430 pm_set_clk_domain_div((pm_clk_domain_t)AVR32_PM_CLK_GRP_PBA, PM_CKSEL_DIVRATIO_8);
00431
00432 pm_set_mclk_source(PM_CLK_SRC_DFLL0);
00433
00434 disable_por33_regulator();
00435
00436 while(1);
00437 }
00438 }
00439