The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Dependents:   hello SerialTestv11 SerialTestv12 Sierpinski ... more

mbed 2

This is the mbed 2 library. If you'd like to learn about Mbed OS please see the mbed-os docs.

Committer:
AnnaBridge
Date:
Wed Feb 20 20:53:29 2019 +0000
Revision:
172:65be27845400
Parent:
171:3a7713b1edbc
mbed library release version 165

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Anna Bridge 169:a7c7b631e539 1 /*******************************************************************************
Anna Bridge 169:a7c7b631e539 2 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
Anna Bridge 169:a7c7b631e539 3 *
Anna Bridge 169:a7c7b631e539 4 * Permission is hereby granted, free of charge, to any person obtaining a
Anna Bridge 169:a7c7b631e539 5 * copy of this software and associated documentation files (the "Software"),
Anna Bridge 169:a7c7b631e539 6 * to deal in the Software without restriction, including without limitation
Anna Bridge 169:a7c7b631e539 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
Anna Bridge 169:a7c7b631e539 8 * and/or sell copies of the Software, and to permit persons to whom the
Anna Bridge 169:a7c7b631e539 9 * Software is furnished to do so, subject to the following conditions:
Anna Bridge 169:a7c7b631e539 10 *
Anna Bridge 169:a7c7b631e539 11 * The above copyright notice and this permission notice shall be included
Anna Bridge 169:a7c7b631e539 12 * in all copies or substantial portions of the Software.
Anna Bridge 169:a7c7b631e539 13 *
Anna Bridge 169:a7c7b631e539 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
Anna Bridge 169:a7c7b631e539 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Anna Bridge 169:a7c7b631e539 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
Anna Bridge 169:a7c7b631e539 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
Anna Bridge 169:a7c7b631e539 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
Anna Bridge 169:a7c7b631e539 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
Anna Bridge 169:a7c7b631e539 20 * OTHER DEALINGS IN THE SOFTWARE.
Anna Bridge 169:a7c7b631e539 21 *
Anna Bridge 169:a7c7b631e539 22 * Except as contained in this notice, the name of Maxim Integrated
Anna Bridge 169:a7c7b631e539 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
Anna Bridge 169:a7c7b631e539 24 * Products, Inc. Branding Policy.
Anna Bridge 169:a7c7b631e539 25 *
Anna Bridge 169:a7c7b631e539 26 * The mere transfer of this software does not imply any licenses
Anna Bridge 169:a7c7b631e539 27 * of trade secrets, proprietary technology, copyrights, patents,
Anna Bridge 169:a7c7b631e539 28 * trademarks, maskwork rights, or any other form of intellectual
Anna Bridge 169:a7c7b631e539 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
Anna Bridge 169:a7c7b631e539 30 * ownership rights.
Anna Bridge 169:a7c7b631e539 31 *
Anna Bridge 169:a7c7b631e539 32 * $Date: 2016-04-27 11:55:43 -0500 (Wed, 27 Apr 2016) $
Anna Bridge 169:a7c7b631e539 33 * $Revision: 22541 $
Anna Bridge 169:a7c7b631e539 34 *
Anna Bridge 169:a7c7b631e539 35 ******************************************************************************/
Anna Bridge 169:a7c7b631e539 36
Anna Bridge 169:a7c7b631e539 37 /**
Anna Bridge 169:a7c7b631e539 38 * @file spim.h
Anna Bridge 169:a7c7b631e539 39 * @brief SPI Master driver header file.
Anna Bridge 169:a7c7b631e539 40 */
Anna Bridge 169:a7c7b631e539 41
Anna Bridge 169:a7c7b631e539 42 #include "mxc_config.h"
Anna Bridge 169:a7c7b631e539 43 #include "mxc_sys.h"
Anna Bridge 169:a7c7b631e539 44 #include "spim_regs.h"
Anna Bridge 169:a7c7b631e539 45
Anna Bridge 169:a7c7b631e539 46 #ifndef _SPIM_H_
Anna Bridge 169:a7c7b631e539 47 #define _SPIM_H_
Anna Bridge 169:a7c7b631e539 48
Anna Bridge 169:a7c7b631e539 49 #ifdef __cplusplus
Anna Bridge 169:a7c7b631e539 50 extern "C" {
Anna Bridge 169:a7c7b631e539 51 #endif
Anna Bridge 169:a7c7b631e539 52
Anna Bridge 169:a7c7b631e539 53 /***** Definitions *****/
Anna Bridge 169:a7c7b631e539 54
Anna Bridge 169:a7c7b631e539 55 /// @brief Active levels for slave select lines.
Anna Bridge 169:a7c7b631e539 56 typedef enum {
Anna Bridge 169:a7c7b631e539 57 SPIM_SSEL0_HIGH = (0x1 << 0),
Anna Bridge 169:a7c7b631e539 58 SPIM_SSEL0_LOW = 0,
Anna Bridge 169:a7c7b631e539 59 SPIM_SSEL1_HIGH = (0x1 << 1),
Anna Bridge 169:a7c7b631e539 60 SPIM_SSEL1_LOW = 0,
Anna Bridge 169:a7c7b631e539 61 SPIM_SSEL2_HIGH = (0x1 << 2),
Anna Bridge 169:a7c7b631e539 62 SPIM_SSEL2_LOW = 0,
Anna Bridge 169:a7c7b631e539 63 SPIM_SSEL3_HIGH = (0x1 << 3),
Anna Bridge 169:a7c7b631e539 64 SPIM_SSEL3_LOW = 0,
Anna Bridge 169:a7c7b631e539 65 SPIM_SSEL4_HIGH = (0x1 << 4),
Anna Bridge 169:a7c7b631e539 66 SPIM_SSEL4_LOW = 0
Anna Bridge 169:a7c7b631e539 67 }
Anna Bridge 169:a7c7b631e539 68 spim_ssel_t;
Anna Bridge 169:a7c7b631e539 69
Anna Bridge 169:a7c7b631e539 70 /// @brief Number of data lines to use.
Anna Bridge 169:a7c7b631e539 71 typedef enum {
Anna Bridge 169:a7c7b631e539 72 SPIM_WIDTH_1 = 0,
Anna Bridge 169:a7c7b631e539 73 SPIM_WIDTH_2 = 1,
Anna Bridge 169:a7c7b631e539 74 SPIM_WIDTH_4 = 2
Anna Bridge 169:a7c7b631e539 75 } spim_width_t;
Anna Bridge 169:a7c7b631e539 76
Anna Bridge 169:a7c7b631e539 77 /// @brief SPIM configuration type.
Anna Bridge 169:a7c7b631e539 78 typedef struct {
Anna Bridge 169:a7c7b631e539 79 uint8_t mode; ///< SPIM mode to use, 0-3.
Anna Bridge 169:a7c7b631e539 80 uint32_t ssel_pol; ///< Mask of active levels for slave select signals, use spim_ssel_t.
Anna Bridge 169:a7c7b631e539 81 uint32_t baud; ///< Baud rate in Hz.
Anna Bridge 169:a7c7b631e539 82 } spim_cfg_t;
Anna Bridge 169:a7c7b631e539 83
Anna Bridge 169:a7c7b631e539 84 /// @brief SPIM Transaction request.
Anna Bridge 169:a7c7b631e539 85 typedef struct spim_req spim_req_t;
Anna Bridge 169:a7c7b631e539 86 struct spim_req {
Anna Bridge 169:a7c7b631e539 87 uint8_t ssel; ///< Slave select number.
Anna Bridge 169:a7c7b631e539 88 uint8_t deass; ///< De-assert slave select at the end of the transaction.
Anna Bridge 169:a7c7b631e539 89 const uint8_t *tx_data; ///< TX buffer.
Anna Bridge 169:a7c7b631e539 90 uint8_t *rx_data; ///< RX buffer.
Anna Bridge 169:a7c7b631e539 91 spim_width_t width; ///< Number of data lines to use
Anna Bridge 169:a7c7b631e539 92 unsigned len; ///< Number of bytes to send.
Anna Bridge 169:a7c7b631e539 93 unsigned read_num; ///< Number of bytes read.
Anna Bridge 169:a7c7b631e539 94 unsigned write_num; ///< Number of bytes written.
Anna Bridge 169:a7c7b631e539 95
Anna Bridge 169:a7c7b631e539 96 /**
Anna Bridge 169:a7c7b631e539 97 * @brief Callback for asynchronous request.
Anna Bridge 169:a7c7b631e539 98 * @param spim_req_t* Pointer to the transaction request.
Anna Bridge 169:a7c7b631e539 99 * @param int Error code.
Anna Bridge 169:a7c7b631e539 100 */
Anna Bridge 169:a7c7b631e539 101 void (*callback)(spim_req_t*, int);
Anna Bridge 169:a7c7b631e539 102 };
Anna Bridge 169:a7c7b631e539 103
Anna Bridge 169:a7c7b631e539 104 /***** Globals *****/
Anna Bridge 169:a7c7b631e539 105
Anna Bridge 169:a7c7b631e539 106 /***** Function Prototypes *****/
Anna Bridge 169:a7c7b631e539 107
Anna Bridge 169:a7c7b631e539 108 /**
Anna Bridge 169:a7c7b631e539 109 * @brief Initialize SPIM module.
Anna Bridge 169:a7c7b631e539 110 * @param spim Pointer to SPIM regs.
Anna Bridge 169:a7c7b631e539 111 * @param cfg Pointer to SPIM configuration.
Anna Bridge 169:a7c7b631e539 112 * @param sys_cfg Pointer to system configuration object
Anna Bridge 169:a7c7b631e539 113 * @returns #E_NO_ERROR if everything is successful
Anna Bridge 169:a7c7b631e539 114 */
Anna Bridge 169:a7c7b631e539 115 int SPIM_Init(mxc_spim_regs_t *spim, const spim_cfg_t *cfg, const sys_cfg_spim_t *sys_cfg);
Anna Bridge 169:a7c7b631e539 116
Anna Bridge 169:a7c7b631e539 117 /**
Anna Bridge 169:a7c7b631e539 118 * @brief Shutdown SPIM module.
Anna Bridge 169:a7c7b631e539 119 * @param spim Pointer to SPIM regs.
Anna Bridge 169:a7c7b631e539 120 * @returns #E_NO_ERROR if everything is successful
Anna Bridge 169:a7c7b631e539 121 */
Anna Bridge 169:a7c7b631e539 122 int SPIM_Shutdown(mxc_spim_regs_t *spim);
Anna Bridge 169:a7c7b631e539 123
Anna Bridge 169:a7c7b631e539 124 /**
Anna Bridge 169:a7c7b631e539 125 * @brief Send Clock cycles on SCK without reading or writing.
Anna Bridge 169:a7c7b631e539 126 * @param spim Pointer to SPIM regs.
Anna Bridge 169:a7c7b631e539 127 * @param len Number of clock cycles to send.
Anna Bridge 169:a7c7b631e539 128 * @param ssel Slave select number.
Anna Bridge 169:a7c7b631e539 129 * @param deass De-assert slave select at the end of the transaction.
Anna Bridge 169:a7c7b631e539 130 * @returns Cycles transacted if everything is successful, error if unsuccessful.
Anna Bridge 169:a7c7b631e539 131 */
Anna Bridge 169:a7c7b631e539 132 int SPIM_Clocks(mxc_spim_regs_t *spim, uint32_t len, uint8_t ssel, uint8_t deass);
Anna Bridge 169:a7c7b631e539 133
Anna Bridge 169:a7c7b631e539 134 /**
Anna Bridge 169:a7c7b631e539 135 * @brief Read/write SPIM data. Will block until transaction is complete.
Anna Bridge 169:a7c7b631e539 136 * @param spim Pointer to SPIM regs.
Anna Bridge 169:a7c7b631e539 137 * @param req Request for a SPIM transaction.
Anna Bridge 169:a7c7b631e539 138 * @note Callback is ignored.
Anna Bridge 169:a7c7b631e539 139 * @returns Bytes transacted if everything is successful, error if unsuccessful.
Anna Bridge 169:a7c7b631e539 140 */
Anna Bridge 169:a7c7b631e539 141 int SPIM_Trans(mxc_spim_regs_t *spim, spim_req_t *req);
Anna Bridge 169:a7c7b631e539 142
Anna Bridge 169:a7c7b631e539 143 /**
Anna Bridge 169:a7c7b631e539 144 * @brief Asynchronously read/write SPIM data.
Anna Bridge 169:a7c7b631e539 145 * @param spim Pointer to SPIM regs.
Anna Bridge 169:a7c7b631e539 146 * @param req Request for a SPIM transaction.
Anna Bridge 169:a7c7b631e539 147 * @note Request struct must remain allocated until callback.
Anna Bridge 169:a7c7b631e539 148 * @returns #E_NO_ERROR if everything is successful, error if unsuccessful.
Anna Bridge 169:a7c7b631e539 149 */
Anna Bridge 169:a7c7b631e539 150 int SPIM_TransAsync(mxc_spim_regs_t *spim, spim_req_t *req);
Anna Bridge 169:a7c7b631e539 151
Anna Bridge 169:a7c7b631e539 152 /**
Anna Bridge 169:a7c7b631e539 153 * @brief Abort asynchronous request.
Anna Bridge 169:a7c7b631e539 154 * @param req Pointer to request for a SPIM transaction.
Anna Bridge 169:a7c7b631e539 155 * @returns #E_NO_ERROR if request aborted, error if unsuccessful.
Anna Bridge 169:a7c7b631e539 156 */
Anna Bridge 169:a7c7b631e539 157 int SPIM_AbortAsync(spim_req_t *req);
Anna Bridge 169:a7c7b631e539 158
Anna Bridge 169:a7c7b631e539 159 /**
Anna Bridge 169:a7c7b631e539 160 * @brief SPIM interrupt handler.
Anna Bridge 169:a7c7b631e539 161 * @details This function should be called by the application from the interrupt
Anna Bridge 169:a7c7b631e539 162 * handler if SPIM interrupts are enabled. Alternately, this function
Anna Bridge 169:a7c7b631e539 163 * can be periodically called by the application if SPIM interrupts are
Anna Bridge 169:a7c7b631e539 164 * disabled.
Anna Bridge 169:a7c7b631e539 165 * @param spim Base address of the SPIM module.
Anna Bridge 169:a7c7b631e539 166 */
Anna Bridge 169:a7c7b631e539 167 void SPIM_Handler(mxc_spim_regs_t *spim);
Anna Bridge 169:a7c7b631e539 168
Anna Bridge 169:a7c7b631e539 169 /**
Anna Bridge 169:a7c7b631e539 170 * @brief Check the SPIM to see if it's busy.
Anna Bridge 169:a7c7b631e539 171 * @param spim Pointer to SPIM regs.
Anna Bridge 169:a7c7b631e539 172 * @returns #E_NO_ERROR if idle, #E_BUSY if in use.
Anna Bridge 169:a7c7b631e539 173 */
Anna Bridge 169:a7c7b631e539 174 int SPIM_Busy(mxc_spim_regs_t *spim);
Anna Bridge 169:a7c7b631e539 175
Anna Bridge 169:a7c7b631e539 176 /**
Anna Bridge 169:a7c7b631e539 177 * @brief Attempt to prepare the SPIM for sleep.
Anna Bridge 169:a7c7b631e539 178 * @param spim Pointer to SPIM regs.
Anna Bridge 169:a7c7b631e539 179 * @details Checks for any ongoing transactions. Disables interrupts if the SPIM
Anna Bridge 169:a7c7b631e539 180 is idle.
Anna Bridge 169:a7c7b631e539 181 * @returns #E_NO_ERROR if ready to sleep, #E_BUSY if not ready for sleep.
Anna Bridge 169:a7c7b631e539 182 */
Anna Bridge 169:a7c7b631e539 183 int SPIM_PrepForSleep(mxc_spim_regs_t *spim);
Anna Bridge 169:a7c7b631e539 184
Anna Bridge 169:a7c7b631e539 185 /**
Anna Bridge 169:a7c7b631e539 186 * @brief Enables the SPIM without overwriting existing configuration.
Anna Bridge 169:a7c7b631e539 187 * @param spim Pointer to SPIM regs.
Anna Bridge 169:a7c7b631e539 188 */
Anna Bridge 169:a7c7b631e539 189 __STATIC_INLINE void SPIM_Enable(mxc_spim_regs_t *spim)
Anna Bridge 169:a7c7b631e539 190 {
Anna Bridge 169:a7c7b631e539 191 spim->gen_ctrl |= (MXC_F_SPIM_GEN_CTRL_SPI_MSTR_EN |
Anna Bridge 169:a7c7b631e539 192 MXC_F_SPIM_GEN_CTRL_TX_FIFO_EN | MXC_F_SPIM_GEN_CTRL_RX_FIFO_EN);
Anna Bridge 169:a7c7b631e539 193 }
Anna Bridge 169:a7c7b631e539 194
Anna Bridge 169:a7c7b631e539 195 /**
Anna Bridge 169:a7c7b631e539 196 * @brief Drain all of the data in the RXFIFO.
Anna Bridge 169:a7c7b631e539 197 * @param spim Pointer to UART regs.
Anna Bridge 169:a7c7b631e539 198 */
Anna Bridge 169:a7c7b631e539 199 __STATIC_INLINE void SPIM_DrainRX(mxc_spim_regs_t *spim)
Anna Bridge 169:a7c7b631e539 200 {
Anna Bridge 169:a7c7b631e539 201 uint32_t ctrl_save = spim->gen_ctrl;
Anna Bridge 169:a7c7b631e539 202 spim->gen_ctrl = (ctrl_save & ~MXC_F_SPIM_GEN_CTRL_RX_FIFO_EN);
Anna Bridge 169:a7c7b631e539 203 spim->gen_ctrl = ctrl_save;
Anna Bridge 169:a7c7b631e539 204 }
Anna Bridge 169:a7c7b631e539 205
Anna Bridge 169:a7c7b631e539 206 /**
Anna Bridge 169:a7c7b631e539 207 * @brief Drain all of the data in the TXFIFO.
Anna Bridge 169:a7c7b631e539 208 * @param spim Pointer to UART regs.
Anna Bridge 169:a7c7b631e539 209 */
Anna Bridge 169:a7c7b631e539 210 __STATIC_INLINE void SPIM_DrainTX(mxc_spim_regs_t *spim)
Anna Bridge 169:a7c7b631e539 211 {
Anna Bridge 169:a7c7b631e539 212 uint32_t ctrl_save = spim->gen_ctrl;
Anna Bridge 169:a7c7b631e539 213 spim->gen_ctrl = (ctrl_save & ~MXC_F_SPIM_GEN_CTRL_TX_FIFO_EN);
Anna Bridge 169:a7c7b631e539 214 spim->gen_ctrl = ctrl_save;
Anna Bridge 169:a7c7b631e539 215 }
Anna Bridge 169:a7c7b631e539 216
Anna Bridge 169:a7c7b631e539 217 /**
Anna Bridge 169:a7c7b631e539 218 * @brief TX FIFO availability.
Anna Bridge 169:a7c7b631e539 219 * @param spim Pointer to UART regs.
Anna Bridge 169:a7c7b631e539 220 * @returns Number of empty bytes available in write FIFO.
Anna Bridge 169:a7c7b631e539 221 */
Anna Bridge 169:a7c7b631e539 222 __STATIC_INLINE unsigned SPIM_NumWriteAvail(mxc_spim_regs_t *spim)
Anna Bridge 169:a7c7b631e539 223 {
Anna Bridge 169:a7c7b631e539 224 return (MXC_CFG_SPIM_FIFO_DEPTH - ((spim->fifo_ctrl &
Anna Bridge 169:a7c7b631e539 225 MXC_F_SPIM_FIFO_CTRL_TX_FIFO_USED) >> MXC_F_SPIM_FIFO_CTRL_TX_FIFO_USED_POS));
Anna Bridge 169:a7c7b631e539 226 }
Anna Bridge 169:a7c7b631e539 227
Anna Bridge 169:a7c7b631e539 228 /**
Anna Bridge 169:a7c7b631e539 229 * @brief RX FIFO availability.
Anna Bridge 169:a7c7b631e539 230 * @param spim Pointer to UART regs.
Anna Bridge 169:a7c7b631e539 231 * @returns Number of bytes in read FIFO.
Anna Bridge 169:a7c7b631e539 232 */
Anna Bridge 169:a7c7b631e539 233 __STATIC_INLINE unsigned SPIM_NumReadAvail(mxc_spim_regs_t *spim)
Anna Bridge 169:a7c7b631e539 234 {
Anna Bridge 169:a7c7b631e539 235 return ((spim->fifo_ctrl & MXC_F_SPIM_FIFO_CTRL_RX_FIFO_USED) >>
Anna Bridge 169:a7c7b631e539 236 MXC_F_SPIM_FIFO_CTRL_RX_FIFO_USED_POS);
Anna Bridge 169:a7c7b631e539 237 }
Anna Bridge 169:a7c7b631e539 238
Anna Bridge 169:a7c7b631e539 239 /**
Anna Bridge 169:a7c7b631e539 240 * @brief Clear interrupt flags.
Anna Bridge 169:a7c7b631e539 241 * @param spim Pointer to SPIM regs.
Anna Bridge 169:a7c7b631e539 242 * @param mask Mask of interrupts to clear.
Anna Bridge 169:a7c7b631e539 243 */
Anna Bridge 169:a7c7b631e539 244 __STATIC_INLINE void SPIM_ClearFlags(mxc_spim_regs_t *spim, uint32_t mask)
Anna Bridge 169:a7c7b631e539 245 {
Anna Bridge 169:a7c7b631e539 246 spim->intfl = mask;
Anna Bridge 169:a7c7b631e539 247 }
Anna Bridge 169:a7c7b631e539 248
Anna Bridge 169:a7c7b631e539 249 /**
Anna Bridge 169:a7c7b631e539 250 * @brief Get interrupt flags.
Anna Bridge 169:a7c7b631e539 251 * @param spim Pointer to SPIM regs.
Anna Bridge 169:a7c7b631e539 252 * @returns Mask of active flags.
Anna Bridge 169:a7c7b631e539 253 */
Anna Bridge 169:a7c7b631e539 254 __STATIC_INLINE unsigned SPIM_GetFlags(mxc_spim_regs_t *spim)
Anna Bridge 169:a7c7b631e539 255 {
Anna Bridge 169:a7c7b631e539 256 return (spim->intfl);
Anna Bridge 169:a7c7b631e539 257 }
Anna Bridge 169:a7c7b631e539 258
Anna Bridge 169:a7c7b631e539 259 #ifdef __cplusplus
Anna Bridge 169:a7c7b631e539 260 }
Anna Bridge 169:a7c7b631e539 261 #endif
Anna Bridge 169:a7c7b631e539 262
Anna Bridge 169:a7c7b631e539 263 #endif /* _SPIM_H_ */