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

@ -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,6 +868,16 @@ void NorFlash_Write_PageProgram(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t Num
}
/*******************************************************************************
* : 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;
@ -778,7 +960,6 @@ void NorFlash_Write(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite)
for(i=0;i<secRemain;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)
{

View File

@ -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;

View File

@ -1,4 +1,3 @@
/*
* Copyright (c) 2020 AIIT XUOS Lab
* XiUOS is licensed under Mulan PSL v2.
@ -27,13 +26,13 @@ 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
*/
/*******************************************************************************
* : Receive_Byte
* :
* : c:
timeout:
* : 0:,-1:
*******************************************************************************/
static int32_t Receive_Byte(uint8_t *c, uint32_t timeout)
{
while(timeout-- > 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
*/
/*******************************************************************************
* : 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:16crc数据
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,18 +134,15 @@ 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
*/
/*******************************************************************************
* : 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;
@ -202,11 +206,15 @@ 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
*/
/*******************************************************************************
* : 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;
@ -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);
@ -346,11 +354,13 @@ 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};

View File

@ -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)