//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 <stdio.h>#include <stdint.h>#define EPOCH_YEAR 1970//Define bitmask for LED's /* BSRR = Bit Set Reset Register Each port has a BSRR port which can set or reset the pin, when the port is configured for Output. Setting bit 0 to 15 will set - put a high - on the Ports corresponding pin Setting bit 16 to 31 will reset - put a low - on the Ports corresponding pin - 16 Writing 0x100 to GPIO port E's BSRR will set Port E[0] to high (1) Writing 0x10000000 to GPIO port E's BSRR wilt reset Port E[0] to low (0)*///Setting BSRR bit 8 to 15 bitmasks (lowest 16 bit set bits on)// LED's are attached to bit 8 to 15 on the evaluation board.const uint32_t led_on[] = { 0x00000100, // PORT-E bit 8 ON 0x00000200, // PORT-E bit 9 ON 0x00000400, // PORT-E bit 10 ON 0x00000800, // PORT-E bit 11 ON 0x00001000, // PORT-E bit 12 ON 0x00002000, // PORT-E bit 13 ON 0x00004000, // PORT-E bit 14 ON 0x00008000 // PORT-E bit 15 ON};//Resetting BSRR bit 8 to 15 bitmasks (highest 16 bits reset pins)const uint32_t led_off[] = { 0x01000000, // PORT-E bit 8 OFF 0x02000000, // PORT-E bit 9 OFF 0x04000000, // PORT-E bit 10 OFF 0x08000000, // PORT-E bit 11 OFF 0x10000000, // PORT-E bit 12 OFF 0x20000000, // PORT-E bit 13 OFF 0x40000000, // PORT-E bit 14 OFF 0x80000000 // PORT-E bit 15 OFF};/* * days_in_month(month, year) * Returns the number of days in a given month and year, taking into account leap years. * * month: numeric month (1-12) * year: numeric year * * Prec: month is an integer between 1 and 12, inclusive, and year is an integer. * Post: none */// corrected by ben at sparkyb dot netint days_in_month(int month, int year) { // calculate number of days in a month return month == 2 ? (year % 4 ? 28 : (year % 100 ? 29 : (year % 400 ? 28 : 29))) : ((month - 1) % 7 % 2 ? 30 : 31);}int days_in_year(int year){ int i, seconds = 0; for(i=1; i<=12; i++){ seconds += days_in_month(i, year); } return seconds;}struct datetime { // Declare structure TimeStruct int sec; // seconds 00 to 59 int min; // minutes 00 to 59 int hour; // hours 00 to 23 int mday; // day of the month 1 to 31 int mon; // month 1 to 12 int year; // year 1970 to 2106};/* * Convert seconds to datetime struct */void seconds_to_datetime(int seconds, struct datetime *d) { printf("\r\nSeconds remaining: %i\r\n", seconds); d->year = EPOCH_YEAR; d->mon = 0; d->mday = 0; d->hour = 0; d->min = 0; d->sec = 0; // Run as long as we still have seconds left for a full year or leap year while(seconds >= 60*60*24*365 || seconds >= 60*60*24*366){ if(d->year%400 ==0 || (d->year%100 != 0 && d->year%4 == 0)){ seconds -= 60*60*24*366; } else { seconds -= 60*60*24*365; } d->year++; } // Run as long as we still have seconds left for a month while(seconds >= 60*60*24*30 || seconds >= 60*60*24*31 || seconds >= 60*60*24*28 || seconds >= 60*60*24*29){ seconds -= 60*60*24*days_in_month(d->mon, d->year); d->mon++; } // Run as long as we still have seconds left for a day while(seconds >= 60*60*24){ seconds -= 60*60*24; d->mday++; } // Run as long as we still have seconds left for an hour while(seconds >= 60*60){ seconds -= 60*60; d->hour++; } // Run as long as we still have seconds left for a minute while(seconds >= 60){ seconds -= 60; d->hour++; } // Just add reaming seconds d->sec = seconds;}/* * Convert a datetime struct to seconds */int datetime_to_seconds(struct datetime *d){ int year_i, mon_i, mday_i, mhour_i, mmin_i, seconds; // Convert time struct to seconds since 1970 for(year_i = EPOCH_YEAR; year_i<=d->year; year_i++) { // All years upto current if(year_i<d->year){ //Calc number of days in year int days = days_in_year(year_i); seconds += 60*60*24*days; } // Current year if(year_i == d->year){ // Calculate seconds in past months in current year for(mon_i = 1; mon_i<=d->mon; mon_i++){ //Calc number of days in year int days = days_in_month(mon_i, year_i); seconds += 60*60*24*days; } // Calculate seconds in past days in current month for(mday_i = 1; mday_i<=d->mday; mday_i++){ seconds += 60*60*24; } // Calculate seconds in past hours in current day for(mhour_i = 0; mhour_i<=d->hour; mhour_i++){ seconds += 60*60*mhour_i; } // Calculate seconds in past min in current day for(mmin_i = 0; mmin_i<=d->min; mmin_i++){ seconds += 60*mmin_i; } // Calculate seconds in past seocnds in current minute seconds += d->sec; } } return seconds;}// ledon: led = 1 to 8void ledon(uint8_t led) { if ( ( led == 0 ) | ( led > 8 ) ) { printf("ERROR: Attempt to turn on led %i (Led 1 to 8 valid)\n",led); } else { GPIOE->BSRR = led_on[led-1]; }}// ledon: led = 1 to 8void ledoff(uint8_t led) { if ( ( led == 0 ) | ( led > 8 ) ) { printf("ERROR: Attempt to turn off led %i (Led 1 to 8 valid)\n",led); } else { GPIOE->BSRR = led_off[led-1]; }}int main (void) { int led, seconds; struct datetime d; // 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"); d.sec = 54; d.min = 20; d.hour = 10; d.mday = 2; d.mon = 5; d.year = 2012; seconds = datetime_to_seconds(&d); printf("%i seconds has gone since year %i\r\n", seconds, EPOCH_YEAR); seconds_to_datetime(seconds, &d); printf("%i-%i-%i %i:%i:%i\r\n", d.year, d.mon, d.mday, d.hour, d.min, d.sec); //Do stuff printf("Starting main-loop\r\n"); printf("==================\r\n\n"); while (1) { printf("\r\nEnter led to turn on: "); scanf("%i", &led ); ledon(led); }}