mbed library sources. Supersedes mbed-src. Fixed broken STM32F1xx RTC on rtc_api.c

Dependents:   Nucleo_F103RB_RTC_battery_bkup_pwr_off_okay

Fork of mbed-dev by mbed official

Committer:
maxxir
Date:
Tue Nov 07 16:46:29 2017 +0000
Revision:
177:619788de047e
Parent:
157:ff67d9f36b67
To fix broken RTC on Nucleo_F103RB / STM32F103 BluePill etc..;  Used direct RTC register manipulation for STM32F1xx;  rtc_read() && rtc_write()  (native rtc_init() - works good);  also added stub for non-working on STM32F1xx rtc_read_subseconds().

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 157:ff67d9f36b67 1 /**
<> 157:ff67d9f36b67 2 * @file
<> 157:ff67d9f36b67 3 * @brief System Clock Management (CLKMAN) Function Implementations.
<> 157:ff67d9f36b67 4 */
<> 157:ff67d9f36b67 5 /* *****************************************************************************
<> 157:ff67d9f36b67 6 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
<> 157:ff67d9f36b67 7 *
<> 157:ff67d9f36b67 8 * Permission is hereby granted, free of charge, to any person obtaining a
<> 157:ff67d9f36b67 9 * copy of this software and associated documentation files (the "Software"),
<> 157:ff67d9f36b67 10 * to deal in the Software without restriction, including without limitation
<> 157:ff67d9f36b67 11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
<> 157:ff67d9f36b67 12 * and/or sell copies of the Software, and to permit persons to whom the
<> 157:ff67d9f36b67 13 * Software is furnished to do so, subject to the following conditions:
<> 157:ff67d9f36b67 14 *
<> 157:ff67d9f36b67 15 * The above copyright notice and this permission notice shall be included
<> 157:ff67d9f36b67 16 * in all copies or substantial portions of the Software.
<> 157:ff67d9f36b67 17 *
<> 157:ff67d9f36b67 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
<> 157:ff67d9f36b67 19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
<> 157:ff67d9f36b67 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
<> 157:ff67d9f36b67 21 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
<> 157:ff67d9f36b67 22 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
<> 157:ff67d9f36b67 23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
<> 157:ff67d9f36b67 24 * OTHER DEALINGS IN THE SOFTWARE.
<> 157:ff67d9f36b67 25 *
<> 157:ff67d9f36b67 26 * Except as contained in this notice, the name of Maxim Integrated
<> 157:ff67d9f36b67 27 * Products, Inc. shall not be used except as stated in the Maxim Integrated
<> 157:ff67d9f36b67 28 * Products, Inc. Branding Policy.
<> 157:ff67d9f36b67 29 *
<> 157:ff67d9f36b67 30 * The mere transfer of this software does not imply any licenses
<> 157:ff67d9f36b67 31 * of trade secrets, proprietary technology, copyrights, patents,
<> 157:ff67d9f36b67 32 * trademarks, maskwork rights, or any other form of intellectual
<> 157:ff67d9f36b67 33 * property whatsoever. Maxim Integrated Products, Inc. retains all
<> 157:ff67d9f36b67 34 * ownership rights.
<> 157:ff67d9f36b67 35 *
<> 157:ff67d9f36b67 36 * $Date: 2016-08-15 11:08:12 -0500 (Mon, 15 Aug 2016) $
<> 157:ff67d9f36b67 37 * $Revision: 24058 $
<> 157:ff67d9f36b67 38 *
<> 157:ff67d9f36b67 39 **************************************************************************** */
<> 157:ff67d9f36b67 40
<> 157:ff67d9f36b67 41 /* **** Includes **** */
<> 157:ff67d9f36b67 42 #include "mxc_config.h"
<> 157:ff67d9f36b67 43 #include "mxc_assert.h"
<> 157:ff67d9f36b67 44 #include "clkman.h"
<> 157:ff67d9f36b67 45 #include "pwrseq_regs.h"
<> 157:ff67d9f36b67 46
<> 157:ff67d9f36b67 47 /**
<> 157:ff67d9f36b67 48 * @ingroup clkman
<> 157:ff67d9f36b67 49 * @{
<> 157:ff67d9f36b67 50 */
<> 157:ff67d9f36b67 51
<> 157:ff67d9f36b67 52 /* ************************************************************************* */
<> 157:ff67d9f36b67 53 void CLKMAN_SetSystemClock(clkman_system_source_select_t select, clkman_system_scale_t scale)
<> 157:ff67d9f36b67 54 {
<> 157:ff67d9f36b67 55 MXC_CLKMAN->clk_ctrl = ((MXC_CLKMAN->clk_ctrl & ~MXC_F_CLKMAN_CLK_CTRL_SYSTEM_SOURCE_SELECT) |
<> 157:ff67d9f36b67 56 (MXC_V_CLKMAN_CLK_CTRL_SYSTEM_SOURCE_SELECT_96MHZ_RO));
<> 157:ff67d9f36b67 57
<> 157:ff67d9f36b67 58 switch(select) {
<> 157:ff67d9f36b67 59 case CLKMAN_SYSTEM_SOURCE_96MHZ:
<> 157:ff67d9f36b67 60 default:
<> 157:ff67d9f36b67 61 // Enable and select the 96MHz oscillator
<> 157:ff67d9f36b67 62 MXC_PWRSEQ->reg0 |= (MXC_F_PWRSEQ_REG0_PWR_ROEN_RUN);
<> 157:ff67d9f36b67 63 MXC_PWRSEQ->reg0 &= ~(MXC_F_PWRSEQ_REG0_PWR_OSC_SELECT);
<> 157:ff67d9f36b67 64
<> 157:ff67d9f36b67 65 // Disable the 4MHz oscillator
<> 157:ff67d9f36b67 66 MXC_PWRSEQ->reg0 &= ~MXC_F_PWRSEQ_REG0_PWR_RCEN_RUN;
<> 157:ff67d9f36b67 67
<> 157:ff67d9f36b67 68 // Divide the system clock by the scale
<> 157:ff67d9f36b67 69 MXC_PWRSEQ->reg3 = ((MXC_PWRSEQ->reg3 & ~MXC_F_PWRSEQ_REG3_PWR_RO_DIV) |
<> 157:ff67d9f36b67 70 (scale << MXC_F_PWRSEQ_REG3_PWR_RO_DIV_POS));
<> 157:ff67d9f36b67 71
<> 157:ff67d9f36b67 72 break;
<> 157:ff67d9f36b67 73 case CLKMAN_SYSTEM_SOURCE_4MHZ:
<> 157:ff67d9f36b67 74 // Enable and select the 4MHz oscillator
<> 157:ff67d9f36b67 75 MXC_PWRSEQ->reg0 |= (MXC_F_PWRSEQ_REG0_PWR_RCEN_RUN);
<> 157:ff67d9f36b67 76 MXC_PWRSEQ->reg0 |= (MXC_F_PWRSEQ_REG0_PWR_OSC_SELECT);
<> 157:ff67d9f36b67 77
<> 157:ff67d9f36b67 78 // Disable the 96MHz oscillator
<> 157:ff67d9f36b67 79 MXC_PWRSEQ->reg0 &= ~MXC_F_PWRSEQ_REG0_PWR_ROEN_RUN;
<> 157:ff67d9f36b67 80
<> 157:ff67d9f36b67 81 // 4MHz System source can only be divided down by a maximum factor of 8
<> 157:ff67d9f36b67 82 MXC_ASSERT(scale <= CLKMAN_SYSTEM_SCALE_DIV_8);
<> 157:ff67d9f36b67 83
<> 157:ff67d9f36b67 84 // Divide the system clock by the scale
<> 157:ff67d9f36b67 85 MXC_PWRSEQ->reg3 = ((MXC_PWRSEQ->reg3 & ~MXC_F_PWRSEQ_REG3_PWR_RC_DIV) |
<> 157:ff67d9f36b67 86 (scale << MXC_F_PWRSEQ_REG3_PWR_RC_DIV_POS));
<> 157:ff67d9f36b67 87 break;
<> 157:ff67d9f36b67 88 }
<> 157:ff67d9f36b67 89
<> 157:ff67d9f36b67 90 SystemCoreClockUpdate();
<> 157:ff67d9f36b67 91 }
<> 157:ff67d9f36b67 92
<> 157:ff67d9f36b67 93 /* ************************************************************************* */
<> 157:ff67d9f36b67 94 void CLKMAN_CryptoClockEnable(int enable)
<> 157:ff67d9f36b67 95 {
<> 157:ff67d9f36b67 96 if (enable) {
<> 157:ff67d9f36b67 97 /* Enable oscillator */
<> 157:ff67d9f36b67 98 MXC_CLKMAN->clk_config |= MXC_F_CLKMAN_CLK_CONFIG_CRYPTO_ENABLE;
<> 157:ff67d9f36b67 99 /* Un-gate clock to TPU modules */
<> 157:ff67d9f36b67 100 MXC_CLKMAN->clk_ctrl |= MXC_F_CLKMAN_CLK_CTRL_CRYPTO_CLOCK_ENABLE;
<> 157:ff67d9f36b67 101 } else {
<> 157:ff67d9f36b67 102 /* Gate clock off */
<> 157:ff67d9f36b67 103 MXC_CLKMAN->clk_ctrl &= ~MXC_F_CLKMAN_CLK_CTRL_CRYPTO_CLOCK_ENABLE;
<> 157:ff67d9f36b67 104 /* Disable oscillator */
<> 157:ff67d9f36b67 105 MXC_CLKMAN->clk_config &= ~MXC_F_CLKMAN_CLK_CONFIG_CRYPTO_ENABLE;
<> 157:ff67d9f36b67 106 }
<> 157:ff67d9f36b67 107 }
<> 157:ff67d9f36b67 108
<> 157:ff67d9f36b67 109 /* ************************************************************************* */
<> 157:ff67d9f36b67 110 void CLKMAN_SetClkScale(clkman_clk_t clk, clkman_scale_t scale)
<> 157:ff67d9f36b67 111 {
<> 157:ff67d9f36b67 112 volatile uint32_t *clk_ctrl_reg;
<> 157:ff67d9f36b67 113
<> 157:ff67d9f36b67 114 MXC_ASSERT(clk <= CLKMAN_CLK_MAX);
<> 157:ff67d9f36b67 115 MXC_ASSERT(scale != CLKMAN_SCALE_AUTO);
<> 157:ff67d9f36b67 116
<> 157:ff67d9f36b67 117 if (clk < CLKMAN_CRYPTO_CLK_AES) {
<> 157:ff67d9f36b67 118 clk_ctrl_reg = &MXC_CLKMAN->sys_clk_ctrl_0_cm4 + clk;
<> 157:ff67d9f36b67 119 } else {
<> 157:ff67d9f36b67 120 clk_ctrl_reg = &MXC_CLKMAN->crypt_clk_ctrl_0_aes + (clk - CLKMAN_CRYPTO_CLK_AES);
<> 157:ff67d9f36b67 121 }
<> 157:ff67d9f36b67 122
<> 157:ff67d9f36b67 123 *clk_ctrl_reg = scale;
<> 157:ff67d9f36b67 124 }
<> 157:ff67d9f36b67 125
<> 157:ff67d9f36b67 126 /* ************************************************************************* */
<> 157:ff67d9f36b67 127 clkman_scale_t CLKMAN_GetClkScale(clkman_clk_t clk)
<> 157:ff67d9f36b67 128 {
<> 157:ff67d9f36b67 129 volatile uint32_t *clk_ctrl_reg;
<> 157:ff67d9f36b67 130 MXC_ASSERT(clk <= CLKMAN_CLK_MAX);
<> 157:ff67d9f36b67 131
<> 157:ff67d9f36b67 132 if (clk < CLKMAN_CRYPTO_CLK_AES) {
<> 157:ff67d9f36b67 133 clk_ctrl_reg = &MXC_CLKMAN->sys_clk_ctrl_0_cm4 + clk;
<> 157:ff67d9f36b67 134 } else {
<> 157:ff67d9f36b67 135 clk_ctrl_reg = &MXC_CLKMAN->crypt_clk_ctrl_0_aes + (clk - CLKMAN_CRYPTO_CLK_AES);
<> 157:ff67d9f36b67 136 }
<> 157:ff67d9f36b67 137
<> 157:ff67d9f36b67 138 return (clkman_scale_t)*clk_ctrl_reg;
<> 157:ff67d9f36b67 139 }
<> 157:ff67d9f36b67 140
<> 157:ff67d9f36b67 141 /* ************************************************************************* */
<> 157:ff67d9f36b67 142 void CLKMAN_ClockGate(clkman_enable_clk_t clk, int enable)
<> 157:ff67d9f36b67 143 {
<> 157:ff67d9f36b67 144 if (enable) {
<> 157:ff67d9f36b67 145 MXC_CLKMAN->clk_ctrl |= clk;
<> 157:ff67d9f36b67 146 } else {
<> 157:ff67d9f36b67 147 MXC_CLKMAN->clk_ctrl &= ~clk;
<> 157:ff67d9f36b67 148 }
<> 157:ff67d9f36b67 149 }
<> 157:ff67d9f36b67 150
<> 157:ff67d9f36b67 151 /* ************************************************************************ */
<> 157:ff67d9f36b67 152 int CLKMAN_WdtClkSelect(unsigned int idx, clkman_wdt_clk_select_t select)
<> 157:ff67d9f36b67 153 {
<> 157:ff67d9f36b67 154 MXC_ASSERT(idx < MXC_CFG_WDT_INSTANCES);
<> 157:ff67d9f36b67 155
<> 157:ff67d9f36b67 156 if (select == CLKMAN_WDT_SELECT_DISABLED) {
<> 157:ff67d9f36b67 157 if (idx == 0) {
<> 157:ff67d9f36b67 158 MXC_CLKMAN->clk_ctrl &= ~MXC_F_CLKMAN_CLK_CTRL_WDT0_CLOCK_ENABLE;
<> 157:ff67d9f36b67 159 } else if (idx == 1) {
<> 157:ff67d9f36b67 160 MXC_CLKMAN->clk_ctrl &= ~MXC_F_CLKMAN_CLK_CTRL_WDT1_CLOCK_ENABLE;
<> 157:ff67d9f36b67 161 } else {
<> 157:ff67d9f36b67 162 return E_BAD_PARAM;
<> 157:ff67d9f36b67 163 }
<> 157:ff67d9f36b67 164 } else {
<> 157:ff67d9f36b67 165 if (idx == 0) {
<> 157:ff67d9f36b67 166 MXC_CLKMAN->clk_ctrl = (MXC_CLKMAN->clk_ctrl & ~MXC_F_CLKMAN_CLK_CTRL_WDT0_CLOCK_SELECT) |
<> 157:ff67d9f36b67 167 MXC_F_CLKMAN_CLK_CTRL_WDT0_CLOCK_ENABLE |
<> 157:ff67d9f36b67 168 ((select << MXC_F_CLKMAN_CLK_CTRL_WDT0_CLOCK_SELECT_POS) & MXC_F_CLKMAN_CLK_CTRL_WDT0_CLOCK_SELECT);
<> 157:ff67d9f36b67 169 } else if (idx == 1) {
<> 157:ff67d9f36b67 170 MXC_CLKMAN->clk_ctrl = (MXC_CLKMAN->clk_ctrl & ~MXC_F_CLKMAN_CLK_CTRL_WDT1_CLOCK_SELECT) |
<> 157:ff67d9f36b67 171 MXC_F_CLKMAN_CLK_CTRL_WDT1_CLOCK_ENABLE |
<> 157:ff67d9f36b67 172 ((select << MXC_F_CLKMAN_CLK_CTRL_WDT1_CLOCK_SELECT_POS) & MXC_F_CLKMAN_CLK_CTRL_WDT1_CLOCK_SELECT);
<> 157:ff67d9f36b67 173 } else {
<> 157:ff67d9f36b67 174 return E_BAD_PARAM;
<> 157:ff67d9f36b67 175 }
<> 157:ff67d9f36b67 176 }
<> 157:ff67d9f36b67 177
<> 157:ff67d9f36b67 178 return E_NO_ERROR;
<> 157:ff67d9f36b67 179 }
<> 157:ff67d9f36b67 180
<> 157:ff67d9f36b67 181 /* ************************************************************************* */
<> 157:ff67d9f36b67 182 /* NOTE: CLKMAN_TrimRO() is implemented in system_max32XXX.c */
<> 157:ff67d9f36b67 183 /* ************************************************************************* */
<> 157:ff67d9f36b67 184
<> 157:ff67d9f36b67 185 /**@} end of group clkman */