Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define FLASH_CONFIG_START 0x00000000 //Napisi naslov flasha kjer se konfiguracija začne
- typedef struct {
- uint8_t config1;
- uint16_t config2;
- uint32_t config3;
- uint8_t config4;
- uint8_t config5;
- } FLASH_Struct;
- //podatki: - kaj se zapise
- //count: - koliko zapisat
- void FLASH_Write(uint8_t* podatki, uint16_t count) {
- //Koda za zapis
- //Najprej pobrišeš sektor, ki ga imaš za nastavitve, potem zapišeš notri.
- }
- //podatki: - kam se prebere
- //count: - koliko prebrat
- void FLASH_Read(uint8_t* podatki, uint16_t count) {
- //Koda za prebrat
- //Branje je enostavno, samo memory copy iz flasha v RAM kadar rabiš:
- memcpy(podatki, (uint8_t *) FLASH_CONFIG_START, count);
- }
- //Uporaba
- FLASH_Struct config;
- //Preberi nastavitve iz flasha
- FLASH_Read((uint8_t *)&config, sizeof(FLASH_Struct));
- //Zamenjaj kar imaš :D
- config.config5 = nekaj;
- //Zapiši nastavitve iz flasha, vse ponovno
- FLASH_Write((uint8_t *)&config, sizeof(FLASH_Struct));
- uint32_t TM_FLASHLOADER_ProcessPacket(uint8_t* data, uint32_t count) {
- uint32_t status;
- uint32_t blocks;
- /* Check if sectors erased */
- if (TM_FLASHLOADER_EraseSectors()) {
- /* Return error */
- return 1;
- }
- /* Clear all flash flags */
- FLASH_ClearFlag(FLAG_ALL);
- /* Status flag */
- status = 0;
- /* Get number of blocks in buffer */
- blocks = count >> 2;
- /* Write data to flash memory */
- while (blocks--) {
- /* Check if application end address has reached */
- if (FLASHLOADER.WritePointer >= FLASHLOADER.ApplicationAddress + FLASHLOADER.ApplicationProgramSize) {
- /* No memory available for program anymore */
- return 2;
- }
- /* Program flash as words */
- status += FLASH_ProgramWord(FLASHLOADER.WritePointer, *(uint32_t *)data) != FLASH_COMPLETE;
- /* Increase pointer */
- FLASHLOADER.WritePointer += 4;
- data += 4;
- }
- /* Write remaining data */
- count = count % 4;
- /* Write to flash */
- while (count--) {
- /* Check if application end address has reached */
- if (FLASHLOADER.WritePointer >= FLASHLOADER.ApplicationAddress + FLASHLOADER.ApplicationProgramSize) {
- /* No memory available for program anymore */
- return 2;
- }
- /* Program flash as bytes */
- status += FLASH_ProgramByte(FLASHLOADER.WritePointer++, *(__IO uint8_t *)data++) != FLASH_COMPLETE;
- }
- /* Everything OK = status = 0 */
- return status;
- }
Advertisement
Add Comment
Please, Sign In to add comment