hpw421初始版本
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
/*!
|
||||
* \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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief main
|
||||
* @details
|
||||
* @param[in] void
|
||||
* @param[out] void
|
||||
* @retval int - 0
|
||||
* @retval void
|
||||
* @par identifier
|
||||
* reserve
|
||||
* @par other
|
||||
* void
|
||||
* @par change log
|
||||
* Alex created on 2023-05-31
|
||||
*/
|
||||
int main(void)
|
||||
{
|
||||
clock_init( );
|
||||
|
||||
app_uart_init( );
|
||||
|
||||
DEBUG("__START__\n");
|
||||
|
||||
/* spi_dma_flash demo */
|
||||
spi_dma_flash_demo( );
|
||||
|
||||
|
||||
while(1)
|
||||
{
|
||||
/* main loop */
|
||||
;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,339 @@
|
||||
/*!
|
||||
* \file spi_dma_flash.c
|
||||
*
|
||||
* \brief Target spi dma flash 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 "spi_dma_flash.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 SPI_DMA_FLASH_ALL_BIT_TEST 0
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
void spi_dma_flash_all_bit_test(uint8_t spi_idx)
|
||||
{
|
||||
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_spi_dma_flash_erase_sector(spi_idx, 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_spi_dma_flash_write_page(spi_idx, (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_spi_dma_flash_read_page(spi_idx, (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_spi_dma_flash_erase_sector(spi_idx, 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_spi_dma_flash_write_page(spi_idx, (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_spi_dma_flash_read_page(spi_idx, (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_spi_dma_flash_erase_sector(spi_idx, 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_spi_dma_flash_write_page(spi_idx, (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_spi_dma_flash_read_page(spi_idx, (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]);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 spi_dma_flash_any_addr_test(uint8_t spi_idx)
|
||||
{
|
||||
uint8_t data[FLASH_PAGE_SIZE];
|
||||
uint8_t rd_data[FLASH_PAGE_SIZE];
|
||||
uint32_t start_addr = 28*1024;
|
||||
|
||||
xc_spi_dma_flash_erase_sector(spi_idx, start_addr);
|
||||
|
||||
for (uint16_t i = 0; i < FLASH_SECTOR_SIZE/FLASH_PAGE_SIZE; i++) {
|
||||
xc_spi_dma_flash_read_page(spi_idx, 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_spi_dma_flash_write_page(spi_idx, start_addr + i * FLASH_PAGE_SIZE, data, FLASH_PAGE_SIZE);
|
||||
|
||||
memset(rd_data, 0xff, FLASH_PAGE_SIZE);
|
||||
xc_spi_dma_flash_read_page(spi_idx, 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;
|
||||
|
||||
DEBUG("Flash Write Any finish\n");
|
||||
xc_spi_dma_flash_erase_sector(spi_idx, start_addr);
|
||||
|
||||
memset(data, 0x11, sizeof(data));
|
||||
xc_spi_dma_flash_write(spi_idx, start_addr+w_pos, data, sizeof(data));
|
||||
|
||||
|
||||
xc_spi_dma_flash_read(spi_idx, 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");
|
||||
|
||||
DEBUG("Flash Write Any finish2\n");
|
||||
xc_spi_dma_flash_erase_sector(spi_idx, start_addr);
|
||||
w_pos = 196;
|
||||
memset(data, 0x22, sizeof(data));
|
||||
xc_spi_dma_flash_write(spi_idx, start_addr+w_pos, data, 115);
|
||||
|
||||
|
||||
xc_spi_dma_flash_read(spi_idx, 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 spi_dma_flash_demo
|
||||
* @details
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
void spi_dma_flash_demo(void)
|
||||
{
|
||||
DEBUG("__SPI_DMA_FLASH_DEMO__\n");
|
||||
/**
|
||||
* Pay attention!!!
|
||||
* If you want the project to run in xip mode,
|
||||
* the spi0 interface cannot be used but fmc_spi interface
|
||||
* needs to be used!
|
||||
* Spi1 & Spi2 interfaces are not affected.
|
||||
*/
|
||||
uint8_t spi_idx = SPI1_IDX;
|
||||
|
||||
if (spi_idx == SPI1_IDX) {
|
||||
GPIO_InitCfg_t gpio_cfg = {0};
|
||||
|
||||
gpio_cfg.Mux = GPIO_Mux0;
|
||||
gpio_cfg.Dir = GPIO_DIR_OUTPUT;
|
||||
gpio_cfg.FunSel = SSI1_CLK;
|
||||
gpio_cfg.Int = NOT_INT;
|
||||
gpio_cfg.Pull = GPIO_PULLDOWN;
|
||||
gpio_cfg.Pin = GPIO_14;
|
||||
|
||||
xc_gpio_init(&gpio_cfg);
|
||||
|
||||
gpio_cfg.FunSel = SSI1_SSN;
|
||||
gpio_cfg.Pin = GPIO_15;
|
||||
xc_gpio_init(&gpio_cfg);
|
||||
|
||||
gpio_cfg.FunSel = SSI1_TX;
|
||||
gpio_cfg.Pin = GPIO_16;
|
||||
xc_gpio_init(&gpio_cfg);
|
||||
|
||||
gpio_cfg.FunSel = SSI1_RX;
|
||||
gpio_cfg.Pin = GPIO_17;
|
||||
xc_gpio_init(&gpio_cfg);
|
||||
} else if (spi_idx == SPI2_IDX) {
|
||||
GPIO_InitCfg_t gpio_cfg = {0};
|
||||
|
||||
gpio_cfg.Mux = GPIO_Mux2;
|
||||
gpio_cfg.Dir = GPIO_DIR_OUTPUT;
|
||||
gpio_cfg.FunSel = GPIO_Dx;
|
||||
gpio_cfg.Int = NOT_INT;
|
||||
gpio_cfg.Pull = GPIO_PULLDOWN;
|
||||
gpio_cfg.Pin = GPIO_14; // clk
|
||||
|
||||
xc_gpio_init(&gpio_cfg);
|
||||
|
||||
gpio_cfg.FunSel = GPIO_Dx;
|
||||
gpio_cfg.Pin = GPIO_15; // ssn
|
||||
xc_gpio_init(&gpio_cfg);
|
||||
|
||||
gpio_cfg.FunSel = GPIO_Dx;
|
||||
gpio_cfg.Pin = GPIO_16;
|
||||
xc_gpio_init(&gpio_cfg); // mosi
|
||||
|
||||
gpio_cfg.FunSel = GPIO_Dx;
|
||||
gpio_cfg.Pin = GPIO_17;
|
||||
xc_gpio_init(&gpio_cfg); // miso
|
||||
}
|
||||
|
||||
SPI_InitCfg_t spi_cfg = {0};
|
||||
|
||||
spi_cfg.Mode = SPI_MODE_MASTER;
|
||||
spi_cfg.DataSize = SSI_CTRL0_DFS_LEN_8BIT;
|
||||
spi_cfg.Direction = SSI_CTRL0_TMOD_WR;
|
||||
spi_cfg.BaudRatePrescaler = SSI_BAUD_DIV_2;
|
||||
spi_cfg.CLKPolarity = SSI_CTRL0_SCPOL_LOW;
|
||||
spi_cfg.CLKPhase = SSI_CTRL0_SCPHA_LEAD;
|
||||
spi_cfg.FirstBit = SPI_FirstBit_MSB;
|
||||
|
||||
xc_spi_init(spi_idx, &spi_cfg);
|
||||
NVIC_EnableIRQ(DMA_IRQn);
|
||||
|
||||
xc_spi_dma_flash_wakeup(spi_idx);
|
||||
|
||||
uint32_t mid = 0;
|
||||
xc_spi_dma_flash_rdid(spi_idx, (uint8_t*)&mid);
|
||||
|
||||
DEBUG("Flash RDID: 0x%08x\n", mid);
|
||||
|
||||
uint8_t ruid[16];
|
||||
xc_spi_dma_flash_ruid(spi_idx, ruid);
|
||||
|
||||
DEBUG("Flash RUID: ");
|
||||
for(int i = 0;i < 16;i++)
|
||||
{
|
||||
PRINT("%02x ", ruid[i]);
|
||||
}
|
||||
PRINT("\n");
|
||||
|
||||
#if (SPI_DMA_FLASH_ALL_BIT_TEST == 1) // for only ram
|
||||
spi_dma_flash_all_bit_test(spi_idx);
|
||||
#else
|
||||
spi_dma_flash_any_addr_test(spi_idx);
|
||||
#endif
|
||||
}
|
||||
Reference in New Issue
Block a user