int main(void) { /* Initialize system */ SystemInit(); /* Initialize delay */ TM_DELAY_Init(); /* Initiaize button */ TM_DISCO_ButtonInit(); /* Initialize Leds */ TM_DISCO_LedInit(); /* Initialize USART, TX: PB10, RX: PB11 */ TM_USART_Init(USART3, TM_USART_PinsPack_1, 115200); /* Initialize RTC with internal 32768Hz clock */ /* It's not very accurate */ if (!TM_RTC_Init(TM_RTC_ClockSource_Internal)) { /* RTC was first time initialized */ /* Do your stuff here */ /* eg. set default time */ } /* Set wakeup interrupt every 125 ms */ TM_RTC_Interrupts(TM_RTC_Int_125ms); while (1) { /* If button pressed */ if (TM_DISCO_ButtonPressed()) { /* Subseconds are ignored when writing new time */ datatime.hours = 0; datatime.minutes = 59; datatime.seconds = 55; datatime.year = 14; datatime.month = 6; datatime.date = 30; datatime.day = 6; /* Set new time */ TM_RTC_SetDateTime(&datatime, TM_RTC_Format_BIN); } } }
void setDate(void) { TM_RTC_Time_t time; int tmp=0; TM_RTC_GetDateTime(&time, TM_RTC_Format_BIN); tmp = cmdlineGetArgInt(1); if (tmp > 0 && tmp < 32) time.date=tmp; tmp = cmdlineGetArgInt(2); if (tmp > 0 && tmp < 13) time.month=tmp; tmp = cmdlineGetArgInt(3); if (tmp >= 0 && tmp <= 99) time.year=tmp; TM_RTC_SetDateTime(&time, TM_RTC_Format_BIN); conf_bit(SET,SETTINGS_1,DATE_SET); TM_USART_Puts(MENU_USART,"Date has been sucessfully set!\n\r"); }
void setTime(void) { TM_RTC_Time_t time; int tmp=0; TM_RTC_GetDateTime(&time, TM_RTC_Format_BIN); tmp = cmdlineGetArgInt(1); if (tmp > 0 && tmp < 25) time.hours=tmp; tmp = cmdlineGetArgInt(2); if (tmp > 0 && tmp < 61) time.minutes=tmp; tmp = cmdlineGetArgInt(3); if (tmp > 0 && tmp < 61) time.seconds=tmp; TM_RTC_SetDateTime(&time, TM_RTC_Format_BIN); conf_bit(SET,SETTINGS_1,TIME_SET); TM_USART_Puts(MENU_USART,"Time has been sucessfully set!\n\r"); }
TM_RTC_Result_t TM_RTC_SetDateTimeString(char* str) { TM_RTC_t tmp; uint8_t i = 0; /* Get date */ tmp.Day = 0; while (RTC_CHARISNUM(*(str + i))) { tmp.Day = tmp.Day * 10 + RTC_CHAR2NUM(*(str + i)); i++; } i++; /* Get month */ tmp.Month = 0; while (RTC_CHARISNUM(*(str + i))) { tmp.Month = tmp.Month * 10 + RTC_CHAR2NUM(*(str + i)); i++; } i++; /* Get year */ tmp.Year = 0; while (RTC_CHARISNUM(*(str + i))) { tmp.Year = tmp.Year * 10 + RTC_CHAR2NUM(*(str + i)); i++; } i++; /* Get day in a week */ tmp.WeekDay = 0; while (RTC_CHARISNUM(*(str + i))) { tmp.WeekDay = tmp.WeekDay * 10 + RTC_CHAR2NUM(*(str + i)); i++; } i++; /* Get hours */ tmp.Hours = 0; while (RTC_CHARISNUM(*(str + i))) { tmp.Hours = tmp.Hours * 10 + RTC_CHAR2NUM(*(str + i)); i++; } i++; /* Get minutes */ tmp.Minutes = 0; while (RTC_CHARISNUM(*(str + i))) { tmp.Minutes = tmp.Minutes * 10 + RTC_CHAR2NUM(*(str + i)); i++; } i++; /* Get seconds */ tmp.Seconds = 0; while (RTC_CHARISNUM(*(str + i))) { tmp.Seconds = tmp.Seconds * 10 + RTC_CHAR2NUM(*(str + i)); i++; } i++; /* Return status from set date time function */ return TM_RTC_SetDateTime(&tmp, TM_RTC_Format_BIN); }
int main(void) { /* Initialize system */ SystemInit(); /* Initialize delay */ TM_DELAY_Init(); /* Initiaize button */ TM_DISCO_ButtonInit(); /* Initialize Leds */ TM_DISCO_LedInit(); /* Initialize USART, TX: PB10, RX: PB11 */ TM_USART_Init(USART3, TM_USART_PinsPack_1, 115200); /* Initialize RTC with internal 32768Hz clock */ /* It's not very accurate */ if (!TM_RTC_Init(TM_RTC_ClockSource_Internal)) { /* RTC was first time initialized */ /* Do your stuff here */ /* eg. set default time */ } /* Set wakeup interrupt every 1 second */ TM_RTC_Interrupts(TM_RTC_Int_1s); while (1) { /* If button pressed */ if (TM_DISCO_ButtonPressed()) { /* Set new time */ Time.hours = 21; Time.minutes = 11; Time.seconds = 00; Time.year = 14; Time.month = 10; Time.date = 20; Time.day = 1; /* Set new RTC time */ TM_RTC_SetDateTime(&Time, TM_RTC_Format_BIN); /* Set alarm A each day 1 (Monday) in a week */ /* Alarm will be first triggered 5 seconds later as time is configured for RTC */ AlarmTime.hours = Time.hours; AlarmTime.minutes = Time.minutes; AlarmTime.seconds = Time.seconds + 5; AlarmTime.alarmtype = TM_RTC_AlarmType_DayInWeek; AlarmTime.day = 1; /* Set RTC alarm A, time in binary format */ TM_RTC_SetAlarm(TM_RTC_Alarm_A, &AlarmTime, TM_RTC_Format_BIN); /* Set alarm B each 20th day in a month */ /* Alarm will be first triggered 10 seconds later as time is configured for RTC */ AlarmTime.hours = Time.hours; AlarmTime.minutes = Time.minutes; AlarmTime.seconds = Time.seconds + 10; AlarmTime.day = 20; AlarmTime.alarmtype = TM_RTC_AlarmType_DayInMonth; /* Set RTC alarm B, time in binary format */ TM_RTC_SetAlarm(TM_RTC_Alarm_B, &AlarmTime, TM_RTC_Format_BIN); } } }
uint32_t TM_RTC_Init(TM_RTC_ClockSource_t source) { uint32_t status; uint8_t stat = 1; TM_RTC_Time_t datatime; /* Enable RTC peripheral clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); /* Allow access to BKP Domain */ PWR_BackupAccessCmd(ENABLE); /* Get RTC status */ status = RTC_ReadBackupRegister(RTC_STATUS_REG); if (status == RTC_STATUS_TIME_OK) { TM_RTC_Status = RTC_STATUS_TIME_OK; /* Start internal clock if we choose internal clock */ if (source == TM_RTC_ClockSource_Internal) { TM_RTC_Config(TM_RTC_ClockSource_Internal); } /* Wait for RTC APB registers synchronisation (needed after start-up from Reset) */ RTC_WaitForSynchro(); /* Clear interrupt flags */ RTC_ClearITPendingBit(RTC_IT_WUT); EXTI_ClearITPendingBit(EXTI_Line22); /* Get date and time */ TM_RTC_GetDateTime(&datatime, TM_RTC_Format_BIN); } else if (status == RTC_STATUS_INIT_OK) { TM_RTC_Status = RTC_STATUS_INIT_OK; /* Start internal clock if we choose internal clock */ if (source == TM_RTC_ClockSource_Internal) { TM_RTC_Config(TM_RTC_ClockSource_Internal); } /* Wait for RTC APB registers synchronisation (needed after start-up from Reset) */ RTC_WaitForSynchro(); /* Clear interrupt flags */ RTC_ClearITPendingBit(RTC_IT_WUT); EXTI_ClearITPendingBit(EXTI_Line22); /* Get date and time */ TM_RTC_GetDateTime(&datatime, TM_RTC_Format_BIN); } else { TM_RTC_Status = RTC_STATUS_ZERO; /* Return status = 0 -> RTC Never initialized before */ stat = RTC_STATUS_ZERO; /* Config RTC */ TM_RTC_Config(source); /* Set date and time */ datatime.date = 1; datatime.day = 1; datatime.month = 1; datatime.year = 0; datatime.hours = 0; datatime.minutes = 0; datatime.seconds = 0; /* Set date and time */ TM_RTC_SetDateTime(&datatime, TM_RTC_Format_BIN); /* Initialized OK */ TM_RTC_Status = RTC_STATUS_INIT_OK; } /* If first time initialized */ if (stat == RTC_STATUS_ZERO) { return 0; } return TM_RTC_Status; }
int rtc_set(void *data) { TM_RTC_SetDateTime(data); return 0; }