2014-05-03 18:27:38 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2014-03-15 08:54:48 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2015-07-28 11:36:26 -04:00
|
|
|
#include STM32_HAL_H
|
2014-03-15 08:54:48 -04:00
|
|
|
|
2015-01-01 16:06:20 -05:00
|
|
|
#include "py/runtime.h"
|
2014-03-15 08:54:48 -04:00
|
|
|
#include "rtc.h"
|
2015-10-31 13:44:20 -04:00
|
|
|
#include "irq.h"
|
2014-03-15 08:54:48 -04:00
|
|
|
|
2014-05-03 07:40:53 -04:00
|
|
|
/// \moduleref pyb
|
|
|
|
/// \class RTC - real time clock
|
|
|
|
///
|
|
|
|
/// The RTC is and independent clock that keeps track of the date
|
|
|
|
/// and time.
|
2014-05-03 08:02:21 -04:00
|
|
|
///
|
|
|
|
/// Example usage:
|
|
|
|
///
|
|
|
|
/// rtc = pyb.RTC()
|
|
|
|
/// rtc.datetime((2014, 5, 1, 4, 13, 0, 0, 0))
|
|
|
|
/// print(rtc.datetime())
|
2014-05-03 07:40:53 -04:00
|
|
|
|
2014-04-18 21:17:30 -04:00
|
|
|
RTC_HandleTypeDef RTCHandle;
|
2014-03-15 08:54:48 -04:00
|
|
|
|
2014-04-21 07:45:59 -04:00
|
|
|
// rtc_info indicates various things about RTC startup
|
|
|
|
// it's a bit of a hack at the moment
|
2014-07-03 08:25:24 -04:00
|
|
|
static mp_uint_t rtc_info;
|
2014-03-15 08:54:48 -04:00
|
|
|
|
|
|
|
// Note: LSI is around (32KHz), these dividers should work either way
|
|
|
|
// ck_spre(1Hz) = RTCCLK(LSE) /(uwAsynchPrediv + 1)*(uwSynchPrediv + 1)
|
|
|
|
#define RTC_ASYNCH_PREDIV (0x7f)
|
|
|
|
#define RTC_SYNCH_PREDIV (0x00ff)
|
|
|
|
|
|
|
|
#if 0
|
2014-04-21 07:45:59 -04:00
|
|
|
#define RTC_INFO_USE_EXISTING (0)
|
|
|
|
#define RTC_INFO_USE_LSE (1)
|
|
|
|
#define RTC_INFO_USE_LSI (3)
|
|
|
|
|
2014-03-15 08:54:48 -04:00
|
|
|
void rtc_init(void) {
|
|
|
|
// Enable the PWR clock
|
|
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
|
|
|
|
|
|
|
|
// 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
|
|
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
|
|
|
|
|
|
|
|
// Allow access to RTC
|
|
|
|
PWR_BackupAccessCmd(ENABLE);
|
|
|
|
|
|
|
|
// Enable the LSE OSC
|
|
|
|
RCC_LSEConfig(RCC_LSE_ON);
|
|
|
|
|
|
|
|
// Wait till LSE is ready
|
2014-07-03 08:25:24 -04:00
|
|
|
mp_uint_t sys_tick = sys_tick_counter;
|
2014-03-15 08:54:48 -04:00
|
|
|
while((RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) && (--timeout > 0)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
// Enable the LSI OSC
|
|
|
|
RCC_LSICmd(ENABLE);
|
|
|
|
|
|
|
|
// Wait till LSI is ready
|
|
|
|
while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) {
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
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)
|
|
|
|
uint32_t uwSynchPrediv = 0xFF;
|
|
|
|
uint32_t uwAsynchPrediv = 0x7F;
|
|
|
|
|
|
|
|
// Enable the RTC Clock
|
|
|
|
RCC_RTCCLKCmd(ENABLE);
|
|
|
|
|
|
|
|
// Wait for RTC APB registers synchronisation
|
|
|
|
RTC_WaitForSynchro();
|
|
|
|
|
|
|
|
// Configure the RTC data register and RTC prescaler
|
|
|
|
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
|
|
|
|
RTC_WriteBackupRegister(RTC_BKP_DR0, 0x32F2);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-12-02 07:40:37 -05:00
|
|
|
STATIC void RTC_CalendarConfig(void);
|
2014-03-15 08:54:48 -04:00
|
|
|
|
2015-11-01 08:31:44 -05:00
|
|
|
#if defined(MICROPY_HW_RTC_USE_LSE) && MICROPY_HW_RTC_USE_LSE
|
|
|
|
STATIC bool rtc_use_lse = true;
|
|
|
|
#else
|
|
|
|
STATIC bool rtc_use_lse = false;
|
|
|
|
#endif
|
|
|
|
|
2014-03-15 08:54:48 -04:00
|
|
|
void rtc_init(void) {
|
2014-04-21 07:45:59 -04:00
|
|
|
RTCHandle.Instance = RTC;
|
2014-03-15 08:54:48 -04:00
|
|
|
|
2014-04-21 07:45:59 -04:00
|
|
|
/* Configure RTC prescaler and RTC data registers */
|
|
|
|
/* RTC configured as follow:
|
2014-03-15 08:54:48 -04:00
|
|
|
- Hour Format = Format 24
|
|
|
|
- Asynch Prediv = Value according to source clock
|
|
|
|
- Synch Prediv = Value according to source clock
|
|
|
|
- OutPut = Output Disable
|
|
|
|
- OutPutPolarity = High Polarity
|
2014-04-21 07:45:59 -04:00
|
|
|
- OutPutType = Open Drain */
|
|
|
|
RTCHandle.Init.HourFormat = RTC_HOURFORMAT_24;
|
|
|
|
RTCHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV;
|
|
|
|
RTCHandle.Init.SynchPrediv = RTC_SYNCH_PREDIV;
|
|
|
|
RTCHandle.Init.OutPut = RTC_OUTPUT_DISABLE;
|
|
|
|
RTCHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
|
|
|
|
RTCHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
|
|
|
|
|
2015-10-27 06:55:47 -04:00
|
|
|
if ((RCC->BDCR & (RCC_BDCR_LSEON | RCC_BDCR_LSERDY)) == (RCC_BDCR_LSEON | RCC_BDCR_LSERDY)) {
|
2015-11-01 08:31:44 -05:00
|
|
|
// LSE is enabled & ready --> no need to (re-)init RTC
|
2015-10-27 06:55:47 -04:00
|
|
|
// remove Backup Domain write protection
|
2015-11-01 08:31:44 -05:00
|
|
|
HAL_PWR_EnableBkUpAccess();
|
2015-10-27 06:55:47 -04:00
|
|
|
// Clear source Reset Flag
|
|
|
|
__HAL_RCC_CLEAR_RESET_FLAGS();
|
|
|
|
// provide some status information
|
|
|
|
rtc_info |= 0x40000 | (RCC->BDCR & 7) | (RCC->CSR & 3) << 8;
|
|
|
|
return;
|
2015-11-01 08:31:44 -05:00
|
|
|
} else if ((RCC->BDCR & RCC_BDCR_RTCSEL) == RCC_BDCR_RTCSEL_1) {
|
|
|
|
// LSI is already active
|
|
|
|
// remove Backup Domain write protection
|
|
|
|
HAL_PWR_EnableBkUpAccess();
|
|
|
|
// Clear source Reset Flag
|
|
|
|
__HAL_RCC_CLEAR_RESET_FLAGS();
|
|
|
|
RCC->CSR |= 1;
|
|
|
|
// provide some status information
|
|
|
|
rtc_info |= 0x80000 | (RCC->BDCR & 7) | (RCC->CSR & 3) << 8;
|
|
|
|
return;
|
2015-10-27 06:55:47 -04:00
|
|
|
}
|
|
|
|
|
2014-07-03 08:25:24 -04:00
|
|
|
mp_uint_t tick = HAL_GetTick();
|
2014-03-15 08:54:48 -04:00
|
|
|
|
2014-04-21 07:45:59 -04:00
|
|
|
if (HAL_RTC_Init(&RTCHandle) != HAL_OK) {
|
2015-11-01 08:31:44 -05:00
|
|
|
if (rtc_use_lse) {
|
|
|
|
// fall back to LSI...
|
|
|
|
rtc_use_lse = false;
|
2015-11-07 12:42:26 -05:00
|
|
|
HAL_PWR_EnableBkUpAccess();
|
2015-11-01 08:31:44 -05:00
|
|
|
RTCHandle.State = HAL_RTC_STATE_RESET;
|
|
|
|
if (HAL_RTC_Init(&RTCHandle) != HAL_OK) {
|
|
|
|
rtc_info = 0x0100ffff; // indicate error
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// init error
|
|
|
|
rtc_info = 0xffff; // indicate error
|
|
|
|
return;
|
|
|
|
}
|
2014-04-21 07:45:59 -04:00
|
|
|
}
|
|
|
|
|
2014-03-15 08:54:48 -04:00
|
|
|
// record how long it took for the RTC to start up
|
|
|
|
rtc_info = HAL_GetTick() - tick;
|
|
|
|
|
2015-11-01 08:31:44 -05:00
|
|
|
// fresh reset; configure RTC Calendar
|
|
|
|
RTC_CalendarConfig();
|
|
|
|
|
|
|
|
if(__HAL_RCC_GET_FLAG(RCC_FLAG_PORRST) != RESET) {
|
|
|
|
// power on reset occurred
|
|
|
|
rtc_info |= 0x10000;
|
2014-03-15 08:54:48 -04:00
|
|
|
}
|
2015-11-01 08:31:44 -05:00
|
|
|
if(__HAL_RCC_GET_FLAG(RCC_FLAG_PINRST) != RESET) {
|
|
|
|
// external reset occurred
|
|
|
|
rtc_info |= 0x20000;
|
|
|
|
}
|
|
|
|
// Clear source Reset Flag
|
|
|
|
__HAL_RCC_CLEAR_RESET_FLAGS();
|
2014-04-21 07:45:59 -04:00
|
|
|
}
|
|
|
|
|
2014-12-02 07:40:37 -05:00
|
|
|
STATIC void RTC_CalendarConfig(void) {
|
2014-04-21 07:45:59 -04:00
|
|
|
// set the date to 1st Jan 2014
|
|
|
|
RTC_DateTypeDef date;
|
2014-05-10 06:56:58 -04:00
|
|
|
date.Year = 14;
|
|
|
|
date.Month = 1;
|
|
|
|
date.Date = 1;
|
2014-04-21 07:45:59 -04:00
|
|
|
date.WeekDay = RTC_WEEKDAY_WEDNESDAY;
|
|
|
|
|
2014-05-10 06:56:58 -04:00
|
|
|
if(HAL_RTC_SetDate(&RTCHandle, &date, FORMAT_BIN) != HAL_OK) {
|
2014-04-21 07:45:59 -04:00
|
|
|
// init error
|
|
|
|
return;
|
2014-03-15 08:54:48 -04:00
|
|
|
}
|
|
|
|
|
2014-04-21 07:45:59 -04:00
|
|
|
// set the time to 00:00:00
|
|
|
|
RTC_TimeTypeDef time;
|
2014-05-10 06:56:58 -04:00
|
|
|
time.Hours = 0;
|
|
|
|
time.Minutes = 0;
|
|
|
|
time.Seconds = 0;
|
2014-04-21 07:45:59 -04:00
|
|
|
time.TimeFormat = RTC_HOURFORMAT12_AM;
|
|
|
|
time.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
|
|
|
|
time.StoreOperation = RTC_STOREOPERATION_RESET;
|
|
|
|
|
2014-05-10 06:56:58 -04:00
|
|
|
if (HAL_RTC_SetTime(&RTCHandle, &time, FORMAT_BIN) != HAL_OK) {
|
2014-04-21 07:45:59 -04:00
|
|
|
// init error
|
|
|
|
return;
|
|
|
|
}
|
2014-03-15 08:54:48 -04:00
|
|
|
}
|
|
|
|
|
2014-12-02 07:40:37 -05:00
|
|
|
/*
|
|
|
|
Note: Care must be taken when HAL_RCCEx_PeriphCLKConfig() is used to select
|
|
|
|
the RTC clock source; in this case the Backup domain will be reset in
|
|
|
|
order to modify the RTC Clock source, as consequence RTC registers (including
|
|
|
|
the backup registers) and RCC_BDCR register are set to their reset values.
|
|
|
|
*/
|
|
|
|
void HAL_RTC_MspInit(RTC_HandleTypeDef *hrtc) {
|
|
|
|
RCC_OscInitTypeDef RCC_OscInitStruct;
|
|
|
|
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
|
|
|
|
|
|
|
|
/* To change the source clock of the RTC feature (LSE, LSI), You have to:
|
|
|
|
- Enable the power clock using __PWR_CLK_ENABLE()
|
|
|
|
- Enable write access using HAL_PWR_EnableBkUpAccess() function before to
|
|
|
|
configure the RTC clock source (to be done once after reset).
|
|
|
|
- Reset the Back up Domain using __HAL_RCC_BACKUPRESET_FORCE() and
|
|
|
|
__HAL_RCC_BACKUPRESET_RELEASE().
|
|
|
|
- Configure the needed RTc clock source */
|
|
|
|
|
2015-03-16 18:54:44 -04:00
|
|
|
// RTC clock source uses LSE (external crystal) only if relevant
|
|
|
|
// configuration variable is set. Otherwise it uses LSI (internal osc).
|
|
|
|
|
2014-12-02 07:40:37 -05:00
|
|
|
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE;
|
|
|
|
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
|
2015-11-01 08:31:44 -05:00
|
|
|
if (rtc_use_lse) {
|
|
|
|
RCC_OscInitStruct.LSEState = RCC_LSE_ON;
|
|
|
|
RCC_OscInitStruct.LSIState = RCC_LSI_OFF;
|
|
|
|
} else {
|
|
|
|
RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
|
|
|
|
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
|
|
|
|
}
|
2014-12-02 07:40:37 -05:00
|
|
|
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
|
|
|
|
//Error_Handler();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
|
2015-11-01 08:31:44 -05:00
|
|
|
if (rtc_use_lse) {
|
|
|
|
PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
|
|
|
|
} else {
|
|
|
|
PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
|
|
|
|
}
|
2014-12-02 07:40:37 -05:00
|
|
|
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
|
|
|
|
//Error_Handler();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// enable RTC peripheral clock
|
|
|
|
__HAL_RCC_RTC_ENABLE();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HAL_RTC_MspDeInit(RTC_HandleTypeDef *hrtc) {
|
|
|
|
__HAL_RCC_RTC_DISABLE();
|
|
|
|
}
|
|
|
|
|
2014-03-15 08:54:48 -04:00
|
|
|
/******************************************************************************/
|
|
|
|
// Micro Python bindings
|
|
|
|
|
2014-04-18 21:17:30 -04:00
|
|
|
typedef struct _pyb_rtc_obj_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
} pyb_rtc_obj_t;
|
2014-03-15 08:54:48 -04:00
|
|
|
|
2014-04-18 21:17:30 -04:00
|
|
|
STATIC const pyb_rtc_obj_t pyb_rtc_obj = {{&pyb_rtc_type}};
|
2014-03-15 08:54:48 -04:00
|
|
|
|
2014-05-03 07:40:53 -04:00
|
|
|
/// \classmethod \constructor()
|
|
|
|
/// Create an RTC object.
|
2014-08-29 19:35:11 -04:00
|
|
|
STATIC mp_obj_t pyb_rtc_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
|
2014-04-18 21:17:30 -04:00
|
|
|
// check arguments
|
2014-04-19 19:16:30 -04:00
|
|
|
mp_arg_check_num(n_args, n_kw, 0, 0, false);
|
2014-04-18 21:17:30 -04:00
|
|
|
|
|
|
|
// return constant object
|
|
|
|
return (mp_obj_t)&pyb_rtc_obj;
|
2014-03-15 08:54:48 -04:00
|
|
|
}
|
|
|
|
|
2014-05-03 07:40:53 -04:00
|
|
|
/// \method info()
|
|
|
|
/// Get information about the startup time and reset source.
|
|
|
|
///
|
|
|
|
/// - The lower 0xffff are the number of milliseconds the RTC took to
|
|
|
|
/// start up.
|
|
|
|
/// - Bit 0x10000 is set if a power-on reset occurred.
|
|
|
|
/// - Bit 0x20000 is set if an external reset occurred
|
2014-04-18 21:17:30 -04:00
|
|
|
mp_obj_t pyb_rtc_info(mp_obj_t self_in) {
|
|
|
|
return mp_obj_new_int(rtc_info);
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(pyb_rtc_info_obj, pyb_rtc_info);
|
|
|
|
|
2014-05-03 07:40:53 -04:00
|
|
|
/// \method datetime([datetimetuple])
|
|
|
|
/// Get or set the date and time of the RTC.
|
|
|
|
///
|
|
|
|
/// With no arguments, this method returns an 8-tuple with the current
|
|
|
|
/// date and time. With 1 argument (being an 8-tuple) it sets the date
|
|
|
|
/// and time.
|
|
|
|
///
|
|
|
|
/// The 8-tuple has the following format:
|
|
|
|
///
|
|
|
|
/// (year, month, day, weekday, hours, minutes, seconds, subseconds)
|
|
|
|
///
|
|
|
|
/// `weekday` is 1-7 for Monday through Sunday.
|
|
|
|
///
|
2014-05-03 13:28:11 -04:00
|
|
|
/// `subseconds` counts down from 255 to 0
|
2014-08-29 19:35:11 -04:00
|
|
|
mp_obj_t pyb_rtc_datetime(mp_uint_t n_args, const mp_obj_t *args) {
|
2014-04-18 21:17:30 -04:00
|
|
|
if (n_args == 1) {
|
|
|
|
// get date and time
|
|
|
|
// note: need to call get time then get date to correctly access the registers
|
|
|
|
RTC_DateTypeDef date;
|
|
|
|
RTC_TimeTypeDef time;
|
|
|
|
HAL_RTC_GetTime(&RTCHandle, &time, FORMAT_BIN);
|
|
|
|
HAL_RTC_GetDate(&RTCHandle, &date, FORMAT_BIN);
|
|
|
|
mp_obj_t tuple[8] = {
|
|
|
|
mp_obj_new_int(2000 + date.Year),
|
|
|
|
mp_obj_new_int(date.Month),
|
|
|
|
mp_obj_new_int(date.Date),
|
|
|
|
mp_obj_new_int(date.WeekDay),
|
|
|
|
mp_obj_new_int(time.Hours),
|
|
|
|
mp_obj_new_int(time.Minutes),
|
|
|
|
mp_obj_new_int(time.Seconds),
|
|
|
|
mp_obj_new_int(time.SubSeconds),
|
|
|
|
};
|
|
|
|
return mp_obj_new_tuple(8, tuple);
|
|
|
|
} else {
|
|
|
|
// set date and time
|
|
|
|
mp_obj_t *items;
|
|
|
|
mp_obj_get_array_fixed_n(args[1], 8, &items);
|
|
|
|
|
|
|
|
RTC_DateTypeDef date;
|
|
|
|
date.Year = mp_obj_get_int(items[0]) - 2000;
|
|
|
|
date.Month = mp_obj_get_int(items[1]);
|
|
|
|
date.Date = mp_obj_get_int(items[2]);
|
|
|
|
date.WeekDay = mp_obj_get_int(items[3]);
|
2014-05-10 06:56:58 -04:00
|
|
|
HAL_RTC_SetDate(&RTCHandle, &date, FORMAT_BIN);
|
2014-04-18 21:17:30 -04:00
|
|
|
|
|
|
|
RTC_TimeTypeDef time;
|
|
|
|
time.Hours = mp_obj_get_int(items[4]);
|
|
|
|
time.Minutes = mp_obj_get_int(items[5]);
|
|
|
|
time.Seconds = mp_obj_get_int(items[6]);
|
|
|
|
time.SubSeconds = mp_obj_get_int(items[7]);
|
|
|
|
time.TimeFormat = RTC_HOURFORMAT12_AM;
|
|
|
|
time.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
|
|
|
|
time.StoreOperation = RTC_STOREOPERATION_SET;
|
2014-05-10 06:56:58 -04:00
|
|
|
HAL_RTC_SetTime(&RTCHandle, &time, FORMAT_BIN);
|
2014-04-18 21:17:30 -04:00
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_rtc_datetime_obj, 1, 2, pyb_rtc_datetime);
|
|
|
|
|
2015-03-15 13:15:55 -04:00
|
|
|
// wakeup(None)
|
|
|
|
// wakeup(ms, callback=None)
|
|
|
|
// wakeup(wucksel, wut, callback)
|
|
|
|
mp_obj_t pyb_rtc_wakeup(mp_uint_t n_args, const mp_obj_t *args) {
|
|
|
|
// wut is wakeup counter start value, wucksel is clock source
|
|
|
|
// counter is decremented at wucksel rate, and wakes the MCU when it gets to 0
|
|
|
|
// wucksel=0b000 is RTC/16 (RTC runs at 32768Hz)
|
|
|
|
// wucksel=0b001 is RTC/8
|
|
|
|
// wucksel=0b010 is RTC/4
|
|
|
|
// wucksel=0b011 is RTC/2
|
|
|
|
// wucksel=0b100 is 1Hz clock
|
|
|
|
// wucksel=0b110 is 1Hz clock with 0x10000 added to wut
|
|
|
|
// so a 1 second wakeup could be wut=2047, wucksel=0b000, or wut=4095, wucksel=0b001, etc
|
|
|
|
|
|
|
|
// disable wakeup IRQ while we configure it
|
|
|
|
HAL_NVIC_DisableIRQ(RTC_WKUP_IRQn);
|
|
|
|
|
|
|
|
bool enable = false;
|
|
|
|
mp_int_t wucksel;
|
|
|
|
mp_int_t wut;
|
|
|
|
mp_obj_t callback = mp_const_none;
|
|
|
|
if (n_args <= 3) {
|
|
|
|
if (args[1] == mp_const_none) {
|
|
|
|
// disable wakeup
|
|
|
|
} else {
|
|
|
|
// time given in ms
|
|
|
|
mp_int_t ms = mp_obj_get_int(args[1]);
|
|
|
|
mp_int_t div = 2;
|
|
|
|
wucksel = 3;
|
|
|
|
while (div <= 16 && ms > 2000 * div) {
|
|
|
|
div *= 2;
|
|
|
|
wucksel -= 1;
|
|
|
|
}
|
|
|
|
if (div <= 16) {
|
|
|
|
wut = 32768 / div * ms / 1000;
|
|
|
|
} else {
|
2015-10-08 07:41:12 -04:00
|
|
|
// use 1Hz clock
|
2015-03-15 13:15:55 -04:00
|
|
|
wucksel = 4;
|
|
|
|
wut = ms / 1000;
|
2015-10-08 07:41:12 -04:00
|
|
|
if (wut > 0x10000) {
|
|
|
|
// wut too large for 16-bit register, try to offset by 0x10000
|
|
|
|
wucksel = 6;
|
|
|
|
wut -= 0x10000;
|
|
|
|
if (wut > 0x10000) {
|
|
|
|
// wut still too large
|
2015-03-15 13:15:55 -04:00
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "wakeup value too large"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-08 07:41:12 -04:00
|
|
|
// wut register should be 1 less than desired value, but guard against wut=0
|
|
|
|
if (wut > 0) {
|
|
|
|
wut -= 1;
|
|
|
|
}
|
2015-03-15 13:15:55 -04:00
|
|
|
enable = true;
|
|
|
|
}
|
|
|
|
if (n_args == 3) {
|
|
|
|
callback = args[2];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// config values given directly
|
|
|
|
wucksel = mp_obj_get_int(args[1]);
|
|
|
|
wut = mp_obj_get_int(args[2]);
|
|
|
|
callback = args[3];
|
|
|
|
enable = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the callback
|
|
|
|
MP_STATE_PORT(pyb_extint_callback)[22] = callback;
|
|
|
|
|
|
|
|
// disable register write protection
|
|
|
|
RTC->WPR = 0xca;
|
|
|
|
RTC->WPR = 0x53;
|
|
|
|
|
|
|
|
// clear WUTE
|
|
|
|
RTC->CR &= ~(1 << 10);
|
|
|
|
|
|
|
|
// wait until WUTWF is set
|
|
|
|
while (!(RTC->ISR & (1 << 2))) {
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enable) {
|
|
|
|
// program WUT
|
|
|
|
RTC->WUTR = wut;
|
|
|
|
|
|
|
|
// set WUTIE to enable wakeup interrupts
|
|
|
|
// set WUTE to enable wakeup
|
|
|
|
// program WUCKSEL
|
2015-10-06 18:39:57 -04:00
|
|
|
RTC->CR = (RTC->CR & ~7) | (1 << 14) | (1 << 10) | (wucksel & 7);
|
2015-03-15 13:15:55 -04:00
|
|
|
|
|
|
|
// enable register write protection
|
|
|
|
RTC->WPR = 0xff;
|
|
|
|
|
|
|
|
// enable external interrupts on line 22
|
|
|
|
EXTI->IMR |= 1 << 22;
|
|
|
|
EXTI->RTSR |= 1 << 22;
|
|
|
|
|
|
|
|
// clear interrupt flags
|
|
|
|
RTC->ISR &= ~(1 << 10);
|
|
|
|
EXTI->PR = 1 << 22;
|
|
|
|
|
2015-10-31 13:44:20 -04:00
|
|
|
HAL_NVIC_SetPriority(RTC_WKUP_IRQn, IRQ_PRI_RTC_WKUP, IRQ_SUBPRI_RTC_WKUP);
|
2015-03-15 13:15:55 -04:00
|
|
|
HAL_NVIC_EnableIRQ(RTC_WKUP_IRQn);
|
|
|
|
|
|
|
|
//printf("wut=%d wucksel=%d\n", wut, wucksel);
|
|
|
|
} else {
|
|
|
|
// clear WUTIE to disable interrupts
|
|
|
|
RTC->CR &= ~(1 << 14);
|
|
|
|
|
|
|
|
// enable register write protection
|
|
|
|
RTC->WPR = 0xff;
|
|
|
|
|
|
|
|
// disable external interrupts on line 22
|
|
|
|
EXTI->IMR &= ~(1 << 22);
|
|
|
|
}
|
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_rtc_wakeup_obj, 2, 4, pyb_rtc_wakeup);
|
|
|
|
|
2015-05-07 13:18:52 -04:00
|
|
|
// calibration(None)
|
|
|
|
// calibration(cal)
|
|
|
|
// When an integer argument is provided, check that it falls in the range [-511 to 512]
|
|
|
|
// and set the calibration value; otherwise return calibration value
|
|
|
|
mp_obj_t pyb_rtc_calibration(mp_uint_t n_args, const mp_obj_t *args) {
|
|
|
|
mp_int_t cal;
|
|
|
|
if (n_args == 2) {
|
2015-10-20 07:04:37 -04:00
|
|
|
cal = mp_obj_get_int(args[1]);
|
|
|
|
mp_uint_t cal_p, cal_m;
|
|
|
|
if (cal < -511 || cal > 512) {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
|
|
|
|
"calibration value out of range"));
|
|
|
|
}
|
|
|
|
if (cal > 0) {
|
|
|
|
cal_p = RTC_SMOOTHCALIB_PLUSPULSES_SET;
|
|
|
|
cal_m = 512 - cal;
|
|
|
|
} else {
|
|
|
|
cal_p = RTC_SMOOTHCALIB_PLUSPULSES_RESET;
|
|
|
|
cal_m = -cal;
|
|
|
|
}
|
|
|
|
HAL_RTCEx_SetSmoothCalib(&RTCHandle, RTC_SMOOTHCALIB_PERIOD_32SEC, cal_p, cal_m);
|
|
|
|
return mp_const_none;
|
2015-05-07 13:18:52 -04:00
|
|
|
} else {
|
|
|
|
// printf("CALR = 0x%x\n", (mp_uint_t) RTCHandle.Instance->CALR); // DEBUG
|
2015-10-20 07:04:37 -04:00
|
|
|
// Test if CALP bit is set in CALR:
|
|
|
|
if (RTCHandle.Instance->CALR & 0x8000) {
|
|
|
|
cal = 512 - (RTCHandle.Instance->CALR & 0x1ff);
|
|
|
|
} else {
|
|
|
|
cal = -(RTCHandle.Instance->CALR & 0x1ff);
|
|
|
|
}
|
|
|
|
return mp_obj_new_int(cal);
|
2015-05-07 13:18:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_rtc_calibration_obj, 1, 2, pyb_rtc_calibration);
|
2015-10-20 07:04:37 -04:00
|
|
|
|
2014-04-18 21:17:30 -04:00
|
|
|
STATIC const mp_map_elem_t pyb_rtc_locals_dict_table[] = {
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_info), (mp_obj_t)&pyb_rtc_info_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_datetime), (mp_obj_t)&pyb_rtc_datetime_obj },
|
2015-03-15 13:15:55 -04:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_wakeup), (mp_obj_t)&pyb_rtc_wakeup_obj },
|
2015-05-07 13:18:52 -04:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_calibration), (mp_obj_t)&pyb_rtc_calibration_obj },
|
2014-04-18 21:17:30 -04:00
|
|
|
};
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(pyb_rtc_locals_dict, pyb_rtc_locals_dict_table);
|
|
|
|
|
|
|
|
const mp_obj_type_t pyb_rtc_type = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_RTC,
|
|
|
|
.make_new = pyb_rtc_make_new,
|
|
|
|
.locals_dict = (mp_obj_t)&pyb_rtc_locals_dict,
|
|
|
|
};
|