本文整理汇总了C++中HAL_FLASH_Program函数的典型用法代码示例。如果您正苦于以下问题:C++ HAL_FLASH_Program函数的具体用法?C++ HAL_FLASH_Program怎么用?C++ HAL_FLASH_Program使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了HAL_FLASH_Program函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: flash_write
void flash_write(const uint32_t *src, uint32_t dst, uint32_t size)
{
// Unlock flash
HAL_FLASH_Unlock();
#if defined(MCU_SERIES_H7)
// Program the flash 32 bytes at a time.
for (int i=0; i<size/32; i++) {
if (HAL_FLASH_Program(TYPEPROGRAM_WORD, dst, (uint64_t)(uint32_t) src) != HAL_OK) {
// error occurred during flash write
HAL_FLASH_Lock(); // lock the flash
__fatal_error();
}
src += 8;
dst += 32;
}
#else
// Program the flash 4 bytes at a time.
for (int i=0; i<size/4; i++) {
if (HAL_FLASH_Program(TYPEPROGRAM_WORD, dst, *src) != HAL_OK) {
// error occurred during flash write
HAL_FLASH_Lock(); // lock the flash
__fatal_error();
}
src += 1;
dst += 4;
}
#endif
// lock the flash
HAL_FLASH_Lock();
}
示例2: flash_program_page
/** Program one page starting at defined address
*
* The page should be at page boundary, should not cross multiple sectors.
* This function does not do any check for address alignments or if size
* is aligned to a page size.
* @param obj The flash object
* @param address The sector starting address
* @param data The data buffer to be programmed
* @param size The number of bytes to program
* @return 0 for success, -1 for error
*/
int32_t flash_program_page(flash_t *obj, uint32_t address,
const uint8_t *data, uint32_t size)
{
uint32_t StartAddress = 0;
int32_t status = 0;
if ((address >= (FLASH_BASE + FLASH_SIZE)) || (address < FLASH_BASE)) {
return -1;
}
if ((size % 8) != 0) {
/* L4 flash devices can only be programmed 64bits/8 bytes at a time */
return -1;
}
if (flash_unlock() != HAL_OK) {
return -1;
}
/* Program the user Flash area word by word */
StartAddress = address;
/* HW needs an aligned address to program flash, which data
* parameters doesn't ensure */
if ((uint32_t) data % 4 != 0) {
volatile uint64_t data64;
while ((address < (StartAddress + size)) && (status == 0)) {
for (uint8_t i =0; i < 8; i++) {
*(((uint8_t *) &data64) + i) = *(data + i);
}
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, address, data64)
== HAL_OK) {
address = address + 8;
data = data + 8;
} else {
status = -1;
}
}
} else { /* case where data is aligned, so let's avoid any copy */
while ((address < (StartAddress + size)) && (status == 0)) {
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, address,
*((uint64_t*) data))
== HAL_OK) {
address = address + 8;
data = data + 8;
} else {
status = -1;
}
}
}
flash_lock();
return status;
}
示例3: flash_program_page
int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, uint32_t size)
{
uint32_t StartAddress = 0;
int32_t status = 0;
if (!(IS_FLASH_PROGRAM_ADDRESS(address))) {
return -1;
}
if ((size % MIN_PROG_SIZE) != 0) {
return -1;
}
if (flash_unlock() != HAL_OK) {
return -1;
}
/* Program the user Flash area word by word */
StartAddress = address;
/* HW needs an aligned address to program flash, which data parameter doesn't ensure */
if ((uint32_t) data % 4 != 0) {
volatile uint32_t data32;
while (address < (StartAddress + size) && (status == 0)) {
for (uint8_t i = 0; i < MIN_PROG_SIZE; i++) {
*(((uint8_t *) &data32) + i) = *(data + i);
}
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, address, data32) == HAL_OK) {
address = address + MIN_PROG_SIZE;
data = data + MIN_PROG_SIZE;
} else {
status = -1;
}
}
} else { /* case where data is aligned, so let's avoid any copy */
while ((address < (StartAddress + size)) && (status == 0)) {
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, address, *((uint32_t*) data)) == HAL_OK) {
address = address + MIN_PROG_SIZE;
data = data + MIN_PROG_SIZE;
} else {
status = -1;
}
}
}
flash_lock();
return status;
}
示例4: EE_VerifyPageFullWriteVariable
/**
* @brief Verify if active page is full and Writes variable in EEPROM.
* @param VirtAddress: 16 bit virtual address of the variable
* @param Data: 16 bit data to be written as variable value
* @retval Success or error status:
* - FLASH_COMPLETE: on success
* - PAGE_FULL: if valid page is full
* - NO_VALID_PAGE: if no valid page was found
* - Flash error code: on write Flash error
*/
static uint16_t EE_VerifyPageFullWriteVariable(uint16_t VirtAddress, uint16_t Data)
{
HAL_StatusTypeDef FlashStatus = HAL_OK;
uint16_t ValidPage = PAGE0;
uint32_t Address = EEPROM_START_ADDRESS, PageEndAddress = EEPROM_START_ADDRESS+PAGE_SIZE;
/* Get valid Page for write operation */
ValidPage = EE_FindValidPage(WRITE_IN_VALID_PAGE);
/* Check if there is no valid page */
if (ValidPage == NO_VALID_PAGE)
{
return NO_VALID_PAGE;
}
/* Get the valid Page start Address */
Address = (uint32_t)(EEPROM_START_ADDRESS + (uint32_t)(ValidPage * PAGE_SIZE));
/* Get the valid Page end Address */
PageEndAddress = (uint32_t)((EEPROM_START_ADDRESS - 1) + (uint32_t)((ValidPage + 1) * PAGE_SIZE));
/* Check each active page address starting from begining */
while (Address < PageEndAddress)
{
/* Verify if Address and Address+2 contents are 0xFFFFFFFF */
if ((*(__IO uint32_t*)Address) == 0xFFFFFFFF)
{
/* Set variable data */
FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, Address, Data);
/* If program operation was failed, a Flash error code is returned */
if (FlashStatus != HAL_OK)
{
return FlashStatus;
}
/* Set variable virtual address */
FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, Address + 2, VirtAddress);
/* Return program operation status */
return FlashStatus;
}
else
{
/* Next address location */
Address = Address + 4;
}
}
/* Return PAGE_FULL in case the valid page is full */
return PAGE_FULL;
}
示例5: FLASH_If_Write
/**
* @brief This function writes a data buffer in flash (data are 32-bit aligned).
* @note After writing data buffer, the flash content is checked.
* @param destination: start address for target location
* @param p_source: pointer on buffer with data to write
* @param length: length of data buffer (unit is 32-bit word)
* @retval uint32_t 0: Data successfully written to Flash memory
* 1: Error occurred while writing data in Flash memory
* 2: Written Data in flash memory is different from expected one
*/
uint32_t FLASH_If_Write(uint32_t destination, uint32_t *p_source, uint32_t length)
{
uint32_t i = 0;
/* Unlock the Flash to enable the flash control register access *************/
HAL_FLASH_Unlock();
for (i = 0; (i < length) && (destination <= (USER_FLASH_END_ADDRESS-4)); i++)
{
/* Device voltage range supposed to be [2.7V to 3.6V], the operation will
be done by word */
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, destination, *(uint32_t*)(p_source+i)) == HAL_OK)
{
/* Check the written value */
if (*(uint32_t*)destination != *(uint32_t*)(p_source+i))
{
/* Flash content doesn't match SRAM content */
return(FLASHIF_WRITINGCTRL_ERROR);
}
/* Increment FLASH destination address */
destination += 4;
}
else
{
/* Error occurred while writing data in Flash memory */
return (FLASHIF_WRITING_ERROR);
}
}
/* Lock the Flash to disable the flash control register access (recommended
to protect the FLASH memory against possible unwanted operation) *********/
HAL_FLASH_Lock();
return (FLASHIF_OK);
}
示例6: writeEEPROM
// FIXME: HAL for now this will only work for F4/F7 as flash layout is different
void writeEEPROM(void)
{
// Generate compile time error if the config does not fit in the reserved area of flash.
BUILD_BUG_ON(sizeof(master_t) > FLASH_TO_RESERVE_FOR_CONFIG);
HAL_StatusTypeDef status;
uint32_t wordOffset;
int8_t attemptsRemaining = 3;
suspendRxSignal();
// prepare checksum/version constants
masterConfig.version = EEPROM_CONF_VERSION;
masterConfig.size = sizeof(master_t);
masterConfig.magic_be = 0xBE;
masterConfig.magic_ef = 0xEF;
masterConfig.chk = 0; // erase checksum before recalculating
masterConfig.chk = calculateChecksum((const uint8_t *) &masterConfig, sizeof(master_t));
// write it
/* Unlock the Flash to enable the flash control register access *************/
HAL_FLASH_Unlock();
while (attemptsRemaining--)
{
/* Fill EraseInit structure*/
FLASH_EraseInitTypeDef EraseInitStruct = {0};
EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3; // 2.7-3.6V
EraseInitStruct.Sector = (FLASH_SECTOR_TOTAL-1);
EraseInitStruct.NbSectors = 1;
uint32_t SECTORError;
status = HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError);
if (status != HAL_OK)
{
continue;
}
else
{
for (wordOffset = 0; wordOffset < sizeof(master_t); wordOffset += 4)
{
status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, CONFIG_START_FLASH_ADDRESS + wordOffset, *(uint32_t *) ((char *) &masterConfig + wordOffset));
if(status != HAL_OK)
{
break;
}
}
}
if (status == HAL_OK) {
break;
}
}
HAL_FLASH_Lock();
// Flash write failed - just die now
if (status != HAL_OK || !isEEPROMContentValid()) {
failureMode(FAILURE_FLASH_WRITE_FAILED);
}
resumeRxSignal();
}
示例7: WriteFlash
HAL_StatusTypeDef WriteFlash(int32_t* table, uint32_t Address, uint32_t lenth)
{
HAL_StatusTypeDef res;
HAL_FLASH_Unlock();
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR);
/* Erase the user Flash area
(area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
EraseFlash(Address);
for(int i=0;i<lenth;i++)
HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD,Address+4*i,*(table+i));
res = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD,Address+4*lenth-4,table[lenth-1]);
HAL_FLASH_Lock();
return res;
}
示例8: EE_Format
/**
* @brief Erases PAGE and PAGE1 and writes VALID_PAGE header to PAGE
* @param None
* @retval Status of the last operation (Flash write or erase) done during
* EEPROM formating
*/
static HAL_StatusTypeDef EE_Format(void)
{
HAL_StatusTypeDef OperationStatus = HAL_OK;
/* Erase Page0 */
FLASH_Erase_Sector(PAGE0_ID, VOLTAGE_RANGE);
/* If erase operation was failed, a Flash error code is returned */
OperationStatus = FLASH_WaitForLastOperation(1000);
if(OperationStatus != HAL_OK)
{
return OperationStatus;
}
/* Set Page0 as valid page: Write VALID_PAGE at Page0 base address */
OperationStatus = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, PAGE0_BASE_ADDRESS, VALID_PAGE);
/* If program operation was failed, a Flash error code is returned */
if (OperationStatus != HAL_OK)
{
return OperationStatus;
}
/* Erase Page1 */
FLASH_Erase_Sector(PAGE1_ID, VOLTAGE_RANGE);
OperationStatus = FLASH_WaitForLastOperation(1000);
if(OperationStatus != HAL_OK)
{
return OperationStatus;
}
/* Return Page1 erase operation status */
return OperationStatus;
}
示例9: Flash_If_Write
/**
* @brief Writes Data into Memory.
* @param src: Pointer to the source buffer. Address to be written to.
* @param dest: Pointer to the destination buffer.
* @param Len: Number of data to be written (in bytes).
* @retval 0 if operation is successeful, MAL_FAIL else.
*/
uint16_t Flash_If_Write(uint8_t *src, uint8_t *dest, uint32_t Len)
{
uint32_t i = 0;
for(i = 0; i < Len; i+=4)
{
/* Device voltage range supposed to be [2.7V to 3.6V], the operation will
be done by byte */
if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, (uint32_t)(dest+i), *(uint32_t*)(src+i)) == HAL_OK)
{
/* Check the written value */
if(*(uint32_t *)(src + i) != *(uint32_t*)(dest+i))
{
/* Flash content doesn't match SRAM content */
return 2;
}
}
else
{
/* Error occurred while writing data in Flash memory */
return 1;
}
}
return 0;
}
示例10: stm32f3_flash_program
/**
* @brief Write data to flash memory
*
* @param dev
* @param base
* @param data
* @param len
*
* @return Negative error code
*/
static int stm32f3_flash_program(struct flash_dev *dev, uint32_t base, const void* data, size_t len) {
int err = 0;
int offset;
if (!stm32f3_flash_check_align(base, len)
|| ((uintptr_t) data & 0x1) != 0) {
return -EINVAL;
}
if (!stm32f3_flash_check_range(dev, base, len)) {
return -EFBIG;
}
HAL_FLASH_Unlock();
/* Write data halfword by halfword */
for (offset = 0; offset < len; offset += 2) {
if (HAL_OK != HAL_FLASH_Program(TYPEPROGRAM_HALFWORD,
dev->start + base + offset,
*((uint32_t*) data + offset)))
return -1; /* TODO: set appropriate code */
}
/* Lock to prevent unwanted operations with flash memory */
HAL_FLASH_Lock();
return err;
}
示例11: program_flash_data
int program_flash_data(uint32_t start, uint32_t size, uint8_t *data)
{
uint64_t data64;
uint32_t offset = 0;
while (offset < size) {
data64 = 0xFFFFFFFFFFFFFFFF;
if((size - offset) < 8) {
memcpy((uint8_t *)&data64, (uint8_t *)&data[offset], (size - offset));
} else {
memcpy((uint8_t *)&data64, (uint8_t *)&data[offset], 8);
}
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, start, data64) == HAL_OK) {
start += 8;
offset += 8;
} else {
/* Error occurred while writing data in Flash memory.
User can add here some code to deal with this error */
while (1) {
}
}
}
return 0;
}
示例12: stm32f4_flash_write
static int
stm32f4_flash_write(const struct hal_flash *dev, uint32_t address,
const void *src, uint32_t num_bytes)
{
const uint8_t *sptr;
uint32_t i;
int rc;
sptr = src;
/*
* Clear status of previous operation.
*/
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | \
FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR| FLASH_FLAG_PGSERR);
for (i = 0; i < num_bytes; i++) {
rc = HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, address, sptr[i]);
if (rc != 0) {
return rc;
}
address++;
}
return 0;
}
示例13: FLASH_If_Write
/**
* @brief This function writes a data buffer in flash (data are 32-bit aligned).
* @note After writing data buffer, the flash content is checked.
* @param FlashAddress: start address for writing data buffer
* @param Data: pointer on data buffer
* @param DataLength: length of data buffer (unit is 32-bit word)
* @retval 0: Data successfully written to Flash memory
* 1: Error occurred while writing data in Flash memory
* 2: Written Data in flash memory is different from expected one
*/
uint32_t FLASH_If_Write(__IO uint32_t* FlashAddress, uint32_t* Data ,uint16_t DataLength)
{
uint32_t i = 0;
for (i = 0; (i < DataLength) && (*FlashAddress <= (USER_FLASH_END_ADDRESS-4)); i++)
{
/* Device voltage range supposed to be [2.7V to 3.6V], the operation will
be done by word */
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, *FlashAddress, *(uint32_t*)(Data+i)) == HAL_OK)
{
/* Check the written value */
if (*(uint32_t*)*FlashAddress != *(uint32_t*)(Data+i))
{
/* Flash content doesn't match SRAM content */
return(2);
}
/* Increment FLASH destination address */
*FlashAddress += 4;
}
else
{
/* Error occurred while writing data in Flash memory */
return (1);
}
}
return (0);
}
示例14: SaveSSIDPasswordToMemory
/**
* @brief Save Access Point parameters to FLASH
* @param None
* @retval System_Status_t (MODULE_SUCCESS/MODULE_ERROR)
*/
System_Status_t SaveSSIDPasswordToMemory(void)
{
System_Status_t status = MODULE_ERROR;
/* Reset Before The data in Memory */
status = ResetSSIDPasswordInMemory();
if (status) {
/* Store in Flash Memory */
uint32_t Address = BLUEMSYS_FLASH_ADD;
int32_t WriteIndex;
/* Unlock the Flash to enable the flash control register access *************/
HAL_FLASH_Unlock();
/* Write the Magic Number */
{
uint32_t MagicNumber = WIFI_CHECK_SSID_KEY;
if (HAL_FLASH_Program(TYPEPROGRAM_WORD, Address,MagicNumber) == HAL_OK) {
Address = Address + 4;
} else {
printf("\r\nError while writing in FLASH");
status = MODULE_ERROR;
}
}
/* Write the Wifi */
for (WriteIndex=0;WriteIndex<UNION_DATA_SIZE;WriteIndex++) {
if (HAL_FLASH_Program(TYPEPROGRAM_WORD, Address,UnionWifiToken.Data[WriteIndex]) == HAL_OK){
Address = Address + 4;
} else {
printf("\r\nError while writing in FLASH");
status = MODULE_ERROR;
}
}
/* Lock the Flash to disable the flash control register access (recommended
to protect the FLASH memory against possible unwanted operation) *********/
HAL_FLASH_Lock();
printf("\n\rSaveSSIDPasswordToMemory OK");
} else {
printf("\n\rError while resetting FLASH memory");
}
return status;
}
示例15: program_flash_dword
int program_flash_dword(const uint64_t *dword)
{
int rv;
dbgprintx32("program_flash_dword ", PARTITION_FLASHMODE_START, "\r\n");
rv = HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, PARTITION_FLASHMODE_START, *dword);
return rv;
}