Add function comments for flash related functions

This commit is contained in:
wgzAIIT 2023-04-21 10:15:15 +08:00
parent 8bc7546828
commit 6074b2b147
4 changed files with 361 additions and 129 deletions

View File

@ -18,7 +18,7 @@
#include "flash.h" #include "flash.h"
#include "MIMXRT1052.h" #include "MIMXRT1052.h"
uint8_t NorFlash_BUFFER[4096]; //4K buffer cache uint8_t NorFlash_BUFFER[4096]; //4K buffer cache
uint8_t buffer[FLASH_PAGE_SIZE]; //256 bytes buffer cache uint8_t buffer[FLASH_PAGE_SIZE]; //256 bytes buffer cache
/******************************************************************************* /*******************************************************************************
* Prototypes * Prototypes
@ -26,8 +26,8 @@ uint8_t buffer[FLASH_PAGE_SIZE]; //256 bytes buffer cache
static status_t Flexspi_Nor_Wait_Busy(uint32_t instance, uint32_t baseAddr); static status_t Flexspi_Nor_Wait_Busy(uint32_t instance, uint32_t baseAddr);
static status_t Flexspi_Nor_Write_Enable(uint32_t instance, uint32_t baseAddr); static status_t Flexspi_Nor_Write_Enable(uint32_t instance, uint32_t baseAddr);
static void flexspi_clock_config(uint32_t instance, uint32_t freq, uint32_t sampleClkMode); static void flexspi_clock_config(uint32_t instance, uint32_t freq, uint32_t sampleClkMode);
static void flexspi_clock_gate_enable(uint32_t instance); static void flexspi_clock_gate_enable(void);
static void flexspi_clock_gate_disable(uint32_t instance); static void flexspi_clock_gate_disable(void);
static status_t flexspi_get_clock(uint32_t instance, flexspi_clock_type_t type, uint32_t *freq); static status_t flexspi_get_clock(uint32_t instance, flexspi_clock_type_t type, uint32_t *freq);
static status_t flexspi_get_ticks(uint32_t *ticks, uint32_t intervalNs, uint32_t freq, uint32_t unit); static status_t flexspi_get_ticks(uint32_t *ticks, uint32_t intervalNs, uint32_t freq, uint32_t unit);
static status_t flexspi_configure_dll(uint32_t instance, flexspi_mem_config_t *config); static status_t flexspi_configure_dll(uint32_t instance, flexspi_mem_config_t *config);
@ -57,6 +57,13 @@ static const lookuptable_t FlashLookupTable={
}; };
/*******************************************************************************
* : Flexspi_Nor_Wait_Busy
* : FlexSPI NOR Flash忙碌状态结束
* : instance:FlexSPI实例号
baseAddr:Flash地址(32bit)
* : kStatus_Success
*******************************************************************************/
static status_t Flexspi_Nor_Wait_Busy(uint32_t instance, uint32_t baseAddr) static status_t Flexspi_Nor_Wait_Busy(uint32_t instance, uint32_t baseAddr)
{ {
status_t status = kStatus_InvalidArgument; status_t status = kStatus_InvalidArgument;
@ -93,6 +100,13 @@ static status_t Flexspi_Nor_Wait_Busy(uint32_t instance, uint32_t baseAddr)
} }
/*******************************************************************************
* : Flexspi_Nor_Write_Enable
* : 使 FlexSPI NOR Flash的写入操作
* : instance:FlexSPI实例号
baseAddr:Flash地址(32bit)
* : kStatus_Success
*******************************************************************************/
static status_t Flexspi_Nor_Write_Enable(uint32_t instance, uint32_t baseAddr) static status_t Flexspi_Nor_Write_Enable(uint32_t instance, uint32_t baseAddr)
{ {
status_t status = kStatus_InvalidArgument; status_t status = kStatus_InvalidArgument;
@ -110,6 +124,14 @@ static status_t Flexspi_Nor_Write_Enable(uint32_t instance, uint32_t baseAddr)
} }
/*******************************************************************************
* : flexspi_clock_config
* : FlexSPI模块的时钟
* : instance:FlexSPI实例号
freq:FlexSPI时钟频率
sampleClkMode:FlexSPI时钟的采样时钟模式,SDR或DDR模式
* : kStatus_Success
*******************************************************************************/
static void flexspi_clock_config(uint32_t instance, uint32_t freq, uint32_t sampleClkMode) static void flexspi_clock_config(uint32_t instance, uint32_t freq, uint32_t sampleClkMode)
{ {
uint32_t pfd480 = 0; uint32_t pfd480 = 0;
@ -166,7 +188,7 @@ static void flexspi_clock_config(uint32_t instance, uint32_t freq, uint32_t samp
cscmr1 |= CCM_CSCMR1_FLEXSPI_PODF(podf - 1); cscmr1 |= CCM_CSCMR1_FLEXSPI_PODF(podf - 1);
FLEXSPI->MCR0 |= FLEXSPI_MCR0_MDIS_MASK; FLEXSPI->MCR0 |= FLEXSPI_MCR0_MDIS_MASK;
flexspi_clock_gate_disable(instance); flexspi_clock_gate_disable();
if(pfd480 != CCM_ANALOG->PFD_480) if(pfd480 != CCM_ANALOG->PFD_480)
{ {
@ -176,7 +198,7 @@ static void flexspi_clock_config(uint32_t instance, uint32_t freq, uint32_t samp
{ {
CCM->CSCMR1 = cscmr1; CCM->CSCMR1 = cscmr1;
} }
flexspi_clock_gate_enable(instance); flexspi_clock_gate_enable();
FLEXSPI->MCR0 &= ~FLEXSPI_MCR0_MDIS_MASK; FLEXSPI->MCR0 &= ~FLEXSPI_MCR0_MDIS_MASK;
} }
else else
@ -187,18 +209,38 @@ static void flexspi_clock_config(uint32_t instance, uint32_t freq, uint32_t samp
} }
static void flexspi_clock_gate_enable(uint32_t instance) /*******************************************************************************
* : flexspi_clock_gate_enable
* : FlexSPI模块的时钟门控
* :
* :
*******************************************************************************/
static void flexspi_clock_gate_enable(void)
{ {
CCM->CCGR6 |= CCM_CCGR6_CG5_MASK; CCM->CCGR6 |= CCM_CCGR6_CG5_MASK;
} }
static void flexspi_clock_gate_disable(uint32_t instance) /*******************************************************************************
* : flexspi_clock_gate_disable
* : FlexSPI模块的时钟门控
* :
* :
*******************************************************************************/
static void flexspi_clock_gate_disable(void)
{ {
CCM->CCGR6 &= (uint32_t)~CCM_CCGR6_CG5_MASK; CCM->CCGR6 &= (uint32_t)~CCM_CCGR6_CG5_MASK;
} }
/*******************************************************************************
* : flexspi_get_clock
* : FlexSPI时钟频率
* : instance:FlexSPI实例号
type:
freq:
* : kStatus_Success
*******************************************************************************/
static status_t flexspi_get_clock(uint32_t instance, flexspi_clock_type_t type, uint32_t *freq) static status_t flexspi_get_clock(uint32_t instance, flexspi_clock_type_t type, uint32_t *freq)
{ {
uint32_t clockFrequency = 0; uint32_t clockFrequency = 0;
@ -248,6 +290,15 @@ static status_t flexspi_get_clock(uint32_t instance, flexspi_clock_type_t type,
} }
/*******************************************************************************
* : flexspi_get_ticks
* : FlexSPI时钟周期数
* : ticks:
intervalNs:ns
freq:FlexSPI时钟频率MHz
unit:,,ns为单位
* : kStatus_Success
*******************************************************************************/
static status_t flexspi_get_ticks(uint32_t *ticks, uint32_t intervalNs, uint32_t freq, uint32_t unit) static status_t flexspi_get_ticks(uint32_t *ticks, uint32_t intervalNs, uint32_t freq, uint32_t unit)
{ {
status_t status = kStatus_InvalidArgument; status_t status = kStatus_InvalidArgument;
@ -278,6 +329,13 @@ static status_t flexspi_get_ticks(uint32_t *ticks, uint32_t intervalNs, uint32_t
} }
/*******************************************************************************
* : flexspi_configure_dll
* : FLEXSPI存储器的DLL()
* : instance:FLEXSPI实例号
config:
* : kStatus_Success
*******************************************************************************/
static status_t flexspi_configure_dll(uint32_t instance, flexspi_mem_config_t *config) static status_t flexspi_configure_dll(uint32_t instance, flexspi_mem_config_t *config)
{ {
status_t status = kStatus_InvalidArgument; status_t status = kStatus_InvalidArgument;
@ -414,6 +472,13 @@ static status_t flexspi_configure_dll(uint32_t instance, flexspi_mem_config_t *c
} }
/*******************************************************************************
* : flexspi_config_mcr1
* : FlexSPI模块的MCR1寄存器
* : instance:FLEXSPI实例号
onfig指向FlexSPI存储器配置结构体的指针
* : kStatus_Success
*******************************************************************************/
static status_t flexspi_config_mcr1(uint32_t instance, flexspi_mem_config_t *config) static status_t flexspi_config_mcr1(uint32_t instance, flexspi_mem_config_t *config)
{ {
uint32_t seqWaitTicks = 0xFFFFu; uint32_t seqWaitTicks = 0xFFFFu;
@ -447,6 +512,12 @@ static status_t flexspi_config_mcr1(uint32_t instance, flexspi_mem_config_t *con
} }
/*******************************************************************************
* : FLASH_GetSectorSize
* :
* :
* : ,HYPER FLASH为64K字节,NOR FLASH为4K字节
*******************************************************************************/
uint32_t FLASH_GetSectorSize(void) uint32_t FLASH_GetSectorSize(void)
{ {
#ifndef HYPER_FLASH #ifndef HYPER_FLASH
@ -457,6 +528,12 @@ uint32_t FLASH_GetSectorSize(void)
} }
/*******************************************************************************
* : FLASH_GetProgramCmd
* :
* :
* : ,HYPER FLASH为512字节,NOR FLASH为256字节
*******************************************************************************/
uint32_t FLASH_GetProgramCmd(void) uint32_t FLASH_GetProgramCmd(void)
{ {
uint32_t Program_Unit; uint32_t Program_Unit;
@ -470,6 +547,12 @@ uint32_t FLASH_GetProgramCmd(void)
} }
/*******************************************************************************
* : FLASH_Init
* : Flash接口初始化,Flash相关操作前进行调用
* :
* :
*******************************************************************************/
void FLASH_Init(void) void FLASH_Init(void)
{ {
/* Update LUT Table for Status, Write Enable, Erase and Program */ /* Update LUT Table for Status, Write Enable, Erase and Program */
@ -483,6 +566,12 @@ void FLASH_Init(void)
} }
/*******************************************************************************
* : FLASH_DeInit
* : Flash接口反初始化Flash相关操作后进行调用
* :
* :
*******************************************************************************/
void FLASH_DeInit(void) void FLASH_DeInit(void)
{ {
lookuptable_t clearlut; lookuptable_t clearlut;
@ -493,6 +582,13 @@ void FLASH_DeInit(void)
} }
/*******************************************************************************
* : FLASH_EraseSector
* : Flash扇区
* : addr:
* : None
* : :30ms~200/400ms
*******************************************************************************/
uint8_t FLASH_EraseSector(uint32_t addr) uint8_t FLASH_EraseSector(uint32_t addr)
{ {
status_t status; status_t status;
@ -516,6 +612,15 @@ uint8_t FLASH_EraseSector(uint32_t addr)
} }
/*******************************************************************************
* : FLASH_WritePage
* : Flash一个页
* : addr:
buf:
len:(256)
* : kStatus_Success
* : 256
*******************************************************************************/
uint8_t FLASH_WritePage(uint32_t addr, const uint32_t *buf, uint32_t len) uint8_t FLASH_WritePage(uint32_t addr, const uint32_t *buf, uint32_t len)
{ {
status_t status; status_t status;
@ -542,6 +647,14 @@ uint8_t FLASH_WritePage(uint32_t addr, const uint32_t *buf, uint32_t len)
} }
/*******************************************************************************
* : FLASH_Read
* : Flash内容
* : addr:
buf:
len:
* : kStatus_Success
*******************************************************************************/
status_t FLASH_Read(uint32_t addr, uint32_t *buf, uint32_t len) status_t FLASH_Read(uint32_t addr, uint32_t *buf, uint32_t len)
{ {
status_t status; status_t status;
@ -566,6 +679,14 @@ status_t FLASH_Read(uint32_t addr, uint32_t *buf, uint32_t len)
} }
/*******************************************************************************
* : flash_erase
* : Flash指定长度的空间
* : addr:
byte_cnt:,4k字节为最小擦除单位
* : None
* : 4k字节的4k字节
*******************************************************************************/
status_t flash_erase(uint32_t start_addr, uint32_t byte_cnt) status_t flash_erase(uint32_t start_addr, uint32_t byte_cnt)
{ {
uint32_t addr; uint32_t addr;
@ -584,12 +705,30 @@ status_t flash_erase(uint32_t start_addr, uint32_t byte_cnt)
return status; return status;
} }
/*******************************************************************************
* : flash_write
* : FLASH_WritePage功能相同Flash一个页
* : addr:
buf:
len:(256)
* : kStatus_Success
* : 256
*******************************************************************************/
status_t flash_write(uint32_t start_addr, uint8_t *buf, uint32_t byte_cnt) status_t flash_write(uint32_t start_addr, uint8_t *buf, uint32_t byte_cnt)
{ {
return FLASH_WritePage(start_addr, (void *)buf, byte_cnt); return FLASH_WritePage(start_addr, (void *)buf, byte_cnt);
} }
/*******************************************************************************
* : flash_read
* : Flash内容
* : addr:
buf:
len:
* : kStatus_Success
*******************************************************************************/
status_t flash_read(uint32_t addr, uint8_t *buf, uint32_t len) status_t flash_read(uint32_t addr, uint8_t *buf, uint32_t len)
{ {
/* For FlexSPI Memory ReadBack, use IP Command instead of AXI command for security */ /* For FlexSPI Memory ReadBack, use IP Command instead of AXI command for security */
@ -614,12 +753,20 @@ status_t flash_read(uint32_t addr, uint8_t *buf, uint32_t len)
} }
/*******************************************************************************
* : flash_copy
* : flash数据在分区之间的拷贝
* : srcAddr:flash的起始地址
dstAddr:flash的起始地址;
imageSize:flash空间大小,
* : kStatus_Success
*******************************************************************************/
status_t flash_copy(uint32_t srcAddr,uint32_t dstAddr, uint32_t imageSize) status_t flash_copy(uint32_t srcAddr,uint32_t dstAddr, uint32_t imageSize)
{ {
uint32_t PageNum, Remain, i; uint32_t PageNum, Remain, i;
status_t status; status_t status;
if((srcAddr == dstAddr) || imageSize > (APP_FLASH_SIZE + 1)) if((srcAddr == dstAddr) || imageSize > APP_FLASH_SIZE)
{ {
return (status_t)kStatus_Fail; return (status_t)kStatus_Fail;
} }
@ -672,6 +819,13 @@ status_t flash_copy(uint32_t srcAddr,uint32_t dstAddr, uint32_t imageSize)
} }
/*******************************************************************************
* : NOR_FLASH_Erase
* : Flash指定长度的空间,imageSize
* : addr:
imageSize:
* : None
*******************************************************************************/
status_t NOR_FLASH_Erase(uint32_t app_base_addr,uint32_t imageSize) status_t NOR_FLASH_Erase(uint32_t app_base_addr,uint32_t imageSize)
{ {
uint16_t i; uint16_t i;
@ -691,6 +845,15 @@ status_t NOR_FLASH_Erase(uint32_t app_base_addr,uint32_t imageSize)
} }
/*******************************************************************************
* : NorFlash_Write_PageProgram
* : Flash指定长度的数据
* : pBuffer:
WriteAddr:
NumByteToWrite:(256)
* : kStatus_Success
* : 256
*******************************************************************************/
void NorFlash_Write_PageProgram(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite) void NorFlash_Write_PageProgram(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite)
{ {
uint8_t temp_data[256] = {0xff}; uint8_t temp_data[256] = {0xff};
@ -705,7 +868,17 @@ void NorFlash_Write_PageProgram(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t Num
} }
void NorFlash_Write_NoCheck(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite) /*******************************************************************************
* : NorFlash_Write_NoCheck
* : W25QXX从指定地址开始指定长度的数据
* : pBuffer:
WriteAddr:(24bit)
NumByteToWrite:(65535)
* :
* : 0XFF,0XFF处写入的数据将失败!
,,!
*******************************************************************************/
void NorFlash_Write_NoCheck(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite)
{ {
uint16_t pageRemain; uint16_t pageRemain;
@ -742,6 +915,15 @@ void NorFlash_Write_NoCheck(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByte
} }
/*******************************************************************************
* : NorFlash_Write
* : W25QXX在指定地址开始写入指定长度的数据
* : pBuffer:
WriteAddr:(24bit)
NumByteToWrite:(65535)
* : None
* :
*******************************************************************************/
void NorFlash_Write(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite) void NorFlash_Write(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite)
{ {
uint32_t secPos; uint32_t secPos;
@ -754,16 +936,16 @@ void NorFlash_Write(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite)
WriteAddr &= 0x0FFFFFFF; WriteAddr &= 0x0FFFFFFF;
secPos = WriteAddr/SECTOR_SIZE;//ÉÈÇøµØÖ· secPos = WriteAddr/SECTOR_SIZE;//扇区地址
secOff = WriteAddr%SECTOR_SIZE;//在扇区内的偏移 secOff = WriteAddr%SECTOR_SIZE;//在扇区内的偏移
secRemain = SECTOR_SIZE - secOff;//扇区剩余空间大小 secRemain = SECTOR_SIZE - secOff;//扇区剩余空间大小
if(NumByteToWrite <= secRemain) if(NumByteToWrite <= secRemain)
{ {
secRemain = NumByteToWrite;//²»´óÓÚ4096¸ö×Ö½Ú secRemain = NumByteToWrite;//不大于4096个字节
} }
while(1) while(1)
{ {
FLASH_Read(FLASH_BASE + secPos*SECTOR_SIZE, (void *)NorFlash_BUF, SECTOR_SIZE);//读出整个扇区的内容 FLASH_Read(FLASH_BASE + secPos*SECTOR_SIZE, (void *)NorFlash_BUF, SECTOR_SIZE);//读出整个扇区的内容
for(i=0;i<secRemain;i++)//校验数据 for(i=0;i<secRemain;i++)//校验数据
{ {
@ -777,8 +959,7 @@ void NorFlash_Write(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite)
FLASH_EraseSector(FLASH_BASE + secPos*SECTOR_SIZE); FLASH_EraseSector(FLASH_BASE + secPos*SECTOR_SIZE);
for(i=0;i<secRemain;i++)//复制 for(i=0;i<secRemain;i++)//复制
{ {
NorFlash_BUF[i+secOff] = pBuffer[i]; NorFlash_BUF[i+secOff] = pBuffer[i];
} }
NorFlash_Write_NoCheck(NorFlash_BUF,FLASH_BASE + secPos*SECTOR_SIZE,SECTOR_SIZE);//写入整个扇区 NorFlash_Write_NoCheck(NorFlash_BUF,FLASH_BASE + secPos*SECTOR_SIZE,SECTOR_SIZE);//写入整个扇区
} }
@ -812,6 +993,14 @@ void NorFlash_Write(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite)
} }
/*******************************************************************************
* : NOR_FLASH_Write
* : W25QXX在指定地址开始写入指定长度的数据
* : FlashAddress:Flash地址的指针
Data:
DataLength:
* : 0
*******************************************************************************/
#ifndef USE_HIGHT_SPEED_TRANS #ifndef USE_HIGHT_SPEED_TRANS
uint32_t NOR_FLASH_Write(uint32_t* FlashAddress, uint8_t* Data ,uint16_t DataLength) uint32_t NOR_FLASH_Write(uint32_t* FlashAddress, uint8_t* Data ,uint16_t DataLength)
{ {

View File

@ -24,6 +24,7 @@
#include "common.h" #include "common.h"
#include "imxrt_ota.h" #include "imxrt_ota.h"
static const uint32_t crc32tab[] = { static const uint32_t crc32tab[] = {
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba,
0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
@ -92,6 +93,13 @@ static const uint32_t crc32tab[] = {
}; };
/*******************************************************************************
* : calculate_crc32
* : Flash内存地址范围中数据的CRC32校验和
* : addr:Flash地址的起始位置
len:CRC32的数据长度
* : CRC32值
*******************************************************************************/
static uint32_t calculate_crc32(uint32_t addr, uint32_t len) static uint32_t calculate_crc32(uint32_t addr, uint32_t len)
{ {
uint32_t crc = 0xFFFFFFFF; uint32_t crc = 0xFFFFFFFF;
@ -105,6 +113,13 @@ static uint32_t calculate_crc32(uint32_t addr, uint32_t len)
return crc^0xFFFFFFFF; return crc^0xFFFFFFFF;
} }
/*******************************************************************************
* : UpdateOTAStatus
* : OTA的状态信息
* : status:
* :
*******************************************************************************/
void UpdateOTAStatus(ota_status_t status) void UpdateOTAStatus(ota_status_t status)
{ {
ota_info_t ota_info; ota_info_t ota_info;
@ -116,6 +131,17 @@ void UpdateOTAStatus(ota_status_t status)
flash_write(FLAG_FLAH_ADDRESS,(void *)&ota_info,sizeof(ota_info_t)); flash_write(FLAG_FLAH_ADDRESS,(void *)&ota_info,sizeof(ota_info_t));
} }
/*******************************************************************************
* : UpdateOTAFlag
* : OTA Flag区域的信息app里进行调用
* : app_size:,
version:
status:OTA的状态信息
description:
error_message:
* :
*******************************************************************************/
void UpdateOTAFlag(uint32_t app_size, uint32_t version, uint32_t status, uint8_t* description, uint8_t* error_message) void UpdateOTAFlag(uint32_t app_size, uint32_t version, uint32_t status, uint8_t* description, uint8_t* error_message)
{ {
ota_info_t ota_info; // ¶¨ÒåOTAÐÅÏ¢½á¹¹Ìå ota_info_t ota_info; // ¶¨ÒåOTAÐÅÏ¢½á¹¹Ìå
@ -133,6 +159,14 @@ void UpdateOTAFlag(uint32_t app_size, uint32_t version, uint32_t status, uint8_t
flash_write(FLAG_FLAH_ADDRESS,(void *)&ota_info,sizeof(ota_info_t)); flash_write(FLAG_FLAH_ADDRESS,(void *)&ota_info,sizeof(ota_info_t));
} }
/*******************************************************************************
* : UpdateApplication
* : bootloader里进行调用,Flash中Flag分区中的信息决定是否进行版本更新
* :
* :
* : app分区
*******************************************************************************/
void UpdateApplication(void) void UpdateApplication(void)
{ {
status_t status; status_t status;

View File

@ -1,4 +1,3 @@
/* /*
* Copyright (c) 2020 AIIT XUOS Lab * Copyright (c) 2020 AIIT XUOS Lab
* XiUOS is licensed under Mulan PSL v2. * XiUOS is licensed under Mulan PSL v2.
@ -27,18 +26,18 @@ uint8_t tab_1024[1024] ={0};
uint8_t FileName[FILE_NAME_LENGTH]; uint8_t FileName[FILE_NAME_LENGTH];
/** /*******************************************************************************
* @brief Receive byte from sender * : Receive_Byte
* @param c: Character * :
* @param timeout: Timeout * : c:
* @retval 0: Byte received timeout:
* -1: Timeout * : 0:,-1:
*/ *******************************************************************************/
static int32_t Receive_Byte (uint8_t *c, uint32_t timeout) static int32_t Receive_Byte(uint8_t *c, uint32_t timeout)
{ {
while (timeout-- > 0) while(timeout-- > 0)
{ {
if (SerialKeyPressed(c) == 1) if(SerialKeyPressed(c) == 1)
{ {
return 0; return 0;
} }
@ -46,23 +45,27 @@ static int32_t Receive_Byte (uint8_t *c, uint32_t timeout)
return -1; return -1;
} }
/**
* @brief Send a byte /*******************************************************************************
* @param c: Character * : Send_Byte
* @retval 0: Byte sent * :
*/ * : c:
static uint32_t Send_Byte (uint8_t c) * : 0
*******************************************************************************/
static uint32_t Send_Byte(uint8_t c)
{ {
SerialPutChar(c); SerialPutChar(c);
return 0; return 0;
} }
/**
* @brief Update CRC16 for input byte /*******************************************************************************
* @param CRC input value * : UpdateCRC16
* @param input byte * : CRC16校验
* @retval Updated CRC value * : crcIn:16crc数据
*/ byte:8
* : crc数据
*******************************************************************************/
uint16_t UpdateCRC16(uint16_t crcIn, uint8_t byte) uint16_t UpdateCRC16(uint16_t crcIn, uint8_t byte)
{ {
uint32_t crc = crcIn; uint32_t crc = crcIn;
@ -87,12 +90,14 @@ uint16_t UpdateCRC16(uint16_t crcIn, uint8_t byte)
return (crc&0xffffu); return (crc&0xffffu);
} }
/**
* @brief Cal CRC16 for YModem Packet /*******************************************************************************
* @param data * : Cal_CRC16
* @param length * : CRC16,YModem的数据包
* @retval CRC value * : data:buffer
*/ size:
* : crc数据
*******************************************************************************/
uint16_t Cal_CRC16(const uint8_t* data, uint32_t size) uint16_t Cal_CRC16(const uint8_t* data, uint32_t size)
{ {
uint32_t crc = 0; uint32_t crc = 0;
@ -108,12 +113,14 @@ uint16_t Cal_CRC16(const uint8_t* data, uint32_t size)
return (crc&0xffffu); return (crc&0xffffu);
} }
/**
* @brief Cal Check sum for YModem Packet /*******************************************************************************
* @param data * : CalChecksum
* @param length * : YModem数据包的总和
* @retval None * : data:buffer
*/ size:
* :
*******************************************************************************/
uint8_t CalChecksum(const uint8_t* data, uint32_t size) uint8_t CalChecksum(const uint8_t* data, uint32_t size)
{ {
uint32_t sum = 0; uint32_t sum = 0;
@ -127,24 +134,21 @@ uint8_t CalChecksum(const uint8_t* data, uint32_t size)
return (sum&0xffu); return (sum&0xffu);
} }
/**
* @brief Receive a packet from sender /*******************************************************************************
* @param data * : Receive_Packet
* @param length * :
* @param timeout * : data:buffer
* 0: end of transmission length:
* -1: abort by sender timeout:
* >0: packet length * : 0:,-1://,1:
* @retval 0: normally return *******************************************************************************/
* -1: timeout or packet error static int32_t Receive_Packet(uint8_t *data, int32_t *length, uint32_t timeout)
* 1: abort by user
*/
static int32_t Receive_Packet (uint8_t *data, int32_t *length, uint32_t timeout)
{ {
uint16_t i, packet_size, computedcrc; uint16_t i, packet_size, computedcrc;
uint8_t c; uint8_t c;
*length = 0; *length = 0;
if (Receive_Byte(&c, timeout) != 0) if(Receive_Byte(&c, timeout) != 0)
{ {
return -1; return -1;
} }
@ -159,7 +163,7 @@ static int32_t Receive_Packet (uint8_t *data, int32_t *length, uint32_t timeout)
case EOT: case EOT:
return 0; return 0;
case CA: case CA:
if ((Receive_Byte(&c, timeout) == 0) && (c == CA)) if((Receive_Byte(&c, timeout) == 0) && (c == CA))
{ {
*length = -1; *length = -1;
return 0; return 0;
@ -175,14 +179,14 @@ static int32_t Receive_Packet (uint8_t *data, int32_t *length, uint32_t timeout)
return -1; return -1;
} }
*data = c; *data = c;
for (i = 1; i < (packet_size + PACKET_OVERHEAD); i ++) for(i = 1; i < (packet_size + PACKET_OVERHEAD); i ++)
{ {
if (Receive_Byte(data + i, timeout) != 0) if(Receive_Byte(data + i, timeout) != 0)
{ {
return -1; return -1;
} }
} }
if (data[PACKET_SEQNO_INDEX] != ((data[PACKET_SEQNO_COMP_INDEX] ^ 0xff) & 0xff)) if(data[PACKET_SEQNO_INDEX] != ((data[PACKET_SEQNO_COMP_INDEX] ^ 0xff) & 0xff))
{ {
return -1; return -1;
} }
@ -192,7 +196,7 @@ static int32_t Receive_Packet (uint8_t *data, int32_t *length, uint32_t timeout)
/* Check that received CRC match the already computed CRC value /* Check that received CRC match the already computed CRC value
data[packet_size+3]<<8) | data[packet_size+4] contains the received CRC data[packet_size+3]<<8) | data[packet_size+4] contains the received CRC
computedcrc contains the computed CRC value */ computedcrc contains the computed CRC value */
if (computedcrc != (uint16_t)((data[packet_size+3]<<8) | data[packet_size+4])) if(computedcrc != (uint16_t)((data[packet_size+3]<<8) | data[packet_size+4]))
{ {
/* CRC error */ /* CRC error */
return -1; return -1;
@ -202,12 +206,16 @@ static int32_t Receive_Packet (uint8_t *data, int32_t *length, uint32_t timeout)
return 0; return 0;
} }
/**
* @brief Receive a file using the ymodem protocol /*******************************************************************************
* @param buf: Address of the first byte,addr:download flash start address * : Ymodem_Receive
* @retval The size of the file * : 使ymodem协议接收文件
*/ * : buf:buffer
int32_t Ymodem_Receive (uint8_t *buf, const uint32_t addr) addr:flash起始地址
timeout:
* :
*******************************************************************************/
int32_t Ymodem_Receive(uint8_t *buf, const uint32_t addr)
{ {
uint8_t packet_data[PACKET_1K_SIZE + PACKET_OVERHEAD], file_size[FILE_SIZE_LENGTH], *file_ptr, *buf_ptr; uint8_t packet_data[PACKET_1K_SIZE + PACKET_OVERHEAD], file_size[FILE_SIZE_LENGTH], *file_ptr, *buf_ptr;
int32_t i, packet_length, session_done, file_done, packets_received, errors, session_begin, size = 0; int32_t i, packet_length, session_done, file_done, packets_received, errors, session_begin, size = 0;
@ -216,15 +224,15 @@ int32_t Ymodem_Receive (uint8_t *buf, const uint32_t addr)
/* Initialize flashdestination variable */ /* Initialize flashdestination variable */
flashdestination = addr; flashdestination = addr;
for (session_done = 0, errors = 0, session_begin = 0; ;) for(session_done = 0, errors = 0, session_begin = 0; ;)
{ {
for (packets_received = 0, file_done = 0, buf_ptr = buf; ;) for(packets_received = 0, file_done = 0, buf_ptr = buf; ;)
{ {
switch (Receive_Packet(packet_data, &packet_length, NAK_TIMEOUT)) switch(Receive_Packet(packet_data, &packet_length, NAK_TIMEOUT))
{ {
case 0: case 0:
errors = 0; errors = 0;
switch (packet_length) switch(packet_length)
{ {
/* Abort by sender */ /* Abort by sender */
case - 1: case - 1:
@ -237,24 +245,24 @@ int32_t Ymodem_Receive (uint8_t *buf, const uint32_t addr)
break; break;
/* Normal packet */ /* Normal packet */
default: default:
if ((packet_data[PACKET_SEQNO_INDEX] & 0xff) != (packets_received & 0xff)) if((packet_data[PACKET_SEQNO_INDEX] & 0xff) != (packets_received & 0xff))
{ {
Send_Byte(NAK); Send_Byte(NAK);
} }
else else
{ {
if (packets_received == 0) if(packets_received == 0)
{ {
/* Filename packet */ /* Filename packet */
if (packet_data[PACKET_HEADER] != 0) if(packet_data[PACKET_HEADER] != 0)
{ {
/* Filename packet has valid data */ /* Filename packet has valid data */
for (i = 0, file_ptr = packet_data + PACKET_HEADER; (*file_ptr != 0) && (i < FILE_NAME_LENGTH);) for(i = 0, file_ptr = packet_data + PACKET_HEADER; (*file_ptr != 0) && (i < FILE_NAME_LENGTH);)
{ {
FileName[i++] = *file_ptr++; FileName[i++] = *file_ptr++;
} }
FileName[i++] = '\0'; FileName[i++] = '\0';
for (i = 0, file_ptr ++; (*file_ptr != ' ') && (i < (FILE_SIZE_LENGTH - 1));) for(i = 0, file_ptr ++; (*file_ptr != ' ') && (i < (FILE_SIZE_LENGTH - 1));)
{ {
file_size[i++] = *file_ptr++; file_size[i++] = *file_ptr++;
} }
@ -263,7 +271,7 @@ int32_t Ymodem_Receive (uint8_t *buf, const uint32_t addr)
/* Test the size of the image to be sent */ /* Test the size of the image to be sent */
/* Image size is greater than Flash size */ /* Image size is greater than Flash size */
if (size > (USER_FLASH_SIZE + 1)) if(size > APP_FLASH_SIZE)
{ {
/* End session */ /* End session */
Send_Byte(CA); Send_Byte(CA);
@ -292,9 +300,9 @@ int32_t Ymodem_Receive (uint8_t *buf, const uint32_t addr)
/* Write received data in Flash */ /* Write received data in Flash */
#ifndef USE_HIGHT_SPEED_TRANS #ifndef USE_HIGHT_SPEED_TRANS
if (NOR_FLASH_Write(&flashdestination, buf, (uint16_t) packet_length) == 0) if(NOR_FLASH_Write(&flashdestination, buf, (uint16_t)packet_length) == 0)
#else #else
if (NOR_FLASH_Write(&flashdestination, buf, (uint16_t) packet_length,0) == 0) if(NOR_FLASH_Write(&flashdestination, buf, (uint16_t)packet_length, 0) == 0)
#endif #endif
{ {
Send_Byte(ACK); Send_Byte(ACK);
@ -317,11 +325,11 @@ int32_t Ymodem_Receive (uint8_t *buf, const uint32_t addr)
Send_Byte(CA); Send_Byte(CA);
return -3; return -3;
default: default:
if (session_begin > 0) if(session_begin > 0)
{ {
errors ++; errors ++;
} }
if (errors > MAX_ERRORS) if(errors > MAX_ERRORS)
{ {
Send_Byte(CA); Send_Byte(CA);
Send_Byte(CA); Send_Byte(CA);
@ -330,12 +338,12 @@ int32_t Ymodem_Receive (uint8_t *buf, const uint32_t addr)
Send_Byte(CRC16); Send_Byte(CRC16);
break; break;
} }
if (file_done != 0) if(file_done != 0)
{ {
break; break;
} }
} }
if (session_done != 0) if(session_done != 0)
{ {
break; break;
} }
@ -346,44 +354,46 @@ int32_t Ymodem_Receive (uint8_t *buf, const uint32_t addr)
return (int32_t)size; return (int32_t)size;
} }
/**
* @brief Download a file via serial port /*******************************************************************************
* @param flash start addr * : SerialDownload
* @retval None * :
*/ * : addr:flash起始地址
* :
*******************************************************************************/
int32_t SerialDownload(const uint32_t addr) int32_t SerialDownload(const uint32_t addr)
{ {
uint8_t Number[10] = {0}; uint8_t Number[10] = {0};
int32_t Size = 0; int32_t Size = 0;
Serial_PutString("Waiting for the file to be sent ... (press 'a' to abort)\n\r"); Serial_PutString("Waiting for the file to be sent ... (press 'a' to abort)\n\r");
Size = Ymodem_Receive(&tab_1024[0], addr); Size = Ymodem_Receive(&tab_1024[0], addr);
if (Size > 0) if(Size > 0)
{ {
Serial_PutString("\n\n\r Programming Completed Successfully!\n\r--------------------------------\r\n Name: "); Serial_PutString("\n\n\r Programming Completed Successfully!\n\r--------------------------------\r\n Name: ");
Serial_PutString(FileName); Serial_PutString(FileName);
Int2Str(Number, Size); Int2Str(Number, Size);
Serial_PutString("\n\r Size: "); Serial_PutString("\n\r Size: ");
Serial_PutString(Number); Serial_PutString(Number);
Serial_PutString(" Bytes\r\n"); Serial_PutString(" Bytes\r\n");
Serial_PutString("-------------------\n"); Serial_PutString("-------------------\n");
} }
else if (Size == -1) else if(Size == -1)
{ {
Serial_PutString("\n\n\rThe image size is higher than the allowed space memory!\n\r"); Serial_PutString("\n\n\rThe image size is higher than the allowed space memory!\n\r");
} }
else if (Size == -2) else if(Size == -2)
{ {
Serial_PutString("\n\n\rVerification failed!\n\r"); Serial_PutString("\n\n\rVerification failed!\n\r");
} }
else if (Size == -3) else if(Size == -3)
{ {
Serial_PutString("\r\n\nAborted by user.\n\r"); Serial_PutString("\r\n\nAborted by user.\n\r");
} }
else else
{ {
Serial_PutString("\n\rFailed to receive the file!\n\r"); Serial_PutString("\n\rFailed to receive the file!\n\r");
} }
return Size; return Size;
} }

View File

@ -25,7 +25,6 @@
#include <stdint.h> #include <stdint.h>
#include "common.h" #include "common.h"
#define USER_FLASH_SIZE 0x100000 //Application package size is limited to 1M
#define PACKET_SEQNO_INDEX (1) #define PACKET_SEQNO_INDEX (1)
#define PACKET_SEQNO_COMP_INDEX (2) #define PACKET_SEQNO_COMP_INDEX (2)
@ -53,6 +52,6 @@
#define MAX_ERRORS (5) #define MAX_ERRORS (5)
int32_t SerialDownload(const uint32_t addr); int32_t SerialDownload(const uint32_t addr);
int32_t Ymodem_Receive (uint8_t *buf, const uint32_t addr); int32_t Ymodem_Receive(uint8_t *buf, const uint32_t addr);
#endif #endif