tmr.h

00001 /*
00002  * Copyright (c) 2010, Mariano Alvira <mar@devl.org> and other contributors
00003  * to the MC1322x project (http://mc1322x.devl.org)
00004  * All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice, this list of conditions and the following disclaimer.
00011  * 2. Redistributions in binary form must reproduce the above copyright
00012  *    notice, this list of conditions and the following disclaimer in the
00013  *    documentation and/or other materials provided with the distribution.
00014  * 3. Neither the name of the Institute nor the names of its contributors
00015  *    may be used to endorse or promote products derived from this software
00016  *    without specific prior written permission.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
00019  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
00022  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00023  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00024  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00025  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00026  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00027  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00028  * SUCH DAMAGE.
00029  *
00030  * This file is part of libmc1322x: see http://mc1322x.devl.org
00031  * for details. 
00032  *
00033  *
00034  */
00035 
00036 #ifndef TMR_H
00037 #define TMR_H
00038 
00039 #include "utils.h"
00040 
00041 /* Timer registers are all 16-bit wide with 16-bit access only */
00042 #define TMR_OFFSET      (0x20)
00043 #define TMR_BASE        (0x80007000)
00044 #define TMR0_BASE       (TMR_BASE)
00045 #define TMR1_BASE       (TMR_BASE + TMR_OFFSET*1)
00046 #define TMR2_BASE       (TMR_BASE + TMR_OFFSET*2)
00047 #define TMR3_BASE       (TMR_BASE + TMR_OFFSET*3)
00048 
00049 /* Structure-based register definitions */
00050 /* Example use:
00051         TMR2->CTRL = 0x1234;
00052         TMR2->CTRLbits = (struct TMR_CTRL) {
00053                 .DIR = 1,
00054                 .OUTPUT_MODE = 2,
00055         };
00056         TMR2->CTRLbits.PRIMARY_CNT_SOURCE = 3;
00057 */
00058 
00059 struct TMR_struct {
00060         uint16_t COMP1;
00061         uint16_t COMP2;
00062         uint16_t CAPT;
00063         uint16_t LOAD;
00064         uint16_t HOLD;
00065         uint16_t CNTR;
00066         union {
00067                 uint16_t CTRL;
00068                 struct TMR_CTRL {
00069                         uint16_t OUTPUT_MODE:3;
00070                         uint16_t CO_INIT:1;
00071                         uint16_t DIR:1;
00072                         uint16_t LENGTH:1;
00073                         uint16_t ONCE:1;
00074                         uint16_t SECONDARY_CNT_SOURCE:2;
00075                         uint16_t PRIMARY_CNT_SOURCE:4;
00076                         uint16_t COUNT_MODE:3;
00077                 } CTRLbits;
00078         };
00079         union {
00080                 uint16_t SCTRL;
00081                 struct TMR_SCTRL {
00082                         uint16_t OEN:1;
00083                         uint16_t OPS:1;
00084                         uint16_t FORCE:1;
00085                         uint16_t VAL:1;
00086                         uint16_t EEOF:1;
00087                         uint16_t MSTR:1;
00088                         uint16_t CAPTURE_MODE:2;
00089                         uint16_t INPUT:1;
00090                         uint16_t IPS:1;
00091                         uint16_t IEFIE:1;
00092                         uint16_t IEF:1;
00093                         uint16_t TOFIE:1;
00094                         uint16_t TOF:1;
00095                         uint16_t TCFIE:1;
00096                         uint16_t TCF:1;
00097                 } SCTRLbits;
00098         };
00099         uint16_t CMPLD1;
00100         uint16_t CMPLD2;
00101         union {
00102                 uint16_t CSCTRL;
00103                 struct TMR_CSCTRL {
00104                         uint16_t CL1:2;
00105                         uint16_t CL2:2;
00106                         uint16_t TCF1:1;
00107                         uint16_t TCF2:1;
00108                         uint16_t TCF1EN:1;
00109                         uint16_t TCF2EN:1;
00110                         uint16_t :5;
00111                         uint16_t FILT_EN:1;
00112                         uint16_t DBG_EN:2;
00113                 } CSCTRLbits;
00114         };
00115 
00116         uint16_t reserved[4];
00117 
00118         union {
00119                 uint16_t ENBL;
00120                 struct TMR_ENBL {
00121                         union {
00122                                 struct {
00123                                         uint16_t ENBL:4;
00124                                 };
00125                                 struct {
00126                                         uint16_t ENBL3:1;
00127                                         uint16_t ENBL2:1;
00128                                         uint16_t ENBL1:1;
00129                                         uint16_t ENBL0:1;
00130                                 };
00131                         };
00132                         uint16_t :12;
00133                 } ENBLbits;
00134         };
00135 };
00136 
00137 static volatile struct TMR_struct * const TMR0 = (void *) (TMR0_BASE);
00138 static volatile struct TMR_struct * const TMR1 = (void *) (TMR1_BASE);
00139 static volatile struct TMR_struct * const TMR2 = (void *) (TMR2_BASE);
00140 static volatile struct TMR_struct * const TMR3 = (void *) (TMR3_BASE);
00141 
00142 /* Get timer pointer from timer number */
00143 #define TMR_ADDR(x) ((volatile struct TMR_struct *)(((uint32_t)(x) * TMR_OFFSET) + TMR_BASE))
00144 
00145 /* Get timer number from the timer pointer. */
00146 #define TMR_NUM(x) (((uint32_t)(x) - TMR_BASE) / TMR_OFFSET)
00147 
00148 /* Used to compute which enable bit to set for a particular timer, e.g.
00149      TMR0.ENBL |= TMR_ENABLE_BIT(TMR2);
00150    Helpful when you're using macros to define timers
00151 */
00152 #define TMR_ENABLE_BIT(x) (1 << TMR_NUM(x))
00153 
00154 #define TMR0_PIN GPIO_08
00155 #define TMR1_PIN GPIO_09
00156 #define TMR2_PIN GPIO_10
00157 #define TMR3_PIN GPIO_11
00158 
00159 /* Old timer definitions, for compatibility */
00160 #ifndef REG_NO_COMPAT
00161 
00162 #define TMR_REGOFF_COMP1    (0x0)
00163 #define TMR_REGOFF_COMP2    (0x2)
00164 #define TMR_REGOFF_CAPT     (0x4)
00165 #define TMR_REGOFF_LOAD     (0x6)
00166 #define TMR_REGOFF_HOLD     (0x8)
00167 #define TMR_REGOFF_CNTR     (0xa)
00168 #define TMR_REGOFF_CTRL     (0xc)
00169 #define TMR_REGOFF_SCTRL    (0xe)
00170 #define TMR_REGOFF_CMPLD1   (0x10)
00171 #define TMR_REGOFF_CMPLD2   (0x12)
00172 #define TMR_REGOFF_CSCTRL   (0x14)
00173 #define TMR_REGOFF_ENBL     (0x1e)
00174 
00175 /* one enable register to rule them all */
00176 #define TMR_ENBL     ((volatile uint16_t *) (TMR0_BASE + TMR_REGOFF_ENBL))
00177 
00178 /* Timer 0 registers */
00179 #define TMR0_COMP1   ((volatile uint16_t *) (TMR0_BASE + TMR_REGOFF_COMP1))
00180 #define TMR0_COMP_UP TMR0_COMP1
00181 #define TMR0_COMP2   ((volatile uint16_t *) (TMR0_BASE + TMR_REGOFF_COMP2))
00182 #define TMR0_COMP_DOWN TMR0_COMP2
00183 #define TMR0_CAPT    ((volatile uint16_t *) (TMR0_BASE + TMR_REGOFF_CAPT))
00184 #define TMR0_LOAD    ((volatile uint16_t *) (TMR0_BASE + TMR_REGOFF_LOAD))
00185 #define TMR0_HOLD    ((volatile uint16_t *) (TMR0_BASE + TMR_REGOFF_HOLD))
00186 #define TMR0_CNTR    ((volatile uint16_t *) (TMR0_BASE + TMR_REGOFF_CNTR))
00187 #define TMR0_CTRL    ((volatile uint16_t *) (TMR0_BASE + TMR_REGOFF_CTRL))
00188 #define TMR0_SCTRL   ((volatile uint16_t *) (TMR0_BASE + TMR_REGOFF_SCTRL))
00189 #define TMR0_CMPLD1  ((volatile uint16_t *) (TMR0_BASE + TMR_REGOFF_CMPLD1))
00190 #define TMR0_CMPLD2  ((volatile uint16_t *) (TMR0_BASE + TMR_REGOFF_CMPLD2))
00191 #define TMR0_CSCTRL  ((volatile uint16_t *) (TMR0_BASE + TMR_REGOFF_CSCTRL))
00192 
00193 /* Timer 1 registers */
00194 #define TMR1_COMP1   ((volatile uint16_t *) (TMR1_BASE + TMR_REGOFF_COMP1))
00195 #define TMR1_COMP_UP TMR1_COMP1
00196 #define TMR1_COMP2   ((volatile uint16_t *) (TMR1_BASE + TMR_REGOFF_COMP2))
00197 #define TMR1_COMP_DOWN TMR1_COMP2
00198 #define TMR1_CAPT    ((volatile uint16_t *) (TMR1_BASE + TMR_REGOFF_CAPT))
00199 #define TMR1_LOAD    ((volatile uint16_t *) (TMR1_BASE + TMR_REGOFF_LOAD))
00200 #define TMR1_HOLD    ((volatile uint16_t *) (TMR1_BASE + TMR_REGOFF_HOLD))
00201 #define TMR1_CNTR    ((volatile uint16_t *) (TMR1_BASE + TMR_REGOFF_CNTR))
00202 #define TMR1_CTRL    ((volatile uint16_t *) (TMR1_BASE + TMR_REGOFF_CTRL))
00203 #define TMR1_SCTRL   ((volatile uint16_t *) (TMR1_BASE + TMR_REGOFF_SCTRL))
00204 #define TMR1_CMPLD1  ((volatile uint16_t *) (TMR1_BASE + TMR_REGOFF_CMPLD1))
00205 #define TMR1_CMPLD2  ((volatile uint16_t *) (TMR1_BASE + TMR_REGOFF_CMPLD2))
00206 #define TMR1_CSCTRL  ((volatile uint16_t *) (TMR1_BASE + TMR_REGOFF_CSCTRL))
00207 
00208 /* Timer 2 registers */
00209 #define TMR2_COMP1   ((volatile uint16_t *) (TMR2_BASE + TMR_REGOFF_COMP1))
00210 #define TMR2_COMP_UP TMR2_COMP1
00211 #define TMR2_COMP2   ((volatile uint16_t *) (TMR2_BASE + TMR_REGOFF_COMP2))
00212 #define TMR2_COMP_DOWN TMR2_COMP2
00213 #define TMR2_CAPT    ((volatile uint16_t *) (TMR2_BASE + TMR_REGOFF_CAPT))
00214 #define TMR2_LOAD    ((volatile uint16_t *) (TMR2_BASE + TMR_REGOFF_LOAD))
00215 #define TMR2_HOLD    ((volatile uint16_t *) (TMR2_BASE + TMR_REGOFF_HOLD))
00216 #define TMR2_CNTR    ((volatile uint16_t *) (TMR2_BASE + TMR_REGOFF_CNTR))
00217 #define TMR2_CTRL    ((volatile uint16_t *) (TMR2_BASE + TMR_REGOFF_CTRL))
00218 #define TMR2_SCTRL   ((volatile uint16_t *) (TMR2_BASE + TMR_REGOFF_SCTRL))
00219 #define TMR2_CMPLD1  ((volatile uint16_t *) (TMR2_BASE + TMR_REGOFF_CMPLD1))
00220 #define TMR2_CMPLD2  ((volatile uint16_t *) (TMR2_BASE + TMR_REGOFF_CMPLD2))
00221 #define TMR2_CSCTRL  ((volatile uint16_t *) (TMR2_BASE + TMR_REGOFF_CSCTRL))
00222 
00223 /* Timer 3 registers */
00224 #define TMR3_COMP1   ((volatile uint16_t *) (TMR3_BASE + TMR_REGOFF_COMP1))
00225 #define TMR3_COMP_UP TMR3_COMP1
00226 #define TMR3_COMP2   ((volatile uint16_t *) (TMR3_BASE + TMR_REGOFF_COMP2))
00227 #define TMR3_COMP_DOWN TMR3_COMP2
00228 #define TMR3_CAPT    ((volatile uint16_t *) (TMR3_BASE + TMR_REGOFF_CAPT))
00229 #define TMR3_LOAD    ((volatile uint16_t *) (TMR3_BASE + TMR_REGOFF_LOAD))
00230 #define TMR3_HOLD    ((volatile uint16_t *) (TMR3_BASE + TMR_REGOFF_HOLD))
00231 #define TMR3_CNTR    ((volatile uint16_t *) (TMR3_BASE + TMR_REGOFF_CNTR))
00232 #define TMR3_CTRL    ((volatile uint16_t *) (TMR3_BASE + TMR_REGOFF_CTRL))
00233 #define TMR3_SCTRL   ((volatile uint16_t *) (TMR3_BASE + TMR_REGOFF_SCTRL))
00234 #define TMR3_CMPLD1  ((volatile uint16_t *) (TMR3_BASE + TMR_REGOFF_CMPLD1))
00235 #define TMR3_CMPLD2  ((volatile uint16_t *) (TMR3_BASE + TMR_REGOFF_CMPLD2))
00236 #define TMR3_CSCTRL  ((volatile uint16_t *) (TMR3_BASE + TMR_REGOFF_CSCTRL))
00237 
00238 #define TMR(num, reg)  CAT2(TMR,num,_##reg)
00239 
00240 #endif /* REG_NO_COMPAT */
00241 
00242 /* Initialize timer.  This just counts and interrupts, doesn't drive an output.
00243    timer_num = 0, 1, 2, 3
00244    rate = desired rate in Hz,
00245    enable_int = whether to enable an interrupt on every cycle
00246    Returns actual timer rate. */
00247 uint32_t timer_setup_ex(int timer_num, uint32_t rate, int enable_int);
00248 
00249 /* Initialize timer.  This just counts and interrupts, doesn't drive an output.
00250    timer = TMR0, TMR1, TMR2, TMR3
00251    rate = desired rate in Hz,
00252    enable_int = whether to enable an interrupt on every cycle
00253    Returns actual timer rate. */
00254 #define timer_setup(timer,rate,enable_int) timer_setup_ex(TMR_NUM(timer), rate, enable_int)
00255 
00256 #endif

Generated on Mon Apr 11 14:23:38 2011 for Contiki 2.5 by  doxygen 1.6.1