BA / SerialCom

Fork of OmniWheels by Gustav Atmel

Committer:
gustavatmel
Date:
Tue May 01 15:55:34 2018 +0000
Revision:
2:798925c9e4a8
Parent:
1:9c5af431a1f1
bluetooth

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gustavatmel 1:9c5af431a1f1 1 /**
gustavatmel 1:9c5af431a1f1 2 * @file
gustavatmel 1:9c5af431a1f1 3 * @brief Registers, Bit Masks and Bit Positions for the SPI Master module.
gustavatmel 1:9c5af431a1f1 4 */
gustavatmel 1:9c5af431a1f1 5 /* ****************************************************************************
gustavatmel 1:9c5af431a1f1 6 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
gustavatmel 1:9c5af431a1f1 7 *
gustavatmel 1:9c5af431a1f1 8 * Permission is hereby granted, free of charge, to any person obtaining a
gustavatmel 1:9c5af431a1f1 9 * copy of this software and associated documentation files (the "Software"),
gustavatmel 1:9c5af431a1f1 10 * to deal in the Software without restriction, including without limitation
gustavatmel 1:9c5af431a1f1 11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
gustavatmel 1:9c5af431a1f1 12 * and/or sell copies of the Software, and to permit persons to whom the
gustavatmel 1:9c5af431a1f1 13 * Software is furnished to do so, subject to the following conditions:
gustavatmel 1:9c5af431a1f1 14 *
gustavatmel 1:9c5af431a1f1 15 * The above copyright notice and this permission notice shall be included
gustavatmel 1:9c5af431a1f1 16 * in all copies or substantial portions of the Software.
gustavatmel 1:9c5af431a1f1 17 *
gustavatmel 1:9c5af431a1f1 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
gustavatmel 1:9c5af431a1f1 19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
gustavatmel 1:9c5af431a1f1 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
gustavatmel 1:9c5af431a1f1 21 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
gustavatmel 1:9c5af431a1f1 22 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
gustavatmel 1:9c5af431a1f1 23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
gustavatmel 1:9c5af431a1f1 24 * OTHER DEALINGS IN THE SOFTWARE.
gustavatmel 1:9c5af431a1f1 25 *
gustavatmel 1:9c5af431a1f1 26 * Except as contained in this notice, the name of Maxim Integrated
gustavatmel 1:9c5af431a1f1 27 * Products, Inc. shall not be used except as stated in the Maxim Integrated
gustavatmel 1:9c5af431a1f1 28 * Products, Inc. Branding Policy.
gustavatmel 1:9c5af431a1f1 29 *
gustavatmel 1:9c5af431a1f1 30 * The mere transfer of this software does not imply any licenses
gustavatmel 1:9c5af431a1f1 31 * of trade secrets, proprietary technology, copyrights, patents,
gustavatmel 1:9c5af431a1f1 32 * trademarks, maskwork rights, or any other form of intellectual
gustavatmel 1:9c5af431a1f1 33 * property whatsoever. Maxim Integrated Products, Inc. retains all
gustavatmel 1:9c5af431a1f1 34 * ownership rights.
gustavatmel 1:9c5af431a1f1 35 *
gustavatmel 1:9c5af431a1f1 36 * $Date: 2016-10-10 19:42:44 -0500 (Mon, 10 Oct 2016) $
gustavatmel 1:9c5af431a1f1 37 * $Revision: 24672 $
gustavatmel 1:9c5af431a1f1 38 *
gustavatmel 1:9c5af431a1f1 39 **************************************************************************** */
gustavatmel 1:9c5af431a1f1 40
gustavatmel 1:9c5af431a1f1 41 /* **** Includes **** */
gustavatmel 1:9c5af431a1f1 42 #include "mxc_config.h"
gustavatmel 1:9c5af431a1f1 43 #include "mxc_sys.h"
gustavatmel 1:9c5af431a1f1 44 #include "spim_regs.h"
gustavatmel 1:9c5af431a1f1 45
gustavatmel 1:9c5af431a1f1 46 /* Define to prevent redundant inclusion */
gustavatmel 1:9c5af431a1f1 47 #ifndef _SPIM_H_
gustavatmel 1:9c5af431a1f1 48 #define _SPIM_H_
gustavatmel 1:9c5af431a1f1 49
gustavatmel 1:9c5af431a1f1 50 #ifdef __cplusplus
gustavatmel 1:9c5af431a1f1 51 extern "C" {
gustavatmel 1:9c5af431a1f1 52 #endif
gustavatmel 1:9c5af431a1f1 53 /**
gustavatmel 1:9c5af431a1f1 54 * @ingroup commperipherals
gustavatmel 1:9c5af431a1f1 55 * @defgroup spi_comm SPI
gustavatmel 1:9c5af431a1f1 56 * @brief SPI Master and Slave Communication Peripherals
gustavatmel 1:9c5af431a1f1 57 */
gustavatmel 1:9c5af431a1f1 58
gustavatmel 1:9c5af431a1f1 59 /**
gustavatmel 1:9c5af431a1f1 60 * @ingroup spi_comm
gustavatmel 1:9c5af431a1f1 61 * @defgroup spim SPI Master
gustavatmel 1:9c5af431a1f1 62 * @brief Serial Peripheral Interface Master (SPIM) Communications
gustavatmel 1:9c5af431a1f1 63 * Interface.
gustavatmel 1:9c5af431a1f1 64 * @{
gustavatmel 1:9c5af431a1f1 65 */
gustavatmel 1:9c5af431a1f1 66
gustavatmel 1:9c5af431a1f1 67 /* **** Definitions **** */
gustavatmel 1:9c5af431a1f1 68
gustavatmel 1:9c5af431a1f1 69 /**
gustavatmel 1:9c5af431a1f1 70 * Enumeration type for selecting the active levels for the SPI Master Slave Select (SS) lines.
gustavatmel 1:9c5af431a1f1 71 */
gustavatmel 1:9c5af431a1f1 72 typedef enum {
gustavatmel 1:9c5af431a1f1 73 SPIM_SSEL0_HIGH = (0x1 << 0), /**< Slave Select 0 High. */
gustavatmel 1:9c5af431a1f1 74 SPIM_SSEL0_LOW = 0, /**< Slave Select 0 Low. */
gustavatmel 1:9c5af431a1f1 75 SPIM_SSEL1_HIGH = (0x1 << 1), /**< Slave Select 1 High. */
gustavatmel 1:9c5af431a1f1 76 SPIM_SSEL1_LOW = 0, /**< Slave Select 1 Low. */
gustavatmel 1:9c5af431a1f1 77 SPIM_SSEL2_HIGH = (0x1 << 2), /**< Slave Select 2 High. */
gustavatmel 1:9c5af431a1f1 78 SPIM_SSEL2_LOW = 0, /**< Slave Select 2 Low. */
gustavatmel 1:9c5af431a1f1 79 SPIM_SSEL3_HIGH = (0x1 << 3), /**< Slave Select 3 High. */
gustavatmel 1:9c5af431a1f1 80 SPIM_SSEL3_LOW = 0, /**< Slave Select 3 Low. */
gustavatmel 1:9c5af431a1f1 81 SPIM_SSEL4_HIGH = (0x1 << 4), /**< Slave Select 4 High. */
gustavatmel 1:9c5af431a1f1 82 SPIM_SSEL4_LOW = 0 /**< Slave Select 4 Low. */
gustavatmel 1:9c5af431a1f1 83 }
gustavatmel 1:9c5af431a1f1 84 spim_ssel_t;
gustavatmel 1:9c5af431a1f1 85
gustavatmel 1:9c5af431a1f1 86 /**
gustavatmel 1:9c5af431a1f1 87 * Enumeration type for setting the number data lines to use for communication.
gustavatmel 1:9c5af431a1f1 88 */
gustavatmel 1:9c5af431a1f1 89 typedef enum {
gustavatmel 1:9c5af431a1f1 90 SPIM_WIDTH_1 = 0, /**< 1 Data Line. */
gustavatmel 1:9c5af431a1f1 91 SPIM_WIDTH_2 = 1, /**< 2 Data Lines (x2). */
gustavatmel 1:9c5af431a1f1 92 SPIM_WIDTH_4 = 2 /**< 4 Data Lines (x4). */
gustavatmel 1:9c5af431a1f1 93 } spim_width_t;
gustavatmel 1:9c5af431a1f1 94
gustavatmel 1:9c5af431a1f1 95 /**
gustavatmel 1:9c5af431a1f1 96 * Structure type for configuring a SPIM port.
gustavatmel 1:9c5af431a1f1 97 */
gustavatmel 1:9c5af431a1f1 98 typedef struct {
gustavatmel 1:9c5af431a1f1 99 uint8_t mode; /**< SPIM mode selection, 0 to 3. */
gustavatmel 1:9c5af431a1f1 100 uint32_t ssel_pol; /**< Mask of active levels for the slave select signals, see #spim_ssel_t. */
gustavatmel 1:9c5af431a1f1 101 uint32_t baud; /**< Baud rate in Hz. */
gustavatmel 1:9c5af431a1f1 102 } spim_cfg_t;
gustavatmel 1:9c5af431a1f1 103
gustavatmel 1:9c5af431a1f1 104 /**
gustavatmel 1:9c5af431a1f1 105 * Structure type representing a SPI Master Transaction request.
gustavatmel 1:9c5af431a1f1 106 */
gustavatmel 1:9c5af431a1f1 107 typedef struct spim_req spim_req_t;
gustavatmel 1:9c5af431a1f1 108
gustavatmel 1:9c5af431a1f1 109 /**
gustavatmel 1:9c5af431a1f1 110 * @brief Callback function type used in asynchromous SPIM communications requests.
gustavatmel 1:9c5af431a1f1 111 * @details The function declaration for the SPIM callback is:
gustavatmel 1:9c5af431a1f1 112 * @code
gustavatmel 1:9c5af431a1f1 113 * void callback(spim_req_t * req, int error_code);
gustavatmel 1:9c5af431a1f1 114 * @endcode
gustavatmel 1:9c5af431a1f1 115 * | | |
gustavatmel 1:9c5af431a1f1 116 * | -----: | :----------------------------------------- |
gustavatmel 1:9c5af431a1f1 117 * | \p req | Pointer to a #spim_req object representing the active SPIM active transaction. |
gustavatmel 1:9c5af431a1f1 118 * | \p error_code | An error code if the active transaction had a failure or #E_NO_ERROR if successful. |
gustavatmel 1:9c5af431a1f1 119 * @addtogroup spim_async
gustavatmel 1:9c5af431a1f1 120 */
gustavatmel 1:9c5af431a1f1 121 typedef void (*spim_callback_fn)(spim_req_t * req, int error_code);
gustavatmel 1:9c5af431a1f1 122
gustavatmel 1:9c5af431a1f1 123 /**
gustavatmel 1:9c5af431a1f1 124 * @brief Structure definition for an SPI Master Transaction request.
gustavatmel 1:9c5af431a1f1 125 * @note When using this structure for an asynchronous operation, the
gustavatmel 1:9c5af431a1f1 126 * structure must remain allocated until the callback is completed.
gustavatmel 1:9c5af431a1f1 127 * @addtogroup spim_async
gustavatmel 1:9c5af431a1f1 128 */
gustavatmel 1:9c5af431a1f1 129 struct spim_req {
gustavatmel 1:9c5af431a1f1 130 uint8_t ssel; /**< Number of the Slave Select to use. */
gustavatmel 1:9c5af431a1f1 131 uint8_t deass; /**< Set to de-assert slave select at the completions of the transaction.*/
gustavatmel 1:9c5af431a1f1 132 const uint8_t *tx_data; /**< Pointer to a buffer to transmit data from. */
gustavatmel 1:9c5af431a1f1 133 uint8_t *rx_data; /**< Pointer to a buffer to store data received. */
gustavatmel 1:9c5af431a1f1 134 spim_width_t width; /**< Number of data lines to use, see #spim_width_t. */
gustavatmel 1:9c5af431a1f1 135 unsigned len; /**< Number of bytes to send from the \p tx_data buffer. */
gustavatmel 1:9c5af431a1f1 136 unsigned read_num; /**< Number of bytes read and stored in \p rx_data buffer. */
gustavatmel 1:9c5af431a1f1 137 unsigned write_num; /**< Number of bytes sent from the \p tx_data buffer, this will be filled by the driver after up to \p len bytes have been transmitted. */
gustavatmel 1:9c5af431a1f1 138 spim_callback_fn callback; /**< Function pointer to a callback function if desired, NULL otherwise */
gustavatmel 1:9c5af431a1f1 139 };
gustavatmel 1:9c5af431a1f1 140
gustavatmel 1:9c5af431a1f1 141 /* **** Globals **** */
gustavatmel 1:9c5af431a1f1 142
gustavatmel 1:9c5af431a1f1 143 /* **** Function Prototypes **** */
gustavatmel 1:9c5af431a1f1 144
gustavatmel 1:9c5af431a1f1 145 /**
gustavatmel 1:9c5af431a1f1 146 * @brief Initialize the SPIM peripheral module.
gustavatmel 1:9c5af431a1f1 147 *
gustavatmel 1:9c5af431a1f1 148 * @param spim Pointer to the SPIM register structure.
gustavatmel 1:9c5af431a1f1 149 * @param cfg Pointer to an SPIM configuration object.
gustavatmel 1:9c5af431a1f1 150 * @param sys_cfg Pointer to a system configuration object to select the
gustavatmel 1:9c5af431a1f1 151 * peripheral clock rate and assign the requested GPIO.
gustavatmel 1:9c5af431a1f1 152 *
gustavatmel 1:9c5af431a1f1 153 * @return #E_NO_ERROR if the SPIM port is initialized successfully, @ref MXC_Error_Codes
gustavatmel 1:9c5af431a1f1 154 * "error" if unsuccessful.
gustavatmel 1:9c5af431a1f1 155 */
gustavatmel 1:9c5af431a1f1 156 int SPIM_Init(mxc_spim_regs_t *spim, const spim_cfg_t *cfg, const sys_cfg_spim_t *sys_cfg);
gustavatmel 1:9c5af431a1f1 157
gustavatmel 1:9c5af431a1f1 158 /**
gustavatmel 1:9c5af431a1f1 159 * @brief Shutdown the SPIM peripheral module instance represented by the
gustavatmel 1:9c5af431a1f1 160 * @p spim parameter.
gustavatmel 1:9c5af431a1f1 161 *
gustavatmel 1:9c5af431a1f1 162 * @param spim Pointer to the SPIM register structure.
gustavatmel 1:9c5af431a1f1 163 *
gustavatmel 1:9c5af431a1f1 164 * @return #E_NO_ERROR if the SPIM is shutdown successfully, @ref
gustavatmel 1:9c5af431a1f1 165 * MXC_Error_Codes "error" if unsuccessful.
gustavatmel 1:9c5af431a1f1 166 */
gustavatmel 1:9c5af431a1f1 167 int SPIM_Shutdown(mxc_spim_regs_t *spim);
gustavatmel 1:9c5af431a1f1 168
gustavatmel 1:9c5af431a1f1 169 /**
gustavatmel 1:9c5af431a1f1 170 * @brief Send Clock cycles on SCK without reading or writing.
gustavatmel 1:9c5af431a1f1 171 *
gustavatmel 1:9c5af431a1f1 172 * @param spim Pointer to the SPIM register structure.
gustavatmel 1:9c5af431a1f1 173 * @param len Number of clock cycles to send.
gustavatmel 1:9c5af431a1f1 174 * @param ssel Slave select number.
gustavatmel 1:9c5af431a1f1 175 * @param deass De-assert slave select at the end of the transaction.
gustavatmel 1:9c5af431a1f1 176 *
gustavatmel 1:9c5af431a1f1 177 * @return Cycles transacted if everything is successful, @ref
gustavatmel 1:9c5af431a1f1 178 * MXC_Error_Codes "error" if unsuccessful.
gustavatmel 1:9c5af431a1f1 179 */
gustavatmel 1:9c5af431a1f1 180 int SPIM_Clocks(mxc_spim_regs_t *spim, uint32_t len, uint8_t ssel, uint8_t deass);
gustavatmel 1:9c5af431a1f1 181
gustavatmel 1:9c5af431a1f1 182 /**
gustavatmel 1:9c5af431a1f1 183 * @brief Read/write SPIM data. This function will block until the
gustavatmel 1:9c5af431a1f1 184 * transaction is complete.
gustavatmel 1:9c5af431a1f1 185 *
gustavatmel 1:9c5af431a1f1 186 * @param spim Pointer to the SPIM register structure.
gustavatmel 1:9c5af431a1f1 187 * @param req Request for a SPIM transaction.
gustavatmel 1:9c5af431a1f1 188 * @note If a callback function is registered it will not be called when using a blocking function.
gustavatmel 1:9c5af431a1f1 189 *
gustavatmel 1:9c5af431a1f1 190 * @return Bytes transacted if everything is successful, error if
gustavatmel 1:9c5af431a1f1 191 * unsuccessful.
gustavatmel 1:9c5af431a1f1 192 */
gustavatmel 1:9c5af431a1f1 193 int SPIM_Trans(mxc_spim_regs_t *spim, spim_req_t *req);
gustavatmel 1:9c5af431a1f1 194 /**
gustavatmel 1:9c5af431a1f1 195 * @defgroup spim_async SPIM Asynchrous Functions
gustavatmel 1:9c5af431a1f1 196 * @{
gustavatmel 1:9c5af431a1f1 197 */
gustavatmel 1:9c5af431a1f1 198 /**
gustavatmel 1:9c5af431a1f1 199 * @brief Asynchronously read/write SPIM data.
gustavatmel 1:9c5af431a1f1 200 *
gustavatmel 1:9c5af431a1f1 201 * @param spim Pointer to the SPIM register structure.
gustavatmel 1:9c5af431a1f1 202 * @param req Request for a SPIM transaction.
gustavatmel 1:9c5af431a1f1 203 * @note Request struct must remain allocated until callback.
gustavatmel 1:9c5af431a1f1 204 *
gustavatmel 1:9c5af431a1f1 205 * @return #E_NO_ERROR if everything is successful, @ref MXC_Error_Codes
gustavatmel 1:9c5af431a1f1 206 * "error" if unsuccessful.
gustavatmel 1:9c5af431a1f1 207 */
gustavatmel 1:9c5af431a1f1 208 int SPIM_TransAsync(mxc_spim_regs_t *spim, spim_req_t *req);
gustavatmel 1:9c5af431a1f1 209
gustavatmel 1:9c5af431a1f1 210 /**
gustavatmel 1:9c5af431a1f1 211 * @brief Abort asynchronous request.
gustavatmel 1:9c5af431a1f1 212 *
gustavatmel 1:9c5af431a1f1 213 * @param req Pointer to a request structure for a SPIM transaction.
gustavatmel 1:9c5af431a1f1 214 *
gustavatmel 1:9c5af431a1f1 215 * @return #E_NO_ERROR if request aborted, , @ref MXC_Error_Codes "error" if
gustavatmel 1:9c5af431a1f1 216 * unsuccessful.
gustavatmel 1:9c5af431a1f1 217 */
gustavatmel 1:9c5af431a1f1 218 int SPIM_AbortAsync(spim_req_t *req);
gustavatmel 1:9c5af431a1f1 219
gustavatmel 1:9c5af431a1f1 220 /**
gustavatmel 1:9c5af431a1f1 221 * @brief SPIM interrupt handler.
gustavatmel 1:9c5af431a1f1 222 * @details This function should be called by the application from the
gustavatmel 1:9c5af431a1f1 223 * interrupt handler if SPIM interrupts are enabled. Alternately,
gustavatmel 1:9c5af431a1f1 224 * this function can be periodically polled by the application if
gustavatmel 1:9c5af431a1f1 225 * SPIM interrupts are disabled.
gustavatmel 1:9c5af431a1f1 226 *
gustavatmel 1:9c5af431a1f1 227 * @param spim Base address of the SPIM module.
gustavatmel 1:9c5af431a1f1 228 */
gustavatmel 1:9c5af431a1f1 229 void SPIM_Handler(mxc_spim_regs_t *spim);
gustavatmel 1:9c5af431a1f1 230
gustavatmel 1:9c5af431a1f1 231 /**
gustavatmel 1:9c5af431a1f1 232 * @brief Check the SPIM to see if it's busy.
gustavatmel 1:9c5af431a1f1 233 *
gustavatmel 1:9c5af431a1f1 234 * @param spim Pointer to the SPIM register structure.
gustavatmel 1:9c5af431a1f1 235 *
gustavatmel 1:9c5af431a1f1 236 * @retval #E_NO_ERROR if idle.
gustavatmel 1:9c5af431a1f1 237 * @retval #E_BUSY if in use.
gustavatmel 1:9c5af431a1f1 238 */
gustavatmel 1:9c5af431a1f1 239 int SPIM_Busy(mxc_spim_regs_t *spim);
gustavatmel 1:9c5af431a1f1 240 /**@} end of spim_async define group */
gustavatmel 1:9c5af431a1f1 241
gustavatmel 1:9c5af431a1f1 242 /**
gustavatmel 1:9c5af431a1f1 243 * @brief Attempts to prepare the SPIM for Low Power Sleep Modes.
gustavatmel 1:9c5af431a1f1 244 * @details Checks for any ongoing transactions. Disables interrupts if the
gustavatmel 1:9c5af431a1f1 245 * SPIM is idle.
gustavatmel 1:9c5af431a1f1 246 *
gustavatmel 1:9c5af431a1f1 247 * @param spim The spim
gustavatmel 1:9c5af431a1f1 248 *
gustavatmel 1:9c5af431a1f1 249 * @return #E_NO_ERROR if ready to sleep.
gustavatmel 1:9c5af431a1f1 250 * @return #E_BUSY if not able to sleep at this time.
gustavatmel 1:9c5af431a1f1 251 */
gustavatmel 1:9c5af431a1f1 252 int SPIM_PrepForSleep(mxc_spim_regs_t *spim);
gustavatmel 1:9c5af431a1f1 253
gustavatmel 1:9c5af431a1f1 254 /**
gustavatmel 1:9c5af431a1f1 255 * @brief Enables the SPIM without overwriting the existing configuration.
gustavatmel 1:9c5af431a1f1 256 *
gustavatmel 1:9c5af431a1f1 257 * @param spim Pointer to the SPIM register structure.
gustavatmel 1:9c5af431a1f1 258 */
gustavatmel 1:9c5af431a1f1 259 __STATIC_INLINE void SPIM_Enable(mxc_spim_regs_t *spim)
gustavatmel 1:9c5af431a1f1 260 {
gustavatmel 1:9c5af431a1f1 261 spim->gen_ctrl |= (MXC_F_SPIM_GEN_CTRL_SPI_MSTR_EN |
gustavatmel 1:9c5af431a1f1 262 MXC_F_SPIM_GEN_CTRL_TX_FIFO_EN | MXC_F_SPIM_GEN_CTRL_RX_FIFO_EN);
gustavatmel 1:9c5af431a1f1 263 }
gustavatmel 1:9c5af431a1f1 264
gustavatmel 1:9c5af431a1f1 265 /**
gustavatmel 1:9c5af431a1f1 266 * @brief Drains/empties the data in the RX FIFO.
gustavatmel 1:9c5af431a1f1 267 *
gustavatmel 1:9c5af431a1f1 268 * @param spim Pointer to the SPIM register structure.
gustavatmel 1:9c5af431a1f1 269 */
gustavatmel 1:9c5af431a1f1 270 __STATIC_INLINE void SPIM_DrainRX(mxc_spim_regs_t *spim)
gustavatmel 1:9c5af431a1f1 271 {
gustavatmel 1:9c5af431a1f1 272 uint32_t ctrl_save = spim->gen_ctrl;
gustavatmel 1:9c5af431a1f1 273 spim->gen_ctrl = (ctrl_save & ~MXC_F_SPIM_GEN_CTRL_RX_FIFO_EN);
gustavatmel 1:9c5af431a1f1 274 spim->gen_ctrl = ctrl_save;
gustavatmel 1:9c5af431a1f1 275 }
gustavatmel 1:9c5af431a1f1 276
gustavatmel 1:9c5af431a1f1 277 /**
gustavatmel 1:9c5af431a1f1 278 * @brief Drains/empties the data in the TX FIFO.
gustavatmel 1:9c5af431a1f1 279 *
gustavatmel 1:9c5af431a1f1 280 * @param spim Pointer to the SPIM register structure.
gustavatmel 1:9c5af431a1f1 281 */
gustavatmel 1:9c5af431a1f1 282 __STATIC_INLINE void SPIM_DrainTX(mxc_spim_regs_t *spim)
gustavatmel 1:9c5af431a1f1 283 {
gustavatmel 1:9c5af431a1f1 284 uint32_t ctrl_save = spim->gen_ctrl;
gustavatmel 1:9c5af431a1f1 285 spim->gen_ctrl = (ctrl_save & ~MXC_F_SPIM_GEN_CTRL_TX_FIFO_EN);
gustavatmel 1:9c5af431a1f1 286 spim->gen_ctrl = ctrl_save;
gustavatmel 1:9c5af431a1f1 287 }
gustavatmel 1:9c5af431a1f1 288
gustavatmel 1:9c5af431a1f1 289 /**
gustavatmel 1:9c5af431a1f1 290 * @brief Returns the number of bytes free in the TX FIFO.
gustavatmel 1:9c5af431a1f1 291 *
gustavatmel 1:9c5af431a1f1 292 * @param spim Pointer to the SPIM register structure.
gustavatmel 1:9c5af431a1f1 293 *
gustavatmel 1:9c5af431a1f1 294 * @return Number of bytes free in Transmit FIFO.
gustavatmel 1:9c5af431a1f1 295 */
gustavatmel 1:9c5af431a1f1 296 __STATIC_INLINE unsigned SPIM_NumWriteAvail(mxc_spim_regs_t *spim)
gustavatmel 1:9c5af431a1f1 297 {
gustavatmel 1:9c5af431a1f1 298 return (MXC_CFG_SPIM_FIFO_DEPTH - ((spim->fifo_ctrl &
gustavatmel 1:9c5af431a1f1 299 MXC_F_SPIM_FIFO_CTRL_TX_FIFO_USED) >> MXC_F_SPIM_FIFO_CTRL_TX_FIFO_USED_POS));
gustavatmel 1:9c5af431a1f1 300 }
gustavatmel 1:9c5af431a1f1 301
gustavatmel 1:9c5af431a1f1 302 /**
gustavatmel 1:9c5af431a1f1 303 * @brief Returns the number of bytes available to read in the RX FIFO.
gustavatmel 1:9c5af431a1f1 304 *
gustavatmel 1:9c5af431a1f1 305 * @param spim Pointer to the SPIM register structure.
gustavatmel 1:9c5af431a1f1 306 *
gustavatmel 1:9c5af431a1f1 307 * @return Number of bytes in RX FIFO.
gustavatmel 1:9c5af431a1f1 308 */
gustavatmel 1:9c5af431a1f1 309 __STATIC_INLINE unsigned SPIM_NumReadAvail(mxc_spim_regs_t *spim)
gustavatmel 1:9c5af431a1f1 310 {
gustavatmel 1:9c5af431a1f1 311 return ((spim->fifo_ctrl & MXC_F_SPIM_FIFO_CTRL_RX_FIFO_USED) >>
gustavatmel 1:9c5af431a1f1 312 MXC_F_SPIM_FIFO_CTRL_RX_FIFO_USED_POS);
gustavatmel 1:9c5af431a1f1 313 }
gustavatmel 1:9c5af431a1f1 314
gustavatmel 1:9c5af431a1f1 315 /**
gustavatmel 1:9c5af431a1f1 316 * @brief Clear the SPIM interrupt flags.
gustavatmel 1:9c5af431a1f1 317 *
gustavatmel 1:9c5af431a1f1 318 * @param spim Pointer to the SPIM register structure.
gustavatmel 1:9c5af431a1f1 319 * @param mask Mask of the SPIM interrupt flags to clear, see @ref
gustavatmel 1:9c5af431a1f1 320 * SPIM_INTFL_Register Register for the SPIM interrupt flag
gustavatmel 1:9c5af431a1f1 321 * bit masks.
gustavatmel 1:9c5af431a1f1 322 */
gustavatmel 1:9c5af431a1f1 323 __STATIC_INLINE void SPIM_ClearFlags(mxc_spim_regs_t *spim, uint32_t mask)
gustavatmel 1:9c5af431a1f1 324 {
gustavatmel 1:9c5af431a1f1 325 spim->intfl = mask;
gustavatmel 1:9c5af431a1f1 326 }
gustavatmel 1:9c5af431a1f1 327
gustavatmel 1:9c5af431a1f1 328 /**
gustavatmel 1:9c5af431a1f1 329 * @brief Read the current SPIM interrupt flags.
gustavatmel 1:9c5af431a1f1 330 *
gustavatmel 1:9c5af431a1f1 331 * @param spim Pointer to the SPIM register structure.
gustavatmel 1:9c5af431a1f1 332 *
gustavatmel 1:9c5af431a1f1 333 * @return Mask of currently set SPIM interrupt flags, see @ref
gustavatmel 1:9c5af431a1f1 334 * SPIM_INTFL_Register Register for the SPIM interrupt flag bit
gustavatmel 1:9c5af431a1f1 335 * masks.
gustavatmel 1:9c5af431a1f1 336 */
gustavatmel 1:9c5af431a1f1 337 __STATIC_INLINE unsigned SPIM_GetFlags(mxc_spim_regs_t *spim)
gustavatmel 1:9c5af431a1f1 338 {
gustavatmel 1:9c5af431a1f1 339 return (spim->intfl);
gustavatmel 1:9c5af431a1f1 340 }
gustavatmel 1:9c5af431a1f1 341
gustavatmel 1:9c5af431a1f1 342 /**@} end of group spim_comm */
gustavatmel 1:9c5af431a1f1 343 #ifdef __cplusplus
gustavatmel 1:9c5af431a1f1 344 }
gustavatmel 1:9c5af431a1f1 345 #endif
gustavatmel 1:9c5af431a1f1 346
gustavatmel 1:9c5af431a1f1 347 #endif /* _SPIM_H_ */