Project

General

Profile

Statistics
| Revision:

root / branches / colonetmk2 / code / projects / libdragonfly / time.c @ 1456

History | View | Annotate | Download (6.95 KB)

1 241 bcoltin
/**
2
 * Copyright (c) 2007 Colony Project
3
 *
4
 * Permission is hereby granted, free of charge, to any person
5
 * obtaining a copy of this software and associated documentation
6
 * files (the "Software"), to deal in the Software without
7
 * restriction, including without limitation the rights to use,
8
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9
 * copies of the Software, and to permit persons to whom the
10
 * Software is furnished to do so, subject to the following
11
 * conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be
14
 * included in all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
 * OTHER DEALINGS IN THE SOFTWARE.
24
 **/
25
26
27
/**
28
 * @file time.c
29
 * @brief Timer code
30
 *
31
 * Implementation of functions for timers.
32
 *
33
 * @author Colony Project, CMU Robotics Club
34
 **/
35
36 8 bcoltin
/*
37 380 jknichel
  time.c
38
  anything that requires a delay
39
  mostly delay_ms
40 8 bcoltin

41 380 jknichel
  author: Robotics Club, Colony Project
42 8 bcoltin

43 380 jknichel
  Change Log:
44
  2.5.07 - Kevin
45
  Aaron fixed the orb/servo code and made them use timer3 but compare registers B and C. He hard set the prescaler
46
  to 8 so the RTC broke. Changed it so that we use a 8 prescaler which sets the compare match at 1/16th of a second.
47
  You now count how many 16ths of a second you want until you trigger your actual interrupt. Works. Changed defines
48
  for time so you can still call rtc_init with a scale but now it is defined in terms of actual time like second, quarter_second
49
  etc. Read that section in the time.h file for more information. Tested and works, though the clock drifts more than
50
  it used to
51
  1.30.07 - Kevin
52
  Modified the clock to run on timer3 on the Dragonfly. Works with decent accuracy. Using a prescaler of 256
53
  the timer counts up to a precomputer value which will trigger an interrupt and reset the timer. Multiples of
54
  256 change it by that multiple. Refer to the time.h file for all possible prescalers.
55
  The interrupt will call a specified function _rtc_func every pulse.
56
  All of it has been tested and it works.
57 8 bcoltin

58
*/
59
#include <avr/interrupt.h>
60
#include <util/delay.h>
61 1456 rcahoon
#include "time.h"
62
63 601 kwoo
64
/* Calculate how many cycles to delay for to get 1 ms. Based on F_CPU which should be defined by the makefile */
65
#ifdef F_CPU
66
#define WAIT_CYCLES ((F_CPU / 1000) / 10)
67
#else
68
#define WAIT_CYCLES (8000 / 10)
69
#endif
70 8 bcoltin
71
static volatile int _rtc_val = 0;
72
static volatile int _rtc_pulse = 0;
73
static volatile int _rtc_scale = 32;        //Defaults to 1 Second per pulse
74
static void (*_rtc_f)(void) = 0;
75 601 kwoo
76
77 8 bcoltin
78
/**
79
 * @defgroup time Time
80
 * @brief Time functions
81
 *
82
 * Functions dealing with time.
83
 *
84
 * @{
85
 **/
86
87
/**
88
 * Delays for the specified number of milliseconds.
89 601 kwoo
 * It depends on F_CPU to be defined in order to calculate how many cycles
90
 * it should delay. If it is not defined, a default clock of 8MHz is assumed.
91
 *
92
 * We use _delay_loop_2 which will run assembly instructions that should be
93
 * 4 cycles long. Optimizations must be enabled for this to be true.
94
 * That function is called to ensure around 1ms per execution. To generate
95
 * multiple ms we run a for loop of how many milliseconds are desired.
96
 *
97
 * The error should be just the skew on the oscillator as the formula to
98
 * calculate delay cycles should always be a whole number. The is some skew
99
 * in practice though it is unavoidable. Delaying for less than 1s should make
100
 * the error negligable.
101 8 bcoltin
 *
102
 * @param ms the number of milliseconds to delay for
103
 **/
