mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Wed Feb 26 09:45:12 2014 +0000
Revision:
106:ced8cbb51063
Parent:
80:66393a7b209d
Synchronized with git revision 4222735eff5868389433f0e9271976b39c8115cd

Full URL: https://github.com/mbedmicro/mbed/commit/4222735eff5868389433f0e9271976b39c8115cd/

[NUCLEO_xxx] Update STM32CubeF4 driver V1.0.0 + update license

Who changed what in which revision?

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