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-05-18 16:27:43 -0500 (Wed, 18 May 2016) $
Anna Bridge 169:a7c7b631e539 33 * $Revision: 22908 $
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 spis.s
Anna Bridge 169:a7c7b631e539 39 * @brief SPI Slave 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 "spis_regs.h"
Anna Bridge 169:a7c7b631e539 45
Anna Bridge 169:a7c7b631e539 46 #ifndef _SPIS_H_
Anna Bridge 169:a7c7b631e539 47 #define _SPIS_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 Number of data lines to use.
Anna Bridge 169:a7c7b631e539 56 typedef enum {
Anna Bridge 169:a7c7b631e539 57 SPIS_WIDTH_1 = 0,
Anna Bridge 169:a7c7b631e539 58 SPIS_WIDTH_2 = 1,
Anna Bridge 169:a7c7b631e539 59 SPIS_WIDTH_4 = 2
Anna Bridge 169:a7c7b631e539 60 } spis_width_t;
Anna Bridge 169:a7c7b631e539 61
Anna Bridge 169:a7c7b631e539 62 /// @brief SPIS Transaction request.
Anna Bridge 169:a7c7b631e539 63 typedef struct spis_req spis_req_t;
Anna Bridge 169:a7c7b631e539 64 struct spis_req {
Anna Bridge 169:a7c7b631e539 65 uint8_t deass; ///< End the transaction when SS is deasserted.
Anna Bridge 169:a7c7b631e539 66 const uint8_t *tx_data; ///< TX buffer.
Anna Bridge 169:a7c7b631e539 67 uint8_t *rx_data; ///< RX buffer.
Anna Bridge 169:a7c7b631e539 68 spis_width_t width; ///< Number of data lines to use.
Anna Bridge 169:a7c7b631e539 69 unsigned len; ///< Number of bytes to send.
Anna Bridge 169:a7c7b631e539 70 unsigned read_num; ///< Number of bytes transacted.
Anna Bridge 169:a7c7b631e539 71 unsigned write_num; ///< Number of bytes transacted.
Anna Bridge 169:a7c7b631e539 72
Anna Bridge 169:a7c7b631e539 73 /**
Anna Bridge 169:a7c7b631e539 74 * @brief Callback for asynchronous request.
Anna Bridge 169:a7c7b631e539 75 * @param spis_req_t* Pointer to the transaction request.
Anna Bridge 169:a7c7b631e539 76 * @param int Error code.
Anna Bridge 169:a7c7b631e539 77 */
Anna Bridge 169:a7c7b631e539 78 void (*callback)(spis_req_t*, int);
Anna Bridge 169:a7c7b631e539 79 };
Anna Bridge 169:a7c7b631e539 80
Anna Bridge 169:a7c7b631e539 81 /***** Globals *****/
Anna Bridge 169:a7c7b631e539 82
Anna Bridge 169:a7c7b631e539 83 /***** Function Prototypes *****/
Anna Bridge 169:a7c7b631e539 84
Anna Bridge 169:a7c7b631e539 85 /**
Anna Bridge 169:a7c7b631e539 86 * @brief Initialize SPIS module.
Anna Bridge 169:a7c7b631e539 87 * @param spis Pointer to SPIS regs.
Anna Bridge 169:a7c7b631e539 88 * @param cfg Pointer to SPIS configuration.
Anna Bridge 169:a7c7b631e539 89 * @param mode SPI Mode to configure the slave.
Anna Bridge 169:a7c7b631e539 90 * @param sys_cfg Pointer to system configuration object.
Anna Bridge 169:a7c7b631e539 91 * @returns #E_NO_ERROR if everything is successful.
Anna Bridge 169:a7c7b631e539 92 */
Anna Bridge 169:a7c7b631e539 93 int SPIS_Init(mxc_spis_regs_t *spis, uint8_t mode, const sys_cfg_spis_t *sys_cfg);
Anna Bridge 169:a7c7b631e539 94
Anna Bridge 169:a7c7b631e539 95
Anna Bridge 169:a7c7b631e539 96 /**
Anna Bridge 169:a7c7b631e539 97 * @brief Shutdown SPIS module.
Anna Bridge 169:a7c7b631e539 98 * @param spis Pointer to SPIS regs.
Anna Bridge 169:a7c7b631e539 99 * @returns #E_NO_ERROR if everything is successful
Anna Bridge 169:a7c7b631e539 100 */
Anna Bridge 169:a7c7b631e539 101 int SPIS_Shutdown(mxc_spis_regs_t *spis);
Anna Bridge 169:a7c7b631e539 102
Anna Bridge 169:a7c7b631e539 103 /**
Anna Bridge 169:a7c7b631e539 104 * @brief Read/write SPIS data. Will block until transaction is complete.
Anna Bridge 169:a7c7b631e539 105 * @param spis Pointer to SPIS regs.
Anna Bridge 169:a7c7b631e539 106 * @param req Request for a SPIS transaction.
Anna Bridge 169:a7c7b631e539 107 * @note Callback is ignored.
Anna Bridge 169:a7c7b631e539 108 * @returns Bytes transacted if everything is successful, error if unsuccessful.
Anna Bridge 169:a7c7b631e539 109 */
Anna Bridge 169:a7c7b631e539 110 int SPIS_Trans(mxc_spis_regs_t *spis, spis_req_t *req);
Anna Bridge 169:a7c7b631e539 111
Anna Bridge 169:a7c7b631e539 112 /**
Anna Bridge 169:a7c7b631e539 113 * @brief Asynchronously read/write SPIS data.
Anna Bridge 169:a7c7b631e539 114 * @param spis Pointer to SPIS regs.
Anna Bridge 169:a7c7b631e539 115 * @param req Request for a SPIS transaction.
Anna Bridge 169:a7c7b631e539 116 * @note Request struct must remain allocated until callback.
Anna Bridge 169:a7c7b631e539 117 * @returns #E_NO_ERROR if everything is successful, error if unsuccessful.
Anna Bridge 169:a7c7b631e539 118 */
Anna Bridge 169:a7c7b631e539 119 int SPIS_TransAsync(mxc_spis_regs_t *spis, spis_req_t *req);
Anna Bridge 169:a7c7b631e539 120
Anna Bridge 169:a7c7b631e539 121 /**
Anna Bridge 169:a7c7b631e539 122 * @brief Abort asynchronous request.
Anna Bridge 169:a7c7b631e539 123 * @param req Pointer to request for a SPIS transaction.
Anna Bridge 169:a7c7b631e539 124 * @returns #E_NO_ERROR if request aborted, error if unsuccessful.
Anna Bridge 169:a7c7b631e539 125 */
Anna Bridge 169:a7c7b631e539 126 int SPIS_AbortAsync(spis_req_t *req);
Anna Bridge 169:a7c7b631e539 127
Anna Bridge 169:a7c7b631e539 128 /**
Anna Bridge 169:a7c7b631e539 129 * @brief SPIS interrupt handler.
Anna Bridge 169:a7c7b631e539 130 * @details This function should be called by the application from the interrupt
Anna Bridge 169:a7c7b631e539 131 * handler if SPIS interrupts are enabled. Alternately, this function
Anna Bridge 169:a7c7b631e539 132 * can be periodically called by the application if SPIS interrupts are
Anna Bridge 169:a7c7b631e539 133 * disabled.
Anna Bridge 169:a7c7b631e539 134 * @param spis Base address of the SPIS module.
Anna Bridge 169:a7c7b631e539 135 */
Anna Bridge 169:a7c7b631e539 136 void SPIS_Handler(mxc_spis_regs_t *spis);
Anna Bridge 169:a7c7b631e539 137
Anna Bridge 169:a7c7b631e539 138 /**
Anna Bridge 169:a7c7b631e539 139 * @brief Check the SPIS to see if it's busy.
Anna Bridge 169:a7c7b631e539 140 * @param spis Pointer to SPIS regs.
Anna Bridge 169:a7c7b631e539 141 * @returns #E_NO_ERROR if idle, #E_BUSY if in use.
Anna Bridge 169:a7c7b631e539 142 */
Anna Bridge 169:a7c7b631e539 143 int SPIS_Busy(mxc_spis_regs_t *spis);
Anna Bridge 169:a7c7b631e539 144
Anna Bridge 169:a7c7b631e539 145 /**
Anna Bridge 169:a7c7b631e539 146 * @brief Attempt to prepare the SPIS for sleep.
Anna Bridge 169:a7c7b631e539 147 * @param spis Pointer to SPIS regs.
Anna Bridge 169:a7c7b631e539 148 * @details Checks for any ongoing transactions. Disables interrupts if the SPIS
Anna Bridge 169:a7c7b631e539 149 is idle.
Anna Bridge 169:a7c7b631e539 150 * @returns #E_NO_ERROR if ready to sleep, #E_BUSY if not ready for sleep.
Anna Bridge 169:a7c7b631e539 151 */
Anna Bridge 169:a7c7b631e539 152 int SPIS_PrepForSleep(mxc_spis_regs_t *spis);
Anna Bridge 169:a7c7b631e539 153
Anna Bridge 169:a7c7b631e539 154 /**
Anna Bridge 169:a7c7b631e539 155 * @brief Enables the SPIS without overwriting existing configuration.
Anna Bridge 169:a7c7b631e539 156 * @param spis Pointer to SPIS regs.
Anna Bridge 169:a7c7b631e539 157 */
Anna Bridge 169:a7c7b631e539 158 __STATIC_INLINE void SPIS_Enable(mxc_spis_regs_t *spis)
Anna Bridge 169:a7c7b631e539 159 {
Anna Bridge 169:a7c7b631e539 160 spis->gen_ctrl |= (MXC_F_SPIS_GEN_CTRL_SPI_SLAVE_EN |
Anna Bridge 169:a7c7b631e539 161 MXC_F_SPIS_GEN_CTRL_TX_FIFO_EN | MXC_F_SPIS_GEN_CTRL_RX_FIFO_EN);
Anna Bridge 169:a7c7b631e539 162 }
Anna Bridge 169:a7c7b631e539 163
Anna Bridge 169:a7c7b631e539 164 /**
Anna Bridge 169:a7c7b631e539 165 * @brief Drain all of the data in the RXFIFO.
Anna Bridge 169:a7c7b631e539 166 * @param spis Pointer to UART regs.
Anna Bridge 169:a7c7b631e539 167 */
Anna Bridge 169:a7c7b631e539 168 __STATIC_INLINE void SPIS_DrainRX(mxc_spis_regs_t *spis)
Anna Bridge 169:a7c7b631e539 169 {
Anna Bridge 169:a7c7b631e539 170 uint32_t ctrl_save = spis->gen_ctrl;
Anna Bridge 169:a7c7b631e539 171 spis->gen_ctrl = (ctrl_save & ~MXC_F_SPIS_GEN_CTRL_RX_FIFO_EN);
Anna Bridge 169:a7c7b631e539 172 spis->gen_ctrl = ctrl_save;
Anna Bridge 169:a7c7b631e539 173 }
Anna Bridge 169:a7c7b631e539 174
Anna Bridge 169:a7c7b631e539 175 /**
Anna Bridge 169:a7c7b631e539 176 * @brief Drain all of the data in the TXFIFO.
Anna Bridge 169:a7c7b631e539 177 * @param spis Pointer to UART regs.
Anna Bridge 169:a7c7b631e539 178 */
Anna Bridge 169:a7c7b631e539 179 __STATIC_INLINE void SPIS_DrainTX(mxc_spis_regs_t *spis)
Anna Bridge 169:a7c7b631e539 180 {
Anna Bridge 169:a7c7b631e539 181 uint32_t ctrl_save = spis->gen_ctrl;
Anna Bridge 169:a7c7b631e539 182 spis->gen_ctrl = (ctrl_save & ~MXC_F_SPIS_GEN_CTRL_TX_FIFO_EN);
Anna Bridge 169:a7c7b631e539 183 spis->gen_ctrl = ctrl_save;
Anna Bridge 169:a7c7b631e539 184 }
Anna Bridge 169:a7c7b631e539 185
Anna Bridge 169:a7c7b631e539 186 /**
Anna Bridge 169:a7c7b631e539 187 * @brief TX FIFO availability.
Anna Bridge 169:a7c7b631e539 188 * @param spis Pointer to UART regs.
Anna Bridge 169:a7c7b631e539 189 * @returns Number of empty bytes available in write FIFO.
Anna Bridge 169:a7c7b631e539 190 */
Anna Bridge 169:a7c7b631e539 191 __STATIC_INLINE unsigned SPIS_NumWriteAvail(mxc_spis_regs_t *spis)
Anna Bridge 169:a7c7b631e539 192 {
Anna Bridge 169:a7c7b631e539 193 return (MXC_CFG_SPIS_FIFO_DEPTH - ((spis->fifo_stat &
Anna Bridge 169:a7c7b631e539 194 MXC_F_SPIS_FIFO_STAT_TX_FIFO_USED) >> MXC_F_SPIS_FIFO_STAT_TX_FIFO_USED_POS));
Anna Bridge 169:a7c7b631e539 195 }
Anna Bridge 169:a7c7b631e539 196
Anna Bridge 169:a7c7b631e539 197 /**
Anna Bridge 169:a7c7b631e539 198 * @brief RX FIFO availability.
Anna Bridge 169:a7c7b631e539 199 * @param spis Pointer to UART regs.
Anna Bridge 169:a7c7b631e539 200 * @returns Number of bytes in read FIFO.
Anna Bridge 169:a7c7b631e539 201 */
Anna Bridge 169:a7c7b631e539 202 __STATIC_INLINE unsigned SPIS_NumReadAvail(mxc_spis_regs_t *spis)
Anna Bridge 169:a7c7b631e539 203 {
Anna Bridge 169:a7c7b631e539 204 return ((spis->fifo_stat & MXC_F_SPIS_FIFO_STAT_RX_FIFO_USED) >>
Anna Bridge 169:a7c7b631e539 205 MXC_F_SPIS_FIFO_STAT_RX_FIFO_USED_POS);
Anna Bridge 169:a7c7b631e539 206 }
Anna Bridge 169:a7c7b631e539 207
Anna Bridge 169:a7c7b631e539 208 /**
Anna Bridge 169:a7c7b631e539 209 * @brief Clear interrupt flags.
Anna Bridge 169:a7c7b631e539 210 * @param spis Pointer to SPIS regs.
Anna Bridge 169:a7c7b631e539 211 * @param mask Mask of interrupts to clear.
Anna Bridge 169:a7c7b631e539 212 */
Anna Bridge 169:a7c7b631e539 213 __STATIC_INLINE void SPIS_ClearFlags(mxc_spis_regs_t *spis, uint32_t mask)
Anna Bridge 169:a7c7b631e539 214 {
Anna Bridge 169:a7c7b631e539 215 spis->intfl = mask;
Anna Bridge 169:a7c7b631e539 216 }
Anna Bridge 169:a7c7b631e539 217
Anna Bridge 169:a7c7b631e539 218 /**
Anna Bridge 169:a7c7b631e539 219 * @brief Get interrupt flags.
Anna Bridge 169:a7c7b631e539 220 * @param spis Pointer to SPIS regs.
Anna Bridge 169:a7c7b631e539 221 * @returns Mask of active flags.
Anna Bridge 169:a7c7b631e539 222 */
Anna Bridge 169:a7c7b631e539 223 __STATIC_INLINE unsigned SPIS_GetFlags(mxc_spis_regs_t *spis)
Anna Bridge 169:a7c7b631e539 224 {
Anna Bridge 169:a7c7b631e539 225 return (spis->intfl);
Anna Bridge 169:a7c7b631e539 226 }
Anna Bridge 169:a7c7b631e539 227
Anna Bridge 169:a7c7b631e539 228 #ifdef __cplusplus
Anna Bridge 169:a7c7b631e539 229 }
Anna Bridge 169:a7c7b631e539 230 #endif
Anna Bridge 169:a7c7b631e539 231
Anna Bridge 169:a7c7b631e539 232 #endif /* _SPIS_H_ */