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:
<>
Date:
Tue Nov 08 17:45:16 2016 +0000
Revision:
150:02e0a0aed4ec
This updates the lib to the mbed lib v129

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-04-27 11:55:43 -0500 (Wed, 27 Apr 2016) $
<> 150:02e0a0aed4ec 33 * $Revision: 22541 $
<> 150:02e0a0aed4ec 34 *
<> 150:02e0a0aed4ec 35 ******************************************************************************/
<> 150:02e0a0aed4ec 36
<> 150:02e0a0aed4ec 37 /**
<> 150:02e0a0aed4ec 38 * @file spim.h
<> 150:02e0a0aed4ec 39 * @brief SPI Master driver header file.
<> 150:02e0a0aed4ec 40 */
<> 150:02e0a0aed4ec 41
<> 150:02e0a0aed4ec 42 #include "mxc_config.h"
<> 150:02e0a0aed4ec 43 #include "mxc_sys.h"
<> 150:02e0a0aed4ec 44 #include "spim_regs.h"
<> 150:02e0a0aed4ec 45
<> 150:02e0a0aed4ec 46 #ifndef _SPIM_H_
<> 150:02e0a0aed4ec 47 #define _SPIM_H_
<> 150:02e0a0aed4ec 48
<> 150:02e0a0aed4ec 49 #ifdef __cplusplus
<> 150:02e0a0aed4ec 50 extern "C" {
<> 150:02e0a0aed4ec 51 #endif
<> 150:02e0a0aed4ec 52
<> 150:02e0a0aed4ec 53 /***** Definitions *****/
<> 150:02e0a0aed4ec 54
<> 150:02e0a0aed4ec 55 /// @brief Active levels for slave select lines.
<> 150:02e0a0aed4ec 56 typedef enum {
<> 150:02e0a0aed4ec 57 SPIM_SSEL0_HIGH = (0x1 << 0),
<> 150:02e0a0aed4ec 58 SPIM_SSEL0_LOW = 0,
<> 150:02e0a0aed4ec 59 SPIM_SSEL1_HIGH = (0x1 << 1),
<> 150:02e0a0aed4ec 60 SPIM_SSEL1_LOW = 0,
<> 150:02e0a0aed4ec 61 SPIM_SSEL2_HIGH = (0x1 << 2),
<> 150:02e0a0aed4ec 62 SPIM_SSEL2_LOW = 0,
<> 150:02e0a0aed4ec 63 SPIM_SSEL3_HIGH = (0x1 << 3),
<> 150:02e0a0aed4ec 64 SPIM_SSEL3_LOW = 0,
<> 150:02e0a0aed4ec 65 SPIM_SSEL4_HIGH = (0x1 << 4),
<> 150:02e0a0aed4ec 66 SPIM_SSEL4_LOW = 0
<> 150:02e0a0aed4ec 67 }
<> 150:02e0a0aed4ec 68 spim_ssel_t;
<> 150:02e0a0aed4ec 69
<> 150:02e0a0aed4ec 70 /// @brief Number of data lines to use.
<> 150:02e0a0aed4ec 71 typedef enum {
<> 150:02e0a0aed4ec 72 SPIM_WIDTH_1 = 0,
<> 150:02e0a0aed4ec 73 SPIM_WIDTH_2 = 1,
<> 150:02e0a0aed4ec 74 SPIM_WIDTH_4 = 2
<> 150:02e0a0aed4ec 75 } spim_width_t;
<> 150:02e0a0aed4ec 76
<> 150:02e0a0aed4ec 77 /// @brief SPIM configuration type.
<> 150:02e0a0aed4ec 78 typedef struct {
<> 150:02e0a0aed4ec 79 uint8_t mode; ///< SPIM mode to use, 0-3.
<> 150:02e0a0aed4ec 80 uint32_t ssel_pol; ///< Mask of active levels for slave select signals, use spim_ssel_t.
<> 150:02e0a0aed4ec 81 uint32_t baud; ///< Baud rate in Hz.
<> 150:02e0a0aed4ec 82 } spim_cfg_t;
<> 150:02e0a0aed4ec 83
<> 150:02e0a0aed4ec 84 /// @brief SPIM Transaction request.
<> 150:02e0a0aed4ec 85 typedef struct spim_req spim_req_t;
<> 150:02e0a0aed4ec 86 struct spim_req {
<> 150:02e0a0aed4ec 87 uint8_t ssel; ///< Slave select number.
<> 150:02e0a0aed4ec 88 uint8_t deass; ///< De-assert slave select at the end of the transaction.
<> 150:02e0a0aed4ec 89 const uint8_t *tx_data; ///< TX buffer.
<> 150:02e0a0aed4ec 90 uint8_t *rx_data; ///< RX buffer.
<> 150:02e0a0aed4ec 91 spim_width_t width; ///< Number of data lines to use
<> 150:02e0a0aed4ec 92 unsigned len; ///< Number of bytes to send.
<> 150:02e0a0aed4ec 93 unsigned read_num; ///< Number of bytes read.
<> 150:02e0a0aed4ec 94 unsigned write_num; ///< Number of bytes written.
<> 150:02e0a0aed4ec 95
<> 150:02e0a0aed4ec 96 /**
<> 150:02e0a0aed4ec 97 * @brief Callback for asynchronous request.
<> 150:02e0a0aed4ec 98 * @param spim_req_t* Pointer to the transaction request.
<> 150:02e0a0aed4ec 99 * @param int Error code.
<> 150:02e0a0aed4ec 100 */
<> 150:02e0a0aed4ec 101 void (*callback)(spim_req_t*, int);
<> 150:02e0a0aed4ec 102 };
<> 150:02e0a0aed4ec 103
<> 150:02e0a0aed4ec 104 /***** Globals *****/
<> 150:02e0a0aed4ec 105
<> 150:02e0a0aed4ec 106 /***** Function Prototypes *****/
<> 150:02e0a0aed4ec 107
<> 150:02e0a0aed4ec 108 /**
<> 150:02e0a0aed4ec 109 * @brief Initialize SPIM module.
<> 150:02e0a0aed4ec 110 * @param spim Pointer to SPIM regs.
<> 150:02e0a0aed4ec 111 * @param cfg Pointer to SPIM configuration.
<> 150:02e0a0aed4ec 112 * @param sys_cfg Pointer to system configuration object
<> 150:02e0a0aed4ec 113 * @returns #E_NO_ERROR if everything is successful
<> 150:02e0a0aed4ec 114 */
<> 150:02e0a0aed4ec 115 int SPIM_Init(mxc_spim_regs_t *spim, const spim_cfg_t *cfg, const sys_cfg_spim_t *sys_cfg);
<> 150:02e0a0aed4ec 116
<> 150:02e0a0aed4ec 117 /**
<> 150:02e0a0aed4ec 118 * @brief Shutdown SPIM module.
<> 150:02e0a0aed4ec 119 * @param spim Pointer to SPIM regs.
<> 150:02e0a0aed4ec 120 * @returns #E_NO_ERROR if everything is successful
<> 150:02e0a0aed4ec 121 */
<> 150:02e0a0aed4ec 122 int SPIM_Shutdown(mxc_spim_regs_t *spim);
<> 150:02e0a0aed4ec 123
<> 150:02e0a0aed4ec 124 /**
<> 150:02e0a0aed4ec 125 * @brief Send Clock cycles on SCK without reading or writing.
<> 150:02e0a0aed4ec 126 * @param spim Pointer to SPIM regs.
<> 150:02e0a0aed4ec 127 * @param len Number of clock cycles to send.
<> 150:02e0a0aed4ec 128 * @param ssel Slave select number.
<> 150:02e0a0aed4ec 129 * @param deass De-assert slave select at the end of the transaction.
<> 150:02e0a0aed4ec 130 * @returns Cycles transacted if everything is successful, error if unsuccessful.
<> 150:02e0a0aed4ec 131 */
<> 150:02e0a0aed4ec 132 int SPIM_Clocks(mxc_spim_regs_t *spim, uint32_t len, uint8_t ssel, uint8_t deass);
<> 150:02e0a0aed4ec 133
<> 150:02e0a0aed4ec 134 /**
<> 150:02e0a0aed4ec 135 * @brief Read/write SPIM data. Will block until transaction is complete.
<> 150:02e0a0aed4ec 136 * @param spim Pointer to SPIM regs.
<> 150:02e0a0aed4ec 137 * @param req Request for a SPIM transaction.
<> 150:02e0a0aed4ec 138 * @note Callback is ignored.
<> 150:02e0a0aed4ec 139 * @returns Bytes transacted if everything is successful, error if unsuccessful.
<> 150:02e0a0aed4ec 140 */
<> 150:02e0a0aed4ec 141 int SPIM_Trans(mxc_spim_regs_t *spim, spim_req_t *req);
<> 150:02e0a0aed4ec 142
<> 150:02e0a0aed4ec 143 /**
<> 150:02e0a0aed4ec 144 * @brief Asynchronously read/write SPIM data.
<> 150:02e0a0aed4ec 145 * @param spim Pointer to SPIM regs.
<> 150:02e0a0aed4ec 146 * @param req Request for a SPIM transaction.
<> 150:02e0a0aed4ec 147 * @note Request struct must remain allocated until callback.
<> 150:02e0a0aed4ec 148 * @returns #E_NO_ERROR if everything is successful, error if unsuccessful.
<> 150:02e0a0aed4ec 149 */
<> 150:02e0a0aed4ec 150 int SPIM_TransAsync(mxc_spim_regs_t *spim, spim_req_t *req);
<> 150:02e0a0aed4ec 151
<> 150:02e0a0aed4ec 152 /**
<> 150:02e0a0aed4ec 153 * @brief Abort asynchronous request.
<> 150:02e0a0aed4ec 154 * @param req Pointer to request for a SPIM transaction.
<> 150:02e0a0aed4ec 155 * @returns #E_NO_ERROR if request aborted, error if unsuccessful.
<> 150:02e0a0aed4ec 156 */
<> 150:02e0a0aed4ec 157 int SPIM_AbortAsync(spim_req_t *req);
<> 150:02e0a0aed4ec 158
<> 150:02e0a0aed4ec 159 /**
<> 150:02e0a0aed4ec 160 * @brief SPIM interrupt handler.
<> 150:02e0a0aed4ec 161 * @details This function should be called by the application from the interrupt
<> 150:02e0a0aed4ec 162 * handler if SPIM interrupts are enabled. Alternately, this function
<> 150:02e0a0aed4ec 163 * can be periodically called by the application if SPIM interrupts are
<> 150:02e0a0aed4ec 164 * disabled.
<> 150:02e0a0aed4ec 165 * @param spim Base address of the SPIM module.
<> 150:02e0a0aed4ec 166 */
<> 150:02e0a0aed4ec 167 void SPIM_Handler(mxc_spim_regs_t *spim);
<> 150:02e0a0aed4ec 168
<> 150:02e0a0aed4ec 169 /**
<> 150:02e0a0aed4ec 170 * @brief Check the SPIM to see if it's busy.
<> 150:02e0a0aed4ec 171 * @param spim Pointer to SPIM regs.
<> 150:02e0a0aed4ec 172 * @returns #E_NO_ERROR if idle, #E_BUSY if in use.
<> 150:02e0a0aed4ec 173 */
<> 150:02e0a0aed4ec 174 int SPIM_Busy(mxc_spim_regs_t *spim);
<> 150:02e0a0aed4ec 175
<> 150:02e0a0aed4ec 176 /**
<> 150:02e0a0aed4ec 177 * @brief Attempt to prepare the SPIM for sleep.
<> 150:02e0a0aed4ec 178 * @param spim Pointer to SPIM regs.
<> 150:02e0a0aed4ec 179 * @details Checks for any ongoing transactions. Disables interrupts if the SPIM
<> 150:02e0a0aed4ec 180 is idle.
<> 150:02e0a0aed4ec 181 * @returns #E_NO_ERROR if ready to sleep, #E_BUSY if not ready for sleep.
<> 150:02e0a0aed4ec 182 */
<> 150:02e0a0aed4ec 183 int SPIM_PrepForSleep(mxc_spim_regs_t *spim);
<> 150:02e0a0aed4ec 184
<> 150:02e0a0aed4ec 185 /**
<> 150:02e0a0aed4ec 186 * @brief Enables the SPIM without overwriting existing configuration.
<> 150:02e0a0aed4ec 187 * @param spim Pointer to SPIM regs.
<> 150:02e0a0aed4ec 188 */
<> 150:02e0a0aed4ec 189 __STATIC_INLINE void SPIM_Enable(mxc_spim_regs_t *spim)
<> 150:02e0a0aed4ec 190 {
<> 150:02e0a0aed4ec 191 spim->gen_ctrl |= (MXC_F_SPIM_GEN_CTRL_SPI_MSTR_EN |
<> 150:02e0a0aed4ec 192 MXC_F_SPIM_GEN_CTRL_TX_FIFO_EN | MXC_F_SPIM_GEN_CTRL_RX_FIFO_EN);
<> 150:02e0a0aed4ec 193 }
<> 150:02e0a0aed4ec 194
<> 150:02e0a0aed4ec 195 /**
<> 150:02e0a0aed4ec 196 * @brief Drain all of the data in the RXFIFO.
<> 150:02e0a0aed4ec 197 * @param spim Pointer to UART regs.
<> 150:02e0a0aed4ec 198 */
<> 150:02e0a0aed4ec 199 __STATIC_INLINE void SPIM_DrainRX(mxc_spim_regs_t *spim)
<> 150:02e0a0aed4ec 200 {
<> 150:02e0a0aed4ec 201 uint32_t ctrl_save = spim->gen_ctrl;
<> 150:02e0a0aed4ec 202 spim->gen_ctrl = (ctrl_save & ~MXC_F_SPIM_GEN_CTRL_RX_FIFO_EN);
<> 150:02e0a0aed4ec 203 spim->gen_ctrl = ctrl_save;
<> 150:02e0a0aed4ec 204 }
<> 150:02e0a0aed4ec 205
<> 150:02e0a0aed4ec 206 /**
<> 150:02e0a0aed4ec 207 * @brief Drain all of the data in the TXFIFO.
<> 150:02e0a0aed4ec 208 * @param spim Pointer to UART regs.
<> 150:02e0a0aed4ec 209 */
<> 150:02e0a0aed4ec 210 __STATIC_INLINE void SPIM_DrainTX(mxc_spim_regs_t *spim)
<> 150:02e0a0aed4ec 211 {
<> 150:02e0a0aed4ec 212 uint32_t ctrl_save = spim->gen_ctrl;
<> 150:02e0a0aed4ec 213 spim->gen_ctrl = (ctrl_save & ~MXC_F_SPIM_GEN_CTRL_TX_FIFO_EN);
<> 150:02e0a0aed4ec 214 spim->gen_ctrl = ctrl_save;
<> 150:02e0a0aed4ec 215 }
<> 150:02e0a0aed4ec 216
<> 150:02e0a0aed4ec 217 /**
<> 150:02e0a0aed4ec 218 * @brief TX FIFO availability.
<> 150:02e0a0aed4ec 219 * @param spim Pointer to UART regs.
<> 150:02e0a0aed4ec 220 * @returns Number of empty bytes available in write FIFO.
<> 150:02e0a0aed4ec 221 */
<> 150:02e0a0aed4ec 222 __STATIC_INLINE unsigned SPIM_NumWriteAvail(mxc_spim_regs_t *spim)
<> 150:02e0a0aed4ec 223 {
<> 150:02e0a0aed4ec 224 return (MXC_CFG_SPIM_FIFO_DEPTH - ((spim->fifo_ctrl &
<> 150:02e0a0aed4ec 225 MXC_F_SPIM_FIFO_CTRL_TX_FIFO_USED) >> MXC_F_SPIM_FIFO_CTRL_TX_FIFO_USED_POS));
<> 150:02e0a0aed4ec 226 }
<> 150:02e0a0aed4ec 227
<> 150:02e0a0aed4ec 228 /**
<> 150:02e0a0aed4ec 229 * @brief RX FIFO availability.
<> 150:02e0a0aed4ec 230 * @param spim Pointer to UART regs.
<> 150:02e0a0aed4ec 231 * @returns Number of bytes in read FIFO.
<> 150:02e0a0aed4ec 232 */
<> 150:02e0a0aed4ec 233 __STATIC_INLINE unsigned SPIM_NumReadAvail(mxc_spim_regs_t *spim)
<> 150:02e0a0aed4ec 234 {
<> 150:02e0a0aed4ec 235 return ((spim->fifo_ctrl & MXC_F_SPIM_FIFO_CTRL_RX_FIFO_USED) >>
<> 150:02e0a0aed4ec 236 MXC_F_SPIM_FIFO_CTRL_RX_FIFO_USED_POS);
<> 150:02e0a0aed4ec 237 }
<> 150:02e0a0aed4ec 238
<> 150:02e0a0aed4ec 239 /**
<> 150:02e0a0aed4ec 240 * @brief Clear interrupt flags.
<> 150:02e0a0aed4ec 241 * @param spim Pointer to SPIM regs.
<> 150:02e0a0aed4ec 242 * @param mask Mask of interrupts to clear.
<> 150:02e0a0aed4ec 243 */
<> 150:02e0a0aed4ec 244 __STATIC_INLINE void SPIM_ClearFlags(mxc_spim_regs_t *spim, uint32_t mask)
<> 150:02e0a0aed4ec 245 {
<> 150:02e0a0aed4ec 246 spim->intfl = mask;
<> 150:02e0a0aed4ec 247 }
<> 150:02e0a0aed4ec 248
<> 150:02e0a0aed4ec 249 /**
<> 150:02e0a0aed4ec 250 * @brief Get interrupt flags.
<> 150:02e0a0aed4ec 251 * @param spim Pointer to SPIM regs.
<> 150:02e0a0aed4ec 252 * @returns Mask of active flags.
<> 150:02e0a0aed4ec 253 */
<> 150:02e0a0aed4ec 254 __STATIC_INLINE unsigned SPIM_GetFlags(mxc_spim_regs_t *spim)
<> 150:02e0a0aed4ec 255 {
<> 150:02e0a0aed4ec 256 return (spim->intfl);
<> 150:02e0a0aed4ec 257 }
<> 150:02e0a0aed4ec 258
<> 150:02e0a0aed4ec 259 #ifdef __cplusplus
<> 150:02e0a0aed4ec 260 }
<> 150:02e0a0aed4ec 261 #endif
<> 150:02e0a0aed4ec 262
<> 150:02e0a0aed4ec 263 #endif /* _SPIM_H_ */