Merge pull request #2476 from bulislaw/lp_ticker

K22F/K64F: Add lp_ticker implementation and HAL lp_ticker tests
pull/2550/head
Sam Grove 2016-08-25 09:19:18 -05:00 committed by GitHub
commit 3dac791b4a
6 changed files with 485 additions and 3 deletions

View File

@ -0,0 +1,132 @@
/* mbed Microcontroller Library
* Copyright (c) 2016 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#if !DEVICE_LOWPOWERTIMER
#error [NOT_SUPPORTED] Low power timer not supported for this target
#endif
#include "utest/utest.h"
#include "unity/unity.h"
#include "greentea-client/test_env.h"
#include "mbed.h"
#include "us_ticker_api.h"
using namespace utest::v1;
volatile static bool complete;
static LowPowerTimeout lpt;
/* Timeouts are quite arbitrary due to large number of boards with varying level of accuracy */
#define LONG_TIMEOUT (100000)
#define SHORT_TIMEOUT (600)
void cb_done() {
complete = true;
}
#if DEVICE_SLEEP
void lp_timeout_1s_deepsleep(void)
{
complete = false;
timestamp_t start = us_ticker_read();
lpt.attach(&cb_done, 1);
deepsleep();
while (!complete);
timestamp_t end = us_ticker_read();
/* It takes longer to wake up from deep sleep */
TEST_ASSERT_UINT32_WITHIN(LONG_TIMEOUT, 1000000, end - start);
TEST_ASSERT_TRUE(complete);
}
void lp_timeout_1s_sleep(void)
{
complete = false;
timestamp_t start = us_ticker_read();
lpt.attach(&cb_done, 1);
sleep();
while (!complete);
timestamp_t end = us_ticker_read();
TEST_ASSERT_UINT32_WITHIN(LONG_TIMEOUT, 1000000, end - start);
TEST_ASSERT_TRUE(complete);
}
#endif /* DEVICE_SLEEP */
void lp_timeout_us(uint32_t delay_us, uint32_t tolerance)
{
complete = false;
timestamp_t start = us_ticker_read();
lpt.attach_us(&cb_done, delay_us);
while (!complete);
timestamp_t end = us_ticker_read();
/* Using RTC which is less accurate */
TEST_ASSERT_UINT32_WITHIN(tolerance, delay_us, end - start);
TEST_ASSERT_TRUE(complete);
}
void lp_timeout_5s(void)
{
lp_timeout_us(5000000, LONG_TIMEOUT);
}
void lp_timeout_1s(void)
{
lp_timeout_us(1000000, LONG_TIMEOUT);
}
void lp_timeout_1ms(void)
{
lp_timeout_us(1000, SHORT_TIMEOUT);
}
void lp_timeout_500us(void)
{
lp_timeout_us(500, SHORT_TIMEOUT);
}
status_t greentea_failure_handler(const Case *const source, const failure_t reason) {
greentea_case_failure_abort_handler(source, reason);
return STATUS_CONTINUE;
}
Case cases[] = {
Case("500us LowPowerTimeout", lp_timeout_500us, greentea_failure_handler),
Case("1ms LowPowerTimeout", lp_timeout_1ms, greentea_failure_handler),
Case("1sec LowPowerTimeout", lp_timeout_1s, greentea_failure_handler),
Case("5sec LowPowerTimeout", lp_timeout_5s, greentea_failure_handler),
#if DEVICE_SLEEP
Case("1sec LowPowerTimeout from sleep", lp_timeout_1s_sleep, greentea_failure_handler),
Case("1sec LowPowerTimeout from deepsleep", lp_timeout_1s_deepsleep, greentea_failure_handler),
#endif /* DEVICE_SLEEP */
};
status_t greentea_test_setup(const size_t number_of_cases) {
GREENTEA_SETUP(20, "default_auto");
return greentea_test_setup_handler(number_of_cases);
}
Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
int main() {
Harness::run(specification);
}

View File

