TL-LED 发表于 2024-9-23 17:45

【瑞萨RA8D1板卡】 AT25SF128 FLASH读写测试

<p><span style="font-size:16px;">测试AT25SF128 FLASH读写。</span></p>

<p>&nbsp;</p>

<p><span style="font-size:16px;"><strong>一、硬件电路图</strong></span></p>

<p><span style="font-size:16px;">开发板AT25SF128部分电路图</span></p>

<p><span style="font-size:16px;"></span></p>

<p>&nbsp;</p>

<p><span style="font-size:16px;"><strong>二、软件配置</strong></span></p>

<p>&nbsp;</p>

<p><span style="font-size:16px;">2.1、添加SPI</span></p>

<p><span style="font-size:16px;"></span></p>

<p>&nbsp;</p>

<p><span style="font-size:16px;">2.2、配置SPI引脚</span></p>

<p><span style="font-size:16px;"></span></p>

<p>&nbsp;</p>

<p><span style="font-size:16px;">2.3、根据FLASH芯片设置此参数</span></p>

<p><span style="font-size:16px;"></span></p>

<p>&nbsp;</p>

<p><strong><span style="font-size:16px;">三、程序代码</span></strong></p>

<p>&nbsp;</p>

<p><span style="font-size:16px;">3.1、main.c</span></p>

<pre>
<code>#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);

/*******************************************************************************************************************//**
* @brief 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_tversion                     = {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(&amp;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();
    }
}</code></pre>

<p>&nbsp;</p>

<p><span style="font-size:16px;">3.2、bsp_ospi_flash.c</span></p>

<pre>
<code>#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 ;
/* Global variables */
uint8_t g_read_data = {RESET_VALUE};
const uint8_t g_write_data = {
    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;
    err = R_OSPI_B_DirectTransfer(&amp;g_ospi_flash_ctrl, &amp;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;
    err = R_OSPI_B_DirectTransfer(&amp;g_ospi_flash_ctrl, &amp;transfer, SPI_FLASH_DIRECT_TRANSFER_DIR_WRITE);

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

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

/*******************************************************************************************************************//**
* @brief       This function wait until OSPI operation completes.
* @param   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(&amp;g_ospi_flash_ctrl, &amp;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   *p_addressPointer to flash device memory address
* @param*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 &lt; (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(&amp;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   *p_address      Pointer to flash device memory address
* @param*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(&amp;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   *p_addressPointer to flash device memory address
* @param*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(&amp;g_read_data, RESET_VALUE, OSPI_B_APP_DATA_SIZE);

    /* Read data from flash device */
    memcpy(&amp;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_SUCCESSUnsuccessful 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(&amp;g_ospi_flash_ctrl, &amp;g_ospi_flash_cfg);

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

    /* Reset flash device by driving OM_RESET pin */
    R_XSPI-&gt;LIOCTL_b.RSTCS0 = 0;
    R_BSP_SoftwareDelay(OSPI_B_TIME_RESET_PULSE, OSPI_B_TIME_UNIT);
    R_XSPI-&gt;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&lt;=OSPI_B_APP_DATA_SIZE-1;i++)
    {
      printf("%d ",g_write_data);
    }
    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&lt;=sizeof(g_read_data)-1;i++)
    {
      printf("%d ",g_read_data);
    }

    if(RESET_VALUE == memcmp(&amp;g_read_data, &amp;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;
}

</code></pre>

<p>&nbsp;</p>

<p><strong><span style="font-size:16px;">四、运行结果</span></strong></p>

<p>&nbsp;</p>

<p><span style="font-size:16px;">读写数据测试结果</span></p>

<p><span style="font-size:16px;"></span></p>

<p>&nbsp;</p>

秦天qintian0303 发表于 2024-12-8 20:41

<p>楼主您好,这个AT25SF128 FLASH读写测试的工程可以分享一下吗?</p>

TL-LED 发表于 2024-12-9 14:23

秦天qintian0303 发表于 2024-12-8 20:41
楼主您好,这个AT25SF128 FLASH读写测试的工程可以分享一下吗?

<p>当时测试的工程文件,我现在忘记存到哪里了,我找找。</p>

秦天qintian0303 发表于 2024-12-9 15:05

TL-LED 发表于 2024-12-9 14:23
当时测试的工程文件,我现在忘记存到哪里了,我找找。

<p>辛苦辛苦<img height="48" src="https://bbs.eeworld.com.cn/static/editor/plugins/hkemoji/sticker/facebook/congra.gif" width="48" />&nbsp;&nbsp;</p>

TL-LED 发表于 2024-12-9 22:26

秦天qintian0303 发表于 2024-12-9 15:05
辛苦辛苦&nbsp;&nbsp;

<div></div>
页: [1]
查看完整版本: 【瑞萨RA8D1板卡】 AT25SF128 FLASH读写测试