OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 4872|回复: 12

STM32F030c8t6的SPI操作W25Q32

[复制链接]

24

主题

157

帖子

0

精华

高级会员

Rank: 4

积分
523
金钱
523
注册时间
2016-1-7
在线时间
131 小时
发表于 2018-1-26 16:09:36 | 显示全部楼层 |阅读模式
10金钱
STM32F030c8t6的SPI操作W25Q32不成功,求助

最佳答案

查看完整内容[请看2#楼]

STM32F030C8T6里面没有SPI2,只有SPI1,问题就出在这里
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

24

主题

157

帖子

0

精华

高级会员

Rank: 4

积分
523
金钱
523
注册时间
2016-1-7
在线时间
131 小时
 楼主| 发表于 2018-1-26 16:09:37 | 显示全部楼层

STM32F030C8T6里面没有SPI2,只有SPI1,问题就出在这里
回复

使用道具 举报

24

主题

157

帖子

0

精华

高级会员

Rank: 4

积分
523
金钱
523
注册时间
2016-1-7
在线时间
131 小时
 楼主| 发表于 2018-1-26 16:13:23 | 显示全部楼层
[mw_shl_code=c,true]void SPI_FLASH_Init(void)
{
         
  GPIO_InitTypeDef  GPIO_InitStruct;
  SPI_InitTypeDef   SPI_InitStruct;

  /*!< SD_SPI_CS_GPIO, SD_SPI_MOSI_GPIO, SD_SPI_MISO_GPIO, SD_SPI_DETECT_GPIO
       and SD_SPI_SCK_GPIO Periph clock enable */
//          RCC_AHBPeriphClockCmd(FLASH_CS_PIN_SCK|FLASH_SCK_PIN_SCK|FLASH_MISO_PIN_SCK | FLASH_MOSI_PIN_SCK, ENABLE);
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA| RCC_AHBPeriph_GPIOB, ENABLE);
  /*!< SD_SPI Periph clock enable */
       
  RCC_APB2PeriphClockCmd(FLASH_SPI1, ENABLE); //打开SP1时钟

  /*!< Configure SD_SPI pins: SCK */
  GPIO_InitStruct.GPIO_Pin = FLASH_SCK_PIN;
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStruct.GPIO_Speed = GPIO_Speed_Level_3;
  GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStruct.GPIO_PuPd  = GPIO_PuPd_UP;
  GPIO_Init(FLASH_SCK_PORT, &GPIO_InitStruct);

  /*!< Configure SD_SPI pins: MISO */
  GPIO_InitStruct.GPIO_Pin = FLASH_MISO_PIN;
   GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStruct.GPIO_Speed = GPIO_Speed_Level_3;
  GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStruct.GPIO_PuPd  = GPIO_PuPd_UP;
  GPIO_Init(FLASH_MISO_PORT, &GPIO_InitStruct);

  /*!< Configure SD_SPI pins: MOSI */
  GPIO_InitStruct.GPIO_Pin =FLASH_MOSI_PIN;
   GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStruct.GPIO_Speed = GPIO_Speed_Level_3;
  GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStruct.GPIO_PuPd  = GPIO_PuPd_UP;
  GPIO_Init(FLASH_MOSI_PORT, &GPIO_InitStruct);
  
  /* Connect PXx to SD_SPI_SCK */
  GPIO_PinAFConfig(FLASH_SCK_PORT, FLASH_SCK_SOURCE, FLASH_SCK_AF);

  /* Connect PXx to SD_SPI_MISO */
  GPIO_PinAFConfig(FLASH_MISO_PORT, FLASH_MISO_SOURCE, FLASH_MISO_AF);

  /* Connect PXx to SD_SPI_MOSI */
  GPIO_PinAFConfig(FLASH_MOSI_PORT, FLASH_MOSI_SOURCE, FLASH_MOSI_AF);
       
       
         /*!< Configure SD_SPI_CS_PIN pin: SD Card CS pin */
  GPIO_InitStruct.GPIO_Pin =FLASH_CS_PIN;
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_InitStruct.GPIO_Speed = GPIO_Speed_Level_3;
  GPIO_Init(FLASH_CS_PORT, &GPIO_InitStruct);
       
  SPI_FLASH_CS_HIGH();
  GPIO_SetBits(GPIOA,GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7);
  /*!< SD_SPI Config */
  SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  SPI_InitStruct.SPI_Mode = SPI_Mode_Master;
  SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;
  SPI_InitStruct.SPI_CPOL = SPI_CPOL_High;
  SPI_InitStruct.SPI_CPHA = SPI_CPHA_2Edge;
  SPI_InitStruct.SPI_NSS = SPI_NSS_Soft;
  SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;
  SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
  SPI_InitStruct.SPI_CRCPolynomial = 7;
  SPI_Init(SPI1, &SPI_InitStruct);
  SPI_RxFIFOThresholdConfig(SPI1, SPI_RxFIFOThreshold_QF);
  SPI_Cmd(SPI1, ENABLE); /*!< SD_SPI enable */
   
         
        // SPI_FLASH_CS_LOW();
  
}
/*******************************************************************************
* Function Name  : SPI_FLASH_SectorErase  
* Description    : Erases the specified FLASH sector.擦除FLASH扇区
* Input          : SectorAddr: address of the sector to erase.要擦除的扇区地址
* Output         : None
* Return         : None
*******************************************************************************/
void SPI_FLASH_SectorErase(uint32_t SectorAddr)
{
  /* Send write enable instruction */
  SPI_FLASH_WriteEnable();
  SPI_FLASH_WaitForWriteEnd();
  /* Sector Erase */
  /* Select the FLASH: Chip Select low */
  SPI_FLASH_CS_LOW();
  /* Send Sector Erase instruction */
  SPI_FLASH_SendByte(W25X_SectorErase);
  /* Send SectorAddr high nibble address byte */
  SPI_FLASH_SendByte((SectorAddr & 0xFF0000) >> 16);
  /* Send SectorAddr medium nibble address byte */
  SPI_FLASH_SendByte((SectorAddr & 0xFF00) >> 8);
  /* Send SectorAddr low nibble address byte */
  SPI_FLASH_SendByte(SectorAddr & 0xFF);
  /* Deselect the FLASH: Chip Select high */
  SPI_FLASH_CS_HIGH();
  /* Wait the end of Flash writing */
  SPI_FLASH_WaitForWriteEnd();
}

