| 1 | /***************************************************************************//** |
| 2 | * @file TIME.c |
| 3 | * @brief Implementation of TIME Driver for RENESAS RL78G14 Processor. |
| 4 | * @author Dan Nechita |
| 5 | ******************************************************************************** |
| 6 | * Copyright 2012(c) Analog Devices, Inc. |
| 7 | * |
| 8 | * All rights reserved. |
| 9 | * |
| 10 | * Redistribution and use in source and binary forms, with or without |
| 11 | * modification, are permitted provided that the following conditions are met: |
| 12 | * - Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * - Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in |
| 16 | * the documentation and/or other materials provided with the |
| 17 | * distribution. |
| 18 | * - Neither the name of Analog Devices, Inc. nor the names of its |
| 19 | * contributors may be used to endorse or promote products derived |
| 20 | * from this software without specific prior written permission. |
| 21 | * - The use of this software may or may not infringe the patent rights |
| 22 | * of one or more patent holders. This license does not release you |
| 23 | * from the requirement that you obtain separate licenses from these |
| 24 | * patent holders to use this software. |
| 25 | * - Use of the software either in source or binary form, must be run |
| 26 | * on or directly connected to an Analog Devices Inc. component. |
| 27 | * |
| 28 | * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR |
| 29 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, |
| 30 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 31 | * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 32 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 33 | * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 34 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 35 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 36 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 37 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 38 | * |
| 39 | ******************************************************************************** |
| 40 | * SVN Revision: $WCREV$ |
| 41 | *******************************************************************************/ |
| 42 | |
| 43 | /******************************************************************************/ |
| 44 | /***************************** Include Files **********************************/ |
| 45 | /******************************************************************************/ |
| 46 | #include "RDKRL78G14.h" // RDKRL78G14 definitions. |
| 47 | #include "TIME.h" // TIME definitions. |
| 48 | |
| 49 | /*****************************************************************************/ |
| 50 | /************************** Macros Definitions *******************************/ |
| 51 | /*****************************************************************************/ |
| 52 | #define TIMER_CNT_INTERVAL 65536 |
| 53 | |
| 54 | /*****************************************************************************/ |
| 55 | /************************ Variables Declarations *****************************/ |
| 56 | /*****************************************************************************/ |
| 57 | unsigned short overflowCounter = 0; |
| 58 | |
| 59 | /******************************************************************************/ |
| 60 | /************************ Functions Definitions *******************************/ |
| 61 | /******************************************************************************/ |
| 62 | |
| 63 | /***************************************************************************//** |
| 64 | * @brief INTTM00 interrupt service routine. |
| 65 | * |
| 66 | * @return None. |
| 67 | *******************************************************************************/ |
| 68 | #pragma vector = INTTM00_vect |
| 69 | __interrupt static void Timer0InterruptHandler(void) |
| 70 | { |
| 71 | overflowCounter++; |
| 72 | } |
| 73 | |
| 74 | /***************************************************************************//** |
| 75 | * @brief Initializes the timer used in this driver. |
| 76 | * |
| 77 | * @return status - Result of the initialization procedure. |
| 78 | * Example: 0 - if initialization was successful; |
| 79 | * -1 - if initialization was unsuccessful. |
| 80 | *******************************************************************************/ |
| 81 | char TIME_Init(void) |
| 82 | { |
| 83 | PER0 |= 0x0001; // Enable Supply for Unit 0 that contains timers 0..7. |
| 84 | TT0 |= 0x0001; // Stop channel 0(Timer 0). |
| 85 | TPS0 = 0x0005; // CK00 = FCLK / 32. |
| 86 | TMR00 |= 0x0000; // CK00 input clock for channel 0. |
| 87 | TMMK00 = 1; // Disable INTTM00 interrupt. |
| 88 | TMIF00 = 0; // Clear INTM00 interrupt flag. |
| 89 | /* Set INTTM00 low priority */ |
| 90 | TMPR100 = 1; |
| 91 | TMPR000 = 1; |
| 92 | /* Configure channel 0 as a interval timer. */ |
| 93 | TMR00 |= 0x0000; // Interval timer mode. |
| 94 | TO0 &= ~0x0001; // Timer 0 output value is "0". |
| 95 | TOE0 &= ~0x0001; // Disable output of timer 0. |
| 96 | TMMK00 = 0; // Enable INTTM00 interrupt. |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | /***************************************************************************//** |
| 102 | * @brief The timer begins to count in steps of microseconds(us) until the user |
| 103 | * calls a stop measurement function. |
| 104 | * |
| 105 | * @return None. |
| 106 | *******************************************************************************/ |
| 107 | void TIME_StartMeasure(void) |
| 108 | { |
| 109 | TDR00 = 0xFFFF; |
| 110 | overflowCounter = 0; |
| 111 | TS0 |= 0x0001; // Start timer 0. |
| 112 | TMIF00 = 0; // Clear INTM00 interrupt flag. |
| 113 | } |
| 114 | |
| 115 | /***************************************************************************//** |
| 116 | * @brief Stops the measurement process when the functions is called. |
| 117 | * |
| 118 | * @return Time(in microseconds) elapsed since the measurement began. |
| 119 | *******************************************************************************/ |
| 120 | unsigned long TIME_StopMeasure(void) |
| 121 | { |
| 122 | unsigned long elapsedUs = 0; |
| 123 | unsigned short timerCnt = 0; |
| 124 | |
| 125 | timerCnt = TCR00; |
| 126 | TT0 |= 0x0001; // Stop Timer 0. |
| 127 | TMIF00 = 0; // Clear INTM00 interrupt flag. |
| 128 | overflowCounter = 0; |
| 129 | elapsedUs = ((unsigned long)overflowCounter * TIMER_CNT_INTERVAL) + |
| 130 | (0xFFFF - timerCnt); |
| 131 | |
| 132 | return elapsedUs; |
| 133 | } |
| 134 | |
| 135 | /***************************************************************************//** |
| 136 | * @brief Creates a delay of microseconds. |
| 137 | * |
| 138 | * @return None. |
| 139 | *******************************************************************************/ |
| 140 | void TIME_DelayUs(unsigned short usUnits) |
| 141 | { |
| 142 | TDR00 = usUnits; |
| 143 | TS0 |= 0x0001; // Start timer 0. |
| 144 | while(!overflowCounter) |
| 145 | { |
| 146 | ; |
| 147 | } |
| 148 | overflowCounter = 0; |
| 149 | TT0 |= 0x0001; // Stop Timer 0. |
| 150 | } |
| 151 | |
| 152 | /***************************************************************************//** |
| 153 | * @brief Creates a delay of milliseconds. |
| 154 | * |
| 155 | * @return None. |
| 156 | *******************************************************************************/ |
| 157 | void TIME_DelayMs(unsigned short msUnits) |
| 158 | { |
| 159 | TDR00 = 0x3E7; // 0x3E7 = 999(decimal). Timer will count down to 0. |
| 160 | TS0 |= 0x0001; // Start timer 0. |
| 161 | while(overflowCounter < msUnits) |
| 162 | { |
| 163 | ; |
| 164 | } |
| 165 | overflowCounter = 0; |
| 166 | TT0 |= 0x0001; // Stop Timer 0. |
| 167 | } |