#ifndef __HAL_SPI__
#define __HAL_SPI__

#include "hal_w25qxx.h"
#include "hc32_ll_spi.h"
#include "hc32_ll_utility.h"
#include "hc32_ll_gpio.h"
#include "hc32_ll_fcg.h"

#if 0
/***************************测试引脚******************************/
#define BSP_SPI_UNIT                    CM_SPI1
#define BSP_SPI_PERIPH_CLK              FCG1_PERIPH_SPI1

#define BSP_SPI_CS_PORT                 (GPIO_PORT_B)
#define BSP_SPI_CS_PIN                  (GPIO_PIN_06)
#define BSP_SPI_CS_PIN_FUNC             (GPIO_FUNC_46)

#define BSP_SPI_CS_ACTIVE()             (GPIO_ResetPins(BSP_SPI_CS_PORT, BSP_SPI_CS_PIN))
#define BSP_SPI_CS_INACTIVE()           (GPIO_SetPins(BSP_SPI_CS_PORT, BSP_SPI_CS_PIN))

#define BSP_SPI_SCK_PORT                (GPIO_PORT_B)
#define BSP_SPI_SCK_PIN                 (GPIO_PIN_07)
#define BSP_SPI_SCK_PIN_FUNC            (GPIO_FUNC_47)

#define BSP_SPI_MOSI_PORT               (GPIO_PORT_B)
#define BSP_SPI_MOSI_PIN                (GPIO_PIN_09)
#define BSP_SPI_MOSI_PIN_FUNC           (GPIO_FUNC_44)

#define BSP_SPI_MISO_PORT               (GPIO_PORT_B)
#define BSP_SPI_MISO_PIN                (GPIO_PIN_08)
#define BSP_SPI_MISO_PIN_FUNC           (GPIO_FUNC_45)

#else
/***************************实际引脚******************************/
#define BSP_SPI_UNIT                    CM_SPI1
#define BSP_SPI_PERIPH_CLK              FCG1_PERIPH_SPI1

#define BSP_SPI_CS_PORT                 (GPIO_PORT_A)
#define BSP_SPI_CS_PIN                  (GPIO_PIN_15)
#define BSP_SPI_CS_PIN_FUNC             (GPIO_FUNC_42)

#define BSP_SPI_CS_ACTIVE()             (GPIO_ResetPins(BSP_SPI_CS_PORT, BSP_SPI_CS_PIN))
#define BSP_SPI_CS_INACTIVE()           (GPIO_SetPins(BSP_SPI_CS_PORT, BSP_SPI_CS_PIN))

#define BSP_SPI_SCK_PORT                (GPIO_PORT_D)
#define BSP_SPI_SCK_PIN                 (GPIO_PIN_00)
#define BSP_SPI_SCK_PIN_FUNC            (GPIO_FUNC_43)

#define BSP_SPI_MOSI_PORT               (GPIO_PORT_D)
#define BSP_SPI_MOSI_PIN                (GPIO_PIN_02)
#define BSP_SPI_MOSI_PIN_FUNC           (GPIO_FUNC_40)

#define BSP_SPI_MISO_PORT               (GPIO_PORT_D)
#define BSP_SPI_MISO_PIN                (GPIO_PIN_01)
#define BSP_SPI_MISO_PIN_FUNC           (GPIO_FUNC_41)
#endif




/*******************************MPU spi****************************************************/
#define MPU_SPI_UNIT                    CM_SPI3
#define MPU_SPI_PERIPH_CLK              FCG1_PERIPH_SPI3

#define MPU_SPI_CS_PORT                 (GPIO_PORT_B)
#define MPU_SPI_CS_PIN                  (GPIO_PIN_06)
#define MPU_SPI_CS_PIN_FUNC             (GPIO_FUNC_42)

#define MPU_SPI_CS_ACTIVE()             (GPIO_ResetPins(MPU_SPI_CS_PORT, MPU_SPI_CS_PIN))
#define MPU_SPI_CS_INACTIVE()           (GPIO_SetPins(MPU_SPI_CS_PORT, MPU_SPI_CS_PIN))

#define MPU_SPI_SCK_PORT                (GPIO_PORT_B)
#define MPU_SPI_SCK_PIN                 (GPIO_PIN_07)
#define MPU_SPI_SCK_PIN_FUNC            (GPIO_FUNC_43)

#define MPU_SPI_MISO_PORT               (GPIO_PORT_B)
#define MPU_SPI_MISO_PIN                (GPIO_PIN_08)
#define MPU_SPI_MISO_PIN_FUNC           (GPIO_FUNC_41)

#define MPU_SPI_MOSI_PORT               (GPIO_PORT_B)
#define MPU_SPI_MOSI_PIN                (GPIO_PIN_09)
#define MPU_SPI_MOSI_PIN_FUNC           (GPIO_FUNC_40)

#define SPI_TX_EVT_SRC                  (EVT_SRC_SPI3_SPTI)
#define SPI_RX_EVT_SRC                  (EVT_SRC_SPI3_SPRI)

