Skip to content

Ticker specification (branch feature-hal-spec-ticker) #5164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions TESTS/mbed_hal/lp_ticker_api_tests.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* mbed Microcontroller Library
* Copyright (c) 2017-2017 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.
*/

/** \addtogroup hal_lp_ticker_tests
* @{
*/

#ifndef LP_TICKER_API_TESTS_H
#define LP_TICKER_API_TESTS_H

#include "device.h"


#ifdef __cplusplus
extern "C" {
#endif


/** Test that the ticker has the right frequency and number of bits
*
*/
void lp_ticker_info_test(void);

/** Test that the ticker operates in deep sleep mode
*
*/
void lp_ticker_deepsleep_test(void);


/**@}*/

#ifdef __cplusplus
}
#endif

#endif

/**@}*/
103 changes: 103 additions & 0 deletions TESTS/mbed_hal/ticker_api_tests.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/* mbed Microcontroller Library
* Copyright (c) 2017-2017 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.
*/

/** \addtogroup hal_ticker_tests */
/** @{*/

#ifndef TICKER_API_TESTS_H
#define TICKER_API_TESTS_H

#include "device.h"


#ifdef __cplusplus
extern "C" {
#endif

/** Test that ticker_init can be called multiple times
*
*/
void ticker_init_test(void);

/** Test that ticker has a valid frequency and bitwidth
*
*/
void ticker_info_test(void);

/** Test that the ticker increments by one on each tick
*
*/
void ticker_increment_test(void);

/** Test that the interrupt fires at the right time
*
*/
void ticker_interrupt_test(void);

/** Test that an interrupt is not triggered when ticker_set_interrupt is called with a time from the past
*
*/
void ticker_past_test(void);

/** Test that a ticker can be rescheduled repeatedly before the handler has been called
*
*/
void ticker_repeat_reschedule_test(void);

/** Test that ticker_fire_interrupt causes and interrupt to get fired immediately
*
*/
void ticker_fire_now_test(void);

/** Test that common ticker functions complete with the required amount of time
*
* Ensure that ticker_read, ticker_clear_interrupt, ticker_set_interrupt
* and ticker_fire_interrupt take less than 20us to complete.
*
*/
void ticker_speed_test(void);

/** Test that the ticker correctly handles overflows
*
* This test verifies that rollover is properly handled and
* that scheduling for a time after overflow works.
*
*/
void ticker_overflow_test(void);

/** Test that rescheduling does not cause drift
*
* This test verifies that repeated rescheduling does not cause a
* drift.
*
*/
void ticker_reschedule_test(void);

/** Test that the ticker is operating at the frequency it specifies
*
*/
void ticker_frequency_test(void);


/**@}*/

#ifdef __cplusplus
}
#endif

#endif

/**@}*/
45 changes: 45 additions & 0 deletions TESTS/mbed_hal/us_ticker_api_tests.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* mbed Microcontroller Library
* Copyright (c) 2017-2017 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.
*/

/** \addtogroup hal_us_ticker_tests */
/** @{*/

#ifndef US_TICKER_API_TESTS_H
#define US_TICKER_API_TESTS_H

#include "device.h"


#ifdef __cplusplus
extern "C" {
#endif


/** Test that the ticker has the right frequency and number of bits
*
*/
void us_ticker_info_test(void);


/**@}*/

#ifdef __cplusplus
}
#endif

#endif

