瑞萨e2studio(19)----串口获取数据通过SPI存储于W25Q128外部flash

概述

SPI是串行外设接口(Serial Peripheral Interface)的缩写,是一种高速的,全双工,同步的通信总线,并且在芯片的管脚上只占用四根线,节约了芯片的管脚,同时为PCB的布局上节省空间,提供方便,正是出于这种简单易用的特性,越来越多的芯片集成了这种通信协议,比如 EEPROM,FLASH,实时时钟,AD转换器。
W25Q128 是一款SPI接口的Flash芯片,其存储空间为 128Mbit,相当于16M字节。W25Q128可以支持 SPI 的模式 0 和模式 3,也就是 CPOL=0/CPHA=0 和CPOL=1/CPHA=1 这两种模式。

本篇文章主要介绍如何使用e2studio对瑞萨进行spi配置,同时移植stm32上的W25Q128到瑞萨上,同时通过对该FLASH进行读写操作,验证是否正确。

往期STM32CUBEMX(13)–SPI,W25Q128外部Flash移植
https://blog.csdn.net/qq_24312945/article/details/117756829

硬件准备

首先需要准备一个开发板,这里我准备的是芯片型号R7FA2L1AB2DFL的开发板:

在这里插入图片描述
Flash就是淘宝上SPI接口的W25Q128模块。
在这里插入图片描述

线序接法

RA2L1W25Q128
P103CS
P102CLK
P101DI
P100DO

新建工程

在这里插入图片描述

工程模板

在这里插入图片描述

保存工程路径

在这里插入图片描述

芯片配置

本文中使用R7FA2L1AB2DFL来进行演示。
在这里插入图片描述

工程模板选择

在这里插入图片描述

SPI配置

点击Stacks->New Stack->Driver->Connectivity->SPI Driver on r_spi。

在这里插入图片描述

SPI属性配置

在这里插入图片描述

片选CS管脚设置

设置P103管脚为输出管脚,作为CS片选。
在这里插入图片描述

设置e2studio堆栈

在这里插入图片描述

e2studio的重定向printf设置

在这里插入图片描述

C++ 构建->设置->GNU ARM Cross C Linker->Miscellaneous去掉Other linker flags中的 “–specs=rdimon.specs”
在这里插入图片描述

uart配置

点击Stacks->New Stack->Driver->Connectivity -> UART Driver on r_sci_uart。
在这里插入图片描述

uart属性配置

配置串口,用于打印数据。
在这里插入图片描述

printf输出重定向到串口

打印最常用的方法是printf,所以要解决的问题是将printf的输出重定向到串口,然后通过串口将数据发送出去。
注意一定要加上头文件#include <stdio.h>

#ifdef __GNUC__                                 //串口重定向
    #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
    #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif

PUTCHAR_PROTOTYPE
{
        err = R_SCI_UART_Write(&g_uart0_ctrl, (uint8_t *)&ch, 1);
        if(FSP_SUCCESS != err) __BKPT();
        while(uart_send_complete_flag == false){}
        uart_send_complete_flag = false;
        return ch;
}
int _write(int fd,char *pBuffer,int size)
{
    for(int i=0;i<size;i++)
    {
        __io_putchar(*pBuffer++);
    }
    return size;
}

STM32移植瑞萨说明

在STM32的W25Qx.h中,有个片选定义,代码如下。

#define W25Qx_Enable()  HAL_GPIO_WritePin(CS_GPIO_Port, CS_Pin, GPIO_PIN_RESET)
#define W25Qx_Disable()     HAL_GPIO_WritePin(CS_GPIO_Port, CS_Pin, GPIO_PIN_SET)

修改后如下所示。

#define W25Qx_Enable()          R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_01_PIN_03, BSP_IO_LEVEL_LOW);
#define W25Qx_Disable()         R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_01_PIN_03, BSP_IO_LEVEL_HIGH);

在STM32的W25Qx.c中,有对数据进行发送和接受,代码如下。

 /* Send the read status command */
    HAL_SPI_Transmit(&hspi1, cmd, 1, W25Qx_TIMEOUT_VALUE);  
    /* Reception of the data */
    HAL_SPI_Receive(&hspi1,&status, 1, W25Qx_TIMEOUT_VALUE);

修改后如下所示。

 /* Send the read status command */
    g_transfer_complete = false;
    err = R_SPI_Write(&g_spi0_ctrl, cmd, 1, SPI_BIT_WIDTH_8_BITS);
    assert(FSP_SUCCESS == err);
    /* Wait for SPI_EVENT_TRANSFER_COMPLETE callback event. */
    while (  g_transfer_complete==false)
    {
        ;
    }
    /* Reception of the data */
    g_transfer_complete = false;
    err = R_SPI_Read(&g_spi0_ctrl, &status, 1, SPI_BIT_WIDTH_8_BITS);
    assert(FSP_SUCCESS == err);
    /* Wait for SPI_EVENT_TRANSFER_COMPLETE callback event. */
    while (  g_transfer_complete==false)
    {
        ;
    }

W25Q128说明

