stm32_ota/HARDWARE/LOGFLASH/logflash.c

158 lines
8.7 KiB
C
Raw Normal View History

2023-12-15 06:28:06 +00:00
#include "logflash.h"
#include <string.h>
#include "sysport.h"
#include "rtc.h"
#include "user_flash.h"
/********************************************************************************
* @file logflash.c
* @author <EFBFBD>̳ϿƼ<EFBFBD> Mr.Wang
* @version V1.0.0
* @date 11-Dec-2018
* @brief MCUδʹ<EFBFBD>õ<EFBFBD>FLASH<EFBFBD>ռ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>־<EFBFBD>Ĵ
******************************************************************************
* @attention:LogoFlashInfo<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ռ<EFBFBD><EFBFBD>1ҳ<EFBFBD>ռ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD>LOGFLASH_INFO_ADDRESS<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>uLogFlash<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼logflash<EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD>
* LogoFlash<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD>FLASH<EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>пռ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><EFBFBD>ַΪ<EFBFBD><EFBFBD>LOGFLASH_START_ADDRESS<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>LogoFlashInfo<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD>LOG<EFBFBD><EFBFBD><EFBFBD>ݡ<EFBFBD>
* @use<EFBFBD><EFBFBD>
* <EFBFBD>ȵ<EFBFBD><EFBFBD><EFBFBD>void LogFlash_Init(void)<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϲ<EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>LOG_AREA_ADDR<EFBFBD><EFBFBD>ַ
*******************************************************************************/
/*****************************************
*<EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD>ʹ<EFBFBD>ó<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
****************************************/
LogFlash_u uLogFlash ; //LogFlash<73><68>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>uLogFlash<73><68><EFBFBD><EFBFBD><EFBFBD><EFBFBD>logflash<73><68><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
/****************************************************************************
* <EFBFBD><EFBFBD> <EFBFBD>ƣ<EFBFBD>void LogFlash_Init(void)
* <EFBFBD><EFBFBD> <EFBFBD>ܣ<EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>LogFlash<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD>uLogFlash<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʷLOG
****************************************************************************/
void LogFlash_Init(void)
{
Read_Flash_Byte(LOGFLASH_INFO_ADDRESS, uLogFlash.bytes, sizeof(LogFlash_s)) ; //<2F><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>flash<73><68>LogoFlashInfo<66><6F><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E6B4A2>LogFlash<73><68>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if( (uLogFlash.sLogFlash.head == 0x50505050) && (uLogFlash.sLogFlash.tail == 0x05050505)) /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><CBB5>LogFlash<73>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ù<EFBFBD>*/
{
AppLogPrintf("Log Flash<73><EFBFBD><E6B4A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>%d<><64> <20><>\r\n<EFBFBD><EFBFBD><EFBFBD><EFBFBD>LOG: ", uLogFlash.sLogFlash.overflowCounter) ; //<2F><><EFBFBD><EFBFBD>LogoFlash<73><68><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E6B4A2><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD>
ReadLogFromFlash(LOGFLASH_START_ADDRESS, uLogFlash.sLogFlash.writeOffset) ; //<2F><><EFBFBD>ִ<EFBFBD><D6B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>LOG<4F><47><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8>debug<75><67><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
uLogFlash.sLogFlash.overflowCounter = 0 ; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>LOG<4F>󣬽<EFBFBD>LogoFlash<73><68><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E6B4A2><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Refresh_LogFlash_Info() ; //<2F><><EFBFBD>´<EFBFBD><C2B4><EFBFBD>flash<73><68>LogoFlashInfo<66><6F><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E6B4A2>LogFlash<73><68>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
return ;
}
else /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><CBB5>LogFlash<73><68>һ<EFBFBD>α<EFBFBD><CEB1><EFBFBD>ʼ<EFBFBD><CABC>*/
{
uLogFlash.sLogFlash.head = 0x50505050 ; //uLogFlash<73><68>Ϣ <20><><EFBFBD><EFBFBD>ͷ
uLogFlash.sLogFlash.tail = 0x05050505 ; //uLogFlash<73><68>Ϣ <20><><EFBFBD><EFBFBD>β
uLogFlash.sLogFlash.overflowCounter = 0 ; //uLogFlash<73><68>Ϣ <20><EFBFBD><E6B4A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Erase_LogFlash() ; //<2F><>LogoFlash<73><EFBFBD><E6B4A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
}
/**************************************************************************************************
* <EFBFBD><EFBFBD> <EFBFBD>ƣ<EFBFBD>void Erase_LogFlash(void)
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>LogFlash<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD>÷<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ⲿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*************************************************************************************************/
void Erase_LogFlash(void)
{
FLASH_Unlock(); //<2F><><EFBFBD><EFBFBD>FLASH
for( uint8_t n = 0; n< LOGFLASH_SIZE/FLASH_PAGE_SIZE; n++) //ѭ<><D1AD>ҳ<EFBFBD><D2B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>LOGFLASH<53><48><EFBFBD><EFBFBD>
{
FLASH_ErasePage(LOGFLASH_START_ADDRESS+n*FLASH_PAGE_SIZE) ;//<2F><><EFBFBD><EFBFBD>һҳ<D2BB><D2B3><EFBFBD><EFBFBD>
}
uLogFlash.sLogFlash.writeOffset = 0 ; //uLogFlash<73><68>Ϣ дƫ<D0B4><C6AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
uLogFlash.sLogFlash.readOffset = 0 ; //uLogFlash<73><68>Ϣ <20><>ƫ<EFBFBD><C6AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Refresh_LogFlash_Info() ; //<2F><>uLogFlash<73><68><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݴ洢<DDB4><E6B4A2>LogoFlashInfo<66><6F><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
FLASH_Lock(); //FLASH<53><48><EFBFBD><EFBFBD>
}
/****************************************************************************
* <EFBFBD><EFBFBD> <EFBFBD>ƣ<EFBFBD>void Refresh_LogFlash_Info(void)
* <EFBFBD><EFBFBD> <EFBFBD>ܣ<EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>flash<EFBFBD><EFBFBD>LogoFlashInfo<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>LogFlash<EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>uLogFlash
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD><EFBFBD>LogoFlashInfo<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD>
****************************************************************************/
void Refresh_LogFlash_Info(void)
{
FLASH_ErasePage(LOGFLASH_INFO_ADDRESS) ; //<2F><><EFBFBD><EFBFBD>LogoFlashInfo<66><6F><EFBFBD><EFBFBD>
Write_Flash_OnePage(LOGFLASH_INFO_ADDRESS, uLogFlash.bytes, sizeof(LogFlash_s)) ; //<2F><>uLogFlash<73><68><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݴ洢<DDB4><E6B4A2>LogoFlashInfo<66><6F><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
/****************************************************************************
* <EFBFBD><EFBFBD> <EFBFBD>ƣ<EFBFBD>void WriteLogToFlash(char *buffer)
* <EFBFBD><EFBFBD> <EFBFBD>ܣ<EFBFBD><EFBFBD><EFBFBD>FLASHд<EFBFBD><EFBFBD>buffer
* <EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param1 *buffer <EFBFBD><EFBFBD>Ҫд<EFBFBD><EFBFBD>FLASH<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
****************************************************************************/
void WriteLogToFlash(char *buffer)
{
uint16_t bufferLen = strlen(buffer) + 20 ; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4>ռ<D5BC>ȣ<EFBFBD><C8A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>20bytes<65><73><EFBFBD>ڴ洢uCalendar.bytes<65><73><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
char* logWriteBuffer = portMalloc(bufferLen) ; //<2F><><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD>
uint16_t outLen = 0, index = 0, timeOut = 0 ;
outLen = snprintf((char*)(logWriteBuffer), bufferLen, "*%s %s* ",(const char*)&uCalendar.bytes[11], buffer) ; //ƴ<><C6B4><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֹlogFlashBufferԽ<72><D4BD>
outLen = outLen/2+(outLen%2) ; //<2F><>logLenǿ<6E>Ƹı<C6B8>Ϊ2<CEAA>ı<EFBFBD><C4B1><EFBFBD> <20><>logLen<65>ֽڳ<D6BD><DAB3><EFBFBD>תΪ<D7AA><CEAA><EFBFBD>ֳ<EFBFBD><D6B3><EFBFBD>
FLASH_Unlock(); //FLASH<53><48><EFBFBD><EFBFBD>
FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR) ; //<2F><>FLASH<53><48>־λ
for( timeOut=0; (SET == FLASH_GetFlagStatus(FLASH_FLAG_BSY)); timeOut++)
{
Wait_For_Nms(10) ;
if( timeOut>10 )
{
ErrorLogPrintf("дFlashæ<EFBFBD>ȴ<EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>") ;
FLASH_Unlock(); //FLASH<53><48><EFBFBD><EFBFBD>
portFree(logWriteBuffer) ;
return ;
}
}
uint16_t halfWord ;
while(outLen--)
{
FLASH_ClearFlag( FLASH_FLAG_EOP ) ;
halfWord = *(logWriteBuffer+(index++)) ; //ȡuint16_t<5F><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>halfWord LSB
if( (halfWord & 0x00FF) == 0x0000) //<2F><>halfWord<72>еĿ<D0B5><C4BF>ַ<EFBFBD><D6B7><EFBFBD>0x00<30><30><EFBFBD>á<EFBFBD><C3A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ţ<EFBFBD>0x2D<32><44><EFBFBD>
halfWord = halfWord | 0x002D ;
halfWord |= *(logWriteBuffer+(index++)) << 8 ; //ȡuint16_t<5F><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>halfWord HSB
if( (halfWord & 0xFF00) == 0x0000)
halfWord = halfWord | 0x2D00 ;
if( (uLogFlash.sLogFlash.writeOffset) >= (LOGFLASH_SIZE - bufferLen) ) //<2F><>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>FLASH<53><48>Χ<EFBFBD><CEA7><EFBFBD><EFBFBD><EFBFBD>¿<EFBFBD>ʼд
{
uLogFlash.sLogFlash.writeOffset = 0 ;
uLogFlash.sLogFlash.overflowCounter++ ; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ
}
if( ( (LOGFLASH_START_ADDRESS + uLogFlash.sLogFlash.writeOffset) % FLASH_PAGE_SIZE ) == 0 ) //д<><D0B4><EFBFBD><EFBFBD>ַΪҳ<CEAA>׵<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҳ
{
FLASH_ErasePage(LOGFLASH_START_ADDRESS+uLogFlash.sLogFlash.writeOffset) ;
}
FLASH_ProgramHalfWord(LOGFLASH_START_ADDRESS+uLogFlash.sLogFlash.writeOffset, halfWord) ; //<2F><>ַƫ<D6B7><C6AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƫ<EFBFBD>ƺ<EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD>ֹFLASH_ERROR_PG<50><47><EFBFBD><EFBFBD>
uLogFlash.sLogFlash.writeOffset += 2;
}
Refresh_LogFlash_Info() ; //<2F><><EFBFBD>´<EFBFBD><C2B4><EFBFBD>flash<73><68>LogoFlashInfo<66><6F><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E6B4A2>LogFlash<73><68>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
FLASH_Lock(); //FLASH<53><48><EFBFBD><EFBFBD>
AppLogPrintf( "Flashƫ<EFBFBD>Ƴ<EFBFBD><EFBFBD><EFBFBD>:%#x", uLogFlash.sLogFlash.writeOffset );
portFree(logWriteBuffer) ; //<2F>ͷŶ<CDB7>̬<EFBFBD>ڴ<EFBFBD><DAB4>ռ<EFBFBD>
}
/****************************************************************************
* <EFBFBD><EFBFBD> <EFBFBD>ƣ<EFBFBD>void ReadLogFromFlash(uint32_t readAddr, uint32_t length)
* <EFBFBD><EFBFBD> <EFBFBD>ܣ<EFBFBD><EFBFBD><EFBFBD>readAddr<EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>log<EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param1 readAddr <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>ַ
* @param3 length <EFBFBD><EFBFBD>Ҫд<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵij<EFBFBD><EFBFBD><EFBFBD>
****************************************************************************/
void ReadLogFromFlash(uint32_t readAddr, uint32_t length)
{
uint8_t* logReadBuffer = portMalloc(FLASH_PAGE_SIZE) ;
uint16_t n = length/FLASH_PAGE_SIZE ;
while(n--)
{
Read_Flash_Byte(readAddr, logReadBuffer, FLASH_PAGE_SIZE) ;
UARTx_SendData(UART_DEBUG, (char*)logReadBuffer, FLASH_PAGE_SIZE ) ;
readAddr = readAddr+FLASH_PAGE_SIZE ;
}
memset(logReadBuffer, 0, FLASH_PAGE_SIZE) ;
Read_Flash_Byte(readAddr, logReadBuffer, length%FLASH_PAGE_SIZE ) ;
UARTx_SendData(UART_DEBUG, (char*)logReadBuffer, length%FLASH_PAGE_SIZE ) ;
portFree(logReadBuffer) ;
}