stmhal: Get RTC working.

This commit is contained in:
Damien George 2014-03-15 12:54:48 +00:00
parent 07174c6fca
commit 8b96af6907
6 changed files with 314 additions and 62 deletions

View File

@ -77,6 +77,7 @@ SRC_C = \
gpio.c \
exti.c \
usrsw.c \
rtc.c \
# lcd.c \
# servo.c \
@ -88,7 +89,6 @@ SRC_C = \
# sdcard.c \
# i2c.c \
# adc.c \
# rtc.c \
# file.c \
# pybwlan.c \
@ -103,44 +103,15 @@ SRC_HAL = $(addprefix $(HAL_DIR)/src/,\
stm32f4xx_hal_gpio.c \
stm32f4xx_hal_pcd.c \
stm32f4xx_hal_rcc.c \
stm32f4xx_hal_rcc_ex.c \
stm32f4xx_hal_rtc.c \
stm32f4xx_hal_rtc_ex.c \
stm32f4xx_hal_tim.c \
stm32f4xx_hal_tim_ex.c \
stm32f4xx_hal_uart.c \
stm32f4xx_ll_usb.c \
)
SRC_STMPERIPH = $(addprefix $(STMPERIPH_DIR)/,\
stm_misc.c \
stm32f4xx_rcc.c \
stm32f4xx_syscfg.c \
stm32f4xx_flash.c \
stm32f4xx_dma.c \
stm32f4xx_gpio.c \
stm32f4xx_exti.c \
stm32f4xx_tim.c \
stm32f4xx_sdio.c \
stm32f4xx_pwr.c \
stm32f4xx_rtc.c \
stm32f4xx_usart.c \
stm32f4xx_spi.c \
stm32f4xx_dac.c \
stm32f4xx_rng.c \
stm32f4xx_i2c.c \
stm32f4xx_adc.c \
stm324x7i_eval.c \
stm324x7i_eval_sdio_sd.c \
)
SRC_STMUSB = $(addprefix $(STMUSB_DIR)/,\
usb_core.c \
usb_bsp.c \
usb_dcd.c \
usb_dcd_int.c \
usb_hcd.c \
usb_hcd_int.c \
)
# usb_otg.c \
SRC_USBDEV = $(addprefix $(USBDEV_DIR)/,\
core/src/usbd_core.c \
core/src/usbd_ctlreq.c \
@ -162,17 +133,6 @@ SRC_USBDEV = $(addprefix $(USBDEV_DIR)/,\
usbd_storage_msd.c \
)
SRC_STMUSBH = $(addprefix $(STMUSBH_DIR)/,\
usbh_core.c \
usbh_hcs.c \
usbh_stdreq.c \
usbh_ioreq.c \
usbh_usr.c \
usbh_hid_core.c \
usbh_hid_mouse.c \
usbh_hid_keybd.c \
)
SRC_FATFS = $(addprefix $(FATFS_DIR)/,\
ff.c \
diskio.c \

View File

@ -3,19 +3,6 @@
#include <stm32f4xx_hal.h>
#include <stm32f4xx_hal_gpio.h>
#if 0
#include <stm32f4xx.h>
#include <stm32f4xx_rcc.h>
#include <stm32f4xx_syscfg.h>
#include <stm32f4xx_gpio.h>
#include <stm32f4xx_exti.h>
#include <stm32f4xx_tim.h>
#include <stm32f4xx_pwr.h>
#include <stm32f4xx_rtc.h>
#include <stm32f4xx_usart.h>
#include <stm32f4xx_rng.h>
#include <usbd_storage_msd.h>
#endif
#include "std.h"
#include "misc.h"
@ -41,6 +28,7 @@
#include "exti.h"
#include "usrsw.h"
#include "usb.h"
#include "rtc.h"
#if 0
#include "ff.h"
#include "lexerfatfs.h"
@ -51,7 +39,6 @@
#include "accel.h"
#include "timer.h"
#include "pybwlan.h"
#include "rtc.h"
#include "file.h"
#include "pin.h"
#endif
@ -254,11 +241,9 @@ int main(void) {
// turn on LED to indicate bootup
led_state(PYB_LED_GREEN, 1);
#if 0
#if MICROPY_HW_ENABLE_RTC
rtc_init();
#endif
#endif
#if 0
// more sub-system init

View File

@ -20,8 +20,8 @@
#include "pin.h"
#include "exti.h"
#include "usrsw.h"
#if 0
#include "rtc.h"
#if 0
#include "servo.h"
#include "storage.h"
#include "usb.h"
@ -241,12 +241,12 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_rand), (mp_obj_t)&pyb_rng_get_obj },
#endif
#if 0
#if MICROPY_HW_ENABLE_RTC
{ MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&pyb_rtc_read_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_rtc_info), (mp_obj_t)&pyb_rtc_info_obj },
#endif
#if 0
#if MICROPY_HW_ENABLE_SERVO
{ MP_OBJ_NEW_QSTR(MP_QSTR_pwm), (mp_obj_t)&pyb_pwm_set_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_servo), (mp_obj_t)&pyb_servo_set_obj },

241
stmhal/rtc.c Normal file
View File