W25Q128将16M的容量分为256个块(Block),每个块大小为64K字节,每个块又分为16个扇区(Sector),每个扇区4K个字节。W25Q128的最小擦除单位为一个扇区,也就是每次必须擦除4K个字节。芯片ID如下所示。
0XEF13,表示芯片型号为W25Q80
0XEF14,表示芯片型号为W25Q16
0XEF15,表示芯片型号为W25Q32
0XEF16,表示芯片型号为W25Q64
0XEF17,表示芯片型号为W25Q128

演示效果

开机会打印W25Q128的ID,ID为0XEF17,实际如下所示。
并且之前保存的数据也正确读取出来了。

在这里插入图片描述

定义数组DataBuff,其中DataBuff[0]表示写入扇区, DataBuff[1]表示写入位置,剩下的为写入数据,同时以0xff结尾。
分别输入数据 01 02 01 02 03 04 ff与02 20 aa bb cc dd ff

在这里插入图片描述

在这里插入图片描述

W25Qx.c

/*
 * W25Qx.c
 *
 *  Created on: 2021年11月25日
 *      Author: leovo
 */


/*********************************************************************************************************
*
* File                : ws_W25Qx.c
* Hardware Environment:
* Build Environment   : RealView MDK-ARM  Version: 4.20
* Version             : V1.0
* By                  :
*
*                                  (c) Copyright 2005-2011, WaveShare
*                                       http://www.waveshare.net
*                                          All Rights Reserved
*
*********************************************************************************************************/

#include "W25Qx.h"
#include "stdlib.h"
//#include "delay.h"
#include "hal_data.h"


extern fsp_err_t err ;
extern volatile bool g_transfer_complete ;


/**
  * @brief  Initializes the W25Q128FV interface.
  * @retval None
  */
 uint8_t BSP_W25Qx_Init(void)
{
    /* Reset W25Qxxx */
    BSP_W25Qx_Reset();

    return BSP_W25Qx_GetStatus();
}

/**
  * @brief  This function reset the W25Qx.
  * @retval None
  */
static void BSP_W25Qx_Reset(void)
{
    uint8_t cmd[2] = {RESET_ENABLE_CMD,RESET_MEMORY_CMD};

    W25Qx_Enable();



    /* Send the reset command */
//    HAL_SPI_Transmit(&hspi1, cmd, 2, W25Qx_TIMEOUT_VALUE);

    g_transfer_complete = false;
    err = R_SPI_Write(&g_spi0_ctrl, cmd, 2, SPI_BIT_WIDTH_8_BITS);
    assert(FSP_SUCCESS == err);
    /* Wait for SPI_EVENT_TRANSFER_COMPLETE callback event. */
    while (  g_transfer_complete==false)
    {
        ;
    }


    W25Qx_Disable();

}

/**
  * @brief  Reads current status of the W25Q128FV.
  * @retval W25Q128FV memory status
  */
static uint8_t BSP_W25Qx_GetStatus(void)
{
    uint8_t cmd[] = {READ_STATUS_REG1_CMD};
    uint8_t status;

    W25Qx_Enable();
    /* Send the read status command */
//    HAL_SPI_Transmit(&hspi1, cmd, 1, W25Qx_TIMEOUT_VALUE);
    g_transfer_complete = false;
    err = R_SPI_Write(&g_spi0_ctrl, cmd, 1, SPI_BIT_WIDTH_8_BITS);
    assert(FSP_SUCCESS == err);
    /* Wait for SPI_EVENT_TRANSFER_COMPLETE callback event. */
    while (  g_transfer_complete==false)
    {
        ;
    }


    /* Reception of the data */
//    HAL_SPI_Receive(&hspi1,&status, 1, W25Qx_TIMEOUT_VALUE);
    g_transfer_complete = false;
    err = R_SPI_Read(&g_spi0_ctrl, &status, 1, SPI_BIT_WIDTH_8_BITS);
    assert(FSP_SUCCESS == err);
    /* Wait for SPI_EVENT_TRANSFER_COMPLETE callback event. */
    while (  g_transfer_complete==false)
    {
        ;
    }



    W25Qx_Disable();

    /* Check the value of the register */
  if((status & W25Q128FV_FSR_BUSY) != 0)
  {
    return W25Qx_BUSY;
  }
    else
    {
        return W25Qx_OK;
    }
}

/**
  * @brief  This function send a Write Enable and wait it is effective.
  * @retval None
  */
uint8_t BSP_W25Qx_WriteEnable(void)
{
    uint8_t cmd[] = {WRITE_ENABLE_CMD};
//    uint32_t tickstart = HAL_GetTick();

    /*Select the FLASH: Chip Select low */
    W25Qx_Enable();
    /* Send the read ID command */
//    HAL_SPI_Transmit(&hspi1, cmd, 1, W25Qx_TIMEOUT_VALUE);
    g_transfer_complete = false;
    err = R_SPI_Write(&g_spi0_ctrl, cmd, 1, SPI_BIT_WIDTH_8_BITS);
    assert(FSP_SUCCESS == err);
    /* Wait for SPI_EVENT_TRANSFER_COMPLETE callback event. */
    while (  g_transfer_complete==false)
    {
        ;
    }






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

    /* Wait the end of Flash writing */
    while(BSP_W25Qx_GetStatus() == W25Qx_BUSY);
    {
        /* Check for the Timeout */
//    if((HAL_GetTick() - tickstart) > W25Qx_TIMEOUT_VALUE)
//    {
//            return W25Qx_TIMEOUT;
//    }
    }

    return W25Qx_OK;
}

