ESP8266 ILI9341 display support code with printf sources, wire-frame viewer and custom fonts  1.0
ESP8266ILI9341DisplayProject
rtc.c
Go to the documentation of this file.
1 
13 #include "user_config.h"
14 #include "time.h"
15 
16 #include "rtc.h"
17 
18 #ifdef RTC_DEBUG
19 #include "mathio.h"
20 #endif
21 
22 #include <stdlib.h>
23 #include <string.h>
24 
25 int rtc_ok = 0;
26 
27 
37 {
38  return( ( (data/10U) << 4 ) | (data%10U) );
39 }
40 
41 
49 {
50  return ( ( ( (data&0xf0U) >> 4) * 10 ) + (data&0x0fU) );
51 }
52 
53 
54 
64 {
65  uint8_t b = 0;
66 
67  if ( !I2C_read(DS1307, 0, 1, &b, 1) )
68  {
69  I2C_display_error();
70  return (-1);
71  }
72  if(b & 8)
73  return 0;
74  return 1;
75 }
76 
77 
87 int rtc_run(int run)
88 {
89  uint8_t b = 0;
90 
91  if ( !I2C_read(DS1307, 0, 1, &b, 1) )
92  {
93  I2C_display_error();
94  return -1;
95  }
96 
97  if(run == -1)
98  return ((b & 0x80) ? 0 : 1);
99 
100  b = ( b & 0x7f) | (run ? 0 : 0x80);
101 
102  if ( !I2C_write(DS1307, 0, 1, &b, 1) )
103  {
104  I2C_display_error();
105  return -1;
106  }
107 
108  return(run);
109 }
110 
111 
120 MEMSPACE
122 {
123  uint8_t buf[8]; /* RTC R/W buffer */
124  uint8_t addr;
125  int8_t state;
126 
127  tm_t *tmp;
128 
129  I2C_init(50000UL, 20000UL); // 50KHZ 20mS ttimeout
130 
131  if(!force)
132  {
133  state = rtc_run(-1);
134  if(state < 0)
135  {
136  rtc_ok = 0;
137  return 0;
138  }
139  if(state == 0) // stopped
140  force = 1;
141  }
142 
143  if(force) // INIT
144  {
145  if(rtc_run(0) < 0) // STOP RTC
146  {
147  rtc_ok = 0; // Fail
148  return 0;
149  }
150  tmp = gmtime(&seconds);
151  if(!rtc_write(tmp))
152  {
153 #ifdef RTC_DEBUG
154  printf("rtc _write epoch failed\n");
155 #endif
156  rtc_ok = 0;
157  return 0;
158  }
159 
160  memset(buf, 0, 8);
161  for (addr = 8; addr < 0x3f; addr += 8)
162  {
163  if ( !I2C_write(DS1307, addr, 1, buf, 8) )
164  {
165  I2C_display_error();
166  return -1;
167  }
168  }
169 
170  if(rtc_run(1) < 0) // START RTC
171  {
172  rtc_ok = 0; // Fail
173  return 0;
174  }
175  }
176  rtc_ok = 1;
177  return 1;
178 }
179 
180 
181 
188 MEMSPACE
190 {
191  uint8_t buf[8];
192 
193  buf[0] = BINtoBCD(t->tm_sec) & 0x7f;
194  buf[1] = BINtoBCD(t->tm_min) & 0x7f;
195  buf[2] = BINtoBCD(t->tm_hour) & 0x3f;
196  buf[3] = ((t->tm_wday & 7) + 1) & 0x0f;
197  buf[4] = BINtoBCD(t->tm_mday ) & 0x3f;
198  buf[5] = BINtoBCD(t->tm_mon + 1) & 0x1f;
199  buf[6] = BINtoBCD(t->tm_year - 100) & 0xff; // 2000 = 0
200  buf[7] = 0x93; // 32khz, out square wave
201 
202 #ifdef RTC_DEBUG
203  printf("rtc_write():%d, day:%d,mon:%d,hour:%d,min:%d,sec:%d, wday:%d\n",
204  t->tm_year + 1900,
205  t->tm_mon,
206  t->tm_mday,
207  t->tm_hour,
208  t->tm_min,
209  t->tm_sec,
210  t->tm_wday);
211 #endif
212 
213 #ifdef RTC_DEBUG
214  printf("%4x\n", t);
215  int i;
216  printf("rtc_write buf: ");
217  for(i=0;i<7;++i)
218  printf("%02x ", 0xff & buf[i]);
219  printf("\n");
220 #endif
221 
222  if ( !I2C_write(DS1307, 0, 1, buf, 8) )
223  {
224  I2C_display_error();
225  return -1;
226  }
227  return(1);
228 }
229 
230 
231 
237 MEMSPACE
239 {
240  uint8_t buf[8];
241 
242  if ( !I2C_read(DS1307, 0, 1, buf, 8) )
243  {
244  I2C_display_error();
245  return (0);
246  }
247 #ifdef RTC_DEBUG
248  printf("%4x\n", t);
249  int i;
250  printf("rtc_read buf: ");
251  for(i=0;i<7;++i)
252  printf("%02x ", 0xff & buf[i]);
253  printf("\n");
254 #endif
255 
256  t->tm_sec = BCDtoBIN( buf[0] & 0x7f);
257  t->tm_min = BCDtoBIN( buf[1] & 0x7f);
258  t->tm_hour = BCDtoBIN( buf[2] & 0x3f);
259  t->tm_wday = ( buf[3] & 0x07) - 1;
260  t->tm_mday = BCDtoBIN( buf[4] & 0x3f) ;
261  t->tm_mon= BCDtoBIN( buf[5] & 0x1f) - 1;
262  t->tm_year = BCDtoBIN( buf[6] & 0xff) + 100;
263 
264 #ifdef RTC_DEBUG
265  printf("rtc_read():%d, day:%d,mon:%d,hour:%d,min:%d,sec:%d, wday:%d\n",
266  t->tm_year + 1900,
267  t->tm_mon,
268  t->tm_mday,
269  t->tm_hour,
270  t->tm_min,
271  t->tm_sec,
272  t->tm_wday);
273 #endif
274 
275  return 1;
276 }
277 
278 
294 
295 
298 
299 #if 0
300 MEMSPACE
307 {
308  uint32_t fat;
309 /* Pack date and time into a uint32_t variable */
310  fat = ((uint32_t)(t->tm_year - 80) << 25)
311  | (((uint32_t)t->tm_mon+1) << 21)
312  | (((uint32_t)t->tm_mday) << 16)
313  | ((uint32_t)t->tm_hour << 11)
314  | ((uint32_t)t->tm_min << 5)
315  | ((uint32_t)t->tm_sec >> 1);
316  return(fat);
317 }
318 #endif
319 
320 
321 #if 0
322 MEMSPACE
327 uint32_t get_fattime (void)
328 {
329  tm_t t;
330 
331 /* Get local time */
332  if(!rtc_read(&t))
333  return(0);
334  return( tm_to_fat(&t) );
335 }
336 #endif
MEMSPACE DWORD get_fattime(void)
Read time and convert to FAT32 time.
Definition: fatfs_sup.c:130
int tm_min
Definition: time.h:44
Master include file for project Includes all project includes and defines here.
Common Linux/POSIX time functions.
MEMSPACE uint8_t rtc_read(tm_t *t)
Read DS1307 RTC into POSIX struct tm * structure.
Definition: rtc.c:238
int tm_mday
Definition: time.h:46
MEMSPACE uint8_t BINtoBCD(uint8_t data)
Convert number >= 0 and <= 99 to BCD.
Definition: rtc.c:36
int tm_year
Definition: time.h:48
unsigned int uint32_t
Definition: send.c:19
#define DS1307
Definition: rtc.h:16
uint32_t time_t
type of EPOCH result.
Definition: time.h:35
int tm_mon
Definition: time.h:47
MEMSPACE int8_t rtc_run_test()
Check if the DS1307 device is running.
Definition: rtc.c:63
MEMSPACE uint8_t rtc_write(tm_t *t)
Set DS1307 RTC from POSIX struct tm * structure.
Definition: rtc.c:189
signed char int8_t
MEMSPACE uint8_t rtc_init(int force, time_t seconds)
Initialize DS1307 rtc if not initialied - or if forced.
Definition: rtc.c:121
Math IO functions, and verious conversion code with floating point support.
MEMSPACE tm_t * gmtime(time_t *tp)
Convert epoch GMT time_t *tp into POSIX static tm_t *t.
Definition: time.c:471
int rtc_ok
Definition: rtc.c:25
MEMSPACE uint8_t BCDtoBIN(uint8_t data)
Convert two "digit" BCD number to binary.
Definition: rtc.c:48
POSIX struct tm.
Definition: time.h:41
MEMSPACE int rtc_run(int run)
Set DS1307 run state.
Definition: rtc.c:87
int tm_wday
Definition: time.h:49
#define MEMSPACE
Definition: cpu.h:25
MEMSPACE int printf(const char *format,...)
int tm_sec
Definition: time.h:43
int tm_hour
Definition: time.h:45
unsigned char uint8_t
Definition: send.c:17
time_t seconds
Definition: user_main.c:293
MEMSPACE uint32_t tm_to_fat(tm_t *t)
FAT time structer reference.
Definition: fatfs_sup.c:113