| 1 | /**************************************************************************//** |
| 2 | * @file timer.h |
| 3 | * @brief Timer header file. |
| 4 | * @author acozma ([email protected]) |
| 5 | * |
| 6 | ******************************************************************************* |
| 7 | * Copyright 2011(c) Analog Devices, Inc. |
| 8 | * |
| 9 | * All rights reserved. |
| 10 | * |
| 11 | * Redistribution and use in source and binary forms, with or without modification, |
| 12 | * are permitted provided that the following conditions are met: |
| 13 | * - Redistributions of source code must retain the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer. |
| 15 | * - Redistributions in binary form must reproduce the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer in |
| 17 | * the documentation and/or other materials provided with the |
| 18 | * distribution. |
| 19 | * - Neither the name of Analog Devices, Inc. nor the names of its |
| 20 | * contributors may be used to endorse or promote products derived |
| 21 | * from this software without specific prior written permission. |
| 22 | * - The use of this software may or may not infringe the patent rights |
| 23 | * of one or more patent holders. This license does not release you |
| 24 | * from the requirement that you obtain separate licenses from these |
| 25 | * patent holders to use this software. |
| 26 | * - Use of the software either in source or binary form, must be run |
| 27 | * on or directly connected to an Analog Devices Inc. component. |
| 28 | * |
| 29 | * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR IMPLIED |
| 30 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY |
| 31 | * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 32 | * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 33 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 34 | * INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 35 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 36 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 37 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 38 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 39 | * |
| 40 | ******************************************************************************* |
| 41 | * SVN Revision: $WCREV$ |
| 42 | ******************************************************************************/ |
| 43 | |
| 44 | #ifndef __TIMER__H__ |
| 45 | #define __TIMER_H__ |
| 46 | |
| 47 | /*****************************************************************************/ |
| 48 | /***************************** Include Files *********************************/ |
| 49 | /*****************************************************************************/ |
| 50 | #include "xil_io.h" |
| 51 | |
| 52 | /*****************************************************************************/ |
| 53 | /******************* Timer Registers Definitions *****************************/ |
| 54 | /*****************************************************************************/ |
| 55 | // Register address |
| 56 | #define TCSR0 0x00 |
| 57 | #define TLR0 0x04 |
| 58 | #define TCR0 0x08 |
| 59 | #define TCSR1 0x10 |
| 60 | #define TLR1 0x14 |
| 61 | #define TCR1 0x18 |
| 62 | |
| 63 | /*****************************************************************************/ |
| 64 | /******************* Timer Registers Bits ************************************/ |
| 65 | /*****************************************************************************/ |
| 66 | // control/status register bits |
| 67 | #define MDT (1 << 0) |
| 68 | #define UDT (1 << 1) |
| 69 | #define GENT (1 << 2) |
| 70 | #define CAPT (1 << 3) |
| 71 | #define ARHT (1 << 4) |
| 72 | #define LOAD (1 << 5) |
| 73 | #define EINT (1 << 6) |
| 74 | #define ENT (1 << 7) |
| 75 | #define TINT (1 << 8) |
| 76 | #define PWMA (1 << 9) |
| 77 | #define ENALL (1 << 10) |
| 78 | #define CASC (1 << 11) |
| 79 | |
| 80 | /*****************************************************************************/ |
| 81 | /******************* Macros and Constants Definitions ************************/ |
| 82 | /*****************************************************************************/ |
| 83 | #define CPU_CYCLE_TIME 10 //CPU cycle time in ns, cycle time = 1/CPU_CLOCK_RATE |
| 84 | |
| 85 | #define TIMER0_RUN(timerBaseAddr, loadVal) \ |
| 86 | Xil_Out32(timerBaseAddr + TLR0, loadVal); \ |
| 87 | Xil_Out32(timerBaseAddr + TCSR0, (UDT | LOAD)); \ |
| 88 | Xil_Out32(timerBaseAddr + TCSR0, (UDT | ENT)); |
| 89 | |
| 90 | #define TIMER0_STOP(timerBaseAddr) \ |
| 91 | Xil_Out32(timerBaseAddr + TCSR0, 0); |
| 92 | |
| 93 | #define TIMER0_GET_COUNTER(timerBaseAddr) \ |
| 94 | Xil_In32(timerBaseAddr + TCR0); |
| 95 | |
| 96 | #define TIMER0_WAIT(timerBaseAddr, ns) \ |
| 97 | Xil_Out32(timerBaseAddr + TLR0, (ns < CPU_CYCLE_TIME ? 1 : ns/CPU_CYCLE_TIME)); \ |
| 98 | Xil_Out32(timerBaseAddr + TCSR0, (UDT | LOAD)); \ |
| 99 | Xil_Out32(timerBaseAddr + TCSR0, (UDT | ENT)); \ |
| 100 | while(Xil_In32(timerBaseAddr + TCR0)!=-1); |
| 101 | |
| 102 | #define TIMER1_RUN(timerBaseAddr, loadVal) \ |
| 103 | Xil_Out32(timerBaseAddr + TLR1, loadVal); \ |
| 104 | Xil_Out32(timerBaseAddr + TCSR1, (UDT | LOAD)); \ |
| 105 | Xil_Out32(timerBaseAddr + TCSR1, (UDT | ENT)); |
| 106 | |
| 107 | #define TIMER1_STOP(timerBaseAddr) \ |
| 108 | Xil_Out32(timerBaseAddr + TCSR1, 0); |
| 109 | |
| 110 | #define TIMER1_GET_COUNTER(timerBaseAddr) \ |
| 111 | Xil_In32(timerBaseAddr + TCR1); |
| 112 | |
| 113 | #define TIMER1_WAIT(timerBaseAddr, ns) \ |
| 114 | Xil_Out32(timerBaseAddr + TLR1, (ns < CPU_CYCLE_TIME ? 1 : ns/CPU_CYCLE_TIME)); \ |
| 115 | Xil_Out32(timerBaseAddr + TCSR1, (UDT | LOAD)); \ |
| 116 | Xil_Out32(timerBaseAddr + TCSR1, (UDT | ENT)); \ |
| 117 | while(Xil_In32(timerBaseAddr + TCR1)!=-1); |
| 118 | |
| 119 | #endif /*__TIMER_H__*/ |
| 120 |