@ -0,0 +1,148 @@
/* mbed Microcontroller Library
* Copyright (c) 2016 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#if !DEVICE_LOWPOWERTIMER
#error [NOT_SUPPORTED] Low power timer not supported for this target
#endif
#include "utest/utest.h"
#include "unity/unity.h"
#include "greentea-client/test_env.h"
#include "mbed.h"
#include "us_ticker_api.h"
#include "lp_ticker_api.h"
using namespace utest::v1;
volatile static bool complete;
static ticker_event_t delay_event;
static const ticker_data_t *lp_ticker_data = get_lp_ticker_data();
/* Timeouts are quite arbitrary due to large number of boards with varying level of accuracy */
#define LONG_TIMEOUT (100000)
#define SHORT_TIMEOUT (600)
void cb_done(uint32_t id) {
complete = true;
}
void lp_ticker_delay_us(uint32_t delay_us, uint32_t tolerance)
{
complete = false;
uint32_t delay_ts;
ticker_set_handler(lp_ticker_data, cb_done);
ticker_remove_event(lp_ticker_data, &delay_event);
delay_ts = lp_ticker_read() + delay_us;
timestamp_t start = us_ticker_read();
ticker_insert_event(lp_ticker_data, &delay_event, delay_ts, (uint32_t)&delay_event);
while (!complete);
timestamp_t end = us_ticker_read();
TEST_ASSERT_UINT32_WITHIN(tolerance, delay_us, end - start);
TEST_ASSERT_TRUE(complete);
}
#if DEVICE_SLEEP
void lp_ticker_1s_deepsleep()
{
complete = false;
uint32_t delay_ts;
ticker_set_handler(lp_ticker_data, cb_done);
ticker_remove_event(lp_ticker_data, &delay_event);
delay_ts = lp_ticker_read() + 1000000;
timestamp_t start = us_ticker_read();
ticker_insert_event(lp_ticker_data, &delay_event, delay_ts, (uint32_t)&delay_event);
deepsleep();
while (!complete);
timestamp_t end = us_ticker_read();
TEST_ASSERT_UINT32_WITHIN(LONG_TIMEOUT, 1000000, end - start);
TEST_ASSERT_TRUE(complete);
}
void lp_ticker_1s_sleep()
{
complete = false;
uint32_t delay_ts;
ticker_set_handler(lp_ticker_data, cb_done);
ticker_remove_event(lp_ticker_data, &delay_event);
delay_ts = lp_ticker_read() + 1000000;
timestamp_t start = us_ticker_read();
ticker_insert_event(lp_ticker_data, &delay_event, delay_ts, (uint32_t)&delay_event);
sleep();
while (!complete);
timestamp_t end = us_ticker_read();
TEST_ASSERT_UINT32_WITHIN(LONG_TIMEOUT, 1000000, end - start);
TEST_ASSERT_TRUE(complete);
}
#endif /* DEVICE_SLEEP */
void lp_ticker_500us(void)
{
lp_ticker_delay_us(500, SHORT_TIMEOUT);
}
void lp_ticker_1ms(void)
{
lp_ticker_delay_us(1000, SHORT_TIMEOUT);
}
void lp_ticker_1s(void)
{
lp_ticker_delay_us(1000000, LONG_TIMEOUT);
}
void lp_ticker_5s(void)
{
lp_ticker_delay_us(5000000, LONG_TIMEOUT);
}
status_t greentea_failure_handler(const Case *const source, const failure_t reason) {
greentea_case_failure_abort_handler(source, reason);
return STATUS_CONTINUE;
}
Case cases[] = {
Case("500us lp_ticker", lp_ticker_500us, greentea_failure_handler),
Case("1ms lp_ticker", lp_ticker_1ms, greentea_failure_handler),
Case("1s lp_ticker", lp_ticker_1s, greentea_failure_handler),
Case("5s lp_ticker", lp_ticker_5s, greentea_failure_handler),
#if DEVICE_SLEEP
Case("1s lp_ticker sleep", lp_ticker_1s_sleep, greentea_failure_handler),
Case("1s lp_ticker deepsleep", lp_ticker_1s_deepsleep, greentea_failure_handler),
#endif /* DEVICE_SLEEP */
};
status_t greentea_test_setup(const size_t number_of_cases) {
GREENTEA_SETUP(20, "default_auto");
lp_ticker_data->interface->init();
return greentea_test_setup_handler(number_of_cases);
}
Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
int main() {
Harness::run(specification);
}

