This is a RTC additional function. This is only for Nucleo F401RE & F411RE mbed(Added L152RE, F334R8, L476RG & F746xx). If you connected battery backup circuit for internal RTC, you can make a power-off and reset condition. RTC still has proper time and date.
Dependents: Nucleo_rtos_sample PB_Emma_Ethernet
Please refer following NOTE information.
/users/kenjiArai/notebook/nucleo-series-rtc-control-under-power-onoff-and-re/
Revision 14:78e453d7bb85, committed 2016-07-02
- Comitter:
- kenjiArai
- Date:
- Sat Jul 02 03:00:33 2016 +0000
- Parent:
- 13:44e5327acb05
- Commit message:
- Added F746xx and modified main.c
Changed in this revision
diff -r 44e5327acb05 -r 78e453d7bb85 SetRTC.cpp --- a/SetRTC.cpp Fri May 27 20:16:09 2016 +0000 +++ b/SetRTC.cpp Sat Jul 02 03:00:33 2016 +0000 @@ -7,7 +7,7 @@ * http://www.page.sannet.ne.jp/kenjia/index.html * http://mbed.org/users/kenjiArai/ * Created: October 24th, 2014 - * Revised: May 28th, 2016 + * Revised: July 2nd, 2016 * * 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 @@ -18,13 +18,17 @@ #if (defined(TARGET_STM32F401RE) || defined(TARGET_STM32F411RE) \ || defined(TARGET_STM32L152RE) || defined(TARGET_STM32F334R8) \ - || defined(TARGET_STM32L476RG) ) + || defined(TARGET_STM32L476RG) \ + || defined(TARGET_STM32F746NG) || defined(TARGET_STM32F746ZG) ) //#define DEBUG // use Communication with PC(UART) // Include --------------------------------------------------------------------------------------- #include "mbed.h" #include "SetRTC.h" +#if (defined(TARGET_STM32F746NG) || defined(TARGET_STM32F746ZG) ) +#include "stm32f7xx_hal.h" +#endif // Definition ------------------------------------------------------------------------------------ #ifdef DEBUG @@ -79,7 +83,7 @@ // Enable Power Clock __PWR_CLK_ENABLE(); // Enable write access to Backup domain -#if defined(TARGET_STM32L476RG) +#if (defined(TARGET_STM32L476RG) || defined(TARGET_STM32F746NG) || defined(TARGET_STM32F746ZG)) PWR->CR1 |= PWR_CR1_DBP; #else PWR->CR |= PWR_CR_DBP; @@ -87,7 +91,7 @@ // Wait for Backup domain Write protection disable timeout = HAL_GetTick() + DBP_TIMEOUT_VALUE; PRINTF("Time-Out %d\r\n", timeout); -#if defined(TARGET_STM32L476RG) +#if (defined(TARGET_STM32L476RG) || defined(TARGET_STM32F746NG) || defined(TARGET_STM32F746ZG)) while((PWR->CR1 & PWR_CR1_DBP) == RESET) { #else while((PWR->CR & PWR_CR_DBP) == RESET) { @@ -120,7 +124,8 @@ PRINTF("Time-Out %d\r\n", timeout); // Wait till LSE is ready #if (defined(TARGET_STM32F401RE) || defined(TARGET_STM32F411RE) \ - || defined(TARGET_STM32F334R8) || defined(TARGET_STM32L476RG) ) + || defined(TARGET_STM32F334R8) || defined(TARGET_STM32L476RG) \ + || defined(TARGET_STM32F746NG) || defined(TARGET_STM32F746ZG) ) while((RCC->BDCR & 0x02) != 2){ #elif defined(TARGET_STM32L152RE) while(__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET) { @@ -150,14 +155,14 @@ // Enable Power Clock __PWR_CLK_ENABLE(); // Enable write access to Backup domain -#if defined(TARGET_STM32L476RG) +#if (defined(TARGET_STM32L476RG) || defined(TARGET_STM32F746NG) || defined(TARGET_STM32F746ZG)) PWR->CR1 |= PWR_CR1_DBP; #else PWR->CR |= PWR_CR_DBP; #endif // Wait for Backup domain Write protection disable timeout = HAL_GetTick() + DBP_TIMEOUT_VALUE; -#if defined(TARGET_STM32L476RG) +#if (defined(TARGET_STM32L476RG) || defined(TARGET_STM32F746NG) || defined(TARGET_STM32F746ZG)) while((PWR->CR1 & PWR_CR1_DBP) == RESET) { #else while((PWR->CR & PWR_CR_DBP) == RESET) { @@ -177,7 +182,8 @@ } } // Connect LSI to RTC -#if !(defined(TARGET_STM32F334R8) || defined(TARGET_STM32L476RG)) +#if !(defined(TARGET_STM32F334R8) || defined(TARGET_STM32L476RG) \ + || defined(TARGET_STM32F746NG) || defined(TARGET_STM32F746ZG) ) __HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSI); #endif __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI); @@ -197,7 +203,8 @@ // Check backup condition if ( check_RTC_backup_reg() ) { #if (defined(TARGET_STM32F401RE) || defined(TARGET_STM32F411RE) \ - || defined(TARGET_STM32F334R8) || defined(TARGET_STM32L476RG) ) + || defined(TARGET_STM32F334R8) || defined(TARGET_STM32L476RG) \ + || defined(TARGET_STM32F746NG) || defined(TARGET_STM32F746ZG) ) if ((RCC->BDCR & 0x8307) == 0x8103){ #else if ((RCC->CSR & 0x430703) == 0x410300) { @@ -225,7 +232,9 @@ // Enable LSE Oscillator if (set_RTC_LSE() == 1) { // Connect LSE to RTC -#if !(defined(TARGET_STM32F334R8) || defined(TARGET_STM32L476RG)) +#if !(defined(TARGET_STM32F334R8) || defined(TARGET_STM32L476RG) \ + || defined(TARGET_STM32F746NG) || defined(TARGET_STM32F746ZG) \ + || defined(TARGET_STM32F746NG) || defined(TARGET_STM32F746ZG) ) __HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSE); #endif __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE); @@ -291,7 +300,8 @@ ); pcr.printf( "Show RCC registers (only RTC Related reg.)\r\n" ); #if (defined(TARGET_STM32F401RE) || defined(TARGET_STM32F411RE) \ - || defined(TARGET_STM32F334R8) || defined(TARGET_STM32L476RG) ) + || defined(TARGET_STM32F334R8) || defined(TARGET_STM32L476RG) \ + || defined(TARGET_STM32F746NG) || defined(TARGET_STM32F746ZG) ) pcr.printf( " RCC_BDCR=0x...%05x\r\n", RCC->BDCR); #else pcr.printf( " RCC_CSR =0x%08x\r\n", RCC->CSR); @@ -568,7 +578,7 @@ ; // No implementation } #endif // defined(TARGET_STM32L152RE) +#else // defined(TARGET_STM32F401RE,_F411RE,_L152RE,_F334R8,_L476RG, _F746xx) +#error "No suport this mbed, only for Nucleo mbed" +#endif // defined(TARGET_STM32F401RE,_F411RE,_L152RE,_F334R8,_L476RG, _F746xx) -#else // defined(TARGET_STM32F401RE,_F411RE,_L152RE,_F334R8,_L476RG) -#error "No suport this mbed, only for Nucleo mbed" -#endif // defined(TARGET_STM32F401RE,_F411RE,_L152RE,_F334R8,_L476RG)
diff -r 44e5327acb05 -r 78e453d7bb85 modify_info_F746.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/modify_info_F746.h Sat Jul 02 03:00:33 2016 +0000 @@ -0,0 +1,28 @@ +/* + * mbed Application program + * Library differ part (This is only information document) + * + * Copyright (c) 2016 Kenji Arai / JH1PJL + * http://www.page.sannet.ne.jp/kenjia/index.html + * http://mbed.org/users/kenjiArai/ + * Created: July 2nd, 2016 + * Revised: July 2nd, 2016 + * + * 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. + */ +//************************************************************************************************* +// Replace following files in the library +// rtc_api.c -> rtc_api_F746.c +//************************************************************************************************* +// +// (1) Update mbed-dev library if you want +// (2) File rtc_api_F746.c copy to directry +// /mbed-dev/targets/hal/TARGET_STM/TARGET_STM32F7/ +// (3) Delete rtc_api.c in the same directory +// (4) Edit rtc_api_F746.c +// Modify 1st line #if 0 --> #if 1 +//
diff -r 44e5327acb05 -r 78e453d7bb85 modify_info_L476.h --- a/modify_info_L476.h Fri May 27 20:16:09 2016 +0000 +++ b/modify_info_L476.h Sat Jul 02 03:00:33 2016 +0000 @@ -6,7 +6,7 @@ * http://www.page.sannet.ne.jp/kenjia/index.html * http://mbed.org/users/kenjiArai/ * Created: May 28th, 2016 - * Revised: May 28th, 2016 + * Revised: July 2nd, 2016 * * 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 @@ -20,7 +20,7 @@ //************************************************************************************************* // // (1) Update mbed-dev library if you want -// (2) File rtc_api_L152.c copy to directry +// (2) File rtc_api_L476.c copy to directry // /mbed-dev/targets/hal/TARGET_STM/TARGET_STM32L4/ // (3) Delete rtc_api.c in the same directory // (4) Edit rtc_api_L476.c
diff -r 44e5327acb05 -r 78e453d7bb85 rtc_api_F746.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rtc_api_F746.c Sat Jul 02 03:00:33 2016 +0000 @@ -0,0 +1,296 @@ +#if 0 +/* mbed Microcontroller Library + ******************************************************************************* + * Copyright (c) 2015, STMicroelectronics + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************* + */ +#include "rtc_api.h" + +#if DEVICE_RTC + +#include "mbed_error.h" + +#if DEVICE_RTC_LSI +static int rtc_inited = 0; +#endif + +static RTC_HandleTypeDef RtcHandle; + +//#define ORIGINAL +#if defined(ORIGINAL) +#warning "You cannot use SetRTC functions" +#else +#warning "You modified mbed-src rtc_api.c for SetRTC functions" +#endif + +#if defined(ORIGINAL) // modify part1 of 4 (Start) +#else +#define RTC_DAT0 0x55aa55aa +#define RTC_DAT1 0xaa55aa55 + +uint32_t RTC_ReadBackupRegister(uint32_t RTC_BKP_DR) +{ + __IO uint32_t tmp = 0; + + // Check the parameters + assert_param(IS_RTC_BKP(RTC_BKP_DR)); + tmp = RTC_BASE + 0x50; + tmp += (RTC_BKP_DR * 4); + // Read the specified register + return (*(__IO uint32_t *)tmp); +} + +/* Check RTC Backup registers contents */ +uint32_t Check_Rtc_backup_reg( void ) +{ + if ( RTC_ReadBackupRegister( RTC_BKP_DR0 ) == RTC_DAT0 ) { + if ( RTC_ReadBackupRegister( RTC_BKP_DR1 ) == RTC_DAT1 ) { + return 1; + } + } + return 0; +} + +void RTC_WriteBackupRegister(uint32_t RTC_BKP_DR, uint32_t Data) +{ + __IO uint32_t tmp = 0; + + // Check the parameters/ + assert_param(IS_RTC_BKP(RTC_BKP_DR)); + tmp = RTC_BASE + 0x50; + tmp += (RTC_BKP_DR * 4); + // Write the specified register/ + *(__IO uint32_t *)tmp = (uint32_t)Data; +} +#endif // modify part1 of 4 (End) + +void rtc_init(void) +{ + RCC_OscInitTypeDef RCC_OscInitStruct; + uint32_t rtc_freq = 0; + + RtcHandle.Instance = RTC; + +#if defined(ORIGINAL) // modify part2 of 4 (Start) +#else + // Enable Power clock + __PWR_CLK_ENABLE(); + + // Enable access to Backup domain + HAL_PWR_EnableBkUpAccess(); + + // Check backup condition + if ( Check_Rtc_backup_reg() ) { + return; + } +#endif // modify part2 of 4 (End) +#if !DEVICE_RTC_LSI + // Enable LSE Oscillator + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE; + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured! + RCC_OscInitStruct.LSEState = RCC_LSE_ON; // External 32.768 kHz clock on OSC_IN/OSC_OUT + RCC_OscInitStruct.LSIState = RCC_LSI_OFF; + if (HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK) { // Check if LSE has started correctly + // Connect LSE to RTC + __HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSE); + __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE); + rtc_freq = LSE_VALUE; + } else { + error("Cannot initialize RTC with LSE\n"); + } +#else + // Enable Power clock + __PWR_CLK_ENABLE(); + + // Enable access to Backup domain + HAL_PWR_EnableBkUpAccess(); + + // Reset Backup domain + __HAL_RCC_BACKUPRESET_FORCE(); + __HAL_RCC_BACKUPRESET_RELEASE(); + + // Enable LSI clock + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured! + RCC_OscInitStruct.LSEState = RCC_LSE_OFF; + RCC_OscInitStruct.LSIState = RCC_LSI_ON; + if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { + error("Cannot initialize RTC with LSI\n"); + } + // Connect LSI to RTC + __HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSI); + __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI); + // This value is LSI typical value. To be measured precisely using a timer input capture for example. + rtc_freq = LSI_VALUE; +#endif + + // Enable RTC + __HAL_RCC_RTC_ENABLE(); + + RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24; + RtcHandle.Init.AsynchPrediv = 127; + RtcHandle.Init.SynchPrediv = (rtc_freq / 128) - 1; + RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE; + RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; + RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; + +#if defined(ORIGINAL) // modify part3 of 4 (Start) +#else + /* Write backup data into the backup RAM */ + RTC_WriteBackupRegister( RTC_BKP_DR0, RTC_DAT0 ); + RTC_WriteBackupRegister( RTC_BKP_DR1, RTC_DAT1 ); +#endif // modify part3 of 4 (End) + + if (HAL_RTC_Init(&RtcHandle) != HAL_OK) { + error("RTC error: RTC initialization failed."); + } +} + +void rtc_free(void) +{ +#if DEVICE_RTC_LSI + // Enable Power clock + __PWR_CLK_ENABLE(); + + // Enable access to Backup domain + HAL_PWR_EnableBkUpAccess(); + + // Reset Backup domain + __HAL_RCC_BACKUPRESET_FORCE(); + __HAL_RCC_BACKUPRESET_RELEASE(); + + // Disable access to Backup domain + HAL_PWR_DisableBkUpAccess(); +#endif + + // Disable LSI and LSE clocks + RCC_OscInitTypeDef RCC_OscInitStruct; + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; + RCC_OscInitStruct.LSIState = RCC_LSI_OFF; + RCC_OscInitStruct.LSEState = RCC_LSE_OFF; + HAL_RCC_OscConfig(&RCC_OscInitStruct); + +#if DEVICE_RTC_LSI + rtc_inited = 0; +#endif +} + +int rtc_isenabled(void) +{ +#if DEVICE_RTC_LSI + return rtc_inited; +#else +#if defined(ORIGINAL) // modify part4 of 4 (Start) + if ((RTC->ISR & RTC_ISR_INITS) == RTC_ISR_INITS) { + return 1; + } else { + return 0; + } +#else + return Check_Rtc_backup_reg(); +#endif // modify part4 of 4 (End) +#endif +} + +/* + RTC Registers + RTC_WeekDay 1=monday, 2=tuesday, ..., 7=sunday + RTC_Month 1=january, 2=february, ..., 12=december + RTC_Date day of the month 1-31 + RTC_Year year 0-99 + struct tm + tm_sec seconds after the minute 0-61 + tm_min minutes after the hour 0-59 + tm_hour hours since midnight 0-23 + tm_mday day of the month 1-31 + tm_mon months since January 0-11 + tm_year years since 1900 + tm_wday days since Sunday 0-6 + tm_yday days since January 1 0-365 + tm_isdst Daylight Saving Time flag +*/ +time_t rtc_read(void) +{ + RTC_DateTypeDef dateStruct; + RTC_TimeTypeDef timeStruct; + struct tm timeinfo; + + RtcHandle.Instance = RTC; + + // Read actual date and time + // Warning: the time must be read first! + HAL_RTC_GetTime(&RtcHandle, &timeStruct, FORMAT_BIN); + HAL_RTC_GetDate(&RtcHandle, &dateStruct, FORMAT_BIN); + + // Setup a tm structure based on the RTC + timeinfo.tm_wday = dateStruct.WeekDay; + timeinfo.tm_mon = dateStruct.Month - 1; + timeinfo.tm_mday = dateStruct.Date; + timeinfo.tm_year = dateStruct.Year + 100; + timeinfo.tm_hour = timeStruct.Hours; + timeinfo.tm_min = timeStruct.Minutes; + timeinfo.tm_sec = timeStruct.Seconds; + + // Convert to timestamp + time_t t = mktime(&timeinfo); + + return t; +} + +void rtc_write(time_t t) +{ + RTC_DateTypeDef dateStruct; + RTC_TimeTypeDef timeStruct; + + RtcHandle.Instance = RTC; + + // Enable write access to Backup domain + HAL_PWR_EnableBkUpAccess(); + + // Convert the time into a tm + struct tm *timeinfo = localtime(&t); + + // Fill RTC structures + dateStruct.WeekDay = timeinfo->tm_wday; + dateStruct.Month = timeinfo->tm_mon + 1; + dateStruct.Date = timeinfo->tm_mday; + dateStruct.Year = timeinfo->tm_year - 100; + timeStruct.Hours = timeinfo->tm_hour; + timeStruct.Minutes = timeinfo->tm_min; + timeStruct.Seconds = timeinfo->tm_sec; + timeStruct.TimeFormat = RTC_HOURFORMAT12_PM; + timeStruct.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; + timeStruct.StoreOperation = RTC_STOREOPERATION_RESET; + + // Change the RTC current date/time + HAL_RTC_SetDate(&RtcHandle, &dateStruct, FORMAT_BIN); + HAL_RTC_SetTime(&RtcHandle, &timeStruct, FORMAT_BIN); +} + +#endif +#endif