Skip to content
This repository has been archived by the owner on Mar 24, 2024. It is now read-only.

Commit

Permalink
STM32 RTC Hardware Support & User Interface for Watch and Calendar (#128
Browse files Browse the repository at this point in the history
)

* Update rg_main.c

Added a menu to view and configure RTC parameters. It is activated by pressing 'TIME' button in retro-go main menu.

* Update main.c

Enable LSE oscillator and enable it as RTC clock source. Additionally enable CRS Sync LSE.

* Added time display to RTC menu

Added time display to RTC menu and also modified RTC initialization code so that it does not set the RTC date and time. Assuming that a fresh RTC has sensible values.

* Add date display to time menu

Added date display to time menu

* Fix predivs for LSE

Fixed predivs for LSE clock

* move RTC stuff to rg_rtc

Consolidate all RTC functions to rg_rtc. So far getters have been implemented only.

* Time setup UI + setters for Time

Setters for Time have been implemented and Time setup has been added into the UI to use them.

* Date setup added

Date setup with callbacks added

* Add real time datetime display

Modified the time menu to display and update in real time.

* Add a function to return Unix time

Unix time is returned as a 64-bit time_t. Casting to uint32_t will yield time in seconds since 1st Jan 1970.

* Fix setters' return type

Fixing warnings about wrong setter return type.

* Update rg_rtc.h

Missed the .h while updating return types of setters.

* Update rg_rtc.c

Add a nicer year display

* Reactivate LSI

This should fix an issue with DACs that causes Brightness control not to work.
  • Loading branch information
northskysl authored Aug 12, 2021
1 parent 5b8a944 commit d9542eb
Show file tree
Hide file tree
Showing 5 changed files with 488 additions and 5 deletions.
61 changes: 61 additions & 0 deletions Core/Inc/retro-go/rg_rtc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#ifndef _GW_RTC_H_
#define _GW_RTC_H_

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "stm32h7xx_hal.h"
#include "odroid_system.h"

/* Exported constants --------------------------------------------------------*/
extern RTC_TimeTypeDef GW_currentTime;
extern RTC_DateTypeDef GW_currentDate;
extern const char * GW_RTC_Weekday[];

/* Exported functions prototypes ---------------------------------------------*/

// Getters
uint8_t GW_GetCurrentHour(void);
uint8_t GW_GetCurrentMinute(void);
uint8_t GW_GetCurrentSecond(void);

uint8_t GW_GetCurrentMonth(void);
uint8_t GW_GetCurrentDay(void);

uint8_t GW_GetCurrentWeekday(void);
uint8_t GW_GetCurrentYear(void);

time_t GW_GetUnixTime(void);

// Setters
void GW_SetCurrentHour(const uint8_t hour);
void GW_SetCurrentMinute(const uint8_t minute);
void GW_SetCurrentSecond(const uint8_t second);

void GW_SetCurrentMonth(const uint8_t month);
void GW_SetCurrentDay(const uint8_t day);

void GW_SetCurrentWeekday(const uint8_t weekday);
void GW_SetCurrentYear(const uint8_t year);

// Callbacks for UI purposes
bool hour_update_cb(odroid_dialog_choice_t *option, odroid_dialog_event_t event, uint32_t repeat);
bool minute_update_cb(odroid_dialog_choice_t *option, odroid_dialog_event_t event, uint32_t repeat);
bool second_update_cb(odroid_dialog_choice_t *option, odroid_dialog_event_t event, uint32_t repeat);

bool month_update_cb(odroid_dialog_choice_t *option, odroid_dialog_event_t event, uint32_t repeat);
bool day_update_cb(odroid_dialog_choice_t *option, odroid_dialog_event_t event, uint32_t repeat);

bool weekday_update_cb(odroid_dialog_choice_t *option, odroid_dialog_event_t event, uint32_t repeat);
bool year_update_cb(odroid_dialog_choice_t *option, odroid_dialog_event_t event, uint32_t repeat);

bool time_display_cb(odroid_dialog_choice_t *option, odroid_dialog_event_t event, uint32_t repeat);
bool date_display_cb(odroid_dialog_choice_t *option, odroid_dialog_event_t event, uint32_t repeat);

#ifdef __cplusplus
}
#endif

#endif
31 changes: 26 additions & 5 deletions Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ void SystemClock_Config(void)
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
RCC_CRSInitTypeDef RCC_CRSInitStruct = {0};

/** Supply configuration update enable
*/
Expand All @@ -532,13 +533,20 @@ void SystemClock_Config(void)
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE0);

while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}

/** Configure LSE Drive Capability
*/
HAL_PWR_EnableBkUpAccess();
__HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_HIGH);