/* DMA definition */
#define DMA_UNIT                        (CM_DMA1)
#define DMA_CLK                         (FCG0_PERIPH_DMA1 | FCG0_PERIPH_AOS)
#define DMA_TX_CH                       (DMA_CH0)
#define DMA_TX_TRIG_CH                  (AOS_DMA1_0)

#define DMA_RX_CH                       (DMA_CH1)
#define DMA_RX_INT_CH                   (DMA_INT_TC_CH1)
#define DMA_RX_TRIG_CH                  (AOS_DMA1_1)
#define DMA_RX_INT_SRC                  (INT_SRC_DMA1_TC1)
#define DMA_RX_IRQ_NUM                  (INT006_IRQn)






//#define MPU_SPI_UNIT                    CM_SPI1
//#define MPU_SPI_PERIPH_CLK              FCG1_PERIPH_SPI1

//#define MPU_SPI_CS_PORT                 (GPIO_PORT_B)
//#define MPU_SPI_CS_PIN                  (GPIO_PIN_06)
//#define MPU_SPI_CS_PIN_FUNC             (GPIO_FUNC_46)

//#define MPU_SPI_CS_ACTIVE()             (GPIO_ResetPins(MPU_SPI_CS_PORT, MPU_SPI_CS_PIN))
//#define MPU_SPI_CS_INACTIVE()           (GPIO_SetPins(MPU_SPI_CS_PORT, MPU_SPI_CS_PIN))

//#define MPU_SPI_SCK_PORT                (GPIO_PORT_B)
//#define MPU_SPI_SCK_PIN                 (GPIO_PIN_07)
//#define MPU_SPI_SCK_PIN_FUNC            (GPIO_FUNC_47)

//#define MPU_SPI_MISO_PORT               (GPIO_PORT_B)
//#define MPU_SPI_MISO_PIN                (GPIO_PIN_08)
//#define MPU_SPI_MISO_PIN_FUNC           (GPIO_FUNC_45)

//#define MPU_SPI_MOSI_PORT               (GPIO_PORT_B)
//#define MPU_SPI_MOSI_PIN                (GPIO_PIN_09)
//#define MPU_SPI_MOSI_PIN_FUNC           (GPIO_FUNC_44)

//#define SPI_TX_EVT_SRC                  (EVT_SRC_SPI1_SPTI)
//#define SPI_RX_EVT_SRC                  (EVT_SRC_SPI1_SPRI)

///* DMA definition */
//#define DMA_UNIT                        (CM_DMA1)
//#define DMA_CLK                         (FCG0_PERIPH_DMA1 | FCG0_PERIPH_AOS)
//#define DMA_TX_CH                       (DMA_CH0)
//#define DMA_TX_TRIG_CH                  (AOS_DMA1_0)

//#define DMA_RX_CH                       (DMA_CH1)
//#define DMA_RX_INT_CH                   (DMA_INT_TC_CH1)
//#define DMA_RX_TRIG_CH                  (AOS_DMA1_1)
//#define DMA_RX_INT_SRC                  (INT_SRC_DMA1_TC1)
//#define DMA_RX_IRQ_NUM                  (INT006_IRQn)
/**
 * @}
 */

/**
 * @defgroup W25Qxx_SPI_Timeout W25Qxx SPI Timeout
 * @{
 */
#define BSP_SPI_TIMEOUT                 ((HCLK_VALUE / 1000UL))
/**
 * @}
 */

/**
 * @defgroup W25Q64_Size W25Q64 Size
 * @{
 */
#define W25Q64_PAGE_SIZE                (256UL)
#define W25Q64_SECTOR_SIZE              (1024UL * 4UL)
#define W25Q64_BLOCK_SIZE               (1024UL * 64UL)
#define W25Q64_PAGE_PER_SECTOR          (W25Q64_SECTOR_SIZE / W25Qs64_PAGE_SIZE)
#define W25Q64_MAX_ADDR                 (0x800000UL)
/**
 * @}
 */








#include "FreeRTOS.h"
#include "queue.h"
 

void bsp_spi_init(void);
void bsp_w25qxx_init(void);
void BSP_W25QXX_DeInit(void);
int32_t BSP_W25QXX_Write(uint32_t u32Addr, const uint8_t *pu8Data, uint32_t u32NumByteToWrite);
int32_t BSP_W25QXX_Read(uint32_t u32Addr, uint8_t *pu8Data, uint32_t u32NumByteToRead);
int32_t BSP_W25QXX_EraseSector(uint32_t u32Addr);
int32_t BSP_W25QXX_EraseChip(void);

int32_t bsp_spi_trans(const uint8_t *pu8TxBuf, uint32_t u32Size);
int32_t bsp_spi_receive(uint8_t *pu8RxBuf, uint32_t u32Size);
 

int32_t spi_flash_read(uint32_t u32Addr, uint8_t *pu8Data, uint32_t u32NumByteToRead);
void 		spi_flash_write(uint32_t WriteAddr,uint8_t* pBuffer,uint16_t NumByteToWrite);




#endif
