|
1 | 1 | ## DeepSleepLock
|
2 | 2 |
|
3 |
| -There is only one sleep function in Mbed OS 5.6: |
| 3 | +The `DeepSleepLock` class provides an RAII object for disabling sleep. In other words, creating a DeepSleepLock object calls its constructor, which increments the deep sleep prevention lock. The DeepSleepLock object automatically releases the deep sleep prevention lock in its destructor when the object goes out of scope. Another way to look at this is when the DeepSleepLock object exists, it prevents deep sleep. |
4 | 4 |
|
5 |
| -```c++ |
6 |
| -void sleep(); |
7 |
| -``` |
8 |
| - |
9 |
| -This function invokes sleep manager, which we introduce below. |
10 |
| - |
11 |
| -The idle loop invokes sleep manager by default. You can overwrite this default behavior by attaching a different idle hook function pointer. |
12 |
| - |
13 |
| -```c++ |
14 |
| -void new_idle_loop() |
15 |
| -{ |
16 |
| - // do nothing |
17 |
| -} |
18 |
| - |
19 |
| -void main() |
20 |
| -{ |
21 |
| - rtos_attach_idle_hook(&new_idle_loop); |
22 |
| -} |
23 |
| -``` |
24 |
| - |
25 |
| -### Sleep modes |
26 |
| - |
27 |
| -There are two available sleep modes: |
28 |
| - |
29 |
| -1. Sleep mode |
30 |
| - |
31 |
| - The system clock to the core stops until a reset or an interrupt occurs. This eliminates dynamic power that the processor, memory systems and buses use. This mode maintains the processor, peripheral and memory state, and the peripherals continue to work and can generate interrupts. |
32 |
| - |
33 |
| - You can wake up the processor by any internal peripheral interrupt or external pin interrupt. |
34 |
| - |
35 |
| -2. Deep sleep mode |
36 |
| - |
37 |
| - This mode is similar to sleep but saves more power and has a longer wakeup time. It saves power by turning off the high-speed clocks. Because of this, you can only enter this mode when peripherals relying on high-speed clocks are not in use. Peripherals that do not rely on high-speed clocks include the lp ticker, RTC and external interrupt on a pin. This mode maintains all state. |
38 |
| - |
39 |
| -### Sleep manager |
40 |
| - |
41 |
| -The sleep manager provides an API to control sleep modes. Deep sleep might introduce some power savings that can affect an application, for instance high speed clock dependent drivers. |
42 |
| - |
43 |
| -The `DeepSleepLock` class provides an RAII object for disabling sleep, or explicit lock/unlock methods. To prevent an application from entering deep sleep, invoke the `DeepSleepLock()::lock()` method. As soon as an application is ready for the deep sleep, allow it by invoking the `DeepSleepLock::unlock()` method. |
44 |
| - |
45 |
| -These Mbed OS drivers contain locking deep sleep: |
46 |
| - |
47 |
| -- `Ticker`. |
48 |
| -- `Timeout`. |
49 |
| -- `Timer`. |
50 |
| -- `SPI`. |
51 |
| -- `I2C`. |
52 |
| -- `CAN`. |
53 |
| -- `SerialBase`. |
54 |
| - |
55 |
| -### SleepManager class reference |
56 |
| - |
57 |
| -[https://github.com/ARMmbed/mbed-os/blob/master/platform/mbed_sleep.h](https://github.com/ARMmbed/mbed-os/blob/master/platform/mbed_sleep.h) |
| 5 | +<span class="notes">**Note:** Drivers that don't work in deep sleep mode automatically prevent deep sleep mode, so DeepSleepLock does not need to protect them.</span> |
58 | 6 |
|
59 | 7 | ### DeepSleepLock class reference
|
60 | 8 |
|
61 | 9 | [](https://os.mbed.com/docs/v5.6/mbed-os-api-doxy/classmbed_1_1_deep_sleep_lock.html)
|
62 | 10 |
|
63 | 11 | ### Example
|
64 | 12 |
|
65 |
| -This example shows SPI asynchronous transfer with deep sleep locking. |
66 |
| - |
67 |
| -```c++ |
68 |
| - |
69 |
| -SPI::transfer(const Type *tx_buffer, int tx_length, Type *rx_buffer, int rx_length, const event_callback_t& callback, int event = SPI_EVENT_COMPLETE) |
70 |
| -{ |
71 |
| - if (spi_active(&_spi)) { |
72 |
| - return queue_transfer(tx_buffer, tx_length, rx_buffer, rx_length, sizeof(Type)*8, callback, event); |
73 |
| - } |
74 |
| - // This driver requires high speed clock, needs to wait for complete flag set via a callback to unblock the deep sleep |
75 |
| - sleep_manager_lock_deep_sleep(); |
76 |
| - start_transfer(tx_buffer, tx_length, rx_buffer, rx_length, sizeof(Type)*8, callback, event); |
77 |
| - return 0; |
78 |
| -} |
79 |
| - |
| 13 | +This example shows how you can lock deep sleep to decrease interrupt latency at the expense of increased power consumption. |
80 | 14 |
|
81 |
| -void SPI::irq_handler_asynch(void) |
82 |
| -{ |
83 |
| - int event = spi_irq_handler_asynch(&_spi); |
84 |
| - if (_callback && (event & SPI_EVENT_ALL)) { |
85 |
| - _callback.call(event & SPI_EVENT_ALL); |
86 |
| - } |
87 |
| - if (event & (SPI_EVENT_ALL | SPI_EVENT_INTERNAL_TRANSFER_COMPLETE)) { |
88 |
| - // all data should be written now, unlock the deep sleep |
89 |
| - sleep_manager_unlock_deep_sleep(); |
90 |
| -#if TRANSACTION_QUEUE_SIZE_SPI |
91 |
| - // SPI peripheral is free (event happend), dequeue transaction |
92 |
| - dequeue_transaction(); |
93 |
| -#endif |
94 |
| - } |
95 |
| -} |
96 |
| -``` |
| 15 | +[](https://os.mbed.com/teams/mbed_example/code/DeepSleepLock_Example_1/file/66aac0656e71/main.cpp) |
0 commit comments