8 #include PLATFORM_HEADER
9 #include "hal/micro/micro-common.h"
108 #define WAKEINFOVALID_INTERNAL_WAKE_EVENT_BIT 31
109 #define SLEEPSKIPPED_INTERNAL_WAKE_EVENT_BIT 30
110 #define CSYSPWRUPREQ_INTERNAL_WAKE_EVENT_BIT 29
111 #define CDBGPWRUPREQ_INTERNAL_WAKE_EVENT_BIT 28
112 #define WAKE_CORE_INTERNAL_WAKE_EVENT_BIT 27
113 #define WRAP_INTERNAL_WAKE_EVENT_BIT 26
114 #define CMPB_INTERNAL_WAKE_EVENT_BIT 25
115 #define CMPA_INTERNAL_WAKE_EVENT_BIT 24
118 #define INTERNAL_WAKE_EVENT_BIT_SHIFT 20
120 static int32u halInternalWakeEvent=0;
124 return halInternalWakeEvent;
132 int32u SLEEPTMR_CLKEN_SAVED = SLEEPTMR_CLKEN;
141 int32u gpioWakeSel = (GPIO_PAWAKE<<0);
142 gpioWakeSel |= (GPIO_PBWAKE<<8);
143 gpioWakeSel |= (GPIO_PCWAKE<<16);
146 if(GPIO_PBWAKE & PB2) {
147 WAKE_SEL |= WAKE_SC1;
151 if(GPIO_PAWAKE & PA2) {
152 WAKE_SEL |= WAKE_SC2;
156 if(gpioWakeSel &
BIT(GPIO_IRQDSEL)) {
157 WAKE_SEL |= WAKE_IRQD;
160 halInternalWakeEvent = 0;
164 case SLEEPMODE_NOTIMER:
171 case SLEEPMODE_WAKETIMER:
180 if(INT_SLEEPTMRCFG&INT_SLEEPTMRWRAP) {
181 WAKE_SEL |= WAKE_SLEEPTMRWRAP;
183 if(INT_SLEEPTMRCFG&INT_SLEEPTMRCMPB) {
184 WAKE_SEL |= WAKE_SLEEPTMRCMPB;
186 if(INT_SLEEPTMRCFG&INT_SLEEPTMRCMPA) {
187 WAKE_SEL |= WAKE_SLEEPTMRCMPA;
191 case SLEEPMODE_MAINTAINTIMER:
213 boolean skipSleep =
FALSE;
251 int32u INT_SLEEPTMRCFG_SAVED = INT_SLEEPTMRCFG_REG;
252 int32u INT_MGMTCFG_SAVED = INT_MGMTCFG_REG;
258 int32u GPIO_INTCFGA_SAVED = GPIO_INTCFGA_REG;
259 int32u GPIO_INTCFGB_SAVED = GPIO_INTCFGB_REG;
260 int32u GPIO_INTCFGC_SAVED = GPIO_INTCFGC_REG;
261 int32u GPIO_INTCFGD_SAVED = GPIO_INTCFGD_REG;
265 int32u OSC24M_BIASTRIM_SAVED = OSC24M_BIASTRIM_REG;
266 int32u OSCHF_TUNE_SAVED = OSCHF_TUNE_REG;
267 int32u DITHER_DIS_SAVED = DITHER_DIS_REG;
272 int32u PCTRACE_SEL_SAVED = PCTRACE_SEL_REG;
274 int32u MEM_PROT_0_SAVED = MEM_PROT_0_REG;
275 int32u MEM_PROT_1_SAVED = MEM_PROT_1_REG;
276 int32u MEM_PROT_2_SAVED = MEM_PROT_2_REG;
277 int32u MEM_PROT_3_SAVED = MEM_PROT_3_REG;
278 int32u MEM_PROT_4_SAVED = MEM_PROT_4_REG;
279 int32u MEM_PROT_5_SAVED = MEM_PROT_5_REG;
280 int32u MEM_PROT_6_SAVED = MEM_PROT_6_REG;
281 int32u MEM_PROT_7_SAVED = MEM_PROT_7_REG;
282 int32u MEM_PROT_EN_SAVED = MEM_PROT_EN_REG;
298 int32u INT_CFGSET_SAVED = INT_CFGSET_REG;
305 int32u SCS_VTOR_SAVED = SCS_VTOR_REG;
317 EVENT_CTRL = LV_FREEZE;
323 int32u GPIO_IN_SAVED = GPIO_PAIN;
324 GPIO_IN_SAVED |= (GPIO_PBIN<<8);
325 GPIO_IN_SAVED |= (GPIO_PCIN<<16);
327 PWRUP_EVENT = 0xFFFFFFFF;
344 if((INT_PENDSET&INT_IRQA) &&
346 (WAKE_SEL&GPIO_WAKE)) {
355 if((INT_PENDSET&INT_IRQB) &&
357 (WAKE_SEL&GPIO_WAKE)) {
366 if((INT_PENDSET&INT_IRQC) &&
367 (gpioWakeSel&
BIT(GPIO_IRQCSEL)) &&
368 (WAKE_SEL&GPIO_WAKE)) {
371 halInternalWakeEvent |=
BIT(GPIO_IRQCSEL);
377 if((INT_PENDSET&INT_IRQD) &&
378 (gpioWakeSel&
BIT(GPIO_IRQDSEL)) &&
379 ((WAKE_SEL&GPIO_WAKE) ||
380 (WAKE_SEL&WAKE_IRQD))) {
383 halInternalWakeEvent |=
BIT(GPIO_IRQDSEL);
389 if((INT_SLEEPTMR&INT_SLEEPTMRCMPA) && (WAKE_SEL&WAKE_SLEEPTMRCMPA)) {
392 halInternalWakeEvent |=
BIT32(CMPA_INTERNAL_WAKE_EVENT_BIT);
398 if((INT_SLEEPTMR&INT_SLEEPTMRCMPB) && (WAKE_SEL&WAKE_SLEEPTMRCMPB)) {
401 halInternalWakeEvent |=
BIT32(CMPB_INTERNAL_WAKE_EVENT_BIT);
407 if((INT_SLEEPTMR&INT_SLEEPTMRWRAP) && (WAKE_SEL&WAKE_SLEEPTMRWRAP)) {
410 halInternalWakeEvent |=
BIT32(WRAP_INTERNAL_WAKE_EVENT_BIT);
416 if((INT_PENDSET&INT_DEBUG) && (WAKE_SEL&WAKE_WAKE_CORE)) {
419 halInternalWakeEvent |=
BIT32(WAKE_CORE_INTERNAL_WAKE_EVENT_BIT);
428 int32u wakeSourceInterruptMask = 0;
430 if(GPIO_PBWAKE&PB0) {
431 wakeSourceInterruptMask |= INT_IRQA;
436 if(GPIO_PBWAKE&PB6) {
437 wakeSourceInterruptMask |= INT_IRQB;
442 if(gpioWakeSel&
BIT(GPIO_IRQCSEL)) {
443 wakeSourceInterruptMask |= INT_IRQC;
448 if(gpioWakeSel&
BIT(GPIO_IRQDSEL)) {
449 wakeSourceInterruptMask |= INT_IRQD;
454 if( (WAKE_SEL&WAKE_SLEEPTMRCMPA) ||
455 (WAKE_SEL&WAKE_SLEEPTMRCMPB) ||
456 (WAKE_SEL&WAKE_SLEEPTMRWRAP) ) {
457 wakeSourceInterruptMask |= INT_SLEEPTMR;
462 if(WAKE_SEL&WAKE_WAKE_CORE) {
463 wakeSourceInterruptMask |= INT_DEBUG;
469 INT_CFGSET_SAVED &= wakeSourceInterruptMask;
512 CSYSPWRUPACK_INHIBIT = CSYSPWRUPACK_INHIBIT_CSYSPWRUPACK_INHIBIT;
516 int32u wakeSel = WAKE_SEL;
518 while( (CSYSPWRUPACK_STATUS) && (!(PWRUP_EVENT&wakeSel)) ) {}
520 if(PWRUP_EVENT&wakeSel) {
521 CSYSPWRUPACK_INHIBIT = CSYSPWRUPACK_INHIBIT_RESET;
541 OSC24M_CTRL &= ~OSC24M_CTRL_OSC24M_SEL;
545 SCS_SCR |= SCS_SCR_SLEEPDEEP;
546 extern volatile boolean halPendSvSaveContext;
547 halPendSvSaveContext = 1;
551 SCS_ICSR |= SCS_ICSR_PENDSVSET;
556 while(halPendSvSaveContext) {}
560 halInternalWakeEvent |=
BIT32(SLEEPSKIPPED_INTERNAL_WAKE_EVENT_BIT);
585 INT_SLEEPTMRFLAG = (INT_SLEEPTMRCMPA |
588 INT_GPIOFLAG = (INT_IRQAFLAG |
604 SLEEPTMR_CLKEN_REG = SLEEPTMR_CLKEN_SAVED;
605 INT_SLEEPTMRCFG_REG = INT_SLEEPTMRCFG_SAVED;
606 INT_MGMTCFG_REG = INT_MGMTCFG_SAVED;
607 GPIO_INTCFGA_REG = GPIO_INTCFGA_SAVED;
608 GPIO_INTCFGB_REG = GPIO_INTCFGB_SAVED;
609 GPIO_INTCFGC_REG = GPIO_INTCFGC_SAVED;
610 GPIO_INTCFGD_REG = GPIO_INTCFGD_SAVED;
611 OSC24M_BIASTRIM_REG = OSC24M_BIASTRIM_SAVED;
612 OSCHF_TUNE_REG = OSCHF_TUNE_SAVED;
613 DITHER_DIS_REG = DITHER_DIS_SAVED;
614 PCTRACE_SEL_REG = PCTRACE_SEL_SAVED;
615 MEM_PROT_0_REG = MEM_PROT_0_SAVED;
616 MEM_PROT_1_REG = MEM_PROT_1_SAVED;
617 MEM_PROT_2_REG = MEM_PROT_2_SAVED;
618 MEM_PROT_3_REG = MEM_PROT_3_SAVED;
619 MEM_PROT_4_REG = MEM_PROT_4_SAVED;
620 MEM_PROT_5_REG = MEM_PROT_5_SAVED;
621 MEM_PROT_6_REG = MEM_PROT_6_SAVED;
622 MEM_PROT_7_REG = MEM_PROT_7_SAVED;
623 MEM_PROT_EN_REG = MEM_PROT_EN_SAVED;
624 INT_CFGSET_REG = INT_CFGSET_SAVED;
625 SCS_VTOR_REG = SCS_VTOR_SAVED;
628 INT_PENDCLR = 0xFFFFFFFF;
633 int32u GPIO_IN_NEW = GPIO_PAIN;
634 GPIO_IN_NEW |= (GPIO_PBIN<<8);
635 GPIO_IN_NEW |= (GPIO_PCIN<<16);
640 int32u powerUpEvents = PWRUP_EVENT;
641 powerUpEvents &= WAKE_SEL;
642 halInternalWakeEvent |= ((GPIO_IN_SAVED^GPIO_IN_NEW)&gpioWakeSel);
644 halInternalWakeEvent |= (!!(powerUpEvents&PWRUP_SC1))<<((1*8)+2);
646 halInternalWakeEvent |= (!!(powerUpEvents&PWRUP_SC2))<<((0*8)+2);
648 halInternalWakeEvent |= (!!(powerUpEvents&PWRUP_IRQD))<<(GPIO_IRQDSEL);
649 halInternalWakeEvent |= ((powerUpEvents &
650 (PWRUP_CSYSPWRUPREQ_MASK |
651 PWRUP_CDBGPWRUPREQ_MASK |
652 PWRUP_WAKECORE_MASK |
653 PWRUP_SLEEPTMRWRAP_MASK |
654 PWRUP_SLEEPTMRCOMPB_MASK |
655 PWRUP_SLEEPTMRCOMPA_MASK ))
656 <<INTERNAL_WAKE_EVENT_BIT_SHIFT);
685 EVENT_CTRL = EVENT_CTRL_RESET;
690 if(restoreWatchdog) {
707 if( (INT_CFGSET&INT_DEBUG) &&
708 (halInternalWakeEvent&
BIT(WAKE_CORE_INTERNAL_WAKE_EVENT_BIT)) ) {
709 WAKE_CORE = WAKE_CORE_FIELD;
719 if( (INT_SLEEPTMRCFG&INT_SLEEPTMRCMPA) &&
720 (halInternalWakeEvent&
BIT(CMPA_INTERNAL_WAKE_EVENT_BIT)) ) {
721 INT_SLEEPTMRFORCE = INT_SLEEPTMRCMPA;
730 if( (INT_SLEEPTMRCFG&INT_SLEEPTMRCMPB) &&
731 (halInternalWakeEvent&
BIT(CMPB_INTERNAL_WAKE_EVENT_BIT)) ) {
732 INT_SLEEPTMRFORCE = INT_SLEEPTMRCMPB;
741 if( (INT_SLEEPTMRCFG&INT_SLEEPTMRWRAP) &&
742 (halInternalWakeEvent&
BIT(WRAP_INTERNAL_WAKE_EVENT_BIT)) ) {
743 INT_SLEEPTMRFORCE = INT_SLEEPTMRWRAP;
755 if( ((GPIO_INTCFGA&GPIO_INTMOD)!=0) &&
757 INT_PENDSET = INT_IRQA;
764 if( ((GPIO_INTCFGB&GPIO_INTMOD)!=0) &&
766 INT_PENDSET = INT_IRQB;
773 if( ((GPIO_INTCFGC&GPIO_INTMOD)!=0) &&
774 (halInternalWakeEvent&
BIT(GPIO_IRQCSEL)) ) {
775 INT_PENDSET = INT_IRQC;
782 if( ((GPIO_INTCFGD&GPIO_INTMOD)!=0) &&
783 (halInternalWakeEvent&
BIT(GPIO_IRQDSEL)) ) {
784 INT_PENDSET = INT_IRQD;
797 halInternalWakeEvent |=
BIT32(WAKEINFOVALID_INTERNAL_WAKE_EVENT_BIT);
851 GPIO_PAWAKE = (gpioWakeBitMask>>0)&0xFF;
852 GPIO_PBWAKE = (gpioWakeBitMask>>8)&0xFF;
853 GPIO_PCWAKE = (gpioWakeBitMask>>16)&0xFF;
860 if((GPIO_PAWAKE)||(GPIO_PBWAKE)||(GPIO_PCWAKE)) {
861 WAKE_SEL |= GPIO_WAKE;
864 WAKE_SEL |= WAKE_CDBGPWRUPREQ;
866 WAKE_SEL |= WAKE_CSYSPWRUPREQ;
868 WAKE_SEL |= WAKE_WAKE_CORE;