STM8L15x Standard Peripherals Drivers: stm8l15x_spi.h Source File

STM8L15x/16x Standard Peripherals Drivers

STM8L15x Standard Peripherals Drivers

stm8l15x_spi.h

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm8l15x_spi.h
00004   * @author  MCD Application Team
00005   * @version V1.5.0
00006   * @date    13-May-2011
00007   * @brief   This file contains all the functions prototypes for the SPI firmware
00008   *          library.
00009   ******************************************************************************
00010   * @attention
00011   *
00012   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
00013   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
00014   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
00015   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
00016   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
00017   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
00018   *
00019   * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
00020   ******************************************************************************  
00021   */
00022 
00023 /* Define to prevent recursive inclusion -------------------------------------*/
00024 #ifndef __STM8L15x_SPI_H
00025 #define __STM8L15x_SPI_H
00026 
00027 /* Includes ------------------------------------------------------------------*/
00028 #include "stm8l15x.h"
00029 
00030 /** @addtogroup STM8L15x_StdPeriph_Driver
00031   * @{
00032   */
00033   
00034 /** @addtogroup SPI
00035   * @{
00036   */
00037   
00038 /* Exported types ------------------------------------------------------------*/
00039 /** @defgroup SPI_Exported_Types
00040   * @{
00041   */
00042 
00043 /** @defgroup SPI_Direction_Mode
00044   * @brief element values correspond to BDM, BDOE, RXONLY bits position
00045   * @{
00046   */
00047 typedef enum {
00048   SPI_Direction_2Lines_FullDuplex = (uint8_t)0x00, /*!< 2-line uni-directional data mode enable */
00049   SPI_Direction_2Lines_RxOnly     = (uint8_t)0x04, /*!< Receiver only in 2 line uni-directional data mode */
00050   SPI_Direction_1Line_Rx          = (uint8_t)0x80, /*!< Receiver only in 1 line bi-directional data mode */
00051   SPI_Direction_1Line_Tx          = (uint8_t)0xC0  /*!< Transmit only in 1 line bi-directional data mode */
00052 } SPI_DirectionMode_TypeDef;
00053 
00054 /**
00055   * @}
00056   */
00057 
00058 /** @defgroup SPI_SlaveSelect_Management
00059   * @brief element values correspond to LSBFIRST bit position
00060   * @{
00061   */  
00062 typedef enum
00063 {
00064   SPI_NSS_Soft  = (uint8_t)0x02, /*!< Software slave management disabled */
00065   SPI_NSS_Hard  = (uint8_t)0x00  /*!< Software slave management enabled */
00066 } SPI_NSS_TypeDef;
00067 
00068 /**
00069   * @}
00070   */
00071   
00072 /** @defgroup SPI_Direction
00073   * @{
00074   */
00075 typedef enum 
00076 {
00077   SPI_Direction_Rx = (uint8_t)0x00, /*!< Select Rx receive direction in bi-directional mode */
00078   SPI_Direction_Tx = (uint8_t)0x01  /*!< Select Tx transmission direction in bi-directional mode */
00079 } SPI_Direction_TypeDef;
00080 
00081 /**
00082   * @}
00083   */
00084   
00085 /** @defgroup SPI_Mode
00086   * @{
00087   */
00088 typedef enum
00089 {
00090   SPI_Mode_Master = (uint8_t)0x04, /*!< SPI Master configuration */
00091   SPI_Mode_Slave  = (uint8_t)0x00  /*!< SPI Slave configuration */
00092 } SPI_Mode_TypeDef;
00093 
00094 /**
00095   * @}
00096   */
00097   
00098 /** @defgroup SPI_BaudRate_Prescaler
00099   * @{
00100   */
00101 typedef enum {
00102   SPI_BaudRatePrescaler_2   = (uint8_t)0x00, /*!< SPI frequency = frequency(CPU)/2 */
00103   SPI_BaudRatePrescaler_4   = (uint8_t)0x08, /*!< SPI frequency = frequency(CPU)/4 */
00104   SPI_BaudRatePrescaler_8   = (uint8_t)0x10, /*!< SPI frequency = frequency(CPU)/8 */
00105   SPI_BaudRatePrescaler_16  = (uint8_t)0x18, /*!< SPI frequency = frequency(CPU)/16 */
00106   SPI_BaudRatePrescaler_32  = (uint8_t)0x20, /*!< SPI frequency = frequency(CPU)/32 */
00107   SPI_BaudRatePrescaler_64  = (uint8_t)0x28, /*!< SPI frequency = frequency(CPU)/64 */
00108   SPI_BaudRatePrescaler_128 = (uint8_t)0x30, /*!< SPI frequency = frequency(CPU)/128 */
00109   SPI_BaudRatePrescaler_256 = (uint8_t)0x38  /*!< SPI frequency = frequency(CPU)/256 */
00110 } SPI_BaudRatePrescaler_TypeDef;
00111 
00112 /**
00113   * @}
00114   */
00115   
00116 /** @defgroup SPI_Clock_Polarity
00117   * @{
00118   */
00119 typedef enum 
00120 {
00121   SPI_CPOL_Low  = (uint8_t)0x00, /*!< Clock to 0 when idle */
00122   SPI_CPOL_High = (uint8_t)0x02  /*!< Clock to 1 when idle */
00123 } SPI_CPOL_TypeDef;
00124 
00125 /**
00126   * @}
00127   */
00128   
00129 /** @defgroup SPI_Clock_Phase
00130   * @{
00131   */
00132 typedef enum 
00133 {
00134   SPI_CPHA_1Edge = (uint8_t)0x00, /*!< The first clock transition is the first data capture edge */
00135   SPI_CPHA_2Edge = (uint8_t)0x01  /*!< The second clock transition is the first data capture edge */
00136 } SPI_CPHA_TypeDef;
00137 
00138 /**
00139   * @}
00140   */
00141   
00142 /** @defgroup SPI_Frame_Format
00143   * @{
00144   */
00145 typedef enum 
00146 {
00147   SPI_FirstBit_MSB = (uint8_t)0x00, /*!< MSB bit will be transmitted first */
00148   SPI_FirstBit_LSB = (uint8_t)0x80  /*!< LSB bit will be transmitted first */
00149 } SPI_FirstBit_TypeDef;
00150 
00151 /**
00152   * @}
00153   */
00154   
00155 /** @defgroup SPI_DMA_requests
00156   * @{
00157   */
00158 typedef enum {
00159   SPI_DMAReq_RX = (uint8_t)0x01,    /*!< SPI DMA Rx transfer requests */
00160   SPI_DMAReq_TX = (uint8_t)0x02     /*!< SPI DMA Tx transfer requests */
00161 } SPI_DMAReq_TypeDef;
00162 
00163 /**
00164   * @}
00165   */
00166   
00167 /** @defgroup SPI_CRC
00168   * @{
00169   */
00170 typedef enum {
00171   SPI_CRC_RX = (uint8_t)0x00, /*!< Select Tx CRC register */
00172   SPI_CRC_TX = (uint8_t)0x01  /*!< Select Rx CRC register */
00173 } SPI_CRC_TypeDef;
00174 
00175 /**
00176   * @}
00177   */
00178   
00179 /** @defgroup SPI_Flags
00180   * @{
00181   */
00182 typedef enum {
00183   SPI_FLAG_BSY    = (uint8_t)0x80, /*!< Busy flag */
00184   SPI_FLAG_OVR    = (uint8_t)0x40, /*!< Overrun flag */
00185   SPI_FLAG_MODF   = (uint8_t)0x20, /*!< Mode fault */
00186   SPI_FLAG_CRCERR = (uint8_t)0x10, /*!< CRC error flag */
00187   SPI_FLAG_WKUP   = (uint8_t)0x08, /*!< Wake-up flag */
00188   SPI_FLAG_TXE    = (uint8_t)0x02, /*!< Transmit buffer empty */
00189   SPI_FLAG_RXNE   = (uint8_t)0x01  /*!< Receive buffer empty */
00190 } SPI_FLAG_TypeDef;
00191 
00192 /**
00193   * @}
00194   */
00195   
00196 /** @defgroup SPI_Interrupts
00197   * @brief SPI_IT possible values
00198   * Elements values convention: 0xYX
00199   *   X: Position of the corresponding Interrupt
00200   *   Y: ITPENDINGBIT position
00201   * @{
00202   */
00203 typedef enum
00204 {
00205   SPI_IT_WKUP   = (uint8_t)0x34, /*!< Wake-up interrupt*/
00206   SPI_IT_OVR    = (uint8_t)0x65,  /*!< Overrun interrupt*/
00207   SPI_IT_MODF   = (uint8_t)0x55, /*!< Mode fault interrupt*/
00208   SPI_IT_CRCERR = (uint8_t)0x45, /*!< CRC error interrupt*/
00209   SPI_IT_TXE    = (uint8_t)0x17, /*!< Transmit buffer empty interrupt*/
00210   SPI_IT_RXNE   = (uint8_t)0x06, /*!< Receive buffer not empty interrupt*/
00211   SPI_IT_ERR    = (uint8_t)0x05  /*!< Error interrupt*/
00212 } SPI_IT_TypeDef;
00213 
00214 /**
00215   * @}
00216   */
00217   
00218 /**
00219   * @}
00220   */
00221 
00222 /* Exported constants --------------------------------------------------------*/
00223 /* Exported macros -----------------------------------------------------------*/
00224 
00225 /** @defgroup SPI_Exported_Macros
00226   * @brief Macros used by the assert_param function to check the different functions parameters.
00227   * @{
00228   */
00229 
00230 /**
00231   * @brief Macro used by the assert_param function in order to check the data direction mode values
00232   */
00233 #define IS_SPI_DATA_DIRECTION(Mode) (((Mode) == SPI_Direction_2Lines_FullDuplex) || \
00234                                      ((Mode) == SPI_Direction_2Lines_RxOnly) || \
00235                                      ((Mode) == SPI_Direction_1Line_Rx) || \
00236                                      ((Mode) == SPI_Direction_1Line_Tx))
00237 
00238 /**
00239   * @brief Macro used by the assert_param function in order to check the mode half duplex data direction values
00240   */
00241 #define IS_SPI_DIRECTION(Direction) (((Direction) == SPI_Direction_Rx) || \
00242                                      ((Direction) == SPI_Direction_Tx))
00243 
00244 /**
00245   * @brief Macro used by the assert_param function in order to check the NSS management values
00246   */
00247 #define IS_SPI_SLAVEMANAGEMENT(NSS) (((NSS) == SPI_NSS_Soft) || \
00248                                      ((NSS) == SPI_NSS_Hard))
00249 
00250 
00251 /**
00252   * @brief Macro used by the assert_param function in order to check the different
00253   *  sensitivity values for the CRC polynomial
00254   */
00255 #define IS_SPI_CRC_POLYNOMIAL(Polynomial) ((Polynomial) > (uint8_t)0x00)
00256 
00257 /**
00258   * @brief Macro used by the assert_param function in order to check the SPI Mode values
00259   */
00260 #define IS_SPI_MODE(Mode) (((Mode) == SPI_Mode_Master) || \
00261                            ((Mode) == SPI_Mode_Slave))
00262 
00263 /**
00264   * @brief Macro used by the assert_param function in order to check the baudrate values
00265   */
00266 #define IS_SPI_BAUDRATE_PRESCALER(Prescaler) (((Prescaler) == SPI_BaudRatePrescaler_2) || \
00267                                               ((Prescaler) == SPI_BaudRatePrescaler_4) || \
00268                                               ((Prescaler) == SPI_BaudRatePrescaler_8) || \
00269                                               ((Prescaler) == SPI_BaudRatePrescaler_16) || \
00270                                               ((Prescaler) == SPI_BaudRatePrescaler_32) || \
00271                                               ((Prescaler) == SPI_BaudRatePrescaler_64) || \
00272                                               ((Prescaler) == SPI_BaudRatePrescaler_128) || \
00273                                               ((Prescaler) == SPI_BaudRatePrescaler_256))
00274 
00275 /**
00276   * @brief Macro used by the assert_param function in order to check the polarity values
00277   */
00278 #define IS_SPI_POLARITY(ClkPol) (((ClkPol) == SPI_CPOL_Low) || \
00279                                  ((ClkPol) == SPI_CPOL_High))
00280 
00281 /**
00282   * @brief Macro used by the assert_param function in order to check the phase values
00283   */
00284 #define IS_SPI_PHASE(ClkPha) (((ClkPha) == SPI_CPHA_1Edge) || \
00285                               ((ClkPha) == SPI_CPHA_2Edge))
00286 
00287 /**
00288   * @brief Macro used by the assert_param function in order to check the first bit
00289   *   to be transmitted values
00290   */
00291 #define IS_SPI_FIRSTBIT(Bit) (((Bit) == SPI_FirstBit_MSB) || \
00292                               ((Bit) == SPI_FirstBit_LSB))
00293 
00294 /**
00295   * @brief Macro used by the assert_param function in order to check the CRC Transmit/Receive
00296   */
00297 #define IS_SPI_CRC(CRC) (((CRC) == SPI_CRC_TX) || \
00298                          ((CRC) == SPI_CRC_RX))
00299 
00300 /**
00301   * @brief Macro used by the assert_param function in order to check the DMA transfer requests
00302   */
00303 #define IS_SPI_DMAREQ(DMAREQ) ((((DMAREQ) & (uint16_t)0xFC) == 0x00) && ((DMAREQ) != 0x00))
00304 
00305 /**
00306   * @brief Macro used by the assert_param function in order to check the different flags values
00307   */
00308 #define IS_SPI_FLAG(Flag) (((Flag) == SPI_FLAG_OVR) || \
00309                            ((Flag) == SPI_FLAG_MODF) || \
00310                            ((Flag) == SPI_FLAG_CRCERR) || \
00311                            ((Flag) == SPI_FLAG_WKUP) || \
00312                            ((Flag) == SPI_FLAG_TXE) || \
00313                            ((Flag) == SPI_FLAG_RXNE) || \
00314                            ((Flag) == SPI_FLAG_BSY))
00315 
00316 /**
00317   * @brief Macro used by the assert_param function in order to check the different
00318   *   sensitivity values for the flag that can be cleared by writing 0
00319   */
00320 #define IS_SPI_CLEAR_FLAG(Flag) (((Flag) == SPI_FLAG_CRCERR) || \
00321                                  ((Flag) == SPI_FLAG_WKUP))
00322 
00323 /**
00324   * @brief Macro used by the assert_param function in order to check the different
00325   *   sensitivity values for the Interrupts
00326   */
00327 #define IS_SPI_CONFIG_IT(Interrupt) (((Interrupt) == SPI_IT_TXE)  || \
00328                                      ((Interrupt) == SPI_IT_RXNE)  || \
00329                                      ((Interrupt) == SPI_IT_ERR) || \
00330                                      ((Interrupt) == SPI_IT_WKUP))
00331 
00332 /**
00333   * @brief Macro used by the assert_param function in order to check the different
00334   *   sensitivity values for the pending bit
00335   */
00336 #define IS_SPI_GET_IT(ITPendingBit) (((ITPendingBit) == SPI_IT_OVR)  || \
00337                                      ((ITPendingBit) == SPI_IT_MODF) || \
00338                                      ((ITPendingBit) == SPI_IT_CRCERR) || \
00339                                      ((ITPendingBit) == SPI_IT_WKUP) || \
00340                                      ((ITPendingBit) == SPI_IT_TXE)  || \
00341                                      ((ITPendingBit) == SPI_IT_RXNE))
00342 
00343 /**
00344   * @brief Macro used by the assert_param function in order to check the different
00345   *   sensitivity values for the pending bit that can be cleared by writing 0
00346   */
00347 #define IS_SPI_CLEAR_IT(ITPendingBit) (((ITPendingBit) == SPI_IT_CRCERR) || \
00348                                        ((ITPendingBit) == SPI_IT_WKUP))
00349 
00350 /**
00351   * @}
00352   */
00353 
00354 /* Exported functions ------------------------------------------------------- */
00355 /* Function used to set the SPI configuration to the default reset state *****/ 
00356 void SPI_DeInit(SPI_TypeDef* SPIx);
00357 /* Initialization and Configuration functions *********************************/
00358 void SPI_Init(SPI_TypeDef* SPIx, SPI_FirstBit_TypeDef SPI_FirstBit,
00359               SPI_BaudRatePrescaler_TypeDef SPI_BaudRatePrescaler,
00360               SPI_Mode_TypeDef SPI_Mode, SPI_CPOL_TypeDef SPI_CPOL,
00361               SPI_CPHA_TypeDef SPI_CPHA, SPI_DirectionMode_TypeDef SPI_Data_Direction,
00362               SPI_NSS_TypeDef SPI_Slave_Management, uint8_t CRCPolynomial);
00363 void SPI_Cmd(SPI_TypeDef* SPIx, FunctionalState NewState);
00364 void SPI_NSSInternalSoftwareCmd(SPI_TypeDef* SPIx, FunctionalState NewState);
00365 void SPI_BiDirectionalLineConfig(SPI_TypeDef* SPIx, SPI_Direction_TypeDef SPI_Direction);
00366 
00367 /* Data transfers functions ***************************************************/
00368 void SPI_SendData(SPI_TypeDef* SPIx, uint8_t Data);
00369 uint8_t SPI_ReceiveData(SPI_TypeDef* SPIx);
00370 
00371 /* Hardware CRC Calculation functions *****************************************/
00372 void SPI_TransmitCRC(SPI_TypeDef* SPIx);
00373 void SPI_CalculateCRCCmd(SPI_TypeDef* SPIx, FunctionalState NewState);
00374 uint8_t SPI_GetCRC(SPI_TypeDef* SPIx, SPI_CRC_TypeDef SPI_CRC);
00375 void SPI_ResetCRC(SPI_TypeDef* SPIx);
00376 uint8_t SPI_GetCRCPolynomial(SPI_TypeDef* SPIx);
00377 
00378 /* DMA transfer management functions *****************************************/
00379 void SPI_DMACmd(SPI_TypeDef* SPIx, SPI_DMAReq_TypeDef SPI_DMAReq, FunctionalState NewState);
00380 
00381 /* Interrupts and flags management functions **********************************/
00382 void SPI_ITConfig(SPI_TypeDef* SPIx, SPI_IT_TypeDef SPI_IT, FunctionalState NewState);
00383 FlagStatus SPI_GetFlagStatus(SPI_TypeDef* SPIx, SPI_FLAG_TypeDef SPI_FLAG);
00384 void SPI_ClearFlag(SPI_TypeDef* SPIx, SPI_FLAG_TypeDef SPI_FLAG);
00385 ITStatus SPI_GetITStatus(SPI_TypeDef* SPIx, SPI_IT_TypeDef SPI_IT);
00386 void SPI_ClearITPendingBit(SPI_TypeDef* SPIx, SPI_IT_TypeDef SPI_IT);
00387 
00388 #endif /* __STM8L15x_SPI_H */
00389 
00390 /**
00391   * @}
00392   */
00393   
00394 /**
00395   * @}
00396   */
00397 
00398 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
STM8S Firmware Library: Overview

 

 

 

For complete documentation on STM8L15x 8-bit microcontrollers platform visit www.st.com