Commit 30264662 authored by Damien George's avatar Damien George
Browse files

stm: Add RTC start-up info, and uPy bindings.

parent 46239413
......@@ -430,7 +430,8 @@ soft_reset:
#endif
rt_store_attr(m, MP_QSTR_hid, rt_make_function_n(1, pyb_hid_send_report));
#if MICROPY_HW_ENABLE_RTC
rt_store_attr(m, MP_QSTR_time, rt_make_function_n(0, pyb_rtc_read));
rt_store_attr(m, MP_QSTR_time, (mp_obj_t)&pyb_rtc_read_obj);
rt_store_attr(m, qstr_from_str("rtc_info"), (mp_obj_t)&pyb_rtc_info_obj);
#endif
#if MICROPY_HW_ENABLE_RNG
rt_store_attr(m, MP_QSTR_rand, rt_make_function_n(0, pyb_rng_get));
......
......@@ -5,38 +5,50 @@
#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "systick.h"
#include "rtc.h"
machine_uint_t rtc_info;
#define RTC_INFO_USE_EXISTING (0)
#define RTC_INFO_USE_LSE (1)
#define RTC_INFO_USE_LSI (3)
void rtc_init(void) {
/* Enable the PWR clock */
// Enable the PWR clock
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
/* Allow access to RTC */
// Allow access to RTC
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();
rtc_info = RTC_INFO_USE_EXISTING;
return;
}
uint32_t timeout = 10000000;
/* Enable the PWR clock */
// Enable the PWR clock
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
/* Allow access to RTC */
// Allow access to RTC
PWR_BackupAccessCmd(ENABLE);
/* Enable the LSE OSC */
// Enable the LSE OSC
RCC_LSEConfig(RCC_LSE_ON);
/* Wait till LSE is ready */
// Wait till LSE is ready
machine_uint_t sys_tick = sys_tick_counter;
while((RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) && (--timeout > 0)) {
}
/* If LSE timed out, use LSI instead */
// 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
if (timeout == 0) {
// Disable the LSE OSC
RCC_LSEConfig(RCC_LSE_OFF);
......@@ -44,29 +56,35 @@ void rtc_init(void) {
// Enable the LSI OSC
RCC_LSICmd(ENABLE);
/* Wait till LSI is ready */
// Wait till LSI is ready
while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) {
}
/* Use LSI as the RTC Clock Source */
// Use LSI as the RTC Clock Source
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
// record that we are using the LSI
rtc_info |= RTC_INFO_USE_LSI;
} else {
/* Use LSE as the RTC Clock Source */
// Use LSE as the RTC Clock Source
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
// record that we are using the LSE
rtc_info |= RTC_INFO_USE_LSE;
}
/* Note: LSI is around (32KHz), these dividers should work either way */
/* ck_spre(1Hz) = RTCCLK(LSE) /(uwAsynchPrediv + 1)*(uwSynchPrediv + 1)*/
// Note: LSI is around (32KHz), these dividers should work either way
// ck_spre(1Hz) = RTCCLK(LSE) /(uwAsynchPrediv + 1)*(uwSynchPrediv + 1)
uint32_t uwSynchPrediv = 0xFF;
uint32_t uwAsynchPrediv = 0x7F;
/* Enable the RTC Clock */
// Enable the RTC Clock
RCC_RTCCLKCmd(ENABLE);
/* Wait for RTC APB registers synchronisation */
// Wait for RTC APB registers synchronisation
RTC_WaitForSynchro();
/* Configure the RTC data register and RTC prescaler */
// Configure the RTC data register and RTC prescaler
RTC_InitTypeDef RTC_InitStructure;
RTC_InitStructure.RTC_AsynchPrediv = uwAsynchPrediv;
RTC_InitStructure.RTC_SynchPrediv = uwSynchPrediv;
......@@ -94,7 +112,13 @@ void rtc_init(void) {
}
/******************************************************************************/
/* Micro Python bindings */
// 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);
mp_obj_t pyb_rtc_read(void) {
RTC_TimeTypeDef RTC_TimeStructure;
......@@ -102,3 +126,5 @@ mp_obj_t pyb_rtc_read(void) {
printf("%02d:%02d:%02d\n", RTC_TimeStructure.RTC_Hours, RTC_TimeStructure.RTC_Minutes, RTC_TimeStructure.RTC_Seconds);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_0(pyb_rtc_read_obj, pyb_rtc_read);
void rtc_init(void);
mp_obj_t pyb_rtc_read(void);
MP_DECLARE_CONST_FUN_OBJ(pyb_rtc_info_obj);
MP_DECLARE_CONST_FUN_OBJ(pyb_rtc_read_obj);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment