forked from xuos/xiuos
Add function comments for flash related functions
This commit is contained in:
parent
8bc7546828
commit
6074b2b147
|
@ -18,7 +18,7 @@
|
|||
#include "flash.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
|
||||
/*******************************************************************************
|
||||
* 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_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_gate_enable(uint32_t instance);
|
||||
static void flexspi_clock_gate_disable(uint32_t instance);
|
||||
static void flexspi_clock_gate_enable(void);
|
||||
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_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);
|
||||
|
@ -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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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);
|
||||
|
||||
FLEXSPI->MCR0 |= FLEXSPI_MCR0_MDIS_MASK;
|
||||
flexspi_clock_gate_disable(instance);
|
||||
flexspi_clock_gate_disable();
|
||||
|
||||
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;
|
||||
}
|
||||
flexspi_clock_gate_enable(instance);
|
||||
flexspi_clock_gate_enable();
|
||||
FLEXSPI->MCR0 &= ~FLEXSPI_MCR0_MDIS_MASK;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
* 函 数 名: 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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
#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 Program_Unit;
|
||||
|
@ -470,6 +547,12 @@ uint32_t FLASH_GetProgramCmd(void)
|
|||
}
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
* 函 数 名: FLASH_Init
|
||||
* 功能描述: Flash接口初始化,需在进行Flash相关操作前进行调用
|
||||
* 形 参: 无
|
||||
* 返 回 值: 无
|
||||
*******************************************************************************/
|
||||
void FLASH_Init(void)
|
||||
{
|
||||
/* 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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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 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)
|
||||
{
|
||||
uint32_t addr;
|
||||
|
@ -584,12 +705,30 @@ status_t flash_erase(uint32_t start_addr, uint32_t byte_cnt)
|
|||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
/* 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)
|
||||
{
|
||||
uint32_t PageNum, Remain, i;
|
||||
status_t status;
|
||||
|
||||
if((srcAddr == dstAddr) || imageSize > (APP_FLASH_SIZE + 1))
|
||||
if((srcAddr == dstAddr) || imageSize > APP_FLASH_SIZE)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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;
|
||||
|
||||
|
@ -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)
|
||||
{
|
||||
uint32_t secPos;
|
||||
|
@ -754,16 +936,16 @@ void NorFlash_Write(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite)
|
|||
|
||||
WriteAddr &= 0x0FFFFFFF;
|
||||
|
||||
secPos = WriteAddr/SECTOR_SIZE;//ÉÈÇøµØÖ·
|
||||
secPos = WriteAddr/SECTOR_SIZE;//扇区地址
|
||||
secOff = WriteAddr%SECTOR_SIZE;//在扇区内的偏移
|
||||
secRemain = SECTOR_SIZE - secOff;//扇区剩余空间大小
|
||||
|
||||
if(NumByteToWrite <= secRemain)
|
||||
{
|
||||
secRemain = NumByteToWrite;//²»´óÓÚ4096¸ö×Ö½Ú
|
||||
}
|
||||
secRemain = NumByteToWrite;//不大于4096个字节
|
||||
}
|
||||
while(1)
|
||||
{
|
||||
{
|
||||
FLASH_Read(FLASH_BASE + secPos*SECTOR_SIZE, (void *)NorFlash_BUF, SECTOR_SIZE);//读出整个扇区的内容
|
||||
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);
|
||||
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);//写入整个扇区
|
||||
}
|
||||
|
@ -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
|
||||
uint32_t NOR_FLASH_Write(uint32_t* FlashAddress, uint8_t* Data ,uint16_t DataLength)
|
||||
{
|
||||
|
|
|
@ -24,6 +24,7 @@
|
|||
#include "common.h"
|
||||
#include "imxrt_ota.h"
|
||||
|
||||
|
||||
static const uint32_t crc32tab[] = {
|
||||
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba,
|
||||
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)
|
||||
{
|
||||
uint32_t crc = 0xFFFFFFFF;
|
||||
|
@ -105,6 +113,13 @@ static uint32_t calculate_crc32(uint32_t addr, uint32_t len)
|
|||
return crc^0xFFFFFFFF;
|
||||
}
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
* 函 数 名: UpdateOTAStatus
|
||||
* 功能描述: 更新OTA的状态信息
|
||||
* 形 参: status:将要更改的状态值
|
||||
* 返 回 值: 无
|
||||
*******************************************************************************/
|
||||
void UpdateOTAStatus(ota_status_t status)
|
||||
{
|
||||
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));
|
||||
}
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
* 函 数 名: 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)
|
||||
{
|
||||
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));
|
||||
}
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
* 函 数 名: UpdateApplication
|
||||
* 功能描述: 在bootloader里进行调用,根据Flash中Flag分区中的信息决定是否进行版本更新
|
||||
* 形 参: 无
|
||||
* 返 回 值: 无
|
||||
* 注 释: 该函数调用后无论结果如何都将跳转到app分区
|
||||
*******************************************************************************/
|
||||
void UpdateApplication(void)
|
||||
{
|
||||
status_t status;
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
|
||||
/*
|
||||
* Copyright (c) 2020 AIIT XUOS Lab
|
||||
* XiUOS is licensed under Mulan PSL v2.
|
||||
|
@ -27,18 +26,18 @@ uint8_t tab_1024[1024] ={0};
|
|||
uint8_t FileName[FILE_NAME_LENGTH];
|
||||
|
||||
|
||||
/**
|
||||
* @brief Receive byte from sender
|
||||
* @param c: Character
|
||||
* @param timeout: Timeout
|
||||
* @retval 0: Byte received
|
||||
* -1: Timeout
|
||||
*/
|
||||
static int32_t Receive_Byte (uint8_t *c, uint32_t timeout)
|
||||
/*******************************************************************************
|
||||
* 函 数 名: Receive_Byte
|
||||
* 功能描述: 从发送方接收字节
|
||||
* 形 参: c:用于存储接收到的字符
|
||||
timeout:超时时间
|
||||
* 返 回 值: 0:收到数据,-1:超时未接收到
|
||||
*******************************************************************************/
|
||||
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;
|
||||
}
|
||||
|
@ -46,23 +45,27 @@ static int32_t Receive_Byte (uint8_t *c, uint32_t timeout)
|
|||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Send a byte
|
||||
* @param c: Character
|
||||
* @retval 0: Byte sent
|
||||
*/
|
||||
static uint32_t Send_Byte (uint8_t c)
|
||||
|
||||
/*******************************************************************************
|
||||
* 函 数 名: Send_Byte
|
||||
* 功能描述: 发送一个字节的数据
|
||||
* 形 参: c:要发送的数据
|
||||
* 返 回 值: 0
|
||||
*******************************************************************************/
|
||||
static uint32_t Send_Byte(uint8_t c)
|
||||
{
|
||||
SerialPutChar(c);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Update CRC16 for input byte
|
||||
* @param CRC input value
|
||||
* @param input byte
|
||||
* @retval Updated CRC value
|
||||
*/
|
||||
|
||||
/*******************************************************************************
|
||||
* 函 数 名: UpdateCRC16
|
||||
* 功能描述: 更新输入数据的CRC16校验
|
||||
* 形 参: crcIn:输入的16位crc数据
|
||||
byte:输入的8位数据
|
||||
* 返 回 值: 更新后的crc数据
|
||||
*******************************************************************************/
|
||||
uint16_t UpdateCRC16(uint16_t crcIn, uint8_t byte)
|
||||
{
|
||||
uint32_t crc = crcIn;
|
||||
|
@ -87,12 +90,14 @@ uint16_t UpdateCRC16(uint16_t crcIn, uint8_t byte)
|
|||
return (crc&0xffffu);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Cal CRC16 for YModem Packet
|
||||
* @param data
|
||||
* @param length
|
||||
* @retval CRC value
|
||||
*/
|
||||
|
||||
/*******************************************************************************
|
||||
* 函 数 名: Cal_CRC16
|
||||
* 功能描述: 计算CRC16,用于YModem的数据包
|
||||
* 形 参: data:数据buffer
|
||||
size:数据长度
|
||||
* 返 回 值: 生成的crc数据
|
||||
*******************************************************************************/
|
||||
uint16_t Cal_CRC16(const uint8_t* data, uint32_t size)
|
||||
{
|
||||
uint32_t crc = 0;
|
||||
|
@ -108,12 +113,14 @@ uint16_t Cal_CRC16(const uint8_t* data, uint32_t size)
|
|||
return (crc&0xffffu);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Cal Check sum for YModem Packet
|
||||
* @param data
|
||||
* @param length
|
||||
* @retval None
|
||||
*/
|
||||
|
||||
/*******************************************************************************
|
||||
* 函 数 名: CalChecksum
|
||||
* 功能描述: 计算检查YModem数据包的总和
|
||||
* 形 参: data:数据buffer
|
||||
size:数据长度
|
||||
* 返 回 值: 计算到的数据包总和
|
||||
*******************************************************************************/
|
||||
uint8_t CalChecksum(const uint8_t* data, uint32_t size)
|
||||
{
|
||||
uint32_t sum = 0;
|
||||
|
@ -127,24 +134,21 @@ uint8_t CalChecksum(const uint8_t* data, uint32_t size)
|
|||
return (sum&0xffu);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Receive a packet from sender
|
||||
* @param data
|
||||
* @param length
|
||||
* @param timeout
|
||||
* 0: end of transmission
|
||||
* -1: abort by sender
|
||||
* >0: packet length
|
||||
* @retval 0: normally return
|
||||
* -1: timeout or packet error
|
||||
* 1: abort by user
|
||||
*/
|
||||
static int32_t Receive_Packet (uint8_t *data, int32_t *length, uint32_t timeout)
|
||||
|
||||
/*******************************************************************************
|
||||
* 函 数 名: Receive_Packet
|
||||
* 功能描述: 从发送方接收数据包
|
||||
* 形 参: data:数据buffer
|
||||
length:存储数据长度的指针
|
||||
timeout:超时时间
|
||||
* 返 回 值: 0:正常返回,-1:发送者中止/超时/数据包错误,1:用户中止
|
||||
*******************************************************************************/
|
||||
static int32_t Receive_Packet(uint8_t *data, int32_t *length, uint32_t timeout)
|
||||
{
|
||||
uint16_t i, packet_size, computedcrc;
|
||||
uint8_t c;
|
||||
*length = 0;
|
||||
if (Receive_Byte(&c, timeout) != 0)
|
||||
if(Receive_Byte(&c, timeout) != 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
@ -159,7 +163,7 @@ static int32_t Receive_Packet (uint8_t *data, int32_t *length, uint32_t timeout)
|
|||
case EOT:
|
||||
return 0;
|
||||
case CA:
|
||||
if ((Receive_Byte(&c, timeout) == 0) && (c == CA))
|
||||
if((Receive_Byte(&c, timeout) == 0) && (c == CA))
|
||||
{
|
||||
*length = -1;
|
||||
return 0;
|
||||
|
@ -175,14 +179,14 @@ static int32_t Receive_Packet (uint8_t *data, int32_t *length, uint32_t timeout)
|
|||
return -1;
|
||||
}
|
||||
*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;
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
@ -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
|
||||
data[packet_size+3]<<8) | data[packet_size+4] contains the received CRC
|
||||
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 */
|
||||
return -1;
|
||||
|
@ -202,12 +206,16 @@ static int32_t Receive_Packet (uint8_t *data, int32_t *length, uint32_t timeout)
|
|||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Receive a file using the ymodem protocol
|
||||
* @param buf: Address of the first byte,addr:download flash start address
|
||||
* @retval The size of the file
|
||||
*/
|
||||
int32_t Ymodem_Receive (uint8_t *buf, const uint32_t addr)
|
||||
|
||||
/*******************************************************************************
|
||||
* 函 数 名: Ymodem_Receive
|
||||
* 功能描述: 使用ymodem协议接收文件
|
||||
* 形 参: buf:数据buffer
|
||||
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;
|
||||
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 */
|
||||
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:
|
||||
errors = 0;
|
||||
switch (packet_length)
|
||||
switch(packet_length)
|
||||
{
|
||||
/* Abort by sender */
|
||||
case - 1:
|
||||
|
@ -237,24 +245,24 @@ int32_t Ymodem_Receive (uint8_t *buf, const uint32_t addr)
|
|||
break;
|
||||
/* Normal packet */
|
||||
default:
|
||||
if ((packet_data[PACKET_SEQNO_INDEX] & 0xff) != (packets_received & 0xff))
|
||||
if((packet_data[PACKET_SEQNO_INDEX] & 0xff) != (packets_received & 0xff))
|
||||
{
|
||||
Send_Byte(NAK);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (packets_received == 0)
|
||||
if(packets_received == 0)
|
||||
{
|
||||
/* Filename packet */
|
||||
if (packet_data[PACKET_HEADER] != 0)
|
||||
if(packet_data[PACKET_HEADER] != 0)
|
||||
{
|
||||
/* 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++] = '\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++;
|
||||
}
|
||||
|
@ -263,7 +271,7 @@ int32_t Ymodem_Receive (uint8_t *buf, const uint32_t addr)
|
|||
|
||||
/* Test the size of the image to be sent */
|
||||
/* Image size is greater than Flash size */
|
||||
if (size > (USER_FLASH_SIZE + 1))
|
||||
if(size > APP_FLASH_SIZE)
|
||||
{
|
||||
/* End session */
|
||||
Send_Byte(CA);
|
||||
|
@ -292,9 +300,9 @@ int32_t Ymodem_Receive (uint8_t *buf, const uint32_t addr)
|
|||
|
||||
/* Write received data in Flash */
|
||||
#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
|
||||
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
|
||||
{
|
||||
Send_Byte(ACK);
|
||||
|
@ -317,11 +325,11 @@ int32_t Ymodem_Receive (uint8_t *buf, const uint32_t addr)
|
|||
Send_Byte(CA);
|
||||
return -3;
|
||||
default:
|
||||
if (session_begin > 0)
|
||||
if(session_begin > 0)
|
||||
{
|
||||
errors ++;
|
||||
}
|
||||
if (errors > MAX_ERRORS)
|
||||
if(errors > MAX_ERRORS)
|
||||
{
|
||||
Send_Byte(CA);
|
||||
Send_Byte(CA);
|
||||
|
@ -330,12 +338,12 @@ int32_t Ymodem_Receive (uint8_t *buf, const uint32_t addr)
|
|||
Send_Byte(CRC16);
|
||||
break;
|
||||
}
|
||||
if (file_done != 0)
|
||||
if(file_done != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (session_done != 0)
|
||||
if(session_done != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
@ -346,44 +354,46 @@ int32_t Ymodem_Receive (uint8_t *buf, const uint32_t addr)
|
|||
return (int32_t)size;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Download a file via serial port
|
||||
* @param flash start addr
|
||||
* @retval None
|
||||
*/
|
||||
|
||||
/*******************************************************************************
|
||||
* 函 数 名: SerialDownload
|
||||
* 功能描述: 通过串口下载文件
|
||||
* 形 参: addr:存储文件的flash起始地址
|
||||
* 返 回 值: 文件的大小
|
||||
*******************************************************************************/
|
||||
int32_t SerialDownload(const uint32_t addr)
|
||||
{
|
||||
uint8_t Number[10] = {0};
|
||||
int32_t Size = 0;
|
||||
uint8_t Number[10] = {0};
|
||||
int32_t Size = 0;
|
||||
|
||||
Serial_PutString("Waiting for the file to be sent ... (press 'a' to abort)\n\r");
|
||||
Size = Ymodem_Receive(&tab_1024[0], addr);
|
||||
if (Size > 0)
|
||||
{
|
||||
Serial_PutString("\n\n\r Programming Completed Successfully!\n\r--------------------------------\r\n Name: ");
|
||||
Serial_PutString(FileName);
|
||||
Int2Str(Number, Size);
|
||||
Serial_PutString("\n\r Size: ");
|
||||
Serial_PutString(Number);
|
||||
Serial_PutString(" Bytes\r\n");
|
||||
Serial_PutString("-------------------\n");
|
||||
}
|
||||
else if (Size == -1)
|
||||
{
|
||||
Serial_PutString("\n\n\rThe image size is higher than the allowed space memory!\n\r");
|
||||
}
|
||||
else if (Size == -2)
|
||||
{
|
||||
Serial_PutString("\n\n\rVerification failed!\n\r");
|
||||
}
|
||||
else if (Size == -3)
|
||||
{
|
||||
Serial_PutString("\r\n\nAborted by user.\n\r");
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial_PutString("\n\rFailed to receive the file!\n\r");
|
||||
}
|
||||
Serial_PutString("Waiting for the file to be sent ... (press 'a' to abort)\n\r");
|
||||
Size = Ymodem_Receive(&tab_1024[0], addr);
|
||||
if(Size > 0)
|
||||
{
|
||||
Serial_PutString("\n\n\r Programming Completed Successfully!\n\r--------------------------------\r\n Name: ");
|
||||
Serial_PutString(FileName);
|
||||
Int2Str(Number, Size);
|
||||
Serial_PutString("\n\r Size: ");
|
||||
Serial_PutString(Number);
|
||||
Serial_PutString(" Bytes\r\n");
|
||||
Serial_PutString("-------------------\n");
|
||||
}
|
||||
else if(Size == -1)
|
||||
{
|
||||
Serial_PutString("\n\n\rThe image size is higher than the allowed space memory!\n\r");
|
||||
}
|
||||
else if(Size == -2)
|
||||
{
|
||||
Serial_PutString("\n\n\rVerification failed!\n\r");
|
||||
}
|
||||
else if(Size == -3)
|
||||
{
|
||||
Serial_PutString("\r\n\nAborted by user.\n\r");
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial_PutString("\n\rFailed to receive the file!\n\r");
|
||||
}
|
||||
|
||||
return Size;
|
||||
return Size;
|
||||
}
|
||||
|
|
|
@ -25,7 +25,6 @@
|
|||
#include <stdint.h>
|
||||
#include "common.h"
|
||||
|
||||
#define USER_FLASH_SIZE 0x100000 //Application package size is limited to 1M
|
||||
#define PACKET_SEQNO_INDEX (1)
|
||||
#define PACKET_SEQNO_COMP_INDEX (2)
|
||||
|
||||
|
@ -53,6 +52,6 @@
|
|||
#define MAX_ERRORS (5)
|
||||
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue