/** * @brief Write sectors. * @param SectorAddress: Address of flash sector to write. * @param DataLength: Length of data to write. * @retval Media Status. */ uint16_t SdkEvalDFUFlashIfWrite(uint32_t lSectorAddress, uint32_t lDataLength) { uint32_t idx = 0; __IO uint32_t* malPointer = (uint32_t *)MAL_Buffer; __IO uint32_t* memPointer = (uint32_t *)lSectorAddress; __IO uint32_t memBuffer[32]; /* Temporary buffer holding data that will be written in a half-page space */ __IO uint32_t* mempBuffer = memBuffer; __IO uint32_t* tmp; if (lDataLength & 0x3) /* Not an aligned data */ { for (idx = lDataLength; idx < ((lDataLength & 0xFFFC) + 4); idx++) { MAL_Buffer[idx] = 0xFF; } } /* Reinitialize the intermediate buffer pointer */ mempBuffer = memBuffer; /* If the address is not aligned to half-page fill the first location with existing data */ if (((uint32_t)memPointer & 0x7F) != 0) { /* get the aligned address */ tmp = (uint32_t *)((uint32_t)memPointer & 0xFFFFFF80); /* Read the first part from the memory */ while (tmp < memPointer) { *(uint32_t *)(mempBuffer++) = *(uint32_t *)(tmp++); } } while (malPointer < (uint32_t*)(MAL_Buffer + lDataLength)) { /* Fill with the received buffer */ while (mempBuffer < (memBuffer + 32)) { /* If there are still data available in the received buffer */ if (malPointer < ((uint32_t *)MAL_Buffer + lDataLength)) { *(uint32_t *)(mempBuffer++) = *(uint32_t *)(malPointer++); } else /* no more data avaible in the received buffer: fill remaining with dummy 0 */ { *(uint32_t *)(mempBuffer++) = 0; } } /* Write the buffer to the memory*/ FLASH_ProgramHalfPage(((uint32_t)memPointer & 0xFFFFFF80), (uint32_t *)(memBuffer)); /* Increment the memory pointer */ memPointer = (uint32_t *)(((uint32_t)memPointer & 0xFFFFFF80) + (32*4)); /* Reinitialize the intermediate buffer pointer */ mempBuffer = memBuffer; } return MAL_OK; }
/******************************************************************************* * Function Name : FLASH_If_Write * Description : Write sectors * Input : None * Output : None * Return : None *******************************************************************************/ uint16_t FLASH_If_Write(uint32_t SectorAddress, uint32_t DataLength) { uint32_t idx = 0; #ifdef STM32L1XX_MD __IO uint32_t* malPointer = (uint32_t *)MAL_Buffer; __IO uint32_t* memPointer = (uint32_t *)SectorAddress; __IO uint32_t memBuffer[32]; /* Temporary buffer holding data that will be written in a half-page space */ __IO uint32_t* mempBuffer = memBuffer; __IO uint32_t* tmp; #endif /* STM32L1XX_MD */ if (DataLength & 0x3) /* Not an aligned data */ { for (idx = DataLength; idx < ((DataLength & 0xFFFC) + 4); idx++) { MAL_Buffer[idx] = 0xFF; } } #ifdef STM32L1XX_MD /* Reinitialize the intermediate buffer pointer */ mempBuffer = memBuffer; /* If the address is not aligned to half-page fill the first location with existing data */ if (((uint32_t)memPointer & 0x7F) != 0) { /* get the aligned address */ tmp = (uint32_t *)((uint32_t)memPointer & 0xFFFFFF80); /* Read the first part from the memory */ while (tmp < memPointer) { *(uint32_t *)(mempBuffer++) = *(uint32_t *)(tmp++); } } while (malPointer < (uint32_t*)(MAL_Buffer + DataLength)) { /* Fill with the received buffer */ while (mempBuffer < (memBuffer + 32)) { /* If there are still data available in the received buffer */ if (malPointer < ((uint32_t *)MAL_Buffer + DataLength)) { *(uint32_t *)(mempBuffer++) = *(uint32_t *)(malPointer++); } else /* no more data available in the received buffer: fill remaining with dummy 0 */ { *(uint32_t *)(mempBuffer++) = 0; } } /* Write the buffer to the memory*/ FLASH_ProgramHalfPage(((uint32_t)memPointer & 0xFFFFFF80), (uint32_t *)(memBuffer)); /* Increment the memory pointer */ memPointer = (uint32_t *)(((uint32_t)memPointer & 0xFFFFFF80) + (32*4)); /* Reinitialize the intermediate buffer pointer */ mempBuffer = memBuffer; } #else /* Data received are Word multiple */ for (idx = 0; idx < DataLength; idx = idx + 4) { FLASH_ProgramWord(SectorAddress, *(uint32_t *)(MAL_Buffer + idx)); SectorAddress += 4; } #endif /* STM32L1XX_MD */ return MAL_OK; }