/** Macro to configure the PLL clock source
*/
__HAL_RCC_PLL_PLLSOURCE_CONFIG(RCC_PLLSOURCE_HSI);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI;
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_LSE;
RCC_OscInitStruct.LSEState = RCC_LSE_ON;
RCC_OscInitStruct.HSIState = RCC_HSI_DIV1;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
Expand Down Expand Up @@ -598,13 +606,26 @@ void SystemClock_Config(void)
PeriphClkInitStruct.Sai1ClockSelection = RCC_SAI1CLKSOURCE_PLL2;
PeriphClkInitStruct.Spi123ClockSelection = RCC_SPI123CLKSOURCE_CLKP;
PeriphClkInitStruct.AdcClockSelection = RCC_ADCCLKSOURCE_PLL2;
PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
PeriphClkInitStruct.TIMPresSelection = RCC_TIMPRES_ACTIVATED;

if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Enable the SYSCFG APB clock
*/
__HAL_RCC_CRS_CLK_ENABLE();
/** Configures CRS
*/
RCC_CRSInitStruct.Prescaler = RCC_CRS_SYNC_DIV1;
RCC_CRSInitStruct.Source = RCC_CRS_SYNC_SOURCE_LSE;
RCC_CRSInitStruct.Polarity = RCC_CRS_SYNC_POLARITY_RISING;
RCC_CRSInitStruct.ReloadValue = __HAL_RCC_CRS_RELOADVALUE_CALCULATE(48000000,32768);
RCC_CRSInitStruct.ErrorLimitValue = 34;
RCC_CRSInitStruct.HSI48CalibrationValue = 32;

HAL_RCCEx_CRSConfig(&RCC_CRSInitStruct);
}

/**
Expand Down Expand Up @@ -912,8 +933,8 @@ static void MX_RTC_Init(void)
*/
hrtc.Instance = RTC;
hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
hrtc.Init.AsynchPrediv = 0; // Currently this is configured to allow for profiling
hrtc.Init.SynchPrediv = 32250; // This the internal 32khz oscillator calibrated to one specific unit
hrtc.Init.AsynchPrediv = 127; // Recommended value from application note for LSE, the higher the value the better the accuracy and power consumption
hrtc.Init.SynchPrediv = 255; // Recommended value from application note for LSE
hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
Expand All @@ -924,7 +945,7 @@ static void MX_RTC_Init(void)
}

/* USER CODE BEGIN Check_RTC_BKUP */

return; // Retain RTC values on boot
/* USER CODE END Check_RTC_BKUP */

/** Initialize RTC and set the Time and Date
Expand Down
51 changes: 51 additions & 0 deletions Core/Src/retro-go/rg_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "main.h"
#include "gw_buttons.h"
#include "gw_flash.h"
#include "rg_rtc.h"

#if 0
#define KEY_SELECTED_TAB "SelectedTab"
Expand Down Expand Up @@ -159,6 +160,8 @@ void retro_loop()
int selected_tab_last = -1;
uint32_t idle_s;



// Read the initial state as to not trigger on button held down during boot
odroid_input_read_gamepad(&gui.joystick);

Expand Down Expand Up @@ -302,6 +305,54 @@ void retro_loop()
odroid_overlay_settings_menu(choices);
gui_redraw();
}
// TIME menu
else if (last_key == ODROID_INPUT_SELECT) {

char time_str[14];
char date_str[24];

odroid_dialog_choice_t rtcinfo[] = {
{0, "Time", time_str, 1, &time_display_cb},
{1, "Date", date_str, 1, &date_display_cb},
ODROID_DIALOG_CHOICE_LAST
};
int sel = odroid_overlay_dialog("TIME", rtcinfo, 0);

if (sel == 0) {
static char hour_value[8];
static char minute_value[8];
static char second_value[8];

// Time setup
odroid_dialog_choice_t timeoptions[32] = {
{0, "Hour", hour_value, 1, &hour_update_cb},
{1, "Minute", minute_value, 1, &minute_update_cb},
{2, "Second", second_value, 1, &second_update_cb},
ODROID_DIALOG_CHOICE_LAST
};
sel = odroid_overlay_dialog("Time setup", timeoptions, 0);
}
else if (sel == 1) {

static char day_value[8];
static char month_value[8];
static char year_value[8];
static char weekday_value[8];

// Date setup
odroid_dialog_choice_t dateoptions[32] = {
{0, "Day", day_value, 1, &day_update_cb},
{1, "Month", month_value, 1, &month_update_cb},
{2, "Year", year_value, 1, &year_update_cb},
{3, "Weekday", weekday_value, 1, &weekday_update_cb},
ODROID_DIALOG_CHOICE_LAST
};
sel = odroid_overlay_dialog("Date setup", dateoptions, 0);
}

(void) sel;
gui_redraw();
}
else if (last_key == ODROID_INPUT_UP) {
gui_scroll_list(tab, LINE_UP);
repeat++;
Expand Down
Loading

0 comments on commit d9542eb

Please sign in to comment.