/**
  * @brief  Read Manufacture/Device ID.
    * @param  return value address
  * @retval None
  */
void BSP_W25Qx_Read_ID(uint8_t *ID)
{
    uint8_t cmd[4] = {READ_ID_CMD,0x00,0x00,0x00};

    W25Qx_Enable();
    /* Send the read ID command */
//    HAL_SPI_Transmit(&hspi1, cmd, 4, W25Qx_TIMEOUT_VALUE);

    g_transfer_complete = false;
    err = R_SPI_Write(&g_spi0_ctrl, cmd, 4, SPI_BIT_WIDTH_8_BITS);
    assert(FSP_SUCCESS == err);
    /* Wait for SPI_EVENT_TRANSFER_COMPLETE callback event. */
    while (  g_transfer_complete==false)
    {
        ;
    }

    R_BSP_SoftwareDelay(1, BSP_DELAY_UNITS_MILLISECONDS); // NOLINT
    /* Reception of the data */
//    HAL_SPI_Receive(&hspi1,ID, 2, W25Qx_TIMEOUT_VALUE);
    g_transfer_complete = false;
    err = R_SPI_Read(&g_spi0_ctrl, ID, 2, SPI_BIT_WIDTH_8_BITS);
     assert(FSP_SUCCESS == err);
     /* Wait for SPI_EVENT_TRANSFER_COMPLETE callback event. */
     while (  g_transfer_complete==false)
     {
         ;
     }

     R_BSP_SoftwareDelay(1, BSP_DELAY_UNITS_MILLISECONDS); // NOLINT

    W25Qx_Disable();

}

/**
  * @brief  Reads an amount of data from the QSPI memory.
  * @param  pData: Pointer to data to be read
  * @param  ReadAddr: Read start address
  * @param  Size: Size of data to read
  * @retval QSPI memory status
  */
uint8_t BSP_W25Qx_Read(uint8_t* pData, uint32_t ReadAddr, uint32_t Size)
{
    uint8_t cmd[4];

    /* Configure the command */
    cmd[0] = READ_CMD;
    cmd[1] = (uint8_t)(ReadAddr >> 16);
    cmd[2] = (uint8_t)(ReadAddr >> 8);
    cmd[3] = (uint8_t)(ReadAddr);

    W25Qx_Enable();
    /* Send the read ID command */
//    HAL_SPI_Transmit(&hspi1, cmd, 4, W25Qx_TIMEOUT_VALUE);

    g_transfer_complete = false;
    err = R_SPI_Write(&g_spi0_ctrl, cmd, 4, SPI_BIT_WIDTH_8_BITS);
    assert(FSP_SUCCESS == err);
    /* Wait for SPI_EVENT_TRANSFER_COMPLETE callback event. */
    while (  g_transfer_complete==false)
    {
        ;
    }


    /* Reception of the data */
//    if (HAL_SPI_Receive(&hspi1, pData,Size,W25Qx_TIMEOUT_VALUE) != HAL_OK)
//  {
//    return W25Qx_ERROR;
//  }

    g_transfer_complete = false;
    err = R_SPI_Read(&g_spi0_ctrl, pData, Size, SPI_BIT_WIDTH_8_BITS);
     assert(FSP_SUCCESS == err);
     /* Wait for SPI_EVENT_TRANSFER_COMPLETE callback event. */
     while (  g_transfer_complete==false)
     {
         ;
     }

    if(err!=FSP_SUCCESS)
    {
        return W25Qx_ERROR;
    }

    W25Qx_Disable();
    return W25Qx_OK;
}

/**
  * @brief  Writes an amount of data to the QSPI memory.
  * @param  pData: Pointer to data to be written
  * @param  WriteAddr: Write start address
  * @param  Size: Size of data to write,No more than 256byte.
  * @retval QSPI memory status
  */
