/*******************************************************************************
* Function Name : SPI_FLASH_Init
* Description : Initializes the peripherals used by the SPI FLASH driver.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void SPI_FLASH_Init(void)
{
#ifdef _BOARD //DEMO Board
GpioInitTypeDef gpioInitStr;
// 初始化模拟SPI的IO口
// SPI_FLASH_SCK_S PA5
gpioInitStr.GpioPin = SPI_FLASH_SCK_PIN_S;
gpioInitStr.GpioSpeed = GpioSpeed2MHz;
gpioInitStr.GpioMode = Gpio_Mode_Out_PP;
GpioInit(SPI_FLASH_SCK_PORT_S, &gpioInitStr);
// SPI_FLASH_MOSI_S PA7
gpioInitStr.GpioPin = SPI_FLASH_MOSI_PIN_S;
GpioInit(SPI_FLASH_MOSI_PORY_S, &gpioInitStr);
// SPI_FLASH_NSS_S PA4
gpioInitStr.GpioPin = SPI_FLASH_NSS_PIN_S;
GpioInit(SPI_FLASH_NSS_PORT_S, &gpioInitStr);
// SPI_FLASH_MISO_S PA6
gpioInitStr.GpioPin = SPI_FLASH_MISO_PIN_S;
gpioInitStr.GpioMode = Gpio_Mode_IN_FLOATING;
//gpioInitStr.GpioMode = Gpio_Mode_IPU;
GpioInit(SPI_FLASH_MISO_PORY_S, &gpioInitStr);
#else
#ifdef SPI_FLASH_HAL
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable SPI1 and GPIO clocks */
/*!< SPI_FLASH_SPI_CS_GPIO, SPI_FLASH_SPI_MOSI_GPIO,
SPI_FLASH_SPI_MISO_GPIO, SPI_FLASH_SPI_DETECT_GPIO
and SPI_FLASH_SPI_SCK_GPIO Periph clock enable */
// spi2管脚重映射
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD, ENABLE);
/*!< SPI_FLASH_SPI Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
/*!< AFIO Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
/*!< Configure SPI_FLASH_SPI pins: SCK */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/*!< Configure SPI_FLASH_SPI pins: MISO */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/*!< Configure SPI_FLASH_SPI pins: MOSI */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/*!< Configure SPI_FLASH_SPI_CS_PIN pin: SPI_FLASH Card CS pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* Deselect the FLASH: Chip Select high */
SPI_FLASH_CS_HIGH();
/* SPI1 configuration */
// W25X40: data input on the DIO pin is sampled on the rising edge of the CLK.
// Data on the DO and DIO pins are clocked out on the falling edge of CLK.
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //高位先发
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI2, &SPI_InitStructure);
/* Enable SPI1 */
SPI_Cmd(SPI2, ENABLE);
#else
GpioInitTypeDef gpioInitStr;
// 初始化模拟SPI的IO口
// SPI_FLASH_SCK_S PD14
gpioInitStr.GpioPin = SPI_FLASH_SCK_PIN_S;
gpioInitStr.GpioSpeed = GpioSpeed2MHz;
gpioInitStr.GpioMode = Gpio_Mode_Out_PP;
GpioInit(SPI_FLASH_SCK_PORT_S, &gpioInitStr);
// SPI_FLASH_MOSI_S PD13
gpioInitStr.GpioPin = SPI_FLASH_MOSI_PIN_S;
GpioInit(SPI_FLASH_MOSI_PORY_S, &gpioInitStr);
// SPI_FLASH_NSS_S PC8
gpioInitStr.GpioPin = SPI_FLASH_NSS_PIN_S;
GpioInit(SPI_FLASH_NSS_PORT_S, &gpioInitStr);
// SPI_FLASH_MISO_S PC7
gpioInitStr.GpioPin = SPI_FLASH_MISO_PIN_S;
gpioInitStr.GpioMode = Gpio_Mode_IN_FLOATING;
//gpioInitStr.GpioMode = Gpio_Mode_IPU;
GpioInit(SPI_FLASH_MISO_PORY_S, &gpioInitStr);
#endif
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD, ENABLE);
//Write_Protect PC6
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
//Hold PD15
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
SPI_FLASH_Write_Protect_DISABLE();//flash 关闭写保护
SPI_FLASH_Hold_DISABLE();//flash HOLD DISABLE
#endif
} |