OpenEdv-开源电子网

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

新人求助 ,有谁用过美光的N25Q256A flash芯片的?

[复制链接]

4

主题

15

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
220
金钱
220
注册时间
2016-1-11
在线时间
28 小时
发表于 2016-1-12 14:09:54 | 显示全部楼层 |阅读模式
2金钱
小弟刚刚接触STM32,还不是很熟悉,现有一个比较急的任务,要实现对N25Q256A NOR FLASH  进行读写,有哪位大神做过这个,给个代码参考一下,或者其它有参考意义的芯片的程序也行,
新人刚注册的帐号,金钱有限!谢谢各位啦!
邮箱:441778923@qq.com

最佳答案

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

[mw_shl_code=c,true] u16 W25QXX_TYPE=W25Q128; //默认是W25Q128 //4Kbytes为一个Sector //16个扇区为1个Block //W25Q128 //容量为16M字节,共有128个Block,4096个Sector //初始化SPI FLASH的IO口 void W25QXX_Init(void) { RCC->AHB1ENR|=1>8)); SPI1_ReadWriteByte((u8)WriteAddr); for(i=0;i256)pageremain=256; //一次可以写入256个字节 else pageremain=NumByteToWrite; ...
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165540
金钱
165540
注册时间
2010-12-1
在线时间
2117 小时
发表于 2016-1-12 14:09:55 | 显示全部楼层
[mw_shl_code=c,true]
u16 W25QXX_TYPE=W25Q128;        //默认是W25Q128

//4Kbytes为一个Sector
//16个扇区为1个Block
//W25Q128
//容量为16M字节,共有128个Block,4096个Sector
                                                                                                         
//初始化SPI FLASH的IO口
void W25QXX_Init(void)
{
        RCC->AHB1ENR|=1<<1;     //使能PORTB时钟
        RCC->AHB1ENR|=1<<6;     //使能PORTG时钟
        GPIO_Set(GPIOB,PIN14,GPIO_MODE_OUT,GPIO_OTYPE_PP,GPIO_SPEED_100M,GPIO_PUPD_PU);        //PB14推挽输出
        GPIO_Set(GPIOG,PIN7,GPIO_MODE_OUT,GPIO_OTYPE_PP,GPIO_SPEED_100M,GPIO_PUPD_PU);        //PG7推挽输出
        GPIOG->ODR|=1<<7;                //PG7输出1,防止NRF干扰SPI FLASH的通信  
        W25QXX_CS=1;                        //SPI FLASH不选中
        SPI1_Init();                                           //初始化SPI
        SPI1_SetSpeed(SPI_SPEED_2);                //设置为42M时钟,高速模式
        W25QXX_TYPE=W25QXX_ReadID();        //读取FLASH ID.
}  

