mbed library sources

Dependents:   Marvino mbot

Fork of mbed-src by mbed official

Committer:
jaerts
Date:
Tue Dec 22 13:22:16 2015 +0000
Revision:
637:ed69428d4850
Parent:
155:8435094ec241
Add very shady LPC1768 CAN Filter implementation

Who changed what in which revision?

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