00001 /* 00002 * Copyright Droids Corporation (2008) 00003 * 00004 * This program is free software; you can redistribute it and/or modify 00005 * it under the terms of the GNU General Public License as published by 00006 * the Free Software Foundation; either version 2 of the License, or 00007 * (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software 00016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 * 00018 */ 00019 00020 /* 00021 * Author : Julien LE GUEN - jlg@jleguen.info 00022 */ 00023 #ifndef _TIME_EXT_H_ 00024 #define _TIME_EXT_H 00025 00026 00027 #include <time_ext_config.h> 00028 00029 /* 00030 * Since we want the maximum resolution available, 00031 * the prescaler of timer2 is set to 1. 00032 * Thus counter2 is incremented every positive edge of the external quartz. 00033 * You can define the frequency of your quartz in time_ext_config.h 00034 */ 00035 00036 00037 /* Number of NANOSECONDS */ 00038 #define NANO_PER_US 1000UL 00039 #define NANO_PER_MS 1000000UL 00040 #define NANO_PER_S 1000000000UL 00041 00042 00043 /* Prescaler */ 00044 #define TIMER2_PRESCALER_OFF 0 00045 #define TIMER2_PRESCALER_1 1 00046 #define TIMER2_PRESCALER_8 2 00047 #define TIMER2_PRESCALER_32 3 00048 #define TIMER2_PRESCALER_64 4 00049 #define TIMER2_PRESCALER_128 5 00050 #define TIMER2_PRESCALER_256 6 00051 #define TIMER2_PRESCALER_1024 7 00052 00053 00054 /* Useful defines */ 00055 #define NANO_PER_QUARTZ_TICK TIME_EXT_QUARTZ_PERIOD 00056 00057 00058 00059 00060 /* 00061 * Time structure. 00062 * We want to have the best resolution possible. 00063 * Thus, we store nano-seconds and seconds. 00064 */ 00065 typedef struct _time_ext 00066 { 00067 uint32_t sec; 00068 uint32_t nano; 00069 } time_ext_t; 00070 00071 00072 /* Initialize the module */ 00073 void time_ext_init(void); 00074 00075 /* 00076 * Setter and getter for the prescaler 00077 * By default, it is set to 1 00078 */ 00079 inline void time_ext_set_prescaler(uint8_t p); 00080 inline uint8_t time_ext_get_prescaler(void); 00081 00082 /* 00083 * Get time (seconds only, nanos only, or both) 00084 */ 00085 inline uint32_t time_ext_get_s(void); 00086 inline uint32_t time_ext_get_ns(void); 00087 time_ext_t time_ext_get(void); 00088 00089 /* 00090 * Sets the global time. 00091 * This resets TCNT2 as well 00092 */ 00093 inline void time_ext_set(uint32_t sec, uint32_t nano); 00094 00095 #endif /* _TIME_EXT_H_ */