mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
shaoziyang
Date:
Sat Sep 13 14:25:46 2014 +0000
Revision:
323:9e901b0a5aa1
Parent:
130:1dec54e4aec3
test with CLOCK_SETUP = 0

Who changed what in which revision?

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