uint8_t BSP_W25Qx_Write(uint8_t* pData, uint32_t WriteAddr, uint32_t Size)
{
    uint8_t cmd[4];
    uint32_t end_addr, current_size, current_addr;
//    uint32_t tickstart = HAL_GetTick();

    /* Calculation of the size between the write address and the end of the page */
  current_addr = 0;

  while (current_addr <= WriteAddr)
  {
    current_addr += W25Q128FV_PAGE_SIZE;
  }
  current_size = current_addr - WriteAddr;

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

  /* Initialize the adress variables */
  current_addr = WriteAddr;
  end_addr = WriteAddr + Size;

  /* Perform the write page by page */
  do
  {
        /* Configure the command */
        cmd[0] = PAGE_PROG_CMD;
        cmd[1] = (uint8_t)(current_addr >> 16);
        cmd[2] = (uint8_t)(current_addr >> 8);
        cmd[3] = (uint8_t)(current_addr);

        /* Enable write operations */
        BSP_W25Qx_WriteEnable();

        W25Qx_Enable();
    /* Send the command */
//    if (HAL_SPI_Transmit(&hspi1,cmd, 4, W25Qx_TIMEOUT_VALUE) != HAL_OK)
//    {
//      return W25Qx_ERROR;
//    }

    g_transfer_complete = false;
    err = R_SPI_Write(&g_spi0_ctrl, cmd, 4, SPI_BIT_WIDTH_8_BITS);
    assert(FSP_SUCCESS == err);
    /* Wait for SPI_EVENT_TRANSFER_COMPLETE callback event. */
    while (  g_transfer_complete==false)
    {
        ;
    }

    if(err!=FSP_SUCCESS)
    {
        return W25Qx_ERROR;
    }





    /* Transmission of the data */
//    if (HAL_SPI_Transmit(&hspi1, pData,current_size, W25Qx_TIMEOUT_VALUE) != HAL_OK)
//    {
//      return W25Qx_ERROR;
//    }


    g_transfer_complete = false;
    err = R_SPI_Write(&g_spi0_ctrl, pData, current_size, SPI_BIT_WIDTH_8_BITS);
    assert(FSP_SUCCESS == err);
    /* Wait for SPI_EVENT_TRANSFER_COMPLETE callback event. */
    while (  g_transfer_complete==false)
    {
        ;
    }

    if(err!=FSP_SUCCESS)
    {
        return W25Qx_ERROR;
    }



            W25Qx_Disable();
        /* Wait the end of Flash writing */
        while(BSP_W25Qx_GetStatus() == W25Qx_BUSY);
        {
            /* Check for the Timeout */
//            if((HAL_GetTick() - tickstart) > W25Qx_TIMEOUT_VALUE)
//            {
//                return W25Qx_TIMEOUT;
//            }
        }

    /* Update the address and size variables for next page programming */
    current_addr += current_size;
    pData += current_size;
    current_size = ((current_addr + W25Q128FV_PAGE_SIZE) > end_addr) ? (end_addr - current_addr) : W25Q128FV_PAGE_SIZE;
  } while (current_addr < end_addr);


    return W25Qx_OK;
}

/**
  * @brief  Erases the specified block of the QSPI memory.
  * @param  BlockAddress: Block address to erase
  * @retval QSPI memory status
  */
uint8_t BSP_W25Qx_Erase_Block(uint32_t Address)
{
    uint8_t cmd[4];
//    uint32_t tickstart = HAL_GetTick();
    cmd[0] = SECTOR_ERASE_CMD;
    cmd[1] = (uint8_t)(Address >> 16);
    cmd[2] = (uint8_t)(Address >> 8);
    cmd[3] = (uint8_t)(Address);

    /* Enable write operations */
    BSP_W25Qx_WriteEnable();

    /*Select the FLASH: Chip Select low */
    W25Qx_Enable();
    /* Send the read ID command */
//    HAL_SPI_Transmit(&hspi1, cmd, 4, W25Qx_TIMEOUT_VALUE);

    g_transfer_complete = false;
    err = R_SPI_Write(&g_spi0_ctrl, cmd, 4, SPI_BIT_WIDTH_8_BITS);
    assert(FSP_SUCCESS == err);
    /* Wait for SPI_EVENT_TRANSFER_COMPLETE callback event. */
    while (  g_transfer_complete==false)
    {
        ;
    }



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

    /* Wait the end of Flash writing */
    while(BSP_W25Qx_GetStatus() == W25Qx_BUSY);
    {
        /* Check for the Timeout */
//    if((HAL_GetTick() - tickstart) > W25Q128FV_SECTOR_ERASE_MAX_TIME)
//    {
//            return W25Qx_TIMEOUT;
//    }
    }
    return W25Qx_OK;
}

/**
  * @brief  Erases the entire QSPI memory.This function will take a very long time.
  * @retval QSPI memory status
  */
uint8_t BSP_W25Qx_Erase_Chip(void)
{
    uint8_t cmd[4];
    uint32_t tickstart = HAL_GetTick();
    cmd[0] = SECTOR_ERASE_CMD;

    /* Enable write operations */
    BSP_W25Qx_WriteEnable();

    /*Select the FLASH: Chip Select low */
    W25Qx_Enable();
    /* Send the read ID command */
//    HAL_SPI_Transmit(&hspi1, cmd, 1, W25Qx_TIMEOUT_VALUE);
    g_transfer_complete = false;
    err = R_SPI_Write(&g_spi0_ctrl, cmd, 1, SPI_BIT_WIDTH_8_BITS);
    assert(FSP_SUCCESS == err);
    /* Wait for SPI_EVENT_TRANSFER_COMPLETE callback event. */
    while (  g_transfer_complete==false)
    {
        ;
    }



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

    /* Wait the end of Flash writing */
    while(BSP_W25Qx_GetStatus() != W25Qx_BUSY);
    {
        /* Check for the Timeout */
    if((HAL_GetTick() - tickstart) > W25Q128FV_BULK_ERASE_MAX_TIME)
    {
            return W25Qx_TIMEOUT;
    }
    }
    return W25Qx_OK;
}




W25Qx.h

