Skip to main content
RKuma.12.300
Associate
Aug 28, 2019 12:05 PM UTC
Question

STM32G0 READ AND WRITE ISSUE

Hello,

I am using STM32G070CBT6 Controller.

I am trying to write and read in 63rd page of flash memory .

HAL_FLASH_Unlock();

 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP  |

             FLASH_FLAG_WRPERR |

FLASH_FLAG_PGAERR);

 FLASH_PageErase(63);

 HAL_StatusTypeDef Stat1 =HAL_FLASH_Program(TYPEPROGRAM_DOUBLEWORD, 0x0801F800 , 0xAB); // 0xAB is dummy write

 HAL_FLASH_Lock();

 HAL_Delay(10);

 const volatile uint8_t *userConfig=(const volatile uint8_t *)0x0801F800;

 HAL_UART_Transmit(&huart1,(uint8_t *)&userConfig,1,20);

When i program this i am receiving 0x00 in UART.

Please guide.

This topic has been closed for replies.

13 replies

FredB
Associate
Sep 12, 2019 5:37 AM UTC

rohit,

Before you worry about what comes out of the USART, you need to make sure all the parts of your code work first. You have three things you're doing in your code, but it sounds like you have no idea if all three are working. The three things you're doing are:

1: Writing a value to FLASH

2: Reading the value you wrote from FLASH

3: Sending that value over the USART

The first thing to do is check if your USART is setup and working properly. Comment out all the code your showed in your post code and replace it with something simple like:

uint8_t ValueToSend = 0xAB;

HAL_UART_Transmit(&huart1, &ValueToSend, 1, 200);

Note that I also opened up the timeout value as you don't want that to be an issue. When you run this, your should see the hex value 0xAB appear using whatever code or device you are using to test with. Personally, I'dd just use a regular ASCII character and a serial terminal to verify this as it's probably easier to make sure the test works. Something like this:

uint8_t ValueToSend = 'A';

HAL_UART_Transmit(&huart1, &ValueToSend, 1, 200);

You'd expect to see the letter "A" appear in your serial terminal. Don't assume your USART HAL call is working. Test it and verify it.

Next, comment out the USART code and just try your FLASH write and read code. Set a breakpoint at this line in your code:

 const volatile uint8_t *userConfig=(const volatile uint8_t *)0x0801F800;

Single step through the line and see what value appears in "userConfig". Hopefully you know how to use a debugger and set breakpoints. If you don't know how to, then go do some Google searching and learn how. Learning proper use of a debugger will make your life 1000% easier.

Once you try the two tests I suggested, you'll know if the problem lies in your UASRT code or your FLASH read/write code and you can start troubleshooting from there.

Also, your line to to read from the FLASH address is like a keyword buffet. Instead of trying to do everything in one line, break it up a bit. Also, since you don't know what is or isn't working, don't do all the elaborate casting. Do this instead:

uint8_t* userConfig;

userConfig = (uint8_t *)0x0801F800;

HAL_UART_Transmit(&huart1, userConfig, 1, 200);

Much simpler to read and understand what's going on. Also much easier to not make a mistake writing. If you get this working, then you start adding back your "const volatile" and see if things still work. That's assuming you even need those, which I don't think you do.

Remember, the key to debugging is breaking things down to simple pieces, testing those simple pieces, and then adding together the pieces that work until you get the whole thing working. And using a debugger (and setting breakpoints) will make is all simple.

RKuma.12.300
Associate
Sep 16, 2019 11:04 AM UTC

I checked the memory contents in debug mode but at address 0x0801F800  nothing has updated. .

 

HAL_FLASH_Unlock();          // Unlock Flash to write

 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGSERR);

 FLASH_PageErase(63);   // Erase 63rd Page

HAL_FLASH_Program(TYPEPROGRAM_DOUBLEWORD, 0x0801F800 , 1234); // Write 1234 in 0x0801F800  address 

 HAL_FLASH_Lock();         // Lock Flash

SLA
Associate
Sep 24, 2019 3:18 PM UTC

​Hi rohit,

I have the same issue with the STM32G071. Do you have any new knowledgements? 

RKuma.12.300
Associate
Sep 25, 2019 3:33 AM UTC

Not yet. If you come across the solution please update in the forum

SLA
Associate
Sep 25, 2019 11:37 AM UTC

this seems to work..

flash_write(0x0801F060, 0x66AA);

flash_page_erase(62);

void flash_page_erase(uint32_t page)

{

    HAL_StatusTypeDef test = HAL_FLASH_Unlock();

    test = FLASH_WaitForLastOperation(1000); //1s timeout

    __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR);

    FLASH_PageErase(page);

    test = FLASH_WaitForLastOperation(1000);

    HAL_FLASH_Lock();

}

void flash_write(uint32_t address, uint32_t data)

{

    HAL_StatusTypeDef test = HAL_FLASH_Unlock();

    HAL_StatusTypeDef Stat1 = HAL_FLASH_Program(TYPEPROGRAM_DOUBLEWORD, address , (uint64_t)data);

    HAL_FLASH_Lock();

}

*************************************************************************************************

..this makes problems..

HAL_FLASH_Program(TYPEPROGRAM_DOUBLEWORD, 0x0801F800 , 1234); // Write 1234 in 0x0801F800  address 

..and

FLASH_PageErase(63);   // Erase 63rd Page

HAL_FLASH_Program(...

RKuma.12.300
Associate
Sep 25, 2019 12:20 PM UTC

Yes. This is working.

But if i try first erasing page and writing flash it does not work.

flash_page_erase(62);

flash_write(0x0801F060, 0x66AA);

Please help.

SLA
Associate
Sep 25, 2019 12:56 PM UTC

Clearing the FLASH_CR_PER seems to help.

I hope the ST profis can tell us something more about this issue?!

void flash_page_erase(uint32_t page)

{

HAL_StatusTypeDef test = HAL_FLASH_Unlock();

test = FLASH_WaitForLastOperation(1000); //1s timeout

       __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR);

      FLASH_PageErase(page);

        test = FLASH_WaitForLastOperation(1000);

       CLEAR_BIT(FLASH->CR, FLASH_CR_PER);

       HAL_FLASH_Lock();

}

SLA
Associate
Sep 26, 2019 2:05 PM UTC

For all how want to do a small EEPROM Emulation with the STM32G0. It use only one page of Flash. Please note that minimum datalength to store in flash is 64bit. Because of this I use 32bit for a virtual EEaddress and 32bit for EEdata.

Use:

init_EEArray();

write_EEdata2Flash(EEPROM_CHECKSUM,0x12345678); //write to flash

while(1)

{

 test_ee = EE_Array[EEPROM_CHECKSUM]; //read from array

}

Attention the code is new and barely tested and will be shared under no warranty!

SLA
Associate
Sep 26, 2019 2:06 PM UTC

.c ​code

Danny1
Visitor II
Jan 9, 2020 12:07 PM UTC

SLA, Thanks!

My first steps with eeprom emulation.

What is ?

EE_ADR_MAX

Is this correct?

#define EE_ADR_MAX					((FEE_PAGE_SIZE/4)-1) //???

You don´t declare EE_ADR_MAX in your example.

Thanks!