123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713 |
- #include "stm8s_flash.h"
- #define FLASH_CLEAR_BYTE ((uint8_t)0x00)
- #define FLASH_SET_BYTE ((uint8_t)0xFF)
- #define OPERATION_TIMEOUT ((uint16_t)0xFFFF)
- void FLASH_Unlock(FLASH_MemType_TypeDef FLASH_MemType)
- {
-
- assert_param(IS_MEMORY_TYPE_OK(FLASH_MemType));
-
-
- if(FLASH_MemType == FLASH_MEMTYPE_PROG)
- {
- FLASH->PUKR = FLASH_RASS_KEY1;
- FLASH->PUKR = FLASH_RASS_KEY2;
- }
-
- else
- {
- FLASH->DUKR = FLASH_RASS_KEY2;
- FLASH->DUKR = FLASH_RASS_KEY1;
- }
- }
- void FLASH_Lock(FLASH_MemType_TypeDef FLASH_MemType)
- {
-
- assert_param(IS_MEMORY_TYPE_OK(FLASH_MemType));
-
-
- FLASH->IAPSR &= (uint8_t)FLASH_MemType;
- }
- void FLASH_DeInit(void)
- {
- FLASH->CR1 = FLASH_CR1_RESET_VALUE;
- FLASH->CR2 = FLASH_CR2_RESET_VALUE;
- FLASH->NCR2 = FLASH_NCR2_RESET_VALUE;
- FLASH->IAPSR &= (uint8_t)(~FLASH_IAPSR_DUL);
- FLASH->IAPSR &= (uint8_t)(~FLASH_IAPSR_PUL);
- (void) FLASH->IAPSR;
- }
- void FLASH_ITConfig(FunctionalState NewState)
- {
-
- assert_param(IS_FUNCTIONALSTATE_OK(NewState));
-
- if(NewState != DISABLE)
- {
- FLASH->CR1 |= FLASH_CR1_IE;
- }
- else
- {
- FLASH->CR1 &= (uint8_t)(~FLASH_CR1_IE);
- }
- }
- void FLASH_EraseByte(uint32_t Address)
- {
-
- assert_param(IS_FLASH_ADDRESS_OK(Address));
-
-
- *(PointerAttr uint8_t*) (MemoryAddressCast)Address = FLASH_CLEAR_BYTE;
- }
- void FLASH_ProgramByte(uint32_t Address, uint8_t Data)
- {
-
- assert_param(IS_FLASH_ADDRESS_OK(Address));
- *(PointerAttr uint8_t*) (MemoryAddressCast)Address = Data;
- }
- uint8_t FLASH_ReadByte(uint32_t Address)
- {
-
- assert_param(IS_FLASH_ADDRESS_OK(Address));
-
-
- return(*(PointerAttr uint8_t *) (MemoryAddressCast)Address);
- }
- void FLASH_ProgramWord(uint32_t Address, uint32_t Data)
- {
-
- assert_param(IS_FLASH_ADDRESS_OK(Address));
-
-
- FLASH->CR2 |= FLASH_CR2_WPRG;
- FLASH->NCR2 &= (uint8_t)(~FLASH_NCR2_NWPRG);
-
-
- *((PointerAttr uint8_t*)(MemoryAddressCast)Address) = *((uint8_t*)(&Data));
-
- *(((PointerAttr uint8_t*)(MemoryAddressCast)Address) + 1) = *((uint8_t*)(&Data)+1);
-
- *(((PointerAttr uint8_t*)(MemoryAddressCast)Address) + 2) = *((uint8_t*)(&Data)+2);
-
- *(((PointerAttr uint8_t*)(MemoryAddressCast)Address) + 3) = *((uint8_t*)(&Data)+3);
- }
- void FLASH_ProgramOptionByte(uint16_t Address, uint8_t Data)
- {
-
- assert_param(IS_OPTION_BYTE_ADDRESS_OK(Address));
-
-
- FLASH->CR2 |= FLASH_CR2_OPT;
- FLASH->NCR2 &= (uint8_t)(~FLASH_NCR2_NOPT);
-
-
- if(Address == 0x4800)
- {
-
- *((NEAR uint8_t*)Address) = Data;
- }
- else
- {
-
- *((NEAR uint8_t*)Address) = Data;
- *((NEAR uint8_t*)((uint16_t)(Address + 1))) = (uint8_t)(~Data);
- }
- FLASH_WaitForLastOperation(FLASH_MEMTYPE_PROG);
-
-
- FLASH->CR2 &= (uint8_t)(~FLASH_CR2_OPT);
- FLASH->NCR2 |= FLASH_NCR2_NOPT;
- }
- void FLASH_EraseOptionByte(uint16_t Address)
- {
-
- assert_param(IS_OPTION_BYTE_ADDRESS_OK(Address));
-
-
- FLASH->CR2 |= FLASH_CR2_OPT;
- FLASH->NCR2 &= (uint8_t)(~FLASH_NCR2_NOPT);
-
-
- if(Address == 0x4800)
- {
-
- *((NEAR uint8_t*)Address) = FLASH_CLEAR_BYTE;
- }
- else
- {
-
- *((NEAR uint8_t*)Address) = FLASH_CLEAR_BYTE;
- *((NEAR uint8_t*)((uint16_t)(Address + (uint16_t)1 ))) = FLASH_SET_BYTE;
- }
- FLASH_WaitForLastOperation(FLASH_MEMTYPE_PROG);
-
-
- FLASH->CR2 &= (uint8_t)(~FLASH_CR2_OPT);
- FLASH->NCR2 |= FLASH_NCR2_NOPT;
- }
- uint16_t FLASH_ReadOptionByte(uint16_t Address)
- {
- uint8_t value_optbyte, value_optbyte_complement = 0;
- uint16_t res_value = 0;
-
-
- assert_param(IS_OPTION_BYTE_ADDRESS_OK(Address));
-
- value_optbyte = *((NEAR uint8_t*)Address);
- value_optbyte_complement = *(((NEAR uint8_t*)Address) + 1);
-
-
- if(Address == 0x4800)
- {
- res_value = value_optbyte;
- }
- else
- {
- if(value_optbyte == (uint8_t)(~value_optbyte_complement))
- {
- res_value = (uint16_t)((uint16_t)value_optbyte << 8);
- res_value = res_value | (uint16_t)value_optbyte_complement;
- }
- else
- {
- res_value = FLASH_OPTIONBYTE_ERROR;
- }
- }
- return(res_value);
- }
- void FLASH_SetLowPowerMode(FLASH_LPMode_TypeDef FLASH_LPMode)
- {
-
- assert_param(IS_FLASH_LOW_POWER_MODE_OK(FLASH_LPMode));
-
-
- FLASH->CR1 &= (uint8_t)(~(FLASH_CR1_HALT | FLASH_CR1_AHALT));
-
-
- FLASH->CR1 |= (uint8_t)FLASH_LPMode;
- }
- void FLASH_SetProgrammingTime(FLASH_ProgramTime_TypeDef FLASH_ProgTime)
- {
-
- assert_param(IS_FLASH_PROGRAM_TIME_OK(FLASH_ProgTime));
-
- FLASH->CR1 &= (uint8_t)(~FLASH_CR1_FIX);
- FLASH->CR1 |= (uint8_t)FLASH_ProgTime;
- }
- FLASH_LPMode_TypeDef FLASH_GetLowPowerMode(void)
- {
- return((FLASH_LPMode_TypeDef)(FLASH->CR1 & (uint8_t)(FLASH_CR1_HALT | FLASH_CR1_AHALT)));
- }
- FLASH_ProgramTime_TypeDef FLASH_GetProgrammingTime(void)
- {
- return((FLASH_ProgramTime_TypeDef)(FLASH->CR1 & FLASH_CR1_FIX));
- }
- uint32_t FLASH_GetBootSize(void)
- {
- uint32_t temp = 0;
-
-
- temp = (uint32_t)((uint32_t)FLASH->FPR * (uint32_t)512);
-
-
- if(FLASH->FPR == 0xFF)
- {
- temp += 512;
- }
-
-
- return(temp);
- }
- FlagStatus FLASH_GetFlagStatus(FLASH_Flag_TypeDef FLASH_FLAG)
- {
- FlagStatus status = RESET;
-
- assert_param(IS_FLASH_FLAGS_OK(FLASH_FLAG));
-
-
- if((FLASH->IAPSR & (uint8_t)FLASH_FLAG) != (uint8_t)RESET)
- {
- status = SET;
- }
- else
- {
- status = RESET;
- }
-
-
- return status;
- }
-
- #if defined (_COSMIC_) && defined (RAM_EXECUTION)
- #pragma section (FLASH_CODE)
- #endif
- IN_RAM(FLASH_Status_TypeDef FLASH_WaitForLastOperation(FLASH_MemType_TypeDef FLASH_MemType))
- {
- uint8_t flagstatus = 0x00;
- uint16_t timeout = OPERATION_TIMEOUT;
-
-
- #if defined (STM8S208) || defined(STM8S207) || defined(STM8S007) || defined(STM8S105) || \
- defined (STM8S005) || defined(STM8AF52Ax) || defined(STM8AF62Ax) || defined(STM8AF626x)
- if(FLASH_MemType == FLASH_MEMTYPE_PROG)
- {
- while((flagstatus == 0x00) && (timeout != 0x00))
- {
- flagstatus = (uint8_t)(FLASH->IAPSR & (uint8_t)(FLASH_IAPSR_EOP |
- FLASH_IAPSR_WR_PG_DIS));
- timeout--;
- }
- }
- else
- {
- while((flagstatus == 0x00) && (timeout != 0x00))
- {
- flagstatus = (uint8_t)(FLASH->IAPSR & (uint8_t)(FLASH_IAPSR_HVOFF |
- FLASH_IAPSR_WR_PG_DIS));
- timeout--;
- }
- }
- #else
- UNUSED(FLASH_MemType);
- while((flagstatus == 0x00) && (timeout != 0x00))
- {
- flagstatus = (uint8_t)(FLASH->IAPSR & (FLASH_IAPSR_EOP | FLASH_IAPSR_WR_PG_DIS));
- timeout--;
- }
- #endif
-
- if(timeout == 0x00 )
- {
- flagstatus = FLASH_STATUS_TIMEOUT;
- }
-
- return((FLASH_Status_TypeDef)flagstatus);
- }
- IN_RAM(void FLASH_EraseBlock(uint16_t BlockNum, FLASH_MemType_TypeDef FLASH_MemType))
- {
- uint32_t startaddress = 0;
-
- #if defined(STM8S105) || defined(STM8S005) || defined(STM8S103) || defined(STM8S003) || \
- defined(STM8S001) || defined(STM8S903) || defined (STM8AF626x) || defined (STM8AF622x)
- uint32_t PointerAttr *pwFlash;
- #elif defined (STM8S208) || defined(STM8S207) || defined(STM8S007) || defined (STM8AF62Ax) || defined (STM8AF52Ax)
- uint8_t PointerAttr *pwFlash;
- #endif
-
-
- assert_param(IS_MEMORY_TYPE_OK(FLASH_MemType));
- if(FLASH_MemType == FLASH_MEMTYPE_PROG)
- {
- assert_param(IS_FLASH_PROG_BLOCK_NUMBER_OK(BlockNum));
- startaddress = FLASH_PROG_START_PHYSICAL_ADDRESS;
- }
- else
- {
- assert_param(IS_FLASH_DATA_BLOCK_NUMBER_OK(BlockNum));
- startaddress = FLASH_DATA_START_PHYSICAL_ADDRESS;
- }
-
-
- #if defined (STM8S208) || defined(STM8S207) || defined(STM8S007) || defined (STM8AF62Ax) || defined (STM8AF52Ax)
- pwFlash = (PointerAttr uint8_t *)(MemoryAddressCast)(startaddress + ((uint32_t)BlockNum * FLASH_BLOCK_SIZE));
- #elif defined(STM8S105) || defined(STM8S005) || defined(STM8S103) || defined(STM8S003) || \
- defined(STM8S001) || defined (STM8S903) || defined (STM8AF626x) || defined (STM8AF622x)
- pwFlash = (PointerAttr uint32_t *)(MemoryAddressCast)(startaddress + ((uint32_t)BlockNum * FLASH_BLOCK_SIZE));
- #endif
-
-
- FLASH->CR2 |= FLASH_CR2_ERASE;
- FLASH->NCR2 &= (uint8_t)(~FLASH_NCR2_NERASE);
-
- #if defined(STM8S105) || defined(STM8S005) || defined(STM8S103) || defined(STM8S003) || \
- defined(STM8S001) || defined(STM8S903) || defined (STM8AF626x) || defined (STM8AF622x)
- *pwFlash = (uint32_t)0;
- #elif defined (STM8S208) || defined(STM8S207) || defined(STM8S007) || defined (STM8AF62Ax) || \
- defined (STM8AF52Ax)
- *pwFlash = (uint8_t)0;
- *(pwFlash + 1) = (uint8_t)0;
- *(pwFlash + 2) = (uint8_t)0;
- *(pwFlash + 3) = (uint8_t)0;
- #endif
- }
- 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;
-
-
- assert_param(IS_MEMORY_TYPE_OK(FLASH_MemType));
- assert_param(IS_FLASH_PROGRAM_MODE_OK(FLASH_ProgMode));
- if(FLASH_MemType == FLASH_MEMTYPE_PROG)
- {
- assert_param(IS_FLASH_PROG_BLOCK_NUMBER_OK(BlockNum));
- startaddress = FLASH_PROG_START_PHYSICAL_ADDRESS;
- }
- else
- {
- assert_param(IS_FLASH_DATA_BLOCK_NUMBER_OK(BlockNum));
- startaddress = FLASH_DATA_START_PHYSICAL_ADDRESS;
- }
-
-
- startaddress = startaddress + ((uint32_t)BlockNum * FLASH_BLOCK_SIZE);
-
-
- if(FLASH_ProgMode == FLASH_PROGRAMMODE_STANDARD)
- {
-
- FLASH->CR2 |= FLASH_CR2_PRG;
- FLASH->NCR2 &= (uint8_t)(~FLASH_NCR2_NPRG);
- }
- else
- {
-
- FLASH->CR2 |= FLASH_CR2_FPRG;
- FLASH->NCR2 &= (uint8_t)(~FLASH_NCR2_NFPRG);
- }
-
-
- for(Count = 0; Count < FLASH_BLOCK_SIZE; Count++)
- {
- *((PointerAttr uint8_t*) (MemoryAddressCast)startaddress + Count) = ((uint8_t)(Buffer[Count]));
- }
- }
- #if defined (_COSMIC_) && defined (RAM_EXECUTION)
-
- #pragma section ()
- #endif
-
|