[经验] 【瑞萨RA8D1板卡】 AT25SF128 FLASH读写测试

TL-LED   2024-9-23 17:45 楼主

测试AT25SF128 FLASH读写。

 

一、硬件电路图

开发板AT25SF128部分电路图

001.png

 

二、软件配置

 

2.1、添加SPI

002.png

 

2.2、配置SPI引脚

003.png

 

2.3、根据FLASH芯片设置此参数

004.png

 

三、程序代码

 

3.1、main.c

#include "hal_data.h"
#include "led/led.h"
#include "debug_uart/bsp_debug_uart.h"
#include "sdram/board_sdram.h"
#include "ospi_flash/bsp_ospi_flash.h"
#include "common_utils.h"

fsp_err_t err;

void R_BSP_WarmStart(bsp_warm_start_event_t event);

/*******************************************************************************************************************//**
 * [url=home.php?mod=space&uid=159083]@brief[/url] Blinky example application
 *
 * Blinks all leds at a rate of 1 second using the software delay function provided by the BSP.
 *
 **********************************************************************************************************************/
void hal_entry (void)
{
    fsp_err_t           err                         = FSP_SUCCESS;
    fsp_pack_version_t  version                     = {RESET_VALUE};
    uint32_t            flash_id                    = RESET_VALUE;
    uint8_t             * p_ospi_b_address           = NULL;

    init_led();
    Debug_UART3_Init();
    //bsp_sdram_init();
    err = ospi_b_init();

    /* Read Flash device ID */
    err = ospi_b_read_device_id(&flash_id);
    printf("ID:%lx\r\n",flash_id);

    /* 选择Flash 测试地址 */
    p_ospi_b_address = OSPI_B_APP_ADDRESS(OSPI_B_SECTOR_FIRST);
    /* Flash 读写操作测试 */
    ospi_b_operation(p_ospi_b_address);


    while (1)
    {
        R_BSP_SoftwareDelay(100, BSP_DELAY_UNITS_MILLISECONDS);
        led201_on();
        R_BSP_SoftwareDelay(100, BSP_DELAY_UNITS_MILLISECONDS);
        led201_off();
    }
}

 

3.2、bsp_ospi_flash.c

#include "ospi_flash/bsp_ospi_flash.h"
#include "stdio.h"
#include "ospi_flash/ospi_b_commands.h"
#include "common_utils.h"


extern spi_flash_direct_transfer_t g_ospi_b_direct_transfer [OSPI_B_TRANSFER_MAX];
/* Global variables */
uint8_t g_read_data [OSPI_B_APP_DATA_SIZE]  = {RESET_VALUE};
const uint8_t g_write_data [OSPI_B_APP_DATA_SIZE] = {
    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
    0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
    0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F,
    0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
};

fsp_err_t ospi_b_read_device_id (uint32_t * const p_id)
{
    fsp_err_t                   err             = FSP_SUCCESS;
    spi_flash_direct_transfer_t transfer        = {RESET_VALUE};

    /* Read and check flash device ID */
    transfer = g_ospi_b_direct_transfer[OSPI_B_TRANSFER_READ_DEVICE_ID_SPI];
    err = R_OSPI_B_DirectTransfer(&g_ospi_flash_ctrl, &transfer, SPI_FLASH_DIRECT_TRANSFER_DIR_READ);
		
    if(err!=FSP_SUCCESS)
		{
				printf("R_OSPI_B_DirectTransfer API FAILED \r\n");
		}
    /* Get flash device ID */
    *p_id = transfer.data;
		
    return err;
}

/*******************************************************************************************************************//**
 * @brief       This functions enables write and verify the read data.
 * @param       None
 * @retval      FSP_SUCCESS     Upon successful operation
 * @retval      FSP_ERR_ABORTED Upon incorrect read data.
 * @retval      Any Other Error code apart from FSP_SUCCESS Unsuccessful operation
 **********************************************************************************************************************/
