1 /****************************************************************************** 2 * 3 * @brief header file for SPI module utilities (SPI). 4 * 5 ******************************************************************************* 6 * 7 * provide APIs for accessing SPI module (SPI) 8 ******************************************************************************/ 9 10 #ifndef SPI_H_ 11 #define SPI_H_ 12 #ifdef __cplusplus 13 extern "C" { 14 #endif 15 /****************************************************************************** 16 * Includes 17 ******************************************************************************/ 18 19 /****************************************************************************** 20 * Constants 21 ******************************************************************************/ 22 23 /****************************************************************************** 24 * Macros 25 ******************************************************************************/ 26 /* maximum number of SPIs */ 27 #define MAX_SPI_NO 2 28 29 30 31 /****************************************************************************** 32 * define SPI register default value 33 * 34 *//*! @addtogroup spi_default_value 35 * @{ 36 *******************************************************************************/ 37 38 #define SPI_C1_DEFAULT 0x04 /*!< SPI C1 register */ 39 #define SPI_C2_DEFAULT 0x00 /*!< SPI C2 register */ 40 #define SPI_BR_DEFAULT 0x00 /*!< SPI BR register */ 41 #define SPI_S_DEFAULT 0x20 /*!< SPI S register */ 42 #define SPI_M_DEFAULT 0x00 /*!< SPI M register */ 43 /*! @} End of spi_default_value */ 44 45 /****************************************************************************** 46 * define SPI error status 47 * 48 *//*! @addtogroup spi_error_list 49 * @{ 50 *******************************************************************************/ 51 52 #define SPI_ERR_SUCCESS 0 /*!< success */ 53 #define SPI_ERR_CODE_BASE ((uint32)SPI0 - 0x40000000L) /*!< error code base for SPI */ 54 #define SPI_ERR_TXBUF_NOT_EMPTY (SPI_ERR_CODE_BASE+1) /*!< failure due to SPTEF (empty) not set */ 55 #define SPI_ERR_RXBUF_NOT_FULL (SPI_ERR_CODE_BASE+2) /*!< failure due to SPRF (full) not set */ 56 /*! @} End of spi_error_list */ 57 58 /****************************************************************************** 59 * Types 60 ******************************************************************************/ 61 62 typedef uint8_t SPI_WidthType; /* SPI width type */ 63 typedef uint32_t ResultType; /* SPI routine Result code */ 64 65 /****************************************************************************** 66 * define SPI call back funtion 67 * 68 *//*! @addtogroup spi_callback 69 * @{ 70 *******************************************************************************/ 71 typedef void (*SPI_CallbackType)(void); /*!< SPI call back function */ 72 /*! @} End of spi_callback */ 73 74 /****************************************************************************** 75 * 76 *//*! @addtogroup spi_setting_type 77 * @{ 78 *******************************************************************************/ 79 /*! 80 * @brief SPI setting type. 81 * 82 */ 83 typedef struct 84 { 85 uint32_t bIntEn : 1; /*!< 1: Interrupt Enable, 0: Interrupt disable */ 86 uint32_t bModuleEn : 1; /*!< 1: SPI module Enable, 0: SPI module disable */ 87 uint32_t bTxIntEn : 1; /*!< 1: Tx Interrupt Enable, 0: Tx Interrupt disable */ 88 uint32_t bMasterMode : 1; /*!< 1: Master mode, 0: Slave mode */ 89 uint32_t bClkPolarityLow : 1; /*!< 1: Active-low SPI clock, 0: Active-HIgh SPI clock */ 90 uint32_t bClkPhase1 : 1; /*!< Set clock phase */ 91 uint32_t bMasterAutoDriveSS : 1; /*!< Slave select output enable */ 92 uint32_t bShiftLSBFirst : 1; /*!< 1: LSB first, 0: MSB first */ 93 uint32_t bMatchIntEn : 1; /*!< 1: Match interrupt Enable, 0: Match interrupt disable */ 94 uint32_t bModeFaultEn : 1; /*!< Master mode-fault function enable */ 95 uint32_t bBidirectionModeEn : 1; /*!< Bidirectional mode output enable */ 96 uint32_t bPinAsOuput : 1; /*!< enables bidirectional pin configurations */ 97 uint32_t bStopInWaitMode : 1; /*!< SPI stop in wait mode */ 98 uint32_t bRsvd : 19; 99 } SPI_SettingType; 100 /*! @} End of spi_setting_type */ 101 102 /****************************************************************************** 103 * 104 *//*! @addtogroup spi_config_type 105 * @{ 106 *******************************************************************************/ 107 /*! 108 * @brief SPI configuration type. 109 * 110 */ 111 typedef struct 112 { 113 SPI_SettingType sSettings; /*!< SPI settings */ 114 uint32_t u32BitRate; /*!< set baud rate */ 115 uint32_t u32BusClkHz; /*!< input bus clock */ 116 } SPI_ConfigType; /*!< SPI configuration structure */ 117 /*! @} End of spi_config_type */ 118 119 /****************************************************************************** 120 * Global variables 121 ******************************************************************************/ 122 123 /****************************************************************************** 124 * inline function 125 ******************************************************************************/ 126 /****************************************************************************** 127 * 128 *//*! @addtogroup spi_api_list 129 * @{ 130 *******************************************************************************/ 131 /*****************************************************************************//*! 132 * 133 * @brief LSB first (shifter direction). 134 * 135 * @param[in] pSPI point to SPI module type. 136 * 137 * @return none. 138 * 139 * @ Pass/ Fail criteria: none. 140 *****************************************************************************/ 141 SPI_SetLSBFirst(SPI_Type * pSPI)142 __STATIC_INLINE void SPI_SetLSBFirst(SPI_Type *pSPI) 143 { 144 pSPI->C1 |= SPI_C1_LSBFE_MASK; 145 } 146 /*****************************************************************************//*! 147 * 148 * @brief MSB first (shifter direction). 149 * 150 * @param[in] pSPI point to SPI module type. 151 * 152 * @return none. 153 * 154 * @ Pass/ Fail criteria: none. 155 *****************************************************************************/ 156 SPI_SetMSBFirst(SPI_Type * pSPI)157 __STATIC_INLINE void SPI_SetMSBFirst(SPI_Type *pSPI) 158 { 159 pSPI->C1 &= ~SPI_C1_LSBFE_MASK; 160 } 161 /*****************************************************************************//*! 162 * 163 * @brief set SPI clock polarity. 164 * 165 * @param[in] pSPI point to SPI module type. 166 * @param[in] u8PolLow set clock polarity, 1 - Active-low SPI clock (idles high). 167 * @return none 168 * 169 * @ Pass/ Fail criteria: none 170 *****************************************************************************/ 171 SPI_SetClockPol(SPI_Type * pSPI,uint8_t u8PolLow)172 __STATIC_INLINE void SPI_SetClockPol(SPI_Type *pSPI,uint8_t u8PolLow) 173 { 174 if( u8PolLow ) 175 { 176 pSPI->C1 |= SPI_C1_CPOL_MASK; 177 } 178 else 179 { 180 pSPI->C1 &= ~SPI_C1_CPOL_MASK; 181 } 182 } 183 /*****************************************************************************//*! 184 * 185 * @brief set SPI clock phase. 186 * 187 * @param[in] pSPI point to SPI module type. 188 * @param[in] u8Phase set clock phase, 1 - First edge on SPSCK occurs at the start of the first cycle of a data transfer. 189 * 190 * @return none 191 * 192 * @ Pass/ Fail criteria: none 193 *****************************************************************************/ 194 SPI_SetClockPhase(SPI_Type * pSPI,uint8_t u8Phase)195 __STATIC_INLINE void SPI_SetClockPhase(SPI_Type *pSPI,uint8_t u8Phase) 196 { 197 if( u8Phase ) 198 { 199 pSPI->C1 |= SPI_C1_CPHA_MASK; 200 } 201 else 202 { 203 pSPI->C1 &= ~SPI_C1_CPHA_MASK; 204 } 205 } 206 /*****************************************************************************//*! 207 * 208 * @brief enable SPI module. 209 * 210 * @param[in] pSPI point to SPI module type. 211 * 212 * @return none 213 * 214 * @ Pass/ Fail criteria: none 215 *****************************************************************************/ 216 SPI_Enable(SPI_Type * pSPI)217 __STATIC_INLINE void SPI_Enable(SPI_Type *pSPI) 218 { 219 pSPI->C1 |= SPI_C1_SPE_MASK; 220 } 221 /*****************************************************************************//*! 222 * 223 * @brief disable SPI module. 224 * 225 * @param[in] pSPI point to SPI module type. 226 * 227 * @return none 228 * 229 * @ Pass/ Fail criteria: none 230 *****************************************************************************/ 231 SPI_Disable(SPI_Type * pSPI)232 __STATIC_INLINE void SPI_Disable(SPI_Type *pSPI) 233 { 234 pSPI->C1 &= ~SPI_C1_SPE_MASK; 235 } 236 /*****************************************************************************//*! 237 * 238 * @brief enable SPI interrupt. 239 * 240 * @param[in] pSPI point to SPI module type. 241 * 242 * @return none 243 * 244 * @ Pass/ Fail criteria: none 245 *****************************************************************************/ 246 SPI_IntEnable(SPI_Type * pSPI)247 __STATIC_INLINE void SPI_IntEnable(SPI_Type *pSPI) 248 { 249 pSPI->C1 |= SPI_C1_SPIE_MASK; 250 } 251 /*****************************************************************************//*! 252 * 253 * @brief disable SPI interrupt. 254 * 255 * @param[in] pSPI point to SPI module type. 256 * 257 * @return none 258 * 259 * @ Pass/ Fail criteria: none 260 *****************************************************************************/ SPI_IntDisable(SPI_Type * pSPI)261 __STATIC_INLINE void SPI_IntDisable(SPI_Type *pSPI) 262 { 263 pSPI->C1 &= ~SPI_C1_SPIE_MASK; 264 } 265 /*****************************************************************************//*! 266 * 267 * @brief set SPI to master mode. 268 * 269 * @param[in] pSPI point to SPI module type. 270 * 271 * @return none 272 * 273 * @ Pass/ Fail criteria: none 274 *****************************************************************************/ SPI_SetMasterMode(SPI_Type * pSPI)275 __STATIC_INLINE void SPI_SetMasterMode(SPI_Type *pSPI) 276 { 277 pSPI->C1 |= SPI_C1_MSTR_MASK; 278 } 279 /*****************************************************************************//*! 280 * 281 * @brief set SPI to slave mode. 282 * 283 * @param[in] pSPI point to SPI module type. 284 * 285 * @return none 286 * 287 * @ Pass/ Fail criteria: none 288 *****************************************************************************/ SPI_SetSlaveMode(SPI_Type * pSPI)289 __STATIC_INLINE void SPI_SetSlaveMode(SPI_Type *pSPI) 290 { 291 pSPI->C1 &= ~SPI_C1_MSTR_MASK; 292 } 293 /*****************************************************************************//*! 294 * 295 * @brief SPI transmit interrupt enable. 296 * 297 * @param[in] pSPI point to SPI module type. 298 * 299 * @return none. 300 * 301 * @ Pass/ Fail criteria: none. 302 *****************************************************************************/ SPI_TxIntEnable(SPI_Type * pSPI)303 __STATIC_INLINE void SPI_TxIntEnable(SPI_Type *pSPI) 304 { 305 pSPI->C1 |= SPI_C1_SPTIE_MASK; 306 } 307 /*****************************************************************************//*! 308 * 309 * @brief SPI transmit interrupt disable. 310 * 311 * @param[in] pSPI point to SPI module type. 312 * 313 * @return none 314 * 315 * @ Pass/ Fail criteria: none 316 *****************************************************************************/ SPI_TxIntDisable(SPI_Type * pSPI)317 __STATIC_INLINE void SPI_TxIntDisable(SPI_Type *pSPI) 318 { 319 pSPI->C1 &= ~SPI_C1_SPTIE_MASK; 320 } 321 /*****************************************************************************//*! 322 * 323 * @brief Slave select output enable. 324 * 325 * @param[in] pSPI point to SPI module type. 326 * 327 * @return none 328 * 329 * @ Pass/ Fail criteria: none 330 *****************************************************************************/ SPI_SSOutputEnable(SPI_Type * pSPI)331 __STATIC_INLINE void SPI_SSOutputEnable(SPI_Type *pSPI ) 332 { 333 pSPI->C1 |= SPI_C1_SSOE_MASK; 334 } 335 /*****************************************************************************//*! 336 * 337 * @brief Slave select output disable. 338 * 339 * @param[in] pSPI point to SPI module type. 340 * 341 * @return none 342 * 343 * @ Pass/ Fail criteria: none 344 *****************************************************************************/ SPI_SSOutputDisable(SPI_Type * pSPI)345 __STATIC_INLINE void SPI_SSOutputDisable(SPI_Type *pSPI ) 346 { 347 pSPI->C1 &= ~SPI_C1_SSOE_MASK; 348 } 349 /*****************************************************************************//*! 350 * 351 * @brief SPI match interrupt enable. 352 * 353 * @param[in] pSPI point to SPI module type. 354 * 355 * @return none 356 * 357 * @ Pass/ Fail criteria: none 358 *****************************************************************************/ SPI_MatchIntEnable(SPI_Type * pSPI)359 __STATIC_INLINE void SPI_MatchIntEnable(SPI_Type *pSPI ) 360 { 361 pSPI->C2 |= SPI_C2_SPMIE_MASK; 362 } 363 /*****************************************************************************//*! 364 * 365 * @brief SPI match interrupt disable. 366 * 367 * @param[in] pSPI point to SPI module type. 368 * 369 * @return none. 370 * 371 * @ Pass/ Fail criteria: none. 372 *****************************************************************************/ SPI_MatchIntDisable(SPI_Type * pSPI)373 __STATIC_INLINE void SPI_MatchIntDisable(SPI_Type *pSPI ) 374 { 375 pSPI->C2 &= ~SPI_C2_SPMIE_MASK; 376 } 377 /*****************************************************************************//*! 378 * 379 * @brief Master mode-fault function disable. 380 * 381 * @param[in] pSPI point to SPI module type. 382 * 383 * @return none. 384 * 385 * @ Pass/ Fail criteria: none. 386 *****************************************************************************/ SPI_ModfDisable(SPI_Type * pSPI)387 __STATIC_INLINE void SPI_ModfDisable(SPI_Type *pSPI ) 388 { 389 pSPI->C2 &= ~SPI_C2_MODFEN_MASK; 390 } 391 /*****************************************************************************//*! 392 393 * 394 * @brief Master mode-fault function enable. 395 * 396 * @param[in] pSPI point to SPI module type. 397 * 398 * @return none. 399 * 400 * @ Pass/ Fail criteria: none. 401 *****************************************************************************/ SPI_ModfEnable(SPI_Type * pSPI)402 __STATIC_INLINE void SPI_ModfEnable(SPI_Type *pSPI ) 403 { 404 pSPI->C2 |= SPI_C2_MODFEN_MASK; 405 } 406 /*****************************************************************************//*! 407 * 408 * @brief Bidirectional mode output enable. 409 * 410 * @param[in] pSPI point to SPI module type. 411 * 412 * @return none. 413 * 414 * @ Pass/ Fail criteria: none. 415 *****************************************************************************/ SPI_BidirOutEnable(SPI_Type * pSPI)416 __STATIC_INLINE void SPI_BidirOutEnable(SPI_Type *pSPI ) 417 { 418 pSPI->C2 |= SPI_C2_BIDIROE_MASK; 419 } 420 /*****************************************************************************//*! 421 * 422 * @brief Bidirectional mode output disable. 423 * 424 * @param[in] pSPI point to SPI module type. 425 * 426 * @return none. 427 * 428 * @ Pass/ Fail criteria: none. 429 *****************************************************************************/ SPI_BidirOutDisable(SPI_Type * pSPI)430 __STATIC_INLINE void SPI_BidirOutDisable(SPI_Type *pSPI ) 431 { 432 pSPI->C2 &= ~SPI_C2_BIDIROE_MASK; 433 } 434 /*****************************************************************************//*! 435 * 436 * @brief SPI stop in wait mode 437 * 438 * @param[in] pSPI point to SPI module type. 439 * 440 * @return none. 441 * 442 * @ Pass/ Fail criteria: none. 443 *****************************************************************************/ SPI_ClockStopDisable(SPI_Type * pSPI)444 __STATIC_INLINE void SPI_ClockStopDisable(SPI_Type *pSPI ) 445 { 446 pSPI->C2 &= ~SPI_C2_SPISWAI_MASK; 447 } 448 /*****************************************************************************//*! 449 * 450 * @brief SPI stop in wait mode. 451 * 452 * @param[in] pSPI point to SPI module type. 453 * 454 * @return none 455 * 456 * @ Pass/ Fail criteria: none 457 *****************************************************************************/ SPI_ClockStopEnable(SPI_Type * pSPI)458 __STATIC_INLINE void SPI_ClockStopEnable(SPI_Type *pSPI ) 459 { 460 pSPI->C2 |= SPI_C2_SPISWAI_MASK; 461 } 462 /*****************************************************************************//*! 463 * 464 * @brief enables bidirectional pin configurations. 465 * 466 * @param[in] pSPI point to SPI module type. 467 * 468 * @return none 469 * 470 * @ Pass/ Fail criteria: none 471 *****************************************************************************/ SPI_BidirPinEnable(SPI_Type * pSPI)472 __STATIC_INLINE void SPI_BidirPinEnable(SPI_Type *pSPI) 473 { 474 pSPI->C2 |= SPI_C2_SPC0_MASK; 475 } 476 /*****************************************************************************//*! 477 * 478 * @brief enables bidirectional pin configurations. 479 * 480 * @param[in] pSPI point to SPI module type. 481 * 482 * @return none 483 * 484 * @ Pass/ Fail criteria: none 485 *****************************************************************************/ SPI_BidirPinDisable(SPI_Type * pSPI)486 __STATIC_INLINE void SPI_BidirPinDisable(SPI_Type *pSPI) 487 { 488 pSPI->C2 &= ~SPI_C2_SPC0_MASK; 489 } 490 /*****************************************************************************//*! 491 * 492 * @brief check SPI read buffer full flag. 493 * 494 * @param[in] pSPI point to SPI module type. 495 * 496 * @return TRUE or FALSE. 497 * 498 * @ Pass/ Fail criteria: none. 499 *****************************************************************************/ SPI_IsSPRF(SPI_Type * pSPI)500 __STATIC_INLINE uint8_t SPI_IsSPRF(SPI_Type *pSPI ) 501 { 502 return(pSPI->S & SPI_S_SPRF_MASK); 503 } 504 /*****************************************************************************//*! 505 * 506 * @brief check SPI match flag. 507 * 508 * @param[in] pSPI point to SPI module type. 509 * 510 * @return TRUE or FALSE. 511 * 512 * @ Pass/ Fail criteria: none. 513 *****************************************************************************/ SPI_IsSPMF(SPI_Type * pSPI)514 __STATIC_INLINE uint8_t SPI_IsSPMF(SPI_Type *pSPI ) 515 { 516 return(pSPI->S & SPI_S_SPMF_MASK); 517 } 518 /*****************************************************************************//*! 519 * 520 * @brief check SPI transmit buffer empty flag. 521 * 522 * @param[in] pSPI point to SPI module type. 523 * 524 * @return TRUE or FALSE. 525 * 526 * @ Pass/ Fail criteria: none 527 *****************************************************************************/ SPI_IsSPTEF(SPI_Type * pSPI)528 __STATIC_INLINE uint8_t SPI_IsSPTEF(SPI_Type *pSPI ) 529 { 530 return(pSPI->S & SPI_S_SPTEF_MASK); 531 } 532 /*****************************************************************************//*! 533 * 534 * @brief check master mode fault flag. 535 * 536 * @param[in] pSPI point to SPI module type. 537 * 538 * @return TRUE or FALSE. 539 * 540 * @ Pass/ Fail criteria: none 541 *****************************************************************************/ SPI_IsMODF(SPI_Type * pSPI)542 __STATIC_INLINE uint8_t SPI_IsMODF(SPI_Type *pSPI ) 543 { 544 return(pSPI->S & SPI_S_MODF_MASK); 545 } 546 /*****************************************************************************//*! 547 * 548 * @brief read SPI data register. 549 * 550 * @param[in] pSPI point to SPI module type. 551 * 552 * @return data register value 553 * 554 * @ Pass/ Fail criteria: none 555 *****************************************************************************/ SPI_ReadDataReg(SPI_Type * pSPI)556 __STATIC_INLINE uint8_t SPI_ReadDataReg(SPI_Type *pSPI ) 557 { 558 return pSPI->D; 559 } 560 /*****************************************************************************//*! 561 * 562 * @brief write SPI data register. 563 * 564 * @param[in] pSPI point to SPI module type. 565 * @param[in] u8WrBuff data buffer write to spi data register. 566 * 567 * @return none 568 * 569 * @ Pass/ Fail criteria: none 570 *****************************************************************************/ SPI_WriteDataReg(SPI_Type * pSPI,uint8_t u8WrBuff)571 __STATIC_INLINE void SPI_WriteDataReg(SPI_Type *pSPI, uint8_t u8WrBuff ) 572 { 573 pSPI->D = u8WrBuff; 574 } 575 /*****************************************************************************//*! 576 * 577 * @brief write SPI match register. 578 * 579 * @param[in] pSPI point to SPI module type. 580 * @param[in] u8WrBuff the data buffer write to match register. 581 * 582 * @return none 583 * 584 * @ Pass/ Fail criteria: none 585 *****************************************************************************/ SPI_WriteMatchValue(SPI_Type * pSPI,uint8_t u8WrBuff)586 __STATIC_INLINE void SPI_WriteMatchValue(SPI_Type *pSPI, uint8_t u8WrBuff ) 587 { 588 pSPI->M = u8WrBuff; 589 } 590 /****************************************************************************** 591 * Global functions 592 ******************************************************************************/ 593 void SPI_Enable(SPI_Type *pSPI); 594 void SPI_Disable(SPI_Type *pSPI); 595 void SPI_SetLSBFirst(SPI_Type *pSPI); 596 void SPI_SetMSBFirst(SPI_Type *pSPI); 597 void SPI_IntEnable(SPI_Type *pSPI); 598 void SPI_IntDisable(SPI_Type *pSPI); 599 void SPI_SetMasterMode(SPI_Type *pSPI); 600 void SPI_SetSlaveMode(SPI_Type *pSPI); 601 void SPI_TxIntEnable(SPI_Type *pSPI); 602 void SPI_TxIntDisable(SPI_Type *pSPI); 603 void SPI_SSOutputEnable(SPI_Type *pSPI ); 604 void SPI_SSOutputDisable(SPI_Type *pSPI ); 605 void SPI_MatchIntEnable(SPI_Type *pSPI ); 606 void SPI_MatchIntDisable(SPI_Type *pSPI ); 607 void SPI_ModfDisable(SPI_Type *pSPI ); 608 void SPI_ModfEnable(SPI_Type *pSPI ); 609 void SPI_BidirOutEnable(SPI_Type *pSPI ); 610 void SPI_BidirOutDisable(SPI_Type *pSPI ); 611 void SPI_ClockStopDisable(SPI_Type *pSPI ); 612 void SPI_ClockStopEnable(SPI_Type *pSPI ); 613 void SPI_BidirPinEnable(SPI_Type *pSPI ); 614 void SPI_BidirPinDisable(SPI_Type *pSPI ); 615 void SPI_SetClockPol(SPI_Type *pSPI,uint8_t u8PolLow); 616 void SPI_SetClockPhase(SPI_Type *pSPI,uint8_t u8Phase); 617 void SPI_SetBaudRate(SPI_Type *pSPI,uint32_t u32BusClock,uint32_t u32Bps ); 618 uint8_t SPI_IsSPRF(SPI_Type *pSPI ); 619 uint8_t SPI_IsSPMF(SPI_Type *pSPI ); 620 uint8_t SPI_IsSPTEF(SPI_Type *pSPI ); 621 uint8_t SPI_IsMODF(SPI_Type *pSPI ); 622 uint8_t SPI_ReadDataReg(SPI_Type *pSPI ); 623 void SPI_WriteDataReg(SPI_Type *pSPI, uint8_t u8WrBuff ); 624 void SPI_WriteMatchValue(SPI_Type *pSPI, uint8_t u8WrBuff ); 625 void SPI_Init(SPI_Type *pSPI, SPI_ConfigType *pConfig); 626 void SPI_DeInit(SPI_Type *pSPI); 627 ResultType SPI_TransferWait(SPI_Type *pSPI, SPI_WidthType* pRdBuff, SPI_WidthType *pWrBuff,uint32 uiLength); 628 void SPI_SetCallback(SPI_Type *pSPI,SPI_CallbackType pfnCallback); 629 630 /*! @} End of spi_api_list */ 631 #ifdef __cplusplus 632 } 633 #endif 634 #endif /* SPI_H_ */ 635