/*******************************************************************************
* Function Name  : SPI_FLASH_BulkErase
* Description    : Erases the entire FLASH.   擦除FLASH扇区,整片擦除
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void SPI_FLASH_BulkErase(void)
{
  /* Send write enable instruction */
  SPI_FLASH_WriteEnable();

  /* Bulk Erase */
  /* Select the FLASH: Chip Select low */
  SPI_FLASH_CS_LOW();
  /* Send Bulk Erase instruction  */
  SPI_FLASH_SendByte(W25X_ChipErase);
  /* Deselect the FLASH: Chip Select high */
  SPI_FLASH_CS_HIGH();

  /* Wait the end of Flash writing */
  SPI_FLASH_WaitForWriteEnd();
}

/*******************************************************************************
* Function Name  : SPI_FLASH_PageWrite
* Description    : Writes more than one byte to the FLASH with a single WRITE   对FLASH按页写入数据,调用本函数写入数据前需要先擦除扇区
*                  cycle(Page WRITE sequence). The number of byte can't exceed
*                  the FLASH page size.
* Input          : - pBuffer : pointer to the buffer  containing the data to be   pBuffer,要写入数据的指针
*                    written to the FLASH.
*                  - WriteAddr : FLASH's internal address to write to.   写入地址
*                  - NumByteToWrite : number of bytes to write to the FLASH, NumByteToWrite,写入数据长度,必须小于等于SPI_FLASH_PerWritePageSize
*                    must be equal or less than "SPI_FLASH_PageSize" value.
* Output         : None
* Return         : None
*******************************************************************************/
void SPI_FLASH_PageWrite(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite)
{
  /* Enable the write access to the FLASH */
  SPI_FLASH_WriteEnable();

  /* Select the FLASH: Chip Select low */
  SPI_FLASH_CS_LOW();
  /* Send "Write to Memory " instruction */
  SPI_FLASH_SendByte(W25X_PageProgram);
  /* Send WriteAddr high nibble address byte to write to */
  SPI_FLASH_SendByte((WriteAddr & 0xFF0000) >> 16);
  /* Send WriteAddr medium nibble address byte to write to */
  SPI_FLASH_SendByte((WriteAddr & 0xFF00) >> 8);
  /* Send WriteAddr low nibble address byte to write to */
  SPI_FLASH_SendByte(WriteAddr & 0xFF);

  if(NumByteToWrite > SPI_FLASH_PerWritePageSize)
  {
     NumByteToWrite = SPI_FLASH_PerWritePageSize;
     //printf("\n\r Err: SPI_FLASH_PageWrite too large!");
  }

  /* while there is data to be written on the FLASH */
  while (NumByteToWrite--)
  {
    /* Send the current byte */
    SPI_FLASH_SendByte(*pBuffer);
    /* Point on the next byte to be written */
    pBuffer++;
  }

  /* Deselect the FLASH: Chip Select high */
  SPI_FLASH_CS_HIGH();

  /* Wait the end of Flash writing */
  SPI_FLASH_WaitForWriteEnd();
}

