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 Orefatoi

Committer:
wataloh
Date:
Tue Dec 20 01:51:02 2016 +0000
Revision:
0:20bce0dcc921
kl25z teensy merged

Who changed what in which revision?

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