STM32L1 and FLASH_Write_Word

General discussion on mikroC PRO for ARM.
Post Reply
Author
Message
orpheedulogis
Posts: 240
Joined: 16 Jan 2010 22:26

STM32L1 and FLASH_Write_Word

#1 Post by orpheedulogis » 14 Mar 2023 11:13

Hi,

BIG problem using FLASH_Write_Word on an STM32L151RDT6.

This code works correctly (I can see AABBCCDD at all adresses)

Code: Select all

#pragma orgall 0x200 

//------------------------------------------------------------------------------
void FLASH_EraseWritePage(unsigned long address)
{
    char x1,y1=0;
    unsigned int x2=0,y2;
    unsigned long dataToWrite,x4,y4,z4;

    Flash_Unlock();

    FLASH_ErasePage(address);


    for(x2=0;x2<0x800;x2=x2+4)
    {
        FLASH_Write_Word(x2+address, 0xAABBCCDD);
        p_WDC=~p_WDC;
    }

    Flash_Lock();

}
void main() 
{
    InitSetUp();
    FLASH_EraseWritePage([b][u]0x3000[/u][/b])
}
But here the problem: code is the same except "orgall" (and of course flash destination) has changed.
In the following code , I can see it write only 255 bytes at destination address

Code: Select all

#pragma orgall 0x2A000

//------------------------------------------------------------------------------
void FLASH_EraseWritePage(unsigned long address)
{
    char x1,y1=0;
    unsigned int x2=0,y2;
    unsigned long dataToWrite,x4,y4,z4;

    Flash_Unlock();

    FLASH_ErasePage(address);


    for(x2=0;x2<0x800;x2=x2+4)
    {
        FLASH_Write_Word(x2+address, 0xAABBCCDD);
        p_WDC=~p_WDC;
    }

    Flash_Lock();

}
void main() 
{
    InitSetUp();
    FLASH_EraseWritePage(0x800)
}
What can I do for this ? thanks for help.

AntiMember
Posts: 136
Joined: 02 Jan 2020 19:00

Re: STM32L1 and FLASH_Write_Word

#2 Post by AntiMember » 14 Mar 2023 19:06

What if FLASH_EraseWritePage(0x1000) ?

orpheedulogis
Posts: 240
Joined: 16 Jan 2010 22:26

Re: STM32L1 and FLASH_Write_Word

#3 Post by orpheedulogis » 15 Mar 2023 07:00

I didn't tried 0x1000. I tried 0x800 (multiple of 2048) because I was thinkg flash_erase_page would erase 2048 bytes.
As I didn't find why it didn't work, I decided to put the (future) "bootloader" before flash program area. This work but it would be more usable to have program area before bootloader.

AntiMember
Posts: 136
Joined: 02 Jan 2020 19:00

Re: STM32L1 and FLASH_Write_Word

#4 Post by AntiMember » 15 Mar 2023 08:53

Table 8. NVM module organization (Cat.1 and Cat.2 devices)
Block Name Memory addresses Size
Program memory
Sector 0
Page 0 0x0800 0000 - 0x0800 00FF 256 bytes
Page 1 0x0800 0100 - 0x0800 01FF 256 bytes
Page 2 0x0800 0200 - 0x0800 02FF 256 bytes
Page 3 0x0800 0300 - 0x0800 03FF 256 bytes
Page 4 to 7 0x0800 0400 - 0x0800 07FF 1 Kbyte
Page 8 to 11 0x0800 0800 - 0x0800 0BFF 1 Kbyte
Page 12 to 15 0x0800 0C00 - 0x0800 0FFF 1 Kbyte
Sector 1 0x0800 1000 - 0x0800 1FFF 4 Kbytes
Sector 2 0x0800 2000 - 0x0800 2FFF 4 Kbytes
Sector 3 0x0800 3000 - 0x0800 3FFF 4 Kbytes
...
...
...
Sector 30 0x0801 E000 - 0x0801 EFFF 4 Kbytes
Sector 31 0x0801 F000 - 0x0801 FFFF 4 Kbytes
Data EEPROM 0x0808 0000 - 0x0808 0FFF 4096 bytes

orpheedulogis
Posts: 240
Joined: 16 Jan 2010 22:26

Re: STM32L1 and FLASH_Write_Word

#5 Post by orpheedulogis » 15 Mar 2023 10:03

I tried this

Code: Select all

//------------------------------------------------------------------------------
#pragma orgall                0x2A000                
#define BOOTLOADER_START_ADDR 0x2A000
#define INIT_PROGRAM_ADRESS   0x2E000   

//------------------------------------------------------------------------------