/*******************************************************************************
* Function Name  : SPI_FLASH_BufferWrite
* Description    : Writes block of data to the FLASH. In this function, the
*                  number of WRITE cycles are reduced, using Page WRITE sequence. 对FLASH写入数据,调用本函数写入数据前需要先擦除扇区
* Input          : - pBuffer : pointer to the buffer  containing the data to be   pBuffer,要写入数据的指针
*                    written to the FLASH.
*                  - WriteAddr : FLASH's internal address to write to.            写入地址
*                  - NumByteToWrite : number of bytes to write to the FLASH.      写入数据长度
* Output         : None
* Return         : None
*******************************************************************************/
void SPI_FLASH_BufferWrite(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite)
{
  uint8_t NumOfPage = 0, NumOfSingle = 0, Addr = 0, count = 0, temp = 0;
  SPI_FLASH_WriteEnable();
  Addr = WriteAddr % SPI_FLASH_PageSize;
  count = SPI_FLASH_PageSize - Addr;
  NumOfPage =  NumByteToWrite / SPI_FLASH_PageSize;
  NumOfSingle = NumByteToWrite % SPI_FLASH_PageSize;

  if (Addr == 0) /* WriteAddr is SPI_FLASH_PageSize aligned  */
  {
    if (NumOfPage == 0) /* NumByteToWrite < SPI_FLASH_PageSize */
    {
      SPI_FLASH_PageWrite(pBuffer, WriteAddr, NumByteToWrite);
    }
    else /* NumByteToWrite > SPI_FLASH_PageSize */
    {
      while (NumOfPage--)
      {
        SPI_FLASH_PageWrite(pBuffer, WriteAddr, SPI_FLASH_PageSize);
        WriteAddr +=  SPI_FLASH_PageSize;
        pBuffer += SPI_FLASH_PageSize;
      }

      SPI_FLASH_PageWrite(pBuffer, WriteAddr, NumOfSingle);
    }
  }
  else /* WriteAddr is not SPI_FLASH_PageSize aligned  */
  {
    if (NumOfPage == 0) /* NumByteToWrite < SPI_FLASH_PageSize */
    {
      if (NumOfSingle > count) /* (NumByteToWrite + WriteAddr) > SPI_FLASH_PageSize */
      {
        temp = NumOfSingle - count;

        SPI_FLASH_PageWrite(pBuffer, WriteAddr, count);
        WriteAddr +=  count;
        pBuffer += count;

        SPI_FLASH_PageWrite(pBuffer, WriteAddr, temp);
      }
      else
      {
        SPI_FLASH_PageWrite(pBuffer, WriteAddr, NumByteToWrite);
      }
    }
    else /* NumByteToWrite > SPI_FLASH_PageSize */
    {
      NumByteToWrite -= count;
      NumOfPage =  NumByteToWrite / SPI_FLASH_PageSize;
      NumOfSingle = NumByteToWrite % SPI_FLASH_PageSize;

      SPI_FLASH_PageWrite(pBuffer, WriteAddr, count);
      WriteAddr +=  count;
      pBuffer += count;

      while (NumOfPage--)
      {
        SPI_FLASH_PageWrite(pBuffer, WriteAddr, SPI_FLASH_PageSize);
        WriteAddr +=  SPI_FLASH_PageSize;
        pBuffer += SPI_FLASH_PageSize;
      }

      if (NumOfSingle != 0)
      {
        SPI_FLASH_PageWrite(pBuffer, WriteAddr, NumOfSingle);
      }
    }
  }
}[/mw_shl_code]
回复