static fsp_err_t ospi_b_write_enable (void)
{
    fsp_err_t                   err             = FSP_SUCCESS;
    spi_flash_direct_transfer_t transfer        = {RESET_VALUE};

    /* Transfer write enable command */
    transfer = g_ospi_b_direct_transfer[OSPI_B_TRANSFER_WRITE_ENABLE_SPI];
    err = R_OSPI_B_DirectTransfer(&g_ospi_flash_ctrl, &transfer, SPI_FLASH_DIRECT_TRANSFER_DIR_WRITE);

    /* Read Status Register */
    transfer = g_ospi_b_direct_transfer[OSPI_B_TRANSFER_READ_STATUS_SPI];
    err = R_OSPI_B_DirectTransfer(&g_ospi_flash_ctrl, &transfer, SPI_FLASH_DIRECT_TRANSFER_DIR_READ);

    /* Check Write Enable bit in Status Register */
    if(OSPI_B_WEN_BIT_MASK != (transfer.data & OSPI_B_WEN_BIT_MASK))
    {
        printf("Write enable FAILED\r\n");
    }
    return err;
}

/*******************************************************************************************************************//**
 * @brief       This function wait until OSPI operation completes.
 * @param[in]   timeout         Maximum waiting time
 * @retval      FSP_SUCCESS     Upon successful wait OSPI operating
 * @retval      FSP_ERR_TIMEOUT Upon time out
 * @retval      Any Other Error code apart from FSP_SUCCESS Unsuccessful operation.
 **********************************************************************************************************************/
static fsp_err_t ospi_b_wait_operation (uint32_t timeout)
{
    fsp_err_t          err    = FSP_SUCCESS;
    spi_flash_status_t status = {RESET_VALUE};

    status.write_in_progress = true;
    while (status.write_in_progress)
    {
        /* Get device status */
        R_OSPI_B_StatusGet(&g_ospi_flash_ctrl, &status);
        if(RESET_VALUE == timeout)
        {
            printf("OSPI time out occurred\r\n");
        }
        R_BSP_SoftwareDelay(1, OSPI_B_TIME_UNIT);
        timeout --;
    }
    return err;
}

/**********************************************************************************************************************
 * @brief       This function performs an erase sector operation on the flash device.
 * @param[in]   *p_address  Pointer to flash device memory address
 * @param[out]  *p_time     Pointer will be used to store execute time
 * @retval      FSP_SUCCESS Upon successful erase operation
 * @retval      Any Other Error code apart from FSP_SUCCESS Unsuccessful operation
 **********************************************************************************************************************/
static fsp_err_t ospi_b_erase_operation (uint8_t * const p_address)
{
    fsp_err_t   err             = FSP_SUCCESS;
    uint32_t    sector_size     = RESET_VALUE;
    uint32_t    erase_timeout   = RESET_VALUE;

    /* Check sector size according to input address pointer, described in S28HS512T data sheet */
    if(OSPI_B_SECTOR_4K_END_ADDRESS < (uint32_t)p_address)
    {
        sector_size = OSPI_B_SECTOR_SIZE_256K;
        erase_timeout = OSPI_B_TIME_ERASE_256K;
    }
    else
    {
        sector_size = OSPI_B_SECTOR_SIZE_4K;
        erase_timeout = OSPI_B_TIME_ERASE_4K;
    }

    /* Performs erase sector */
    err = R_OSPI_B_Erase(&g_ospi_flash_ctrl, p_address, sector_size);

    /* Wait till operation completes */
    err = ospi_b_wait_operation(erase_timeout);

    return err;
}

/**********************************************************************************************************************
 * @brief       This function performs an write operation on the flash device.
 * @param[in]   *p_address      Pointer to flash device memory address
 * @param[out]  *p_time     Pointer will be used to store execute time
 * @retval      FSP_SUCCESS Upon successful write operation
 * @retval      Any Other Error code apart from FSP_SUCCESS Unsuccessful operation
 **********************************************************************************************************************/
static fsp_err_t ospi_b_write_operation (uint8_t * const p_address)
{
    fsp_err_t   err         = FSP_SUCCESS;

    /* Erase sector before write data to flash device */
    err = ospi_b_erase_operation(p_address);
		
    /* Write data to flash device */
    err = R_OSPI_B_Write(&g_ospi_flash_ctrl, g_write_data, p_address, OSPI_B_APP_DATA_SIZE);

    /* Wait until write operation completes */
    err = ospi_b_wait_operation(OSPI_B_TIME_WRITE);

    return err;
}

