/** * @brief Programs a memory block * @note This function should be called and executed from RAM. * @param BlockNum: The block number * @param FLASH_ProgramMode: The programming mode. * This parameter can be one of the following values: * @arg FLASH_ProgramMode_Standard: Standard programming mode * @arg FLASH_ProgramMode_Fast: Fast programming mode * @param Buffer: Pointer to buffer containing source data. * @retval None */ IN_RAM(void FLASH_ProgramBlock(uint8_t BlockNum, FLASH_ProgramMode_TypeDef FLASH_ProgramMode, uint8_t *Buffer)) { uint16_t Count = 0; uint16_t StartAddress = 0; /* Check parameters */ assert_param(IS_FLASH_BLOCK_NUMBER(BlockNum)); assert_param(IS_FLASH_PROGRAM_MODE(FLASH_ProgramMode)); /* Selection of Standard or Fast programming mode */ if (FLASH_ProgramMode == FLASH_ProgramMode_Standard) { /* Standard programming mode */ FLASH->CR2 |= FLASH_CR2_PRG; } else { /* Fast programming mode */ FLASH->CR2 |= FLASH_CR2_FPRG; } StartAddress = FLASH_START_PHYSICAL_ADDRESS; /* Point to the first Block address */ StartAddress = StartAddress + ((uint16_t)BlockNum * (uint16_t)FLASH_BLOCK_SIZE); /* Copy data bytes from RAM to FLASH memory */ for (Count = 0; Count < FLASH_BLOCK_SIZE; Count++) { *((PointerAttr uint8_t*) (uint16_t)StartAddress + Count) = ((uint8_t)(Buffer[Count])); } }
/** * @brief Programs a memory block * @note This function should be called and executed from RAM. * @param FLASH_MemType : The type of memory to program * This parameter can be one of the following values: * @arg FLASH_MemType_Program: Program memory * @arg FLASH_MemType_Data: Data EEPROM memory * @param BlockNum : The block number * @param FLASH_ProgMode : The programming mode. * This parameter can be one of the following values: * @arg FLASH_ProgramMode_Standard: Standard programming mode * @arg FLASH_ProgramMode_Fast: Fast programming mode * @param Buffer : Pointer to buffer containing source data. * @retval None. */ IN_RAM(void FLASH_ProgramBlock(uint16_t BlockNum, FLASH_MemType_TypeDef FLASH_MemType, FLASH_ProgramMode_TypeDef FLASH_ProgMode, uint8_t *Buffer)) { uint16_t Count = 0; uint32_t startaddress = 0; /* Check parameters */ assert_param(IS_FLASH_MEMORY_TYPE(FLASH_MemType)); assert_param(IS_FLASH_PROGRAM_MODE(FLASH_ProgMode)); if (FLASH_MemType == FLASH_MemType_Program) { assert_param(IS_FLASH_PROGRAM_BLOCK_NUMBER(BlockNum)); startaddress = FLASH_PROGRAM_START_PHYSICAL_ADDRESS; } else { assert_param(IS_FLASH_DATA_EEPROM_BLOCK_NUMBER(BlockNum)); startaddress = FLASH_DATA_EEPROM_START_PHYSICAL_ADDRESS; } /* Point to the first block address */ startaddress = startaddress + ((uint32_t)BlockNum * FLASH_BLOCK_SIZE); /* Selection of Standard or Fast programming mode */ if (FLASH_ProgMode == FLASH_ProgramMode_Standard) { /* Standard programming mode */ FLASH->CR2 |= FLASH_CR2_PRG; } else { /* Fast programming mode */ FLASH->CR2 |= FLASH_CR2_FPRG; } /* Copy data bytes from RAM to FLASH memory */ for (Count = 0; Count < FLASH_BLOCK_SIZE; Count++) { #if defined (STM8L15X_MD) || defined (STM8L15X_MDP) || defined (STM8L15X_LD) *((PointerAttr uint8_t*) (uint16_t)startaddress + Count) = ((uint8_t)(Buffer[Count])); #elif defined (STM8L15X_HD) *((PointerAttr uint8_t*) (uint32_t)startaddress + Count) = ((uint8_t)(Buffer[Count])); #endif } }