stm32f407硬件spi读写sst25vf闪存flash代码

void SPI_FLASH_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    SPI_InitTypeDef  SPI_InitStructure;
    NVIC_InitTypeDef   NVIC_InitStructure;

    /* Enable the SPI periph */
    RCC_APB2PeriphClockCmd(FLASH_SPI_CLK, ENABLE);

    /* Enable SCK, MOSI and MISO GPIO clocks */
    RCC_AHB1PeriphClockCmd(FLASH_SPI_SCK_GPIO_CLK | FLASH_SPI_MOSI_GPIO_CLK | FLASH_SPI_MISO_GPIO_CLK, ENABLE);//FLASH_SPI_MISO_GPIO_CLK

    /* Enable CS  GPIO clock */
    RCC_AHB1PeriphClockCmd(FLASH_SPI_CS_GPIO_CLK, ENABLE);

    /* Enable 157  GPIO clock */
    RCC_AHB1PeriphClockCmd(FLASH_SPI_157_GPIO_CLK, ENABLE);

    GPIO_PinAFConfig(FLASH_SPI_SCK_GPIO_PORT, FLASH_SPI_SCK_SOURCE, FLASH_SPI_SCK_AF);
    GPIO_PinAFConfig(FLASH_SPI_MISO_GPIO_PORT, FLASH_SPI_MISO_SOURCE, FLASH_SPI_MISO_AF);
    GPIO_PinAFConfig(FLASH_SPI_MOSI_GPIO_PORT, FLASH_SPI_MOSI_SOURCE, FLASH_SPI_MOSI_AF);

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_DOWN;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

    /* SPI SCK pin configuration */
    GPIO_InitStructure.GPIO_Pin = FLASH_SPI_SCK_PIN;
    GPIO_Init(FLASH_SPI_SCK_GPIO_PORT, &GPIO_InitStructure);

    /* SPI  MOSI pin configuration */
    GPIO_InitStructure.GPIO_Pin =  FLASH_SPI_MOSI_PIN;
    GPIO_Init(FLASH_SPI_MOSI_GPIO_PORT, &GPIO_InitStructure);

    /* SPI MISO pin configuration */
    GPIO_InitStructure.GPIO_Pin = FLASH_SPI_MISO_PIN;
    GPIO_Init(FLASH_SPI_MISO_GPIO_PORT, &GPIO_InitStructure);

    /* Configure GPIO PIN for Lis Chip select */
    GPIO_InitStructure.GPIO_Pin = FLASH_SPI_CS_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(FLASH_SPI_CS_GPIO_PORT, &GPIO_InitStructure);

    SPI_FLASH_CS_HIGH();

  /* Configure GPIO PIN for 157 */
    GPIO_InitStructure.GPIO_Pin = FLASH_SPI_157_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(FLASH_SPI_157_GPIO_PORT, &GPIO_InitStructure);

    /* SPI1 configuration */
    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(FLASH_SPI, &SPI_InitStructure);

    /* Enable SPI1  */
    SPI_Cmd(FLASH_SPI, ENABLE);
    SPI_FLASH_157_DIS();

    //SPI_DMAInit();
    /* I2S DMA IRQ Channel configuration */
   /* NVIC_InitStructure.NVIC_IRQChannel = SPI1_DMA_RX_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    NVIC_InitStructure.NVIC_IRQChannel = SPI1_DMA_TX_IRQn;
    NVIC_Init(&NVIC_InitStructure);
    Delay_Init(168);*/

}

/*------------------------------------------------------------------------------
函数名称 : SPI_FLASH_Writebyte
函数功能 : 写一个字节数据
入口参数 :  address:地址  buffer:数据
出口参数 :  无
------------------------------------------------------------------------------*/

void SPI_FLASH_Writebyte(unsigned int Addr, unsigned char *buf)
{
    unsigned short i = 0, j = 0;

    SPI_FLASH_WriteEnable();

    SPI_FLASH_CS_LOW();
 SPI_FLASH_SendByte(0x02);
 SPI_FLASH_SendByte((Addr)>>16);
    SPI_FLASH_SendByte((Addr)>>8);
 SPI_FLASH_SendByte((Addr));
 SPI_FLASH_SendByte(*buf);
    SPI_FLASH_CS_HIGH();
    SPI_FLASH_WaitForWriteEnd();
    SPI_FLASH_WriteDisable();
    delay_nus(20);
}
/*------------------------------------------------------------------------------
函数名称 : SPI_FLASH_Writenbyte
函数功能 : 写一个字节数据
入口参数 :  address:地址  buffer:数据 num :数量
出口参数 :  无

------------------------------------------------------------------------------*/

void SPI_FLASH_Writenbyte(unsigned int Addr, unsigned char *buf,u16 num)
{
 u8 i;
 for(i=0;i<num;i++)
 {
   SPI_FLASH_Writebyte(Addr++,&buf[i]);
 }

}
/*------------------------------------------------------------------------------
函数名称 : SPI_FLASH_Readnbyte
函数功能 : 写一个字节数据
入口参数 :  address:地址  buffer:数据 num :数量
出口参数 :  无

------------------------------------------------------------------------------*/

void SPI_FLASH_Readnbyte(unsigned int Addr,unsigned char *buf,u16 num)
{
    unsigned short i = 0;

    SPI_FLASH_CS_LOW();

    SPI_FLASH_SendByte(0x0b);
 SPI_FLASH_SendByte(Addr>>16);
 SPI_FLASH_SendByte(Addr>>8);
 SPI_FLASH_SendByte(Addr);
 //dummy
 SPI_FLASH_SendByte(Dummy_Byte);
    for(i=0;i<num;i++)
    {
  buf[i]= SPI_FLASH_SendByte(Dummy_Byte);

 }

    SPI_FLASH_CS_HIGH();

}

main()

{

  u8 bufda[25]="123456789abcdefghijkl";

    SPI_FLASH_Init();

   //  Write_74HC595(HC595_OUT,3,&davalu);
    // Write_595_ENABLE();
         SPI_FLASH_157_EN();//kang 2014.07.10
   SPI_FLASH_ChipErase();
   SPI_FLASH_157_DIS();

    SPI_FLASH_157_EN(); //kang 2014.07.10

    i= SPI_FLASH_ReadID();
    SPI_FLASH_157_DIS();

    SPI_FLASH_157_EN();

     SPI_FLASH_Writenbyte(5,bufda,20);
     SPI_FLASH_157_DIS();

     SPI_FLASH_157_EN();
     SPI_FLASH_Readnbyte(5,readbuf,20);
     SPI_FLASH_157_DIS();

}

 下载地址:链接:http://pan.baidu.com/s/1i3B7ZwD 密码:l7an

sitemap