Intelligent Systems / mbed-src

Fork of mbed-src by mbed official

Committer:
bparrott
Date:
Thu Oct 23 10:10:42 2014 +0000
Revision:
349:591ef4b19797
Parent:
168:cf9372ac0a74
This changes the timer used at the core of MBED from Timer 3 to Timer 0. This allows for the use of external pins with both Timer 2 and 3.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 168:cf9372ac0a74 1 /*
mbed_official 168:cf9372ac0a74 2 * Copyright (c) 2013 - 2014, Freescale Semiconductor, Inc.
mbed_official 168:cf9372ac0a74 3 * All rights reserved.
mbed_official 168:cf9372ac0a74 4 *
mbed_official 168:cf9372ac0a74 5 * Redistribution and use in source and binary forms, with or without modification,
mbed_official 168:cf9372ac0a74 6 * are permitted provided that the following conditions are met:
mbed_official 168:cf9372ac0a74 7 *
mbed_official 168:cf9372ac0a74 8 * o Redistributions of source code must retain the above copyright notice, this list
mbed_official 168:cf9372ac0a74 9 * of conditions and the following disclaimer.
mbed_official 168:cf9372ac0a74 10 *
mbed_official 168:cf9372ac0a74 11 * o Redistributions in binary form must reproduce the above copyright notice, this
mbed_official 168:cf9372ac0a74 12 * list of conditions and the following disclaimer in the documentation and/or
mbed_official 168:cf9372ac0a74 13 * other materials provided with the distribution.
mbed_official 168:cf9372ac0a74 14 *
mbed_official 168:cf9372ac0a74 15 * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
mbed_official 168:cf9372ac0a74 16 * contributors may be used to endorse or promote products derived from this
mbed_official 168:cf9372ac0a74 17 * software without specific prior written permission.
mbed_official 168:cf9372ac0a74 18 *
mbed_official 168:cf9372ac0a74 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
mbed_official 168:cf9372ac0a74 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
mbed_official 168:cf9372ac0a74 21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 168:cf9372ac0a74 22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
mbed_official 168:cf9372ac0a74 23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
mbed_official 168:cf9372ac0a74 24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
mbed_official 168:cf9372ac0a74 25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
mbed_official 168:cf9372ac0a74 26 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
mbed_official 168:cf9372ac0a74 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
mbed_official 168:cf9372ac0a74 28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 168:cf9372ac0a74 29 */
mbed_official 168:cf9372ac0a74 30
mbed_official 168:cf9372ac0a74 31 #if !defined(__SW_TIMER_H__)
mbed_official 168:cf9372ac0a74 32 #define __SW_TIMER_H__
mbed_official 168:cf9372ac0a74 33
mbed_official 168:cf9372ac0a74 34 #include <stdint.h>
mbed_official 168:cf9372ac0a74 35 #include <stdbool.h>
mbed_official 168:cf9372ac0a74 36
mbed_official 168:cf9372ac0a74 37 /*! @addtogroup sw_timer Software Timer
mbed_official 168:cf9372ac0a74 38 * @brief This module is used to interface with Abstract Timer HAL to generate periodical timeouts
mbed_official 168:cf9372ac0a74 39 * required through different modules of the AOA protocol. This block will be based on 1ms
mbed_official 168:cf9372ac0a74 40 * ticks for all the timeout calculations. The HAL Interface block used to communicate with
mbed_official 168:cf9372ac0a74 41 * this must have the same 1ms timeout configured. This module can generate different
mbed_official 168:cf9372ac0a74 42 * software timer channels based on the same 1ms.
mbed_official 168:cf9372ac0a74 43 */
mbed_official 168:cf9372ac0a74 44 /*! @{*/
mbed_official 168:cf9372ac0a74 45
mbed_official 168:cf9372ac0a74 46 /*! Definition of the possible status of a software channel timer. */
mbed_official 168:cf9372ac0a74 47 typedef enum SwTimerChannelStatus
mbed_official 168:cf9372ac0a74 48 {
mbed_official 168:cf9372ac0a74 49 kSwTimerChannelExpired = 0x00, /*!< Indicates the timer channel has counted the given ms*/
mbed_official 168:cf9372ac0a74 50 kSwTimerChannelStillCounting = 0x01, /*!< Indicates the timeout of the channel has not expired
mbed_official 168:cf9372ac0a74 51 and the timer is still counting.*/
mbed_official 168:cf9372ac0a74 52 kSwTimerChannelIsDisable = 0x02, /*!< Indicates the timer channel is not reserved. */
mbed_official 168:cf9372ac0a74 53 kSwTimerChannelNotAvailable = 0xFF /*!< Indicates there are not available channels to reserve
mbed_official 168:cf9372ac0a74 54 or the requested channel is not available.*/
mbed_official 168:cf9372ac0a74 55 }sw_timer_channel_status_t;
mbed_official 168:cf9372ac0a74 56
mbed_official 168:cf9372ac0a74 57 /*! List of status and errors. */
mbed_official 168:cf9372ac0a74 58 enum _sw_timer_errors
mbed_official 168:cf9372ac0a74 59 {
mbed_official 168:cf9372ac0a74 60 kSwTimerStatusSuccess, /*!< The execution was successful.*/
mbed_official 168:cf9372ac0a74 61 kSwTimerStatusFail, /*!< The execution failed.*/
mbed_official 168:cf9372ac0a74 62 kSwTimerStatusInvalidChannel /*!< The given channel is not valid. Valid channels are 0 to
mbed_official 168:cf9372ac0a74 63 (SW_TIMER_NUMBER_CHANNELS - 1). */
mbed_official 168:cf9372ac0a74 64 };
mbed_official 168:cf9372ac0a74 65
mbed_official 168:cf9372ac0a74 66 /*!
mbed_official 168:cf9372ac0a74 67 * Data type of the counter of each timer channel. If it is an int8_t the counter will count
mbed_official 168:cf9372ac0a74 68 * up to 127ms, int16_t up to 32767ms and int32_t up to 2147483647ms.
mbed_official 168:cf9372ac0a74 69 */
mbed_official 168:cf9372ac0a74 70 typedef int32_t time_counter_t;
mbed_official 168:cf9372ac0a74 71
mbed_official 168:cf9372ac0a74 72 /*! Max timeout value according to size of the time counter */
mbed_official 168:cf9372ac0a74 73 enum sw_timer_timeouts
mbed_official 168:cf9372ac0a74 74 {
mbed_official 168:cf9372ac0a74 75 kSwTimerMaxTimeout = 2147483647
mbed_official 168:cf9372ac0a74 76 };
mbed_official 168:cf9372ac0a74 77
mbed_official 168:cf9372ac0a74 78 /*!
mbed_official 168:cf9372ac0a74 79 * Data type of the free running counter. This data type should be unsigned and will count up to
mbed_official 168:cf9372ac0a74 80 * 255ms if it is uint8_t, 65535ms for uint16_t and 4294967295ms for uint32_t.
mbed_official 168:cf9372ac0a74 81 */
mbed_official 168:cf9372ac0a74 82 typedef uint32_t time_free_counter_t;
mbed_official 168:cf9372ac0a74 83
mbed_official 168:cf9372ac0a74 84 #if defined(__cplusplus)
mbed_official 168:cf9372ac0a74 85 extern "C" {
mbed_official 168:cf9372ac0a74 86 #endif /* __cplusplus*/
mbed_official 168:cf9372ac0a74 87
mbed_official 168:cf9372ac0a74 88 /*!
mbed_official 168:cf9372ac0a74 89 * @brief Initializes the software timer module. Prepares variables and HAL layer to provide timer
mbed_official 168:cf9372ac0a74 90 * services. Starts the free running counter which will be available to get its value any
mbed_official 168:cf9372ac0a74 91 * time while the service is running; it is useful whenever a module wants to keep track of
mbed_official 168:cf9372ac0a74 92 * time, but do not wants to reserve a channel.
mbed_official 168:cf9372ac0a74 93 *
mbed_official 168:cf9372ac0a74 94 * @return status_t Returns software timer status after initialization.
mbed_official 168:cf9372ac0a74 95 * @retval kSwTimerStatusSuccess The initialization was successful and the software timer is ready
mbed_official 168:cf9372ac0a74 96 * to provide services.
mbed_official 168:cf9372ac0a74 97 * @retval kSwTimerStatusFail The initialization failed.
mbed_official 168:cf9372ac0a74 98 */
mbed_official 168:cf9372ac0a74 99 uint32_t sw_timer_init_service(void);
mbed_official 168:cf9372ac0a74 100
mbed_official 168:cf9372ac0a74 101 /*!
mbed_official 168:cf9372ac0a74 102 * @brief Deinitializes the software timer module. Shutdown HAL layer, so no timer service can be
mbed_official 168:cf9372ac0a74 103 * provided after the execution of this function.
mbed_official 168:cf9372ac0a74 104 *
mbed_official 168:cf9372ac0a74 105 * @return void
mbed_official 168:cf9372ac0a74 106 */
mbed_official 168:cf9372ac0a74 107 void sw_timer_shutdown_service(void);
mbed_official 168:cf9372ac0a74 108
mbed_official 168:cf9372ac0a74 109 /*!
mbed_official 168:cf9372ac0a74 110 * @brief Reserves a free timer channel to be used by any module and returns its identifier.
mbed_official 168:cf9372ac0a74 111 *
mbed_official 168:cf9372ac0a74 112 * @return uint8_t Returns the number of the channel that was reserved.
mbed_official 168:cf9372ac0a74 113 * @retval Any value between 0 and SW_TIMER_NUMBER_CHANNELS is a valid channel. It indicates the
mbed_official 168:cf9372ac0a74 114 * channel was reserved and can be used.
mbed_official 168:cf9372ac0a74 115 * @retval kSwTimerChannelNotAvailable If there is not any available channel, because all
mbed_official 168:cf9372ac0a74 116 * channels are already reserved.
mbed_official 168:cf9372ac0a74 117 */
mbed_official 168:cf9372ac0a74 118 uint8_t sw_timer_reserve_channel(void);
mbed_official 168:cf9372ac0a74 119
mbed_official 168:cf9372ac0a74 120 /*!
mbed_official 168:cf9372ac0a74 121 * @brief Returns the actual status of the given timer channel. The timer has to be previously
mbed_official 168:cf9372ac0a74 122 * started to return a valid status.
mbed_official 168:cf9372ac0a74 123 *
mbed_official 168:cf9372ac0a74 124 * @param timerChannel [in] Indicates the timer channel which status is going to be returned.
mbed_official 168:cf9372ac0a74 125 *
mbed_official 168:cf9372ac0a74 126 * @return sw_timer_channel_status_t Current status of the given timer channel.
mbed_official 168:cf9372ac0a74 127 * @retval kSwTimerChannelExpired Indicates the timer channel has counted the given ms.
mbed_official 168:cf9372ac0a74 128 * @retval kSwTimerChannelStillCounting Indicates the timeout of the channel has not expired and
mbed_official 168:cf9372ac0a74 129 the timer is still counting.
mbed_official 168:cf9372ac0a74 130 * @retval kSwTimerChannelIsDisable Indicates the timer channel is not reserved.
mbed_official 168:cf9372ac0a74 131 * @retval kSwTimerChannelNotAvailable Indicates the timer channel is invalid.
mbed_official 168:cf9372ac0a74 132 */
mbed_official 168:cf9372ac0a74 133 sw_timer_channel_status_t sw_timer_get_channel_status(uint8_t timerChannel);
mbed_official 168:cf9372ac0a74 134
mbed_official 168:cf9372ac0a74 135 /*!
mbed_official 168:cf9372ac0a74 136 * @brief Starts the count down of the given timer channel. The timer channel has to be previously
mbed_official 168:cf9372ac0a74 137 * reserved.
mbed_official 168:cf9372ac0a74 138 *
mbed_official 168:cf9372ac0a74 139 * @param timerChannel [in] Indicates the timer channel that is going to be started.
mbed_official 168:cf9372ac0a74 140 * @param timeout [in] Time in ms that the timer channel will count. The timeout should be
mbed_official 168:cf9372ac0a74 141 a multiple of count unit of the timer, otherwise it will be taken
mbed_official 168:cf9372ac0a74 142 the integer part of the division and the exact count will not be
mbed_official 168:cf9372ac0a74 143 achieved
mbed_official 168:cf9372ac0a74 144 *
mbed_official 168:cf9372ac0a74 145 * @return status_t Reports failures in the execution of the function.
mbed_official 168:cf9372ac0a74 146 * @retval kSwTimerStatusSuccess A channel was started successfully.
mbed_official 168:cf9372ac0a74 147 * @retval kSwTimerStatusInvalidChannel The timer channel is invalid, it does not exist.
mbed_official 168:cf9372ac0a74 148 */
mbed_official 168:cf9372ac0a74 149 uint32_t sw_timer_start_channel(uint8_t timerChannel, time_counter_t timeout);
mbed_official 168:cf9372ac0a74 150
mbed_official 168:cf9372ac0a74 151 /*!
mbed_official 168:cf9372ac0a74 152 * @brief Releases the given timer channel, so it can be used by someone else.
mbed_official 168:cf9372ac0a74 153 *
mbed_official 168:cf9372ac0a74 154 * @param timerChannel [in] Identifier of the timer channel.
mbed_official 168:cf9372ac0a74 155 *
mbed_official 168:cf9372ac0a74 156 * @return status_t Reports failures in the execution of the function.
mbed_official 168:cf9372ac0a74 157 * @retval kSwTimerStatusSuccess A channel was released successfully.
mbed_official 168:cf9372ac0a74 158 * @retval kSwTimerStatusInvalidChannel The timer channel is invalid, it does not exist.
mbed_official 168:cf9372ac0a74 159 */
mbed_official 168:cf9372ac0a74 160 uint32_t sw_timer_release_channel(uint8_t timerChannel);
mbed_official 168:cf9372ac0a74 161
mbed_official 168:cf9372ac0a74 162 /*!
mbed_official 168:cf9372ac0a74 163 * @brief Gets the current value of the free running counter. Any module can keep track of the time
mbed_official 168:cf9372ac0a74 164 * by reading this counter and calculates time difference. No reservation of timer channel
mbed_official 168:cf9372ac0a74 165 * is needed. Consider for calculations that when the counter overflows it will start from
mbed_official 168:cf9372ac0a74 166 * 0 again.
mbed_official 168:cf9372ac0a74 167 *
mbed_official 168:cf9372ac0a74 168 * @return time_free_counter_t Returns current count of the free running counter.
mbed_official 168:cf9372ac0a74 169 */
mbed_official 168:cf9372ac0a74 170 time_free_counter_t sw_timer_get_free_counter(void);
mbed_official 168:cf9372ac0a74 171
mbed_official 168:cf9372ac0a74 172 /*!
mbed_official 168:cf9372ac0a74 173 * @brief This function is called every 1ms by the interruption and update count down values of all
mbed_official 168:cf9372ac0a74 174 * timer channels.
mbed_official 168:cf9372ac0a74 175 *
mbed_official 168:cf9372ac0a74 176 * @return void
mbed_official 168:cf9372ac0a74 177 */
mbed_official 168:cf9372ac0a74 178 void sw_timer_update_counters(void);
mbed_official 168:cf9372ac0a74 179
mbed_official 168:cf9372ac0a74 180
mbed_official 168:cf9372ac0a74 181 #if defined(__cplusplus)
mbed_official 168:cf9372ac0a74 182 }
mbed_official 168:cf9372ac0a74 183 #endif /* __cplusplus*/
mbed_official 168:cf9372ac0a74 184 /*! @}*/
mbed_official 168:cf9372ac0a74 185 /*Group sw_timer*/
mbed_official 168:cf9372ac0a74 186
mbed_official 168:cf9372ac0a74 187 #endif /* __SW_TIMER_H__ */
mbed_official 168:cf9372ac0a74 188 /*******************************************************************************
mbed_official 168:cf9372ac0a74 189 * EOF
mbed_official 168:cf9372ac0a74 190 ******************************************************************************/
mbed_official 168:cf9372ac0a74 191