stm32f407硬件spi读写sst25vf闪存flash代码
时间:11-24
来源:互联网
点击:
- 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);
- }
- /*--------------------
stm32f407spisst25vfflas 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)