Skip to content

Commit 3a4f591

Browse files
authored
Merge pull request #10124 from jamesbeyond/fm_sleep
Enable low-power ticker and Sleep for FastModels
2 parents 801e555 + 916ec21 commit 3a4f591

File tree

5 files changed

+219
-29
lines changed

5 files changed

+219
-29
lines changed

TESTS/mbed_drivers/lp_timeout/main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,10 @@ Case cases[] = {
7474
Case("1 s delay during deepsleep (attach_us)", test_deepsleep<AttachUSTester<LowPowerTimeout>, 1000000, LONG_DELTA_US>,
7575
greentea_failure_handler),
7676
#endif
77-
77+
#if !defined(__ARM_FM) //FastModels not support time drifting test
7878
Case("Timing drift (attach)", test_drift<AttachTester<LowPowerTimeout> >),
7979
Case("Timing drift (attach_us)", test_drift<AttachUSTester<LowPowerTimeout> >),
80+
#endif
8081
};
8182

8283
utest::v1::status_t greentea_test_setup(const size_t number_of_cases)
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2006-2019 ARM Limited
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
#include <stddef.h>
18+
#include "lp_ticker_api.h"
19+
#include "PeripheralNames.h"
20+
21+
#define LP_TICKER_INTERRUPT CMSDK_TIMER0
22+
#define LP_TICKER_COUNTER CMSDK_TIMER1
23+
#define LP_TICKER_TIMER_IRQn TIMER0_IRQn
24+
25+
26+
/* mbed OS HAL API defined lp_ticker as an increment ticker
27+
* MPS2 platform provided in SSE-200 are decrement tickers
28+
* with interrupt fired counter reaches 0.
29+
*
30+
* So 2 Timers are used to construct mbed OS HAL low power ticker.
31+
*
32+
* TIMER0 is for generating interrupts
33+
* and TIMER0 will turned off when it is generating interrupts
34+
*
35+
* TIMER1 is for counting, and returns inverted binary when read from it
36+
* Because TIMER1 is running at the speed of 25Mhz, it need to be shift by 10,
37+
* in order to meet mbed HAL lp_ticker definitions
38+
*
39+
*/
40+
41+
static int lp_ticker_inited = 0;
42+
43+
void lp_ticker_internal_handler(void)
44+
{
45+
LP_TICKER_INTERRUPT->CTRL &= ~CMSDK_TIMER_CTRL_IRQEN_Msk;
46+
LP_TICKER_INTERRUPT->CTRL &= ~CMSDK_TIMER_CTRL_EN_Msk;
47+
lp_ticker_irq_handler();
48+
}
49+
50+
51+
void lp_ticker_init(void)
52+
{
53+
if (lp_ticker_inited) {
54+
lp_ticker_disable_interrupt();
55+
return;
56+
}
57+
58+
LP_TICKER_COUNTER->CTRL = 0x0ul;
59+
LP_TICKER_INTERRUPT->CTRL = 0x0ul;
60+
61+
LP_TICKER_COUNTER->RELOAD = 0xFFFFFFFFul;
62+
LP_TICKER_INTERRUPT->RELOAD = 0xFFFFFFFFul;
63+
64+
LP_TICKER_COUNTER->CTRL |= CMSDK_TIMER_CTRL_EN_Msk;
65+
66+
NVIC_SetVector(LP_TICKER_TIMER_IRQn, (uint32_t)lp_ticker_internal_handler);
67+
lp_ticker_inited = 1;
68+
}
69+
70+
void lp_ticker_free(void)
71+
{
72+
LP_TICKER_COUNTER->CTRL &= ~CMSDK_TIMER_CTRL_EN_Msk;
73+
lp_ticker_disable_interrupt();
74+
lp_ticker_inited = 0;
75+
}
76+
77+
uint32_t lp_ticker_read()
78+
{
79+
return (~LP_TICKER_COUNTER->VALUE) >> 10;
80+
}
81+
82+
void lp_ticker_set_interrupt(timestamp_t timestamp)
83+
{
84+
uint32_t delta = (timestamp << 10) - (~LP_TICKER_COUNTER->VALUE);
85+
86+
LP_TICKER_INTERRUPT->CTRL &= ~CMSDK_TIMER_CTRL_EN_Msk;
87+
LP_TICKER_INTERRUPT->RELOAD = delta;
88+
LP_TICKER_INTERRUPT->CTRL |= CMSDK_TIMER_CTRL_IRQEN_Msk;
89+
LP_TICKER_INTERRUPT->CTRL |= CMSDK_TIMER_CTRL_EN_Msk;
90+
91+
NVIC_EnableIRQ(LP_TICKER_TIMER_IRQn);
92+
}
93+
94+
void lp_ticker_fire_interrupt(void)
95+
{
96+
NVIC_EnableIRQ(LP_TICKER_TIMER_IRQn);
97+
NVIC_SetPendingIRQ(LP_TICKER_TIMER_IRQn);
98+
}
99+
100+
void lp_ticker_disable_interrupt(void)
101+
{
102+
LP_TICKER_INTERRUPT->CTRL &= ~CMSDK_TIMER_CTRL_IRQEN_Msk;
103+
LP_TICKER_INTERRUPT->CTRL &= ~CMSDK_TIMER_CTRL_EN_Msk;
104+
NVIC_DisableIRQ(LP_TICKER_TIMER_IRQn);
105+
}
106+
107+
void lp_ticker_clear_interrupt(void)
108+
{
109+
LP_TICKER_INTERRUPT->INTCLEAR = CMSDK_TIMER_INTCLEAR_Msk;
110+
}
111+
112+
const ticker_info_t *lp_ticker_get_info(void)
113+
{
114+
static const ticker_info_t info = {
115+
24414, // 10 stages scaled from 25MHz (dived by 1024)
116+
22 // 22 bit counter
117+
};
118+
return &info;
119+
}
120+
121+
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
2+
/** \addtogroup hal */
3+
/** @{*/
4+
/* mbed Microcontroller Library
5+
* Copyright (c) 2018-2019 Arm Limited
6+
* SPDX-License-Identifier: Apache-2.0
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
22+
#include "sleep_api.h"
23+
#include "us_ticker_api.h"
24+
25+
#include "PeripheralNames.h"
26+
#define US_TICKER_COUNTER CMSDK_DUALTIMER1
27+
#define US_TICKER_INTERRUPT CMSDK_DUALTIMER2
28+
#define US_TICKER_TIMER_IRQn DUALTIMER_IRQn
29+
30+
31+
#if DEVICE_SLEEP
32+
33+
void hal_sleep(void)
34+
{
35+
__WFI();
36+
}
37+
38+
/* Since there is no power management function implemented in MPS2,
39+
* Also Deep Sleep mode mean to save power which is not practical on a simulator.
40+
* So mbed HAL Deep sleep is mocked by Sleep,
41+
* representing a "Waiting For Interrupt" state,
42+
* but disabling the Microsec ticker in addition */
43+
44+
void hal_deepsleep(void)
45+
{
46+
#if DEVICE_USTICKER
47+
uint32_t val = US_TICKER_COUNTER->TimerValue;
48+
US_TICKER_COUNTER->TimerControl &= ~CMSDK_DUALTIMER1_CTRL_EN_Msk;
49+
US_TICKER_INTERRUPT->TimerControl &= ~CMSDK_DUALTIMER2_CTRL_EN_Msk;
50+
US_TICKER_INTERRUPT->TimerControl &= ~CMSDK_DUALTIMER2_CTRL_INTEN_Msk;
51+
#endif
52+
53+
__WFI();
54+
55+
#if DEVICE_USTICKER
56+
US_TICKER_COUNTER->TimerLoad = val;
57+
US_TICKER_COUNTER->TimerControl |= CMSDK_DUALTIMER1_CTRL_EN_Msk;
58+
US_TICKER_INTERRUPT->TimerControl |= CMSDK_DUALTIMER1_CTRL_INTEN_Msk;
59+
US_TICKER_INTERRUPT->TimerControl |= CMSDK_DUALTIMER2_CTRL_EN_Msk;
60+
#endif
61+
}
62+
63+
#endif
64+
65+
/**@}*/