/*
 * W25Qx.h
 *
 *  Created on: 2021年11月25日
 *      Author: leovo
 */

#ifndef W25QX_H_
#define W25QX_H_

#include "stdlib.h"
#include "hal_data.h"


#include "r_spi.h"

/* Includes ------------------------------------------------------------------*/


/** @addtogroup BSP
  * @{
  */

/** @addtogroup Components
  * @{
  */

/** @addtogroup W25Q128FV
  * @{
  */

/** @defgroup W25Q128FV_Exported_Types
  * @{
  */

/**
  * @}
  */

/** @defgroup W25Q128FV_Exported_Constants
  * @{
  */

/**
  * @brief  W25Q128FV Configuration
  */
#define W25Q128FV_FLASH_SIZE                  0x1000000 /* 128 MBits => 16MBytes */
#define W25Q128FV_SECTOR_SIZE                 0x10000   /* 256 sectors of 64KBytes */
#define W25Q128FV_SUBSECTOR_SIZE              0x1000    /* 4096 subsectors of 4kBytes */
#define W25Q128FV_PAGE_SIZE                   0x100     /* 65536 pages of 256 bytes */

#define W25Q128FV_DUMMY_CYCLES_READ           4
#define W25Q128FV_DUMMY_CYCLES_READ_QUAD      10

#define W25Q128FV_BULK_ERASE_MAX_TIME         250000
#define W25Q128FV_SECTOR_ERASE_MAX_TIME       3000
#define W25Q128FV_SUBSECTOR_ERASE_MAX_TIME    800
#define W25Qx_TIMEOUT_VALUE 1000

/**
  * @brief  W25Q128FV Commands
  */
/* Reset Operations */
#define RESET_ENABLE_CMD                     0x66
#define RESET_MEMORY_CMD                     0x99

#define ENTER_QPI_MODE_CMD                   0x38
#define EXIT_QPI_MODE_CMD                    0xFF

/* Identification Operations */
#define READ_ID_CMD                          0x90
#define DUAL_READ_ID_CMD                     0x92
#define QUAD_READ_ID_CMD                     0x94
#define READ_JEDEC_ID_CMD                    0x9F

/* Read Operations */
#define READ_CMD                             0x03
#define FAST_READ_CMD                        0x0B
#define DUAL_OUT_FAST_READ_CMD               0x3B
#define DUAL_INOUT_FAST_READ_CMD             0xBB
#define QUAD_OUT_FAST_READ_CMD               0x6B
#define QUAD_INOUT_FAST_READ_CMD             0xEB

/* Write Operations */
#define WRITE_ENABLE_CMD                     0x06
#define WRITE_DISABLE_CMD                    0x04

/* Register Operations */
#define READ_STATUS_REG1_CMD                  0x05
#define READ_STATUS_REG2_CMD                  0x35
#define READ_STATUS_REG3_CMD                  0x15

#define WRITE_STATUS_REG1_CMD                 0x01
#define WRITE_STATUS_REG2_CMD                 0x31
#define WRITE_STATUS_REG3_CMD                 0x11


/* Program Operations */
#define PAGE_PROG_CMD                        0x02
#define QUAD_INPUT_PAGE_PROG_CMD             0x32


/* Erase Operations */
#define SECTOR_ERASE_CMD                     0x20
#define CHIP_ERASE_CMD                       0xC7

#define PROG_ERASE_RESUME_CMD                0x7A
#define PROG_ERASE_SUSPEND_CMD               0x75


/* Flag Status Register */
#define W25Q128FV_FSR_BUSY                    ((uint8_t)0x01)    /*!< busy */
#define W25Q128FV_FSR_WREN                    ((uint8_t)0x02)    /*!< write enable */
#define W25Q128FV_FSR_QE                      ((uint8_t)0x02)    /*!< quad enable */


#define W25Qx_Enable()          R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_01_PIN_03, BSP_IO_LEVEL_LOW);
#define W25Qx_Disable()         R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_01_PIN_03, BSP_IO_LEVEL_HIGH);

#define W25Qx_OK            ((uint8_t)0x00)
#define W25Qx_ERROR         ((uint8_t)0x01)
#define W25Qx_BUSY          ((uint8_t)0x02)
#define W25Qx_TIMEOUT               ((uint8_t)0x03)


uint8_t BSP_W25Qx_Init(void);
static void BSP_W25Qx_Reset(void);
static uint8_t BSP_W25Qx_GetStatus(void);
uint8_t BSP_W25Qx_WriteEnable(void);
void BSP_W25Qx_Read_ID(uint8_t *ID);
uint8_t BSP_W25Qx_Read(uint8_t* pData, uint32_t ReadAddr, uint32_t Size);
uint8_t BSP_W25Qx_Write(uint8_t* pData, uint32_t WriteAddr, uint32_t Size);
uint8_t BSP_W25Qx_Erase_Block(uint32_t Address);
uint8_t BSP_W25Qx_Erase_Chip(void);

/**
  * @}
  */

/** @defgroup W25Q128FV_Exported_Functions
  * @{
  */
/**
  * @}
  */

/**
  * @}
  */

/**
  * @}
  */

/**
  * @}
  */




