Morpheus / target-freescale-ksdk

Fork of target-freescale-ksdk by -deleted-

Committer:
screamer
Date:
Wed Mar 23 21:26:50 2016 +0000
Revision:
0:e4d670b91a9a
Initial revision

Who changed what in which revision?

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