diff --git a/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/common/flash.c b/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/common/flash.c index e7dca44fd..7091142e8 100644 --- a/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/common/flash.c +++ b/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/common/flash.c @@ -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 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 +* 鍔熻兘鎻忚堪: 鏇存柊杈撳叆鏁版嵁鐨凜RC16鏍¢獙 +* 褰 鍙: crcIn:杈撳叆鐨16浣峜rc鏁版嵁 + 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:鏁版嵁闀垮害 +* 杩 鍥 鍊: 鐢熸垚鐨刢rc鏁版嵁 +*******************************************************************************/ 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 +* 鍔熻兘鎻忚堪: 璁$畻妫鏌Modem鏁版嵁鍖呯殑鎬诲拰 +* 褰 鍙: 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:姝e父杩斿洖,-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:瀛樺偍鏂囦欢鐨刦lash璧峰鍦板潃 +* 杩 鍥 鍊: 鏂囦欢鐨勫ぇ灏 +*******************************************************************************/ 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; } diff --git a/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/include/ymodem.h b/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/include/ymodem.h index c08c1221f..3ff7a28e4 100644 --- a/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/include/ymodem.h +++ b/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/include/ymodem.h @@ -25,7 +25,6 @@ #include #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