V1.0
This commit is contained in:
@@ -0,0 +1,280 @@
|
||||
/*!
|
||||
* \file fmc_spi.c
|
||||
*
|
||||
* \brief Target fmc spi implementation
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include "fmc_spi.h"
|
||||
#include "xc_drv_fmc_spi.h"
|
||||
#include "xc_drv_fmc_spi_dma.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
#define FLASH_MAX_SIZE 256 * 1024
|
||||
#define FLASH_MAX_PAGE_NUM (FLASH_MAX_SIZE / FLASH_PAGE_SIZE)
|
||||
#define FLASH_MAX_SECTOR_NUM (FLASH_MAX_SIZE / FLASH_SECTOR_SIZE)
|
||||
|
||||
#define TEST_BASE_ADDR 0x6400
|
||||
#define TEST_START_PAGE_IDX (TEST_BASE_ADDR / FLASH_PAGE_SIZE)
|
||||
#define TEST_START_SECTOR_IDX (TEST_BASE_ADDR / FLASH_SECTOR_SIZE)
|
||||
|
||||
#define TEST_CYCLE_NUM 4u
|
||||
|
||||
#define FLASH_ALL_BIT_TEST 0u
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
void fmc_spi_flash_all_bit_test(void)
|
||||
{
|
||||
uint8_t data[FLASH_PAGE_SIZE];
|
||||
uint8_t rd_data[FLASH_PAGE_SIZE];
|
||||
uint8_t cycle_num = 3;
|
||||
|
||||
while (cycle_num <= TEST_CYCLE_NUM) {
|
||||
DEBUG("---------------- %d Cycle Start ----------------\n", cycle_num);
|
||||
for (uint16_t n = TEST_START_SECTOR_IDX; n < FLASH_MAX_SECTOR_NUM; n++) {
|
||||
memset(data, 0x55, FLASH_PAGE_SIZE);
|
||||
|
||||
xc_fmc_spi_flash_erase_sector(n * FLASH_SECTOR_SIZE);
|
||||
DEBUG("Sector 0x%x Erased.\n", n * FLASH_SECTOR_SIZE);
|
||||
|
||||
for(int i=0;i<FLASH_SECTOR_SIZE/FLASH_PAGE_SIZE;i++){
|
||||
xc_fmc_spi_flash_write_page((n*FLASH_SECTOR_SIZE + i*FLASH_PAGE_SIZE), data, FLASH_PAGE_SIZE);
|
||||
DEBUG("Page 0x%x has been Written.\n", n*FLASH_SECTOR_SIZE + i*FLASH_PAGE_SIZE);
|
||||
}
|
||||
|
||||
for(int i=0;i<FLASH_SECTOR_SIZE/FLASH_PAGE_SIZE;i++){
|
||||
xc_fmc_spi_flash_read_page((n*FLASH_SECTOR_SIZE + i*FLASH_PAGE_SIZE) , rd_data, FLASH_PAGE_SIZE);
|
||||
|
||||
for (uint16_t i = 0; i < FLASH_PAGE_SIZE; i++) {
|
||||
if (rd_data[i] != 0x55){
|
||||
DEBUG("line=%d,err,data[%d]=0x%x,rd_data[%d]=0x%x\n",__LINE__,i,data[i],i,rd_data[i]);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
memset(rd_data, 0, FLASH_PAGE_SIZE);
|
||||
DEBUG("--- Sector 0x%x Check Complete ---\n", n * FLASH_SECTOR_SIZE);
|
||||
}
|
||||
DEBUG("********** Code 0x55 Test End **********\n");
|
||||
|
||||
for (uint16_t n = TEST_START_SECTOR_IDX; n < FLASH_MAX_SECTOR_NUM; n++) {
|
||||
memset(data, 0xaa, FLASH_PAGE_SIZE);
|
||||
|
||||
|
||||
xc_fmc_spi_flash_erase_sector( n * FLASH_SECTOR_SIZE);
|
||||
DEBUG("Sector 0x%x Erased.\n", n * FLASH_SECTOR_SIZE);
|
||||
|
||||
|
||||
for(int i=0;i<FLASH_SECTOR_SIZE/FLASH_PAGE_SIZE;i++){
|
||||
xc_fmc_spi_flash_write_page((n*FLASH_SECTOR_SIZE + i*FLASH_PAGE_SIZE), data, FLASH_PAGE_SIZE);
|
||||
DEBUG("Page 0x%x has been Written.\n", n*FLASH_SECTOR_SIZE + i*FLASH_PAGE_SIZE);
|
||||
}
|
||||
|
||||
for(int i=0;i<FLASH_SECTOR_SIZE/FLASH_PAGE_SIZE;i++){
|
||||
xc_fmc_spi_flash_read_page((n*FLASH_SECTOR_SIZE + i*FLASH_PAGE_SIZE) , rd_data, FLASH_PAGE_SIZE);
|
||||
|
||||
for (uint16_t i = 0; i < FLASH_PAGE_SIZE; i++) {
|
||||
if (rd_data[i] != 0xaa){
|
||||
DEBUG("line=%d,err,data[%d]=0x%x,rd_data[%d]=0x%x\n",__LINE__,i,data[i],i,rd_data[i]);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
memset(rd_data, 0, FLASH_PAGE_SIZE);
|
||||
|
||||
DEBUG("--- Sector 0x%x Check Complete ---\n", n * FLASH_SECTOR_SIZE);
|
||||
}
|
||||
DEBUG("********** Code 0xAA Test End **********\n");
|
||||
|
||||
for (uint16_t n = TEST_START_SECTOR_IDX; n < FLASH_MAX_SECTOR_NUM; n++) {
|
||||
|
||||
memset(data, 0x00, FLASH_PAGE_SIZE);
|
||||
|
||||
xc_fmc_spi_flash_erase_sector(n * FLASH_SECTOR_SIZE);
|
||||
DEBUG("Sector 0x%x Erased.\n", n * FLASH_SECTOR_SIZE);
|
||||
|
||||
for(int i=0;i<FLASH_SECTOR_SIZE/FLASH_PAGE_SIZE;i++){
|
||||
xc_fmc_spi_flash_write_page((n*FLASH_SECTOR_SIZE + i*FLASH_PAGE_SIZE), data, FLASH_PAGE_SIZE);
|
||||
DEBUG("Page 0x%x has been Written.\n", n*FLASH_SECTOR_SIZE + i*FLASH_PAGE_SIZE);
|
||||
}
|
||||
|
||||
for(int i=0;i<FLASH_SECTOR_SIZE/FLASH_PAGE_SIZE;i++){
|
||||
xc_fmc_spi_flash_read_page((n*FLASH_SECTOR_SIZE + i*FLASH_PAGE_SIZE) , rd_data, FLASH_PAGE_SIZE);
|
||||
for (uint16_t i = 0; i < FLASH_PAGE_SIZE; i++) {
|
||||
if (rd_data[i] != 0x00){
|
||||
DEBUG("line=%d,err,data[%d]=0x%x,rd_data[%d]=0x%x\n",__LINE__,i,data[i],i,rd_data[i]);
|
||||
while(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
memset(rd_data, 0, FLASH_PAGE_SIZE);
|
||||
DEBUG("--- Sector 0x%x Check Complete ---\n", n * FLASH_SECTOR_SIZE);
|
||||
}
|
||||
DEBUG("********** Code 0x00 Test End **********\n");
|
||||
|
||||
DEBUG("----------------- %d Cycle End -----------------\n", cycle_num);
|
||||
cycle_num++;
|
||||
}
|
||||
}
|
||||
|
||||
void fmc_spi_flash_any_addr_test(void)
|
||||
{
|
||||
uint8_t data[FLASH_PAGE_SIZE];
|
||||
uint8_t rd_data[FLASH_PAGE_SIZE];
|
||||
uint16_t start_addr = 28*1024;
|
||||
|
||||
xc_fmc_spi_flash_erase_sector( start_addr);
|
||||
for (uint16_t i = 0; i < FLASH_SECTOR_SIZE/FLASH_PAGE_SIZE; i++) {
|
||||
xc_fmc_spi_flash_read_page( start_addr, data, FLASH_PAGE_SIZE);
|
||||
for (uint16_t i = 0; i < FLASH_PAGE_SIZE; i++) {
|
||||
if(data[1] != 0xff){
|
||||
DEBUG("line=%d,err,data[%d]=0x%x,rd_data[%d]=0x%x\n",__LINE__,i,data[i],i,rd_data[i]);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
DEBUG("Erase Success\n");
|
||||
|
||||
|
||||
for (uint16_t i = 0; i < FLASH_PAGE_SIZE; i++) {
|
||||
data[i] = i;
|
||||
}
|
||||
|
||||
for (uint8_t i = 0; i < 16; i++) {
|
||||
|
||||
data[0]++;
|
||||
xc_fmc_spi_flash_write_page(start_addr + i * FLASH_PAGE_SIZE, data, FLASH_PAGE_SIZE);
|
||||
|
||||
memset(rd_data, 0xff, FLASH_PAGE_SIZE);
|
||||
xc_fmc_spi_flash_read_page(start_addr + i * FLASH_PAGE_SIZE, rd_data, FLASH_PAGE_SIZE);
|
||||
|
||||
for(int j=0; j<FLASH_PAGE_SIZE; j++){
|
||||
if(data[j] != rd_data[j]){
|
||||
DEBUG("line=%d,err,data[%d]=0x%x,rd_data[%d]=0x%x\n",__LINE__,i,data[i],i,rd_data[i]);
|
||||
return;
|
||||
}
|
||||
}
|
||||
DEBUG("Flash Read finished%d: \n", i);
|
||||
}
|
||||
|
||||
|
||||
uint8_t w_pos = 57;
|
||||
|
||||
xc_fmc_spi_flash_erase_sector(start_addr);
|
||||
|
||||
memset(data, 0x11, sizeof(data));
|
||||
xc_fmc_spi_flash_write(start_addr + w_pos, data, sizeof(data));
|
||||
DEBUG("Flash Write Any finish1\n");
|
||||
|
||||
memset(rd_data, 0xFF, sizeof(rd_data));
|
||||
xc_fmc_spi_flash_read(start_addr, rd_data, sizeof(rd_data));
|
||||
|
||||
|
||||
for (uint16_t i = 0; i < sizeof(rd_data); i++){
|
||||
if(i>=w_pos && i<(w_pos+sizeof(data))){
|
||||
if(data[i-w_pos] != rd_data[i]){
|
||||
DEBUG("line=%d,err,data[%d]=0x%x,rd_data[%d]=0x%x\n",__LINE__,i,data[i],i,rd_data[i]);
|
||||
return;
|
||||
}
|
||||
}else{
|
||||
if(rd_data[i] != 0xFF){
|
||||
DEBUG("line=%d,err,data[%d]=0x%x,rd_data[%d]=0x%x\n",__LINE__,i,data[i],i,rd_data[i]);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
DEBUG("Flash Read Any finish1\n");
|
||||
|
||||
w_pos = 196;
|
||||
xc_fmc_spi_flash_erase_sector(start_addr);
|
||||
memset(data, 0x22, sizeof(data));
|
||||
xc_fmc_spi_flash_write(start_addr + w_pos, data, 115);
|
||||
DEBUG("Flash Write Any finish2\n");
|
||||
|
||||
xc_fmc_spi_flash_read(start_addr, rd_data, sizeof(rd_data));
|
||||
|
||||
for (uint16_t i = 0; i < sizeof(rd_data); i++){
|
||||
if(i>=w_pos && i<(115+w_pos)){
|
||||
if(data[i-w_pos] != rd_data[i]){
|
||||
DEBUG("line=%d,err,data[%d]=0x%x,rd_data[%d]=0x%x\n",__LINE__,i,data[i],i,rd_data[i]);
|
||||
return;
|
||||
}
|
||||
}else{
|
||||
if(rd_data[i] != 0xFF){
|
||||
DEBUG("line=%d,err,data[%d]=0x%x,rd_data[%d]=0x%x\n",__LINE__,i,data[i],i,rd_data[i]);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
DEBUG("Flash Read Any finish2\n");
|
||||
}
|
||||
|
||||
/**
|
||||
****************************************************************************************
|
||||
* @brief
|
||||
*
|
||||
* @param[in]
|
||||
* @param[in]
|
||||
****************************************************************************************
|
||||
*/
|
||||
__RAM_CODE void fmc_spi_demo(void)
|
||||
{
|
||||
xc_fmc_spi_init_oprt( );
|
||||
|
||||
xc_fmc_spi_flash_wake_up( );
|
||||
|
||||
uint32_t mid = 0;
|
||||
xc_fmc_spi_flash_rdid((uint8_t*)&mid);
|
||||
|
||||
printf("Flash RDID: 0x%x\n", mid);
|
||||
|
||||
uint8_t ruid[16];
|
||||
xc_fmc_spi_flash_ruid(ruid);
|
||||
DEBUG("Flash RUID: ");
|
||||
for(int i = 0;i < 16;i++)
|
||||
{
|
||||
DEBUG("%02x ", ruid[i]);
|
||||
}
|
||||
DEBUG("\n");
|
||||
|
||||
#if (FLASH_ALL_BIT_TEST == 1)
|
||||
fmc_spi_flash_all_bit_test();
|
||||
#else
|
||||
fmc_spi_flash_any_addr_test();
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
/*!
|
||||
* \file main.c
|
||||
*
|
||||
* \brief Target main implementation
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include "main.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Func Prototype
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
void clock_init(void)
|
||||
{
|
||||
CLOCK_InitCfg_t clock_cfg;
|
||||
|
||||
clock_cfg.hfclk_src = CLOCK_HFCLK_SRC_XTAL;
|
||||
clock_cfg.hfclk_in = CLOCK_HFCLK_IN_32M;
|
||||
clock_cfg.lfclk_src = CLOCK_LFCLK_SRC_RC;
|
||||
|
||||
if (clock_cfg.lfclk_src == CLOCK_LFCLK_SRC_XTAL) {
|
||||
clock_cfg.lfclk_in = CLOCK_LFCLK_IN_32768;
|
||||
} else if (clock_cfg.lfclk_src == CLOCK_LFCLK_SRC_RC) {
|
||||
clock_cfg.lfclk_in = CLOCK_LFCLK_IN_32K;
|
||||
}
|
||||
|
||||
xc_clock_init_cfg(&clock_cfg);
|
||||
|
||||
SysTick_Config(xc_clock_hfclk_in_get() / 100);
|
||||
SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
|
||||
}
|
||||
|
||||
void app_uart_init(void)
|
||||
{
|
||||
GPIO_InitCfg_t gpio_cfg = {0};
|
||||
|
||||
gpio_cfg.Mux = GPIO_Mux0;
|
||||
gpio_cfg.Pull = GPIO_PULLUP;
|
||||
gpio_cfg.Int = NOT_INT;
|
||||
gpio_cfg.FunSel = UART0_TX;
|
||||
|
||||
gpio_cfg.Pin = GPIO_18;
|
||||
gpio_cfg.Dir = GPIO_DIR_OUTPUT;
|
||||
xc_gpio_init(&gpio_cfg);
|
||||
|
||||
gpio_cfg.FunSel = UART0_RX;
|
||||
gpio_cfg.Pin = GPIO_19;
|
||||
gpio_cfg.Dir = GPIO_DIR_INPUT;
|
||||
xc_gpio_init(&gpio_cfg);
|
||||
|
||||
UART_InitCfg_t uart_cfg = {0};
|
||||
|
||||
uart_cfg.Parity = UART_PARITY_DISABLE;
|
||||
uart_cfg.StopBits = UART_STOP_1_BITS;
|
||||
uart_cfg.WordLength = UART_DATA_8_BITS;
|
||||
uart_cfg.BaudRate = UART_BAUDRATE_115200;
|
||||
uart_cfg.HardwareFlowControl = UART_HWFC_DISABLE;
|
||||
|
||||
xc_uart_init(UART0_IDX, &uart_cfg);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Clock Initialization
|
||||
clock_init();
|
||||
|
||||
app_uart_init();
|
||||
|
||||
DEBUG("__START__\n");
|
||||
|
||||
/* fmc spi demo */
|
||||
fmc_spi_demo();
|
||||
|
||||
while (1) {
|
||||
/* main loop */
|
||||
;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user