targets/TARGET_ARM_FM/TARGET_FVP_MPS2/us_ticker.c

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* mbed Microcontroller Library
2-
* Copyright (c) 2006-2018 ARM Limited
2+
* Copyright (c) 2006-2019 ARM Limited
3+
* SPDX-License-Identifier: Apache-2.0
34
*
45
* Licensed under the Apache License, Version 2.0 (the "License");
56
* you may not use this file except in compliance with the License.
@@ -16,24 +17,24 @@
1617
#include <stddef.h>
1718
#include "us_ticker_api.h"
1819
#include "PeripheralNames.h"
19-
#define US_TICKER_TIMER1 CMSDK_DUALTIMER1
20-
#define US_TICKER_TIMER2 CMSDK_DUALTIMER2
20+
#define US_TICKER_COUNTER CMSDK_DUALTIMER1
21+
#define US_TICKER_INTERRUPT CMSDK_DUALTIMER2
2122
#define US_TICKER_TIMER_IRQn DUALTIMER_IRQn
2223

2324
/** mbed OS HAL API defined us_ticker as an increment ticker
2425
* MPS2 platform provided in SSE-200 are decrement tickers
2526
* with interrupt fired counter reaches 0.
2627
*
2728
* So 2 Timers are used to construct mbed OS HAL ticker.
28-
*
29+
*
2930
* TIMER1 is for counting, and returns inverted binary when read from it
3031
* TIMER1 will be kept in free-running mode (default, and not generate interrupts)
31-
*
32+
*
3233
* TIMER2 is for generating interrupts
3334
* So TIMER2 is set to periodic mode, which start decrement counting form LOADVALUE generates interrupts at 0
34-
* and TIMER2 also set into one-shot mode, which counter halts when is reaches 0
35+
* and TIMER2 also set into one-shot mode, which counter halts when is reaches 0
3536
*/
36-
37+
3738
static int us_ticker_inited = 0;
3839

