Elektroda.pl
Elektroda.pl
X

Search our partners

Find the latest content on electronic components. Datasheets.com
Please add exception to AdBlock for elektroda.pl.
If you watch the ads, you support portal and users.

AT91sam7 zapis danych do flash

narp 27 Sep 2007 14:38 1625 1
  • #1
    narp
    Level 11  
    Witam.

    Mam problem z zapisaniem danych we Flash`u. Podczas pracy krokowej zapis jest poprawny a kiedy uruchomie procesor w normalnym trybie to wiesza się na zapisie flash.

    Code:


    //Inicjalizacja flash
    void InitFlash(void)
    {
      AT91C_BASE_MC->MC_FMR = (((0x48)<<16) | AT91C_MC_FWS_2FWS);     // konfiguracja MC_FMR
    }




    //Funkcja zapisu do flash
     void at91flashWriteDW(int sektor, int nr_DW, unsigned long wartDW )
     {
     
       while(!(AT91C_BASE_MC->MC_FSR & AT91C_MC_FRDY));
       
       AT91C_IFLASH_MEM->MEM_PAGE_0[nr_DW] = wartDW;                             //wybór komórki DW
     
       AT91C_BASE_MC->MC_FCR = sektor <<8 | (0x5A<<24) | AT91C_MC_FCMD_START_PROG;

       while(!(AT91C_BASE_MC->MC_FSR & AT91C_MC_FRDY));
     }


    W czym jest problem?[/code]

    Dodano po 4 [godziny] 28 [minuty]:

    Chyba problem jest w tym że trzeba tą funkcję uruchomić z ramu tylko jak?
  • #2
    adamusx
    Level 27  
    Przy zapisie do flasha trzeba spelnic dwa warunki:
    1 - funkcja musi byc w RAMie jak sam napisales
    2 - musza byc wylaczone przerwania na czas zapisu


    ja korzystam z tego i dziala :)
    Code:


    #define __RAMFUNC __attribute__ ((long_call, section (".fastrun")))


    __ramfunc int AT91F_Flash_Write( unsigned int Flash_Address ,int size ,unsigned int * buff)
    {
        // set the Flash controller base address
        AT91PS_MC ptMC = AT91C_BASE_MC;
        unsigned int i, page, status;
        unsigned int * Flash;
       
        // init flash pointer
        Flash = (unsigned int *) Flash_Address;
        // Get the Flash page number
        page = ((Flash_Address - (unsigned int)AT91C_IFLASH ) /FLASH_PAGE_SIZE_BYTE);
        // copy the new value
       
        for (i=0; (i < FLASH_PAGE_SIZE_BYTE) & (size > 0) ;i++, Flash++,buff++,size-=4 ){
        *Flash=*buff;
        }
       
        IntDisable();
        unsigned long ulIntrStat = AT91C_BASE_AIC->AIC_IMR;
        AT91C_BASE_AIC->AIC_IDCR = 0xFFFFFFFF;
       
        // Write the write page command
        ptMC->MC_FCR = AT91C_MC_CORRECT_KEY | AT91C_MC_FCMD_START_PROG | (AT91C_MC_PAGEN & (page <<8)) ;
       
        //for a certain ammount of time you cant do any operation over flash memory.
        for(i=0;i<100000;i++) asm("nop");
       
        //Enable ints
        AT91C_BASE_AIC->AIC_IECR = ulIntrStat;
       
        // Wait the end of command
        status = AT91F_Flash_Ready();
       
        IntEnable();
        // Check the result
        if ( (status & ( AT91C_MC_PROGE | AT91C_MC_LOCKE ))!=0)
        return false;
        return true;
    }


    //*----------------------------------------------------------------------------
    //* \fn    AT91F_Flash_Ready
    //* \brief Czekanie na zapis Flash'a
    //*----------------------------------------------------------------------------
    __ramfunc int AT91F_Flash_Ready (void)
    {
        unsigned int status;
        status = 0;

        // Wait the end of command
            while ((status & AT91C_MC_FRDY) != AT91C_MC_FRDY )
            {
              status = AT91C_BASE_MC->MC_FSR;
            }
            return status;
    }