104 601 kwoo
void delay_ms(int ms) {
105
    for (; ms > 0; ms--) {
106
        _delay_loop_2(WAIT_CYCLES);
107
    }
108
}
109
110 8 bcoltin
111
/*         Prescales defined in time.h. SECOND will give you 1 second.
112
        More scales are defined in the time.h file.
113
        rtc_func is the address to a function that you want called every clock tick. */
114
/**
115
 * Initializes the real time clock. Prescales are defined in time.h.
116
 * For example, SECOND will give 1 second. The specified function is
117
 * called every clock tick. For the real time clock to activate,
118
 * interrupts must be enabled. (through sei() )
119
 *
120
 * @param prescale_opt the period with which the timer is triggered
121
 * @param rtc_func the function called when the timer is triggered
122
 *
123
 * @see rtc_get, rtc_reset
124
 *
125
 **/
126
void rtc_init(int prescale_opt, void (*rtc_func)(void)) {
127
128 380 jknichel
  //Clear timer register for Timer 3
129
  TCNT3 = 0;
130 8 bcoltin
131 380 jknichel
  /*         This sets the Waveform Generation Module to CTC (Clear Timer on Compare) Mode (100)
132
        See page135 in Atmega128 Docs for more modes and explanations */
133
  TCCR3B |= _BV(WGM32);
134 8 bcoltin
135 380 jknichel
  /*         This sets the prescaler for the system clock (8MHz) ie: divides the clock by some number.
136
        Currently set to a prescaler of 8 because that is what the orb and servos use (they are on this timer as well)
137
        See page137 in Atemga128 Docs for all the available prescalers */
138
  TCCR3B |= _BV(CS31);
139 8 bcoltin
140 380 jknichel
  /*         Sets the two regsiters that we compare against. So the timer counts up to this number and
141
        then resets back to 0 and calls the compare match interrupt.
142
        8x10^6 / 8 = 1/16 Second. All values are based off of this number. Do not change it unless you
143
        are l337*/
144 8 bcoltin
145 380 jknichel
  OCR3A = 0xF424;
146 8 bcoltin
147 380 jknichel
  /*         Enable Output Compare A Interrupt. When OCR3A is met by the timer TCNT3 this interrupt will be
148
        triggerd. (See page140 in Atmega128 Docs for more information */
149
  ETIMSK |= _BV(OCIE3A);
150 8 bcoltin
151 380 jknichel
  /*        Store the pointer to the function to be used in the interrupt */
152
  _rtc_f = rtc_func;
153 8 bcoltin
154 380 jknichel
  /*        Store how many 1/16ths of a second you want to let by before triggering an interrupt */
155
  _rtc_scale = prescale_opt;
156 8 bcoltin
}
157
158
/**
159
 * Returns the time elapsed in seconds since the last call to
160
 * rtc_init or rtc_reset.
161
 *
162
 * @return the number of seconds since the last call to rtc_init or rtc_reset
163
 *
164
 * @see rtc_init, rtc_reset
165
 **/
166 380 jknichel
int rtc_get(void) {
167
  return _rtc_val;
168 8 bcoltin
}
169
170
/**
171
 * Resets the real time clock counter to 0.
172
 *
173
 * @see rtc_init, rtc_get
174
 **/
175 380 jknichel
void rtc_reset(void) {
176
  _rtc_val = 0;
177 8 bcoltin
}
178
179
/** @} **/ //end defgroup
180
181
/*        Called every pulse. Function in _rtc_f is called every _rtc_scale and also the counter is updated.
182
        Bascially, since the pulse is hard set at 1/16s  you want to count how many 16ths of a second have passed
183
        and when it reaches the amount of time you want, execute the code. */
184
SIGNAL(TIMER3_COMPA_vect) {
185
186 380 jknichel
  if (_rtc_pulse ==  _rtc_scale) {
187
    //Increment the real time clock counter
188
    _rtc_val++;
189 8 bcoltin
190 380 jknichel
    //Calls the function tied to the real time clock if defined
191
    if(_rtc_f != 0)
192
      _rtc_f();
193 8 bcoltin
194 380 jknichel
    //Resets the pulse until the next scale is matched
195
    _rtc_pulse = 0;
196
  }
197 8 bcoltin
198 380 jknichel
  //Updates the amount of pulses seen since the last scale match
199
  _rtc_pulse++;
200 8 bcoltin
201
}