/* -----------------------------------------------------------------
* startAdd: 必须为某一页的起始地址:baseFlashAdd60 -baseFlashAdd63
* countToWrite必须为8的倍数(即一次写入为Two Word的倍数)
*
* ----------------------------------------------------------------*/
void dtkWriteFlash(uint32_t startAdd, uint8_t *writeData, uint16_t countToWrite)
{
uint32_t i=0;
uint64_t tempWriteData;
uint32_t tempWriteAdd;
HAL_StatusTypeDef status;
uint32_t tempW1=0;
uint32_t tempW2=0;
HAL_FLASH_Unlock();
//HAL_FLASH_Unlock();
FLASH_EraseInitTypeDef f;
f.TypeErase = FLASH_TYPEERASE_PAGES;
f.Page = 31; //--只读写Page31的内容(即最后一个Page,2K字节
f.NbPages = 1;
uint32_t PageError = 0;
HAL_FLASHEx_Erase(&f, &PageError);
for(i=0; i<countToWrite ; i=i+8)
{
tempW1=0;
tempW2=0;
tempWriteData=0;
tempW1 = writeData[i];
tempW1 = tempW1<<8;
tempW1 = tempW1 | writeData[i+1];
tempW1 = tempW1<<8;
tempW1 = tempW1 | writeData[i+2];
tempW1 = tempW1<<8;
tempW1 = tempW1 | writeData[i+3];
tempW2 = writeData[i+4];
tempW2 = tempW2<<8;
tempW2 = tempW2 | writeData[i+5];
tempW2 = tempW2<<8;
tempW2 = tempW2 | writeData[i+6];
tempW2 = tempW2<<8;
tempW2 = tempW2 | writeData[i+7];
tempWriteData = tempWriteData|tempW2;
tempWriteData = tempWriteData<<32;
tempWriteData = tempWriteData | tempW1;
tempWriteAdd = startAdd + i;
//HAL_FLASH_Program(FLASH_TYPEPROGRAM_FAST, tempWriteAdd, tempWriteData);
HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, tempWriteAdd, tempWriteData);
// Wait for last operation to be completed
status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
// If the program operation is completed, disable the PG Bit
CLEAR_BIT(FLASH->CR, FLASH_CR_PG);
// In case of error, stop programation procedure
if (status != HAL_OK)
{
break;
}
}
}
|