#endif /* W25QX_H_ */

主程序代码

#include "hal_data.h"
#include <stdio.h>
#include "W25Qx.h"
FSP_CPP_HEADER
void R_BSP_WarmStart(bsp_warm_start_event_t event);
FSP_CPP_FOOTER

void uart1_data(void);

#define BUFFERSIZE 255           //可以接收的最大字符个数
uint8_t ReceiveBuff[BUFFERSIZE]; //接收缓冲区
uint8_t recv_end_flag = 0,Rx_len=0;//接收完成中断标志,接收到字符长度

uint8_t wData1[0x200];
uint8_t wData2[0x200];
uint8_t wData3[0x200];

uint8_t rData1[0x200];
uint8_t rData2[0x200];
uint8_t rData3[0x200];
uint8_t ID[4];
uint32_t i;

uint8_t flag[1] ;
int i_flag = 0;


fsp_err_t err = FSP_SUCCESS;
volatile bool uart_send_complete_flag = false;
uint8_t RxBuff[1];      //进入中断接收数据的数组
uint8_t DataBuff[5000]; //保存接收到的数据的数组
int RxLine=0;           //接收到的数据长度
int Rx_flag=0;                  //接受到数据标志
int Rx_flag_finish=0;                  //接受完成或者时间溢出
void user_uart_callback (uart_callback_args_t * p_args)
{
    if(p_args->event == UART_EVENT_TX_COMPLETE)
    {
        uart_send_complete_flag = true;
    }

    if(p_args->event ==     UART_EVENT_RX_CHAR)
    {
        RxBuff[0] = p_args->data;
        RxLine++;                      //每接收到一个数据,进入回调数据长度加1
        DataBuff[RxLine-1]=RxBuff[0];  //把每次接收到的数据保存到缓存数组
        Rx_flag=1;
        Rx_len++;
        if(RxBuff[0]==0xff)            //接收结束标志位,这个数据可以自定义,根据实际需求,这里只做示例使用,不一定是0xff
        {
            Rx_flag_finish=1;
            Rx_len--;
        }
        RxBuff[0]=0;
    }
}

#ifdef __GNUC__                                 //串口重定向
    #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
    #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif

PUTCHAR_PROTOTYPE
{
        err = R_SCI_UART_Write(&g_uart1_ctrl, (uint8_t *)&ch, 1);
        if(FSP_SUCCESS != err) __BKPT();
        while(uart_send_complete_flag == false){}
        uart_send_complete_flag = false;
        return ch;
}

int _write(int fd,char *pBuffer,int size)
{
    for(int i=0;i<size;i++)
    {
        __io_putchar(*pBuffer++);
    }
    return size;
}

volatile bool g_transfer_complete = false;
void spi_callback (spi_callback_args_t * p_args)
{
    if (SPI_EVENT_TRANSFER_COMPLETE == p_args->event)
    {
        g_transfer_complete = true;
    }
}
/*******************************************************************************************************************//**
 * main() is generated by the RA Configuration editor and is used to generate threads if an RTOS is used.  This function
 * is called by main() when no RTOS is used.
 **********************************************************************************************************************/
