2014-01-23 06:49:06 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stm32f4xx.h>
|
|
|
|
|
|
|
|
#include "misc.h"
|
|
|
|
#include "mpconfig.h"
|
|
|
|
#include "qstr.h"
|
|
|
|
#include "obj.h"
|
2014-02-19 18:15:09 -05:00
|
|
|
#include "systick.h"
|
2014-01-23 06:49:06 -05:00
|
|
|
#include "rtc.h"
|
|
|
|
|
2014-02-19 18:15:09 -05:00
|
|
|
machine_uint_t rtc_info;
|
|
|
|
|
|
|
|
#define RTC_INFO_USE_EXISTING (0)
|
|
|
|
#define RTC_INFO_USE_LSE (1)
|
|
|
|
#define RTC_INFO_USE_LSI (3)
|
|
|
|
|
2014-01-23 06:49:06 -05:00
|
|
|
void rtc_init(void) {
|
2014-02-19 18:15:09 -05:00
|
|
|
// Enable the PWR clock
|
2014-01-28 18:47:28 -05:00
|
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
|
|
|
|
|
2014-02-19 18:15:09 -05:00
|
|
|
// Allow access to RTC
|
2014-01-28 18:47:28 -05:00
|
|
|
PWR_BackupAccessCmd(ENABLE);
|
|
|
|
|
|
|
|
if (RTC_ReadBackupRegister(RTC_BKP_DR0) == 0x32F2) {
|
|
|
|
// RTC still alive, so don't re-init it
|
|
|
|
// wait for RTC APB register synchronisation
|
|
|
|
RTC_WaitForSynchro();
|
2014-02-19 18:15:09 -05:00
|
|
|
rtc_info = RTC_INFO_USE_EXISTING;
|
2014-01-28 18:47:28 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t timeout = 10000000;
|
2014-01-23 06:49:06 -05:00
|
|
|
|
2014-02-19 18:15:09 -05:00
|
|
|
// Enable the PWR clock
|
2014-01-23 06:49:06 -05:00
|
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
|
|
|
|
|
2014-02-19 18:15:09 -05:00
|
|
|
// Allow access to RTC
|
2014-01-23 06:49:06 -05:00
|
|
|
PWR_BackupAccessCmd(ENABLE);
|
|
|
|
|
2014-02-19 18:15:09 -05:00
|
|
|
// Enable the LSE OSC
|
2014-01-23 06:49:06 -05:00
|
|
|
RCC_LSEConfig(RCC_LSE_ON);
|
|
|
|
|
2014-02-19 18:15:09 -05:00
|
|
|
// Wait till LSE is ready
|
|
|
|
machine_uint_t sys_tick = sys_tick_counter;
|
2014-01-23 06:49:06 -05:00
|
|
|
while((RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) && (--timeout > 0)) {
|
|
|
|
}
|
|
|
|
|
2014-02-19 18:15:09 -05:00
|
|
|
// record how long it took for the RTC to start up
|
|
|
|
rtc_info = (sys_tick_counter - sys_tick) << 2;
|
|
|
|
|
|
|
|
// If LSE timed out, use LSI instead
|
2014-01-23 06:49:06 -05:00
|
|
|
if (timeout == 0) {
|
2014-01-28 18:47:28 -05:00
|
|
|
// Disable the LSE OSC
|
|
|
|
RCC_LSEConfig(RCC_LSE_OFF);
|
|
|
|
|
|
|
|
// Enable the LSI OSC
|
2014-01-23 06:49:06 -05:00
|
|
|
RCC_LSICmd(ENABLE);
|
|
|
|
|
2014-02-19 18:15:09 -05:00
|
|
|
// Wait till LSI is ready
|
2014-01-23 06:49:06 -05:00
|
|
|
while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) {
|
|
|
|
}
|
|
|
|
|
2014-02-19 18:15:09 -05:00
|
|
|
// Use LSI as the RTC Clock Source
|
2014-01-28 18:47:28 -05:00
|
|
|
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
|
2014-02-19 18:15:09 -05:00
|
|
|
|
|
|
|
// record that we are using the LSI
|
|
|
|
rtc_info |= RTC_INFO_USE_LSI;
|
2014-01-23 06:49:06 -05:00
|
|
|
} else {
|
2014-02-19 18:15:09 -05:00
|
|
|
// Use LSE as the RTC Clock Source
|
2014-01-28 18:47:28 -05:00
|
|
|
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
|
2014-02-19 18:15:09 -05:00
|
|
|
|
|
|
|
// record that we are using the LSE
|
|
|
|
rtc_info |= RTC_INFO_USE_LSE;
|
2014-01-23 06:49:06 -05:00
|
|
|
}
|
|
|
|
|
2014-02-19 18:15:09 -05:00
|
|
|
// Note: LSI is around (32KHz), these dividers should work either way
|
|
|
|
// ck_spre(1Hz) = RTCCLK(LSE) /(uwAsynchPrediv + 1)*(uwSynchPrediv + 1)
|
2014-01-23 06:49:06 -05:00
|
|
|
uint32_t uwSynchPrediv = 0xFF;
|
|
|
|
uint32_t uwAsynchPrediv = 0x7F;
|
|
|
|
|
2014-02-19 18:15:09 -05:00
|
|
|
// Enable the RTC Clock
|
2014-01-23 06:49:06 -05:00
|
|
|
RCC_RTCCLKCmd(ENABLE);
|
|
|
|
|
2014-02-19 18:15:09 -05:00
|
|
|
// Wait for RTC APB registers synchronisation
|
2014-01-23 06:49:06 -05:00
|
|
|
RTC_WaitForSynchro();
|
|
|
|
|
2014-02-19 18:15:09 -05:00
|
|
|
// Configure the RTC data register and RTC prescaler
|
2014-01-23 06:49:06 -05:00
|
|
|
RTC_InitTypeDef RTC_InitStructure;
|
|
|
|
RTC_InitStructure.RTC_AsynchPrediv = uwAsynchPrediv;
|
|
|
|
RTC_InitStructure.RTC_SynchPrediv = uwSynchPrediv;
|
|
|
|
RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
|
|
|
|
RTC_Init(&RTC_InitStructure);
|
|
|
|
|
|
|
|
// Set the date (BCD)
|
|
|
|
RTC_DateTypeDef RTC_DateStructure;
|
|
|
|
RTC_DateStructure.RTC_Year = 0x13;
|
|
|
|
RTC_DateStructure.RTC_Month = RTC_Month_October;
|
|
|
|
RTC_DateStructure.RTC_Date = 0x26;
|
|
|
|
RTC_DateStructure.RTC_WeekDay = RTC_Weekday_Saturday;
|
|
|
|
RTC_SetDate(RTC_Format_BCD, &RTC_DateStructure);
|
|
|
|
|
|
|
|
// Set the time (BCD)
|
|
|
|
RTC_TimeTypeDef RTC_TimeStructure;
|
|
|
|
RTC_TimeStructure.RTC_H12 = RTC_H12_AM;
|
|
|
|
RTC_TimeStructure.RTC_Hours = 0x01;
|
|
|
|
RTC_TimeStructure.RTC_Minutes = 0x53;
|
|
|
|
RTC_TimeStructure.RTC_Seconds = 0x00;
|
|
|
|
RTC_SetTime(RTC_Format_BCD, &RTC_TimeStructure);
|
|
|
|
|
|
|
|
// Indicator for the RTC configuration
|
2014-01-28 18:47:28 -05:00
|
|
|
RTC_WriteBackupRegister(RTC_BKP_DR0, 0x32F2);
|
2014-01-23 06:49:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2014-02-19 18:15:09 -05:00
|
|
|
// Micro Python bindings
|
|
|
|
|
|
|
|
mp_obj_t pyb_rtc_info(void) {
|
|
|
|
return mp_obj_new_int(rtc_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_0(pyb_rtc_info_obj, pyb_rtc_info);
|
2014-01-23 06:49:06 -05:00
|
|
|
|
|
|
|
mp_obj_t pyb_rtc_read(void) {
|
|
|
|
RTC_TimeTypeDef RTC_TimeStructure;
|
|
|
|
RTC_GetTime(RTC_Format_BIN, &RTC_TimeStructure);
|
|
|
|
printf("%02d:%02d:%02d\n", RTC_TimeStructure.RTC_Hours, RTC_TimeStructure.RTC_Minutes, RTC_TimeStructure.RTC_Seconds);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2014-02-19 18:15:09 -05:00
|
|
|
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_0(pyb_rtc_read_obj, pyb_rtc_read);
|