//Main /**************************************************************** * Author: kh@ipimp.at ***************************************************************** * Minimum setup of GPIO ****************************************************************/ #include "gpio.h" // Generel Purpose Input/Output #include "rcc.h" // Reset and clock Control #include "usart.h" #include "I2C.h" #include #include #define EPOCH_YEAR 1970 // Start year #define TIME_ZONE 3600 // +1 GMT (Denmark) #define DAYLIGHT_SAVING 1 // Winter = 1 #define DEBUG_INFO 0 // Winter = 1 /* * Time structure */ struct time { int sec; int min; int hour; int day; int month; int year; }; /* * Check if year is leap year */ int leap_year(int year){ if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)){ return 1; // Is leap year } else { return 0; // Is not leap year } } /* * Get year lenght in days */ int year_len(int year){ if(leap_year(year)){ return 366; // 366 days in leap year } else { return 365; // 365 days in normal year } } /* * Get month lenght in days */ int month_len(int month, int year){ if(month == 2){ // February is 28/29 days return (leap_year(year) ? 29 : 28); } else { // Normal is 30/31 days return ((month - 1) % 7 % 2 ? 30 : 31); } } /* * Convert time to seconds */ int time_to_seconds(struct time *t){ int i, seconds; // Year if(DEBUG_INFO) printf("Calculating seconds since %i\r\n", EPOCH_YEAR); for(i = EPOCH_YEAR; iyear; i++){ int days; days = year_len(i); if(DEBUG_INFO) printf("Calculating number of seconds in year %i (days: %i)\r\n", i, days); // Convert year to seconds seconds += 60*60*24*days; } // Month for(i = 1; imonth; i++){ int days; days = month_len(i, t->year); if(DEBUG_INFO) printf("Calculating number of seconds in month %i in %i (days: %i)\r\n", i, t->year, days); seconds += 60*60*24*days; } // Days if(DEBUG_INFO) printf("Calculating seconds in days in month %i in year %i\r\n", t->month, t->year); for(i = 1; iday; i++){ if(DEBUG_INFO) printf("Calculating number of seconds in day %i in month %i\r\n", i, t->month); seconds += 60*60*24; } // Hours if(DEBUG_INFO) printf("Calculating seconds in hours in day %i in month %i\r\n", t->day, t->month); seconds += 60*60*t->hour; // Minuts if(DEBUG_INFO) printf("Calculating seconds in minuts in hour %i in day %i in month %i\r\n", t->hour, t->day, t->month); seconds += 60*t->min; // Seconds if(DEBUG_INFO) printf("Adding remaining seconds\r\n"); seconds += t->sec; // Correct daylight saving if(DAYLIGHT_SAVING) seconds -= 3600; // Correct Time Zone seconds -= TIME_ZONE; return seconds; } /* * Convert seconds to time */ int seconds_to_time(int seconds, struct time *t){ return 0; } int main (void) { int seconds; struct time t; // Start Clock on GPIO Port E (Enable port E) rcc_apb2enr(RCC_APB2ENR_IOPEEN,ENABLE); rcc_clockmode5(); usart_enable(); //unsigned int volatile * const RSSE2 = (unsigned int *) (RCC_BASE + 0x18); //*RSSE2 |= 1 << 6; //Setting bit 8 to 15 of PORT E to Generel Purpose output push-pull max 50Mhz GPIOE->CRH=0x33333333; putstr("\x1b[2J\x1b[H"); putstr("I2C Initialise\n\r"); I2C_Init(); printf("Setting time\r\n"); // Convert datetime struct to secconds t.sec = 44; t.min = 11; t.hour = 22; t.day = 5; t.month = 7; t.year = 2012; seconds = time_to_seconds(&t); printf("Converted %i-%i-%i %02i:%02i:%02i to %i secs\r\n", t.year, t.month, t.day, t.hour, t.min, t.sec, seconds); //Do stuff printf("Starting main-loop\r\n"); printf("==================\r\n\n"); while (1) { // Running } }