void hal_entry(void)
{
    /* TODO: add your own code here */

    err = R_SCI_UART_Open(&g_uart1_ctrl, &g_uart1_cfg);
    assert(FSP_SUCCESS == err);
    err = R_SPI_Open(&g_spi0_ctrl, &g_spi0_cfg);
    assert(FSP_SUCCESS == err);
    printf("\r\n SPI-W25Q128 open\n");
    /*##-1- Read the device ID  ########################*/
    BSP_W25Qx_Init();//初始化W25Q128
    BSP_W25Qx_Read_ID(ID);//读取ID
    if((ID[0] != 0xEF) | (ID[1] != 0x17))
    {
        printf("SPI-W25Q128 error");
    }
    else//ID正确,打印ID
    {
        printf("W25Q128 ID : ");
        for(i=0;i<2;i++)
        {
            printf("0x%02X ",ID[i]);
        }
        printf("\r\n\r\n");
    }

    /**************************读取第1扇区数据**************************************************************/
     /*##-3- Read the flash     ########################*/
    /*读取数据,rData读取数据的指针,起始地址0x00,读取数据长度0x200*/
    if(BSP_W25Qx_Read(rData1,0x0,0x200)== W25Qx_OK)
        printf("The first sector success\n");
    else
        printf("The first sector error\n");
    /*打印数据*/
    printf("The first sector data: \r\n");
    for(i =0;i<0x200;i++)
    {
        if(i%20==0)
            printf("\nThe first sector data[%d]--data[%d]: \r\n",i,i+19);
        printf("0x%02X  ",rData1[i]);
    }
        printf("\n");
    /**************************读取第2扇区数据**************************************************************/
    /*##-3- Read the flash     ########################*/
    /*读取数据,rData读取数据的指针,起始地址0x1000,读取数据长度0x200*/
     if(BSP_W25Qx_Read(rData2,0x1000,0x200)== W25Qx_OK)
         printf("The second sector success\n");
     else
         printf("The second sector error\n");
     /*打印数据*/
    printf("The second sector data: \r\n");

    for(i =0;i<0x200;i++)
    {
        if(i%20==0)
            printf("\nThe second sector data[%d]--data[%d]: \r\n",i,i+19);
        printf("0x%02X  ",rData2[i]);
        }
    printf("\n");
    /**************************读取第3扇区数据**************************************************************/
    /*##-3- Read the flash     ########################*/
    /*读取数据,rData读取数据的指针,起始地址0x2000,读取数据长度0x200*/
    if(BSP_W25Qx_Read(rData3,0x2000,0x200)== W25Qx_OK)
        printf("The third  sector success\n");
    else
        printf("The third  sector error\n");
    /*打印数据*/
     printf("The third  sector data: \r\n");
     for(i =0;i<0x200;i++)
    {
         if(i%20==0)
             printf("\nThe third  sector data[%d]--data[%d]: \r\n",i,i+19);
         printf("0x%02X  ",rData3[i]);
    }
    printf("\n");
    /**************************清除第1扇区数据为0**************************************************************/
    /*##-1- Erase Block ##################################*/
    if(BSP_W25Qx_Erase_Block(0) == W25Qx_OK)
        printf(" QSPI Erase Block ok\r\n");
    else
        printf("error\r\n");
    /*##-1- Written to the flash ########################*/
     /* fill buffer */
     printf(" Clear the first sector data[0]--data[0x200]\r\n");
     for(i =0;i<0x200;i ++)
     {
         wData1[i] = 0;
         rData1[i] = 0;
     }
     /*写入数据,wData写入数据的指针,起始地址0x00,写入数据长度0x200*/
     if(BSP_W25Qx_Write(wData1,0x00,0x200)== W25Qx_OK)
         printf("Clear success\r\n");
     else
         printf("Clear error\r\n");

     /*##-1- Read the flash     ########################*/
     /*读取数据,rData读取数据的指针,起始地址0x00,读取数据长度0x200*/
     if(BSP_W25Qx_Read(rData1,0x00,0x200)== W25Qx_OK)
         printf("Read the first sector data[0]--data[0x200]\r\n\r\n");
     else
         printf("Read error\r\n\r\n");
     /*打印数据*/
     printf("the first sector data[0]--data[0x200]: \r\n");
     for(i =0;i<0x200;i++)
     {
         if(i%20==0)
             printf("\ndata[%d]--data[%d]:\r\n",i,i+19);
         printf("0x%02X  ",rData1[i]);
     }
     printf("\n");
    /**************************清除第2扇区数据为0**************************************************************/
    /*##-2- Erase Block ##################################*/
    if(BSP_W25Qx_Erase_Block(0x1000) == W25Qx_OK)
        printf(" QSPI Erase Block ok\r\n");
    else
        printf("error\r\n");
    /*##-2- Written to the flash ########################*/
    /* fill buffer */
    printf(" Clear the second sector data[0]--data[0x200]\r\n");
    for(i =0;i<0x200;i ++)
    {
        wData2[i] = 0;
        rData2[i] = 0;
    }
    /*写入数据,wData写入数据的指针,起始地址0x1000,写入数据长度0x200*/
    if(BSP_W25Qx_Write(wData2,0x1000,0x200)== W25Qx_OK)
        printf("Clear success\r\n");
    else
        printf("Clear error\r\n");
    /*##-2- Read the flash     ########################*/
    /*读取数据,rData读取数据的指针,起始地址0x00,读取数据长度0x200*/
    if(BSP_W25Qx_Read(rData2,0x1000,0x200)== W25Qx_OK)
        printf("Read the second sector data[0]--data[0x200]\r\n\r\n");
    else
        printf("Read error\r\n\r\n");
    /*打印数据*/
    printf("the first sector data[0]--data[0x200]: \r\n");
    for(i =0;i<0x200;i++)
    {
        if(i%20==0)
            printf("\ndata[%d]--data[%d]:\r\n",i,i+19);
        printf("0x%02X  ",rData2[i]);
        }
    printf("\n");
    /**************************清除第3扇区数据为0**************************************************************/
    /*##-3- Erase Block ##################################*/
    if(BSP_W25Qx_Erase_Block(0x2000) == W25Qx_OK)
        printf(" QSPI Erase Block ok\r\n");
    else
        printf("error\r\n");
    /*##-3- Written to the flash ########################*/
    /* fill buffer */
    printf(" Clear the third sector data[0]--data[0x200]\r\n");
    for(i =0;i<0x200;i ++)
    {
        wData3[i] = 0;
        rData3[i] = 0;
    }
    /*写入数据,wData写入数据的指针,起始地址0x2000,写入数据长度0x200*/
    if(BSP_W25Qx_Write(wData3,0x2000,0x200)== W25Qx_OK)
        printf("Clear success\r\n");
    else
        printf("Clear error\r\n");
    /*##-3- Read the flash     ########################*/
    /*读取数据,rData读取数据的指针,起始地址0x00,读取数据长度0x200*/
    if(BSP_W25Qx_Read(rData3,0x2000,0x200)== W25Qx_OK)
        printf("Read the third sector data[0]--data[0x200]\r\n\r\n");
    else
        printf("Read error\r\n\r\n");
    /*打印数据*/
    printf("the first third data[0]--data[0x200]: \r\n");
    for(i =0;i<0x200;i++)
    {
        if(i%20==0)
            printf("\ndata[%d]--data[%d]:\r\n",i,i+19);
        printf("0x%02X  ",rData3[i]);
    }
                printf("\n");
    while(1)
    {
        uart1_data();
        R_BSP_SoftwareDelay(100, BSP_DELAY_UNITS_MILLISECONDS); // NOLINT100->160
    }
#if BSP_TZ_SECURE_BUILD
    /* Enter non-secure code */
    R_BSP_NonSecureEnter();
#endif
}



