Fork of ble-x-nucleo-idb0xa1 with changes required by BleStarMbed

Dependents:   ble-star-mbed

Committer:
lorevee
Date:
Tue Feb 20 11:07:16 2018 +0000
Revision:
0:ac0b0725c6fa
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lorevee 0:ac0b0725c6fa 1 /*
lorevee 0:ac0b0725c6fa 2 * Copyright (c) 2004, Swedish Institute of Computer Science.
lorevee 0:ac0b0725c6fa 3 * All rights reserved.
lorevee 0:ac0b0725c6fa 4 *
lorevee 0:ac0b0725c6fa 5 * Redistribution and use in source and binary forms, with or without
lorevee 0:ac0b0725c6fa 6 * modification, are permitted provided that the following conditions
lorevee 0:ac0b0725c6fa 7 * are met:
lorevee 0:ac0b0725c6fa 8 * 1. Redistributions of source code must retain the above copyright
lorevee 0:ac0b0725c6fa 9 * notice, this list of conditions and the following disclaimer.
lorevee 0:ac0b0725c6fa 10 * 2. Redistributions in binary form must reproduce the above copyright
lorevee 0:ac0b0725c6fa 11 * notice, this list of conditions and the following disclaimer in the
lorevee 0:ac0b0725c6fa 12 * documentation and/or other materials provided with the distribution.
lorevee 0:ac0b0725c6fa 13 * 3. Neither the name of the Institute nor the names of its contributors
lorevee 0:ac0b0725c6fa 14 * may be used to endorse or promote products derived from this software
lorevee 0:ac0b0725c6fa 15 * without specific prior written permission.
lorevee 0:ac0b0725c6fa 16 *
lorevee 0:ac0b0725c6fa 17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
lorevee 0:ac0b0725c6fa 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
lorevee 0:ac0b0725c6fa 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
lorevee 0:ac0b0725c6fa 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
lorevee 0:ac0b0725c6fa 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
lorevee 0:ac0b0725c6fa 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
lorevee 0:ac0b0725c6fa 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
lorevee 0:ac0b0725c6fa 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
lorevee 0:ac0b0725c6fa 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
lorevee 0:ac0b0725c6fa 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
lorevee 0:ac0b0725c6fa 27 * SUCH DAMAGE.
lorevee 0:ac0b0725c6fa 28 *
lorevee 0:ac0b0725c6fa 29 * This file is part of the Contiki operating system.
lorevee 0:ac0b0725c6fa 30 *
lorevee 0:ac0b0725c6fa 31 * Author: Adam Dunkels <adam@sics.se>
lorevee 0:ac0b0725c6fa 32 *
lorevee 0:ac0b0725c6fa 33 */
lorevee 0:ac0b0725c6fa 34
lorevee 0:ac0b0725c6fa 35 #include "ble_gp_timer.h"
lorevee 0:ac0b0725c6fa 36
lorevee 0:ac0b0725c6fa 37 /*---------------------------------------------------------------------------*/
lorevee 0:ac0b0725c6fa 38 /**
lorevee 0:ac0b0725c6fa 39 * Set a timer.
lorevee 0:ac0b0725c6fa 40 *
lorevee 0:ac0b0725c6fa 41 * This function sets a timer for a time sometime in the
lorevee 0:ac0b0725c6fa 42 * future. The function timer_expired() will evaluate to true after
lorevee 0:ac0b0725c6fa 43 * the timer has expired.
lorevee 0:ac0b0725c6fa 44 *
lorevee 0:ac0b0725c6fa 45 * @param[in] t A pointer to the timer
lorevee 0:ac0b0725c6fa 46 * @param[in] interval The interval before the timer expires.
lorevee 0:ac0b0725c6fa 47 *
lorevee 0:ac0b0725c6fa 48 */
lorevee 0:ac0b0725c6fa 49 void
lorevee 0:ac0b0725c6fa 50 Timer_Set(struct timer *t, tClockTime interval)
lorevee 0:ac0b0725c6fa 51 {
lorevee 0:ac0b0725c6fa 52 t->interval = interval;
lorevee 0:ac0b0725c6fa 53 t->start = Clock_Time();
lorevee 0:ac0b0725c6fa 54 }
lorevee 0:ac0b0725c6fa 55 /*---------------------------------------------------------------------------*/
lorevee 0:ac0b0725c6fa 56 /**
lorevee 0:ac0b0725c6fa 57 * Reset the timer with the same interval.
lorevee 0:ac0b0725c6fa 58 *
lorevee 0:ac0b0725c6fa 59 * This function resets the timer with the same interval that was
lorevee 0:ac0b0725c6fa 60 * given to the timer_set() function. The start point of the interval
lorevee 0:ac0b0725c6fa 61 * is the exact time that the timer last expired. Therefore, this
lorevee 0:ac0b0725c6fa 62 * function will cause the timer to be stable over time, unlike the
lorevee 0:ac0b0725c6fa 63 * timer_restart() function.
lorevee 0:ac0b0725c6fa 64 *
lorevee 0:ac0b0725c6fa 65 * \param t A pointer to the timer.
lorevee 0:ac0b0725c6fa 66 *
lorevee 0:ac0b0725c6fa 67 * \sa timer_restart()
lorevee 0:ac0b0725c6fa 68 */
lorevee 0:ac0b0725c6fa 69 void
lorevee 0:ac0b0725c6fa 70 Timer_Reset(struct timer *t)
lorevee 0:ac0b0725c6fa 71 {
lorevee 0:ac0b0725c6fa 72 t->start += t->interval;
lorevee 0:ac0b0725c6fa 73 }
lorevee 0:ac0b0725c6fa 74 /*---------------------------------------------------------------------------*/
lorevee 0:ac0b0725c6fa 75 /**
lorevee 0:ac0b0725c6fa 76 * Restart the timer from the current point in time
lorevee 0:ac0b0725c6fa 77 *
lorevee 0:ac0b0725c6fa 78 * This function restarts a timer with the same interval that was
lorevee 0:ac0b0725c6fa 79 * given to the timer_set() function. The timer will start at the
lorevee 0:ac0b0725c6fa 80 * current time.
lorevee 0:ac0b0725c6fa 81 *
lorevee 0:ac0b0725c6fa 82 * \note A periodic timer will drift if this function is used to reset
lorevee 0:ac0b0725c6fa 83 * it. For preioric timers, use the timer_reset() function instead.
lorevee 0:ac0b0725c6fa 84 *
lorevee 0:ac0b0725c6fa 85 * \param t A pointer to the timer.
lorevee 0:ac0b0725c6fa 86 *
lorevee 0:ac0b0725c6fa 87 * \sa timer_reset()
lorevee 0:ac0b0725c6fa 88 */
lorevee 0:ac0b0725c6fa 89 void
lorevee 0:ac0b0725c6fa 90 Timer_Restart(struct timer *t)
lorevee 0:ac0b0725c6fa 91 {
lorevee 0:ac0b0725c6fa 92 t->start = Clock_Time();
lorevee 0:ac0b0725c6fa 93 }
lorevee 0:ac0b0725c6fa 94 /*---------------------------------------------------------------------------*/
lorevee 0:ac0b0725c6fa 95 /**
lorevee 0:ac0b0725c6fa 96 * Check if a timer has expired.
lorevee 0:ac0b0725c6fa 97 *
lorevee 0:ac0b0725c6fa 98 * This function tests if a timer has expired and returns true or
lorevee 0:ac0b0725c6fa 99 * false depending on its status.
lorevee 0:ac0b0725c6fa 100 *
lorevee 0:ac0b0725c6fa 101 * \param t A pointer to the timer
lorevee 0:ac0b0725c6fa 102 *
lorevee 0:ac0b0725c6fa 103 * \return Non-zero if the timer has expired, zero otherwise.
lorevee 0:ac0b0725c6fa 104 *
lorevee 0:ac0b0725c6fa 105 */
lorevee 0:ac0b0725c6fa 106 int
lorevee 0:ac0b0725c6fa 107 Timer_Expired(struct timer *t)
lorevee 0:ac0b0725c6fa 108 {
lorevee 0:ac0b0725c6fa 109 /* Note: Can not return diff >= t->interval so we add 1 to diff and return
lorevee 0:ac0b0725c6fa 110 t->interval < diff - required to avoid an internal error in mspgcc. */
lorevee 0:ac0b0725c6fa 111 tClockTime diff = (Clock_Time() - t->start) + 1;
lorevee 0:ac0b0725c6fa 112 return t->interval < diff;
lorevee 0:ac0b0725c6fa 113
lorevee 0:ac0b0725c6fa 114 }
lorevee 0:ac0b0725c6fa 115 /*---------------------------------------------------------------------------*/
lorevee 0:ac0b0725c6fa 116 /**
lorevee 0:ac0b0725c6fa 117 * The time until the timer expires
lorevee 0:ac0b0725c6fa 118 *
lorevee 0:ac0b0725c6fa 119 * This function returns the time until the timer expires.
lorevee 0:ac0b0725c6fa 120 *
lorevee 0:ac0b0725c6fa 121 * \param t A pointer to the timer
lorevee 0:ac0b0725c6fa 122 *
lorevee 0:ac0b0725c6fa 123 * \return The time until the timer expires
lorevee 0:ac0b0725c6fa 124 *
lorevee 0:ac0b0725c6fa 125 */
lorevee 0:ac0b0725c6fa 126 tClockTime
lorevee 0:ac0b0725c6fa 127 Timer_Remaining(struct timer *t)
lorevee 0:ac0b0725c6fa 128 {
lorevee 0:ac0b0725c6fa 129 return t->start + t->interval - Clock_Time();
lorevee 0:ac0b0725c6fa 130 }
lorevee 0:ac0b0725c6fa 131 /*---------------------------------------------------------------------------*/
lorevee 0:ac0b0725c6fa 132 #ifdef __DMA_LP__
lorevee 0:ac0b0725c6fa 133
lorevee 0:ac0b0725c6fa 134 tBleStatus Blue_NRG_HCI_Timer_Start(uint32_t expiryTime,
lorevee 0:ac0b0725c6fa 135 TIMER_HCI_TIMEOUT_NOTIFY_CALLBACK_TYPE timercb,
lorevee 0:ac0b0725c6fa 136 uint8_t *timerID)
lorevee 0:ac0b0725c6fa 137 {
lorevee 0:ac0b0725c6fa 138 TIMER_Create(eTimerModuleID_BlueNRG_HCI, timerID, eTimerMode_SingleShot,
lorevee 0:ac0b0725c6fa 139 (pf_TIMER_TimerCallBack_t) timercb);
lorevee 0:ac0b0725c6fa 140 TIMER_Start(*timerID, expiryTime*1000/TIMERSERVER_TICK_VALUE);
lorevee 0:ac0b0725c6fa 141
lorevee 0:ac0b0725c6fa 142 return (BLE_STATUS_SUCCESS);
lorevee 0:ac0b0725c6fa 143 }
lorevee 0:ac0b0725c6fa 144
lorevee 0:ac0b0725c6fa 145 /*---------------------------------------------------------------------------*/
lorevee 0:ac0b0725c6fa 146 tBleStatus Blue_NRG_HCI_Timer_Stop(uint8_t timerID)
lorevee 0:ac0b0725c6fa 147 {
lorevee 0:ac0b0725c6fa 148 TIMER_Delete(timerID);
lorevee 0:ac0b0725c6fa 149
lorevee 0:ac0b0725c6fa 150 return (BLE_STATUS_SUCCESS);
lorevee 0:ac0b0725c6fa 151 }
lorevee 0:ac0b0725c6fa 152
lorevee 0:ac0b0725c6fa 153 #endif /* __DMA_LP__ */
lorevee 0:ac0b0725c6fa 154 /*---------------------------------------------------------------------------*/