hal_spi.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #ifndef __HAL_SPI__
  2. #define __HAL_SPI__
  3. #include "hal_w25qxx.h"
  4. #include "hc32_ll_spi.h"
  5. #include "hc32_ll_utility.h"
  6. #include "hc32_ll_gpio.h"
  7. #include "hc32_ll_fcg.h"
  8. #if 0
  9. /***************************测试引脚******************************/
  10. #define BSP_SPI_UNIT CM_SPI1
  11. #define BSP_SPI_PERIPH_CLK FCG1_PERIPH_SPI1
  12. #define BSP_SPI_CS_PORT (GPIO_PORT_B)
  13. #define BSP_SPI_CS_PIN (GPIO_PIN_06)
  14. #define BSP_SPI_CS_PIN_FUNC (GPIO_FUNC_46)
  15. #define BSP_SPI_CS_ACTIVE() (GPIO_ResetPins(BSP_SPI_CS_PORT, BSP_SPI_CS_PIN))
  16. #define BSP_SPI_CS_INACTIVE() (GPIO_SetPins(BSP_SPI_CS_PORT, BSP_SPI_CS_PIN))
  17. #define BSP_SPI_SCK_PORT (GPIO_PORT_B)
  18. #define BSP_SPI_SCK_PIN (GPIO_PIN_07)
  19. #define BSP_SPI_SCK_PIN_FUNC (GPIO_FUNC_47)
  20. #define BSP_SPI_MOSI_PORT (GPIO_PORT_B)
  21. #define BSP_SPI_MOSI_PIN (GPIO_PIN_09)
  22. #define BSP_SPI_MOSI_PIN_FUNC (GPIO_FUNC_44)
  23. #define BSP_SPI_MISO_PORT (GPIO_PORT_B)
  24. #define BSP_SPI_MISO_PIN (GPIO_PIN_08)
  25. #define BSP_SPI_MISO_PIN_FUNC (GPIO_FUNC_45)
  26. #else
  27. /***************************实际引脚******************************/
  28. #define BSP_SPI_UNIT CM_SPI1
  29. #define BSP_SPI_PERIPH_CLK FCG1_PERIPH_SPI1
  30. #define BSP_SPI_CS_PORT (GPIO_PORT_A)
  31. #define BSP_SPI_CS_PIN (GPIO_PIN_15)
  32. #define BSP_SPI_CS_PIN_FUNC (GPIO_FUNC_42)
  33. #define BSP_SPI_CS_ACTIVE() (GPIO_ResetPins(BSP_SPI_CS_PORT, BSP_SPI_CS_PIN))
  34. #define BSP_SPI_CS_INACTIVE() (GPIO_SetPins(BSP_SPI_CS_PORT, BSP_SPI_CS_PIN))
  35. #define BSP_SPI_SCK_PORT (GPIO_PORT_D)
  36. #define BSP_SPI_SCK_PIN (GPIO_PIN_00)
  37. #define BSP_SPI_SCK_PIN_FUNC (GPIO_FUNC_43)
  38. #define BSP_SPI_MOSI_PORT (GPIO_PORT_D)
  39. #define BSP_SPI_MOSI_PIN (GPIO_PIN_02)
  40. #define BSP_SPI_MOSI_PIN_FUNC (GPIO_FUNC_40)
  41. #define BSP_SPI_MISO_PORT (GPIO_PORT_D)
  42. #define BSP_SPI_MISO_PIN (GPIO_PIN_01)
  43. #define BSP_SPI_MISO_PIN_FUNC (GPIO_FUNC_41)
  44. #endif
  45. /*******************************MPU spi****************************************************/
  46. #define MPU_SPI_UNIT CM_SPI3
  47. #define MPU_SPI_PERIPH_CLK FCG1_PERIPH_SPI3
  48. #define MPU_SPI_CS_PORT (GPIO_PORT_B)
  49. #define MPU_SPI_CS_PIN (GPIO_PIN_06)
  50. #define MPU_SPI_CS_PIN_FUNC (GPIO_FUNC_42)
  51. #define MPU_SPI_CS_ACTIVE() (GPIO_ResetPins(MPU_SPI_CS_PORT, MPU_SPI_CS_PIN))
  52. #define MPU_SPI_CS_INACTIVE() (GPIO_SetPins(MPU_SPI_CS_PORT, MPU_SPI_CS_PIN))
  53. #define MPU_SPI_SCK_PORT (GPIO_PORT_B)
  54. #define MPU_SPI_SCK_PIN (GPIO_PIN_07)
  55. #define MPU_SPI_SCK_PIN_FUNC (GPIO_FUNC_43)
  56. #define MPU_SPI_MISO_PORT (GPIO_PORT_B)
  57. #define MPU_SPI_MISO_PIN (GPIO_PIN_08)
  58. #define MPU_SPI_MISO_PIN_FUNC (GPIO_FUNC_41)
  59. #define MPU_SPI_MOSI_PORT (GPIO_PORT_B)
  60. #define MPU_SPI_MOSI_PIN (GPIO_PIN_09)
  61. #define MPU_SPI_MOSI_PIN_FUNC (GPIO_FUNC_40)
  62. #define SPI_TX_EVT_SRC (EVT_SRC_SPI1_SPTI)
  63. #define SPI_RX_EVT_SRC (EVT_SRC_SPI1_SPRI)
  64. /* DMA definition */
  65. #define DMA_UNIT (CM_DMA1)
  66. #define DMA_CLK (FCG0_PERIPH_DMA1 | FCG0_PERIPH_AOS)
  67. #define DMA_TX_CH (DMA_CH0)
  68. #define DMA_TX_TRIG_CH (AOS_DMA1_0)
  69. #define DMA_RX_CH (DMA_CH1)
  70. #define DMA_RX_INT_CH (DMA_INT_TC_CH1)
  71. #define DMA_RX_TRIG_CH (AOS_DMA1_1)
  72. #define DMA_RX_INT_SRC (INT_SRC_DMA1_TC1)
  73. #define DMA_RX_IRQ_NUM (INT006_IRQn)
  74. //#define MPU_SPI_UNIT CM_SPI1
  75. //#define MPU_SPI_PERIPH_CLK FCG1_PERIPH_SPI1
  76. //#define MPU_SPI_CS_PORT (GPIO_PORT_B)
  77. //#define MPU_SPI_CS_PIN (GPIO_PIN_06)
  78. //#define MPU_SPI_CS_PIN_FUNC (GPIO_FUNC_46)
  79. //#define MPU_SPI_CS_ACTIVE() (GPIO_ResetPins(MPU_SPI_CS_PORT, MPU_SPI_CS_PIN))
  80. //#define MPU_SPI_CS_INACTIVE() (GPIO_SetPins(MPU_SPI_CS_PORT, MPU_SPI_CS_PIN))
  81. //#define MPU_SPI_SCK_PORT (GPIO_PORT_B)
  82. //#define MPU_SPI_SCK_PIN (GPIO_PIN_07)
  83. //#define MPU_SPI_SCK_PIN_FUNC (GPIO_FUNC_47)
  84. //#define MPU_SPI_MISO_PORT (GPIO_PORT_B)
  85. //#define MPU_SPI_MISO_PIN (GPIO_PIN_08)
  86. //#define MPU_SPI_MISO_PIN_FUNC (GPIO_FUNC_45)
  87. //#define MPU_SPI_MOSI_PORT (GPIO_PORT_B)
  88. //#define MPU_SPI_MOSI_PIN (GPIO_PIN_09)
  89. //#define MPU_SPI_MOSI_PIN_FUNC (GPIO_FUNC_44)
  90. //#define SPI_TX_EVT_SRC (EVT_SRC_SPI1_SPTI)
  91. //#define SPI_RX_EVT_SRC (EVT_SRC_SPI1_SPRI)
  92. ///* DMA definition */
  93. //#define DMA_UNIT (CM_DMA1)
  94. //#define DMA_CLK (FCG0_PERIPH_DMA1 | FCG0_PERIPH_AOS)
  95. //#define DMA_TX_CH (DMA_CH0)
  96. //#define DMA_TX_TRIG_CH (AOS_DMA1_0)
  97. //#define DMA_RX_CH (DMA_CH1)
  98. //#define DMA_RX_INT_CH (DMA_INT_TC_CH1)
  99. //#define DMA_RX_TRIG_CH (AOS_DMA1_1)
  100. //#define DMA_RX_INT_SRC (INT_SRC_DMA1_TC1)
  101. //#define DMA_RX_IRQ_NUM (INT006_IRQn)
  102. /**
  103. * @}
  104. */
  105. /**
  106. * @defgroup W25Qxx_SPI_Timeout W25Qxx SPI Timeout
  107. * @{
  108. */
  109. #define BSP_SPI_TIMEOUT ((HCLK_VALUE / 1000UL))
  110. /**
  111. * @}
  112. */
  113. /**
  114. * @defgroup W25Q64_Size W25Q64 Size
  115. * @{
  116. */
  117. #define W25Q64_PAGE_SIZE (256UL)
  118. #define W25Q64_SECTOR_SIZE (1024UL * 4UL)
  119. #define W25Q64_BLOCK_SIZE (1024UL * 64UL)
  120. #define W25Q64_PAGE_PER_SECTOR (W25Q64_SECTOR_SIZE / W25Qs64_PAGE_SIZE)
  121. #define W25Q64_MAX_ADDR (0x800000UL)
  122. /**
  123. * @}
  124. */
  125. #include "FreeRTOS.h"
  126. #include "queue.h"
  127. void bsp_spi_init(void);
  128. void bsp_w25qxx_init(void);
  129. void BSP_W25QXX_DeInit(void);
  130. int32_t BSP_W25QXX_Write(uint32_t u32Addr, const uint8_t *pu8Data, uint32_t u32NumByteToWrite);
  131. int32_t BSP_W25QXX_Read(uint32_t u32Addr, uint8_t *pu8Data, uint32_t u32NumByteToRead);
  132. int32_t BSP_W25QXX_EraseSector(uint32_t u32Addr);
  133. int32_t BSP_W25QXX_EraseChip(void);
  134. int32_t bsp_spi_trans(const uint8_t *pu8TxBuf, uint32_t u32Size);
  135. int32_t bsp_spi_receive(uint8_t *pu8RxBuf, uint32_t u32Size);
  136. int32_t spi_flash_read(uint32_t u32Addr, uint8_t *pu8Data, uint32_t u32NumByteToRead);
  137. void spi_flash_write(uint32_t WriteAddr,uint8_t* pBuffer,uint16_t NumByteToWrite);
  138. #endif