//读取W25QXX的状态寄存器
//BIT7  6   5   4   3   2   1   0
//SPR   RV  TB BP2 BP1 BP0 WEL BUSY
//SPR:默认0,状态寄存器保护位,配合WP使用
//TB,BP2,BP1,BP0:FLASH区域写保护设置
//WEL:写使能锁定
//BUSY:忙标记位(1,忙;0,空闲)
//默认:0x00
u8 W25QXX_ReadSR(void)   
{  
        u8 byte=0;   
        W25QXX_CS=0;                            //使能器件   
        SPI1_ReadWriteByte(W25X_ReadStatusReg);    //发送读取状态寄存器命令   
        byte=SPI1_ReadWriteByte(0Xff);             //读取一个字节  
        W25QXX_CS=1;                            //取消片选     
        return byte;   
}
//写W25QXX状态寄存器
//只有SPR,TB,BP2,BP1,BP0(bit 7,5,4,3,2)可以写!!!
void W25QXX_Write_SR(u8 sr)   
{   
        W25QXX_CS=0;                            //使能器件   
        SPI1_ReadWriteByte(W25X_WriteStatusReg);   //发送写取状态寄存器命令   
        SPI1_ReadWriteByte(sr);               //写入一个字节  
        W25QXX_CS=1;                            //取消片选                  
}   
//W25QXX写使能       
//将WEL置位   
void W25QXX_Write_Enable(void)   
{
        W25QXX_CS=0;                            //使能器件   
    SPI1_ReadWriteByte(W25X_WriteEnable);      //发送写使能  
        W25QXX_CS=1;                            //取消片选                  
}
//W25QXX写禁止       
//将WEL清零  
void W25QXX_Write_Disable(void)   
{  
        W25QXX_CS=0;                            //使能器件   
    SPI1_ReadWriteByte(W25X_WriteDisable);     //发送写禁止指令   
        W25QXX_CS=1;                            //取消片选                  
}                
//读取芯片ID
//返回值如下:                                  
//0XEF13,表示芯片型号为W25Q80  
//0XEF14,表示芯片型号为W25Q16   
//0XEF15,表示芯片型号为W25Q32  
//0XEF16,表示芯片型号为W25Q64
//0XEF17,表示芯片型号为W25Q128           
u16 W25QXX_ReadID(void)
{
        u16 Temp = 0;          
        W25QXX_CS=0;                                    
        SPI1_ReadWriteByte(0x90);//发送读取ID命令            
        SPI1_ReadWriteByte(0x00);             
        SPI1_ReadWriteByte(0x00);             
        SPI1_ReadWriteByte(0x00);                                    
        Temp|=SPI1_ReadWriteByte(0xFF)<<8;  
        Temp|=SPI1_ReadWriteByte(0xFF);         
        W25QXX_CS=1;                                    
        return Temp;
}                       
//读取SPI FLASH  
//在指定地址开始读取指定长度的数据
//pBuffer:数据存储区
//ReadAddr:开始读取的地址(24bit)
//NumByteToRead:要读取的字节数(最大65535)
void W25QXX_Read(u8* pBuffer,u32 ReadAddr,u16 NumByteToRead)   
{
        u16 i;                                                                                       
        W25QXX_CS=0;                            //使能器件   
    SPI1_ReadWriteByte(W25X_ReadData);         //发送读取命令   
    SPI1_ReadWriteByte((u8)((ReadAddr)>>16));  //发送24bit地址   
    SPI1_ReadWriteByte((u8)((ReadAddr)>>8));   
    SPI1_ReadWriteByte((u8)ReadAddr);   
    for(i=0;i<NumByteToRead;i++)
        {
        pBuffer=SPI1_ReadWriteByte(0XFF);   //循环读数  
    }
        W25QXX_CS=1;                                                   
}  
//SPI在一页(0~65535)内写入少于256个字节的数据
//在指定地址开始写入最大256字节的数据
//pBuffer:数据存储区
//WriteAddr:开始写入的地址(24bit)
//NumByteToWrite:要写入的字节数(最大256),该数不应该超过该页的剩余字节数!!!         
void W25QXX_Write_Page(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite)
{
        u16 i;  
    W25QXX_Write_Enable();                  //SET WEL
        W25QXX_CS=0;                            //使能器件   
    SPI1_ReadWriteByte(W25X_PageProgram);      //发送写页命令   
    SPI1_ReadWriteByte((u8)((WriteAddr)>>16)); //发送24bit地址   
    SPI1_ReadWriteByte((u8)((WriteAddr)>>8));   
    SPI1_ReadWriteByte((u8)WriteAddr);   
    for(i=0;i<NumByteToWrite;i++)SPI1_ReadWriteByte(pBuffer);//循环写数  
        W25QXX_CS=1;                            //取消片选
        W25QXX_Wait_Busy();                                           //等待写入结束
}
//无检验写SPI FLASH
//必须确保所写的地址范围内的数据全部为0XFF,否则在非0XFF处写入的数据将失败!
//具有自动换页功能
//在指定地址开始写入指定长度的数据,但是要确保地址不越界!
//pBuffer:数据存储区
//WriteAddr:开始写入的地址(24bit)
//NumByteToWrite:要写入的字节数(最大65535)
//CHECK OK
void W25QXX_Write_NoCheck(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite)   
{                                           
        u16 pageremain;          
        pageremain=256-WriteAddr%256; //单页剩余的字节数                             
        if(NumByteToWrite<=pageremain)pageremain=NumByteToWrite;//不大于256个字节
        while(1)
        {          
                W25QXX_Write_Page(pBuffer,WriteAddr,pageremain);
                if(NumByteToWrite==pageremain)break;//写入结束了
                 else //NumByteToWrite>pageremain
                {
                        pBuffer+=pageremain;
                        WriteAddr+=pageremain;       

                        NumByteToWrite-=pageremain;                          //减去已经写入了的字节数
                        if(NumByteToWrite>256)pageremain=256; //一次可以写入256个字节
                        else pageremain=NumByteToWrite;           //不够256个字节了
                }
        };            
}
//写SPI FLASH  
//在指定地址开始写入指定长度的数据
//该函数带擦除操作!
//pBuffer:数据存储区
//WriteAddr:开始写入的地址(24bit)                                               
//NumByteToWrite:要写入的字节数(最大65535)   
u8 W25QXX_BUFFER[4096];                 
void W25QXX_Write(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite)   
{
        u32 secpos;
        u16 secoff;
        u16 secremain;          
        u16 i;   
        u8 * W25QXX_BUF;          
           W25QXX_BUF=W25QXX_BUFFER;             
        secpos=WriteAddr/4096;//扇区地址  
        secoff=WriteAddr%4096;//在扇区内的偏移
        secremain=4096-secoff;//扇区剩余空间大小   
        //printf("ad:%X,nb:%X\r\n",WriteAddr,NumByteToWrite);//测试用
        if(NumByteToWrite<=secremain)secremain=NumByteToWrite;//不大于4096个字节
        while(1)
        {       
                W25QXX_Read(W25QXX_BUF,secpos*4096,4096);//读出整个扇区的内容
                for(i=0;i<secremain;i++)//校验数据
                {
                        if(W25QXX_BUF[secoff+i]!=0XFF)break;//需要擦除            
                }
                if(i<secremain)//需要擦除
                {
                        W25QXX_Erase_Sector(secpos);//擦除这个扇区
                        for(i=0;i<secremain;i++)           //复制
                        {
                                W25QXX_BUF[i+secoff]=pBuffer;          
                        }
                        W25QXX_Write_NoCheck(W25QXX_BUF,secpos*4096,4096);//写入整个扇区  

                }else W25QXX_Write_NoCheck(pBuffer,WriteAddr,secremain);//写已经擦除了的,直接写入扇区剩余区间.                                   
                if(NumByteToWrite==secremain)break;//写入结束了
                else//写入未结束
                {
                        secpos++;//扇区地址增1
                        secoff=0;//偏移位置为0          

                           pBuffer+=secremain;  //指针偏移
                        WriteAddr+=secremain;//写地址偏移          
                           NumByteToWrite-=secremain;                                //字节数递减
                        if(NumByteToWrite>4096)secremain=4096;        //下一个扇区还是写不完
                        else secremain=NumByteToWrite;                        //下一个扇区可以写完了
                }         
        };         
}
//擦除整个芯片                  
//等待时间超长...
void W25QXX_Erase_Chip(void)   
{                                   
    W25QXX_Write_Enable();                  //SET WEL
    W25QXX_Wait_Busy();   
          W25QXX_CS=0;                            //使能器件   
    SPI1_ReadWriteByte(W25X_ChipErase);        //发送片擦除命令  
        W25QXX_CS=1;                            //取消片选                  
        W25QXX_Wait_Busy();                                      //等待芯片擦除结束
}   
//擦除一个扇区
//Dst_Addr:扇区地址 根据实际容量设置
//擦除一个山区的最少时间:150ms
void W25QXX_Erase_Sector(u32 Dst_Addr)   
{  
        //监视falsh擦除情况,测试用   
        printf("fe:%x\r\n",Dst_Addr);          
        Dst_Addr*=4096;
    W25QXX_Write_Enable();                  //SET WEL          
    W25QXX_Wait_Busy();   
          W25QXX_CS=0;                            //使能器件   
    SPI1_ReadWriteByte(W25X_SectorErase);      //发送扇区擦除指令
    SPI1_ReadWriteByte((u8)((Dst_Addr)>>16));  //发送24bit地址   
    SPI1_ReadWriteByte((u8)((Dst_Addr)>>8));   
    SPI1_ReadWriteByte((u8)Dst_Addr);  
        W25QXX_CS=1;                            //取消片选                  
    W25QXX_Wait_Busy();                                      //等待擦除完成
}  
//等待空闲
void W25QXX_Wait_Busy(void)   
{   
        while((W25QXX_ReadSR()&0x01)==0x01);   // 等待BUSY位清空
}  
//进入掉电模式
void W25QXX_PowerDown(void)   
{
          W25QXX_CS=0;                            //使能器件   
    SPI1_ReadWriteByte(W25X_PowerDown);        //发送掉电命令  
        W25QXX_CS=1;                            //取消片选                  
    delay_us(3);                               //等待TPD  
}   
//唤醒
void W25QXX_WAKEUP(void)   
{  
          W25QXX_CS=0;                            //使能器件   
    SPI1_ReadWriteByte(W25X_ReleasePowerDown);   //  send W25X_PowerDown command 0xAB   
        W25QXX_CS=1;                            //取消片选                  
    delay_us(3);                               //等待TRES1
}   












[/mw_shl_code]
我是开源电子网www.openedv.com站长,有关站务问题请与我联系。
正点原子STM32开发板购买店铺http://openedv.taobao.com
正点原子官方微信公众平台,点击这里关注“正点原子”
回复

使用道具 举报

88

主题

7377

帖子

5

精华

资深版主

Rank: 8Rank: 8

积分
14980
金钱
14980
注册时间
2013-11-13
在线时间
1823 小时
发表于 2016-1-12 19:16:00 | 显示全部楼层
N25A256A就是个SPI FLASH,可以参考我们的W25Q64的实验,不过256Q需要你使能四字节地址模式
开往春天的手扶拖拉机
回复

使用道具 举报

4

主题

15

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
220
金钱
220
注册时间
2016-1-11
在线时间
28 小时
 楼主| 发表于 2016-1-13 08:41:02 | 显示全部楼层
zuozhongkai 发表于 2016-1-12 19:16
N25A256A就是个SPI FLASH,可以参考我们的W25Q64的实验,不过256Q需要你使能四字节地址模式

谢谢!
回复

使用道具 举报

4

主题

15

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
220
金钱
220
注册时间
2016-1-11
在线时间
28 小时
 楼主| 发表于 2016-1-13 08:41:25 | 显示全部楼层
正点原子 发表于 2016-1-12 14:09
[mw_shl_code=c,true]
u16 W25QXX_TYPE=W25Q128;        //默认是W25Q128

多谢原子哥!
回复

使用道具 举报

1

主题

6

帖子

0

精华

新手上路

积分
38
金钱
38
注册时间
2016-8-10
在线时间
5 小时
发表于 2020-6-5 10:25:47 | 显示全部楼层
n25q256可以使用qspi的,
回复

使用道具 举报

1

主题

6

帖子

0

精华

新手上路

