MCAL接口写完FLS后,偶发性出现一直不空闲问题
-
#define FLASH_WRITE_UNIT ( 32 ) // 固定写入单位 #define FLASH_TIMEOUT ( 10000000 ) // 超时时间 bool bsw_fls_write_pflash1_1mb( uint32_t AddrOffset, const uint8_t* data, uint32_t len ) { uint32_t startAddress = 0; uint32_t alignedLen = 0; uint32_t i = 0; uint32_t chunkSize = 0; uint32_t timeout = 0; uint8_t alignedData[ FLASH_WRITE_UNIT ] = { 0xFF }; // 预填充 0xFF uint8_t readBuffer[ FLASH_WRITE_UNIT ] = { 0 }; // 读取缓存初始化 if ( data == NULL || len == 0 ) { return false; } startAddress = Fls_Config.SectorList[ FlsConf_FlsConfigSet_PFLASH1_1MB ].SectorStartAddress + AddrOffset; alignedLen = ( ( len + ( FLASH_WRITE_UNIT - 1 ) ) / FLASH_WRITE_UNIT ) * FLASH_WRITE_UNIT; // 向上取整到 32 字节对齐 // 按 32 字节批量写入 for ( i = 0; i < alignedLen; i += FLASH_WRITE_UNIT ) { chunkSize = ( i + FLASH_WRITE_UNIT <= len ) ? FLASH_WRITE_UNIT : ( len - i ); memset( alignedData, 0xFF, FLASH_WRITE_UNIT ); // 先填充 0xFF memcpy( alignedData, data + i, chunkSize ); // 复制实际数据 // 超时等待 Flash 空闲 timeout = FLASH_TIMEOUT; while ( --timeout > 0 ) { Fls_MainFunction( ); if ( Fls_GetStatus( ) == MEMIF_IDLE ) { break; } if ( timeout == 0 ) { return false; } } // 写入 Flash timeout = 50; while ( --timeout > 0 ) { if ( !Fls_Write( startAddress + i, alignedData, FLASH_WRITE_UNIT ) ) { break; } if ( timeout == 0 ) { return false; // 写入失败 } }; // 超时等待 Flash 空闲 timeout = FLASH_TIMEOUT; while ( --timeout > 0 ) { Fls_MainFunction( ); if ( Fls_GetStatus( ) == MEMIF_IDLE ) { break; } if ( timeout == 0 ) { return false; } } memset( readBuffer, 0, FLASH_WRITE_UNIT ); // 确保读取缓冲区清空 timeout = 50; while ( --timeout > 0 ) { if ( !Fls_Read( startAddress + i, readBuffer, FLASH_WRITE_UNIT ) ) { break; } if ( timeout == 0 ) { return false; // 写入失败 } }; // 等待读取完成 timeout = FLASH_TIMEOUT; while ( --timeout > 0 ) { Fls_MainFunction( ); if ( Fls_GetStatus( ) == MEMIF_IDLE ) { break; } if ( timeout == 0 ) { return false; } } // 进行数据比对 if ( memcmp( readBuffer, alignedData, FLASH_WRITE_UNIT ) != 0 ) { return false; // 校验失败 } } return true; }
以上是我写MCU内部FLS的功能代码,作用是把数据写入1mb的pflash1,以实现A/B SWAP。
现在发现,在调用完Fls_Read后,在等待读取完成代码段中,Fls_GetStatus( )一直不返回MEMIF_IDLE,导致任务卡顿。timeout = 50; while ( --timeout > 0 ) { if ( !Fls_Read( startAddress + i, readBuffer, FLASH_WRITE_UNIT ) ) { break; } if ( timeout == 0 ) { return false; // 写入失败 } }; // 等待读取完成 timeout = FLASH_TIMEOUT; while ( --timeout > 0 ) { Fls_MainFunction( ); if ( Fls_GetStatus( ) == MEMIF_IDLE ) { break; } if ( timeout == 0 ) { return false; } }
请问是我这个函数代码的逻辑有问题吗?
-
houjun_xiao 每次写之前都有擦除操作,也会判断是否空闲。现在代码逻辑上,读取之前已经有一个GetResult了。这个ECC错误,是只能通过擦除操作来清除吗?
-
houjun_xiao 好的,我们按这个方法试试,谢谢!
1/6
发帖前请查看
帮助没办法联网的电脑使用YCT
帮助改进和优化YT CONFIG TOOL,有机会抽取YTM32B1ME0 EVB哦...