Kenji Arai
/
Check_external_RTC
check program for EPSON RX-8025NB and STM M41T62 external RTC library
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 /* 00002 * mbed Application program 00003 * External RTC test program for EPSON RX-8025NB and STM M41T62 00004 * 00005 * Copyright (c) 2020 Kenji Arai / JH1PJL 00006 * http://www7b.biglobe.ne.jp/~kenjia/ 00007 * https://os.mbed.com/users/kenjiArai/ 00008 * Created: August 7th, 2020 00009 * Revised: August 7th, 2020 00010 */ 00011 00012 // tested on Nucleo-L476RG board 00013 00014 // Select target RTC ---------------------------------------------------------- 00015 //#define CHECK_RX8025 00016 #define CHECK_M41T62 00017 00018 // Include -------------------------------------------------------------------- 00019 #include "mbed.h" 00020 #include "redirect_stdio.h" 00021 #if defined(CHECK_RX8025) 00022 # include "RX8025NB.h" 00023 # define RTC_NO 0 00024 #elif defined(CHECK_M41T62) 00025 # include "m41t62_rtc.h" 00026 # define RTC_NO 1 00027 #endif 00028 00029 00030 // Definition ----------------------------------------------------------------- 00031 #define PUSHED_SW 0 // Active low 00032 00033 #define SLEEP_TIME 2 // 2 minuts(minimum setting) 00034 00035 // Object --------------------------------------------------------------------- 00036 DigitalIn userSW(USER_BUTTON); 00037 DigitalOut myled(LED1); // Indicate the sampling period 00038 I2C i2c(D14, D15); 00039 #if defined(CHECK_RX8025) 00040 RX8025 ex_rtc(i2c); // EPSON RX-8025NB 00041 DigitalIn rtc_irq(A5, PullUp); // will change to InterruptIn 00042 #elif defined(CHECK_M41T62) 00043 M41T62 ex_rtc(i2c); // STMicro M41T62 00044 DigitalIn rtc_irq(A4, PullUp); // will change to InterruptIn 00045 #endif 00046 00047 // RAM ------------------------------------------------------------------------ 00048 00049 // ROM / Constant data -------------------------------------------------------- 00050 const char *const msg0 = "Is a time correct? If no, please hit any key. "; 00051 const char *const msg1 = "<Push USER SW then enter the Deep Sleep mode> "; 00052 const char *const rtc_name[2] = {"RX-8025NB", "M41T62"}; 00053 00054 // Function prototypes -------------------------------------------------------- 00055 static void w_check_rtc_time(void); 00056 static void time_enter_mode(void); 00057 static void chk_and_set_time(char *ptr); 00058 static int32_t xatoi(char **str, int32_t *res); 00059 static void get_line(char *buff, int32_t len); 00060 static void rtc_interrut(void); 00061 00062 extern void print_revision(void); 00063 00064 //------------------------------------------------------------------------------ 00065 // Control Program 00066 //------------------------------------------------------------------------------ 00067 int main() 00068 { 00069 char buf[64]; 00070 time_t seconds; 00071 uint8_t wait_counter = 0; 00072 00073 puts("\r\n\r\nTest Nucleo RTC Function."); 00074 printf("External RTC is %s\r\n", rtc_name[RTC_NO]); 00075 print_revision(); 00076 myled = !myled; 00077 ThisThread::sleep_for(100ms); 00078 myled = !myled; 00079 ThisThread::sleep_for(100ms); 00080 // RTC related preparation 00081 //ex_rtc.set_sq_wave(RTC_SQW_NONE); 00082 ex_rtc.clear_IRQ(); 00083 w_check_rtc_time(); 00084 while(true) { 00085 seconds = time(NULL); 00086 strftime(buf, 50, " %B %d,'%y, %H:%M:%S\r\n", localtime(&seconds)); 00087 printf("[Time] %s", buf); 00088 printf("%s", msg0); 00089 printf("%s", msg1); 00090 puts("\r"); 00091 wait_counter = 0; 00092 while (seconds == time(NULL)) { 00093 if (readable() == 1) { // Enter time from PC console 00094 getc(); // dummy read 00095 time_enter_mode(); 00096 } 00097 if (userSW == PUSHED_SW) { // goto sleep and wake up by RTC timer 00098 while (userSW == PUSHED_SW) { 00099 ThisThread::sleep_for(10ms); 00100 } 00101 printf("Please wait %d minutes to wake-up(Reset).", SLEEP_TIME); 00102 puts(" Entered the deep sleep mode. "); 00103 ThisThread::sleep_for(1s); 00104 myled = 0; 00105 #if defined(CHECK_RX8025) 00106 InterruptIn rtc_irq(A5, PullUp); 00107 #elif defined(CHECK_M41T62) 00108 InterruptIn rtc_irq(A4, PullUp); 00109 #endif 00110 rtc_irq.fall(&rtc_interrut); 00111 ex_rtc.set_next_IRQ(SLEEP_TIME); 00112 ThisThread::sleep_for(1s); 00113 DigitalIn dmy0(LED1); 00114 DigitalIn dmy1(USBTX); 00115 DigitalIn dmy2(USBRX); 00116 ThisThread::sleep_for(10000s); // sleep long time 00117 } 00118 ThisThread::sleep_for(50ms); 00119 if (++wait_counter > (2000 / 50)) { 00120 break; 00121 } 00122 } 00123 // delete previous strings 00124 printf("\033[2A"); 00125 puts(""); // null 00126 uint8_t n = strlen(msg0) + strlen(msg1); 00127 memset(buf, ' ', 64); 00128 if (n > 64) { 00129 n -= 64; 00130 puts_wo_cr(buf, 64); 00131 } 00132 if (n > 64) { 00133 puts_wo_cr(buf, 64); 00134 } else { 00135 puts_wo_cr(buf, n); 00136 } 00137 printf("\033[G"); 00138 myled = !myled; 00139 } 00140 } 00141 00142 // Check External RTC data 00143 static void w_check_rtc_time(void) 00144 { 00145 time_t seconds_1st, seconds_2nd, diff; 00146 struct tm t; 00147 00148 for (uint32_t i = 0; i < 5; i++) { 00149 ex_rtc.read_rtc_std(&t); // read External RTC data 00150 printf("Year:%d ",t.tm_year); 00151 printf("Month:%d ",t.tm_mon); 00152 printf("Day:%d ",t.tm_mday); 00153 printf("Hour:%d ",t.tm_hour); 00154 printf("Min:%d ",t.tm_min); 00155 printf("Sec: %d \r\n",t.tm_sec); 00156 seconds_1st = mktime(&t); 00157 ex_rtc.read_rtc_std(&t); // read External RTC data again (make sure) 00158 seconds_2nd = mktime(&t); 00159 diff = seconds_2nd - seconds_1st; 00160 if ((diff == 0) || (diff == 1)) { 00161 break; 00162 } 00163 } 00164 set_time(seconds_2nd); 00165 } 00166 00167 // Interrupt for wake up 00168 static void rtc_interrut(void) 00169 { 00170 system_reset(); // restart from RESET condition 00171 } 00172 00173 // Time adjustment 00174 static void time_enter_mode(void) 00175 { 00176 char *ptr; 00177 char linebuf[64]; 00178 00179 puts("Set time into RTC."); 00180 puts(" e.g. >20 8 1 12 34 56 -> August 01,'20, 12:34:56"); 00181 puts(" If time is fine, just hit enter key."); 00182 putc('>'); 00183 ptr = linebuf; 00184 get_line(ptr, sizeof(linebuf)); 00185 puts("\r"); 00186 chk_and_set_time(ptr); 00187 } 00188 00189 // Change string -> integer 00190 static int32_t xatoi(char **str, int32_t *res) 00191 { 00192 uint32_t val; 00193 uint8_t c, radix, s = 0; 00194 00195 while ((c = **str) == ' ') (*str)++; 00196 if (c == '-') { 00197 s = 1; 00198 c = *(++(*str)); 00199 } 00200 if (c == '0') { 00201 c = *(++(*str)); 00202 if (c <= ' ') { 00203 *res = 0; 00204 return 1; 00205 } 00206 if (c == 'x') { 00207 radix = 16; 00208 c = *(++(*str)); 00209 } else { 00210 if (c == 'b') { 00211 radix = 2; 00212 c = *(++(*str)); 00213 } else { 00214 if ((c >= '0')&&(c <= '9')) { 00215 radix = 8; 00216 } else { 00217 return 0; 00218 } 00219 } 00220 } 00221 } else { 00222 if ((c < '1')||(c > '9')) { 00223 return 0; 00224 } 00225 radix = 10; 00226 } 00227 val = 0; 00228 while (c > ' ') { 00229 if (c >= 'a') c -= 0x20; 00230 c -= '0'; 00231 if (c >= 17) { 00232 c -= 7; 00233 if (c <= 9) return 0; 00234 } 00235 if (c >= radix) return 0; 00236 val = val * radix + c; 00237 c = *(++(*str)); 00238 } 00239 if (s) val = -val; 00240 *res = val; 00241 return 1; 00242 } 00243 00244 // Get key input data 00245 static void get_line(char *buff, int32_t len) 00246 { 00247 char c; 00248 int32_t idx = 0; 00249 00250 for (;;) { 00251 c = getc(); 00252 //printf("0x%x \r\n", c); 00253 if ((c == '\r') || (c == '\n')) { 00254 buff[idx++] = '\r'; 00255 break; 00256 } 00257 if ((c == '\b') && idx) { 00258 idx--; 00259 const char bf_bs[] = 00260 {0x1b, '[', '1', 'D', ' ', 0x1b, '[', '1', 'D',0}; 00261 for(uint32_t i = 0; bf_bs[i] != 0; i++) { 00262 putc(bf_bs[i]); 00263 } 00264 } 00265 if (((uint8_t)c >= ' ') && (idx < len - 1)) { 00266 buff[idx++] = c; 00267 putc(c); 00268 } 00269 } 00270 buff[idx] = 0; 00271 putc('\n'); 00272 } 00273 00274 // Check key input strings and set time 00275 static void chk_and_set_time(char *ptr) 00276 { 00277 int32_t p1; 00278 struct tm t; 00279 time_t seconds; 00280 00281 if (xatoi(&ptr, &p1)) { 00282 t.tm_year = (uint8_t)p1 + 100; 00283 printf("Year:%d ",p1); 00284 xatoi( &ptr, &p1 ); 00285 t.tm_mon = (uint8_t)p1 - 1; 00286 printf("Month:%d ",p1); 00287 xatoi( &ptr, &p1 ); 00288 t.tm_mday = (uint8_t)p1; 00289 printf("Day:%d ",p1); 00290 xatoi( &ptr, &p1 ); 00291 t.tm_hour = (uint8_t)p1; 00292 printf("Hour:%d ",p1); 00293 xatoi( &ptr, &p1 ); 00294 t.tm_min = (uint8_t)p1; 00295 printf("Min:%d ",p1); 00296 xatoi( &ptr, &p1 ); 00297 t.tm_sec = (uint8_t)p1; 00298 printf("Sec: %d \r\n",p1); 00299 seconds = mktime(&t); 00300 set_time(seconds); 00301 } else { 00302 seconds = time(NULL); 00303 } 00304 seconds = time(NULL); 00305 struct tm *time_ajd = localtime(&seconds); 00306 ex_rtc.write_rtc_std(time_ajd); 00307 // Show Time with several example 00308 // ex.1 00309 printf( 00310 "Date: %04d/%02d/%02d, %02d:%02d:%02d\r\n", 00311 t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec 00312 ); 00313 char buf[64]; 00314 // ex.2 00315 strftime(buf, 40, "%x %X", localtime(&seconds)); 00316 printf("Date: %s\r\n", buf); 00317 // ex.3 00318 strftime(buf, 40, "%I:%M:%S %p (%Y/%m/%d)", localtime(&seconds)); 00319 printf("Date: %s\r\n", buf); 00320 // ex.4 00321 strftime(buf, 40, "%B %d,'%y, %H:%M:%S", localtime(&seconds)); 00322 printf("Date: %s\r\n", buf); 00323 }
Generated on Sat Jul 16 2022 05:40:32 by 1.7.2