##// END OF EJS Templates
update de firmware timegine. RTC terminado
aaguilar -
r196:197
parent child
Show More
@@ -0,0 +1,101
1 /*
2 * fixpoint<T>.h
3 *
4 * Created on: Feb 24, 2015
5 * Author: aras
6 */
7
8 #ifndef FIXPOINT_H_
9 #define FIXPOINT_H_
10
11 #include <inttypes.h>
12 #include <math.h>
13 #include <typeinfo>
14 #include <cstdio>
15
16 typedef fixpoint<int32_t> fix19_12;
17 #define N43_20 fixpoint<int64_t>
18 #define N9_23 fixpoint<int32_t>
19
20 #define FAC64 20
21 #define FAC32 12
22
23 template<class T>
24 class fixpoint {
25
26 private:
27 uint8_t factor;
28 T value;
29
30 public:
31 fixpoint<T>();
32 fixpoint<T>(T num);
33
34 void set_from_float(float num){value=T(num*(1<<factor));}
35 void set(T val){value = val;}
36 float tofloat();
37
38
39 friend inline fixpoint<T> operator-(const fixpoint<T> &num1, const fixpoint<T> &num2){
40 return fixpoint<T>(num1.value-num2.value);
41 }
42 friend inline fixpoint<T> operator+(const fixpoint<T> &num1, const fixpoint<T> &num2){
43 return fixpoint<T>(num1.value+num2.value);
44 }
45 friend inline fixpoint<T> operator*(const fixpoint<T> &num1, const fixpoint<T> &num2){
46 int64_t aux = (int64_t(num1.value)*int64_t(num2.value))>>num1.factor;
47 return fixpoint<T>(aux);
48 }
49 friend inline fixpoint<T> operator/(const fixpoint<T> &num1, const fixpoint<T> &num2){
50 int64_t aux = (int64_t(num1.value)<<num1.factor);
51 return fixpoint<T>(aux/num2.value);
52 }
53
54
55 inline fixpoint<T> power();
56 inline fixpoint<T> sqroot();
57 };
58
59 template<class T>
60 fixpoint<T>::fixpoint(){
61 if(typeid(T)==typeid(int32_t)){
62 factor=FAC32;
63 }
64 else{
65 factor=FAC64;
66 }
67 value=0;
68 }
69
70 template<class T>
71 fixpoint<T>::fixpoint(T num){
72 if(typeid(T)==typeid(int32_t)){
73 factor=FAC32;
74 }
75 else{
76 factor=FAC64;
77 }
78 value=num;
79 }
80
81
82 template<class T>
83 float fixpoint<T>::tofloat(){
84 return float(value)/(1<<factor);
85 }
86
87 template<class T>
88 fixpoint<T> fixpoint<T>::power(){
89 int64_t aux = (int64_t(value)*int64_t(value))>>factor;
90 return fixpoint<T>(aux);
91 }
92
93 template<class T>
94 fixpoint<T> fixpoint<T>::sqroot(){
95 T aux = sqrt(value);
96 return fixpoint<T>(aux<<(factor/2));
97 }
98
99
100
101 #endif /* FIXPOINT_H_ */
@@ -9,27 +9,25
9 9
10 10 namespace te_gnss {
11 11
12 bool configure(gnss_config_struct* cs){
12 bool te_gnss_configure(gnss_config_struct &gcs){
13 13 return true;
14 14 }
15 15
16 bool activate(){
16 bool te_gnss_activate(){
17 17 return true;
18 18 }
19 19
20 bool deactivate(){
20 bool te_gnss_deactivate(){
21 21 return true;
22 22 }
23 23
24 bool sync_pps(){
24 bool te_gnss_sync_pps(){
25 25 return true;
26 26 }
27 27
28 bool get_time_info(gnss_precise_time* pt){
29 return true;
30 }
31 28
32 bool is_locked(){
29
30 bool te_gnss_is_locked(){
33 31 return true;
34 32 }
35 33 }
@@ -10,19 +10,30
10 10
11 11 #include "te_structs.h"
12 12
13 namespace te_gnss {
13
14 14 //pin ports to GNSS
15 15 #define PPS 2 /*!< PE4 AVR <> 2 Arduino */
16 16 #define ENB 5 /*!< PE3 AVR <> 5 Arduino */
17 17 #define RX0_GNSS RX0
18 18 #define TX0_GNSS TX0 PE1
19 19
20 bool configure(gnss_config_struct*);
21 bool activate();
22 bool deactivate();
23 bool sync_pps();
24 bool get_time_info(gnss_precise_time*);
25 bool is_locked();
20 bool te_gnss_configure(gnss_config_struct &gcs);
21 bool te_gnss_activate();
22 bool te_gnss_deactivate();
23 bool te_gnss_sync_pps();
26 24
27 }
25 void te_gnss_set_baudrate(uint16_t baud);
26 void te_gnss_scan_baudrate();
27 void te_gnss_scan_baud_func();
28
29
30 void te_gnss_wait_to_be_locked();
31 void te_gnss_scan_lock_func();
32 bool te_gnss_is_locked();
33
34 void te_gnss_get_pps_data();
35 void te_gnss_get_pps_data_funct();
36
37 void te_gnss_set_position(N9_22);
38
28 39 #endif /* LIBTE_GNSS_H_ */
@@ -8,17 +8,18
8 8 #include <rtc1308.h>
9 9 #include <Wire.h>
10 10
11 namespace te_rtc{
12 11
13 12 /*!
14 13 * \fn initialization
15 14 * \brief Inicializacion del modulo que controlara al RTC DS1308.
16 15 */
17 void initialization(void) {
16 bool te_rtc_initialization(void) {
18 17
19 18 pinMode(CLKIN,OUTPUT);
20 19 attachInterrupt(PPS,ISR_gnss_pps, RISING);
21 20 }
21
22
22 23
23 24 /*!
24 25 * \fn configure
@@ -26,7 +27,7
26 27 * \param rtc estructura que contien informacion de configuracion.
27 28 * \see rtc_config_struct
28 29 */
29 void configure(rtc_config_struct &rtc) {
30 bool te_rtc_configure(rtc_config_struct &rtc) {
30 31
31 32 uint8_t *ctl_byte = (uint8_t*)&rtc.ctl_byte;
32 33 uint8_t *clk_halt = (uint8_t*)&rtc.clk_halt;
@@ -42,7 +43,21
42 43 Wire.write(*ctl_byte);
43 44 //Wire.write(1<<ECLK|0<<OSF|0<<LOS|0<<BBCLK|0<<RS1|0<<RS0);
44 45 Wire.endTransmission();
46 return true;
45 47 }
48
49
50 /*!
51 * \fn activate_pps
52 * \brief Activa la generacion de una senal de 1Hz (PPS) desde el RTC DS1308. Este
53 * PPS debe estar sincronizado con el PPS del GNSS.
54 */
55 bool te_rtc_activate_pps(bool actv) {
56 uint8_t value;
57 value = actv?(1<<ECLK):0x00;
58 return te_rtc_write_reg(DS1308_CTL_ADD,&value,1)==0;
59 }
60
46 61
47 62 /*!
48 63 * \fn set_time
@@ -50,41 +65,29
50 65 * \param tm estructura que contien informacion de tiempo.
51 66 * \see tmElements_t
52 67 */
53 void set_time(tmElements_t &tm) {
54
55 Wire.beginTransmission(DS1308_CTRL_ID);
56 // start with Clock Control and the Seconds register
57 Wire.write(DS1308_SEC_ADD);
58 Wire.write((uint8_t)0x80); // Set clock control bit to 1. This stops the clock
59 Wire.write(dec2bcd(tm.Minute));
60 Wire.write(dec2bcd(tm.Hour)); // sets 24 hour format
61 Wire.write(dec2bcd(tm.Wday));
62 Wire.write(dec2bcd(tm.Day));
63 Wire.write(dec2bcd(tm.Month));
64 Wire.write(dec2bcd(tmYearToY2k(tm.Year)));
65 Wire.endTransmission();
66
67 Wire.beginTransmission(DS1308_CTRL_ID);
68 // Address the Clock Control and the Seconds register
69 Wire.write(DS1308_SEC_ADD);
70 // Set clock control bit to 0 and number of seconds. This starts the clock
71 Wire.write(dec2bcd(tm.Second));
72 Wire.endTransmission();
68 bool te_rtc_set_date_time(gnss_precise_time &gt) {
69 uint8_t ptrvalues[7];
70 ptrvalues[0] = dec2bcd(gt.sec);
71 ptrvalues[1] = dec2bcd(gt.minu);
72 ptrvalues[2] = dec2bcd(gt.hour);
73 ptrvalues[3] = dec2bcd(0);
74 ptrvalues[4] = dec2bcd(gt.day);
75 ptrvalues[5] = dec2bcd(gt.month);
76 ptrvalues[6] = dec2bcd(tmYearToY2k(gt.year-2000));
77 return te_rtc_write_reg(DS1308_SEC_ADD, ptrvalues, 7)==0;
73 78 }
74 79
75 /*!
76 * \fn activate_pps
77 * \brief Activa la generacion de una senal de 1Hz (PPS) desde el RTC DS1308. Este
78 * PPS debe estar sincronizado con el PPS del GNSS.
79 */
80 void activate_pps(void) {
81 Wire.beginTransmission(DS1308_CTRL_ID);
82 Wire.write(DS1308_CTL_ADD);
83 // CLKIN com 1Hz de salida (1PPS)
84 Wire.write(0<<ECLK|0<<OSF|1<<SQWE|0<<LOS|0<<BBCLK|0<<RS1|0<<RS0);
85 Wire.endTransmission();
80 bool te_rtc_read_date_time(gnss_precise_time &gt){
81 uint8_t ptrvalues[7];
82 bool ret =te_rtc_read_reg(DS1308_SEC_ADD, ptrvalues, 7)==7;
83 gt.sec = bcd2dec(ptrvalues[0]);
84 gt.minu = bcd2dec(ptrvalues[1]);
85 gt.hour = bcd2dec(ptrvalues[2]);
86 gt.day = bcd2dec(ptrvalues[4]);
87 gt.month = bcd2dec(ptrvalues[5]);
88 gt.year = bcd2dec(y2kYearToTm(ptrvalues[6]))+2000;
89 return ret;
86 90 }
87
88 91 ///////////////////////////////////////////////////////////////////////////////
89 92
90 93
@@ -102,15 +105,15
102 105 * 3 error: data send, nack received
103 106 * 4 other twi error
104 107 */
105 uint8_t read_ram(uint8_t regaddr, uint8_t *pbuf, uint8_t cnt) {
108 uint8_t te_rtc_read_reg(uint8_t regaddr, uint8_t *pbuf, uint8_t cnt) {
106 109
107 110 uint8_t nread=0;
108 while( (cnt > 0) && ( regaddr < DS1308_RAM_REGS ) ) {
111 while(cnt > 0 ) {
109 112 Wire.beginTransmission(DS1308_CTRL_ID);
110 Wire.write(regaddr+DS1308_RAM_BASE);
113 Wire.write(regaddr);
111 114 Wire.endTransmission();
112 115 Wire.requestFrom((uint8_t)DS1308_CTRL_ID, cnt);
113 while(Wire.available() && (cnt > 0) && (regaddr < DS1308_RAM_REGS) ) {
116 while(Wire.available() && (cnt > 0) ) {
114 117 pbuf[nread] = Wire.read();
115 118 nread++; cnt--; regaddr++;
116 119 } /* while */
@@ -118,29 +121,7
118 121 return (nread);
119 122 }
120 123
121 /*!
122 * \fn write_ram
123 * \brief Escribe un valor en la memoria RAM interna del RTC DS1308, en la
124 * direccion especificada.
125 * \param addr direccion relativa a la RAM (0 es la primera) del registro
126 * en DS1308.
127 * \param val valor que se desea escribir en el registro.
128 * \return estatus
129 * 0 success
130 * 2 error: address send, nack received
131 * 3 error: data send, nack received
132 * 4 other twi error
133 */
134 uint8_t write_ram(uint8_t regaddr, uint8_t val) {
135 124
136 uint8_t rc=0;
137 if (regaddr >= DS1308_RAM_REGS) return (DS1308_RAM_REGS); // 0 thru 51
138 Wire.beginTransmission(DS1308_CTRL_ID);
139 Wire.write(regaddr+DS1308_RAM_BASE);
140 Wire.write(val);
141 rc = Wire.endTransmission();
142 return(rc);
143 }
144 125
145 126 /*!
146 127 * \fn write_ram
@@ -156,22 +137,19
156 137 * 3 error: data send, nack received
157 138 * 4 other twi error
158 139 */
159 uint8_t write_ram(uint8_t regaddr, const uint8_t *pbuf,uint8_t cnt) {
160
140 uint8_t te_rtc_write_reg(uint8_t regaddr,uint8_t *pbuf,uint8_t cnt) {
161 141 uint8_t nbytes, rc=0;
162 if (regaddr >= DS1308_RAM_REGS) return (DS1308_RAM_REGS); // 0 thru 55
163 if (regaddr+cnt > DS1308_RAM_REGS) cnt = DS1308_RAM_REGS-regaddr;
164 142 while (cnt > 0) {
165 nbytes = min(cnt, BUFFER_LENGTH-1);
143 nbytes = cnt;
166 144 Wire.beginTransmission(DS1308_CTRL_ID);
167 Wire.write(regaddr+DS1308_RAM_BASE);
145 Wire.write(regaddr);
168 146 Wire.write((uint8_t*)pbuf, nbytes);
169 147 rc = Wire.endTransmission();
170 148 cnt -=nbytes;
171 149 regaddr+=nbytes;
172 150 pbuf +=nbytes;
173 151 }
174 return(rc);
152 return rc;
175 153 }
176 154
177 155
@@ -180,5 +158,6
180 158 return val + 6 * (val / 10);
181 159 }
182 160
183
184 }// end namespace rtc
161 inline uint8_t bcd2dec(uint8_t val){
162 return (val>>4)*10 + (val&0x0f);
163 }
@@ -15,8 +15,6
15 15 #include <te_structs.h>
16 16
17 17 extern void ISR_gnss_pps(void);
18
19 namespace te_rtc{
20 18
21 19 #define DS1308_CTRL_ID 0x68
22 20 #define DS1308_RAM_BASE 0x08 /*!< Direccion base de la memoria RAM */
@@ -46,20 +44,21
46 44 #define CLKIN 38 /*!< internal PPS output PD7 AVR <> 38 ARDUINO MEGA */
47 45 #define PPS 0 /*|< GNSS receptor PPS signal interrupt int0, port 2 (Arduino i/o), pe4 (avr i/o) */
48 46
47 uint8_t te_rtc_write_reg(uint8_t regaddr, uint8_t *ptrvalues, uint8_t nregs);
48 uint8_t te_rtc_read_reg(uint8_t regaddr, uint8_t *ptrvalues, uint8_t nregs);
49 49
50 void initialization(void);
51 void configure(rtc_config_struct &rtc);
52 void set_time(tmElements_t &tm);
53 void activate_pps(void);
54 50
55 uint8_t read_ram(uint8_t regaddr, uint8_t *pbuf, uint8_t cnt);
56 uint8_t write_ram(uint8_t regaddr, uint8_t val);
57 uint8_t write_ram(uint8_t regaddr, const uint8_t *pbuf,uint8_t cnt);
58
59 inline uint8_t dec2bcd(uint8_t val);
51 bool te_rtc_initialization(void);
52 bool te_rtc_configure(rtc_config_struct &rtc);
53 bool te_rtc_activate_pps(bool actv);
54 bool te_rtc_set_date_time(gnss_precise_time &gt);
55 bool te_rtc_read_date_time(gnss_precise_time &gt);
60 56
61 57
62 58
63 }
59
60 // TODO a utils
61 inline uint8_t dec2bcd(uint8_t val);
62 inline uint8_t bcd2dec(uint8_t val);
64 63
65 64 #endif /* RTC1308_H_ */
@@ -15,6 +15,6
15 15 //pin ports to system
16 16 #define LOCK 42 /*!< GNSS receptor LOCK signal PL7 AVR <> 42 ARDUINO MEGA */
17 17
18
18 //
19 19
20 20 #endif /* TE2SAIG_H_ */
General Comments 0
You need to be logged in to leave comments. Login now