使用道具 举报

6

主题

462

帖子

0

精华

高级会员

Rank: 4

积分
906
金钱
906
注册时间
2017-12-15
在线时间
111 小时
发表于 2018-1-26 16:13:27 | 显示全部楼层
请努力调试。。。目前遭遇了什么问题?
回复

使用道具 举报

160

主题

967

帖子

0

精华

金牌会员

Rank: 6Rank: 6

积分
2097
金钱
2097
注册时间
2014-3-7
在线时间
491 小时
发表于 2018-1-26 16:31:03 | 显示全部楼层
吴京都来写程序。
回复

使用道具 举报

2

主题

685

帖子

0

精华

论坛元老

Rank: 8Rank: 8

积分
3448
金钱
3448
注册时间
2017-7-4
在线时间
869 小时
发表于 2018-1-26 16:49:08 | 显示全部楼层
回复

使用道具 举报

13

主题

156

帖子

0

精华

高级会员

Rank: 4

积分
883
金钱
883
注册时间
2017-8-7
在线时间
200 小时
发表于 2018-1-26 17:26:08 | 显示全部楼层
让我在战狼3里当男3我就告诉你
回复

使用道具 举报

24

主题

157

帖子

0

精华

高级会员

Rank: 4

积分
523
金钱
523
注册时间
2016-1-7
在线时间
131 小时
 楼主| 发表于 2018-1-26 19:03:51 | 显示全部楼层
a496298685 发表于 2018-1-26 16:13
请努力调试。。。目前遭遇了什么问题?

无法读,也无法写
回复

使用道具 举报

24

主题

157

帖子

0

精华

高级会员

Rank: 4

积分
523
金钱
523
注册时间
2016-1-7
在线时间
131 小时
 楼主| 发表于 2018-1-26 19:04:13 | 显示全部楼层
无法读也无法写
回复

使用道具 举报

24

主题

157

帖子

0

精华

高级会员

Rank: 4

积分
523
金钱
523
注册时间
2016-1-7
在线时间
131 小时
 楼主| 发表于 2018-1-29 11:08:04 | 显示全部楼层
该问题已解决,一个细节忽略了
回复

使用道具 举报

6

主题

462

帖子

0

精华

高级会员

Rank: 4

积分
906
金钱
906
注册时间
2017-12-15
在线时间
111 小时
发表于 2018-1-29 13:41:49 | 显示全部楼层
lumilu 发表于 2018-1-29 11:08
该问题已解决,一个细节忽略了

说出来看看?
回复

使用道具 举报

6

主题

27

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
235
金钱
235
注册时间
2015-7-22
在线时间
45 小时
发表于 2019-8-24 12:39:21 | 显示全部楼层
请问有文档说明吗??我操作SPI2也是不行,但是网上有些案列我看都是使用STM32F030C8T6的SPI2 写的
回复

使用道具 举报

10

主题

97

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
490
金钱
490
注册时间
2019-12-6
在线时间
153 小时
发表于 2020-3-4 17:43:30 | 显示全部楼层
能不能给下程序代码  我ID都读不对
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-5-21 15:23

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表