3940
void us_ticker_init(void)
@@ -43,47 +44,47 @@ void us_ticker_init(void)
4344
return;
4445
}
4546

46-
US_TICKER_TIMER1->TimerControl = 0x0ul; // disable TIMER1 and reset all control
47-
US_TICKER_TIMER2->TimerControl = 0x0ul; // disable TIMER2 and reset all control
47+
US_TICKER_COUNTER->TimerControl = 0x0ul; // disable TIMER1 and reset all control
48+
US_TICKER_INTERRUPT->TimerControl = 0x0ul; // disable TIMER2 and reset all control
4849

49-
US_TICKER_TIMER1->TimerLoad = 0xFFFFFFFFul;
50-
US_TICKER_TIMER2->TimerLoad = 0xFFFFFFFFul;
50+
US_TICKER_COUNTER->TimerLoad = 0xFFFFFFFFul;
51+
US_TICKER_INTERRUPT->TimerLoad = 0xFFFFFFFFul;
5152

52-
US_TICKER_TIMER1->TimerControl |= CMSDK_DUALTIMER1_CTRL_SIZE_Msk; // set TIMER1 to 32 bit counter
53-
US_TICKER_TIMER2->TimerControl |= CMSDK_DUALTIMER2_CTRL_SIZE_Msk; // set TIMER2 to 32 bit counter
53+
US_TICKER_COUNTER->TimerControl |= CMSDK_DUALTIMER1_CTRL_SIZE_Msk; // set TIMER1 to 32 bit counter
54+
US_TICKER_INTERRUPT->TimerControl |= CMSDK_DUALTIMER2_CTRL_SIZE_Msk; // set TIMER2 to 32 bit counter
5455

55-
US_TICKER_TIMER1->TimerControl |= 0x1 << CMSDK_DUALTIMER1_CTRL_PRESCALE_Pos; // set TIMER1 with 4 stages prescale
56-
US_TICKER_TIMER2->TimerControl |= 0x1 << CMSDK_DUALTIMER2_CTRL_PRESCALE_Pos; // set TIMER2 with 4 stages prescale
56+
US_TICKER_COUNTER->TimerControl |= 0x1 << CMSDK_DUALTIMER1_CTRL_PRESCALE_Pos; // set TIMER1 with 4 stages prescale
57+
US_TICKER_INTERRUPT->TimerControl |= 0x1 << CMSDK_DUALTIMER2_CTRL_PRESCALE_Pos; // set TIMER2 with 4 stages prescale
5758

58-
US_TICKER_TIMER2->TimerControl |= CMSDK_DUALTIMER2_CTRL_MODE_Msk; // set TIMER2 periodic mode
59-
US_TICKER_TIMER2->TimerControl |= CMSDK_DUALTIMER2_CTRL_ONESHOOT_Msk; // set TIMER2 one-shot mode
59+
US_TICKER_INTERRUPT->TimerControl |= CMSDK_DUALTIMER2_CTRL_MODE_Msk; // set TIMER2 periodic mode
60+
US_TICKER_INTERRUPT->TimerControl |= CMSDK_DUALTIMER2_CTRL_ONESHOOT_Msk; // set TIMER2 one-shot mode
6061