/**********************************************************************************************************************
 * @brief       This function performs an read operation on the flash device.
 * @param[in]   *p_address  Pointer to flash device memory address
 * @param[out]  *p_time     Pointer will be used to store execute time
 * @retval      FSP_SUCCESS Upon successful read operation
 * @retval      Any Other Error code apart from FSP_SUCCESS Unsuccessful operation
 **********************************************************************************************************************/
static fsp_err_t ospi_b_read_operation (uint8_t * const p_address)
{
    fsp_err_t err = FSP_SUCCESS;

    /* Clean read buffer */
    memset(&g_read_data, RESET_VALUE, OSPI_B_APP_DATA_SIZE);

    /* Read data from flash device */
    memcpy(&g_read_data, p_address, OSPI_B_APP_DATA_SIZE);

    return err;
}

/*******************************************************************************************************************//**
 * @brief       This functions initializes OSPI module and Flash device.
 * @param       None
 * @retval      FSP_SUCCESS     Upon successful initialization of OSPI module and Flash device
 * @retval      Any Other Error code apart from FSP_SUCCESS  Unsuccessful open
 **********************************************************************************************************************/
fsp_err_t ospi_b_init (void)
{
    /* By default, the flash device is in SPI mode, so it is necessary to open the OSPI module in SPI mode */
    fsp_err_t                   err             = FSP_SUCCESS;

    /* Open OSPI module */
    err = R_OSPI_B_Open(&g_ospi_flash_ctrl, &g_ospi_flash_cfg);

    /* Switch OSPI module to 1S-1S-1S mode to configure flash device */
    err = R_OSPI_B_SpiProtocolSet(&g_ospi_flash_ctrl, SPI_FLASH_PROTOCOL_EXTENDED_SPI);

    /* Reset flash device by driving OM_RESET pin */
    R_XSPI->LIOCTL_b.RSTCS0 = 0;
    R_BSP_SoftwareDelay(OSPI_B_TIME_RESET_PULSE, OSPI_B_TIME_UNIT);
    R_XSPI->LIOCTL_b.RSTCS0 = 1;
    R_BSP_SoftwareDelay(OSPI_B_TIME_RESET_SETUP, OSPI_B_TIME_UNIT);

    /* Transfer write enable command */
    err = ospi_b_write_enable();

    return err;
}


fsp_err_t ospi_b_operation (uint8_t * p_address)
{
    fsp_err_t       err                         = FSP_SUCCESS;
    uint16_t i = 0;

    err = ospi_b_erase_operation(p_address);
    if(err==FSP_SUCCESS)
    {
        printf("Write %d bytes completed successfully\r\n", (int)(OSPI_B_APP_DATA_SIZE));
    }
    else
    {
        printf("Write operation failure\r\n");
    }
    printf("Write Data:\r\n");
    for(i=0;i<=OSPI_B_APP_DATA_SIZE-1;i++)
    {
        printf("%d ",g_write_data[i]);
    }
    err = ospi_b_read_operation (p_address);

    if(err==FSP_SUCCESS)
    {
            /* Print execution time */
        printf("\r\nRead %d bytes completed successfully\r\n", (int)(OSPI_B_APP_DATA_SIZE));
    }
    else
    {
        printf("\r\nRead operation failure\r\n");
    }
    printf("Read Data:\r\n");
    for(i=0;i<=sizeof(g_read_data)-1;i++)
    {
        printf("%d ",g_read_data[i]);
    }

    if(RESET_VALUE == memcmp(&g_read_data, &g_write_data, (size_t)OSPI_B_APP_DATA_SIZE))
    {
        printf("\r\nData read matched data written\r\n");
        printf("flash读写数据成功\r\n");
    }
    else
    {
        printf("\r\nData read does not match data written\r\n");
        printf("flash读写数据失败\r\n");
    }

    /* Performs OSPI erase operation */
    err = ospi_b_erase_operation(p_address);
    if(err==FSP_SUCCESS)
    {
            /* Print execution time */
        printf("Erase sector completed successfully\r\n");
    }
    else
    {
        printf("erase operation failure\r\n");
    }
    return err;
}

 

四、运行结果

 

读写数据测试结果

005.png

 

回复评论

暂无评论,赶紧抢沙发吧
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复