pwm period is now 200us instead of the default 20ms veml6040 config is now AF_BIT | TRIG_BIT
Dependencies: mbed MMA8451Q USBDevice WakeUp vt100
Fork of afero_node_suntory_2017_06_15 by
util/RTC_Handler.cpp@2:dfe671e31221, 2017-01-24 (annotated)
- Committer:
- wataloh
- Date:
- Tue Jan 24 10:00:28 2017 +0000
- Revision:
- 2:dfe671e31221
latest
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wataloh | 2:dfe671e31221 | 1 | #include "RTC_Handler.h" |
wataloh | 2:dfe671e31221 | 2 | |
wataloh | 2:dfe671e31221 | 3 | static RTC_Handler *self = NULL; |
wataloh | 2:dfe671e31221 | 4 | |
wataloh | 2:dfe671e31221 | 5 | RTC_Handler* RTC_Handler::getInstance() |
wataloh | 2:dfe671e31221 | 6 | { |
wataloh | 2:dfe671e31221 | 7 | return self != NULL ? self : self = new RTC_Handler(); |
wataloh | 2:dfe671e31221 | 8 | } |
wataloh | 2:dfe671e31221 | 9 | |
wataloh | 2:dfe671e31221 | 10 | #if defined (TARGET_KL25Z) |
wataloh | 2:dfe671e31221 | 11 | |
wataloh | 2:dfe671e31221 | 12 | #define RTC8564NB_ADR 0xA2 |
wataloh | 2:dfe671e31221 | 13 | |
wataloh | 2:dfe671e31221 | 14 | #define CONTROL1 0x00 |
wataloh | 2:dfe671e31221 | 15 | #define CONTROL2 0x01 |
wataloh | 2:dfe671e31221 | 16 | #define SECONDS 0x02 |
wataloh | 2:dfe671e31221 | 17 | #define MINUTES 0x03 |
wataloh | 2:dfe671e31221 | 18 | #define HOURS 0x04 |
wataloh | 2:dfe671e31221 | 19 | #define DAYS 0x05 |
wataloh | 2:dfe671e31221 | 20 | #define WEEKDAYS 0x06 |
wataloh | 2:dfe671e31221 | 21 | #define MONTHS 0x07 |
wataloh | 2:dfe671e31221 | 22 | #define YEARS 0x08 |
wataloh | 2:dfe671e31221 | 23 | #define MINUTE_ALARM 0x09 |
wataloh | 2:dfe671e31221 | 24 | #define HOUR_ALARM 0x0A |
wataloh | 2:dfe671e31221 | 25 | #define DAY_ALARM 0x0B |
wataloh | 2:dfe671e31221 | 26 | #define WEEKDAY_ALARM 0x0C |
wataloh | 2:dfe671e31221 | 27 | #define CLOCKOUT_FREQ 0x0D |
wataloh | 2:dfe671e31221 | 28 | #define TIMER_CONTROL 0x0E |
wataloh | 2:dfe671e31221 | 29 | #define TIMER 0x0F |
wataloh | 2:dfe671e31221 | 30 | #define _READ 0x01 |
wataloh | 2:dfe671e31221 | 31 | #define JST_TO_UTC_DELTA (9*60*60) |
wataloh | 2:dfe671e31221 | 32 | |
wataloh | 2:dfe671e31221 | 33 | |
wataloh | 2:dfe671e31221 | 34 | RTC_Handler::RTC_Handler() : i2c(PTE0, PTE1) |
wataloh | 2:dfe671e31221 | 35 | { |
wataloh | 2:dfe671e31221 | 36 | } |
wataloh | 2:dfe671e31221 | 37 | |
wataloh | 2:dfe671e31221 | 38 | char |
wataloh | 2:dfe671e31221 | 39 | RTC_Handler::read(char address) |
wataloh | 2:dfe671e31221 | 40 | { |
wataloh | 2:dfe671e31221 | 41 | char value; |
wataloh | 2:dfe671e31221 | 42 | i2c.start(); |
wataloh | 2:dfe671e31221 | 43 | i2c.write(RTC8564NB_ADR); |
wataloh | 2:dfe671e31221 | 44 | i2c.write(address); |
wataloh | 2:dfe671e31221 | 45 | i2c.start(); |
wataloh | 2:dfe671e31221 | 46 | i2c.write(RTC8564NB_ADR | _READ); |
wataloh | 2:dfe671e31221 | 47 | value = i2c.read(0); |
wataloh | 2:dfe671e31221 | 48 | i2c.stop(); |
wataloh | 2:dfe671e31221 | 49 | return value; |
wataloh | 2:dfe671e31221 | 50 | } |
wataloh | 2:dfe671e31221 | 51 | |
wataloh | 2:dfe671e31221 | 52 | void |
wataloh | 2:dfe671e31221 | 53 | RTC_Handler::write(char address, char value) |
wataloh | 2:dfe671e31221 | 54 | { |
wataloh | 2:dfe671e31221 | 55 | i2c.start(); |
wataloh | 2:dfe671e31221 | 56 | i2c.write(RTC8564NB_ADR); |
wataloh | 2:dfe671e31221 | 57 | i2c.write(address); |
wataloh | 2:dfe671e31221 | 58 | i2c.write(value); |
wataloh | 2:dfe671e31221 | 59 | i2c.stop(); |
wataloh | 2:dfe671e31221 | 60 | } |
wataloh | 2:dfe671e31221 | 61 | |
wataloh | 2:dfe671e31221 | 62 | void |
wataloh | 2:dfe671e31221 | 63 | RTC_Handler::set(char y[3], char m[3], char d[3], char h[3], char min[3], char s[3]) |
wataloh | 2:dfe671e31221 | 64 | { |
wataloh | 2:dfe671e31221 | 65 | write(CONTROL1, 0x20); //stop |
wataloh | 2:dfe671e31221 | 66 | write(CONTROL2, 0x00); |
wataloh | 2:dfe671e31221 | 67 | write(YEARS, ((y[0]-0x30)<<4)+(y[1]-0x30)); |
wataloh | 2:dfe671e31221 | 68 | write(MONTHS, ((m[0]-0x30)<<4)+(m[1]-0x30)); |
wataloh | 2:dfe671e31221 | 69 | write(DAYS, ((d[0]-0x30)<<4)+(d[1]-0x30)); |
wataloh | 2:dfe671e31221 | 70 | write(HOURS, ((h[0]-0x30)<<4)+(h[1]-0x30)); |
wataloh | 2:dfe671e31221 | 71 | write(MINUTES, ((min[0]-0x30)<<4)+(min[1]-0x30)); |
wataloh | 2:dfe671e31221 | 72 | write(SECONDS, ((s[0]-0x30)<<4)+(s[1]-0x30)); |
wataloh | 2:dfe671e31221 | 73 | // write(WEEKDAYS, week_val[0] - 0x30); //Sunday = "0", Monday = "1" ... |
wataloh | 2:dfe671e31221 | 74 | write(CLOCKOUT_FREQ, 0x00); // 0x83 = TE on & 1Hz |
wataloh | 2:dfe671e31221 | 75 | write(TIMER_CONTROL, 0x00); |
wataloh | 2:dfe671e31221 | 76 | write(CONTROL1, 0x00); //start |
wataloh | 2:dfe671e31221 | 77 | return; |
wataloh | 2:dfe671e31221 | 78 | } |
wataloh | 2:dfe671e31221 | 79 | |
wataloh | 2:dfe671e31221 | 80 | void |
wataloh | 2:dfe671e31221 | 81 | RTC_Handler::setUTC(time_t t) |
wataloh | 2:dfe671e31221 | 82 | { |
wataloh | 2:dfe671e31221 | 83 | struct tm *date; |
wataloh | 2:dfe671e31221 | 84 | char str[32]; |
wataloh | 2:dfe671e31221 | 85 | memset(str,'\0',sizeof(str)); |
wataloh | 2:dfe671e31221 | 86 | //timer = time(NULL); |
wataloh | 2:dfe671e31221 | 87 | //t -= JST_TO_UTC_DELTA; //make local time UTC |
wataloh | 2:dfe671e31221 | 88 | date = localtime(&t); |
wataloh | 2:dfe671e31221 | 89 | strftime(str, 255, "%y%m%d%H%M%S", date); |
wataloh | 2:dfe671e31221 | 90 | //printf("%s\n", str); |
wataloh | 2:dfe671e31221 | 91 | set(&str[0],&str[2],&str[4],&str[6],&str[8],&str[10]); |
wataloh | 2:dfe671e31221 | 92 | } |
wataloh | 2:dfe671e31221 | 93 | |
wataloh | 2:dfe671e31221 | 94 | time_t |
wataloh | 2:dfe671e31221 | 95 | RTC_Handler::getUTC() |
wataloh | 2:dfe671e31221 | 96 | { |
wataloh | 2:dfe671e31221 | 97 | int year = read(YEARS); |
wataloh | 2:dfe671e31221 | 98 | int mon = read(MONTHS); |
wataloh | 2:dfe671e31221 | 99 | int day = read(DAYS); |
wataloh | 2:dfe671e31221 | 100 | int week = read(WEEKDAYS); |
wataloh | 2:dfe671e31221 | 101 | int hour = read(HOURS); |
wataloh | 2:dfe671e31221 | 102 | int min = read(MINUTES); |
wataloh | 2:dfe671e31221 | 103 | int sec = read(SECONDS); |
wataloh | 2:dfe671e31221 | 104 | |
wataloh | 2:dfe671e31221 | 105 | year = ((year >> 4) & 0x03) * 10 + (year & 0x0F) + 2000; |
wataloh | 2:dfe671e31221 | 106 | mon = ((mon >> 4) & 0x01) * 10 + (mon & 0x0F); |
wataloh | 2:dfe671e31221 | 107 | day = ((day >> 4) & 0x03) * 10 + (day & 0x0F); |
wataloh | 2:dfe671e31221 | 108 | hour = ((hour >> 4) & 0x03) * 10 + (hour & 0x0F); |
wataloh | 2:dfe671e31221 | 109 | min = ((min >> 4) & 0x07) * 10 + (min & 0x0F); |
wataloh | 2:dfe671e31221 | 110 | sec = ((sec >> 4) & 0x07) * 10 + (sec & 0x0F); |
wataloh | 2:dfe671e31221 | 111 | |
wataloh | 2:dfe671e31221 | 112 | struct tm strtim; |
wataloh | 2:dfe671e31221 | 113 | time_t ret; |
wataloh | 2:dfe671e31221 | 114 | memset(( void * )&strtim , '\0' , sizeof( strtim )); |
wataloh | 2:dfe671e31221 | 115 | strtim.tm_year = year-1900; /* 年[1900年から起算] */ |
wataloh | 2:dfe671e31221 | 116 | strtim.tm_mon = mon-1; /* 月[0-11] */ |
wataloh | 2:dfe671e31221 | 117 | strtim.tm_mday = day; /* 日[1-31] */ |
wataloh | 2:dfe671e31221 | 118 | strtim.tm_hour = hour ;/* 時[0-23] */ |
wataloh | 2:dfe671e31221 | 119 | strtim.tm_min = min ;/* 分[0-59] */ |
wataloh | 2:dfe671e31221 | 120 | strtim.tm_sec = sec; /* 秒[0-61]閏秒考慮 */ |
wataloh | 2:dfe671e31221 | 121 | |
wataloh | 2:dfe671e31221 | 122 | /* 日付補完 */ |
wataloh | 2:dfe671e31221 | 123 | ret = mktime( &strtim ); |
wataloh | 2:dfe671e31221 | 124 | if( ret == -1 ) { |
wataloh | 2:dfe671e31221 | 125 | printf( "mmktime error\n" ); |
wataloh | 2:dfe671e31221 | 126 | } |
wataloh | 2:dfe671e31221 | 127 | |
wataloh | 2:dfe671e31221 | 128 | return ret; |
wataloh | 2:dfe671e31221 | 129 | } |
wataloh | 2:dfe671e31221 | 130 | #elif defined (TARGET_TEENSY3_1) |
wataloh | 2:dfe671e31221 | 131 | |
wataloh | 2:dfe671e31221 | 132 | RTC_Handler::RTC_Handler() |
wataloh | 2:dfe671e31221 | 133 | { |
wataloh | 2:dfe671e31221 | 134 | } |
wataloh | 2:dfe671e31221 | 135 | |
wataloh | 2:dfe671e31221 | 136 | void |
wataloh | 2:dfe671e31221 | 137 | RTC_Handler::setUTC(time_t t) |
wataloh | 2:dfe671e31221 | 138 | { |
wataloh | 2:dfe671e31221 | 139 | set_time(t); |
wataloh | 2:dfe671e31221 | 140 | } |
wataloh | 2:dfe671e31221 | 141 | |
wataloh | 2:dfe671e31221 | 142 | time_t |
wataloh | 2:dfe671e31221 | 143 | RTC_Handler::getUTC() |
wataloh | 2:dfe671e31221 | 144 | { |
wataloh | 2:dfe671e31221 | 145 | return time(NULL); |
wataloh | 2:dfe671e31221 | 146 | } |
wataloh | 2:dfe671e31221 | 147 | |
wataloh | 2:dfe671e31221 | 148 | #endif |