61-
US_TICKER_TIMER1->TimerControl |= CMSDK_DUALTIMER1_CTRL_EN_Msk; // enable TIMER1 counter
62+
US_TICKER_COUNTER->TimerControl |= CMSDK_DUALTIMER1_CTRL_EN_Msk; // enable TIMER1 counter
6263

6364
NVIC_SetVector(US_TICKER_TIMER_IRQn, (uint32_t)us_ticker_irq_handler);
6465
us_ticker_inited = 1;
6566
}
6667

6768
void us_ticker_free(void)
6869
{
69-
US_TICKER_TIMER1->TimerControl &= ~CMSDK_DUALTIMER1_CTRL_EN_Msk; // disable TIMER1
70-
US_TICKER_TIMER2->TimerControl &= ~CMSDK_DUALTIMER2_CTRL_EN_Msk; // disable TIMER2
70+
US_TICKER_COUNTER->TimerControl &= ~CMSDK_DUALTIMER1_CTRL_EN_Msk; // disable TIMER1
71+
US_TICKER_INTERRUPT->TimerControl &= ~CMSDK_DUALTIMER2_CTRL_EN_Msk; // disable TIMER2
7172
us_ticker_disable_interrupt();
7273
us_ticker_inited = 0;
7374
}
7475

7576
uint32_t us_ticker_read()
7677
{
77-
return ~US_TICKER_TIMER1->TimerValue;
78+
return ~US_TICKER_COUNTER->TimerValue;
7879
}
7980

8081
void us_ticker_set_interrupt(timestamp_t timestamp)
8182
{
8283
uint32_t delta = timestamp - us_ticker_read();
83-
US_TICKER_TIMER2->TimerControl &= ~CMSDK_DUALTIMER2_CTRL_EN_Msk; // disable TIMER2
84-
US_TICKER_TIMER2->TimerLoad = delta; // Set TIMER2 load value
85-
US_TICKER_TIMER2->TimerControl |= CMSDK_DUALTIMER2_CTRL_INTEN_Msk; // enable TIMER2 interrupt
86-
US_TICKER_TIMER2->TimerControl |= CMSDK_DUALTIMER2_CTRL_EN_Msk; // enable TIMER2 counter
84+
US_TICKER_INTERRUPT->TimerControl &= ~CMSDK_DUALTIMER2_CTRL_EN_Msk; // disable TIMER2
85+
US_TICKER_INTERRUPT->TimerLoad = delta; // Set TIMER2 load value
86+
US_TICKER_INTERRUPT->TimerControl |= CMSDK_DUALTIMER2_CTRL_INTEN_Msk; // enable TIMER2 interrupt
87+
US_TICKER_INTERRUPT->TimerControl |= CMSDK_DUALTIMER2_CTRL_EN_Msk; // enable TIMER2 counter
8788
NVIC_EnableIRQ(US_TICKER_TIMER_IRQn);
8889
}
8990

@@ -96,14 +97,14 @@ void us_ticker_fire_interrupt(void)
9697

9798
void us_ticker_disable_interrupt(void)
9899
{
99-
US_TICKER_TIMER2->TimerControl &= ~CMSDK_DUALTIMER2_CTRL_INTEN_Msk;
100-
US_TICKER_TIMER2->TimerControl &= ~CMSDK_DUALTIMER2_CTRL_EN_Msk; // disable TIMER2
100+
US_TICKER_INTERRUPT->TimerControl &= ~CMSDK_DUALTIMER2_CTRL_INTEN_Msk;
101+
US_TICKER_INTERRUPT->TimerControl &= ~CMSDK_DUALTIMER2_CTRL_EN_Msk; // disable TIMER2
101102
NVIC_DisableIRQ(US_TICKER_TIMER_IRQn);
102103
}
103104

104105
void us_ticker_clear_interrupt(void)
105106
{
106-
US_TICKER_TIMER2->TimerIntClr = CMSDK_DUALTIMER2_INTCLR_Msk;
107+
US_TICKER_INTERRUPT->TimerIntClr = CMSDK_DUALTIMER2_INTCLR_Msk;
107108
}
108109

109110
const ticker_info_t *us_ticker_get_info(void)

targets/targets.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7958,10 +7958,12 @@
79587958
"FLASH",
79597959
"I2C",
79607960
"INTERRUPTIN",
7961+
"LPTICKER",
79617962
"PORTIN",
79627963
"PORTINOUT",
79637964
"PORTOUT",
79647965
"SERIAL",
7966+
"SLEEP",
79657967
"SPI",
79667968
"SPISLAVE",
79677969
"TSC",

0 commit comments

Comments
 (0)