
This is a sample to make Software Standby using OS.
main.cpp@5:70870890e359, 2019-10-08 (annotated)
- Committer:
- dkato
- Date:
- Tue Oct 08 07:53:20 2019 +0000
- Revision:
- 5:70870890e359
- Parent:
- 4:272e885e36a9
Changed interrupt disable function
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dkato | 0:27ae610e630e | 1 | #include "mbed.h" |
dkato | 0:27ae610e630e | 2 | |
dkato | 0:27ae610e630e | 3 | InterruptIn button(USER_BUTTON0); |
dkato | 0:27ae610e630e | 4 | DigitalOut led1(LED1); |
dkato | 0:27ae610e630e | 5 | Thread * pTestTask = NULL; |
dkato | 0:27ae610e630e | 6 | |
dkato | 3:64e8fa1b8787 | 7 | static void idle_hook(void) { |
dkato | 4:272e885e36a9 | 8 | // Do the following instead of "__WFI()" |
dkato | 4:272e885e36a9 | 9 | __SEV(); |
dkato | 4:272e885e36a9 | 10 | __WFE(); |
dkato | 4:272e885e36a9 | 11 | __WFE(); |
dkato | 3:64e8fa1b8787 | 12 | } |
dkato | 3:64e8fa1b8787 | 13 | |
dkato | 3:64e8fa1b8787 | 14 | void SoftwareStandby(void) { |
dkato | 3:64e8fa1b8787 | 15 | volatile uint32_t dummy_32; |
dkato | 3:64e8fa1b8787 | 16 | volatile uint8_t dummy_8; |
dkato | 3:64e8fa1b8787 | 17 | |
dkato | 5:70870890e359 | 18 | core_util_critical_section_enter(); |
dkato | 3:64e8fa1b8787 | 19 | |
dkato | 3:64e8fa1b8787 | 20 | // Set the standby_mode_en bit of the power control register in the PL310 to 1. |
dkato | 3:64e8fa1b8787 | 21 | L2C.REG15_POWER_CTRL = 0x00000001uL; |
dkato | 3:64e8fa1b8787 | 22 | dummy_32 = L2C.REG15_POWER_CTRL; |
dkato | 3:64e8fa1b8787 | 23 | |
dkato | 3:64e8fa1b8787 | 24 | // Clear the TME bit in the watchdog timer control/status register (WTCSR) of the watchdog timer to 0 to stop the watchdog timer. |
dkato | 3:64e8fa1b8787 | 25 | WDT.WTCSR = 0xA518; |
dkato | 3:64e8fa1b8787 | 26 | WDT.WTCNT = 0x5A00; |
dkato | 3:64e8fa1b8787 | 27 | WDT.WTCSR = 0xA51D; // CKS[2:0] = b'101 : 1/1024 x P0 (7.8ms) |
dkato | 3:64e8fa1b8787 | 28 | |
dkato | 3:64e8fa1b8787 | 29 | CPG.STBCR1 = 0x80; |
dkato | 3:64e8fa1b8787 | 30 | dummy_8 = CPG.STBCR1; |
dkato | 3:64e8fa1b8787 | 31 | |
dkato | 3:64e8fa1b8787 | 32 | __WFI(); |
dkato | 3:64e8fa1b8787 | 33 | |
dkato | 5:70870890e359 | 34 | core_util_critical_section_exit(); |
dkato | 3:64e8fa1b8787 | 35 | } |
dkato | 3:64e8fa1b8787 | 36 | |
dkato | 0:27ae610e630e | 37 | static void interrupt_button(void) { |
dkato | 0:27ae610e630e | 38 | #if(0) // It may or may not be present. |
dkato | 0:27ae610e630e | 39 | if (pTestTask != NULL) { |
dkato | 0:27ae610e630e | 40 | pTestTask->signal_set(1); |
dkato | 0:27ae610e630e | 41 | } |
dkato | 0:27ae610e630e | 42 | #endif |
dkato | 0:27ae610e630e | 43 | } |
dkato | 0:27ae610e630e | 44 | |
dkato | 0:27ae610e630e | 45 | void test_task(void) { |
dkato | 0:27ae610e630e | 46 | int test_cnt = 0; |
dkato | 0:27ae610e630e | 47 | |
dkato | 0:27ae610e630e | 48 | button.fall(&interrupt_button); |
dkato | 0:27ae610e630e | 49 | button.rise(&interrupt_button); |
dkato | 0:27ae610e630e | 50 | |
dkato | 0:27ae610e630e | 51 | while (true) { |
dkato | 3:64e8fa1b8787 | 52 | SoftwareStandby(); |
dkato | 0:27ae610e630e | 53 | #if(0) // It may or may not be present. |
dkato | 0:27ae610e630e | 54 | // It becomes sleep during idle task. |
dkato | 0:27ae610e630e | 55 | Thread::signal_wait(1); |
dkato | 0:27ae610e630e | 56 | #endif |
dkato | 0:27ae610e630e | 57 | led1 = !led1; |
dkato | 2:c0f92fd0e8e9 | 58 | printf("%d\r\n", test_cnt++); |
dkato | 2:c0f92fd0e8e9 | 59 | ThisThread::sleep_for(5); // Wait for output completion of printf(). |
dkato | 0:27ae610e630e | 60 | } |
dkato | 0:27ae610e630e | 61 | } |
dkato | 0:27ae610e630e | 62 | |
dkato | 0:27ae610e630e | 63 | int main() { |
dkato | 4:272e885e36a9 | 64 | Thread::attach_idle_hook(idle_hook); |
dkato | 0:27ae610e630e | 65 | pTestTask = new Thread(); |
dkato | 0:27ae610e630e | 66 | pTestTask->start(test_task); |
dkato | 0:27ae610e630e | 67 | } |