1 | /* |
---|
2 | * Copyright (c) 2016 embedded brains GmbH. All rights reserved. |
---|
3 | * |
---|
4 | * embedded brains GmbH |
---|
5 | * Dornierstr. 4 |
---|
6 | * 82178 Puchheim |
---|
7 | * Germany |
---|
8 | * <rtems@embedded-brains.de> |
---|
9 | * |
---|
10 | * The license and distribution terms for this file may be |
---|
11 | * found in the file LICENSE in this distribution or at |
---|
12 | * http://www.rtems.org/license/LICENSE. |
---|
13 | */ |
---|
14 | |
---|
15 | #include <bsp.h> |
---|
16 | #include <bsp/power.h> |
---|
17 | #include <bsp/irq.h> |
---|
18 | |
---|
19 | #include <libchip/chip.h> |
---|
20 | |
---|
21 | /* SCR Sleep deep bit */ |
---|
22 | #define SCR_SLEEPDEEP (1u << 2) |
---|
23 | |
---|
24 | void atsam_power_change_state( |
---|
25 | const atsam_power_control *controls, |
---|
26 | size_t n, |
---|
27 | atsam_power_state state |
---|
28 | ) |
---|
29 | { |
---|
30 | size_t i; |
---|
31 | |
---|
32 | switch (state) { |
---|
33 | case ATSAM_POWER_ON: |
---|
34 | for (i = n; i > 0; --i) { |
---|
35 | const atsam_power_control *c; |
---|
36 | |
---|
37 | c = &controls[i - 1]; |
---|
38 | (*c->handler)(c, state); |
---|
39 | } |
---|
40 | |
---|
41 | break; |
---|
42 | case ATSAM_POWER_INIT: |
---|
43 | case ATSAM_POWER_OFF: |
---|
44 | for (i = 0; i < n; ++i) { |
---|
45 | const atsam_power_control *c; |
---|
46 | |
---|
47 | c = &controls[i]; |
---|
48 | (*c->handler)(c, state); |
---|
49 | } |
---|
50 | |
---|
51 | break; |
---|
52 | default: |
---|
53 | break; |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | void atsam_power_handler_peripheral( |
---|
58 | const atsam_power_control *control, |
---|
59 | atsam_power_state state |
---|
60 | ) |
---|
61 | { |
---|
62 | uint32_t id; |
---|
63 | uint32_t end; |
---|
64 | |
---|
65 | id = control->data.peripherals.first; |
---|
66 | end = control->data.peripherals.last + 1; |
---|
67 | |
---|
68 | switch (state) { |
---|
69 | case ATSAM_POWER_ON: |
---|
70 | while (id != end) { |
---|
71 | PMC_EnablePeripheral(id); |
---|
72 | ++id; |
---|
73 | } |
---|
74 | break; |
---|
75 | case ATSAM_POWER_OFF: |
---|
76 | while (id != end) { |
---|
77 | PMC_DisablePeripheral(id); |
---|
78 | ++id; |
---|
79 | } |
---|
80 | break; |
---|
81 | default: |
---|
82 | break; |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | void atsam_power_handler_sleep_mode(const atsam_power_control *control, atsam_power_state state) |
---|
87 | { |
---|
88 | (void) control; |
---|
89 | |
---|
90 | switch (state) { |
---|
91 | case ATSAM_POWER_OFF: |
---|
92 | /* Enable Low Power Mode in the Fast Startup Mode Register */ |
---|
93 | PMC->PMC_FSMR &= (uint32_t)~PMC_FSMR_LPM; |
---|
94 | /* Do not set deep sleep, but "normal" sleep */ |
---|
95 | SCB->SCR &= (uint32_t)~SCR_SLEEPDEEP; |
---|
96 | |
---|
97 | __asm__ volatile ("wfi"); |
---|
98 | break; |
---|
99 | default: |
---|
100 | break; |
---|
101 | } |
---|
102 | } |
---|