123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612 |
- #include "iflash.h"
- enum {RESET = 0, SET = !RESET};
- #define __IO volatile
-
- #define ADDR_FLASH_SECTOR_0 ((uint32_t)0x08000000)
- #define ADDR_FLASH_SECTOR_1 ((uint32_t)0x08004000)
- #define ADDR_FLASH_SECTOR_2 ((uint32_t)0x08008000)
- #define ADDR_FLASH_SECTOR_3 ((uint32_t)0x0800C000)
- #define ADDR_FLASH_SECTOR_4 ((uint32_t)0x08010000)
- #define ADDR_FLASH_SECTOR_5 ((uint32_t)0x08020000)
- #define ADDR_FLASH_SECTOR_6 ((uint32_t)0x08040000)
- #define ADDR_FLASH_SECTOR_7 ((uint32_t)0x08060000)
- #define ADDR_FLASH_SECTOR_8 ((uint32_t)0x08080000)
- #define ADDR_FLASH_SECTOR_9 ((uint32_t)0x080A0000)
- #define ADDR_FLASH_SECTOR_10 ((uint32_t)0x080C0000)
- #define ADDR_FLASH_SECTOR_11 ((uint32_t)0x080E0000)
- #define ADDR_FLASH_SECTOR_12 ((uint32_t)0x08100000)
- #define ADDR_FLASH_SECTOR_13 ((uint32_t)0x08104000)
- #define ADDR_FLASH_SECTOR_14 ((uint32_t)0x08108000)
- #define ADDR_FLASH_SECTOR_15 ((uint32_t)0x0810C000)
- #define ADDR_FLASH_SECTOR_16 ((uint32_t)0x08110000)
- #define ADDR_FLASH_SECTOR_17 ((uint32_t)0x08120000)
- #define ADDR_FLASH_SECTOR_18 ((uint32_t)0x08140000)
- #define ADDR_FLASH_SECTOR_19 ((uint32_t)0x08160000)
- #define ADDR_FLASH_SECTOR_20 ((uint32_t)0x08180000)
- #define ADDR_FLASH_SECTOR_21 ((uint32_t)0x081A0000)
- #define ADDR_FLASH_SECTOR_22 ((uint32_t)0x081C0000)
- #define ADDR_FLASH_SECTOR_23 ((uint32_t)0x081E0000)
- #define SECTOR_MASK ((uint32_t)0xFFFFFF07)
-
- #define FLASH_FLAG_EOP ((uint32_t)0x00000001)
- #define FLASH_FLAG_OPERR ((uint32_t)0x00000002)
- #define FLASH_FLAG_WRPERR ((uint32_t)0x00000010)
- #define FLASH_FLAG_PGAERR ((uint32_t)0x00000020)
- #define FLASH_FLAG_PGPERR ((uint32_t)0x00000040)
- #define FLASH_FLAG_PGSERR ((uint32_t)0x00000080)
- #define FLASH_FLAG_RDERR ((uint32_t)0x00000100)
- #define FLASH_FLAG_BSY ((uint32_t)0x00010000)
- #define FLASH_Sector_0 ((uint16_t)0x0000)
- #define FLASH_Sector_1 ((uint16_t)0x0008)
- #define FLASH_Sector_2 ((uint16_t)0x0010)
- #define FLASH_Sector_3 ((uint16_t)0x0018)
- #define FLASH_Sector_4 ((uint16_t)0x0020)
- #define FLASH_Sector_5 ((uint16_t)0x0028)
- #define FLASH_Sector_6 ((uint16_t)0x0030)
- #define FLASH_Sector_7 ((uint16_t)0x0038)
- #define FLASH_Sector_8 ((uint16_t)0x0040)
- #define FLASH_Sector_9 ((uint16_t)0x0048)
- #define FLASH_Sector_10 ((uint16_t)0x0050)
- #define FLASH_Sector_11 ((uint16_t)0x0058)
- #define FLASH_Sector_12 ((uint16_t)0x0080)
- #define FLASH_Sector_13 ((uint16_t)0x0088)
- #define FLASH_Sector_14 ((uint16_t)0x0090)
- #define FLASH_Sector_15 ((uint16_t)0x0098)
- #define FLASH_Sector_16 ((uint16_t)0x00A0)
- #define FLASH_Sector_17 ((uint16_t)0x00A8)
- #define FLASH_Sector_18 ((uint16_t)0x00B0)
- #define FLASH_Sector_19 ((uint16_t)0x00B8)
- #define FLASH_Sector_20 ((uint16_t)0x00C0)
- #define FLASH_Sector_21 ((uint16_t)0x00C8)
- #define FLASH_Sector_22 ((uint16_t)0x00D0)
- #define FLASH_Sector_23 ((uint16_t)0x00D8)
-
- typedef enum
- {
- FLASH_BUSY = 1,
- FLASH_ERROR_RD,
- FLASH_ERROR_PGS,
- FLASH_ERROR_PGP,
- FLASH_ERROR_PGA,
- FLASH_ERROR_WRP,
- FLASH_ERROR_PROGRAM,
- FLASH_ERROR_OPERATION,
- FLASH_COMPLETE
- }FLASH_Status;
-
- #define VoltageRange_1 ((uint8_t)0x00)
- #define VoltageRange_2 ((uint8_t)0x01)
- #define VoltageRange_3 ((uint8_t)0x02)
- #define VoltageRange_4 ((uint8_t)0x03)
- #define FLASH_PSIZE_BYTE ((uint32_t)0x00000000)
- #define FLASH_PSIZE_HALF_WORD ((uint32_t)0x00000100)
- #define FLASH_PSIZE_WORD ((uint32_t)0x00000200)
- #define FLASH_PSIZE_DOUBLE_WORD ((uint32_t)0x00000300)
- #define CR_PSIZE_MASK ((uint32_t)0xFFFFFCFF)
- #define PERIPH_BASE ((uint32_t)0x40000000)
- #define AHB1PERIPH_BASE (PERIPH_BASE + 0x00020000)
- #define FLASH_R_BASE (AHB1PERIPH_BASE + 0x3C00)
-
- #define FLASH_KEY1 ((uint32_t)0x45670123)
- #define FLASH_KEY2 ((uint32_t)0xCDEF89AB)
- typedef struct
- {
- __IO uint32_t ACR;
- __IO uint32_t KEYR;
- __IO uint32_t OPTKEYR;
- __IO uint32_t SR;
- __IO uint32_t CR;
- __IO uint32_t OPTCR;
- __IO uint32_t OPTCR1;
- } FLASH_TypeDef;
- #define FLASH ((FLASH_TypeDef *) FLASH_R_BASE)
- #define FLASH_CR_PG ((uint32_t)0x00000001)
- #define FLASH_CR_SER ((uint32_t)0x00000002)
- #define FLASH_CR_STRT ((uint32_t)0x00010000)
- #define FLASH_CR_LOCK ((uint32_t)0x80000000)
- static uint32_t GetSector(uint32_t Address);
- void FLASH_Unlock(void);
- FLASH_Status FLASH_EraseSector(uint32_t FLASH_Sector, uint8_t VoltageRange);
- FLASH_Status FLASH_WaitForLastOperation(void);
- FLASH_Status FLASH_GetStatus(void);
- FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data);
- FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data);
- FLASH_Status FLASH_ProgramByte(uint32_t Address, uint8_t Data);
- void FLASH_Lock(void);
- #define DATA_32 ((uint32_t)0x13246578)
- #define BUFLENGTH 32
- uint32_t wrbuf[BUFLENGTH] = {0xabcdef12};
- #define FLASH_USER_START_ADDR ADDR_FLASH_SECTOR_7
- #define FLASH_USER_END_ADDR ADDR_FLASH_SECTOR_8
-
-
-
-
-
-
- int WriteLicensetoFlash(uint8_t *license )
- {
- int index=0;
- uint32_t eraseSector = 0;
- uint32_t address = ADDR_FLASH_SECTOR_6;
- uint32_t *ptr = (uint32_t*)license;
- FLASH_Unlock();
-
- *((volatile uint32_t *)0x40023c0C) = (((uint32_t)0x00000001) | ((uint32_t)0x00000002) | ((uint32_t)0x00000010) | \
- ((uint32_t)0x00000020) | ((uint32_t)0x00000040) | ((uint32_t)0x00000080));
-
- eraseSector = GetSector(ADDR_FLASH_SECTOR_6);
- if(FLASH_EraseSector(eraseSector, VoltageRange_3) != FLASH_COMPLETE)
- {
- printf("Erase Flash sector 7 failed!\n");
- FLASH_Lock();
- return -1;
- }
- for(index=0;index<32;index++)
- {
- FLASH_ProgramWord(address, (uint32_t)0x11223344);
- address += 4;
- ptr++;
- }
- FLASH_Lock();
- }
- static uint32_t GetSector(uint32_t Address)
- {
- uint32_t sector = 0;
-
- if((Address < ADDR_FLASH_SECTOR_1) && (Address >= ADDR_FLASH_SECTOR_0))
- {
- sector = FLASH_Sector_0;
- }
- else if((Address < ADDR_FLASH_SECTOR_2) && (Address >= ADDR_FLASH_SECTOR_1))
- {
- sector = FLASH_Sector_1;
- }
- else if((Address < ADDR_FLASH_SECTOR_3) && (Address >= ADDR_FLASH_SECTOR_2))
- {
- sector = FLASH_Sector_2;
- }
- else if((Address < ADDR_FLASH_SECTOR_4) && (Address >= ADDR_FLASH_SECTOR_3))
- {
- sector = FLASH_Sector_3;
- }
- else if((Address < ADDR_FLASH_SECTOR_5) && (Address >= ADDR_FLASH_SECTOR_4))
- {
- sector = FLASH_Sector_4;
- }
- else if((Address < ADDR_FLASH_SECTOR_6) && (Address >= ADDR_FLASH_SECTOR_5))
- {
- sector = FLASH_Sector_5;
- }
- else if((Address < ADDR_FLASH_SECTOR_7) && (Address >= ADDR_FLASH_SECTOR_6))
- {
- sector = FLASH_Sector_6;
- }
- else if((Address < ADDR_FLASH_SECTOR_8) && (Address >= ADDR_FLASH_SECTOR_7))
- {
- sector = FLASH_Sector_7;
- }
- else if((Address < ADDR_FLASH_SECTOR_9) && (Address >= ADDR_FLASH_SECTOR_8))
- {
- sector = FLASH_Sector_8;
- }
- else if((Address < ADDR_FLASH_SECTOR_10) && (Address >= ADDR_FLASH_SECTOR_9))
- {
- sector = FLASH_Sector_9;
- }
- else if((Address < ADDR_FLASH_SECTOR_11) && (Address >= ADDR_FLASH_SECTOR_10))
- {
- sector = FLASH_Sector_10;
- }
-
- else if((Address < ADDR_FLASH_SECTOR_12) && (Address >= ADDR_FLASH_SECTOR_11))
- {
- sector = FLASH_Sector_11;
- }
- else if((Address < ADDR_FLASH_SECTOR_13) && (Address >= ADDR_FLASH_SECTOR_12))
- {
- sector = FLASH_Sector_12;
- }
- else if((Address < ADDR_FLASH_SECTOR_14) && (Address >= ADDR_FLASH_SECTOR_13))
- {
- sector = FLASH_Sector_13;
- }
- else if((Address < ADDR_FLASH_SECTOR_15) && (Address >= ADDR_FLASH_SECTOR_14))
- {
- sector = FLASH_Sector_14;
- }
- else if((Address < ADDR_FLASH_SECTOR_16) && (Address >= ADDR_FLASH_SECTOR_15))
- {
- sector = FLASH_Sector_15;
- }
- else if((Address < ADDR_FLASH_SECTOR_17) && (Address >= ADDR_FLASH_SECTOR_16))
- {
- sector = FLASH_Sector_16;
- }
- else if((Address < ADDR_FLASH_SECTOR_18) && (Address >= ADDR_FLASH_SECTOR_17))
- {
- sector = FLASH_Sector_17;
- }
- else if((Address < ADDR_FLASH_SECTOR_19) && (Address >= ADDR_FLASH_SECTOR_18))
- {
- sector = FLASH_Sector_18;
- }
- else if((Address < ADDR_FLASH_SECTOR_20) && (Address >= ADDR_FLASH_SECTOR_19))
- {
- sector = FLASH_Sector_19;
- }
- else if((Address < ADDR_FLASH_SECTOR_21) && (Address >= ADDR_FLASH_SECTOR_20))
- {
- sector = FLASH_Sector_20;
- }
- else if((Address < ADDR_FLASH_SECTOR_22) && (Address >= ADDR_FLASH_SECTOR_21))
- {
- sector = FLASH_Sector_21;
- }
- else if((Address < ADDR_FLASH_SECTOR_23) && (Address >= ADDR_FLASH_SECTOR_22))
- {
- sector = FLASH_Sector_22;
- }
- else
- {
- sector = FLASH_Sector_23;
- }
- return sector;
- }
- void FLASH_Unlock(void)
- {
- if((FLASH->CR & FLASH_CR_LOCK) != RESET)
- {
-
- FLASH->KEYR = FLASH_KEY1;
- FLASH->KEYR = FLASH_KEY2;
- }
- }
- FLASH_Status FLASH_EraseSector(uint32_t FLASH_Sector, uint8_t VoltageRange)
- {
- uint32_t tmp_psize = 0x0;
- FLASH_Status status = FLASH_COMPLETE;
-
- if(VoltageRange == VoltageRange_1)
- {
- tmp_psize = FLASH_PSIZE_BYTE;
- }
- else if(VoltageRange == VoltageRange_2)
- {
- tmp_psize = FLASH_PSIZE_HALF_WORD;
- }
- else if(VoltageRange == VoltageRange_3)
- {
- tmp_psize = FLASH_PSIZE_WORD;
- }
- else
- {
- tmp_psize = FLASH_PSIZE_DOUBLE_WORD;
- }
-
- status = FLASH_WaitForLastOperation();
-
- if(status == FLASH_COMPLETE)
- {
-
- FLASH->CR &= CR_PSIZE_MASK;
- FLASH->CR |= tmp_psize;
- FLASH->CR &= SECTOR_MASK;
- FLASH->CR |= FLASH_CR_SER | FLASH_Sector;
- FLASH->CR |= FLASH_CR_STRT;
-
-
- status = FLASH_WaitForLastOperation();
-
-
- FLASH->CR &= (~FLASH_CR_SER);
- FLASH->CR &= SECTOR_MASK;
- }
-
- return status;
- }
- FLASH_Status FLASH_WaitForLastOperation(void)
- {
- __IO FLASH_Status status = FLASH_COMPLETE;
-
-
- status = FLASH_GetStatus();
-
- while(status == FLASH_BUSY)
- {
- status = FLASH_GetStatus();
- }
-
- return status;
- }
- FLASH_Status FLASH_GetStatus(void)
- {
- FLASH_Status flashstatus = FLASH_COMPLETE;
-
- if((FLASH->SR & FLASH_FLAG_BSY) == FLASH_FLAG_BSY)
- {
- flashstatus = FLASH_BUSY;
- }
- else
- {
- if((FLASH->SR & FLASH_FLAG_WRPERR) != (uint32_t)0x00)
- {
- flashstatus = FLASH_ERROR_WRP;
- }
- else
- {
- if((FLASH->SR & FLASH_FLAG_RDERR) != (uint32_t)0x00)
- {
- flashstatus = FLASH_ERROR_RD;
- }
- else
- {
- if((FLASH->SR & (uint32_t)0xE0) != (uint32_t)0x00)
- {
- flashstatus = FLASH_ERROR_PROGRAM;
- }
- else
- {
- if((FLASH->SR & FLASH_FLAG_OPERR) != (uint32_t)0x00)
- {
- flashstatus = FLASH_ERROR_OPERATION;
- }
- else
- {
- flashstatus = FLASH_COMPLETE;
- }
- }
- }
- }
- }
-
- return flashstatus;
- }
- FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data)
- {
-
-
-
-
-
-
- FLASH->CR &= CR_PSIZE_MASK;
- FLASH->CR |= FLASH_PSIZE_WORD;
- FLASH->CR |= FLASH_CR_PG;
-
- *(__IO uint32_t*)Address = Data;
-
-
-
-
-
-
-
- return 0;
- }
- FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data)
- {
- FLASH_Status status = FLASH_COMPLETE;
-
-
-
- status = FLASH_WaitForLastOperation();
-
- if(status == FLASH_COMPLETE)
- {
-
- FLASH->CR &= CR_PSIZE_MASK;
- FLASH->CR |= FLASH_PSIZE_HALF_WORD;
- FLASH->CR |= FLASH_CR_PG;
-
- *(__IO uint16_t*)Address = Data;
-
-
- status = FLASH_WaitForLastOperation();
-
- FLASH->CR &= (~FLASH_CR_PG);
- }
-
- return status;
- }
- FLASH_Status FLASH_ProgramByte(uint32_t Address, uint8_t Data)
- {
- FLASH_Status status = FLASH_COMPLETE;
-
-
-
- status = FLASH_WaitForLastOperation();
-
- if(status == FLASH_COMPLETE)
- {
-
- FLASH->CR &= CR_PSIZE_MASK;
- FLASH->CR |= FLASH_PSIZE_BYTE;
- FLASH->CR |= FLASH_CR_PG;
-
- *(__IO uint8_t*)Address = Data;
-
-
- status = FLASH_WaitForLastOperation();
-
- FLASH->CR &= (~FLASH_CR_PG);
- }
-
- return status;
- }
- void FLASH_Lock(void)
- {
-
- FLASH->CR |= FLASH_CR_LOCK;
- }
|