void FLASH_EraseWritePage(unsigned long address)
{
    char x1=1,y1;
    unsigned int x2=0,y2;
    unsigned long dataToWrite,x4,y4=address+2048,z4;
    char txt[16];

    Flash_Unlock();
    switch(address)
    {
        case 0x0000:
        FLASH_ErasePage(0x0000); // page 0
        FLASH_ErasePage(0x0100); // page 1
        FLASH_ErasePage(0x0200); // page 2
        FLASH_ErasePage(0x0300); // page 3
        FLASH_ErasePage(0x0400); // page 4...7
        break;

        case 0x0800:
        FLASH_ErasePage(0x0800); // page 8...11
        FLASH_ErasePage(0x0C00); // page 12...15
        break;
        
        default:
        FLASH_ErasePage(address);
        break;        

    }


    x2=0;
    for (x4=address; x4<y4; x4=x4+4)
    {
        if (y1)dataToWrite=0x11223344; else dataToWrite=0x99887766;
        if(y1)y1=0; else y1=1;
        FLASH_Write_Word(x4, dataToWrite);
    }

    Flash_Lock();
    

}

//------------------------------------------------------------------------------

void main() org BOOTLOADER_START_ADDR
{
    FLASH_EraseWritePage(0x800);
    WHILE1LEDXX
}
    

    
But here is the result: partial writing


Image

If I try 0x1000, same problem

Image

orpheedulogis
Posts: 240
Joined: 16 Jan 2010 22:26

Re: STM32L1 and FLASH_Write_Word

#6 Post by orpheedulogis » 15 Mar 2023 10:55

OK.
I found the problem: with this microcontroller , FLASH_ErasePage will erase only 0x100 lenght area.
But there is always a problem
I did this

Code: Select all

void FLASH_EraseWritePage(unsigned long address)
{
    char x1=1,y1;
    unsigned int x2=0,y2;
    unsigned long dataToWrite,x4,y4=address+2048,z4;
    char txt[16];

    Flash_Unlock();

    for(x4=address;x4<y4;x4=x4+0x100)
    {
        FLASH_ErasePage(x4);
    }

    for (x4=address; x4<y4; x4=x4+4)
    {
        if (y1)dataToWrite=0x11223344; else dataToWrite=0x99887766;
        if(y1)y1=0; else y1=1;
        FLASH_Write_Word(x4, dataToWrite);
    }

    Flash_Lock();
}
This give me a good writing functionnality ... but uncomplete too !

Image

orpheedulogis
Posts: 240
Joined: 16 Jan 2010 22:26

Re: STM32L1 and FLASH_Write_Word

#7 Post by orpheedulogis » 15 Mar 2023 10:59

In fact it works adding a delay

void FLASH_EraseWritePage(unsigned long address)
{
char x1=1,y1;
unsigned int x2=0,y2;
unsigned long dataToWrite,x4,y4=address+2048,z4;
char txt[16];

Flash_Unlock();

for(x4=address;x4<y4;x4=x4+0x100)
{
FLASH_ErasePage(x4);
}

for (x4=address; x4<y4; x4=x4+4)
{
if (y1)dataToWrite=0x11223344; else dataToWrite=0x99887766;
if(y1)y1=0; else y1=1;
FLASH_Write_Word(x4, dataToWrite);

}
delay_us(1);
Flash_Lock();
}
Last edited by orpheedulogis on 15 Mar 2023 12:23, edited 1 time in total.

orpheedulogis
Posts: 240
Joined: 16 Jan 2010 22:26

Re: STM32L1 and FLASH_Write_Word

#8 Post by orpheedulogis » 15 Mar 2023 11:52

The problem now is that erasing the flash will be very long
Is there a solution to accelerate this ? (is'nt it possible to erase complete bank ?)

AntiMember
Posts: 136
Joined: 02 Jan 2020 19:00

Re: STM32L1 and FLASH_Write_Word

#9 Post by AntiMember » 15 Mar 2023 18:08

FLASH_EraseSector

orpheedulogis
Posts: 240
Joined: 16 Jan 2010 22:26

Re: STM32L1 and FLASH_Write_Word

#10 Post by orpheedulogis » 16 Mar 2023 13:46

Thanks antimember

But there is no "FLASH_EraseSector" instruction for STM32L151 (->error)
Perhaps there is a method using registers...

AntiMember
Posts: 136
Joined: 02 Jan 2020 19:00

Re: STM32L1 and FLASH_Write_Word

#11 Post by AntiMember » 16 Mar 2023 16:35

Unfortunately there is really no sector erasure... :(
There is only one way out - to use large pages...

Post Reply

Return to “mikroC PRO for ARM General”