积分
38
金钱
38
注册时间
2016-8-10
在线时间
5 小时
发表于 2020-6-5 10:27:59 | 显示全部楼层
qspi实例:
## .c文件
  1. /**
  2.   ******************************************************************************
  3.   * [url=home.php?mod=space&uid=175224]@file[/url]    stm32f769i_eval_qspi.c
  4.   * [url=home.php?mod=space&uid=90321]@Author[/url]  MCD Application Team
  5.   * @version V2.0.0
  6.   * @date    30-December-2016
  7.   * @brief   This file includes a standard driver for the N25Q256A QSPI
  8.   *          memory mounted on STM32F769I-EVAL board.
  9.   @verbatim
  10.   ==============================================================================
  11.                      ##### How to use this driver #####
  12.   ==============================================================================  
  13.   [..]
  14.    (#) This driver is used to drive the N25Q256A QSPI external
  15.        memory mounted on STM32F769I-EVAL evaluation board.
  16.       
  17.    (#) This driver need a specific component driver (N25Q256A) to be included with.

  18.    (#) Initialization steps:
  19.        (++) Initialize the QPSI external memory using the BSP_QSPI_Init() function. This
  20.             function includes the MSP layer hardware resources initialization and the
  21.             QSPI interface with the external memory.
  22.   
  23.    (#) QSPI memory operations
  24.        (++) QSPI memory can be accessed with read/write operations once it is
  25.             initialized.
  26.             Read/write operation can be performed with AHB access using the functions
  27.             BSP_QSPI_Read()/BSP_QSPI_Write().
  28.        (++) The function BSP_QSPI_GetInfo() returns the configuration of the QSPI memory.
  29.             (see the QSPI memory data sheet)
  30.        (++) Perform erase block operation using the function BSP_QSPI_Erase_Block() and by
  31.             specifying the block address. You can perform an erase operation of the whole
  32.             chip by calling the function BSP_QSPI_Erase_Chip().
  33.        (++) The function BSP_QSPI_GetStatus() returns the current status of the QSPI memory.
  34.             (see the QSPI memory data sheet)
  35.   @endverbatim
  36.   ******************************************************************************
  37.   * @attention
  38.   *
  39.   * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  40.   *
  41.   * Redistribution and use in source and binary forms, with or without modification,
  42.   * are permitted provided that the following conditions are met:
  43.   *   1. Redistributions of source code must retain the above copyright notice,
  44.   *      this list of conditions and the following disclaimer.
  45.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  46.   *      this list of conditions and the following disclaimer in the documentation
  47.   *      and/or other materials provided with the distribution.
  48.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  49.   *      may be used to endorse or promote products derived from this software
  50.   *      without specific prior written permission.
  51.   *
  52.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  53.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  54.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  55.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  56.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  57.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  58.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  59.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  60.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  61.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  62.   *
  63.   ******************************************************************************
  64.   */

  65. /* Includes ------------------------------------------------------------------*/
  66. #include "stm32f769i_eval_qspi.h"

  67. /** @addtogroup BSP
  68.   * @{
  69.   */

  70. /** @addtogroup STM32F769I_EVAL
  71.   * @{
  72.   */
  73.   
  74. /** @defgroup STM32F769I_EVAL_QSPI STM32F769I_EVAL QSPI
  75.   * @{
  76.   */


  77. /* Private variables ---------------------------------------------------------*/

  78. /** @defgroup STM32F769I_EVAL_QSPI_Private_Variables Private Variables
  79.   * @{
  80.   */      
  81. QSPI_HandleTypeDef QSPIHandle;

  82. /**
  83.   * @}
  84.   */



  85. /* Private functions ---------------------------------------------------------*/
  86.    
  87. /** @defgroup STM32F769I_EVAL_QSPI_Private_Functions Private Functions
  88.   * @{
  89.   */
  90. static uint8_t QSPI_ResetMemory          (QSPI_HandleTypeDef *hqspi);
  91. static uint8_t QSPI_EnterFourBytesAddress(QSPI_HandleTypeDef *hqspi);
  92. static uint8_t QSPI_DummyCyclesCfg       (QSPI_HandleTypeDef *hqspi);
  93. static uint8_t QSPI_WriteEnable          (QSPI_HandleTypeDef *hqspi);
  94. static uint8_t QSPI_AutoPollingMemReady(QSPI_HandleTypeDef *hqspi, uint32_t Timeout);

  95. /**
  96.   * @}
  97.   */
  98.    
  99. /** @defgroup STM32F769I_EVAL_QSPI_Exported_Functions Exported Functions
  100.   * @{
  101.   */

  102. /**
  103.   * @brief  Initializes the QSPI interface.
  104.   * @retval QSPI memory status
  105.   */
  106. uint8_t BSP_QSPI_Init(void)
  107. {
  108.   QSPIHandle.Instance = QUADSPI;

  109.   /* Call the DeInit function to reset the driver */
  110.   if (HAL_QSPI_DeInit(&QSPIHandle) != HAL_OK)
  111.   {
  112.     return QSPI_ERROR;
  113.   }
  114.         
  115.   /* System level initialization */
  116.   BSP_QSPI_MspInit(&QSPIHandle, NULL);
  117.   
  118.   /* QSPI initialization */
  119.   /* QSPI freq = SYSCLK /(1 + ClockPrescaler) = 216 MHz/(1+1) = 108 Mhz */
  120.   QSPIHandle.Init.ClockPrescaler     = 1;
  121.   QSPIHandle.Init.FifoThreshold      = 4;
  122.   QSPIHandle.Init.SampleShifting     = QSPI_SAMPLE_SHIFTING_HALFCYCLE;
  123.   QSPIHandle.Init.FlashSize          = POSITION_VAL(N25Q256A_FLASH_SIZE) - 1;
  124.   QSPIHandle.Init.ChipSelectHighTime = QSPI_CS_HIGH_TIME_6_CYCLE; /* Min 50ns for nonRead */
  125.   QSPIHandle.Init.ClockMode          = QSPI_CLOCK_MODE_0;
  126.   QSPIHandle.Init.FlashID            = QSPI_FLASH_ID_1;
  127.   QSPIHandle.Init.DualFlash          = QSPI_DUALFLASH_DISABLE;

  128.   if (HAL_QSPI_Init(&QSPIHandle) != HAL_OK)
  129.   {
  130.     return QSPI_ERROR;
  131.   }

  132.   /* QSPI memory reset */
  133.   if (QSPI_ResetMemory(&QSPIHandle) != QSPI_OK)
  134.   {
  135.     return QSPI_NOT_SUPPORTED;
  136.   }

  137.   /* Set the QSPI memory in 4-bytes address mode */
  138.   if (QSPI_EnterFourBytesAddress(&QSPIHandle) != QSPI_OK)
  139.   {
  140.     return QSPI_NOT_SUPPORTED;
  141.   }

  142.   /* Configuration of the dummy cycles on QSPI memory side */
  143.   if (QSPI_DummyCyclesCfg(&QSPIHandle) != QSPI_OK)
  144.   {
  145.     return QSPI_NOT_SUPPORTED;
  146.   }
  147.   
  148.   return QSPI_OK;
  149. }

  150. /**
  151.   * @brief  De-Initializes the QSPI interface.
  152.   * @retval QSPI memory status
  153.   */
  154. uint8_t BSP_QSPI_DeInit(void)
  155. {
  156.   QSPIHandle.Instance = QUADSPI;

  157.   /* Call the DeInit function to reset the driver */
  158.   if (HAL_QSPI_DeInit(&QSPIHandle) != HAL_OK)
  159.   {
  160.     return QSPI_ERROR;
  161.   }
  162.         
  163.   /* System level De-initialization */
  164.   BSP_QSPI_MspDeInit(&QSPIHandle, NULL);
  165.   
  166.   return QSPI_OK;
  167. }

  168. /**
  169.   * @brief  Reads an amount of data from the QSPI memory.
  170.   * @param  pData: Pointer to data to be read
  171.   * @param  ReadAddr: Read start address
  172.   * @param  Size: Size of data to read   
  173.   * @retval QSPI memory status
  174.   */
  175. uint8_t BSP_QSPI_Read(uint8_t* pData, uint32_t ReadAddr, uint32_t Size)
  176. {
  177.   QSPI_CommandTypeDef s_command;

  178.   /* Initialize the read command */
  179.   s_command.InstructionMode   = QSPI_INSTRUCTION_1_LINE;
  180.   s_command.Instruction       = QUAD_OUT_FAST_READ_CMD;
  181.   s_command.AddressMode       = QSPI_ADDRESS_1_LINE;
  182.   s_command.AddressSize       = QSPI_ADDRESS_32_BITS;
  183.   s_command.Address           = ReadAddr;
  184.   s_command.AlternateByteMode = QSPI_ALTERNATE_BYTES_NONE;
  185.   s_command.DataMode          = QSPI_DATA_4_LINES;
  186.   s_command.DummyCycles       = N25Q256A_DUMMY_CYCLES_READ_QUAD;
  187.   s_command.NbData            = Size;
  188.   s_command.DdrMode           = QSPI_DDR_MODE_DISABLE;
  189.   s_command.DdrHoldHalfCycle  = QSPI_DDR_HHC_ANALOG_DELAY;
  190.   s_command.SIOOMode          = QSPI_SIOO_INST_EVERY_CMD;
  191.   
  192.   /* Configure the command */
  193.   if (HAL_QSPI_Command(&QSPIHandle, &s_command, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
  194.   {
  195.     return QSPI_ERROR;
  196.   }
  197.   
  198.   /* Set S# timing for Read command */
  199.   MODIFY_REG(QSPIHandle.Instance->DCR, QUADSPI_DCR_CSHT, QSPI_CS_HIGH_TIME_3_CYCLE);
  200.   
  201.   /* Reception of the data */
  202.   if (HAL_QSPI_Receive(&QSPIHandle, pData, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
  203.   {
  204.     return QSPI_ERROR;
  205.   }
  206.   
  207.   /* Restore S# timing for nonRead commands */
  208.   MODIFY_REG(QSPIHandle.Instance->DCR, QUADSPI_DCR_CSHT, QSPI_CS_HIGH_TIME_6_CYCLE);

  209.   return QSPI_OK;
  210. }

  211. /**
  212.   * @brief  Writes an amount of data to the QSPI memory.
  213.   * @param  pData: Pointer to data to be written
  214.   * @param  WriteAddr: Write start address
  215.   * @param  Size: Size of data to write   
  216.   * @retval QSPI memory status
  217.   */
  218. uint8_t BSP_QSPI_Write(uint8_t* pData, uint32_t WriteAddr, uint32_t Size)
  219. {
  220.   QSPI_CommandTypeDef s_command;
  221.   uint32_t end_addr, current_size, current_addr;

  222.   /* Calculation of the size between the write address and the end of the page */
  223.   current_size = N25Q256A_PAGE_SIZE - (WriteAddr % N25Q256A_PAGE_SIZE);

  224.   /* Check if the size of the data is less than the remaining place in the page */
  225.   if (current_size > Size)
  226.   {
  227.     current_size = Size;
  228.   }

  229.   /* Initialize the address variables */
  230.   current_addr = WriteAddr;
  231.   end_addr = WriteAddr + Size;

  232.   /* Initialize the program command */
  233.   s_command.InstructionMode   = QSPI_INSTRUCTION_1_LINE;
  234.   s_command.Instruction       = QUAD_IN_FAST_PROG_CMD;
  235.   s_command.AddressMode       = QSPI_ADDRESS_1_LINE;
  236.   s_command.AddressSize       = QSPI_ADDRESS_32_BITS;
  237.   s_command.AlternateByteMode = QSPI_ALTERNATE_BYTES_NONE;
  238.   s_command.DataMode          = QSPI_DATA_4_LINES;
  239.   s_command.DummyCycles       = 0;
  240.   s_command.DdrMode           = QSPI_DDR_MODE_DISABLE;
  241.   s_command.DdrHoldHalfCycle  = QSPI_DDR_HHC_ANALOG_DELAY;
  242.   s_command.SIOOMode          = QSPI_SIOO_INST_EVERY_CMD;
  243.   
  244.   /* Perform the write page by page */
  245.   do
  246.   {
  247.     s_command.Address = current_addr;
  248.     s_command.NbData  = current_size;

  249.     /* Enable write operations */
  250.     if (QSPI_WriteEnable(&QSPIHandle) != QSPI_OK)
  251.     {
  252.       return QSPI_ERROR;
  253.     }
  254.    
  255.     /* Configure the command */
  256.     if (HAL_QSPI_Command(&QSPIHandle, &s_command, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
  257.     {
  258.       return QSPI_ERROR;
  259.     }
  260.    
  261.     /* Transmission of the data */
  262.      if (HAL_QSPI_Transmit(&QSPIHandle, pData, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
  263.     {
  264.       return QSPI_ERROR;
  265.     }
  266.    
  267.     /* Configure automatic polling mode to wait for end of program */  
  268.     if (QSPI_AutoPollingMemReady(&QSPIHandle, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != QSPI_OK)
  269.     {
  270.       return QSPI_ERROR;
  271.     }
  272.    
  273.     /* Update the address and size variables for next page programming */
  274.     current_addr += current_size;
  275.     pData += current_size;
  276.     current_size = ((current_addr + N25Q256A_PAGE_SIZE) > end_addr) ? (end_addr - current_addr) : N25Q256A_PAGE_SIZE;
  277.   } while (current_addr < end_addr);
  278.   
  279.   return QSPI_OK;
  280. }

  281. /**
  282.   * @brief  Erases the specified block of the QSPI memory.
  283.   * @param  BlockAddress: Block address to erase  
  284.   * @retval QSPI memory status
  285.   */
  286. uint8_t BSP_QSPI_Erase_Block(uint32_t BlockAddress)
  287. {
  288.   QSPI_CommandTypeDef s_command;

  289.   /* Initialize the erase command */
  290.   s_command.InstructionMode   = QSPI_INSTRUCTION_1_LINE;
  291.   s_command.Instruction       = SUBSECTOR_ERASE_CMD;
  292.   s_command.AddressMode       = QSPI_ADDRESS_1_LINE;
  293.   s_command.AddressSize       = QSPI_ADDRESS_32_BITS;
  294.   s_command.Address           = BlockAddress;
  295.   s_command.AlternateByteMode = QSPI_ALTERNATE_BYTES_NONE;
  296.   s_command.DataMode          = QSPI_DATA_NONE;
  297.   s_command.DummyCycles       = 0;
  298.   s_command.DdrMode           = QSPI_DDR_MODE_DISABLE;
  299.   s_command.DdrHoldHalfCycle  = QSPI_DDR_HHC_ANALOG_DELAY;
  300.   s_command.SIOOMode          = QSPI_SIOO_INST_EVERY_CMD;

  301.   /* Enable write operations */
  302.   if (QSPI_WriteEnable(&QSPIHandle) != QSPI_OK)
  303.   {
  304.     return QSPI_ERROR;
  305.   }

  306.   /* Send the command */
  307.   if (HAL_QSPI_Command(&QSPIHandle, &s_command, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
  308.   {
  309.     return QSPI_ERROR;
  310.   }
  311.   
  312.   /* Configure automatic polling mode to wait for end of erase */  
  313.   if (QSPI_AutoPollingMemReady(&QSPIHandle, N25Q256A_SUBSECTOR_ERASE_MAX_TIME) != QSPI_OK)
  314.   {
  315.     return QSPI_ERROR;
  316.   }

  317.   return QSPI_OK;
  318. }

  319. /**
  320.   * @brief  Erases the entire QSPI memory.
  321.   * @retval QSPI memory status
  322.   */
  323. uint8_t BSP_QSPI_Erase_Chip(void)
  324. {
  325.   QSPI_CommandTypeDef s_command;

  326.   /* Initialize the erase command */
  327.   s_command.InstructionMode   = QSPI_INSTRUCTION_1_LINE;
  328.   s_command.Instruction       = BULK_ERASE_CMD;
  329.   s_command.AddressMode       = QSPI_ADDRESS_NONE;
  330.   s_command.AlternateByteMode = QSPI_ALTERNATE_BYTES_NONE;
  331.   s_command.DataMode          = QSPI_DATA_NONE;
  332.   s_command.DummyCycles       = 0;
  333.   s_command.DdrMode           = QSPI_DDR_MODE_DISABLE;
  334.   s_command.DdrHoldHalfCycle  = QSPI_DDR_HHC_ANALOG_DELAY;
  335.   s_command.SIOOMode          = QSPI_SIOO_INST_EVERY_CMD;

  336.   /* Enable write operations */
  337.   if (QSPI_WriteEnable(&QSPIHandle) != QSPI_OK)
  338.   {
  339.     return QSPI_ERROR;
  340.   }

  341.   /* Send the command */
  342.   if (HAL_QSPI_Command(&QSPIHandle, &s_command, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
  343.   {
  344.     return QSPI_ERROR;
  345.   }
  346.   
  347.   /* Configure automatic polling mode to wait for end of erase */  
  348.   if (QSPI_AutoPollingMemReady(&QSPIHandle, N25Q256A_BULK_ERASE_MAX_TIME) != QSPI_OK)
  349.   {
  350.     return QSPI_ERROR;
  351.   }

  352.   return QSPI_OK;
  353. }

  354. /**
  355.   * @brief  Reads current status of the QSPI memory.
  356.   * @retval QSPI memory status
  357.   */
  358. uint8_t BSP_QSPI_GetStatus(void)
  359. {
  360.   QSPI_CommandTypeDef s_command;
  361.   uint8_t reg;

  362.   /* Initialize the read flag status register command */
  363.   s_command.InstructionMode   = QSPI_INSTRUCTION_1_LINE;
  364.   s_command.Instruction       = READ_FLAG_STATUS_REG_CMD;
  365.   s_command.AddressMode       = QSPI_ADDRESS_NONE;
  366.   s_command.AlternateByteMode = QSPI_ALTERNATE_BYTES_NONE;
  367.   s_command.DataMode          = QSPI_DATA_1_LINE;
  368.   s_command.DummyCycles       = 0;
  369.   s_command.NbData            = 1;
  370.   s_command.DdrMode           = QSPI_DDR_MODE_DISABLE;
  371.   s_command.DdrHoldHalfCycle  = QSPI_DDR_HHC_ANALOG_DELAY;
  372.   s_command.SIOOMode          = QSPI_SIOO_INST_EVERY_CMD;

  373.   /* Configure the command */
  374.   if (HAL_QSPI_Command(&QSPIHandle, &s_command, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
  375.   {
  376.     return QSPI_ERROR;
  377.   }

  378.   /* Reception of the data */
  379.   if (HAL_QSPI_Receive(&QSPIHandle, &reg, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
  380.   {
  381.     return QSPI_ERROR;
  382.   }
  383.   
  384.   /* Check the value of the register */
  385.   if ((reg & (N25Q256A_FSR_PRERR | N25Q256A_FSR_VPPERR | N25Q256A_FSR_PGERR | N25Q256A_FSR_ERERR)) != 0)
  386.   {
  387.     return QSPI_ERROR;
  388.   }
  389.   else if ((reg & (N25Q256A_FSR_PGSUS | N25Q256A_FSR_ERSUS)) != 0)
  390.   {
  391.     return QSPI_SUSPENDED;
  392.   }
  393.   else if ((reg & N25Q256A_FSR_READY) != 0)
  394.   {
  395.     return QSPI_OK;
  396.   }
  397.   else
  398.   {
  399.     return QSPI_BUSY;
  400.   }
  401. }

  402. /**
  403.   * @brief  Return the configuration of the QSPI memory.
  404.   * @param  pInfo: pointer on the configuration structure  
  405.   * @retval QSPI memory status
  406.   */
  407. uint8_t BSP_QSPI_GetInfo(QSPI_Info* pInfo)
  408. {
  409.   /* Configure the structure with the memory configuration */
  410.   pInfo->FlashSize          = N25Q256A_FLASH_SIZE;
  411.   pInfo->EraseSectorSize    = N25Q256A_SUBSECTOR_SIZE;
  412.   pInfo->EraseSectorsNumber = (N25Q256A_FLASH_SIZE/N25Q256A_SUBSECTOR_SIZE);
  413.   pInfo->ProgPageSize       = N25Q256A_PAGE_SIZE;
  414.   pInfo->ProgPagesNumber    = (N25Q256A_FLASH_SIZE/N25Q256A_PAGE_SIZE);
  415.   
  416.   return QSPI_OK;
  417. }

  418. /**
  419.   * @brief  Configure the QSPI in memory-mapped mode
  420.   * @retval QSPI memory status
  421.   */
  422. uint8_t BSP_QSPI_EnableMemoryMappedMode(void)
  423. {
  424.   QSPI_CommandTypeDef      s_command;
  425.   QSPI_MemoryMappedTypeDef s_mem_mapped_cfg;

  426.   /* Configure the command for the read instruction */
  427.   s_command.InstructionMode   = QSPI_INSTRUCTION_1_LINE;
  428.   s_command.Instruction       = QUAD_OUT_FAST_READ_CMD;
  429.   s_command.AddressMode       = QSPI_ADDRESS_1_LINE;
  430.   s_command.AddressSize       = QSPI_ADDRESS_32_BITS;
  431.   s_command.AlternateByteMode = QSPI_ALTERNATE_BYTES_NONE;
  432.   s_command.DataMode          = QSPI_DATA_4_LINES;
  433.   s_command.DummyCycles       = N25Q256A_DUMMY_CYCLES_READ_QUAD;
  434.   s_command.DdrMode           = QSPI_DDR_MODE_DISABLE;
  435.   s_command.DdrHoldHalfCycle  = QSPI_DDR_HHC_ANALOG_DELAY;
  436.   s_command.SIOOMode          = QSPI_SIOO_INST_EVERY_CMD;
  437.   
  438.   /* Configure the memory mapped mode */
  439.   s_mem_mapped_cfg.TimeOutActivation = QSPI_TIMEOUT_COUNTER_DISABLE;
  440.   s_mem_mapped_cfg.TimeOutPeriod     = 0;
  441.   
  442.   if (HAL_QSPI_MemoryMapped(&QSPIHandle, &s_command, &s_mem_mapped_cfg) != HAL_OK)
  443.   {
  444.     return QSPI_ERROR;
  445.   }

  446.   return QSPI_OK;
  447. }

  448. /**
  449.   * @}
  450.   */

  451. /** @addtogroup STM32F769I_EVAL_QSPI_Private_Functions
  452.   * @{
  453.   */

  454. /**
  455.   * @brief QSPI MSP Initialization
  456.   * @param hqspi: QSPI handle
  457.   * @param Params   
  458.   *        This function configures the hardware resources used in this example:
  459.   *           - Peripheral's clock enable
  460.   *           - Peripheral's GPIO Configuration
  461.   *           - NVIC configuration for QSPI interrupt
  462.   * @retval None
  463.   */
  464. __weak void BSP_QSPI_MspInit(QSPI_HandleTypeDef *hqspi, void *Params)
  465. {
  466.   GPIO_InitTypeDef gpio_init_structure;

  467.   /*##-1- Enable peripherals and GPIO Clocks #################################*/
  468.   /* Enable the QuadSPI memory interface clock */
  469.   QSPI_CLK_ENABLE();
  470.   /* Reset the QuadSPI memory interface */
  471.   QSPI_FORCE_RESET();
  472.   QSPI_RELEASE_RESET();
  473.   /* Enable GPIO clocks */
  474.   QSPI_CS_GPIO_CLK_ENABLE();
  475.   QSPI_CLK_GPIO_CLK_ENABLE();
  476.   QSPI_D0_GPIO_CLK_ENABLE();
  477.   QSPI_D1_GPIO_CLK_ENABLE();
  478.   QSPI_D2_GPIO_CLK_ENABLE();
  479.   QSPI_D3_GPIO_CLK_ENABLE();

  480.   /*##-2- Configure peripheral GPIO ##########################################*/
  481.   /* QSPI CS GPIO pin configuration  */
  482.   gpio_init_structure.Pin       = QSPI_CS_PIN;
  483.   gpio_init_structure.Mode      = GPIO_MODE_AF_PP;
  484.   gpio_init_structure.Pull      = GPIO_PULLUP;
  485.   gpio_init_structure.Speed     = GPIO_SPEED_FREQ_VERY_HIGH;
  486.   gpio_init_structure.Alternate = GPIO_AF10_QUADSPI;
  487.   HAL_GPIO_Init(QSPI_CS_GPIO_PORT, &gpio_init_structure);

  488.   /* QSPI CLK GPIO pin configuration  */
  489.   gpio_init_structure.Pin       = QSPI_CLK_PIN;
  490.   gpio_init_structure.Pull      = GPIO_NOPULL;
  491.   gpio_init_structure.Alternate = GPIO_AF9_QUADSPI;
  492.   HAL_GPIO_Init(QSPI_CLK_GPIO_PORT, &gpio_init_structure);

  493.   /* QSPI D0 GPIO pin configuration  */
  494.   gpio_init_structure.Pin       = QSPI_D0_PIN;
  495.   gpio_init_structure.Alternate = GPIO_AF10_QUADSPI;
  496.   HAL_GPIO_Init(QSPI_D0_GPIO_PORT, &gpio_init_structure);

  497.   /* QSPI D1 GPIO pin configuration  */
  498.   gpio_init_structure.Pin       = QSPI_D1_PIN;
  499.   gpio_init_structure.Alternate = GPIO_AF10_QUADSPI;
  500.   HAL_GPIO_Init(QSPI_D1_GPIO_PORT, &gpio_init_structure);

  501.   /* QSPI D2 GPIO pin configuration  */
  502.   gpio_init_structure.Pin       = QSPI_D2_PIN;
  503.   gpio_init_structure.Alternate = GPIO_AF9_QUADSPI;
  504.   HAL_GPIO_Init(QSPI_D2_GPIO_PORT, &gpio_init_structure);

  505.   /* QSPI D3 GPIO pin configuration  */
  506.   gpio_init_structure.Pin       = QSPI_D3_PIN;
  507.   gpio_init_structure.Alternate = GPIO_AF9_QUADSPI;
  508.   HAL_GPIO_Init(QSPI_D3_GPIO_PORT, &gpio_init_structure);

  509.   /*##-3- Configure the NVIC for QSPI #########################################*/
  510.   /* NVIC configuration for QSPI interrupt */
  511.   HAL_NVIC_SetPriority(QUADSPI_IRQn, 0x0F, 0);
  512.   HAL_NVIC_EnableIRQ(QUADSPI_IRQn);
  513. }

  514. /**
  515.   * @brief QSPI MSP De-Initialization
  516.   * @param hqspi: QSPI handle
  517.   * @param Params   
  518.   *        This function frees the hardware resources used in this example:
  519.   *          - Disable the Peripheral's clock
  520.   *          - Revert GPIO and NVIC configuration to their default state
  521.   * @retval None
  522.   */
  523. __weak void BSP_QSPI_MspDeInit(QSPI_HandleTypeDef *hqspi, void *Params)
  524. {
  525.   /*##-1- Disable the NVIC for QSPI ###########################################*/
  526.   HAL_NVIC_DisableIRQ(QUADSPI_IRQn);

  527.   /*##-2- Disable peripherals and GPIO Clocks ################################*/
  528.   /* De-Configure QSPI pins */
  529.   HAL_GPIO_DeInit(QSPI_CS_GPIO_PORT, QSPI_CS_PIN);
  530.   HAL_GPIO_DeInit(QSPI_CLK_GPIO_PORT, QSPI_CLK_PIN);
  531.   HAL_GPIO_DeInit(QSPI_D0_GPIO_PORT, QSPI_D0_PIN);
  532.   HAL_GPIO_DeInit(QSPI_D1_GPIO_PORT, QSPI_D1_PIN);
  533.   HAL_GPIO_DeInit(QSPI_D2_GPIO_PORT, QSPI_D2_PIN);
  534.   HAL_GPIO_DeInit(QSPI_D3_GPIO_PORT, QSPI_D3_PIN);

  535.   /*##-3- Reset peripherals ##################################################*/
  536.   /* Reset the QuadSPI memory interface */
  537.   QSPI_FORCE_RESET();
  538.   QSPI_RELEASE_RESET();

  539.   /* Disable the QuadSPI memory interface clock */
  540.   QSPI_CLK_DISABLE();
  541. }

  542. /**
  543.   * @brief  This function reset the QSPI memory.
  544.   * @param  hqspi: QSPI handle
  545.   * @retval None
  546.   */
  547. static uint8_t QSPI_ResetMemory(QSPI_HandleTypeDef *hqspi)
  548. {
  549.   QSPI_CommandTypeDef s_command;

  550.   /* Initialize the reset enable command */
  551.   s_command.InstructionMode   = QSPI_INSTRUCTION_1_LINE;
  552.   s_command.Instruction       = RESET_ENABLE_CMD;
  553.   s_command.AddressMode       = QSPI_ADDRESS_NONE;
  554.   s_command.AlternateByteMode = QSPI_ALTERNATE_BYTES_NONE;
  555.   s_command.DataMode          = QSPI_DATA_NONE;
  556.   s_command.DummyCycles       = 0;
  557.   s_command.DdrMode           = QSPI_DDR_MODE_DISABLE;
  558.   s_command.DdrHoldHalfCycle  = QSPI_DDR_HHC_ANALOG_DELAY;
  559.   s_command.SIOOMode          = QSPI_SIOO_INST_EVERY_CMD;

  560.   /* Send the command */
  561.   if (HAL_QSPI_Command(hqspi, &s_command, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
  562.   {
  563.     return QSPI_ERROR;
  564.   }

  565.   /* Send the reset memory command */
  566.   s_command.Instruction = RESET_MEMORY_CMD;
  567.   if (HAL_QSPI_Command(hqspi, &s_command, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
  568.   {
  569.     return QSPI_ERROR;
  570.   }

  571.   /* Configure automatic polling mode to wait the memory is ready */  
  572.   if (QSPI_AutoPollingMemReady(hqspi, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != QSPI_OK)
  573.   {
  574.     return QSPI_ERROR;
  575.   }

  576.   return QSPI_OK;
  577. }

  578. /**
  579.   * @brief  This function set the QSPI memory in 4-byte address mode
  580.   * @param  hqspi: QSPI handle
  581.   * @retval None
  582.   */
  583. static uint8_t QSPI_EnterFourBytesAddress(QSPI_HandleTypeDef *hqspi)
  584. {
  585.   QSPI_CommandTypeDef s_command;

  586.   /* Initialize the command */
  587.   s_command.InstructionMode   = QSPI_INSTRUCTION_1_LINE;
  588.   s_command.Instruction       = ENTER_4_BYTE_ADDR_MODE_CMD;
  589.   s_command.AddressMode       = QSPI_ADDRESS_NONE;
  590.   s_command.AlternateByteMode = QSPI_ALTERNATE_BYTES_NONE;
  591.   s_command.DataMode          = QSPI_DATA_NONE;
  592.   s_command.DummyCycles       = 0;
  593.   s_command.DdrMode           = QSPI_DDR_MODE_DISABLE;
  594.   s_command.DdrHoldHalfCycle  = QSPI_DDR_HHC_ANALOG_DELAY;
  595.   s_command.SIOOMode          = QSPI_SIOO_INST_EVERY_CMD;

  596.   /* Enable write operations */
  597.   if (QSPI_WriteEnable(hqspi) != QSPI_OK)
  598.   {
  599.     return QSPI_ERROR;
  600.   }

  601.   /* Send the command */
  602.   if (HAL_QSPI_Command(hqspi, &s_command, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
  603.   {
  604.     return QSPI_ERROR;
  605.   }

  606.   /* Configure automatic polling mode to wait the memory is ready */  
  607.   if (QSPI_AutoPollingMemReady(hqspi, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != QSPI_OK)
  608.   {
  609.     return QSPI_ERROR;
  610.   }

  611.   return QSPI_OK;
  612. }

  613. /**
  614.   * @brief  This function configure the dummy cycles on memory side.
  615.   * @param  hqspi: QSPI handle
  616.   * @retval None
  617.   */
  618. static uint8_t QSPI_DummyCyclesCfg(QSPI_HandleTypeDef *hqspi)
  619. {
  620.   QSPI_CommandTypeDef s_command;
  621.   uint8_t reg;

  622.   /* Initialize the read volatile configuration register command */
  623.   s_command.InstructionMode   = QSPI_INSTRUCTION_1_LINE;
  624.   s_command.Instruction       = READ_VOL_CFG_REG_CMD;
  625.   s_command.AddressMode       = QSPI_ADDRESS_NONE;
  626.   s_command.AlternateByteMode = QSPI_ALTERNATE_BYTES_NONE;
  627.   s_command.DataMode          = QSPI_DATA_1_LINE;
  628.   s_command.DummyCycles       = 0;
  629.   s_command.NbData            = 1;
  630.   s_command.DdrMode           = QSPI_DDR_MODE_DISABLE;
  631.   s_command.DdrHoldHalfCycle  = QSPI_DDR_HHC_ANALOG_DELAY;
  632.   s_command.SIOOMode          = QSPI_SIOO_INST_EVERY_CMD;

  633.   /* Configure the command */
  634.   if (HAL_QSPI_Command(hqspi, &s_command, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
  635.   {
  636.     return QSPI_ERROR;
  637.   }

  638.   /* Reception of the data */
  639.   if (HAL_QSPI_Receive(hqspi, &reg, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
  640.   {
  641.     return QSPI_ERROR;
  642.   }

  643.   /* Enable write operations */
  644.   if (QSPI_WriteEnable(hqspi) != QSPI_OK)
  645.   {
  646.     return QSPI_ERROR;
  647.   }

  648.   /* Update volatile configuration register (with new dummy cycles) */  
  649.   s_command.Instruction = WRITE_VOL_CFG_REG_CMD;
  650.   MODIFY_REG(reg, N25Q256A_VCR_NB_DUMMY, (N25Q256A_DUMMY_CYCLES_READ_QUAD << POSITION_VAL(N25Q256A_VCR_NB_DUMMY)));
  651.       
  652.   /* Configure the write volatile configuration register command */
  653.   if (HAL_QSPI_Command(hqspi, &s_command, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
  654.   {
  655.     return QSPI_ERROR;
  656.   }

  657.   /* Transmission of the data */
  658.   if (HAL_QSPI_Transmit(hqspi, &reg, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
  659.   {
  660.     return QSPI_ERROR;
  661.   }
  662.   
  663.   return QSPI_OK;
  664. }

  665. /**
  666.   * @brief  This function send a Write Enable and wait it is effective.
  667.   * @param  hqspi: QSPI handle
  668.   * @retval None
  669.   */
  670. static uint8_t QSPI_WriteEnable(QSPI_HandleTypeDef *hqspi)
  671. {
  672.   QSPI_CommandTypeDef     s_command;
  673.   QSPI_AutoPollingTypeDef s_config;

  674.   /* Enable write operations */
  675.   s_command.InstructionMode   = QSPI_INSTRUCTION_1_LINE;
  676.   s_command.Instruction       = WRITE_ENABLE_CMD;
  677.   s_command.AddressMode       = QSPI_ADDRESS_NONE;
  678.   s_command.AlternateByteMode = QSPI_ALTERNATE_BYTES_NONE;
  679.   s_command.DataMode          = QSPI_DATA_NONE;
  680.   s_command.DummyCycles       = 0;
  681.   s_command.DdrMode           = QSPI_DDR_MODE_DISABLE;
  682.   s_command.DdrHoldHalfCycle  = QSPI_DDR_HHC_ANALOG_DELAY;
  683.   s_command.SIOOMode          = QSPI_SIOO_INST_EVERY_CMD;

  684.   if (HAL_QSPI_Command(hqspi, &s_command, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
  685.   {
  686.     return QSPI_ERROR;
  687.   }
  688.   
  689.   /* Configure automatic polling mode to wait for write enabling */  
  690.   s_config.Match           = N25Q256A_SR_WREN;
  691.   s_config.Mask            = N25Q256A_SR_WREN;
  692.   s_config.MatchMode       = QSPI_MATCH_MODE_AND;
  693.   s_config.StatusBytesSize = 1;
  694.   s_config.Interval        = 0x10;
  695.   s_config.AutomaticStop   = QSPI_AUTOMATIC_STOP_ENABLE;

  696.   s_command.Instruction    = READ_STATUS_REG_CMD;
  697.   s_command.DataMode       = QSPI_DATA_1_LINE;

  698.   if (HAL_QSPI_AutoPolling(hqspi, &s_command, &s_config, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
  699.   {
  700.     return QSPI_ERROR;
  701.   }

  702.   return QSPI_OK;
  703. }

  704. /**
  705.   * @brief  This function read the SR of the memory and wait the EOP.
  706.   * @param  hqspi: QSPI handle
  707.   * @param  Timeout  
  708.   * @retval None
  709.   */
  710. static uint8_t QSPI_AutoPollingMemReady(QSPI_HandleTypeDef *hqspi, uint32_t Timeout)
  711. {
  712.   QSPI_CommandTypeDef     s_command;
  713.   QSPI_AutoPollingTypeDef s_config;

  714.   /* Configure automatic polling mode to wait for memory ready */  
  715.   s_command.InstructionMode   = QSPI_INSTRUCTION_1_LINE;
  716.   s_command.Instruction       = READ_STATUS_REG_CMD;
  717.   s_command.AddressMode       = QSPI_ADDRESS_NONE;
  718.   s_command.AlternateByteMode = QSPI_ALTERNATE_BYTES_NONE;
  719.   s_command.DataMode          = QSPI_DATA_1_LINE;
  720.   s_command.DummyCycles       = 0;
  721.   s_command.DdrMode           = QSPI_DDR_MODE_DISABLE;
  722.   s_command.DdrHoldHalfCycle  = QSPI_DDR_HHC_ANALOG_DELAY;
  723.   s_command.SIOOMode          = QSPI_SIOO_INST_EVERY_CMD;

  724.   s_config.Match           = 0;
  725.   s_config.Mask            = N25Q256A_SR_WIP;
  726.   s_config.MatchMode       = QSPI_MATCH_MODE_AND;
  727.   s_config.StatusBytesSize = 1;
  728.   s_config.Interval        = 0x10;
  729.   s_config.AutomaticStop   = QSPI_AUTOMATIC_STOP_ENABLE;

  730.   if (HAL_QSPI_AutoPolling(hqspi, &s_command, &s_config, Timeout) != HAL_OK)
  731.   {
  732.     return QSPI_ERROR;
  733.   }

  734.   return QSPI_OK;
  735. }
  736. /**
  737.   * @}
  738.   */  
  739.   
  740. /**
  741.   * @}
  742.   */
  743.   
  744. /**
  745.   * @}
  746.   */
  747.   
  748. /**
  749.   * @}
  750.   */

  751. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

复制代码


## .h文件
/**
  ******************************************************************************
  * @file    stm32f769i_eval_qspi.h
  * @author  MCD Application Team
  * @version V2.0.0
  * @date    30-December-2016
  * @brief   This file contains the common defines and functions prototypes for
  *          the stm32f769i_eval_qspi.c driver.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  *
  * Redistribution and use in source and binary forms, with or without modification,
  * are permitted provided that the following conditions are met:
  *   1. Redistributions of source code must retain the above copyright notice,
  *      this list of conditions and the following disclaimer.
  *   2. Redistributions in binary form must reproduce the above copyright notice,
  *      this list of conditions and the following disclaimer in the documentation
  *      and/or other materials provided with the distribution.
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
  *      may be used to endorse or promote products derived from this software
  *      without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  ******************************************************************************
  */

/** @addtogroup BSP
  * @{
  */

/** @addtogroup STM32F769I_EVAL
  * @{
  */
   
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __STM32F769I_EVAL_QSPI_H
#define __STM32F769I_EVAL_QSPI_H

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "stm32f7xx_hal.h"
#include "n25q256a.h"

/** @addtogroup STM32F769I_EVAL_QSPI
  * @{
  */   

  
/* Exported constants --------------------------------------------------------*/
/** @defgroup STM32F769I_EVAL_QSPI_Exported_Constants Exported Constants
  * @{
  */
/* QSPI Error codes */
#define QSPI_OK            ((uint8_t)0x00)
#define QSPI_ERROR         ((uint8_t)0x01)
#define QSPI_BUSY          ((uint8_t)0x02)
#define QSPI_NOT_SUPPORTED ((uint8_t)0x04)
#define QSPI_SUSPENDED     ((uint8_t)0x08)


/* Definition for QSPI clock resources */
#define QSPI_CLK_ENABLE()          __HAL_RCC_QSPI_CLK_ENABLE()
#define QSPI_CLK_DISABLE()         __HAL_RCC_QSPI_CLK_DISABLE()
#define QSPI_CS_GPIO_CLK_ENABLE()  __HAL_RCC_GPIOB_CLK_ENABLE()
#define QSPI_CLK_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
#define QSPI_D0_GPIO_CLK_ENABLE()  __HAL_RCC_GPIOF_CLK_ENABLE()
#define QSPI_D1_GPIO_CLK_ENABLE()  __HAL_RCC_GPIOF_CLK_ENABLE()
#define QSPI_D2_GPIO_CLK_ENABLE()  __HAL_RCC_GPIOF_CLK_ENABLE()
#define QSPI_D3_GPIO_CLK_ENABLE()  __HAL_RCC_GPIOF_CLK_ENABLE()

#define QSPI_FORCE_RESET()         __HAL_RCC_QSPI_FORCE_RESET()
#define QSPI_RELEASE_RESET()       __HAL_RCC_QSPI_RELEASE_RESET()

/* Definition for QSPI Pins */
#define QSPI_CS_PIN                GPIO_PIN_6
#define QSPI_CS_GPIO_PORT          GPIOB
#define QSPI_CLK_PIN               GPIO_PIN_10
#define QSPI_CLK_GPIO_PORT         GPIOF
#define QSPI_D0_PIN                GPIO_PIN_8
#define QSPI_D0_GPIO_PORT          GPIOF
#define QSPI_D1_PIN                GPIO_PIN_9
#define QSPI_D1_GPIO_PORT          GPIOF
#define QSPI_D2_PIN                GPIO_PIN_7
#define QSPI_D2_GPIO_PORT          GPIOF
#define QSPI_D3_PIN                GPIO_PIN_6
#define QSPI_D3_GPIO_PORT          GPIOF

/* N25Q256A13EF840E Micron memory */
/* Size of the flash */
#define QSPI_FLASH_SIZE            24     /* Address bus width to access whole memory space */
#define QSPI_PAGE_SIZE             256

/**
  * @}
  */

/* Exported types ------------------------------------------------------------*/
/** @defgroup STM32F769I_EVAL_QSPI_Exported_Types Exported Types
  * @{
  */
/* QSPI Info */
typedef struct {
  uint32_t FlashSize;          /*!< Size of the flash */
  uint32_t EraseSectorSize;    /*!< Size of sectors for the erase operation */
  uint32_t EraseSectorsNumber; /*!< Number of sectors for the erase operation */
  uint32_t ProgPageSize;       /*!< Size of pages for the program operation */
  uint32_t ProgPagesNumber;    /*!< Number of pages for the program operation */
} QSPI_Info;

/**
  * @}
  */

  
/* Exported functions --------------------------------------------------------*/
/** @addtogroup STM32F769I_EVAL_QSPI_Exported_Functions
  * @{
  */  
uint8_t BSP_QSPI_Init       (void);
uint8_t BSP_QSPI_DeInit     (void);
uint8_t BSP_QSPI_Read       (uint8_t* pData, uint32_t ReadAddr, uint32_t Size);
uint8_t BSP_QSPI_Write      (uint8_t* pData, uint32_t WriteAddr, uint32_t Size);
uint8_t BSP_QSPI_Erase_Block(uint32_t BlockAddress);
uint8_t BSP_QSPI_Erase_Chip (void);
uint8_t BSP_QSPI_GetStatus  (void);
uint8_t BSP_QSPI_GetInfo    (QSPI_Info* pInfo);
uint8_t BSP_QSPI_EnableMemoryMappedMode(void);

/* These functions can be modified in case the current settings
   need to be changed for specific application needs */
void BSP_QSPI_MspInit(QSPI_HandleTypeDef *hqspi, void *Params);
void BSP_QSPI_MspDeInit(QSPI_HandleTypeDef *hqspi, void *Params);

/**
  * @}
  */

/**
  * @}
  */

#ifdef __cplusplus
}
#endif

#endif /* __STM32F769I_EVAL_QSPI_H */
/**
  * @}
  */

/**
  * @}
  */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
回复

使用道具 举报

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

本版积分规则



关闭

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

正点原子公众号

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

GMT+8, 2025-6-28 19:08

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

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