yc11xx_spi.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright 2016, yichip Semiconductor(shenzhen office)
  3. * All Rights Reserved.
  4. *
  5. * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Yichip Semiconductor;
  6. * the contents of this file may not be disclosed to third parties, copied
  7. * or duplicated in any form, in whole or in part, without the prior
  8. * written permission of Yichip Semiconductor.
  9. */
  10. /** @file
  11. *
  12. * spi support for application
  13. */
  14. #ifndef _SPI_H_
  15. #define _SPI_H_
  16. #include <stdio.h>
  17. #include <stdint.h>
  18. #include <stdbool.h>
  19. //#include "yc11xx.h"
  20. #include "yc_drv_common.h"
  21. /** @
  22. * @defgroup SPI_Mode
  23. */
  24. #define SPI_Mode_Four_Wire 4
  25. #define SPI_Mode_Three_Wire 3
  26. #define SPI_Mode_Two_Wire 2
  27. #define IS_SPI_MODE(MODE) (((MODE) == SPI_Mode_Two_Wire) || \
  28. ((MODE) == SPI_Mode_Three_Wire)) || \
  29. ((MODE) == SPI_Mode_Four_Wire)
  30. /** @
  31. * @defgroup SPI_CPOL
  32. */
  33. #define SPI_CPOL_Active_Low ((uint8_t)0<<4)
  34. #define SPI_CPOL_Active_High ((uint8_t)1<<4)
  35. #define IS_SPI_CPOL(CPOL) (((CPOL) == SPI_CPOL_Active_High) || \
  36. ((CPOL) == SPI_CPOL_Active_Low))
  37. /** @
  38. * @defgroup SPI_CPHA
  39. */
  40. #define SPI_CPHA_First_Edge ((uint8_t)0<<5)
  41. #define SPI_CPHA_Second_Edge ((uint8_t)1<<5)
  42. #define IS_SPI_CPHA(CPHA) (((CPHA) == SPI_CPHA_First_Edge) || \
  43. ((CPHA) == SPI_CPHA_Second_Edge))
  44. /** @
  45. * @defgroup SPI_BaudSpeed
  46. */
  47. #define SPI_BAUDSPEED_2MHz 4
  48. #define SPI_BAUDSPEED_750KHz 5
  49. #define SPI_BAUDSPEED_375KHz 6
  50. #define SPI_BAUDSPEED_187KHz 7
  51. #define IS_SPI_BAUDSPEED(BAUDSPEED) (((BAUDSPEED) >=SPI_BAUDSPEED_2MHz) && ((BAUDSPEED) <= SPI_BAUDSPEED_187KHz))
  52. /** @defgroup SPI_TXLen
  53. * @{
  54. */
  55. #define IS_SPI_TXLen(TxLen) (TxLen != 0)
  56. /**
  57. * @}
  58. */
  59. /** @defgroup SPI_RXlen
  60. * @{
  61. */
  62. #define IS_SPI_RXLen(RxLen) (RxLen != 0)
  63. /**
  64. * @}
  65. */
  66. /**
  67. *@brief SPI initialized structure definition.
  68. */
  69. typedef struct
  70. {
  71. uint16_t SPI_Mode; /*!< Specifies the SPI operating mode.
  72. This parameter can be a value of @ref SPI_mode */
  73. uint16_t SPI_CPOL; /*!< Specifies the serial clock steady state.
  74. This parameter can be a value of @ref SPI_Clock_Polarity */
  75. uint16_t SPI_CPHA; /*!< Specifies the clock active edge for the bit capture.
  76. This parameter can be a value of @ref SPI_Clock_Phase */
  77. uint16_t SPI_BaudSpeed; /*!< Specifies the Baud Rate prescaler value which will be
  78. used to configure the transmit and receive SCK clock.
  79. This parameter can be a value of @ref SPI_BaudRate_Prescaler.
  80. @note The communication clock is derived from the master
  81. clock. The slave clock does not need to be set. */
  82. uint16_t SPI_TXLen; /*!< Specifies Tx DMA buff length */
  83. uint16_t SPI_RXlen; /*!< Specifies Rx DMA buff length */
  84. }SPI_InitTypeDef;
  85. /**
  86. *@brief SPI initialization.
  87. *@param SPI_InitStruct SPI initializing structure.@ref SPI_InitTypeDef
  88. *@return None.
  89. */
  90. void SPI_Init(SPI_InitTypeDef* SPI_InitStruct);
  91. /**
  92. *@brief cancel SPI initialization.
  93. *@param None.
  94. *@return None.
  95. */
  96. void SPI_DeInit(void);
  97. /**
  98. *@brief This function can send data through SPI bus.
  99. *@param Data the data will be send.
  100. *@return None.
  101. */
  102. uint8_t SPI_Flash_SendByte(uint8_t Data);
  103. uint8_t SPI_Flash_RecvByte(uint8_t Data);
  104. void Spi_SendReceDataToBuff(uint8_t *txdata,int txlen,uint8_t *rxbuf,int rxlen);
  105. void SPI_SendData(uint16_t Data);
  106. /**
  107. *@brief The function is used to read the value of the SPI register.
  108. *@param RegAdr address of the register to read.
  109. *@return SPI current read pointer.
  110. */
  111. uint16_t SPI_ReadRegister(uint16_t RegAdr);
  112. /**
  113. *@brief This function can send a set of data through SPI bus.
  114. *@param Buff the pointer of data that will be send.
  115. *@param Len the number of byte to be send.
  116. *@return None.
  117. */
  118. void SPI_SendDataFromBuff(uint8_t *Buff, uint16_t Len);
  119. /**
  120. *@brief This function can receive a set of data through SPI bus.
  121. *@param TxBuff the pointer of data that will be send.
  122. *@param TxLen the number of byte to be send.
  123. *@param RxBuff the pointer that the read data is stored.
  124. *@param RxLen the number of byte received.
  125. *@return None.
  126. */
  127. void SPI_ReceiveDataToBuff(uint8_t *TxBuff, uint16_t TxLen, uint8_t *RxBuff, uint16_t RxLen);
  128. void SPI_ReceiveDataToBuff2(uint8_t *TxBuff, uint16_t TxLen, uint8_t *RxBuff, uint16_t RxLen);
  129. void SPI_SendDataFromBuffWithoutDMAdone(uint8_t *Buff, uint16_t Len);
  130. bool SPI_WaitDone(void);
  131. void SPI_FLASH_Init(void);
  132. void Soft_SPI_Write(uint8_t a);
  133. uint8_t Soft_SPI_Read(void);
  134. void Soft_WaitFlahTobeReady(void);
  135. void Soft_Flash_chipErase(uint32_t addr);
  136. void Soft_SPIFlashPageWrite(uint8_t *Writebuffer, uint32_t Writeaddr, uint16_t NumberbiteToWrite);
  137. void Soft_SPIFlashRead(uint8_t *Readbuffer, uint32_t Readaddr, uint16_t NumberbiteToRead);
  138. uint32_t Soft_SPIFlashIDRead(void);
  139. #endif /* _SPI_H_ */