【Follow me第二季第3期】quad-spi flash和octo-spi flash程序配置及读写速度测试
<p>测试quad-spi flash和octo-spi flash读写速度对比,如下图</p><p > </p>
<p > </p>
<p >对应代码如下:</p>
<p >进入主函数(main.c)找到菜单任务</p>
<p > </p>
<p >通过菜单任务逐布找到菜单选项部分程序,也就是串口调试页面的菜单选项,如下</p>
<p > </p>
<p > </p>
<p >进入spi测试部分程序</p>
<p > </p>
<p > </p>
<p >SPI读写测试程序及转换程序如下</p>
<p align="left" >ospi_performance_test (block_size_actual, &ospi_performance_write_result, &ospi_performance_read_result);</p>
<p align="left" > </p>
<p align="left" >ospi_write_result = ((100000000 / timer_frequency) * ospi_performance_write_result) / 100;</p>
<p align="left" >qspi_write_result = ((100000000 / timer_frequency) * qspi_write_test(block_size_actual)) / 100;</p>
<p align="left" > </p>
<p align="left" >print_to_console((uint8_t *)"Writing to flash completed\r\n");</p>
<p align="left" >print_to_console((uint8_t *)"\r\nReading the text block from external Quad-SPI and Octo-SPI flash memories...\r\n");</p>
<p align="left" > </p>
<p align="left" >ospi_read_result = ((100000000 / timer_frequency) * ospi_performance_read_result) / 100;</p>
<p align="left" >qspi_read_result = ((100000000 / timer_frequency) * qspi_read_test(block_size_actual)) / 100;</p>
<p > </p>
<p >ospi数据写入测试程序如下:</p>
<p >static uint32_t write_dopi_ospi(uint32_t data_size)</p>
<p >{</p>
<p > fsp_err_t err;</p>
<p > uint32_t test_var;</p>
<p > uint8_t * p_dest = (uint8_t *)OSPI_DMA_ADDRESS;</p>
<p > timer_status_t status = {};</p>
<p > uint32_t number_of_pages;</p>
<p > R_GPT_Open(g_memory_performance.p_ctrl, g_memory_performance.p_cfg);</p>
<p > number_of_pages = (uint32_t)(data_size * (1024 / OSPI_TEST_PAGE_SIZE));</p>
<p > </p>
<p >for (test_var = 0; test_var < number_of_pages; test_var++ )</p>
<p > {</p>
<p > R_GPT_Start(g_memory_performance.p_ctrl);</p>
<p > err = R_OSPI_Write(g_ospi.p_ctrl, s_page, p_dest, OSPI_TEST_PAGE_SIZE);</p>
<p > if (FSP_SUCCESS != err)</p>
<p > {</p>
<p > __asm("bkpt");</p>
<p > }</p>
<p > ospi_test_wait_until_wip();</p>
<p > p_dest += OSPI_TEST_PAGE_SIZE;</p>
<p > R_GPT_Stop(g_memory_performance.p_ctrl);</p>
<p > vTaskDelay(1U);</p>
<p > }</p>
<p > </p>
<p > R_GPT_StatusGet(g_memory_performance.p_ctrl, &status);</p>
<p > R_GPT_Reset(g_memory_performance.p_ctrl);</p>
<p > R_GPT_Close(g_memory_performance.p_ctrl);</p>
<p > </p>
<p > return (status.counter);</p>
<p >}</p>
<p > </p>
<p >ospi数据读取测试程序如下:</p>
<p >static uint32_t read_dopi_ospi(uint32_t data_size)</p>
<p >{</p>
<p > uint32_t * p_src = (uint32_t *)OSPI_DMA_ADDRESS;</p>
<p > uint32_t * p_dest = (uint32_t *)s_perf_read;</p>
<p > timer_status_t status = {};</p>
<p > R_GPT_Open(g_memory_performance.p_ctrl, g_memory_performance.p_cfg);</p>
<p >#ifdef READ_PAGE_BY_PAGE</p>
<p > number_of_blocks = data_size * ( 1024 / OSPI_TEST_PAGE_SIZE);</p>
<p > </p>
<p > while(number_of_blocks--)</p>
<p > {</p>
<p > data = (OSPI_TEST_PAGE_SIZE) / 4U;</p>
<p > R_GPT_Start(g_memory_performance.p_ctrl);</p>
<p > </p>
<p >#ifdef MANUAL_READ</p>
<p > while (data)</p>
<p > {</p>
<p > *p_dest = *p_src;</p>
<p > p_dest++;</p>
<p > p_src++;</p>
<p > data--;</p>
<p > }</p>
<p >#endif</p>
<p > </p>
<p >#define READ_MEMCPY</p>
<p >#ifdef READ_MEMCPY</p>
<p > memcpy ( p_dest, p_src, OSPI_TEST_PAGE_SIZE / 4);</p>
<p > p_src += OSPI_TEST_PAGE_SIZE / 4;</p>
<p > p_dest += OSPI_TEST_PAGE_SIZE / 4;</p>
<p > R_GPT_Stop(g_memory_performance.p_ctrl);</p>
<p > vTaskDelay(1U);</p>
<p >#endif</p>
<p > }</p>
<p >#endif</p>
<p > </p>
<p >#ifdef READ_AS_SINGLE_BLOCK</p>
<p > R_GPT_Start(g_memory_performance.p_ctrl);</p>
<p > R_OSPI_XipEnter(g_ospi.p_ctrl);</p>
<p > memcpy (p_dest, p_src, (data_size * 1024) / 4);</p>
<p > R_OSPI_XipExit(g_ospi.p_ctrl);</p>
<p > R_GPT_Stop(g_memory_performance.p_ctrl);</p>
<p >#endif</p>
<p > </p>
<p > R_GPT_StatusGet(g_memory_performance.p_ctrl, &status);</p>
<p > R_GPT_Reset(g_memory_performance.p_ctrl);</p>
<p > R_GPT_Close(g_memory_performance.p_ctrl);</p>
<p > return (status.counter);</p>
<p >}</p>
<p > </p>
<p >qspi数据写入测试程序如下:</p>
<p >static uint32_t qspi_write_test(uint32_t block_size)</p>
<p >{</p>
<p > fsp_err_t fsp_err;</p>
<p > uint32_t qspi_write_result = 0;</p>
<p > timer_status_t status = {};</p>
<p > fsp_err_t err = FSP_SUCCESS;</p>
<p > spi_flash_protocol_t current_spi_mode;</p>
<p > </p>
<p > block_size *= 1024;</p>
<p > current_spi_mode = SPI_FLASH_PROTOCOL_EXTENDED_SPI;</p>
<p > err = qpi_init();</p>
<p > if (FSP_SUCCESS == err)</p>
<p > {</p>
<p > current_spi_mode = g_qspi_cfg.spi_protocol;</p>
<p > }</p>
<p > uint32_t page_write_count = 0;</p>
<p > uint8_t * p_mem_addr;</p>
<p > p_mem_addr = (uint8_t *)QSPI_DEVICE_START_ADDRESS;</p>
<p > while (((page_write_count * SECTOR_SIZE) < block_size)</p>
<p > && ( FSP_SUCCESS == err ) )</p>
<p > {</p>
<p > err = R_QSPI_Erase(&g_qspi_ctrl, p_mem_addr, SECTOR_SIZE);</p>
<p > if (FSP_SUCCESS != err)</p>
<p > {</p>
<p > sprintf(s_print_buffer, "R_QSPI_Erase Failed\r\n");</p>
<p > }</p>
<p > else</p>
<p > {</p>
<p > err = get_flash_status();</p>
<p > if (FSP_SUCCESS != err)</p>
<p > {</p>
<p > sprintf(s_print_buffer, "Failed to get status for QSPI operation\r\n");</p>
<p > }</p>
<p > uint32_t count;</p>
<p > for (count = 0; count < SECTOR_SIZE; count++ )</p>
<p > {</p>
<p > if (DEFAULT_MEM_VAL != p_mem_addr)</p>
<p > {</p>
<p > err = FSP_ERR_NOT_ERASED;</p>
<p > }</p>
<p > }</p>
<p > }</p>
<p > p_mem_addr += SECTOR_SIZE;</p>
<p > page_write_count++;</p>
<p > }</p>
<p > fsp_err = R_GPT_Start(g_memory_performance.p_ctrl);</p>
<p > if (FSP_SUCCESS != fsp_err)</p>
<p > {</p>
<p > SYSTEM_ERROR</p>
<p > }</p>
<p > p_mem_addr = (uint8_t *)QSPI_DEVICE_START_ADDRESS;</p>
<p > page_write_count = 0;</p>
<p > while (((page_write_count * PAGE_WRITE_SIZE) < block_size)</p>
<p > && (FSP_SUCCESS == err))</p>
<p > {</p>
<p > if (FSP_SUCCESS == err)</p>
<p > {</p>
<p > err = R_QSPI_Write(&g_qspi_ctrl, &(sp_source), p_mem_addr, PAGE_WRITE_SIZE);</p>
<p > if (FSP_SUCCESS != err)</p>
<p > {</p>
<p > sprintf(s_print_buffer, "R_QSPI_Write Failed\r\n");</p>
<p > }</p>
<p > else</p>
<p > {</p>
<p > err = get_flash_status();</p>
<p > if (FSP_SUCCESS != err)</p>
<p > {</p>
<p > sprintf(s_print_buffer, "Failed to get status for QSPI operation\r\n");</p>
<p > }</p>
<p > }</p>
<p > }</p>
<p > p_mem_addr += PAGE_WRITE_SIZE;</p>
<p > page_write_count++;</p>
<p > }</p>
<p > deinit_qspi(current_spi_mode);</p>
<p > fsp_err = R_GPT_Stop(g_memory_performance.p_ctrl);</p>
<p > if (FSP_SUCCESS != fsp_err)</p>
<p > {</p>
<p > SYSTEM_ERROR</p>
<p > }</p>
<p > fsp_err = R_GPT_StatusGet(g_memory_performance.p_ctrl, &status);</p>
<p > if (FSP_SUCCESS != fsp_err)</p>
<p > {</p>
<p > SYSTEM_ERROR</p>
<p > }</p>
<p > fsp_err = R_GPT_Reset(g_memory_performance.p_ctrl);</p>
<p > if (FSP_SUCCESS != fsp_err)</p>
<p > {</p>
<p > SYSTEM_ERROR</p>
<p > }</p>
<p > qspi_write_result = status.counter;</p>
<p > return (qspi_write_result);</p>
<p >}</p>
<p > </p>
<p >qspi数据读取测试程序如下:</p>
<p >static uint32_t qspi_read_test(uint32_t block_size)</p>
<p >{</p>
<p > fsp_err_t fsp_err;</p>
<p > fsp_err_t err = FSP_SUCCESS;</p>
<p > uint32_t qspi_read_result = 0;</p>
<p > timer_status_t status = {};</p>
<p > spi_flash_protocol_t current_spi_mode;</p>
<p > uint8_t * p_dma_read_buffer;</p>
<p > uint32_t page_read_count;</p>
<p > uint8_t * p_mem_addr;</p>
<p > block_size *= 1024;</p>
<p > p_dma_read_buffer = pvPortMalloc(block_size);</p>
<p > if (NULL == p_dma_read_buffer)</p>
<p > {</p>
<p > HeapStats_t pxHeapStats;</p>
<p > vPortGetHeapStats(&pxHeapStats);</p>
<p > sprintf(s_print_buffer, "\r\nQSPI malloc operation Failed - Max free mem: %dbytes\r\n",</p>
<p > pxHeapStats.xSizeOfLargestFreeBlockInBytes);</p>
<p > err = FSP_ERR_NOT_ERASED;</p>
<p > } </p>
<p > current_spi_mode = SPI_FLASH_PROTOCOL_EXTENDED_SPI;</p>
<p > err = qpi_init();</p>
<p > if (FSP_SUCCESS == err)</p>
<p > {</p>
<p > current_spi_mode = g_qspi_cfg.spi_protocol;</p>
<p > }</p>
<p > fsp_err = R_GPT_Start(g_memory_performance.p_ctrl);</p>
<p > if (FSP_SUCCESS != fsp_err)</p>
<p > {</p>
<p > SYSTEM_ERROR</p>
<p > }</p>
<p > page_read_count = 0;</p>
<p > p_mem_addr = (uint8_t *)QSPI_DEVICE_START_ADDRESS;</p>
<p > while (((page_read_count * PAGE_WRITE_SIZE) < block_size)</p>
<p > && (FSP_SUCCESS == err))</p>
<p > {</p>
<p > if ((fsp_err_t) (memcmp (p_mem_addr, &(sp_source), PAGE_WRITE_SIZE)) != FSP_SUCCESS)</p>
<p > {</p>
<p > err = FSP_ERR_NOT_ERASED;</p>
<p > sprintf(s_print_buffer, "\r\nQSPI operation Failed -> Data read does not match with written data\r\n");</p>
<p > }</p>
<p > p_mem_addr += PAGE_WRITE_SIZE;</p>
<p > page_read_count++;</p>
<p > }</p>
<p > fsp_err = R_GPT_Stop(g_memory_performance.p_ctrl);</p>
<p > deinit_qspi(current_spi_mode);</p>
<p > if (FSP_SUCCESS != fsp_err)</p>
<p > {</p>
<p > SYSTEM_ERROR</p>
<p > }</p>
<p > fsp_err = R_GPT_StatusGet(g_memory_performance.p_ctrl, &status);</p>
<p > if (FSP_SUCCESS != fsp_err)</p>
<p > {</p>
<p > SYSTEM_ERROR</p>
<p > }</p>
<p > fsp_err = R_GPT_Reset(g_memory_performance.p_ctrl);</p>
<p > if (FSP_SUCCESS != fsp_err)</p>
<p > {</p>
<p > SYSTEM_ERROR</p>
<p > }</p>
<p > qspi_read_result = status.counter;</p>
<p > vPortFree(p_dma_read_buffer);</p>
<p > return (qspi_read_result);</p>
<p >}</p>
页:
[1]