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:
150:02e0a0aed4ec
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
<> 150:02e0a0aed4ec 1 /*******************************************************************************
<> 150:02e0a0aed4ec 2 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
<> 150:02e0a0aed4ec 3 *
<> 150:02e0a0aed4ec 4 * Permission is hereby granted, free of charge, to any person obtaining a
<> 150:02e0a0aed4ec 5 * copy of this software and associated documentation files (the "Software"),
<> 150:02e0a0aed4ec 6 * to deal in the Software without restriction, including without limitation
<> 150:02e0a0aed4ec 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
<> 150:02e0a0aed4ec 8 * and/or sell copies of the Software, and to permit persons to whom the
<> 150:02e0a0aed4ec 9 * Software is furnished to do so, subject to the following conditions:
<> 150:02e0a0aed4ec 10 *
<> 150:02e0a0aed4ec 11 * The above copyright notice and this permission notice shall be included
<> 150:02e0a0aed4ec 12 * in all copies or substantial portions of the Software.
<> 150:02e0a0aed4ec 13 *
<> 150:02e0a0aed4ec 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
<> 150:02e0a0aed4ec 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
<> 150:02e0a0aed4ec 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
<> 150:02e0a0aed4ec 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
<> 150:02e0a0aed4ec 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
<> 150:02e0a0aed4ec 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
<> 150:02e0a0aed4ec 20 * OTHER DEALINGS IN THE SOFTWARE.
<> 150:02e0a0aed4ec 21 *
<> 150:02e0a0aed4ec 22 * Except as contained in this notice, the name of Maxim Integrated
<> 150:02e0a0aed4ec 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
<> 150:02e0a0aed4ec 24 * Products, Inc. Branding Policy.
<> 150:02e0a0aed4ec 25 *
<> 150:02e0a0aed4ec 26 * The mere transfer of this software does not imply any licenses
<> 150:02e0a0aed4ec 27 * of trade secrets, proprietary technology, copyrights, patents,
<> 150:02e0a0aed4ec 28 * trademarks, maskwork rights, or any other form of intellectual
<> 150:02e0a0aed4ec 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
<> 150:02e0a0aed4ec 30 * ownership rights.
<> 150:02e0a0aed4ec 31 *
<> 150:02e0a0aed4ec 32 * $Date: 2016-03-21 14:44:55 -0500 (Mon, 21 Mar 2016) $
<> 150:02e0a0aed4ec 33 * $Revision: 22017 $
<> 150:02e0a0aed4ec 34 *
<> 150:02e0a0aed4ec 35 ******************************************************************************/
<> 150:02e0a0aed4ec 36
<> 150:02e0a0aed4ec 37 /**
<> 150:02e0a0aed4ec 38 * @file pt.h
<> 150:02e0a0aed4ec 39 * @addtogroup pt Pulse Train
<> 150:02e0a0aed4ec 40 * @brief This is the high level API for the pulse train module
<> 150:02e0a0aed4ec 41 * @{
<> 150:02e0a0aed4ec 42 *
<> 150:02e0a0aed4ec 43 */
<> 150:02e0a0aed4ec 44
<> 150:02e0a0aed4ec 45 #ifndef _PT_H
<> 150:02e0a0aed4ec 46 #define _PT_H
<> 150:02e0a0aed4ec 47
<> 150:02e0a0aed4ec 48 #include "mxc_config.h"
<> 150:02e0a0aed4ec 49 #include "pt_regs.h"
<> 150:02e0a0aed4ec 50 #include "mxc_assert.h"
<> 150:02e0a0aed4ec 51 #include "mxc_sys.h"
<> 150:02e0a0aed4ec 52
<> 150:02e0a0aed4ec 53 #ifdef __cplusplus
<> 150:02e0a0aed4ec 54 extern "C" {
<> 150:02e0a0aed4ec 55 #endif
<> 150:02e0a0aed4ec 56
<> 150:02e0a0aed4ec 57 /// @struct pt_pt_cfg_t
<> 150:02e0a0aed4ec 58 /// Pulse train mode configuration structure. @note Do not use for square wave
<> 150:02e0a0aed4ec 59 typedef struct {
<> 150:02e0a0aed4ec 60 uint32_t bps; ///< pulse train bit rate
<> 150:02e0a0aed4ec 61 uint32_t pattern; ///< Output pattern to shift out, starts at LSB
<> 150:02e0a0aed4ec 62 uint8_t ptLength; ///< Number of bits in pulse train, 0 = 32bits, 1 = non valid , 2 = 2 bits, ...
<> 150:02e0a0aed4ec 63 uint16_t loop; ///< Number of times to repeat the train, 0 = continuous
<> 150:02e0a0aed4ec 64 uint16_t loopDelay; ///< Delay between loops specified in bits Example: loopDelay = 4, delays time = time it takes to shift out 4 bits
<> 150:02e0a0aed4ec 65 } pt_pt_cfg_t;
<> 150:02e0a0aed4ec 66
<> 150:02e0a0aed4ec 67 /**
<> 150:02e0a0aed4ec 68 * @brief This function initializes pulse train modules to a known stopped state
<> 150:02e0a0aed4ec 69 * and sets the global PT clock scale
<> 150:02e0a0aed4ec 70 * @param clk_scale scale the system clock for the global PT clock
<> 150:02e0a0aed4ec 71 */
<> 150:02e0a0aed4ec 72 void PT_Init(sys_pt_clk_scale clk_scale);
<> 150:02e0a0aed4ec 73
<> 150:02e0a0aed4ec 74 /**
<> 150:02e0a0aed4ec 75 * @brief Configures the pulse train in the specified mode.
<> 150:02e0a0aed4ec 76 * @details The parameters in config structure must be set before calling this function.
<> 150:02e0a0aed4ec 77 * This function should be used for configuring pulse train mode only.
<> 150:02e0a0aed4ec 78 * @note The pulse train cannot be running when this function is called
<> 150:02e0a0aed4ec 79 *
<> 150:02e0a0aed4ec 80 * @param pt pulse train to operate on
<> 150:02e0a0aed4ec 81 * @param cfg pointer to pulse train configuration
<> 150:02e0a0aed4ec 82 * @param sysCfg pointer to pulse train system GPIO configuration
<> 150:02e0a0aed4ec 83 *
<> 150:02e0a0aed4ec 84 * @returns E_NO_ERROR if everything is successful, error if unsuccessful.
<> 150:02e0a0aed4ec 85 */
<> 150:02e0a0aed4ec 86 int PT_PTConfig(mxc_pt_regs_t *pt, pt_pt_cfg_t *cfg, const sys_cfg_pt_t *sysCfg);
<> 150:02e0a0aed4ec 87
<> 150:02e0a0aed4ec 88 /**
<> 150:02e0a0aed4ec 89 * @brief Configures the pulse train in the square wave mode.
<> 150:02e0a0aed4ec 90 * @details This function should be used for configuring square wave mode only.
<> 150:02e0a0aed4ec 91 * @note The pulse train cannot be running when this function is called
<> 150:02e0a0aed4ec 92 *
<> 150:02e0a0aed4ec 93 * @param pt pulse train to operate on
<> 150:02e0a0aed4ec 94 * @param freq square wave output frequency in Hz
<> 150:02e0a0aed4ec 95 * @param sysCfg pointer to pulse train system GPIO configuration
<> 150:02e0a0aed4ec 96 *
<> 150:02e0a0aed4ec 97 * @returns E_NO_ERROR if everything is successful, error if unsuccessful.
<> 150:02e0a0aed4ec 98 */
<> 150:02e0a0aed4ec 99 int PT_SqrWaveConfig(mxc_pt_regs_t *pt, uint32_t freq, const sys_cfg_pt_t *sysCfg);
<> 150:02e0a0aed4ec 100
<> 150:02e0a0aed4ec 101 /**
<> 150:02e0a0aed4ec 102 * @brief Starts the pulse train specified
<> 150:02e0a0aed4ec 103 *
<> 150:02e0a0aed4ec 104 * @param pt pulse train to operate on
<> 150:02e0a0aed4ec 105 */
<> 150:02e0a0aed4ec 106 __STATIC_INLINE void PT_Start(mxc_pt_regs_t *pt)
<> 150:02e0a0aed4ec 107 {
<> 150:02e0a0aed4ec 108 int ptIndex = MXC_PT_GET_IDX(pt);
<> 150:02e0a0aed4ec 109
<> 150:02e0a0aed4ec 110 MXC_PTG->enable |= (1 << ptIndex);
<> 150:02e0a0aed4ec 111
<> 150:02e0a0aed4ec 112 //wait for PT to start
<> 150:02e0a0aed4ec 113 while( (MXC_PTG->enable & (1 << ptIndex)) == 0 );
<> 150:02e0a0aed4ec 114 }
<> 150:02e0a0aed4ec 115
<> 150:02e0a0aed4ec 116 /**
<> 150:02e0a0aed4ec 117 * @brief Start multiple pulse train modules together.
<> 150:02e0a0aed4ec 118 *
<> 150:02e0a0aed4ec 119 * @param pts set the bits of pulse trains to start
<> 150:02e0a0aed4ec 120 * Bit0-\>pt0, Bit1-\>pt1... etc.
<> 150:02e0a0aed4ec 121 */
<> 150:02e0a0aed4ec 122 __STATIC_INLINE void PT_StartMulti(uint32_t pts)
<> 150:02e0a0aed4ec 123 {
<> 150:02e0a0aed4ec 124 MXC_PTG->enable |= pts;
<> 150:02e0a0aed4ec 125
<> 150:02e0a0aed4ec 126 //wait for PTs to start
<> 150:02e0a0aed4ec 127 while( (MXC_PTG->enable & pts) != pts );
<> 150:02e0a0aed4ec 128 }
<> 150:02e0a0aed4ec 129
<> 150:02e0a0aed4ec 130 /**
<> 150:02e0a0aed4ec 131 * @brief Stops a pulse train
<> 150:02e0a0aed4ec 132 *
<> 150:02e0a0aed4ec 133 * @param pt pulse train to operate on
<> 150:02e0a0aed4ec 134 */
<> 150:02e0a0aed4ec 135 __STATIC_INLINE void PT_Stop(mxc_pt_regs_t *pt)
<> 150:02e0a0aed4ec 136 {
<> 150:02e0a0aed4ec 137 int ptIndex = MXC_PT_GET_IDX(pt);
<> 150:02e0a0aed4ec 138
<> 150:02e0a0aed4ec 139 MXC_PTG->enable &= ~(1 << ptIndex);
<> 150:02e0a0aed4ec 140 }
<> 150:02e0a0aed4ec 141
<> 150:02e0a0aed4ec 142 /**
<> 150:02e0a0aed4ec 143 * @brief Stop multiple pulse trains together
<> 150:02e0a0aed4ec 144 *
<> 150:02e0a0aed4ec 145 * @param pts set the bits of pulse trains to stop
<> 150:02e0a0aed4ec 146 * Bit0-\>pt0, Bit1-\>pt1... etc.
<> 150:02e0a0aed4ec 147 */
<> 150:02e0a0aed4ec 148 __STATIC_INLINE void PT_StopMulti(uint32_t pts)
<> 150:02e0a0aed4ec 149 {
<> 150:02e0a0aed4ec 150 MXC_PTG->enable &= ~(pts);
<> 150:02e0a0aed4ec 151 }
<> 150:02e0a0aed4ec 152
<> 150:02e0a0aed4ec 153 /**
<> 150:02e0a0aed4ec 154 * @brief Determines if the pulse train is running
<> 150:02e0a0aed4ec 155 *
<> 150:02e0a0aed4ec 156 * @param pt pulse train to operate on
<> 150:02e0a0aed4ec 157 *
<> 150:02e0a0aed4ec 158 * @return 0 = pulse train is off, non-zero = pulse train is on
<> 150:02e0a0aed4ec 159 */
<> 150:02e0a0aed4ec 160 __STATIC_INLINE uint32_t PT_IsActive(mxc_pt_regs_t *pt)
<> 150:02e0a0aed4ec 161 {
<> 150:02e0a0aed4ec 162 int ptIndex = MXC_PT_GET_IDX(pt);
<> 150:02e0a0aed4ec 163
<> 150:02e0a0aed4ec 164 return (!!(MXC_PTG->enable & (1 << ptIndex)));
<> 150:02e0a0aed4ec 165 }
<> 150:02e0a0aed4ec 166
<> 150:02e0a0aed4ec 167 /**
<> 150:02e0a0aed4ec 168 * @brief Determines if the pulse trains selected are running
<> 150:02e0a0aed4ec 169 *
<> 150:02e0a0aed4ec 170 * @param pts set the bits of pulse trains to check
<> 150:02e0a0aed4ec 171 * Bit0-\>pt0, Bit1-\>pt1... etc.
<> 150:02e0a0aed4ec 172 *
<> 150:02e0a0aed4ec 173 * @return 0 = all pulse trains are off, non-zero = at least one pulse train is on
<> 150:02e0a0aed4ec 174 */
<> 150:02e0a0aed4ec 175 __STATIC_INLINE uint32_t PT_IsActiveMulti(uint32_t pts)
<> 150:02e0a0aed4ec 176 {
<> 150:02e0a0aed4ec 177 return (MXC_PTG->enable & pts);
<> 150:02e0a0aed4ec 178 }
<> 150:02e0a0aed4ec 179
<> 150:02e0a0aed4ec 180 /**
<> 150:02e0a0aed4ec 181 * @brief Sets the pattern of the pulse train
<> 150:02e0a0aed4ec 182 *
<> 150:02e0a0aed4ec 183 * @param pt Pointer to pulse train to operate on
<> 150:02e0a0aed4ec 184 * @param pattern output pattern
<> 150:02e0a0aed4ec 185 *
<> 150:02e0a0aed4ec 186 */
<> 150:02e0a0aed4ec 187 __STATIC_INLINE void PT_SetPattern(mxc_pt_regs_t *pt, uint32_t pattern)
<> 150:02e0a0aed4ec 188 {
<> 150:02e0a0aed4ec 189 pt->train = pattern;
<> 150:02e0a0aed4ec 190 }
<> 150:02e0a0aed4ec 191
<> 150:02e0a0aed4ec 192 /**
<> 150:02e0a0aed4ec 193 * @brief Enable pulse train interrupt
<> 150:02e0a0aed4ec 194 *
<> 150:02e0a0aed4ec 195 * @param pts Pointer to pulse train to operate on
<> 150:02e0a0aed4ec 196 */
<> 150:02e0a0aed4ec 197 __STATIC_INLINE void PT_EnableINT(mxc_pt_regs_t *pt)
<> 150:02e0a0aed4ec 198 {
<> 150:02e0a0aed4ec 199 int ptIndex = MXC_PT_GET_IDX(pt);
<> 150:02e0a0aed4ec 200
<> 150:02e0a0aed4ec 201 MXC_PTG->inten |= (1 << ptIndex);
<> 150:02e0a0aed4ec 202 }
<> 150:02e0a0aed4ec 203
<> 150:02e0a0aed4ec 204 /**
<> 150:02e0a0aed4ec 205 * @brief Enable interrupts for the pulse trains selected
<> 150:02e0a0aed4ec 206 *
<> 150:02e0a0aed4ec 207 * @param pts set the bits of pulse trains to enable
<> 150:02e0a0aed4ec 208 * Bit0-\>pt0, Bit1-\>pt1... etc.
<> 150:02e0a0aed4ec 209 */
<> 150:02e0a0aed4ec 210 __STATIC_INLINE void PT_EnableINTMulti(uint32_t pts)
<> 150:02e0a0aed4ec 211 {
<> 150:02e0a0aed4ec 212 MXC_PTG->inten |= pts;
<> 150:02e0a0aed4ec 213 }
<> 150:02e0a0aed4ec 214
<> 150:02e0a0aed4ec 215 /**
<> 150:02e0a0aed4ec 216 * @brief Disable pulse train interrupt
<> 150:02e0a0aed4ec 217 *
<> 150:02e0a0aed4ec 218 * @param pts pulse train to operate on
<> 150:02e0a0aed4ec 219 */
<> 150:02e0a0aed4ec 220 __STATIC_INLINE void PT_DisableINT(mxc_pt_regs_t *pt)
<> 150:02e0a0aed4ec 221 {
<> 150:02e0a0aed4ec 222 int ptIndex = MXC_PT_GET_IDX(pt);
<> 150:02e0a0aed4ec 223
<> 150:02e0a0aed4ec 224 MXC_PTG->inten &= ~(1 << ptIndex);
<> 150:02e0a0aed4ec 225 }
<> 150:02e0a0aed4ec 226
<> 150:02e0a0aed4ec 227 /**
<> 150:02e0a0aed4ec 228 * @brief Disable interrupts for the pulse trains selected
<> 150:02e0a0aed4ec 229 *
<> 150:02e0a0aed4ec 230 * @param pts set the bits of pulse trains to enable
<> 150:02e0a0aed4ec 231 * Bit0-\>pt0, Bit1-\>pt1... etc.
<> 150:02e0a0aed4ec 232 */
<> 150:02e0a0aed4ec 233 __STATIC_INLINE void PT_DisableINTMulti(uint32_t pts)
<> 150:02e0a0aed4ec 234 {
<> 150:02e0a0aed4ec 235 MXC_PTG->inten &= ~pts;
<> 150:02e0a0aed4ec 236 }
<> 150:02e0a0aed4ec 237 /**
<> 150:02e0a0aed4ec 238 * @brief Gets the pulse trains's interrupt flags
<> 150:02e0a0aed4ec 239 *
<> 150:02e0a0aed4ec 240 * @return The Pulse Train Flags, bits that are 1 are set.
<> 150:02e0a0aed4ec 241 */
<> 150:02e0a0aed4ec 242 __STATIC_INLINE uint32_t PT_GetFlags(void)
<> 150:02e0a0aed4ec 243 {
<> 150:02e0a0aed4ec 244 return MXC_PTG->intfl;
<> 150:02e0a0aed4ec 245 }
<> 150:02e0a0aed4ec 246
<> 150:02e0a0aed4ec 247 /**
<> 150:02e0a0aed4ec 248 * @brief Clears the pulse train's interrupt flag
<> 150:02e0a0aed4ec 249 *
<> 150:02e0a0aed4ec 250 * @param mask bits to clear
<> 150:02e0a0aed4ec 251 *
<> 150:02e0a0aed4ec 252 */
<> 150:02e0a0aed4ec 253 __STATIC_INLINE void PT_ClearFlags(uint32_t mask)
<> 150:02e0a0aed4ec 254 {
<> 150:02e0a0aed4ec 255 MXC_PTG->intfl = mask;
<> 150:02e0a0aed4ec 256 }
<> 150:02e0a0aed4ec 257
<> 150:02e0a0aed4ec 258 /**
<> 150:02e0a0aed4ec 259 * @brief Setup and enables a pulse train to restart after another pulse train has exited its loop.
<> 150:02e0a0aed4ec 260 * Each pulse train can have up to two restart triggers.
<> 150:02e0a0aed4ec 261 *
<> 150:02e0a0aed4ec 262 * @param ptToRestart pulse train to restart after \a ptStop ends
<> 150:02e0a0aed4ec 263 * @param ptStop pulse train that stops and triggers \a ptToRestart to begin
<> 150:02e0a0aed4ec 264 * @param restartIndex selects which restart trigger to set (0 or 1)
<> 150:02e0a0aed4ec 265 */
<> 150:02e0a0aed4ec 266 __STATIC_INLINE void PT_SetRestart(mxc_pt_regs_t *ptToRestart, mxc_pt_regs_t *ptStop, uint8_t restartIndex)
<> 150:02e0a0aed4ec 267 {
<> 150:02e0a0aed4ec 268 int ptStopIndex = MXC_PT_GET_IDX(ptStop);
<> 150:02e0a0aed4ec 269
<> 150:02e0a0aed4ec 270 MXC_ASSERT(ptStopIndex >= 0);
<> 150:02e0a0aed4ec 271
<> 150:02e0a0aed4ec 272 if(restartIndex) {
<> 150:02e0a0aed4ec 273 ptToRestart->restart |= (ptStopIndex << MXC_F_PT_RESTART_PT_Y_SELECT_POS) |
<> 150:02e0a0aed4ec 274 MXC_F_PT_RESTART_ON_PT_Y_LOOP_EXIT;
<> 150:02e0a0aed4ec 275 } else {
<> 150:02e0a0aed4ec 276 ptToRestart->restart |= (ptStopIndex << MXC_F_PT_RESTART_PT_X_SELECT_POS) |
<> 150:02e0a0aed4ec 277 MXC_F_PT_RESTART_ON_PT_X_LOOP_EXIT;
<> 150:02e0a0aed4ec 278 }
<> 150:02e0a0aed4ec 279 }
<> 150:02e0a0aed4ec 280
<> 150:02e0a0aed4ec 281 /**
<> 150:02e0a0aed4ec 282 * @brief Disable the restart for the specified pulse train
<> 150:02e0a0aed4ec 283 *
<> 150:02e0a0aed4ec 284 * @param ptToRestart pulse train to disable the restart
<> 150:02e0a0aed4ec 285 * @param restartIndex selects which restart trigger to disable (0 or 1)
<> 150:02e0a0aed4ec 286 */
<> 150:02e0a0aed4ec 287 __STATIC_INLINE void PT_RestartDisable(mxc_pt_regs_t *ptToRestart, uint8_t restartIndex)
<> 150:02e0a0aed4ec 288 {
<> 150:02e0a0aed4ec 289 if(restartIndex)
<> 150:02e0a0aed4ec 290 ptToRestart->restart &= ~MXC_F_PT_RESTART_ON_PT_Y_LOOP_EXIT;
<> 150:02e0a0aed4ec 291 else
<> 150:02e0a0aed4ec 292 ptToRestart->restart &= ~MXC_F_PT_RESTART_ON_PT_X_LOOP_EXIT;
<> 150:02e0a0aed4ec 293 }
<> 150:02e0a0aed4ec 294
<> 150:02e0a0aed4ec 295 /**
<> 150:02e0a0aed4ec 296 * @brief Resynchronize individual pulse trains together.
<> 150:02e0a0aed4ec 297 * Resync will stop those resync_pts; others will be still running
<> 150:02e0a0aed4ec 298 *
<> 150:02e0a0aed4ec 299 * @param resyncPts pulse train modules that need to be re-synced by bit number.
<> 150:02e0a0aed4ec 300 * Bit0-\>pt0, Bit1-\>pt1... etc.
<> 150:02e0a0aed4ec 301 */
<> 150:02e0a0aed4ec 302 __STATIC_INLINE void PT_Resync(uint32_t resyncPts)
<> 150:02e0a0aed4ec 303 {
<> 150:02e0a0aed4ec 304 MXC_PTG->resync = resyncPts;
<> 150:02e0a0aed4ec 305 while(MXC_PTG->resync);
<> 150:02e0a0aed4ec 306 }
<> 150:02e0a0aed4ec 307
<> 150:02e0a0aed4ec 308 /**@}*/
<> 150:02e0a0aed4ec 309 #ifdef __cplusplus
<> 150:02e0a0aed4ec 310 }
<> 150:02e0a0aed4ec 311 #endif
<> 150:02e0a0aed4ec 312
<> 150:02e0a0aed4ec 313 #endif /* _PT_H_ */