View File

@ -63,6 +63,7 @@ public:
}
Ticker(const ticker_data_t *data) : TimerEvent(data) {
data->interface->init();
}
/** Attach a function to be called by the Ticker, specifiying the interval in seconds

View File

@ -524,7 +524,7 @@
"inherits": ["Target"],
"progen": {"target": "frdm-k22f"},
"detect_code": ["0231"],
"device_has": ["ANALOGIN", "ANALOGOUT", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
"device_has": ["ANALOGIN", "ANALOGOUT", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
"release_versions": ["2", "5"]
},
"KL27Z": {
@ -565,7 +565,7 @@
"inherits": ["Target"],
"progen": {"target": "frdm-k64f"},
"detect_code": ["0240"],
"device_has": ["ANALOGIN", "ANALOGOUT", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES", "STORAGE"],
"device_has": ["ANALOGIN", "ANALOGOUT", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES", "STORAGE"],
"features": ["IPV4", "STORAGE"],
"release_versions": ["2", "5"]
},
@ -1647,7 +1647,7 @@
"template": ["uvision5_arm_beetle_soc.uvproj.tmpl"]
}
},
"device_has": ["ANALOGIN", "CLCD", "I2C", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "SERIAL", "SPI"],
"device_has": ["ANALOGIN", "CLCD", "I2C", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "SERIAL", "SLEEP", "SPI"],
"features": ["BLE"],
"release_versions": ["2", "5"]
},

View File

@ -0,0 +1,29 @@
/* mbed Microcontroller Library
* Copyright (c) 2016 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "sleep_api.h"
#include "cmsis.h"
void sleep(void)
{
SystemPowerSuspend(POWER_MODE_SLEEP);
SystemPowerResume(POWER_MODE_SLEEP);
}
void deepsleep(void)
{
SystemPowerSuspend(POWER_MODE_DEEP_SLEEP);
SystemPowerResume(POWER_MODE_DEEP_SLEEP);
}

View File

@ -0,0 +1,172 @@
/* mbed Microcontroller Library
* Copyright (c) 2016 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#if DEVICE_LOWPOWERTIMER
#include "lp_ticker_api.h"
#include "fsl_rtc.h"
#include "fsl_lptmr.h"
#include "cmsis.h"
#include "rtc_api.h"
#define MAX_SEC_BITS (12)
#define MAX_SEC_MASK ((1 << MAX_SEC_BITS) - 1)
#define SEC_IN_USEC (1000000)
#define OSC32K_CLK_HZ (32768)
#define MAX_LPTMR_SLEEP ((1 << 16) - 1)
static bool lp_ticker_inited = false;
static int lptmr_schedule = 0;
static void rtc_isr(void)
{
RTC_DisableInterrupts(RTC, kRTC_AlarmInterruptEnable);
RTC->TAR = 0; /* Write clears the IRQ flag */
/* Wait subsecond remainder if any */
if (lptmr_schedule) {
LPTMR_SetTimerPeriod(LPTMR0, lptmr_schedule);
LPTMR_EnableInterrupts(LPTMR0, kLPTMR_TimerInterruptEnable);
LPTMR_StartTimer(LPTMR0);
} else {
lp_ticker_irq_handler();
}
}
static void lptmr_isr(void)
{
LPTMR_ClearStatusFlags(LPTMR0, kLPTMR_TimerCompareFlag);
LPTMR_StopTimer(LPTMR0);
lp_ticker_irq_handler();
}
/** Initialize the low power ticker
*
*/
void lp_ticker_init(void)
{
lptmr_config_t lptmrConfig;
if (lp_ticker_inited) {
return;
}
lp_ticker_inited = true;
/* Setup low resolution clock - RTC */
if (!rtc_isenabled()) {
rtc_init();
RTC_DisableInterrupts(RTC, kRTC_AlarmInterruptEnable | kRTC_SecondsInterruptEnable);
RTC_StartTimer(RTC);
}
NVIC_ClearPendingIRQ(RTC_IRQn);
NVIC_SetVector(RTC_IRQn, (uint32_t)rtc_isr);
NVIC_EnableIRQ(RTC_IRQn);
/* Setup high resolution clock - LPTMR */
LPTMR_GetDefaultConfig(&lptmrConfig);
/* Use 32kHz drive */
CLOCK_SetXtal32Freq(OSC32K_CLK_HZ);
lptmrConfig.prescalerClockSource = kLPTMR_PrescalerClock_2;
LPTMR_Init(LPTMR0, &lptmrConfig);
LPTMR_EnableInterrupts(LPTMR0, kLPTMR_TimerInterruptEnable);
NVIC_ClearPendingIRQ(LPTMR0_IRQn);
NVIC_SetVector(LPTMR0_IRQn, (uint32_t)lptmr_isr);
EnableIRQ(LPTMR0_IRQn);
}
/** Read the current counter
*
* @return The current timer's counter value in microseconds
*/
uint32_t lp_ticker_read(void)
{
uint32_t sec, pre;
if (!lp_ticker_inited) {
lp_ticker_init();
}
sec = RTC->TSR; /* 32b: Seconds */
pre = RTC->TPR; /* 16b: Increments every 32.768kHz clock cycle (30us) */
/* Final value: 11b (4095) for sec and 21b for usec (pre can reach 1,000,000us which is close to 1<<20) */
uint32_t ret = (((sec & MAX_SEC_MASK) * SEC_IN_USEC) + (((uint64_t)pre * SEC_IN_USEC) / OSC32K_CLK_HZ));
return ret;
}
/** Set interrupt for specified timestamp
*
* @param timestamp The time in microseconds to be set
*/
void lp_ticker_set_interrupt(timestamp_t timestamp)
{
uint32_t now_us, delta_us, delta_ticks;
if (!lp_ticker_inited) {
lp_ticker_init();
}
lptmr_schedule = 0;
now_us = lp_ticker_read();
delta_us = timestamp > now_us ? timestamp - now_us : (uint32_t)((uint64_t)timestamp + 0xFFFFFFFF - now_us);
/* Checking if LPTRM can handle this sleep */
delta_ticks = USEC_TO_COUNT(delta_us, CLOCK_GetFreq(kCLOCK_Er32kClk));
if (delta_ticks > MAX_LPTMR_SLEEP) {
/* Using RTC if wait time is over 16b (2s @32kHz) */
uint32_t delta_sec;
delta_us += COUNT_TO_USEC(RTC->TPR, CLOCK_GetFreq(kCLOCK_Er32kClk)); /* Accounting for started second */
delta_sec = delta_us / SEC_IN_USEC;
delta_us -= delta_sec * SEC_IN_USEC;
RTC->TAR = RTC->TSR + delta_sec - 1;
RTC_EnableInterrupts(RTC, kRTC_AlarmInterruptEnable);
/* Set aditional, subsecond, sleep time */
if (delta_us) {
lptmr_schedule = USEC_TO_COUNT(delta_us, CLOCK_GetFreq(kCLOCK_Er32kClk));
}
} else {
/* Below RTC resolution using LPTMR */
LPTMR_SetTimerPeriod(LPTMR0, delta_ticks);
LPTMR_EnableInterrupts(LPTMR0, kLPTMR_TimerInterruptEnable);
LPTMR_StartTimer(LPTMR0);
}
}
/** Disable low power ticker interrupt
*
*/
void lp_ticker_disable_interrupt(void)
{
LPTMR_DisableInterrupts(LPTMR0, kLPTMR_TimerInterruptEnable);
RTC_DisableInterrupts(RTC, kRTC_AlarmInterruptEnable);
}
/** Clear the low power ticker interrupt
*
*/
void lp_ticker_clear_interrupt(void)
{
RTC->TAR = 0; /* Write clears the IRQ flag */
LPTMR_ClearStatusFlags(LPTMR0, kLPTMR_TimerCompareFlag);
}
#endif /* DEVICE_LOWPOWERTIMER */