@ -0,0 +1,241 @@
#include <stdio.h>
#include "stm32f4xx_hal.h"
#include "stm32f4xx_hal_rtc.h"
#include "misc.h"
#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "systick.h"
#include "rtc.h"
static RTC_HandleTypeDef RtcHandle;
static machine_uint_t rtc_info;
#define RTC_INFO_USE_EXISTING (0)
#define RTC_INFO_USE_LSE (1)
#define RTC_INFO_USE_LSI (3)
// 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
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
machine_uint_t sys_tick = sys_tick_counter;
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
static void RTC_CalendarConfig(void);
void rtc_init(void) {
/*##-1- Configure the RTC peripheral #######################################*/
RtcHandle.Instance = RTC;
/* Configure RTC prescaler and RTC data registers */
/* RTC configured as follow:
- Hour Format = Format 24
- Asynch Prediv = Value according to source clock
- Synch Prediv = Value according to source clock
- OutPut = Output Disable
- OutPutPolarity = High Polarity
- 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;
machine_uint_t tick = HAL_GetTick();
if(HAL_RTC_Init(&RtcHandle) != HAL_OK)
{
/* Initialization Error */
//Error_Handler();
return;
}
// record how long it took for the RTC to start up
rtc_info = HAL_GetTick() - tick;
/*##-2- Check if Data stored in BackUp register0: No Need to reconfigure RTC#*/
/* Read the Back Up Register 0 Data */
if(HAL_RTCEx_BKUPRead(&RtcHandle, RTC_BKP_DR0) != 0x32F2)
{
/* Configure RTC Calendar */
RTC_CalendarConfig();
}
else
{
/* Check if the Power On Reset flag is set */
if(__HAL_RCC_GET_FLAG(RCC_FLAG_PORRST) != RESET)
{
/* Turn on LED2: Power on reset occured */
//BSP_LED_On(LED2);
}
/* Check if Pin Reset flag is set */
if(__HAL_RCC_GET_FLAG(RCC_FLAG_PINRST) != RESET)
{
/* Turn on LED4: External reset occured */
//BSP_LED_On(LED4);
}
/* Clear source Reset Flag */
__HAL_RCC_CLEAR_RESET_FLAGS();
}
}
static void RTC_CalendarConfig(void)
{
RTC_DateTypeDef sdatestructure;
RTC_TimeTypeDef stimestructure;
/*##-1- Configure the Date #################################################*/
/* Set Date: Tuesday February 18th 2014 */
sdatestructure.Year = 0x14;
sdatestructure.Month = RTC_MONTH_FEBRUARY;
sdatestructure.Date = 0x18;
sdatestructure.WeekDay = RTC_WEEKDAY_TUESDAY;
if(HAL_RTC_SetDate(&RtcHandle,&sdatestructure,FORMAT_BCD) != HAL_OK)
{
/* Initialization Error */
//Error_Handler();
return;
}
/*##-2- Configure the Time #################################################*/
/* Set Time: 02:00:00 */
stimestructure.Hours = 0x02;
stimestructure.Minutes = 0x00;
stimestructure.Seconds = 0x00;
stimestructure.TimeFormat = RTC_HOURFORMAT12_AM;
stimestructure.DayLightSaving = RTC_DAYLIGHTSAVING_NONE ;
stimestructure.StoreOperation = RTC_STOREOPERATION_RESET;
if(HAL_RTC_SetTime(&RtcHandle,&stimestructure,FORMAT_BCD) != HAL_OK)
{
/* Initialization Error */
//Error_Handler();
return;
}
/*##-3- Writes a data in a RTC Backup data Register0 #######################*/
HAL_RTCEx_BKUPWrite(&RtcHandle,RTC_BKP_DR0,0x32F2);
}
/******************************************************************************/
// 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 time;
RTC_DateTypeDef date;
HAL_RTC_GetTime(&RtcHandle, &time, FORMAT_BIN);
HAL_RTC_GetDate(&RtcHandle, &date, FORMAT_BIN);
printf("%02d-%02d-%04d %02d:%02d:%02d\n",date.Date, date.Month, 2000 + date.Year, time.Hours, time.Minutes, time.Seconds);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_0(pyb_rtc_read_obj, pyb_rtc_read);

4
stmhal/rtc.h Normal file
View File

@ -0,0 +1,4 @@
void rtc_init(void);
MP_DECLARE_CONST_FUN_OBJ(pyb_rtc_info_obj);
MP_DECLARE_CONST_FUN_OBJ(pyb_rtc_read_obj);

View File

@ -93,6 +93,68 @@ void HAL_MspDeInit(void) {
USBD_CDC_TIMx_RELEASE_RESET();
}
/**
* @brief RTC MSP Initialization
* This function configures the hardware resources used in this example
* @param hrtc: RTC handle pointer
*
* @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.
*
* @retval None
*/
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 */
/*##-1- Configue LSE as RTC clock soucre ###################################*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
RCC_OscInitStruct.LSEState = RCC_LSE_ON;
RCC_OscInitStruct.LSIState = RCC_LSI_OFF;
if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
//Error_Handler();
return;
}
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
if(HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
{
//Error_Handler();
return;
}
/*##-2- Enable RTC peripheral Clocks #######################################*/
/* Enable RTC Clock */
__HAL_RCC_RTC_ENABLE();
}
/**
* @brief RTC MSP De-Initialization
* This function frees the hardware resources used in this example:
* - Disable the Peripheral's clock
* @param hrtc: RTC handle pointer
* @retval None
*/
void HAL_RTC_MspDeInit(RTC_HandleTypeDef *hrtc)
{
/*##-1- Reset peripherals ##################################################*/
__HAL_RCC_RTC_DISABLE();
}
/**
* @}
*/