#ifndef __SPI_DMA_H_
#define __SPI_DMA_H_

#include "main.h"

/********************************SPI1*****************************/
/****** GPIO 配置******/
//SPI1 GPIO
#define SPI1_RCC_GPIO_CONFIG()	(RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE))
#define SPI1_RCC_CONFIG()		(RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE))
#define SPI1_PORT_CONFIG		(GPIOA)
#define SPI1_PIN_AF_CONFIG		(GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7)
#define SPI1_PIN_CS_CONFIG		(GPIO_Pin_4)
#define SPI_EEPROM_CS_LOW()     (GPIO_ResetBits(SPI1_PORT_CONFIG, SPI1_PIN_CS_CONFIG))
#define SPI_EEPROM_CS_HIGH()    (GPIO_SetBits(SPI1_PORT_CONFIG, SPI1_PIN_CS_CONFIG))
//通讯参数
#define SPI1_Mode_CONFIG		(SPI_Mode_Master)
#define SPI1_Baud_CONFIG		(SPI_BaudRatePrescaler_256)
//SPI1 号
#define SPI1_CONFIG				(SPI1)
/****** SPI1优先级******/
#define SPI1_NVIC_Group			(NVIC_PriorityGroup_0)
#define SPI1_NVIC_Interrupt		(SPI1_IRQn)				//uart1中断号
#define SPI1_NVIC_Pre			(0)
#define SPI1_NVIC_Sub			(3)

/********DMA配置********/
#define DMA_SPI1_DR_Addr    			((u32)&SPI1->DR)//0x4001300C

#define DMA_SPI1_RCC_CONFIG				(RCC_AHBPeriph_DMA1)
#define DMA_SPI1_Rx_CHANNLE_CONFIG		(DMA1_Channel2)
#define DMA_SPI1_Rx_ADDR_CONFIG			(SPI1_RX_Buff)
#define DMA_SPI1_Tx_CHANNLE_CONFIG		(DMA1_Channel3)
#define DMA_SPI1_Tx_ADDR_CONFIG			(SPI1_TX_Buff)

/****** DMA优先级******/
#define DMA_SPI1_NVIC_Group				(NVIC_PriorityGroup_0)
#define DMA_SPI1_NVIC_Rx_Interrupt		(DMA1_Channel2_IRQn)				//uart1中断号
#define DMA_SPI1_NVIC_Rx_Pre			(0)
#define DMA_SPI1_NVIC_Rx_Sub			(4)
#define DMA_SPI1_NVIC_Tx_Interrupt		(DMA1_Channel3_IRQn)				//uart1中断号
#define DMA_SPI1_NVIC_Tx_Pre			(0)
#define DMA_SPI1_NVIC_Tx_Sub			(5)

/********************************SPI2*****************************/
/****** GPIO 配置******/
//SPI2 GPIO
#define SPI2_RCC_GPIO_CONFIG()	(RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO,ENABLE))
#define SPI2_RCC_CONFIG()		(RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE))
#define SPI2_PORT_CONFIG		(GPIOB)
#define SPI2_PIN_AF_CONFIG		(GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15)
#define SPI2_PIN_CS_CONFIG		(GPIO_Pin_12)
#define SPI2_EEPROM_CS_VAL     	(PBin(12))
//通讯参数
#define SPI2_Mode_CONFIG		(SPI_Mode_Slave)
#define SPI2_Baud_CONFIG		(SPI_BaudRatePrescaler_16)
//SPI1 号
#define SPI2_CONFIG				(SPI2)
/****** SPI1优先级******/
#define SPI2_NVIC_Group			(NVIC_PriorityGroup_0)
#define SPI2_NVIC_Interrupt		(SPI2_IRQn)				//uart1中断号
#define SPI2_NVIC_Pre			(0)
#define SPI2_NVIC_Sub			(6)

/********DMA配置********/
#define DMA_SPI2_DR_Addr    			((u32)&SPI2->DR)//0x4001300C

#define DMA_SPI2_RCC_CONFIG				(RCC_AHBPeriph_DMA1)
#define DMA_SPI2_Rx_CHANNLE_CONFIG		(DMA1_Channel4)
#define DMA_SPI2_Rx_ADDR_CONFIG			(SPI2_RX_Buff)
#define DMA_SPI2_Tx_CHANNLE_CONFIG		(DMA1_Channel5)
#define DMA_SPI2_Tx_ADDR_CONFIG			(SPI2_TX_Buff)

/****** DMA优先级******/
#define DMA_SPI2_NVIC_Group				(NVIC_PriorityGroup_0)
#define DMA_SPI2_NVIC_Rx_Interrupt		(DMA1_Channel4_IRQn)				//uart1中断号
#define DMA_SPI2_NVIC_Rx_Pre			(0)
#define DMA_SPI2_NVIC_Rx_Sub			(7)
#define DMA_SPI2_NVIC_Tx_Interrupt		(DMA1_Channel5_IRQn)				//uart1中断号
#define DMA_SPI2_NVIC_Tx_Pre			(0)
#define DMA_SPI2_NVIC_Tx_Sub			(8)


#define BufferSize     					(32)

extern void SPI1_GPIO_Configuration ( void );
extern void SPI1_NVIC_Configuration ( void );
extern void DMA1_SPI1_Configuration ( void );
extern void DMA1_SPI1_NVIC_Configuration ( void );
extern u8 SPIx_ReadWriteByte(u8 byte);
extern void SPI1_Send ( u8 *buff, u32 len );
extern void SPI1_Recive ( u8 *buff, u32 len );

extern void SPI2_GPIO_Configuration ( void );
extern void SPI2_NVIC_Configuration ( void );
extern void DMA1_SPI2_Configuration ( void );
extern void DMA1_SPI2_NVIC_Configuration ( void );
extern u8 SPI2_ReadWriteByte(u8 byte);
extern void SPI2_Send ( u8 *buff, u32 len );
extern void SPI2_Recive ( u8 *buff, u32 len );


extern u8 SPI1_TX_Buff[];
extern u8 SPI1_RX_Buff[];
extern u8 SPI2_TX_Buff[];
extern u8 SPI2_RX_Buff[];

extern u8 RxCounterSpi1;
extern u8 TxCounterSpi1;
extern u8 RxCounterSpi2;
extern u8 TxCounterSpi2;
extern bool SPI1_data_come;
extern bool SPI2_data_come;

#endif
