mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

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

Committer:
mbed_official
Date:
Wed Mar 19 10:15:22 2014 +0000
Revision:
125:23cc3068a9e4
Synchronized with git revision ace35dfba3748c7cdc102eb38ec6b9e1067c3252

Full URL: https://github.com/mbedmicro/mbed/commit/ace35dfba3748c7cdc102eb38ec6b9e1067c3252/

[NUCLEO_F302R8] Add cmsis and hal files + change F401RE clock to 84MHz

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 125:23cc3068a9e4 1 /**
mbed_official 125:23cc3068a9e4 2 ******************************************************************************
mbed_official 125:23cc3068a9e4 3 * @file stm32f30x_spi.c
mbed_official 125:23cc3068a9e4 4 * @author MCD Application Team
mbed_official 125:23cc3068a9e4 5 * @version V1.1.0
mbed_official 125:23cc3068a9e4 6 * @date 27-February-2014
mbed_official 125:23cc3068a9e4 7 * @brief This file provides firmware functions to manage the following
mbed_official 125:23cc3068a9e4 8 * functionalities of the Serial peripheral interface (SPI):
mbed_official 125:23cc3068a9e4 9 * + Initialization and Configuration
mbed_official 125:23cc3068a9e4 10 * + Data transfers functions
mbed_official 125:23cc3068a9e4 11 * + Hardware CRC Calculation
mbed_official 125:23cc3068a9e4 12 * + DMA transfers management
mbed_official 125:23cc3068a9e4 13 * + Interrupts and flags management
mbed_official 125:23cc3068a9e4 14 *
mbed_official 125:23cc3068a9e4 15 * @verbatim
mbed_official 125:23cc3068a9e4 16
mbed_official 125:23cc3068a9e4 17
mbed_official 125:23cc3068a9e4 18 ===============================================================================
mbed_official 125:23cc3068a9e4 19 ##### How to use this driver #####
mbed_official 125:23cc3068a9e4 20 ===============================================================================
mbed_official 125:23cc3068a9e4 21 [..]
mbed_official 125:23cc3068a9e4 22 (#) Enable peripheral clock using RCC_APBPeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE)
mbed_official 125:23cc3068a9e4 23 function for SPI1 or using RCC_APBPeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE)
mbed_official 125:23cc3068a9e4 24 function for SPI2.
mbed_official 125:23cc3068a9e4 25 (#) Enable SCK, MOSI, MISO and NSS GPIO clocks using RCC_AHBPeriphClockCmd()
mbed_official 125:23cc3068a9e4 26 function.
mbed_official 125:23cc3068a9e4 27 (#) Peripherals alternate function:
mbed_official 125:23cc3068a9e4 28 (++) Connect the pin to the desired peripherals' Alternate
mbed_official 125:23cc3068a9e4 29 Function (AF) using GPIO_PinAFConfig() function.
mbed_official 125:23cc3068a9e4 30 (++) Configure the desired pin in alternate function by:
mbed_official 125:23cc3068a9e4 31 GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AF.
mbed_official 125:23cc3068a9e4 32 (++) Select the type, pull-up/pull-down and output speed via
mbed_official 125:23cc3068a9e4 33 GPIO_PuPd, GPIO_OType and GPIO_Speed members.
mbed_official 125:23cc3068a9e4 34 (++) Call GPIO_Init() function.
mbed_official 125:23cc3068a9e4 35 (#) Program the Polarity, Phase, First Data, Baud Rate Prescaler, Slave
mbed_official 125:23cc3068a9e4 36 Management, Peripheral Mode and CRC Polynomial values using the SPI_Init()
mbed_official 125:23cc3068a9e4 37 function in SPI mode. In I2S mode, program the Mode, Standard, Data Format,
mbed_official 125:23cc3068a9e4 38 MCLK Output, Audio frequency and Polarity using I2S_Init() function.
mbed_official 125:23cc3068a9e4 39 (#) Configure the FIFO threshold using SPI_RxFIFOThresholdConfig() to select
mbed_official 125:23cc3068a9e4 40 at which threshold the RXNE event is generated.
mbed_official 125:23cc3068a9e4 41 (#) Enable the NVIC and the corresponding interrupt using the function
mbed_official 125:23cc3068a9e4 42 SPI_I2S_ITConfig() if you need to use interrupt mode.
mbed_official 125:23cc3068a9e4 43 (#) When using the DMA mode
mbed_official 125:23cc3068a9e4 44 (++) Configure the DMA using DMA_Init() function.
mbed_official 125:23cc3068a9e4 45 (++) Active the needed channel Request using SPI_I2S_DMACmd() function.
mbed_official 125:23cc3068a9e4 46 (#) Enable the SPI using the SPI_Cmd() function or enable the I2S using
mbed_official 125:23cc3068a9e4 47 I2S_Cmd().
mbed_official 125:23cc3068a9e4 48 (#) Enable the DMA using the DMA_Cmd() function when using DMA mode.
mbed_official 125:23cc3068a9e4 49 (#) Optionally you can enable/configure the following parameters without
mbed_official 125:23cc3068a9e4 50 re-initialization (i.e there is no need to call again SPI_Init() function):
mbed_official 125:23cc3068a9e4 51 (++) When bidirectional mode (SPI_Direction_1Line_Rx or SPI_Direction_1Line_Tx)
mbed_official 125:23cc3068a9e4 52 is programmed as Data direction parameter using the SPI_Init() function
mbed_official 125:23cc3068a9e4 53 it can be possible to switch between SPI_Direction_Tx or SPI_Direction_Rx
mbed_official 125:23cc3068a9e4 54 using the SPI_BiDirectionalLineConfig() function.
mbed_official 125:23cc3068a9e4 55 (++) When SPI_NSS_Soft is selected as Slave Select Management parameter
mbed_official 125:23cc3068a9e4 56 using the SPI_Init() function it can be possible to manage the
mbed_official 125:23cc3068a9e4 57 NSS internal signal using the SPI_NSSInternalSoftwareConfig() function.
mbed_official 125:23cc3068a9e4 58 (++) Reconfigure the data size using the SPI_DataSizeConfig() function.
mbed_official 125:23cc3068a9e4 59 (++) Enable or disable the SS output using the SPI_SSOutputCmd() function.
mbed_official 125:23cc3068a9e4 60 (#) To use the CRC Hardware calculation feature refer to the Peripheral
mbed_official 125:23cc3068a9e4 61 CRC hardware Calculation subsection.
mbed_official 125:23cc3068a9e4 62 [..] It is possible to use SPI in I2S full duplex mode, in this case, each SPI
mbed_official 125:23cc3068a9e4 63 peripheral is able to manage sending and receiving data simultaneously
mbed_official 125:23cc3068a9e4 64 using two data lines. Each SPI peripheral has an extended block called I2Sxext
mbed_official 125:23cc3068a9e4 65 (ie. I2S2ext for SPI2 and I2S3ext for SPI3).
mbed_official 125:23cc3068a9e4 66 The extension block is not a full SPI IP, it is used only as I2S slave to
mbed_official 125:23cc3068a9e4 67 implement full duplex mode. The extension block uses the same clock sources
mbed_official 125:23cc3068a9e4 68 as its master.
mbed_official 125:23cc3068a9e4 69 To configure I2S full duplex you have to:
mbed_official 125:23cc3068a9e4 70 (#) Configure SPIx in I2S mode (I2S_Init() function) as described above.
mbed_official 125:23cc3068a9e4 71 (#) Call the I2S_FullDuplexConfig() function using the same strucutre passed to
mbed_official 125:23cc3068a9e4 72 I2S_Init() function.
mbed_official 125:23cc3068a9e4 73 (#) Call I2S_Cmd() for SPIx then for its extended block.
mbed_official 125:23cc3068a9e4 74 (#) Configure interrupts or DMA requests and to get/clear flag status,
mbed_official 125:23cc3068a9e4 75 use I2Sxext instance for the extension block.
mbed_official 125:23cc3068a9e4 76 [..] Functions that can be called with I2Sxext instances are:
mbed_official 125:23cc3068a9e4 77 I2S_Cmd(), I2S_FullDuplexConfig(), SPI_I2S_ReceiveData16(), SPI_I2S_SendData16(),
mbed_official 125:23cc3068a9e4 78 SPI_I2S_DMACmd(), SPI_I2S_ITConfig(), SPI_I2S_GetFlagStatus(), SPI_I2S_ClearFlag(),
mbed_official 125:23cc3068a9e4 79 SPI_I2S_GetITStatus() and SPI_I2S_ClearITPendingBit().
mbed_official 125:23cc3068a9e4 80 [..] Example: To use SPI3 in Full duplex mode (SPI3 is Master Tx, I2S3ext is Slave Rx):
mbed_official 125:23cc3068a9e4 81 [..] RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);
mbed_official 125:23cc3068a9e4 82 I2S_StructInit(&I2SInitStruct);
mbed_official 125:23cc3068a9e4 83 I2SInitStruct.Mode = I2S_Mode_MasterTx;
mbed_official 125:23cc3068a9e4 84 I2S_Init(SPI3, &I2SInitStruct);
mbed_official 125:23cc3068a9e4 85 I2S_FullDuplexConfig(SPI3ext, &I2SInitStruct)
mbed_official 125:23cc3068a9e4 86 I2S_Cmd(SPI3, ENABLE);
mbed_official 125:23cc3068a9e4 87 I2S_Cmd(SPI3ext, ENABLE);
mbed_official 125:23cc3068a9e4 88 ...
mbed_official 125:23cc3068a9e4 89 while (SPI_I2S_GetFlagStatus(SPI2, SPI_FLAG_TXE) == RESET)
mbed_official 125:23cc3068a9e4 90 {}
mbed_official 125:23cc3068a9e4 91 SPI_I2S_SendData16(SPI3, txdata[i]);
mbed_official 125:23cc3068a9e4 92 ...
mbed_official 125:23cc3068a9e4 93 while (SPI_I2S_GetFlagStatus(I2S3ext, SPI_FLAG_RXNE) == RESET)
mbed_official 125:23cc3068a9e4 94 {}
mbed_official 125:23cc3068a9e4 95 rxdata[i] = SPI_I2S_ReceiveData16(I2S3ext);
mbed_official 125:23cc3068a9e4 96 ...
mbed_official 125:23cc3068a9e4 97 [..]
mbed_official 125:23cc3068a9e4 98 (@) In SPI mode: To use the SPI TI mode, call the function SPI_TIModeCmd()
mbed_official 125:23cc3068a9e4 99 just after calling the function SPI_Init().
mbed_official 125:23cc3068a9e4 100
mbed_official 125:23cc3068a9e4 101 @endverbatim
mbed_official 125:23cc3068a9e4 102 ******************************************************************************
mbed_official 125:23cc3068a9e4 103 * @attention
mbed_official 125:23cc3068a9e4 104 *
mbed_official 125:23cc3068a9e4 105 * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
mbed_official 125:23cc3068a9e4 106 *
mbed_official 125:23cc3068a9e4 107 * Redistribution and use in source and binary forms, with or without modification,
mbed_official 125:23cc3068a9e4 108 * are permitted provided that the following conditions are met:
mbed_official 125:23cc3068a9e4 109 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 125:23cc3068a9e4 110 * this list of conditions and the following disclaimer.
mbed_official 125:23cc3068a9e4 111 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 125:23cc3068a9e4 112 * this list of conditions and the following disclaimer in the documentation
mbed_official 125:23cc3068a9e4 113 * and/or other materials provided with the distribution.
mbed_official 125:23cc3068a9e4 114 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 125:23cc3068a9e4 115 * may be used to endorse or promote products derived from this software
mbed_official 125:23cc3068a9e4 116 * without specific prior written permission.
mbed_official 125:23cc3068a9e4 117 *
mbed_official 125:23cc3068a9e4 118 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 125:23cc3068a9e4 119 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 125:23cc3068a9e4 120 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 125:23cc3068a9e4 121 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 125:23cc3068a9e4 122 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 125:23cc3068a9e4 123 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 125:23cc3068a9e4 124 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 125:23cc3068a9e4 125 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 125:23cc3068a9e4 126 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 125:23cc3068a9e4 127 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 125:23cc3068a9e4 128 *
mbed_official 125:23cc3068a9e4 129 ******************************************************************************
mbed_official 125:23cc3068a9e4 130 */
mbed_official 125:23cc3068a9e4 131
mbed_official 125:23cc3068a9e4 132 /* Includes ------------------------------------------------------------------*/
mbed_official 125:23cc3068a9e4 133 #include "stm32f30x_spi.h"
mbed_official 125:23cc3068a9e4 134 #include "stm32f30x_rcc.h"
mbed_official 125:23cc3068a9e4 135
mbed_official 125:23cc3068a9e4 136 /** @addtogroup STM32F30x_StdPeriph_Driver
mbed_official 125:23cc3068a9e4 137 * @{
mbed_official 125:23cc3068a9e4 138 */
mbed_official 125:23cc3068a9e4 139
mbed_official 125:23cc3068a9e4 140 /** @defgroup SPI
mbed_official 125:23cc3068a9e4 141 * @brief SPI driver modules
mbed_official 125:23cc3068a9e4 142 * @{
mbed_official 125:23cc3068a9e4 143 */
mbed_official 125:23cc3068a9e4 144
mbed_official 125:23cc3068a9e4 145 /* Private typedef -----------------------------------------------------------*/
mbed_official 125:23cc3068a9e4 146 /* Private define ------------------------------------------------------------*/
mbed_official 125:23cc3068a9e4 147 /* SPI registers Masks */
mbed_official 125:23cc3068a9e4 148 #define CR1_CLEAR_MASK ((uint16_t)0x3040)
mbed_official 125:23cc3068a9e4 149 #define CR2_LDMA_MASK ((uint16_t)0x9FFF)
mbed_official 125:23cc3068a9e4 150
mbed_official 125:23cc3068a9e4 151 #define I2SCFGR_CLEAR_MASK ((uint16_t)0xF040)
mbed_official 125:23cc3068a9e4 152
mbed_official 125:23cc3068a9e4 153 /* Private macro -------------------------------------------------------------*/
mbed_official 125:23cc3068a9e4 154 /* Private variables ---------------------------------------------------------*/
mbed_official 125:23cc3068a9e4 155 /* Private function prototypes -----------------------------------------------*/
mbed_official 125:23cc3068a9e4 156 /* Private functions ---------------------------------------------------------*/
mbed_official 125:23cc3068a9e4 157
mbed_official 125:23cc3068a9e4 158 /** @defgroup SPI_Private_Functions
mbed_official 125:23cc3068a9e4 159 * @{
mbed_official 125:23cc3068a9e4 160 */
mbed_official 125:23cc3068a9e4 161
mbed_official 125:23cc3068a9e4 162 /** @defgroup SPI_Group1 Initialization and Configuration functions
mbed_official 125:23cc3068a9e4 163 * @brief Initialization and Configuration functions
mbed_official 125:23cc3068a9e4 164 *
mbed_official 125:23cc3068a9e4 165 @verbatim
mbed_official 125:23cc3068a9e4 166 ===============================================================================
mbed_official 125:23cc3068a9e4 167 ##### Initialization and Configuration functions #####
mbed_official 125:23cc3068a9e4 168 ===============================================================================
mbed_official 125:23cc3068a9e4 169 [..] This section provides a set of functions allowing to initialize the SPI Direction,
mbed_official 125:23cc3068a9e4 170 SPI Mode, SPI Data Size, SPI Polarity, SPI Phase, SPI NSS Management, SPI Baud
mbed_official 125:23cc3068a9e4 171 Rate Prescaler, SPI First Bit and SPI CRC Polynomial.
mbed_official 125:23cc3068a9e4 172 [..] The SPI_Init() function follows the SPI configuration procedures for Master mode
mbed_official 125:23cc3068a9e4 173 and Slave mode (details for these procedures are available in reference manual).
mbed_official 125:23cc3068a9e4 174 [..] When the Software NSS management (SPI_InitStruct->SPI_NSS = SPI_NSS_Soft) is selected,
mbed_official 125:23cc3068a9e4 175 use the following function to manage the NSS bit:
mbed_official 125:23cc3068a9e4 176 void SPI_NSSInternalSoftwareConfig(SPI_TypeDef* SPIx, uint16_t SPI_NSSInternalSoft);
mbed_official 125:23cc3068a9e4 177 [..] In Master mode, when the Hardware NSS management (SPI_InitStruct->SPI_NSS = SPI_NSS_Hard)
mbed_official 125:23cc3068a9e4 178 is selected, use the follwoing function to enable the NSS output feature.
mbed_official 125:23cc3068a9e4 179 void SPI_SSOutputCmd(SPI_TypeDef* SPIx, FunctionalState NewState);
mbed_official 125:23cc3068a9e4 180 [..] The NSS pulse mode can be managed by the SPI TI mode when enabling it using the
mbed_official 125:23cc3068a9e4 181 following function: void SPI_TIModeCmd(SPI_TypeDef* SPIx, FunctionalState NewState);
mbed_official 125:23cc3068a9e4 182 And it can be managed by software in the SPI Motorola mode using this function:
mbed_official 125:23cc3068a9e4 183 void SPI_NSSPulseModeCmd(SPI_TypeDef* SPIx, FunctionalState NewState);
mbed_official 125:23cc3068a9e4 184 [..] This section provides also functions to initialize the I2S Mode, Standard,
mbed_official 125:23cc3068a9e4 185 Data Format, MCLK Output, Audio frequency and Polarity.
mbed_official 125:23cc3068a9e4 186 [..] The I2S_Init() function follows the I2S configuration procedures for Master mode
mbed_official 125:23cc3068a9e4 187 and Slave mode.
mbed_official 125:23cc3068a9e4 188
mbed_official 125:23cc3068a9e4 189 @endverbatim
mbed_official 125:23cc3068a9e4 190 * @{
mbed_official 125:23cc3068a9e4 191 */
mbed_official 125:23cc3068a9e4 192
mbed_official 125:23cc3068a9e4 193 /**
mbed_official 125:23cc3068a9e4 194 * @brief Deinitializes the SPIx peripheral registers to their default
mbed_official 125:23cc3068a9e4 195 * reset values.
mbed_official 125:23cc3068a9e4 196 * @param SPIx: To select the SPIx peripheral, where x can be: 1, 2 or 3
mbed_official 125:23cc3068a9e4 197 * in SPI mode.
mbed_official 125:23cc3068a9e4 198 * @retval None
mbed_official 125:23cc3068a9e4 199 */
mbed_official 125:23cc3068a9e4 200 void SPI_I2S_DeInit(SPI_TypeDef* SPIx)
mbed_official 125:23cc3068a9e4 201 {
mbed_official 125:23cc3068a9e4 202 /* Check the parameters */
mbed_official 125:23cc3068a9e4 203 assert_param(IS_SPI_ALL_PERIPH(SPIx));
mbed_official 125:23cc3068a9e4 204
mbed_official 125:23cc3068a9e4 205 if (SPIx == SPI1)
mbed_official 125:23cc3068a9e4 206 {
mbed_official 125:23cc3068a9e4 207 /* Enable SPI1 reset state */
mbed_official 125:23cc3068a9e4 208 RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, ENABLE);
mbed_official 125:23cc3068a9e4 209 /* Release SPI1 from reset state */
mbed_official 125:23cc3068a9e4 210 RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, DISABLE);
mbed_official 125:23cc3068a9e4 211 }
mbed_official 125:23cc3068a9e4 212 else if (SPIx == SPI2)
mbed_official 125:23cc3068a9e4 213 {
mbed_official 125:23cc3068a9e4 214 /* Enable SPI2 reset state */
mbed_official 125:23cc3068a9e4 215 RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, ENABLE);
mbed_official 125:23cc3068a9e4 216 /* Release SPI2 from reset state */
mbed_official 125:23cc3068a9e4 217 RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, DISABLE);
mbed_official 125:23cc3068a9e4 218 }
mbed_official 125:23cc3068a9e4 219 else
mbed_official 125:23cc3068a9e4 220 {
mbed_official 125:23cc3068a9e4 221 if (SPIx == SPI3)
mbed_official 125:23cc3068a9e4 222 {
mbed_official 125:23cc3068a9e4 223 /* Enable SPI3 reset state */
mbed_official 125:23cc3068a9e4 224 RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI3, ENABLE);
mbed_official 125:23cc3068a9e4 225 /* Release SPI3 from reset state */
mbed_official 125:23cc3068a9e4 226 RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI3, DISABLE);
mbed_official 125:23cc3068a9e4 227 }
mbed_official 125:23cc3068a9e4 228 }
mbed_official 125:23cc3068a9e4 229 }
mbed_official 125:23cc3068a9e4 230
mbed_official 125:23cc3068a9e4 231 /**
mbed_official 125:23cc3068a9e4 232 * @brief Fills each SPI_InitStruct member with its default value.
mbed_official 125:23cc3068a9e4 233 * @param SPI_InitStruct: pointer to a SPI_InitTypeDef structure which will be initialized.
mbed_official 125:23cc3068a9e4 234 * @retval None
mbed_official 125:23cc3068a9e4 235 */
mbed_official 125:23cc3068a9e4 236 void SPI_StructInit(SPI_InitTypeDef* SPI_InitStruct)
mbed_official 125:23cc3068a9e4 237 {
mbed_official 125:23cc3068a9e4 238 /*--------------- Reset SPI init structure parameters values -----------------*/
mbed_official 125:23cc3068a9e4 239 /* Initialize the SPI_Direction member */
mbed_official 125:23cc3068a9e4 240 SPI_InitStruct->SPI_Direction = SPI_Direction_2Lines_FullDuplex;
mbed_official 125:23cc3068a9e4 241 /* Initialize the SPI_Mode member */
mbed_official 125:23cc3068a9e4 242 SPI_InitStruct->SPI_Mode = SPI_Mode_Slave;
mbed_official 125:23cc3068a9e4 243 /* Initialize the SPI_DataSize member */
mbed_official 125:23cc3068a9e4 244 SPI_InitStruct->SPI_DataSize = SPI_DataSize_8b;
mbed_official 125:23cc3068a9e4 245 /* Initialize the SPI_CPOL member */
mbed_official 125:23cc3068a9e4 246 SPI_InitStruct->SPI_CPOL = SPI_CPOL_Low;
mbed_official 125:23cc3068a9e4 247 /* Initialize the SPI_CPHA member */
mbed_official 125:23cc3068a9e4 248 SPI_InitStruct->SPI_CPHA = SPI_CPHA_1Edge;
mbed_official 125:23cc3068a9e4 249 /* Initialize the SPI_NSS member */
mbed_official 125:23cc3068a9e4 250 SPI_InitStruct->SPI_NSS = SPI_NSS_Hard;
mbed_official 125:23cc3068a9e4 251 /* Initialize the SPI_BaudRatePrescaler member */
mbed_official 125:23cc3068a9e4 252 SPI_InitStruct->SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
mbed_official 125:23cc3068a9e4 253 /* Initialize the SPI_FirstBit member */
mbed_official 125:23cc3068a9e4 254 SPI_InitStruct->SPI_FirstBit = SPI_FirstBit_MSB;
mbed_official 125:23cc3068a9e4 255 /* Initialize the SPI_CRCPolynomial member */
mbed_official 125:23cc3068a9e4 256 SPI_InitStruct->SPI_CRCPolynomial = 7;
mbed_official 125:23cc3068a9e4 257 }
mbed_official 125:23cc3068a9e4 258
mbed_official 125:23cc3068a9e4 259 /**
mbed_official 125:23cc3068a9e4 260 * @brief Initializes the SPIx peripheral according to the specified
mbed_official 125:23cc3068a9e4 261 * parameters in the SPI_InitStruct.
mbed_official 125:23cc3068a9e4 262 * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
mbed_official 125:23cc3068a9e4 263 * @param SPI_InitStruct: pointer to a SPI_InitTypeDef structure that
mbed_official 125:23cc3068a9e4 264 * contains the configuration information for the specified SPI peripheral.
mbed_official 125:23cc3068a9e4 265 * @retval None
mbed_official 125:23cc3068a9e4 266 */
mbed_official 125:23cc3068a9e4 267 void SPI_Init(SPI_TypeDef* SPIx, SPI_InitTypeDef* SPI_InitStruct)
mbed_official 125:23cc3068a9e4 268 {
mbed_official 125:23cc3068a9e4 269 uint16_t tmpreg = 0;
mbed_official 125:23cc3068a9e4 270
mbed_official 125:23cc3068a9e4 271 /* check the parameters */
mbed_official 125:23cc3068a9e4 272 assert_param(IS_SPI_ALL_PERIPH(SPIx));
mbed_official 125:23cc3068a9e4 273
mbed_official 125:23cc3068a9e4 274 /* Check the SPI parameters */
mbed_official 125:23cc3068a9e4 275 assert_param(IS_SPI_DIRECTION_MODE(SPI_InitStruct->SPI_Direction));
mbed_official 125:23cc3068a9e4 276 assert_param(IS_SPI_MODE(SPI_InitStruct->SPI_Mode));
mbed_official 125:23cc3068a9e4 277 assert_param(IS_SPI_DATA_SIZE(SPI_InitStruct->SPI_DataSize));
mbed_official 125:23cc3068a9e4 278 assert_param(IS_SPI_CPOL(SPI_InitStruct->SPI_CPOL));
mbed_official 125:23cc3068a9e4 279 assert_param(IS_SPI_CPHA(SPI_InitStruct->SPI_CPHA));
mbed_official 125:23cc3068a9e4 280 assert_param(IS_SPI_NSS(SPI_InitStruct->SPI_NSS));
mbed_official 125:23cc3068a9e4 281 assert_param(IS_SPI_BAUDRATE_PRESCALER(SPI_InitStruct->SPI_BaudRatePrescaler));
mbed_official 125:23cc3068a9e4 282 assert_param(IS_SPI_FIRST_BIT(SPI_InitStruct->SPI_FirstBit));
mbed_official 125:23cc3068a9e4 283 assert_param(IS_SPI_CRC_POLYNOMIAL(SPI_InitStruct->SPI_CRCPolynomial));
mbed_official 125:23cc3068a9e4 284
mbed_official 125:23cc3068a9e4 285 /* Configuring the SPI in master mode */
mbed_official 125:23cc3068a9e4 286 if(SPI_InitStruct->SPI_Mode == SPI_Mode_Master)
mbed_official 125:23cc3068a9e4 287 {
mbed_official 125:23cc3068a9e4 288 /*---------------------------- SPIx CR1 Configuration ------------------------*/
mbed_official 125:23cc3068a9e4 289 /* Get the SPIx CR1 value */
mbed_official 125:23cc3068a9e4 290 tmpreg = SPIx->CR1;
mbed_official 125:23cc3068a9e4 291 /* Clear BIDIMode, BIDIOE, RxONLY, SSM, SSI, LSBFirst, BR, MSTR, CPOL and CPHA bits */
mbed_official 125:23cc3068a9e4 292 tmpreg &= CR1_CLEAR_MASK;
mbed_official 125:23cc3068a9e4 293 /* Configure SPIx: direction, NSS management, first transmitted bit, BaudRate prescaler
mbed_official 125:23cc3068a9e4 294 master/slave mode, CPOL and CPHA */
mbed_official 125:23cc3068a9e4 295 /* Set BIDImode, BIDIOE and RxONLY bits according to SPI_Direction value */
mbed_official 125:23cc3068a9e4 296 /* Set SSM, SSI and MSTR bits according to SPI_Mode and SPI_NSS values */
mbed_official 125:23cc3068a9e4 297 /* Set LSBFirst bit according to SPI_FirstBit value */
mbed_official 125:23cc3068a9e4 298 /* Set BR bits according to SPI_BaudRatePrescaler value */
mbed_official 125:23cc3068a9e4 299 /* Set CPOL bit according to SPI_CPOL value */
mbed_official 125:23cc3068a9e4 300 /* Set CPHA bit according to SPI_CPHA value */
mbed_official 125:23cc3068a9e4 301 tmpreg |= (uint16_t)((uint16_t)(SPI_InitStruct->SPI_Direction | SPI_InitStruct->SPI_Mode) |
mbed_official 125:23cc3068a9e4 302 (uint16_t)((uint16_t)(SPI_InitStruct->SPI_CPOL | SPI_InitStruct->SPI_CPHA) |
mbed_official 125:23cc3068a9e4 303 (uint16_t)((uint16_t)(SPI_InitStruct->SPI_NSS | SPI_InitStruct->SPI_BaudRatePrescaler) |
mbed_official 125:23cc3068a9e4 304 SPI_InitStruct->SPI_FirstBit)));
mbed_official 125:23cc3068a9e4 305 /* Write to SPIx CR1 */
mbed_official 125:23cc3068a9e4 306 SPIx->CR1 = tmpreg;
mbed_official 125:23cc3068a9e4 307 /*-------------------------Data Size Configuration -----------------------*/
mbed_official 125:23cc3068a9e4 308 /* Get the SPIx CR2 value */
mbed_official 125:23cc3068a9e4 309 tmpreg = SPIx->CR2;
mbed_official 125:23cc3068a9e4 310 /* Clear DS[3:0] bits */
mbed_official 125:23cc3068a9e4 311 tmpreg &= (uint16_t)~SPI_CR2_DS;
mbed_official 125:23cc3068a9e4 312 /* Configure SPIx: Data Size */
mbed_official 125:23cc3068a9e4 313 tmpreg |= (uint16_t)(SPI_InitStruct->SPI_DataSize);
mbed_official 125:23cc3068a9e4 314 /* Write to SPIx CR2 */
mbed_official 125:23cc3068a9e4 315 SPIx->CR2 = tmpreg;
mbed_official 125:23cc3068a9e4 316 }
mbed_official 125:23cc3068a9e4 317 /* Configuring the SPI in slave mode */
mbed_official 125:23cc3068a9e4 318 else
mbed_official 125:23cc3068a9e4 319 {
mbed_official 125:23cc3068a9e4 320 /*---------------------------- Data size Configuration -----------------------*/
mbed_official 125:23cc3068a9e4 321 /* Get the SPIx CR2 value */
mbed_official 125:23cc3068a9e4 322 tmpreg = SPIx->CR2;
mbed_official 125:23cc3068a9e4 323 /* Clear DS[3:0] bits */
mbed_official 125:23cc3068a9e4 324 tmpreg &= (uint16_t)~SPI_CR2_DS;
mbed_official 125:23cc3068a9e4 325 /* Configure SPIx: Data Size */
mbed_official 125:23cc3068a9e4 326 tmpreg |= (uint16_t)(SPI_InitStruct->SPI_DataSize);
mbed_official 125:23cc3068a9e4 327 /* Write to SPIx CR2 */
mbed_official 125:23cc3068a9e4 328 SPIx->CR2 = tmpreg;
mbed_official 125:23cc3068a9e4 329 /*---------------------------- SPIx CR1 Configuration ------------------------*/
mbed_official 125:23cc3068a9e4 330 /* Get the SPIx CR1 value */
mbed_official 125:23cc3068a9e4 331 tmpreg = SPIx->CR1;
mbed_official 125:23cc3068a9e4 332 /* Clear BIDIMode, BIDIOE, RxONLY, SSM, SSI, LSBFirst, BR, MSTR, CPOL and CPHA bits */
mbed_official 125:23cc3068a9e4 333 tmpreg &= CR1_CLEAR_MASK;
mbed_official 125:23cc3068a9e4 334 /* Configure SPIx: direction, NSS management, first transmitted bit, BaudRate prescaler
mbed_official 125:23cc3068a9e4 335 master/salve mode, CPOL and CPHA */
mbed_official 125:23cc3068a9e4 336 /* Set BIDImode, BIDIOE and RxONLY bits according to SPI_Direction value */
mbed_official 125:23cc3068a9e4 337 /* Set SSM, SSI and MSTR bits according to SPI_Mode and SPI_NSS values */
mbed_official 125:23cc3068a9e4 338 /* Set LSBFirst bit according to SPI_FirstBit value */
mbed_official 125:23cc3068a9e4 339 /* Set BR bits according to SPI_BaudRatePrescaler value */
mbed_official 125:23cc3068a9e4 340 /* Set CPOL bit according to SPI_CPOL value */
mbed_official 125:23cc3068a9e4 341 /* Set CPHA bit according to SPI_CPHA value */
mbed_official 125:23cc3068a9e4 342 tmpreg |= (uint16_t)((uint16_t)(SPI_InitStruct->SPI_Direction | SPI_InitStruct->SPI_Mode) |
mbed_official 125:23cc3068a9e4 343 (uint16_t)((uint16_t)(SPI_InitStruct->SPI_CPOL | SPI_InitStruct->SPI_CPHA) |
mbed_official 125:23cc3068a9e4 344 (uint16_t)((uint16_t)(SPI_InitStruct->SPI_NSS | SPI_InitStruct->SPI_BaudRatePrescaler) |
mbed_official 125:23cc3068a9e4 345 SPI_InitStruct->SPI_FirstBit)));
mbed_official 125:23cc3068a9e4 346
mbed_official 125:23cc3068a9e4 347 /* Write to SPIx CR1 */
mbed_official 125:23cc3068a9e4 348 SPIx->CR1 = tmpreg;
mbed_official 125:23cc3068a9e4 349 }
mbed_official 125:23cc3068a9e4 350
mbed_official 125:23cc3068a9e4 351 /* Activate the SPI mode (Reset I2SMOD bit in I2SCFGR register) */
mbed_official 125:23cc3068a9e4 352 SPIx->I2SCFGR &= (uint16_t)~((uint16_t)SPI_I2SCFGR_I2SMOD);
mbed_official 125:23cc3068a9e4 353
mbed_official 125:23cc3068a9e4 354 /*---------------------------- SPIx CRCPOLY Configuration --------------------*/
mbed_official 125:23cc3068a9e4 355 /* Write to SPIx CRCPOLY */
mbed_official 125:23cc3068a9e4 356 SPIx->CRCPR = SPI_InitStruct->SPI_CRCPolynomial;
mbed_official 125:23cc3068a9e4 357 }
mbed_official 125:23cc3068a9e4 358
mbed_official 125:23cc3068a9e4 359 /**
mbed_official 125:23cc3068a9e4 360 * @brief Fills each I2S_InitStruct member with its default value.
mbed_official 125:23cc3068a9e4 361 * @param I2S_InitStruct : pointer to a I2S_InitTypeDef structure which will be initialized.
mbed_official 125:23cc3068a9e4 362 * @retval None
mbed_official 125:23cc3068a9e4 363 */
mbed_official 125:23cc3068a9e4 364 void I2S_StructInit(I2S_InitTypeDef* I2S_InitStruct)
mbed_official 125:23cc3068a9e4 365 {
mbed_official 125:23cc3068a9e4 366 /*--------------- Reset I2S init structure parameters values -----------------*/
mbed_official 125:23cc3068a9e4 367 /* Initialize the I2S_Mode member */
mbed_official 125:23cc3068a9e4 368 I2S_InitStruct->I2S_Mode = I2S_Mode_SlaveTx;
mbed_official 125:23cc3068a9e4 369
mbed_official 125:23cc3068a9e4 370 /* Initialize the I2S_Standard member */
mbed_official 125:23cc3068a9e4 371 I2S_InitStruct->I2S_Standard = I2S_Standard_Phillips;
mbed_official 125:23cc3068a9e4 372
mbed_official 125:23cc3068a9e4 373 /* Initialize the I2S_DataFormat member */
mbed_official 125:23cc3068a9e4 374 I2S_InitStruct->I2S_DataFormat = I2S_DataFormat_16b;
mbed_official 125:23cc3068a9e4 375
mbed_official 125:23cc3068a9e4 376 /* Initialize the I2S_MCLKOutput member */
mbed_official 125:23cc3068a9e4 377 I2S_InitStruct->I2S_MCLKOutput = I2S_MCLKOutput_Disable;
mbed_official 125:23cc3068a9e4 378
mbed_official 125:23cc3068a9e4 379 /* Initialize the I2S_AudioFreq member */
mbed_official 125:23cc3068a9e4 380 I2S_InitStruct->I2S_AudioFreq = I2S_AudioFreq_Default;
mbed_official 125:23cc3068a9e4 381
mbed_official 125:23cc3068a9e4 382 /* Initialize the I2S_CPOL member */
mbed_official 125:23cc3068a9e4 383 I2S_InitStruct->I2S_CPOL = I2S_CPOL_Low;
mbed_official 125:23cc3068a9e4 384 }
mbed_official 125:23cc3068a9e4 385
mbed_official 125:23cc3068a9e4 386 /**
mbed_official 125:23cc3068a9e4 387 * @brief Initializes the SPIx peripheral according to the specified
mbed_official 125:23cc3068a9e4 388 * parameters in the I2S_InitStruct.
mbed_official 125:23cc3068a9e4 389 * @param SPIx:To select the SPIx peripheral, where x can be: 2 or 3
mbed_official 125:23cc3068a9e4 390 * in I2S mode.
mbed_official 125:23cc3068a9e4 391 * @param I2S_InitStruct: pointer to an I2S_InitTypeDef structure that
mbed_official 125:23cc3068a9e4 392 * contains the configuration information for the specified SPI peripheral
mbed_official 125:23cc3068a9e4 393 * configured in I2S mode.
mbed_official 125:23cc3068a9e4 394 * @note
mbed_official 125:23cc3068a9e4 395 * The function calculates the optimal prescaler needed to obtain the most
mbed_official 125:23cc3068a9e4 396 * accurate audio frequency (depending on the I2S clock source, the PLL values
mbed_official 125:23cc3068a9e4 397 * and the product configuration). But in case the prescaler value is greater
mbed_official 125:23cc3068a9e4 398 * than 511, the default value (0x02) will be configured instead.
mbed_official 125:23cc3068a9e4 399 * @retval None
mbed_official 125:23cc3068a9e4 400 */
mbed_official 125:23cc3068a9e4 401 void I2S_Init(SPI_TypeDef* SPIx, I2S_InitTypeDef* I2S_InitStruct)
mbed_official 125:23cc3068a9e4 402 {
mbed_official 125:23cc3068a9e4 403 uint16_t tmpreg = 0, i2sdiv = 2, i2sodd = 0, packetlength = 1;
mbed_official 125:23cc3068a9e4 404 uint32_t tmp = 0;
mbed_official 125:23cc3068a9e4 405 RCC_ClocksTypeDef RCC_Clocks;
mbed_official 125:23cc3068a9e4 406 uint32_t sourceclock = 0;
mbed_official 125:23cc3068a9e4 407
mbed_official 125:23cc3068a9e4 408 /* Check the I2S parameters */
mbed_official 125:23cc3068a9e4 409 assert_param(IS_SPI_23_PERIPH(SPIx));
mbed_official 125:23cc3068a9e4 410 assert_param(IS_I2S_MODE(I2S_InitStruct->I2S_Mode));
mbed_official 125:23cc3068a9e4 411 assert_param(IS_I2S_STANDARD(I2S_InitStruct->I2S_Standard));
mbed_official 125:23cc3068a9e4 412 assert_param(IS_I2S_DATA_FORMAT(I2S_InitStruct->I2S_DataFormat));
mbed_official 125:23cc3068a9e4 413 assert_param(IS_I2S_MCLK_OUTPUT(I2S_InitStruct->I2S_MCLKOutput));
mbed_official 125:23cc3068a9e4 414 assert_param(IS_I2S_AUDIO_FREQ(I2S_InitStruct->I2S_AudioFreq));
mbed_official 125:23cc3068a9e4 415 assert_param(IS_I2S_CPOL(I2S_InitStruct->I2S_CPOL));
mbed_official 125:23cc3068a9e4 416
mbed_official 125:23cc3068a9e4 417 /*----------------------- SPIx I2SCFGR & I2SPR Configuration -----------------*/
mbed_official 125:23cc3068a9e4 418 /* Clear I2SMOD, I2SE, I2SCFG, PCMSYNC, I2SSTD, CKPOL, DATLEN and CHLEN bits */
mbed_official 125:23cc3068a9e4 419 SPIx->I2SCFGR &= I2SCFGR_CLEAR_MASK;
mbed_official 125:23cc3068a9e4 420 SPIx->I2SPR = 0x0002;
mbed_official 125:23cc3068a9e4 421
mbed_official 125:23cc3068a9e4 422 /* Get the I2SCFGR register value */
mbed_official 125:23cc3068a9e4 423 tmpreg = SPIx->I2SCFGR;
mbed_official 125:23cc3068a9e4 424
mbed_official 125:23cc3068a9e4 425 /* If the default value has to be written, reinitialize i2sdiv and i2sodd*/
mbed_official 125:23cc3068a9e4 426 if(I2S_InitStruct->I2S_AudioFreq == I2S_AudioFreq_Default)
mbed_official 125:23cc3068a9e4 427 {
mbed_official 125:23cc3068a9e4 428 i2sodd = (uint16_t)0;
mbed_official 125:23cc3068a9e4 429 i2sdiv = (uint16_t)2;
mbed_official 125:23cc3068a9e4 430 }
mbed_official 125:23cc3068a9e4 431 /* If the requested audio frequency is not the default, compute the prescaler */
mbed_official 125:23cc3068a9e4 432 else
mbed_official 125:23cc3068a9e4 433 {
mbed_official 125:23cc3068a9e4 434 /* Check the frame length (For the Prescaler computing) */
mbed_official 125:23cc3068a9e4 435 if(I2S_InitStruct->I2S_DataFormat == I2S_DataFormat_16b)
mbed_official 125:23cc3068a9e4 436 {
mbed_official 125:23cc3068a9e4 437 /* Packet length is 16 bits */
mbed_official 125:23cc3068a9e4 438 packetlength = 1;
mbed_official 125:23cc3068a9e4 439 }
mbed_official 125:23cc3068a9e4 440 else
mbed_official 125:23cc3068a9e4 441 {
mbed_official 125:23cc3068a9e4 442 /* Packet length is 32 bits */
mbed_official 125:23cc3068a9e4 443 packetlength = 2;
mbed_official 125:23cc3068a9e4 444 }
mbed_official 125:23cc3068a9e4 445
mbed_official 125:23cc3068a9e4 446 /* I2S Clock source is System clock: Get System Clock frequency */
mbed_official 125:23cc3068a9e4 447 RCC_GetClocksFreq(&RCC_Clocks);
mbed_official 125:23cc3068a9e4 448
mbed_official 125:23cc3068a9e4 449 /* Get the source clock value: based on System Clock value */
mbed_official 125:23cc3068a9e4 450 sourceclock = RCC_Clocks.SYSCLK_Frequency;
mbed_official 125:23cc3068a9e4 451
mbed_official 125:23cc3068a9e4 452 /* Compute the Real divider depending on the MCLK output state with a floating point */
mbed_official 125:23cc3068a9e4 453 if(I2S_InitStruct->I2S_MCLKOutput == I2S_MCLKOutput_Enable)
mbed_official 125:23cc3068a9e4 454 {
mbed_official 125:23cc3068a9e4 455 /* MCLK output is enabled */
mbed_official 125:23cc3068a9e4 456 tmp = (uint16_t)(((((sourceclock / 256) * 10) / I2S_InitStruct->I2S_AudioFreq)) + 5);
mbed_official 125:23cc3068a9e4 457 }
mbed_official 125:23cc3068a9e4 458 else
mbed_official 125:23cc3068a9e4 459 {
mbed_official 125:23cc3068a9e4 460 /* MCLK output is disabled */
mbed_official 125:23cc3068a9e4 461 tmp = (uint16_t)(((((sourceclock / (32 * packetlength)) *10 ) / I2S_InitStruct->I2S_AudioFreq)) + 5);
mbed_official 125:23cc3068a9e4 462 }
mbed_official 125:23cc3068a9e4 463
mbed_official 125:23cc3068a9e4 464 /* Remove the floating point */
mbed_official 125:23cc3068a9e4 465 tmp = tmp / 10;
mbed_official 125:23cc3068a9e4 466
mbed_official 125:23cc3068a9e4 467 /* Check the parity of the divider */
mbed_official 125:23cc3068a9e4 468 i2sodd = (uint16_t)(tmp & (uint16_t)0x0001);
mbed_official 125:23cc3068a9e4 469
mbed_official 125:23cc3068a9e4 470 /* Compute the i2sdiv prescaler */
mbed_official 125:23cc3068a9e4 471 i2sdiv = (uint16_t)((tmp - i2sodd) / 2);
mbed_official 125:23cc3068a9e4 472
mbed_official 125:23cc3068a9e4 473 /* Get the Mask for the Odd bit (SPI_I2SPR[8]) register */
mbed_official 125:23cc3068a9e4 474 i2sodd = (uint16_t) (i2sodd << 8);
mbed_official 125:23cc3068a9e4 475 }
mbed_official 125:23cc3068a9e4 476
mbed_official 125:23cc3068a9e4 477 /* Test if the divider is 1 or 0 or greater than 0xFF */
mbed_official 125:23cc3068a9e4 478 if ((i2sdiv < 2) || (i2sdiv > 0xFF))
mbed_official 125:23cc3068a9e4 479 {
mbed_official 125:23cc3068a9e4 480 /* Set the default values */
mbed_official 125:23cc3068a9e4 481 i2sdiv = 2;
mbed_official 125:23cc3068a9e4 482 i2sodd = 0;
mbed_official 125:23cc3068a9e4 483 }
mbed_official 125:23cc3068a9e4 484
mbed_official 125:23cc3068a9e4 485 /* Write to SPIx I2SPR register the computed value */
mbed_official 125:23cc3068a9e4 486 SPIx->I2SPR = (uint16_t)(i2sdiv | (uint16_t)(i2sodd | (uint16_t)I2S_InitStruct->I2S_MCLKOutput));
mbed_official 125:23cc3068a9e4 487
mbed_official 125:23cc3068a9e4 488 /* Configure the I2S with the SPI_InitStruct values */
mbed_official 125:23cc3068a9e4 489 tmpreg |= (uint16_t)((uint16_t)(SPI_I2SCFGR_I2SMOD | I2S_InitStruct->I2S_Mode) | \
mbed_official 125:23cc3068a9e4 490 (uint16_t)((uint16_t)((uint16_t)(I2S_InitStruct->I2S_Standard |I2S_InitStruct->I2S_DataFormat) |\
mbed_official 125:23cc3068a9e4 491 I2S_InitStruct->I2S_CPOL)));
mbed_official 125:23cc3068a9e4 492
mbed_official 125:23cc3068a9e4 493 /* Write to SPIx I2SCFGR */
mbed_official 125:23cc3068a9e4 494 SPIx->I2SCFGR = tmpreg;
mbed_official 125:23cc3068a9e4 495 }
mbed_official 125:23cc3068a9e4 496
mbed_official 125:23cc3068a9e4 497 /**
mbed_official 125:23cc3068a9e4 498 * @brief Enables or disables the specified SPI peripheral.
mbed_official 125:23cc3068a9e4 499 * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
mbed_official 125:23cc3068a9e4 500 * @param NewState: new state of the SPIx peripheral.
mbed_official 125:23cc3068a9e4 501 * This parameter can be: ENABLE or DISABLE.
mbed_official 125:23cc3068a9e4 502 * @retval None
mbed_official 125:23cc3068a9e4 503 */
mbed_official 125:23cc3068a9e4 504 void SPI_Cmd(SPI_TypeDef* SPIx, FunctionalState NewState)
mbed_official 125:23cc3068a9e4 505 {
mbed_official 125:23cc3068a9e4 506 /* Check the parameters */
mbed_official 125:23cc3068a9e4 507 assert_param(IS_SPI_ALL_PERIPH(SPIx));
mbed_official 125:23cc3068a9e4 508 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 125:23cc3068a9e4 509
mbed_official 125:23cc3068a9e4 510 if (NewState != DISABLE)
mbed_official 125:23cc3068a9e4 511 {
mbed_official 125:23cc3068a9e4 512 /* Enable the selected SPI peripheral */
mbed_official 125:23cc3068a9e4 513 SPIx->CR1 |= SPI_CR1_SPE;
mbed_official 125:23cc3068a9e4 514 }
mbed_official 125:23cc3068a9e4 515 else
mbed_official 125:23cc3068a9e4 516 {
mbed_official 125:23cc3068a9e4 517 /* Disable the selected SPI peripheral */
mbed_official 125:23cc3068a9e4 518 SPIx->CR1 &= (uint16_t)~((uint16_t)SPI_CR1_SPE);
mbed_official 125:23cc3068a9e4 519 }
mbed_official 125:23cc3068a9e4 520 }
mbed_official 125:23cc3068a9e4 521
mbed_official 125:23cc3068a9e4 522 /**
mbed_official 125:23cc3068a9e4 523 * @brief Enables or disables the TI Mode.
mbed_official 125:23cc3068a9e4 524 * @note This function can be called only after the SPI_Init() function has
mbed_official 125:23cc3068a9e4 525 * been called.
mbed_official 125:23cc3068a9e4 526 * @note When TI mode is selected, the control bits SSM, SSI, CPOL and CPHA
mbed_official 125:23cc3068a9e4 527 * are not taken into consideration and are configured by hardware
mbed_official 125:23cc3068a9e4 528 * respectively to the TI mode requirements.
mbed_official 125:23cc3068a9e4 529 * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
mbed_official 125:23cc3068a9e4 530 * @param NewState: new state of the selected SPI TI communication mode.
mbed_official 125:23cc3068a9e4 531 * This parameter can be: ENABLE or DISABLE.
mbed_official 125:23cc3068a9e4 532 * @retval None
mbed_official 125:23cc3068a9e4 533 */
mbed_official 125:23cc3068a9e4 534 void SPI_TIModeCmd(SPI_TypeDef* SPIx, FunctionalState NewState)
mbed_official 125:23cc3068a9e4 535 {
mbed_official 125:23cc3068a9e4 536 /* Check the parameters */
mbed_official 125:23cc3068a9e4 537 assert_param(IS_SPI_ALL_PERIPH(SPIx));
mbed_official 125:23cc3068a9e4 538 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 125:23cc3068a9e4 539
mbed_official 125:23cc3068a9e4 540 if (NewState != DISABLE)
mbed_official 125:23cc3068a9e4 541 {
mbed_official 125:23cc3068a9e4 542 /* Enable the TI mode for the selected SPI peripheral */
mbed_official 125:23cc3068a9e4 543 SPIx->CR2 |= SPI_CR2_FRF;
mbed_official 125:23cc3068a9e4 544 }
mbed_official 125:23cc3068a9e4 545 else
mbed_official 125:23cc3068a9e4 546 {
mbed_official 125:23cc3068a9e4 547 /* Disable the TI mode for the selected SPI peripheral */
mbed_official 125:23cc3068a9e4 548 SPIx->CR2 &= (uint16_t)~((uint16_t)SPI_CR2_FRF);
mbed_official 125:23cc3068a9e4 549 }
mbed_official 125:23cc3068a9e4 550 }
mbed_official 125:23cc3068a9e4 551
mbed_official 125:23cc3068a9e4 552 /**
mbed_official 125:23cc3068a9e4 553 * @brief Enables or disables the specified SPI peripheral (in I2S mode).
mbed_official 125:23cc3068a9e4 554 * @param SPIx:To select the SPIx peripheral, where x can be: 2 or 3 in
mbed_official 125:23cc3068a9e4 555 * I2S mode or I2Sxext for I2S full duplex mode.
mbed_official 125:23cc3068a9e4 556 * @param NewState: new state of the SPIx peripheral.
mbed_official 125:23cc3068a9e4 557 * This parameter can be: ENABLE or DISABLE.
mbed_official 125:23cc3068a9e4 558 * @retval None
mbed_official 125:23cc3068a9e4 559 */
mbed_official 125:23cc3068a9e4 560 void I2S_Cmd(SPI_TypeDef* SPIx, FunctionalState NewState)
mbed_official 125:23cc3068a9e4 561 {
mbed_official 125:23cc3068a9e4 562 /* Check the parameters */
mbed_official 125:23cc3068a9e4 563 assert_param(IS_SPI_23_PERIPH_EXT(SPIx));
mbed_official 125:23cc3068a9e4 564 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 125:23cc3068a9e4 565 if (NewState != DISABLE)
mbed_official 125:23cc3068a9e4 566 {
mbed_official 125:23cc3068a9e4 567 /* Enable the selected SPI peripheral in I2S mode */
mbed_official 125:23cc3068a9e4 568 SPIx->I2SCFGR |= SPI_I2SCFGR_I2SE;
mbed_official 125:23cc3068a9e4 569 }
mbed_official 125:23cc3068a9e4 570 else
mbed_official 125:23cc3068a9e4 571 {
mbed_official 125:23cc3068a9e4 572 /* Disable the selected SPI peripheral in I2S mode */
mbed_official 125:23cc3068a9e4 573 SPIx->I2SCFGR &= (uint16_t)~((uint16_t)SPI_I2SCFGR_I2SE);
mbed_official 125:23cc3068a9e4 574 }
mbed_official 125:23cc3068a9e4 575 }
mbed_official 125:23cc3068a9e4 576
mbed_official 125:23cc3068a9e4 577 /**
mbed_official 125:23cc3068a9e4 578 * @brief Configures the data size for the selected SPI.
mbed_official 125:23cc3068a9e4 579 * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
mbed_official 125:23cc3068a9e4 580 * @param SPI_DataSize: specifies the SPI data size.
mbed_official 125:23cc3068a9e4 581 * For the SPIx peripheral this parameter can be one of the following values:
mbed_official 125:23cc3068a9e4 582 * @arg SPI_DataSize_4b: Set data size to 4 bits
mbed_official 125:23cc3068a9e4 583 * @arg SPI_DataSize_5b: Set data size to 5 bits
mbed_official 125:23cc3068a9e4 584 * @arg SPI_DataSize_6b: Set data size to 6 bits
mbed_official 125:23cc3068a9e4 585 * @arg SPI_DataSize_7b: Set data size to 7 bits
mbed_official 125:23cc3068a9e4 586 * @arg SPI_DataSize_8b: Set data size to 8 bits
mbed_official 125:23cc3068a9e4 587 * @arg SPI_DataSize_9b: Set data size to 9 bits
mbed_official 125:23cc3068a9e4 588 * @arg SPI_DataSize_10b: Set data size to 10 bits
mbed_official 125:23cc3068a9e4 589 * @arg SPI_DataSize_11b: Set data size to 11 bits
mbed_official 125:23cc3068a9e4 590 * @arg SPI_DataSize_12b: Set data size to 12 bits
mbed_official 125:23cc3068a9e4 591 * @arg SPI_DataSize_13b: Set data size to 13 bits
mbed_official 125:23cc3068a9e4 592 * @arg SPI_DataSize_14b: Set data size to 14 bits
mbed_official 125:23cc3068a9e4 593 * @arg SPI_DataSize_15b: Set data size to 15 bits
mbed_official 125:23cc3068a9e4 594 * @arg SPI_DataSize_16b: Set data size to 16 bits
mbed_official 125:23cc3068a9e4 595 * @retval None
mbed_official 125:23cc3068a9e4 596 */
mbed_official 125:23cc3068a9e4 597 void SPI_DataSizeConfig(SPI_TypeDef* SPIx, uint16_t SPI_DataSize)
mbed_official 125:23cc3068a9e4 598 {
mbed_official 125:23cc3068a9e4 599 uint16_t tmpreg = 0;
mbed_official 125:23cc3068a9e4 600
mbed_official 125:23cc3068a9e4 601 /* Check the parameters */
mbed_official 125:23cc3068a9e4 602 assert_param(IS_SPI_ALL_PERIPH(SPIx));
mbed_official 125:23cc3068a9e4 603 assert_param(IS_SPI_DATA_SIZE(SPI_DataSize));
mbed_official 125:23cc3068a9e4 604 /* Read the CR2 register */
mbed_official 125:23cc3068a9e4 605 tmpreg = SPIx->CR2;
mbed_official 125:23cc3068a9e4 606 /* Clear DS[3:0] bits */
mbed_official 125:23cc3068a9e4 607 tmpreg &= (uint16_t)~SPI_CR2_DS;
mbed_official 125:23cc3068a9e4 608 /* Set new DS[3:0] bits value */
mbed_official 125:23cc3068a9e4 609 tmpreg |= SPI_DataSize;
mbed_official 125:23cc3068a9e4 610 SPIx->CR2 = tmpreg;
mbed_official 125:23cc3068a9e4 611 }
mbed_official 125:23cc3068a9e4 612
mbed_official 125:23cc3068a9e4 613 /**
mbed_official 125:23cc3068a9e4 614 * @brief Configures the FIFO reception threshold for the selected SPI.
mbed_official 125:23cc3068a9e4 615 * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
mbed_official 125:23cc3068a9e4 616 * @param SPI_RxFIFOThreshold: specifies the FIFO reception threshold.
mbed_official 125:23cc3068a9e4 617 * This parameter can be one of the following values:
mbed_official 125:23cc3068a9e4 618 * @arg SPI_RxFIFOThreshold_HF: RXNE event is generated if the FIFO
mbed_official 125:23cc3068a9e4 619 * level is greater or equal to 1/2.
mbed_official 125:23cc3068a9e4 620 * @arg SPI_RxFIFOThreshold_QF: RXNE event is generated if the FIFO
mbed_official 125:23cc3068a9e4 621 * level is greater or equal to 1/4.
mbed_official 125:23cc3068a9e4 622 * @retval None
mbed_official 125:23cc3068a9e4 623 */
mbed_official 125:23cc3068a9e4 624 void SPI_RxFIFOThresholdConfig(SPI_TypeDef* SPIx, uint16_t SPI_RxFIFOThreshold)
mbed_official 125:23cc3068a9e4 625 {
mbed_official 125:23cc3068a9e4 626 /* Check the parameters */
mbed_official 125:23cc3068a9e4 627 assert_param(IS_SPI_ALL_PERIPH(SPIx));
mbed_official 125:23cc3068a9e4 628 assert_param(IS_SPI_RX_FIFO_THRESHOLD(SPI_RxFIFOThreshold));
mbed_official 125:23cc3068a9e4 629
mbed_official 125:23cc3068a9e4 630 /* Clear FRXTH bit */
mbed_official 125:23cc3068a9e4 631 SPIx->CR2 &= (uint16_t)~((uint16_t)SPI_CR2_FRXTH);
mbed_official 125:23cc3068a9e4 632
mbed_official 125:23cc3068a9e4 633 /* Set new FRXTH bit value */
mbed_official 125:23cc3068a9e4 634 SPIx->CR2 |= SPI_RxFIFOThreshold;
mbed_official 125:23cc3068a9e4 635 }
mbed_official 125:23cc3068a9e4 636
mbed_official 125:23cc3068a9e4 637 /**
mbed_official 125:23cc3068a9e4 638 * @brief Selects the data transfer direction in bidirectional mode for the specified SPI.
mbed_official 125:23cc3068a9e4 639 * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
mbed_official 125:23cc3068a9e4 640 * @param SPI_Direction: specifies the data transfer direction in bidirectional mode.
mbed_official 125:23cc3068a9e4 641 * This parameter can be one of the following values:
mbed_official 125:23cc3068a9e4 642 * @arg SPI_Direction_Tx: Selects Tx transmission direction
mbed_official 125:23cc3068a9e4 643 * @arg SPI_Direction_Rx: Selects Rx receive direction
mbed_official 125:23cc3068a9e4 644 * @retval None
mbed_official 125:23cc3068a9e4 645 */
mbed_official 125:23cc3068a9e4 646 void SPI_BiDirectionalLineConfig(SPI_TypeDef* SPIx, uint16_t SPI_Direction)
mbed_official 125:23cc3068a9e4 647 {
mbed_official 125:23cc3068a9e4 648 /* Check the parameters */
mbed_official 125:23cc3068a9e4 649 assert_param(IS_SPI_ALL_PERIPH(SPIx));
mbed_official 125:23cc3068a9e4 650 assert_param(IS_SPI_DIRECTION(SPI_Direction));
mbed_official 125:23cc3068a9e4 651 if (SPI_Direction == SPI_Direction_Tx)
mbed_official 125:23cc3068a9e4 652 {
mbed_official 125:23cc3068a9e4 653 /* Set the Tx only mode */
mbed_official 125:23cc3068a9e4 654 SPIx->CR1 |= SPI_Direction_Tx;
mbed_official 125:23cc3068a9e4 655 }
mbed_official 125:23cc3068a9e4 656 else
mbed_official 125:23cc3068a9e4 657 {
mbed_official 125:23cc3068a9e4 658 /* Set the Rx only mode */
mbed_official 125:23cc3068a9e4 659 SPIx->CR1 &= SPI_Direction_Rx;
mbed_official 125:23cc3068a9e4 660 }
mbed_official 125:23cc3068a9e4 661 }
mbed_official 125:23cc3068a9e4 662
mbed_official 125:23cc3068a9e4 663 /**
mbed_official 125:23cc3068a9e4 664 * @brief Configures internally by software the NSS pin for the selected SPI.
mbed_official 125:23cc3068a9e4 665 * @note This function can be called only after the SPI_Init() function has
mbed_official 125:23cc3068a9e4 666 * been called.
mbed_official 125:23cc3068a9e4 667 * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
mbed_official 125:23cc3068a9e4 668 * @param SPI_NSSInternalSoft: specifies the SPI NSS internal state.
mbed_official 125:23cc3068a9e4 669 * This parameter can be one of the following values:
mbed_official 125:23cc3068a9e4 670 * @arg SPI_NSSInternalSoft_Set: Set NSS pin internally
mbed_official 125:23cc3068a9e4 671 * @arg SPI_NSSInternalSoft_Reset: Reset NSS pin internally
mbed_official 125:23cc3068a9e4 672 * @retval None
mbed_official 125:23cc3068a9e4 673 */
mbed_official 125:23cc3068a9e4 674 void SPI_NSSInternalSoftwareConfig(SPI_TypeDef* SPIx, uint16_t SPI_NSSInternalSoft)
mbed_official 125:23cc3068a9e4 675 {
mbed_official 125:23cc3068a9e4 676 /* Check the parameters */
mbed_official 125:23cc3068a9e4 677 assert_param(IS_SPI_ALL_PERIPH(SPIx));
mbed_official 125:23cc3068a9e4 678 assert_param(IS_SPI_NSS_INTERNAL(SPI_NSSInternalSoft));
mbed_official 125:23cc3068a9e4 679
mbed_official 125:23cc3068a9e4 680 if (SPI_NSSInternalSoft != SPI_NSSInternalSoft_Reset)
mbed_official 125:23cc3068a9e4 681 {
mbed_official 125:23cc3068a9e4 682 /* Set NSS pin internally by software */
mbed_official 125:23cc3068a9e4 683 SPIx->CR1 |= SPI_NSSInternalSoft_Set;
mbed_official 125:23cc3068a9e4 684 }
mbed_official 125:23cc3068a9e4 685 else
mbed_official 125:23cc3068a9e4 686 {
mbed_official 125:23cc3068a9e4 687 /* Reset NSS pin internally by software */
mbed_official 125:23cc3068a9e4 688 SPIx->CR1 &= SPI_NSSInternalSoft_Reset;
mbed_official 125:23cc3068a9e4 689 }
mbed_official 125:23cc3068a9e4 690 }
mbed_official 125:23cc3068a9e4 691
mbed_official 125:23cc3068a9e4 692 /**
mbed_official 125:23cc3068a9e4 693 * @brief Configures the full duplex mode for the I2Sx peripheral using its
mbed_official 125:23cc3068a9e4 694 * extension I2Sxext according to the specified parameters in the
mbed_official 125:23cc3068a9e4 695 * I2S_InitStruct.
mbed_official 125:23cc3068a9e4 696 * @param I2Sxext: where x can be 2 or 3 to select the I2S peripheral extension block.
mbed_official 125:23cc3068a9e4 697 * @param I2S_InitStruct: pointer to an I2S_InitTypeDef structure that
mbed_official 125:23cc3068a9e4 698 * contains the configuration information for the specified I2S peripheral
mbed_official 125:23cc3068a9e4 699 * extension.
mbed_official 125:23cc3068a9e4 700 *
mbed_official 125:23cc3068a9e4 701 * @note The structure pointed by I2S_InitStruct parameter should be the same
mbed_official 125:23cc3068a9e4 702 * used for the master I2S peripheral. In this case, if the master is
mbed_official 125:23cc3068a9e4 703 * configured as transmitter, the slave will be receiver and vice versa.
mbed_official 125:23cc3068a9e4 704 * Or you can force a different mode by modifying the field I2S_Mode to the
mbed_official 125:23cc3068a9e4 705 * value I2S_SlaveRx or I2S_SlaveTx indepedently of the master configuration.
mbed_official 125:23cc3068a9e4 706 *
mbed_official 125:23cc3068a9e4 707 * @note The I2S full duplex extension can be configured in slave mode only.
mbed_official 125:23cc3068a9e4 708 *
mbed_official 125:23cc3068a9e4 709 * @retval None
mbed_official 125:23cc3068a9e4 710 */
mbed_official 125:23cc3068a9e4 711 void I2S_FullDuplexConfig(SPI_TypeDef* I2Sxext, I2S_InitTypeDef* I2S_InitStruct)
mbed_official 125:23cc3068a9e4 712 {
mbed_official 125:23cc3068a9e4 713 uint16_t tmpreg = 0, tmp = 0;
mbed_official 125:23cc3068a9e4 714
mbed_official 125:23cc3068a9e4 715 /* Check the I2S parameters */
mbed_official 125:23cc3068a9e4 716 assert_param(IS_I2S_EXT_PERIPH(I2Sxext));
mbed_official 125:23cc3068a9e4 717 assert_param(IS_I2S_MODE(I2S_InitStruct->I2S_Mode));
mbed_official 125:23cc3068a9e4 718 assert_param(IS_I2S_STANDARD(I2S_InitStruct->I2S_Standard));
mbed_official 125:23cc3068a9e4 719 assert_param(IS_I2S_DATA_FORMAT(I2S_InitStruct->I2S_DataFormat));
mbed_official 125:23cc3068a9e4 720 assert_param(IS_I2S_CPOL(I2S_InitStruct->I2S_CPOL));
mbed_official 125:23cc3068a9e4 721
mbed_official 125:23cc3068a9e4 722 /*----------------------- SPIx I2SCFGR & I2SPR Configuration -----------------*/
mbed_official 125:23cc3068a9e4 723 /* Clear I2SMOD, I2SE, I2SCFG, PCMSYNC, I2SSTD, CKPOL, DATLEN and CHLEN bits */
mbed_official 125:23cc3068a9e4 724 I2Sxext->I2SCFGR &= I2SCFGR_CLEAR_MASK;
mbed_official 125:23cc3068a9e4 725 I2Sxext->I2SPR = 0x0002;
mbed_official 125:23cc3068a9e4 726
mbed_official 125:23cc3068a9e4 727 /* Get the I2SCFGR register value */
mbed_official 125:23cc3068a9e4 728 tmpreg = I2Sxext->I2SCFGR;
mbed_official 125:23cc3068a9e4 729
mbed_official 125:23cc3068a9e4 730 /* Get the mode to be configured for the extended I2S */
mbed_official 125:23cc3068a9e4 731 if ((I2S_InitStruct->I2S_Mode == I2S_Mode_MasterTx) || (I2S_InitStruct->I2S_Mode == I2S_Mode_SlaveTx))
mbed_official 125:23cc3068a9e4 732 {
mbed_official 125:23cc3068a9e4 733 tmp = I2S_Mode_SlaveRx;
mbed_official 125:23cc3068a9e4 734 }
mbed_official 125:23cc3068a9e4 735 else
mbed_official 125:23cc3068a9e4 736 {
mbed_official 125:23cc3068a9e4 737 if ((I2S_InitStruct->I2S_Mode == I2S_Mode_MasterRx) || (I2S_InitStruct->I2S_Mode == I2S_Mode_SlaveRx))
mbed_official 125:23cc3068a9e4 738 {
mbed_official 125:23cc3068a9e4 739 tmp = I2S_Mode_SlaveTx;
mbed_official 125:23cc3068a9e4 740 }
mbed_official 125:23cc3068a9e4 741 }
mbed_official 125:23cc3068a9e4 742
mbed_official 125:23cc3068a9e4 743
mbed_official 125:23cc3068a9e4 744 /* Configure the I2S with the SPI_InitStruct values */
mbed_official 125:23cc3068a9e4 745 tmpreg |= (uint16_t)((uint16_t)SPI_I2SCFGR_I2SMOD | (uint16_t)(tmp | \
mbed_official 125:23cc3068a9e4 746 (uint16_t)(I2S_InitStruct->I2S_Standard | (uint16_t)(I2S_InitStruct->I2S_DataFormat | \
mbed_official 125:23cc3068a9e4 747 (uint16_t)I2S_InitStruct->I2S_CPOL))));
mbed_official 125:23cc3068a9e4 748
mbed_official 125:23cc3068a9e4 749 /* Write to SPIx I2SCFGR */
mbed_official 125:23cc3068a9e4 750 I2Sxext->I2SCFGR = tmpreg;
mbed_official 125:23cc3068a9e4 751 }
mbed_official 125:23cc3068a9e4 752
mbed_official 125:23cc3068a9e4 753 /**
mbed_official 125:23cc3068a9e4 754 * @brief Enables or disables the SS output for the selected SPI.
mbed_official 125:23cc3068a9e4 755 * @note This function can be called only after the SPI_Init() function has
mbed_official 125:23cc3068a9e4 756 * been called and the NSS hardware management mode is selected.
mbed_official 125:23cc3068a9e4 757 * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
mbed_official 125:23cc3068a9e4 758 * @param NewState: new state of the SPIx SS output.
mbed_official 125:23cc3068a9e4 759 * This parameter can be: ENABLE or DISABLE.
mbed_official 125:23cc3068a9e4 760 * @retval None
mbed_official 125:23cc3068a9e4 761 */
mbed_official 125:23cc3068a9e4 762 void SPI_SSOutputCmd(SPI_TypeDef* SPIx, FunctionalState NewState)
mbed_official 125:23cc3068a9e4 763 {
mbed_official 125:23cc3068a9e4 764 /* Check the parameters */
mbed_official 125:23cc3068a9e4 765 assert_param(IS_SPI_ALL_PERIPH(SPIx));
mbed_official 125:23cc3068a9e4 766 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 125:23cc3068a9e4 767 if (NewState != DISABLE)
mbed_official 125:23cc3068a9e4 768 {
mbed_official 125:23cc3068a9e4 769 /* Enable the selected SPI SS output */
mbed_official 125:23cc3068a9e4 770 SPIx->CR2 |= (uint16_t)SPI_CR2_SSOE;
mbed_official 125:23cc3068a9e4 771 }
mbed_official 125:23cc3068a9e4 772 else
mbed_official 125:23cc3068a9e4 773 {
mbed_official 125:23cc3068a9e4 774 /* Disable the selected SPI SS output */
mbed_official 125:23cc3068a9e4 775 SPIx->CR2 &= (uint16_t)~((uint16_t)SPI_CR2_SSOE);
mbed_official 125:23cc3068a9e4 776 }
mbed_official 125:23cc3068a9e4 777 }
mbed_official 125:23cc3068a9e4 778
mbed_official 125:23cc3068a9e4 779 /**
mbed_official 125:23cc3068a9e4 780 * @brief Enables or disables the NSS pulse management mode.
mbed_official 125:23cc3068a9e4 781 * @note This function can be called only after the SPI_Init() function has
mbed_official 125:23cc3068a9e4 782 * been called.
mbed_official 125:23cc3068a9e4 783 * @note When TI mode is selected, the control bits NSSP is not taken into
mbed_official 125:23cc3068a9e4 784 * consideration and are configured by hardware respectively to the
mbed_official 125:23cc3068a9e4 785 * TI mode requirements.
mbed_official 125:23cc3068a9e4 786 * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
mbed_official 125:23cc3068a9e4 787 * @param NewState: new state of the NSS pulse management mode.
mbed_official 125:23cc3068a9e4 788 * This parameter can be: ENABLE or DISABLE.
mbed_official 125:23cc3068a9e4 789 * @retval None
mbed_official 125:23cc3068a9e4 790 */
mbed_official 125:23cc3068a9e4 791 void SPI_NSSPulseModeCmd(SPI_TypeDef* SPIx, FunctionalState NewState)
mbed_official 125:23cc3068a9e4 792 {
mbed_official 125:23cc3068a9e4 793 /* Check the parameters */
mbed_official 125:23cc3068a9e4 794 assert_param(IS_SPI_ALL_PERIPH(SPIx));
mbed_official 125:23cc3068a9e4 795 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 125:23cc3068a9e4 796
mbed_official 125:23cc3068a9e4 797 if (NewState != DISABLE)
mbed_official 125:23cc3068a9e4 798 {
mbed_official 125:23cc3068a9e4 799 /* Enable the NSS pulse management mode */
mbed_official 125:23cc3068a9e4 800 SPIx->CR2 |= SPI_CR2_NSSP;
mbed_official 125:23cc3068a9e4 801 }
mbed_official 125:23cc3068a9e4 802 else
mbed_official 125:23cc3068a9e4 803 {
mbed_official 125:23cc3068a9e4 804 /* Disable the NSS pulse management mode */
mbed_official 125:23cc3068a9e4 805 SPIx->CR2 &= (uint16_t)~((uint16_t)SPI_CR2_NSSP);
mbed_official 125:23cc3068a9e4 806 }
mbed_official 125:23cc3068a9e4 807 }
mbed_official 125:23cc3068a9e4 808
mbed_official 125:23cc3068a9e4 809 /**
mbed_official 125:23cc3068a9e4 810 * @}
mbed_official 125:23cc3068a9e4 811 */
mbed_official 125:23cc3068a9e4 812
mbed_official 125:23cc3068a9e4 813 /** @defgroup SPI_Group2 Data transfers functions
mbed_official 125:23cc3068a9e4 814 * @brief Data transfers functions
mbed_official 125:23cc3068a9e4 815 *
mbed_official 125:23cc3068a9e4 816 @verbatim
mbed_official 125:23cc3068a9e4 817 ===============================================================================
mbed_official 125:23cc3068a9e4 818 ##### Data transfers functions #####
mbed_official 125:23cc3068a9e4 819 ===============================================================================
mbed_official 125:23cc3068a9e4 820 [..] This section provides a set of functions allowing to manage the SPI or I2S
mbed_official 125:23cc3068a9e4 821 data transfers.
mbed_official 125:23cc3068a9e4 822 [..] In reception, data are received and then stored into an internal Rx buffer while
mbed_official 125:23cc3068a9e4 823 In transmission, data are first stored into an internal Tx buffer before being
mbed_official 125:23cc3068a9e4 824 transmitted.
mbed_official 125:23cc3068a9e4 825 [..] The read access of the SPI_DR register can be done using the SPI_I2S_ReceiveData()
mbed_official 125:23cc3068a9e4 826 function and returns the Rx buffered value. Whereas a write access to the SPI_DR
mbed_official 125:23cc3068a9e4 827 can be done using SPI_I2S_SendData() function and stores the written data into
mbed_official 125:23cc3068a9e4 828 Tx buffer.
mbed_official 125:23cc3068a9e4 829
mbed_official 125:23cc3068a9e4 830 @endverbatim
mbed_official 125:23cc3068a9e4 831 * @{
mbed_official 125:23cc3068a9e4 832 */
mbed_official 125:23cc3068a9e4 833
mbed_official 125:23cc3068a9e4 834 /**
mbed_official 125:23cc3068a9e4 835 * @brief Transmits a Data through the SPIx peripheral.
mbed_official 125:23cc3068a9e4 836 * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
mbed_official 125:23cc3068a9e4 837 * @param Data: Data to be transmitted.
mbed_official 125:23cc3068a9e4 838 * @retval None
mbed_official 125:23cc3068a9e4 839 */
mbed_official 125:23cc3068a9e4 840 void SPI_SendData8(SPI_TypeDef* SPIx, uint8_t Data)
mbed_official 125:23cc3068a9e4 841 {
mbed_official 125:23cc3068a9e4 842 uint32_t spixbase = 0x00;
mbed_official 125:23cc3068a9e4 843
mbed_official 125:23cc3068a9e4 844 /* Check the parameters */
mbed_official 125:23cc3068a9e4 845 assert_param(IS_SPI_ALL_PERIPH(SPIx));
mbed_official 125:23cc3068a9e4 846
mbed_official 125:23cc3068a9e4 847 spixbase = (uint32_t)SPIx;
mbed_official 125:23cc3068a9e4 848 spixbase += 0x0C;
mbed_official 125:23cc3068a9e4 849
mbed_official 125:23cc3068a9e4 850 *(__IO uint8_t *) spixbase = Data;
mbed_official 125:23cc3068a9e4 851 }
mbed_official 125:23cc3068a9e4 852
mbed_official 125:23cc3068a9e4 853 /**
mbed_official 125:23cc3068a9e4 854 * @brief Transmits a Data through the SPIx/I2Sx peripheral.
mbed_official 125:23cc3068a9e4 855 * @param SPIx: To select the SPIx/I2Sx peripheral, where x can be: 1, 2 or 3
mbed_official 125:23cc3068a9e4 856 * in SPI mode or 2 or 3 in I2S mode or I2Sxext for I2S full duplex mode.
mbed_official 125:23cc3068a9e4 857 * @param Data: Data to be transmitted.
mbed_official 125:23cc3068a9e4 858 * @retval None
mbed_official 125:23cc3068a9e4 859 */
mbed_official 125:23cc3068a9e4 860 void SPI_I2S_SendData16(SPI_TypeDef* SPIx, uint16_t Data)
mbed_official 125:23cc3068a9e4 861 {
mbed_official 125:23cc3068a9e4 862 /* Check the parameters */
mbed_official 125:23cc3068a9e4 863 assert_param(IS_SPI_ALL_PERIPH_EXT(SPIx));
mbed_official 125:23cc3068a9e4 864
mbed_official 125:23cc3068a9e4 865 SPIx->DR = (uint16_t)Data;
mbed_official 125:23cc3068a9e4 866 }
mbed_official 125:23cc3068a9e4 867
mbed_official 125:23cc3068a9e4 868 /**
mbed_official 125:23cc3068a9e4 869 * @brief Returns the most recent received data by the SPIx peripheral.
mbed_official 125:23cc3068a9e4 870 * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
mbed_official 125:23cc3068a9e4 871 * @retval The value of the received data.
mbed_official 125:23cc3068a9e4 872 */
mbed_official 125:23cc3068a9e4 873 uint8_t SPI_ReceiveData8(SPI_TypeDef* SPIx)
mbed_official 125:23cc3068a9e4 874 {
mbed_official 125:23cc3068a9e4 875 uint32_t spixbase = 0x00;
mbed_official 125:23cc3068a9e4 876
mbed_official 125:23cc3068a9e4 877 /* Check the parameters */
mbed_official 125:23cc3068a9e4 878 assert_param(IS_SPI_ALL_PERIPH_EXT(SPIx));
mbed_official 125:23cc3068a9e4 879
mbed_official 125:23cc3068a9e4 880 spixbase = (uint32_t)SPIx;
mbed_official 125:23cc3068a9e4 881 spixbase += 0x0C;
mbed_official 125:23cc3068a9e4 882
mbed_official 125:23cc3068a9e4 883 return *(__IO uint8_t *) spixbase;
mbed_official 125:23cc3068a9e4 884 }
mbed_official 125:23cc3068a9e4 885
mbed_official 125:23cc3068a9e4 886 /**
mbed_official 125:23cc3068a9e4 887 * @brief Returns the most recent received data by the SPIx peripheral.
mbed_official 125:23cc3068a9e4 888 * @param SPIx: To select the SPIx/I2Sx peripheral, where x can be: 1, 2 or 3
mbed_official 125:23cc3068a9e4 889 * in SPI mode or 2 or 3 in I2S mode or I2Sxext for I2S full duplex mode.
mbed_official 125:23cc3068a9e4 890 * @retval The value of the received data.
mbed_official 125:23cc3068a9e4 891 */
mbed_official 125:23cc3068a9e4 892 uint16_t SPI_I2S_ReceiveData16(SPI_TypeDef* SPIx)
mbed_official 125:23cc3068a9e4 893 {
mbed_official 125:23cc3068a9e4 894 /* Check the parameters */
mbed_official 125:23cc3068a9e4 895 assert_param(IS_SPI_ALL_PERIPH_EXT(SPIx));
mbed_official 125:23cc3068a9e4 896
mbed_official 125:23cc3068a9e4 897 return SPIx->DR;
mbed_official 125:23cc3068a9e4 898 }
mbed_official 125:23cc3068a9e4 899 /**
mbed_official 125:23cc3068a9e4 900 * @}
mbed_official 125:23cc3068a9e4 901 */
mbed_official 125:23cc3068a9e4 902
mbed_official 125:23cc3068a9e4 903 /** @defgroup SPI_Group3 Hardware CRC Calculation functions
mbed_official 125:23cc3068a9e4 904 * @brief Hardware CRC Calculation functions
mbed_official 125:23cc3068a9e4 905 *
mbed_official 125:23cc3068a9e4 906 @verbatim
mbed_official 125:23cc3068a9e4 907 ===============================================================================
mbed_official 125:23cc3068a9e4 908 ##### Hardware CRC Calculation functions #####
mbed_official 125:23cc3068a9e4 909 ===============================================================================
mbed_official 125:23cc3068a9e4 910 [..] This section provides a set of functions allowing to manage the SPI CRC hardware
mbed_official 125:23cc3068a9e4 911 calculation.
mbed_official 125:23cc3068a9e4 912 [..] SPI communication using CRC is possible through the following procedure:
mbed_official 125:23cc3068a9e4 913 (#) Program the Data direction, Polarity, Phase, First Data, Baud Rate Prescaler,
mbed_official 125:23cc3068a9e4 914 Slave Management, Peripheral Mode and CRC Polynomial values using the SPI_Init()
mbed_official 125:23cc3068a9e4 915 function.
mbed_official 125:23cc3068a9e4 916 (#) Enable the CRC calculation using the SPI_CalculateCRC() function.
mbed_official 125:23cc3068a9e4 917 (#) Enable the SPI using the SPI_Cmd() function
mbed_official 125:23cc3068a9e4 918 (#) Before writing the last data to the TX buffer, set the CRCNext bit using the
mbed_official 125:23cc3068a9e4 919 SPI_TransmitCRC() function to indicate that after transmission of the last
mbed_official 125:23cc3068a9e4 920 data, the CRC should be transmitted.
mbed_official 125:23cc3068a9e4 921 (#) After transmitting the last data, the SPI transmits the CRC. The SPI_CR1_CRCNEXT
mbed_official 125:23cc3068a9e4 922 bit is reset. The CRC is also received and compared against the SPI_RXCRCR
mbed_official 125:23cc3068a9e4 923 value.
mbed_official 125:23cc3068a9e4 924 If the value does not match, the SPI_FLAG_CRCERR flag is set and an interrupt
mbed_official 125:23cc3068a9e4 925 can be generated when the SPI_I2S_IT_ERR interrupt is enabled.
mbed_official 125:23cc3068a9e4 926 [..]
mbed_official 125:23cc3068a9e4 927 (@)
mbed_official 125:23cc3068a9e4 928 (+@) It is advised to don't read the calculate CRC values during the communication.
mbed_official 125:23cc3068a9e4 929 (+@) When the SPI is in slave mode, be careful to enable CRC calculation only
mbed_official 125:23cc3068a9e4 930 when the clock is stable, that is, when the clock is in the steady state.
mbed_official 125:23cc3068a9e4 931 If not, a wrong CRC calculation may be done. In fact, the CRC is sensitive
mbed_official 125:23cc3068a9e4 932 to the SCK slave input clock as soon as CRCEN is set, and this, whatever
mbed_official 125:23cc3068a9e4 933 the value of the SPE bit.
mbed_official 125:23cc3068a9e4 934 (+@) With high bitrate frequencies, be careful when transmitting the CRC.
mbed_official 125:23cc3068a9e4 935 As the number of used CPU cycles has to be as low as possible in the CRC
mbed_official 125:23cc3068a9e4 936 transfer phase, it is forbidden to call software functions in the CRC
mbed_official 125:23cc3068a9e4 937 transmission sequence to avoid errors in the last data and CRC reception.
mbed_official 125:23cc3068a9e4 938 In fact, CRCNEXT bit has to be written before the end of the transmission/reception
mbed_official 125:23cc3068a9e4 939 of the last data.
mbed_official 125:23cc3068a9e4 940 (+@) For high bit rate frequencies, it is advised to use the DMA mode to avoid the
mbed_official 125:23cc3068a9e4 941 degradation of the SPI speed performance due to CPU accesses impacting the
mbed_official 125:23cc3068a9e4 942 SPI bandwidth.
mbed_official 125:23cc3068a9e4 943 (+@) When the STM32F30x are configured as slaves and the NSS hardware mode is
mbed_official 125:23cc3068a9e4 944 used, the NSS pin needs to be kept low between the data phase and the CRC
mbed_official 125:23cc3068a9e4 945 phase.
mbed_official 125:23cc3068a9e4 946 (+@) When the SPI is configured in slave mode with the CRC feature enabled, CRC
mbed_official 125:23cc3068a9e4 947 calculation takes place even if a high level is applied on the NSS pin.
mbed_official 125:23cc3068a9e4 948 This may happen for example in case of a multislave environment where the
mbed_official 125:23cc3068a9e4 949 communication master addresses slaves alternately.
mbed_official 125:23cc3068a9e4 950 (+@) Between a slave deselection (high level on NSS) and a new slave selection
mbed_official 125:23cc3068a9e4 951 (low level on NSS), the CRC value should be cleared on both master and slave
mbed_official 125:23cc3068a9e4 952 sides in order to resynchronize the master and slave for their respective
mbed_official 125:23cc3068a9e4 953 CRC calculation.
mbed_official 125:23cc3068a9e4 954 [..]
mbed_official 125:23cc3068a9e4 955 (@) To clear the CRC, follow the procedure below:
mbed_official 125:23cc3068a9e4 956 (#@) Disable SPI using the SPI_Cmd() function.
mbed_official 125:23cc3068a9e4 957 (#@) Disable the CRC calculation using the SPI_CalculateCRC() function.
mbed_official 125:23cc3068a9e4 958 (#@) Enable the CRC calculation using the SPI_CalculateCRC() function.
mbed_official 125:23cc3068a9e4 959 (#@) Enable SPI using the SPI_Cmd() function.
mbed_official 125:23cc3068a9e4 960
mbed_official 125:23cc3068a9e4 961 @endverbatim
mbed_official 125:23cc3068a9e4 962 * @{
mbed_official 125:23cc3068a9e4 963 */
mbed_official 125:23cc3068a9e4 964
mbed_official 125:23cc3068a9e4 965 /**
mbed_official 125:23cc3068a9e4 966 * @brief Configures the CRC calculation length for the selected SPI.
mbed_official 125:23cc3068a9e4 967 * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
mbed_official 125:23cc3068a9e4 968 * @param SPI_CRCLength: specifies the SPI CRC calculation length.
mbed_official 125:23cc3068a9e4 969 * This parameter can be one of the following values:
mbed_official 125:23cc3068a9e4 970 * @arg SPI_CRCLength_8b: Set CRC Calculation to 8 bits
mbed_official 125:23cc3068a9e4 971 * @arg SPI_CRCLength_16b: Set CRC Calculation to 16 bits
mbed_official 125:23cc3068a9e4 972 * @retval None
mbed_official 125:23cc3068a9e4 973 */
mbed_official 125:23cc3068a9e4 974 void SPI_CRCLengthConfig(SPI_TypeDef* SPIx, uint16_t SPI_CRCLength)
mbed_official 125:23cc3068a9e4 975 {
mbed_official 125:23cc3068a9e4 976 /* Check the parameters */
mbed_official 125:23cc3068a9e4 977 assert_param(IS_SPI_ALL_PERIPH(SPIx));
mbed_official 125:23cc3068a9e4 978 assert_param(IS_SPI_CRC_LENGTH(SPI_CRCLength));
mbed_official 125:23cc3068a9e4 979
mbed_official 125:23cc3068a9e4 980 /* Clear CRCL bit */
mbed_official 125:23cc3068a9e4 981 SPIx->CR1 &= (uint16_t)~((uint16_t)SPI_CR1_CRCL);
mbed_official 125:23cc3068a9e4 982
mbed_official 125:23cc3068a9e4 983 /* Set new CRCL bit value */
mbed_official 125:23cc3068a9e4 984 SPIx->CR1 |= SPI_CRCLength;
mbed_official 125:23cc3068a9e4 985 }
mbed_official 125:23cc3068a9e4 986
mbed_official 125:23cc3068a9e4 987 /**
mbed_official 125:23cc3068a9e4 988 * @brief Enables or disables the CRC value calculation of the transferred bytes.
mbed_official 125:23cc3068a9e4 989 * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
mbed_official 125:23cc3068a9e4 990 * @param NewState: new state of the SPIx CRC value calculation.
mbed_official 125:23cc3068a9e4 991 * This parameter can be: ENABLE or DISABLE.
mbed_official 125:23cc3068a9e4 992 * @retval None
mbed_official 125:23cc3068a9e4 993 */
mbed_official 125:23cc3068a9e4 994 void SPI_CalculateCRC(SPI_TypeDef* SPIx, FunctionalState NewState)
mbed_official 125:23cc3068a9e4 995 {
mbed_official 125:23cc3068a9e4 996 /* Check the parameters */
mbed_official 125:23cc3068a9e4 997 assert_param(IS_SPI_ALL_PERIPH(SPIx));
mbed_official 125:23cc3068a9e4 998 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 125:23cc3068a9e4 999
mbed_official 125:23cc3068a9e4 1000 if (NewState != DISABLE)
mbed_official 125:23cc3068a9e4 1001 {
mbed_official 125:23cc3068a9e4 1002 /* Enable the selected SPI CRC calculation */
mbed_official 125:23cc3068a9e4 1003 SPIx->CR1 |= SPI_CR1_CRCEN;
mbed_official 125:23cc3068a9e4 1004 }
mbed_official 125:23cc3068a9e4 1005 else
mbed_official 125:23cc3068a9e4 1006 {
mbed_official 125:23cc3068a9e4 1007 /* Disable the selected SPI CRC calculation */
mbed_official 125:23cc3068a9e4 1008 SPIx->CR1 &= (uint16_t)~((uint16_t)SPI_CR1_CRCEN);
mbed_official 125:23cc3068a9e4 1009 }
mbed_official 125:23cc3068a9e4 1010 }
mbed_official 125:23cc3068a9e4 1011
mbed_official 125:23cc3068a9e4 1012 /**
mbed_official 125:23cc3068a9e4 1013 * @brief Transmits the SPIx CRC value.
mbed_official 125:23cc3068a9e4 1014 * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
mbed_official 125:23cc3068a9e4 1015 * @retval None
mbed_official 125:23cc3068a9e4 1016 */
mbed_official 125:23cc3068a9e4 1017 void SPI_TransmitCRC(SPI_TypeDef* SPIx)
mbed_official 125:23cc3068a9e4 1018 {
mbed_official 125:23cc3068a9e4 1019 /* Check the parameters */
mbed_official 125:23cc3068a9e4 1020 assert_param(IS_SPI_ALL_PERIPH(SPIx));
mbed_official 125:23cc3068a9e4 1021
mbed_official 125:23cc3068a9e4 1022 /* Enable the selected SPI CRC transmission */
mbed_official 125:23cc3068a9e4 1023 SPIx->CR1 |= SPI_CR1_CRCNEXT;
mbed_official 125:23cc3068a9e4 1024 }
mbed_official 125:23cc3068a9e4 1025
mbed_official 125:23cc3068a9e4 1026 /**
mbed_official 125:23cc3068a9e4 1027 * @brief Returns the transmit or the receive CRC register value for the specified SPI.
mbed_official 125:23cc3068a9e4 1028 * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
mbed_official 125:23cc3068a9e4 1029 * @param SPI_CRC: specifies the CRC register to be read.
mbed_official 125:23cc3068a9e4 1030 * This parameter can be one of the following values:
mbed_official 125:23cc3068a9e4 1031 * @arg SPI_CRC_Tx: Selects Tx CRC register
mbed_official 125:23cc3068a9e4 1032 * @arg SPI_CRC_Rx: Selects Rx CRC register
mbed_official 125:23cc3068a9e4 1033 * @retval The selected CRC register value..
mbed_official 125:23cc3068a9e4 1034 */
mbed_official 125:23cc3068a9e4 1035 uint16_t SPI_GetCRC(SPI_TypeDef* SPIx, uint8_t SPI_CRC)
mbed_official 125:23cc3068a9e4 1036 {
mbed_official 125:23cc3068a9e4 1037 uint16_t crcreg = 0;
mbed_official 125:23cc3068a9e4 1038 /* Check the parameters */
mbed_official 125:23cc3068a9e4 1039 assert_param(IS_SPI_ALL_PERIPH(SPIx));
mbed_official 125:23cc3068a9e4 1040 assert_param(IS_SPI_CRC(SPI_CRC));
mbed_official 125:23cc3068a9e4 1041
mbed_official 125:23cc3068a9e4 1042 if (SPI_CRC != SPI_CRC_Rx)
mbed_official 125:23cc3068a9e4 1043 {
mbed_official 125:23cc3068a9e4 1044 /* Get the Tx CRC register */
mbed_official 125:23cc3068a9e4 1045 crcreg = SPIx->TXCRCR;
mbed_official 125:23cc3068a9e4 1046 }
mbed_official 125:23cc3068a9e4 1047 else
mbed_official 125:23cc3068a9e4 1048 {
mbed_official 125:23cc3068a9e4 1049 /* Get the Rx CRC register */
mbed_official 125:23cc3068a9e4 1050 crcreg = SPIx->RXCRCR;
mbed_official 125:23cc3068a9e4 1051 }
mbed_official 125:23cc3068a9e4 1052 /* Return the selected CRC register */
mbed_official 125:23cc3068a9e4 1053 return crcreg;
mbed_official 125:23cc3068a9e4 1054 }
mbed_official 125:23cc3068a9e4 1055
mbed_official 125:23cc3068a9e4 1056 /**
mbed_official 125:23cc3068a9e4 1057 * @brief Returns the CRC Polynomial register value for the specified SPI.
mbed_official 125:23cc3068a9e4 1058 * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
mbed_official 125:23cc3068a9e4 1059 * @retval The CRC Polynomial register value.
mbed_official 125:23cc3068a9e4 1060 */
mbed_official 125:23cc3068a9e4 1061 uint16_t SPI_GetCRCPolynomial(SPI_TypeDef* SPIx)
mbed_official 125:23cc3068a9e4 1062 {
mbed_official 125:23cc3068a9e4 1063 /* Check the parameters */
mbed_official 125:23cc3068a9e4 1064 assert_param(IS_SPI_ALL_PERIPH(SPIx));
mbed_official 125:23cc3068a9e4 1065
mbed_official 125:23cc3068a9e4 1066 /* Return the CRC polynomial register */
mbed_official 125:23cc3068a9e4 1067 return SPIx->CRCPR;
mbed_official 125:23cc3068a9e4 1068 }
mbed_official 125:23cc3068a9e4 1069
mbed_official 125:23cc3068a9e4 1070 /**
mbed_official 125:23cc3068a9e4 1071 * @}
mbed_official 125:23cc3068a9e4 1072 */
mbed_official 125:23cc3068a9e4 1073
mbed_official 125:23cc3068a9e4 1074 /** @defgroup SPI_Group4 DMA transfers management functions
mbed_official 125:23cc3068a9e4 1075 * @brief DMA transfers management functions
mbed_official 125:23cc3068a9e4 1076 *
mbed_official 125:23cc3068a9e4 1077 @verbatim
mbed_official 125:23cc3068a9e4 1078 ===============================================================================
mbed_official 125:23cc3068a9e4 1079 ##### DMA transfers management functions #####
mbed_official 125:23cc3068a9e4 1080 ===============================================================================
mbed_official 125:23cc3068a9e4 1081
mbed_official 125:23cc3068a9e4 1082 @endverbatim
mbed_official 125:23cc3068a9e4 1083 * @{
mbed_official 125:23cc3068a9e4 1084 */
mbed_official 125:23cc3068a9e4 1085
mbed_official 125:23cc3068a9e4 1086 /**
mbed_official 125:23cc3068a9e4 1087 * @brief Enables or disables the SPIx/I2Sx DMA interface.
mbed_official 125:23cc3068a9e4 1088 * @param SPIx:To select the SPIx/I2Sx peripheral, where x can be: 1, 2 or 3
mbed_official 125:23cc3068a9e4 1089 * in SPI mode or 2 or 3 in I2S mode or I2Sxext for I2S full duplex mode.
mbed_official 125:23cc3068a9e4 1090 * @param SPI_I2S_DMAReq: specifies the SPI DMA transfer request to be enabled or disabled.
mbed_official 125:23cc3068a9e4 1091 * This parameter can be any combination of the following values:
mbed_official 125:23cc3068a9e4 1092 * @arg SPI_I2S_DMAReq_Tx: Tx buffer DMA transfer request
mbed_official 125:23cc3068a9e4 1093 * @arg SPI_I2S_DMAReq_Rx: Rx buffer DMA transfer request
mbed_official 125:23cc3068a9e4 1094 * @param NewState: new state of the selected SPI DMA transfer request.
mbed_official 125:23cc3068a9e4 1095 * This parameter can be: ENABLE or DISABLE.
mbed_official 125:23cc3068a9e4 1096 * @retval None
mbed_official 125:23cc3068a9e4 1097 */
mbed_official 125:23cc3068a9e4 1098 void SPI_I2S_DMACmd(SPI_TypeDef* SPIx, uint16_t SPI_I2S_DMAReq, FunctionalState NewState)
mbed_official 125:23cc3068a9e4 1099 {
mbed_official 125:23cc3068a9e4 1100 /* Check the parameters */
mbed_official 125:23cc3068a9e4 1101 assert_param(IS_SPI_ALL_PERIPH_EXT(SPIx));
mbed_official 125:23cc3068a9e4 1102 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 125:23cc3068a9e4 1103 assert_param(IS_SPI_I2S_DMA_REQ(SPI_I2S_DMAReq));
mbed_official 125:23cc3068a9e4 1104
mbed_official 125:23cc3068a9e4 1105 if (NewState != DISABLE)
mbed_official 125:23cc3068a9e4 1106 {
mbed_official 125:23cc3068a9e4 1107 /* Enable the selected SPI DMA requests */
mbed_official 125:23cc3068a9e4 1108 SPIx->CR2 |= SPI_I2S_DMAReq;
mbed_official 125:23cc3068a9e4 1109 }
mbed_official 125:23cc3068a9e4 1110 else
mbed_official 125:23cc3068a9e4 1111 {
mbed_official 125:23cc3068a9e4 1112 /* Disable the selected SPI DMA requests */
mbed_official 125:23cc3068a9e4 1113 SPIx->CR2 &= (uint16_t)~SPI_I2S_DMAReq;
mbed_official 125:23cc3068a9e4 1114 }
mbed_official 125:23cc3068a9e4 1115 }
mbed_official 125:23cc3068a9e4 1116
mbed_official 125:23cc3068a9e4 1117 /**
mbed_official 125:23cc3068a9e4 1118 * @brief Configures the number of data to transfer type(Even/Odd) for the DMA
mbed_official 125:23cc3068a9e4 1119 * last transfers and for the selected SPI.
mbed_official 125:23cc3068a9e4 1120 * @note This function have a meaning only if DMA mode is selected and if
mbed_official 125:23cc3068a9e4 1121 * the packing mode is used (data length <= 8 and DMA transfer size halfword)
mbed_official 125:23cc3068a9e4 1122 * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
mbed_official 125:23cc3068a9e4 1123 * @param SPI_LastDMATransfer: specifies the SPI last DMA transfers state.
mbed_official 125:23cc3068a9e4 1124 * This parameter can be one of the following values:
mbed_official 125:23cc3068a9e4 1125 * @arg SPI_LastDMATransfer_TxEvenRxEven: Number of data for transmission Even
mbed_official 125:23cc3068a9e4 1126 * and number of data for reception Even.
mbed_official 125:23cc3068a9e4 1127 * @arg SPI_LastDMATransfer_TxOddRxEven: Number of data for transmission Odd
mbed_official 125:23cc3068a9e4 1128 * and number of data for reception Even.
mbed_official 125:23cc3068a9e4 1129 * @arg SPI_LastDMATransfer_TxEvenRxOdd: Number of data for transmission Even
mbed_official 125:23cc3068a9e4 1130 * and number of data for reception Odd.
mbed_official 125:23cc3068a9e4 1131 * @arg SPI_LastDMATransfer_TxOddRxOdd: RNumber of data for transmission Odd
mbed_official 125:23cc3068a9e4 1132 * and number of data for reception Odd.
mbed_official 125:23cc3068a9e4 1133 * @retval None
mbed_official 125:23cc3068a9e4 1134 */
mbed_official 125:23cc3068a9e4 1135 void SPI_LastDMATransferCmd(SPI_TypeDef* SPIx, uint16_t SPI_LastDMATransfer)
mbed_official 125:23cc3068a9e4 1136 {
mbed_official 125:23cc3068a9e4 1137 /* Check the parameters */
mbed_official 125:23cc3068a9e4 1138 assert_param(IS_SPI_ALL_PERIPH(SPIx));
mbed_official 125:23cc3068a9e4 1139 assert_param(IS_SPI_LAST_DMA_TRANSFER(SPI_LastDMATransfer));
mbed_official 125:23cc3068a9e4 1140
mbed_official 125:23cc3068a9e4 1141 /* Clear LDMA_TX and LDMA_RX bits */
mbed_official 125:23cc3068a9e4 1142 SPIx->CR2 &= CR2_LDMA_MASK;
mbed_official 125:23cc3068a9e4 1143
mbed_official 125:23cc3068a9e4 1144 /* Set new LDMA_TX and LDMA_RX bits value */
mbed_official 125:23cc3068a9e4 1145 SPIx->CR2 |= SPI_LastDMATransfer;
mbed_official 125:23cc3068a9e4 1146 }
mbed_official 125:23cc3068a9e4 1147
mbed_official 125:23cc3068a9e4 1148 /**
mbed_official 125:23cc3068a9e4 1149 * @}
mbed_official 125:23cc3068a9e4 1150 */
mbed_official 125:23cc3068a9e4 1151
mbed_official 125:23cc3068a9e4 1152 /** @defgroup SPI_Group5 Interrupts and flags management functions
mbed_official 125:23cc3068a9e4 1153 * @brief Interrupts and flags management functions
mbed_official 125:23cc3068a9e4 1154 *
mbed_official 125:23cc3068a9e4 1155 @verbatim
mbed_official 125:23cc3068a9e4 1156 ===============================================================================
mbed_official 125:23cc3068a9e4 1157 ##### Interrupts and flags management functions #####
mbed_official 125:23cc3068a9e4 1158 ===============================================================================
mbed_official 125:23cc3068a9e4 1159 [..] This section provides a set of functions allowing to configure the SPI/I2S
mbed_official 125:23cc3068a9e4 1160 Interrupts sources and check or clear the flags or pending bits status.
mbed_official 125:23cc3068a9e4 1161 The user should identify which mode will be used in his application to manage
mbed_official 125:23cc3068a9e4 1162 the communication: Polling mode, Interrupt mode or DMA mode.
mbed_official 125:23cc3068a9e4 1163
mbed_official 125:23cc3068a9e4 1164 *** Polling Mode ***
mbed_official 125:23cc3068a9e4 1165 ====================
mbed_official 125:23cc3068a9e4 1166 [..] In Polling Mode, the SPI/I2S communication can be managed by 9 flags:
mbed_official 125:23cc3068a9e4 1167 (#) SPI_I2S_FLAG_TXE : to indicate the status of the transmit buffer register.
mbed_official 125:23cc3068a9e4 1168 (#) SPI_I2S_FLAG_RXNE : to indicate the status of the receive buffer register.
mbed_official 125:23cc3068a9e4 1169 (#) SPI_I2S_FLAG_BSY : to indicate the state of the communication layer of the SPI.
mbed_official 125:23cc3068a9e4 1170 (#) SPI_FLAG_CRCERR : to indicate if a CRC Calculation error occur.
mbed_official 125:23cc3068a9e4 1171 (#) SPI_FLAG_MODF : to indicate if a Mode Fault error occur.
mbed_official 125:23cc3068a9e4 1172 (#) SPI_I2S_FLAG_OVR : to indicate if an Overrun error occur.
mbed_official 125:23cc3068a9e4 1173 (#) SPI_I2S_FLAG_FRE: to indicate a Frame Format error occurs.
mbed_official 125:23cc3068a9e4 1174 (#) I2S_FLAG_UDR: to indicate an Underrun error occurs.
mbed_official 125:23cc3068a9e4 1175 (#) I2S_FLAG_CHSIDE: to indicate Channel Side.
mbed_official 125:23cc3068a9e4 1176 [..]
mbed_official 125:23cc3068a9e4 1177 (@) Do not use the BSY flag to handle each data transmission or reception.
mbed_official 125:23cc3068a9e4 1178 It is better to use the TXE and RXNE flags instead.
mbed_official 125:23cc3068a9e4 1179 [..] In this Mode it is advised to use the following functions:
mbed_official 125:23cc3068a9e4 1180 (+) FlagStatus SPI_I2S_GetFlagStatus(SPI_TypeDef* SPIx, uint16_t SPI_I2S_FLAG);
mbed_official 125:23cc3068a9e4 1181 (+) void SPI_I2S_ClearFlag(SPI_TypeDef* SPIx, uint16_t SPI_I2S_FLAG);
mbed_official 125:23cc3068a9e4 1182
mbed_official 125:23cc3068a9e4 1183 *** Interrupt Mode ***
mbed_official 125:23cc3068a9e4 1184 ======================
mbed_official 125:23cc3068a9e4 1185 [..] In Interrupt Mode, the SPI/I2S communication can be managed by 3 interrupt sources
mbed_official 125:23cc3068a9e4 1186 and 5 pending bits:
mbed_official 125:23cc3068a9e4 1187 [..] Pending Bits:
mbed_official 125:23cc3068a9e4 1188 (#) SPI_I2S_IT_TXE : to indicate the status of the transmit buffer register.
mbed_official 125:23cc3068a9e4 1189 (#) SPI_I2S_IT_RXNE : to indicate the status of the receive buffer register.
mbed_official 125:23cc3068a9e4 1190 (#) SPI_I2S_IT_OVR : to indicate if an Overrun error occur.
mbed_official 125:23cc3068a9e4 1191 (#) I2S_IT_UDR : to indicate an Underrun Error occurs.
mbed_official 125:23cc3068a9e4 1192 (#) SPI_I2S_FLAG_FRE : to indicate a Frame Format error occurs.
mbed_official 125:23cc3068a9e4 1193 [..] Interrupt Source:
mbed_official 125:23cc3068a9e4 1194 (#) SPI_I2S_IT_TXE: specifies the interrupt source for the Tx buffer empty
mbed_official 125:23cc3068a9e4 1195 interrupt.
mbed_official 125:23cc3068a9e4 1196 (#) SPI_I2S_IT_RXNE : specifies the interrupt source for the Rx buffer not
mbed_official 125:23cc3068a9e4 1197 empty interrupt.
mbed_official 125:23cc3068a9e4 1198 (#) SPI_I2S_IT_ERR : specifies the interrupt source for the errors interrupt.
mbed_official 125:23cc3068a9e4 1199 [..] In this Mode it is advised to use the following functions:
mbed_official 125:23cc3068a9e4 1200 (+) void SPI_I2S_ITConfig(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT, FunctionalState NewState);
mbed_official 125:23cc3068a9e4 1201 (+) ITStatus SPI_I2S_GetITStatus(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT);
mbed_official 125:23cc3068a9e4 1202
mbed_official 125:23cc3068a9e4 1203 *** FIFO Status ***
mbed_official 125:23cc3068a9e4 1204 ===================
mbed_official 125:23cc3068a9e4 1205 [..] It is possible to monitor the FIFO status when a transfer is ongoing using the
mbed_official 125:23cc3068a9e4 1206 following function:
mbed_official 125:23cc3068a9e4 1207 (+) uint32_t SPI_GetFIFOStatus(uint8_t SPI_FIFO_Direction);
mbed_official 125:23cc3068a9e4 1208
mbed_official 125:23cc3068a9e4 1209 *** DMA Mode ***
mbed_official 125:23cc3068a9e4 1210 ================
mbed_official 125:23cc3068a9e4 1211 [..] In DMA Mode, the SPI communication can be managed by 2 DMA Channel requests:
mbed_official 125:23cc3068a9e4 1212 (#) SPI_I2S_DMAReq_Tx: specifies the Tx buffer DMA transfer request.
mbed_official 125:23cc3068a9e4 1213 (#) SPI_I2S_DMAReq_Rx: specifies the Rx buffer DMA transfer request.
mbed_official 125:23cc3068a9e4 1214 [..] In this Mode it is advised to use the following function:
mbed_official 125:23cc3068a9e4 1215 (+) void SPI_I2S_DMACmd(SPI_TypeDef* SPIx, uint16_t SPI_I2S_DMAReq, FunctionalState NewState);
mbed_official 125:23cc3068a9e4 1216
mbed_official 125:23cc3068a9e4 1217 @endverbatim
mbed_official 125:23cc3068a9e4 1218 * @{
mbed_official 125:23cc3068a9e4 1219 */
mbed_official 125:23cc3068a9e4 1220
mbed_official 125:23cc3068a9e4 1221 /**
mbed_official 125:23cc3068a9e4 1222 * @brief Enables or disables the specified SPI/I2S interrupts.
mbed_official 125:23cc3068a9e4 1223 * @param SPIx: To select the SPIx/I2Sx peripheral, where x can be: 1, 2 or 3
mbed_official 125:23cc3068a9e4 1224 * in SPI mode or 2 or 3 in I2S mode or I2Sxext for I2S full duplex mode.
mbed_official 125:23cc3068a9e4 1225 * @param SPI_I2S_IT: specifies the SPI interrupt source to be enabled or disabled.
mbed_official 125:23cc3068a9e4 1226 * This parameter can be one of the following values:
mbed_official 125:23cc3068a9e4 1227 * @arg SPI_I2S_IT_TXE: Tx buffer empty interrupt mask
mbed_official 125:23cc3068a9e4 1228 * @arg SPI_I2S_IT_RXNE: Rx buffer not empty interrupt mask
mbed_official 125:23cc3068a9e4 1229 * @arg SPI_I2S_IT_ERR: Error interrupt mask
mbed_official 125:23cc3068a9e4 1230 * @param NewState: new state of the specified SPI interrupt.
mbed_official 125:23cc3068a9e4 1231 * This parameter can be: ENABLE or DISABLE.
mbed_official 125:23cc3068a9e4 1232 * @retval None
mbed_official 125:23cc3068a9e4 1233 */
mbed_official 125:23cc3068a9e4 1234 void SPI_I2S_ITConfig(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT, FunctionalState NewState)
mbed_official 125:23cc3068a9e4 1235 {
mbed_official 125:23cc3068a9e4 1236 uint16_t itpos = 0, itmask = 0 ;
mbed_official 125:23cc3068a9e4 1237
mbed_official 125:23cc3068a9e4 1238 /* Check the parameters */
mbed_official 125:23cc3068a9e4 1239 assert_param(IS_SPI_ALL_PERIPH_EXT(SPIx));
mbed_official 125:23cc3068a9e4 1240 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 125:23cc3068a9e4 1241 assert_param(IS_SPI_I2S_CONFIG_IT(SPI_I2S_IT));
mbed_official 125:23cc3068a9e4 1242
mbed_official 125:23cc3068a9e4 1243 /* Get the SPI IT index */
mbed_official 125:23cc3068a9e4 1244 itpos = SPI_I2S_IT >> 4;
mbed_official 125:23cc3068a9e4 1245
mbed_official 125:23cc3068a9e4 1246 /* Set the IT mask */
mbed_official 125:23cc3068a9e4 1247 itmask = (uint16_t)1 << (uint16_t)itpos;
mbed_official 125:23cc3068a9e4 1248
mbed_official 125:23cc3068a9e4 1249 if (NewState != DISABLE)
mbed_official 125:23cc3068a9e4 1250 {
mbed_official 125:23cc3068a9e4 1251 /* Enable the selected SPI interrupt */
mbed_official 125:23cc3068a9e4 1252 SPIx->CR2 |= itmask;
mbed_official 125:23cc3068a9e4 1253 }
mbed_official 125:23cc3068a9e4 1254 else
mbed_official 125:23cc3068a9e4 1255 {
mbed_official 125:23cc3068a9e4 1256 /* Disable the selected SPI interrupt */
mbed_official 125:23cc3068a9e4 1257 SPIx->CR2 &= (uint16_t)~itmask;
mbed_official 125:23cc3068a9e4 1258 }
mbed_official 125:23cc3068a9e4 1259 }
mbed_official 125:23cc3068a9e4 1260
mbed_official 125:23cc3068a9e4 1261 /**
mbed_official 125:23cc3068a9e4 1262 * @brief Returns the current SPIx Transmission FIFO filled level.
mbed_official 125:23cc3068a9e4 1263 * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
mbed_official 125:23cc3068a9e4 1264 * @retval The Transmission FIFO filling state.
mbed_official 125:23cc3068a9e4 1265 * - SPI_TransmissionFIFOStatus_Empty: when FIFO is empty
mbed_official 125:23cc3068a9e4 1266 * - SPI_TransmissionFIFOStatus_1QuarterFull: if more than 1 quarter-full.
mbed_official 125:23cc3068a9e4 1267 * - SPI_TransmissionFIFOStatus_HalfFull: if more than 1 half-full.
mbed_official 125:23cc3068a9e4 1268 * - SPI_TransmissionFIFOStatus_Full: when FIFO is full.
mbed_official 125:23cc3068a9e4 1269 */
mbed_official 125:23cc3068a9e4 1270 uint16_t SPI_GetTransmissionFIFOStatus(SPI_TypeDef* SPIx)
mbed_official 125:23cc3068a9e4 1271 {
mbed_official 125:23cc3068a9e4 1272 /* Get the SPIx Transmission FIFO level bits */
mbed_official 125:23cc3068a9e4 1273 return (uint16_t)((SPIx->SR & SPI_SR_FTLVL));
mbed_official 125:23cc3068a9e4 1274 }
mbed_official 125:23cc3068a9e4 1275
mbed_official 125:23cc3068a9e4 1276 /**
mbed_official 125:23cc3068a9e4 1277 * @brief Returns the current SPIx Reception FIFO filled level.
mbed_official 125:23cc3068a9e4 1278 * @param SPIx: where x can be 1, 2 or 3 to select the SPI peripheral.
mbed_official 125:23cc3068a9e4 1279 * @retval The Reception FIFO filling state.
mbed_official 125:23cc3068a9e4 1280 * - SPI_ReceptionFIFOStatus_Empty: when FIFO is empty
mbed_official 125:23cc3068a9e4 1281 * - SPI_ReceptionFIFOStatus_1QuarterFull: if more than 1 quarter-full.
mbed_official 125:23cc3068a9e4 1282 * - SPI_ReceptionFIFOStatus_HalfFull: if more than 1 half-full.
mbed_official 125:23cc3068a9e4 1283 * - SPI_ReceptionFIFOStatus_Full: when FIFO is full.
mbed_official 125:23cc3068a9e4 1284 */
mbed_official 125:23cc3068a9e4 1285 uint16_t SPI_GetReceptionFIFOStatus(SPI_TypeDef* SPIx)
mbed_official 125:23cc3068a9e4 1286 {
mbed_official 125:23cc3068a9e4 1287 /* Get the SPIx Reception FIFO level bits */
mbed_official 125:23cc3068a9e4 1288 return (uint16_t)((SPIx->SR & SPI_SR_FRLVL));
mbed_official 125:23cc3068a9e4 1289 }
mbed_official 125:23cc3068a9e4 1290
mbed_official 125:23cc3068a9e4 1291 /**
mbed_official 125:23cc3068a9e4 1292 * @brief Checks whether the specified SPI flag is set or not.
mbed_official 125:23cc3068a9e4 1293 * @param SPIx: To select the SPIx/I2Sx peripheral, where x can be: 1, 2 or 3
mbed_official 125:23cc3068a9e4 1294 * in SPI mode or 2 or 3 in I2S mode or I2Sxext for I2S full duplex mode.
mbed_official 125:23cc3068a9e4 1295 * @param SPI_I2S_FLAG: specifies the SPI flag to check.
mbed_official 125:23cc3068a9e4 1296 * This parameter can be one of the following values:
mbed_official 125:23cc3068a9e4 1297 * @arg SPI_I2S_FLAG_TXE: Transmit buffer empty flag.
mbed_official 125:23cc3068a9e4 1298 * @arg SPI_I2S_FLAG_RXNE: Receive buffer not empty flag.
mbed_official 125:23cc3068a9e4 1299 * @arg SPI_I2S_FLAG_BSY: Busy flag.
mbed_official 125:23cc3068a9e4 1300 * @arg SPI_I2S_FLAG_OVR: Overrun flag.
mbed_official 125:23cc3068a9e4 1301 * @arg SPI_I2S_FLAG_MODF: Mode Fault flag.
mbed_official 125:23cc3068a9e4 1302 * @arg SPI_I2S_FLAG_CRCERR: CRC Error flag.
mbed_official 125:23cc3068a9e4 1303 * @arg SPI_I2S_FLAG_FRE: TI frame format error flag.
mbed_official 125:23cc3068a9e4 1304 * @arg I2S_FLAG_UDR: Underrun Error flag.
mbed_official 125:23cc3068a9e4 1305 * @arg I2S_FLAG_CHSIDE: Channel Side flag.
mbed_official 125:23cc3068a9e4 1306 * @retval The new state of SPI_I2S_FLAG (SET or RESET).
mbed_official 125:23cc3068a9e4 1307 */
mbed_official 125:23cc3068a9e4 1308 FlagStatus SPI_I2S_GetFlagStatus(SPI_TypeDef* SPIx, uint16_t SPI_I2S_FLAG)
mbed_official 125:23cc3068a9e4 1309 {
mbed_official 125:23cc3068a9e4 1310 FlagStatus bitstatus = RESET;
mbed_official 125:23cc3068a9e4 1311 /* Check the parameters */
mbed_official 125:23cc3068a9e4 1312 assert_param(IS_SPI_ALL_PERIPH_EXT(SPIx));
mbed_official 125:23cc3068a9e4 1313 assert_param(IS_SPI_I2S_GET_FLAG(SPI_I2S_FLAG));
mbed_official 125:23cc3068a9e4 1314
mbed_official 125:23cc3068a9e4 1315 /* Check the status of the specified SPI flag */
mbed_official 125:23cc3068a9e4 1316 if ((SPIx->SR & SPI_I2S_FLAG) != (uint16_t)RESET)
mbed_official 125:23cc3068a9e4 1317 {
mbed_official 125:23cc3068a9e4 1318 /* SPI_I2S_FLAG is set */
mbed_official 125:23cc3068a9e4 1319 bitstatus = SET;
mbed_official 125:23cc3068a9e4 1320 }
mbed_official 125:23cc3068a9e4 1321 else
mbed_official 125:23cc3068a9e4 1322 {
mbed_official 125:23cc3068a9e4 1323 /* SPI_I2S_FLAG is reset */
mbed_official 125:23cc3068a9e4 1324 bitstatus = RESET;
mbed_official 125:23cc3068a9e4 1325 }
mbed_official 125:23cc3068a9e4 1326 /* Return the SPI_I2S_FLAG status */
mbed_official 125:23cc3068a9e4 1327 return bitstatus;
mbed_official 125:23cc3068a9e4 1328 }
mbed_official 125:23cc3068a9e4 1329
mbed_official 125:23cc3068a9e4 1330 /**
mbed_official 125:23cc3068a9e4 1331 * @brief Clears the SPIx CRC Error (CRCERR) flag.
mbed_official 125:23cc3068a9e4 1332 * @param SPIx: To select the SPIx/I2Sx peripheral, where x can be: 1, 2 or 3
mbed_official 125:23cc3068a9e4 1333 * in SPI mode or 2 or 3 in I2S mode or I2Sxext for I2S full duplex mode.
mbed_official 125:23cc3068a9e4 1334 * @param SPI_I2S_FLAG: specifies the SPI flag to clear.
mbed_official 125:23cc3068a9e4 1335 * This function clears only CRCERR flag.
mbed_official 125:23cc3068a9e4 1336 * @note OVR (OverRun error) flag is cleared by software sequence: a read
mbed_official 125:23cc3068a9e4 1337 * operation to SPI_DR register (SPI_I2S_ReceiveData()) followed by a read
mbed_official 125:23cc3068a9e4 1338 * operation to SPI_SR register (SPI_I2S_GetFlagStatus()).
mbed_official 125:23cc3068a9e4 1339 * @note MODF (Mode Fault) flag is cleared by software sequence: a read/write
mbed_official 125:23cc3068a9e4 1340 * operation to SPI_SR register (SPI_I2S_GetFlagStatus()) followed by a
mbed_official 125:23cc3068a9e4 1341 * write operation to SPI_CR1 register (SPI_Cmd() to enable the SPI).
mbed_official 125:23cc3068a9e4 1342 * @retval None
mbed_official 125:23cc3068a9e4 1343 */
mbed_official 125:23cc3068a9e4 1344 void SPI_I2S_ClearFlag(SPI_TypeDef* SPIx, uint16_t SPI_I2S_FLAG)
mbed_official 125:23cc3068a9e4 1345 {
mbed_official 125:23cc3068a9e4 1346 /* Check the parameters */
mbed_official 125:23cc3068a9e4 1347 assert_param(IS_SPI_ALL_PERIPH_EXT(SPIx));
mbed_official 125:23cc3068a9e4 1348 assert_param(IS_SPI_CLEAR_FLAG(SPI_I2S_FLAG));
mbed_official 125:23cc3068a9e4 1349
mbed_official 125:23cc3068a9e4 1350 /* Clear the selected SPI CRC Error (CRCERR) flag */
mbed_official 125:23cc3068a9e4 1351 SPIx->SR = (uint16_t)~SPI_I2S_FLAG;
mbed_official 125:23cc3068a9e4 1352 }
mbed_official 125:23cc3068a9e4 1353
mbed_official 125:23cc3068a9e4 1354 /**
mbed_official 125:23cc3068a9e4 1355 * @brief Checks whether the specified SPI/I2S interrupt has occurred or not.
mbed_official 125:23cc3068a9e4 1356 * @param SPIx: To select the SPIx/I2Sx peripheral, where x can be: 1, 2 or 3
mbed_official 125:23cc3068a9e4 1357 * in SPI mode or 2 or 3 in I2S mode or I2Sxext for I2S full duplex mode.
mbed_official 125:23cc3068a9e4 1358 * @param SPI_I2S_IT: specifies the SPI interrupt source to check.
mbed_official 125:23cc3068a9e4 1359 * This parameter can be one of the following values:
mbed_official 125:23cc3068a9e4 1360 * @arg SPI_I2S_IT_TXE: Transmit buffer empty interrupt.
mbed_official 125:23cc3068a9e4 1361 * @arg SPI_I2S_IT_RXNE: Receive buffer not empty interrupt.
mbed_official 125:23cc3068a9e4 1362 * @arg SPI_IT_MODF: Mode Fault interrupt.
mbed_official 125:23cc3068a9e4 1363 * @arg SPI_I2S_IT_OVR: Overrun interrupt.
mbed_official 125:23cc3068a9e4 1364 * @arg I2S_IT_UDR: Underrun interrupt.
mbed_official 125:23cc3068a9e4 1365 * @arg SPI_I2S_IT_FRE: Format Error interrupt.
mbed_official 125:23cc3068a9e4 1366 * @retval The new state of SPI_I2S_IT (SET or RESET).
mbed_official 125:23cc3068a9e4 1367 */
mbed_official 125:23cc3068a9e4 1368 ITStatus SPI_I2S_GetITStatus(SPI_TypeDef* SPIx, uint8_t SPI_I2S_IT)
mbed_official 125:23cc3068a9e4 1369 {
mbed_official 125:23cc3068a9e4 1370 ITStatus bitstatus = RESET;
mbed_official 125:23cc3068a9e4 1371 uint16_t itpos = 0, itmask = 0, enablestatus = 0;
mbed_official 125:23cc3068a9e4 1372
mbed_official 125:23cc3068a9e4 1373 /* Check the parameters */
mbed_official 125:23cc3068a9e4 1374 assert_param(IS_SPI_ALL_PERIPH_EXT(SPIx));
mbed_official 125:23cc3068a9e4 1375 assert_param(IS_SPI_I2S_GET_IT(SPI_I2S_IT));
mbed_official 125:23cc3068a9e4 1376
mbed_official 125:23cc3068a9e4 1377 /* Get the SPI_I2S_IT index */
mbed_official 125:23cc3068a9e4 1378 itpos = 0x01 << (SPI_I2S_IT & 0x0F);
mbed_official 125:23cc3068a9e4 1379
mbed_official 125:23cc3068a9e4 1380 /* Get the SPI_I2S_IT IT mask */
mbed_official 125:23cc3068a9e4 1381 itmask = SPI_I2S_IT >> 4;
mbed_official 125:23cc3068a9e4 1382
mbed_official 125:23cc3068a9e4 1383 /* Set the IT mask */
mbed_official 125:23cc3068a9e4 1384 itmask = 0x01 << itmask;
mbed_official 125:23cc3068a9e4 1385
mbed_official 125:23cc3068a9e4 1386 /* Get the SPI_I2S_IT enable bit status */
mbed_official 125:23cc3068a9e4 1387 enablestatus = (SPIx->CR2 & itmask) ;
mbed_official 125:23cc3068a9e4 1388
mbed_official 125:23cc3068a9e4 1389 /* Check the status of the specified SPI interrupt */
mbed_official 125:23cc3068a9e4 1390 if (((SPIx->SR & itpos) != (uint16_t)RESET) && enablestatus)
mbed_official 125:23cc3068a9e4 1391 {
mbed_official 125:23cc3068a9e4 1392 /* SPI_I2S_IT is set */
mbed_official 125:23cc3068a9e4 1393 bitstatus = SET;
mbed_official 125:23cc3068a9e4 1394 }
mbed_official 125:23cc3068a9e4 1395 else
mbed_official 125:23cc3068a9e4 1396 {
mbed_official 125:23cc3068a9e4 1397 /* SPI_I2S_IT is reset */
mbed_official 125:23cc3068a9e4 1398 bitstatus = RESET;
mbed_official 125:23cc3068a9e4 1399 }
mbed_official 125:23cc3068a9e4 1400 /* Return the SPI_I2S_IT status */
mbed_official 125:23cc3068a9e4 1401 return bitstatus;
mbed_official 125:23cc3068a9e4 1402 }
mbed_official 125:23cc3068a9e4 1403
mbed_official 125:23cc3068a9e4 1404 /**
mbed_official 125:23cc3068a9e4 1405 * @}
mbed_official 125:23cc3068a9e4 1406 */
mbed_official 125:23cc3068a9e4 1407
mbed_official 125:23cc3068a9e4 1408 /**
mbed_official 125:23cc3068a9e4 1409 * @}
mbed_official 125:23cc3068a9e4 1410 */
mbed_official 125:23cc3068a9e4 1411
mbed_official 125:23cc3068a9e4 1412 /**
mbed_official 125:23cc3068a9e4 1413 * @}
mbed_official 125:23cc3068a9e4 1414 */
mbed_official 125:23cc3068a9e4 1415
mbed_official 125:23cc3068a9e4 1416 /**
mbed_official 125:23cc3068a9e4 1417 * @}
mbed_official 125:23cc3068a9e4 1418 */
mbed_official 125:23cc3068a9e4 1419
mbed_official 125:23cc3068a9e4 1420 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/