void uart1_data(void)
{
    if(Rx_flag_finish ==1)//接收完成标志
    {
        if(DataBuff[0]==0x01)
        {
            printf("LENGTH:%d\n",Rx_len-2);
            for(int i =0;i<Rx_len-2;i++)
            {
                wData1[ (i+DataBuff[1]) ] = DataBuff[i+2];
            }
        /*##-1- Erase Block ##################################*/
        if(BSP_W25Qx_Erase_Block(0) == W25Qx_OK)
            printf(" QSPI Erase Block ok\r\n");
        else
            printf(" error\r\n");
        /*写入数据,wData写入数据的指针,起始地址0x00,写入数据长度0x200*/
        if(BSP_W25Qx_Write(wData1,0x00,0x200)== W25Qx_OK)
            printf("Sector 1 is successfully written\r\n");
        else
            printf(" error\r\n");
        if(BSP_W25Qx_Read(rData1,0x00,0x200)== W25Qx_OK)
            printf("Read the first  sector data[0]--data[0x200]\r\n\r\n");
        else
            printf(" error\r\n");
        /*打印数据*/
        for(i =0;i<0x200;i++)
        {
            if(i%20==0)
                printf("\ndata[%d]--data[%d]:\r\n",i,i+19);
                    printf("0x%02X  ",rData1[i]);
        }
        printf("\n");
    }
    else if(DataBuff[0]==0x02)
    {
            printf("LENGTH:%d\n",Rx_len-2);
            for(int i =0;i<Rx_len-2;i++)
            {
                wData2[ (i+DataBuff[1]) ] = DataBuff[i+2];
            }
        /*##-2- Erase Block ##################################*/
        if(BSP_W25Qx_Erase_Block(0x1000) == W25Qx_OK)
            printf(" QSPI Erase Block ok\r\n");
        else
            printf(" error\r\n");
        /*写入数据,wData写入数据的指针,起始地址0x1000,写入数据长度0x200*/
        if(BSP_W25Qx_Write(wData2,0x1000,0x200)== W25Qx_OK)
            printf("Sector 2 is successfully written\r\n");
        else
            printf(" error\r\n");
        if(BSP_W25Qx_Read(rData2,0x1000,0x200)== W25Qx_OK)
            printf("Read the second  sector data[0]--data[0x200]\r\n\r\n");
        else
            printf(" error\r\n");
        /*打印数据*/
        for(i =0;i<0x200;i++)
        {
            if(i%20==0)
                printf("\ndata[%d]--data[%d]:\r\n",i,i+19);
                    printf("0x%02X  ",rData2[i]);
        }
        printf("\n");
    }
    else if(DataBuff[0]==0x03)
    {
            printf("LENGTH:%d\n",Rx_len-2);
            for(int i =0;i<Rx_len-2;i++)
            {
                wData3[ (i+DataBuff[1]) ] = DataBuff[i+2];
            }
        /*##-2- Erase Block ##################################*/
        if(BSP_W25Qx_Erase_Block(0x2000) == W25Qx_OK)
            printf(" QSPI Erase Block ok\r\n");
        else
            printf(" error\r\n");
        /*写入数据,wData写入数据的指针,起始地址0x2000,写入数据长度0x200*/
        if(BSP_W25Qx_Write(wData3,0x2000,0x200)== W25Qx_OK)
            printf("Sector 3 is successfully written\r\n");
        else
            printf(" error\r\n");
        if(BSP_W25Qx_Read(rData3,0x2000,0x200)== W25Qx_OK)
            printf("Read the third sector data[0]--data[0x200]\r\n\r\n");
        else
            printf(" error\r\n");
        /*打印数据*/
        for(i =0;i<0x200;i++)
        {
            if(i%20==0)
                printf("\ndata[%d]--data[%d]:\r\n",i,i+19);
                    printf("0x%02X  ",rData3[i]);
        }
        printf("\n");
    }
        else
            printf("error!");
    for(int i = 0; i < Rx_len+1 ; i++) //清空接收缓存区
        DataBuff[i]=0;//置0
    Rx_len=0;//接收数据长度清零
    Rx_flag_finish=0;//接收标志位清零
    RxLine=0;
    }
}


最后

以上的代码会在Q群里分享。QQ群:615061293。
或者关注微信公众号『记帖』,持续更新文章和学习资料,可加作者的微信交流学习!
在这里插入图片描述

posted @ 2022-05-28 22:04  记帖  阅读(691)  评论(0编辑  收藏  举报