/**@}*/
129 changes: 124 additions & 5 deletions hal/lp_ticker_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,33 @@ extern "C" {
#endif

/**
* \defgroup hal_LpTicker Low Power Ticker Functions
* \defgroup hal_lp_ticker Low Power Ticker
* Low level interface to the low power ticker of a target
*
* # Defined behavior
* * Has a reported frequency between 8KHz and 64KHz - verified by lp_ticker_info_test
* * Has a counter that is at least 12 bits wide - verified by lp_ticker_info_test
* * Continues operating in deep sleep mode - verified by lp_ticker_sleep_test
* * All behavior defined by the @ref hal_ticker_shared "ticker specification"
*
* # Undefined behavior
* * See the @ref hal_ticker_shared "ticker specification"
*
* @see hal_lp_ticker_tests
*
* @{
*/

/**
* \defgroup hal_lp_ticker_tests Low Power Ticker tests
* Tests to validate the proper implementation of the low power ticker
*
* To run the low power ticker hal tests use the command:
*
* mbed test -t <toolchain> -m <target> -n tests-mbed_hal-us_lp_ticker*,tests-mbed_hal-lp_ticker*
*
*/

typedef void (*ticker_irq_handler_type)(const ticker_data_t *const);

/** Set low power ticker IRQ handler
Expand Down Expand Up @@ -62,39 +85,135 @@ void lp_ticker_irq_handler(void);

/** Initialize the low power ticker
*
* Initialize or re-initialize the ticker. This resets all the
* clocking and prescaler registers, along with disabling
* the compare interrupt.
*
* Pseudo Code:
* @code
* void lp_ticker_init()
* {
* // Enable clock gate so processor can read LPTMR registers
* POWER_CTRL |= POWER_CTRL_LPTMR_Msk;
*
* // Disable the timer and ensure it is powered down
* LPTMR_CTRL &= ~(LPTMR_CTRL_ENABLE_Msk | LPTMR_CTRL_COMPARE_ENABLE_Msk);
*
* // Configure divisors - no division necessary
* LPTMR_PRESCALE = 0;
* LPTMR_CTRL |= LPTMR_CTRL_ENABLE_Msk;
*
* // Install the interrupt handler
* NVIC_SetVector(LPTMR_IRQn, (uint32_t)lp_ticker_irq_handler);
* NVIC_EnableIRQ(LPTMR_IRQn);
* }
* @endcode
*/
void lp_ticker_init(void);

/** Read the current counter
/** Read the current tick
*
* If no rollover has occurred, the seconds passed since ::lp_ticker_init
* was called can be found by dividing the ticks returned by this function
* by the frequency returned by ::lp_ticker_get_info.
*
* @return The current timer's counter value in ticks
*
* @return The current timer's counter value in microseconds
* Pseudo Code:
* @code
* uint32_t lp_ticker_read()
* {
* uint16_t count;
* uint16_t last_count;
*
* // Loop until the same tick is read twice since this
* // is ripple counter on a different clock domain.
* count = LPTMR_COUNT;
* do {
* last_count = count;
* count = LPTMR_COUNT;
* } while (last_count != count);
*
* return count;
* }
* @endcode
*/
uint32_t lp_ticker_read(void);

/** Set interrupt for specified timestamp
*
* @param timestamp The time in microseconds to be set
* @param timestamp The time in ticks to be set
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you detail the behavior if the parameter is out of range (might be unspecified....) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

*
* @note no special handling needs to be done for times in the past
* as the common timer code will detect this and call
* ::lp_ticker_fire_interrupt if this is the case
*
* @note calling this function with timestamp of more than the supported
* number of bits returned by ::lp_ticker_get_info results in undefined
* behavior.
*
* Pseudo Code:
* @code
* void lp_ticker_set_interrupt(timestamp_t timestamp)
* {
* LPTMR_COMPARE = timestamp;
* LPTMR_CTRL |= LPTMR_CTRL_COMPARE_ENABLE_Msk;
* }
* @endcode
*/
void lp_ticker_set_interrupt(timestamp_t timestamp);

/** Disable low power ticker interrupt
*
* Pseudo Code:
* @code
* void lp_ticker_disable_interrupt(void)
* {
* // Disable the compare interrupt
* LPTMR_CTRL &= ~LPTMR_CTRL_COMPARE_ENABLE_Msk;
* }
* @endcode
*/
void lp_ticker_disable_interrupt(void);

/** Clear the low power ticker interrupt
*
* Pseudo Code:
* @code
* void lp_ticker_clear_interrupt(void)
* {
* // Write to the ICR (interrupt clear register) of the LPTMR
* LPTMR_ICR = LPTMR_ICR_COMPARE_Msk;
* }
* @endcode
*/
void lp_ticker_clear_interrupt(void);

/** Set pending interrupt that should be fired right away.
*
* The ticker should be initialized prior calling this function.
* Pseudo Code:
* @code
* void lp_ticker_fire_interrupt(void)
* {
* NVIC_SetPendingIRQ(LPTMR_IRQn);
* }
* @endcode
*/
void lp_ticker_fire_interrupt(void);

/** Get frequency and counter bits of this ticker.
*
* Pseudo Code:
* @code
* const ticker_info_t* lp_ticker_get_info()
* {
* static const ticker_info_t info = {
* 32768, // 32KHz
* 16 // 16 bit counter
* };
* return &info;
* }
* @endcode
*/
const ticker_info_t* lp_ticker_get_info(void);

Expand Down
Loading