| 1 | /***************************************************************************//** |
| 2 | * @file TIME.c |
| 3 | * @brief Implementation of TIME Driver for PIC32MX320F128H Processor. |
| 4 | * @author DNechita ([email protected]) |
| 5 | ******************************************************************************** |
| 6 | * Copyright 2013(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 | |
| 41 | /******************************************************************************/ |
| 42 | /***************************** Include Files **********************************/ |
| 43 | /******************************************************************************/ |
| 44 | #include <xc.h> |
| 45 | #include "TIME.h" |
| 46 | |
| 47 | /******************************************************************************/ |
| 48 | /************************ Variables Declarations ******************************/ |
| 49 | /******************************************************************************/ |
| 50 | static unsigned short overflowCounter = 0; |
| 51 | |
| 52 | /******************************************************************************/ |
| 53 | /************************ Functions Definitions *******************************/ |
| 54 | /******************************************************************************/ |
| 55 | |
| 56 | /***************************************************************************//** |
| 57 | * @brief Interrupt service routine. |
| 58 | * |
| 59 | * @return None. |
| 60 | *******************************************************************************/ |
| 61 | void TIME_ISR(void) |
| 62 | { |
| 63 | overflowCounter++; |
| 64 | } |
| 65 | |
| 66 | /***************************************************************************//** |
| 67 | * @brief Initializes the timer used in this driver. |
| 68 | * |
| 69 | * @return status - Result of the initialization procedure. |
| 70 | * Example: -1 - if initialization was unsuccessful; |
| 71 | * 0 - if initialization was successful. |
| 72 | *******************************************************************************/ |
| 73 | char TIME_Init(void) |
| 74 | { |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | /***************************************************************************//** |
| 79 | * @brief The timer begins to count in steps of microseconds(us) until the user |
| 80 | * calls a stop measurement function. |
| 81 | * |
| 82 | * @return None. |
| 83 | *******************************************************************************/ |
| 84 | void TIME_StartMeasure(void) |
| 85 | { |
| 86 | _CP0_SET_COUNT(0); |
| 87 | } |
| 88 | |
| 89 | /***************************************************************************//** |
| 90 | * @brief Stops the measurement process when the functions is called. |
| 91 | * |
| 92 | * @return Time(in microseconds) elapsed since the measurement began. |
| 93 | *******************************************************************************/ |
| 94 | unsigned long TIME_StopMeasure(void) |
| 95 | { |
| 96 | return (_CP0_GET_COUNT() / 40); |
| 97 | } |
| 98 | |
| 99 | /***************************************************************************//** |
| 100 | * @brief Creates a delay of microseconds. |
| 101 | * |
| 102 | * @return None. |
| 103 | *******************************************************************************/ |
| 104 | void TIME_DelayUs(unsigned short usUnits) |
| 105 | { |
| 106 | unsigned long ticks = 0; |
| 107 | |
| 108 | _CP0_SET_COUNT(0); |
| 109 | while(ticks < TICKS_FOR_1US * usUnits) |
| 110 | { |
| 111 | ticks = _CP0_GET_COUNT(); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /***************************************************************************//** |
| 116 | * @brief Creates a delay of milliseconds. |
| 117 | * |
| 118 | * @return None. |
| 119 | *******************************************************************************/ |
| 120 | void TIME_DelayMs(unsigned short msUnits) |
| 121 | { |
| 122 | unsigned long ticks = 0; |
| 123 | |
| 124 | _CP0_SET_COUNT(0); |
| 125 | while(ticks < TICKS_FOR_1MS * msUnits) |
| 126 | { |
| 127 | ticks = _CP0_GET_COUNT(); |
| 128 | } |
| 129 | } |