Skip to main content
Magnet
Associate II
October 8, 2019
Question

How can I read and write simultaneously on specific SRAM address STM32.

  • October 8, 2019
  • 2 replies
  • 2208 views

Hi,

In the cases below I try to read value of an SRAM address which is in TIM2_IRQHandler(), from main() function or user Program() function, but no result!

anyone who is experienced or has idea?

void Program();
 
void TIM2_IRQHandler()
{ 
if(__HAL_TIM_GET_ITSTATUS(&TIM2_HandleStruct, TIM_IT_UPDATE) !=RESET)
 __HAL_TIM_CLEAR_IT(&TIM2_HandleStruct, TIM_IT_UPDATE);
 
*(__IO uint32_t *) 0x20020000 = Write1++;
 
if(Write1 == 1000)	
Write1 = 0;	
}
 
int main()
{
HAL_Init();
SystemClock_Config();
...
...
...
...
...
Program();
 
if(*(__IO uint32_t *) 0x20020000 == 10) // Does not work!!!
{ 
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_14);
}
 
for(;;)
{
if(*(__IO uint32_t *) 0x20020000 == 10) // Does not work!!!
{ 
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_14);
}
}
 
}
 
void Program()
{
if(*(__IO uint32_t *) 0x20020000 == 10) // Does not work!!!
{
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_14);
}
}
 

This topic has been closed for replies.

2 replies

Tesla DeLorean
Guru
October 8, 2019

Well two of these are dependent on the value being 10 at the given instant of the test, and the one in the loop is likely to toggle VERY fast during the whole time the value is 10. If you want it to toggle once, perhaps code for that.

I'd perhaps prefer the form:

*((volatile uint32_t *) 0x20020000)

Still a bit hacky. Anyway LOOK at the code generated by the compiler, and walk that as a sanity check on what exactly the micro is expected to do.

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
Magnet
MagnetAuthor
Associate II
October 9, 2019

Hi CliveTo.Zero,

my purpose is not toggle actually, but read the address value. I've tried to use 'volatile' that you suggest and then i observed it on debugger, but no value is read on the lines 25, 32 and 42 again. if you have more info and share would be glad.

Tesla DeLorean
Guru
October 9, 2019

What tool chain are you using?

What STM32, specifically, are you using?

Can you print the memory value?

What does the assembler code look like, it dictates what the micro will do?

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
TDK
Super User
October 10, 2019

Well, case 3 only performs the comparison once, then it enters the while(1) loop, so that won't work.

case 1 also only does the comparison once

Case 2 should work. for(;;) is an odd way to do an infinite loop, but it should be valid, assuming nothing else is actually at that memory address.

Try this loop. Debug and step through the code and see if the value variable changes.

while (1) {
 uint32_t value = *(__IO uint32_t *) 0x20020000;
 if(value == 10) {
 HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_14);
 }
}

Also set a breakpoint in your interrupt to verify it's actually changing the value.

Accessing RAM by address is an odd thing to do. It's possible the compiler is using that address to store a variable unless you're treating it differently from other RAM addresses in your linker file.

"If you feel a post has answered your question, please click ""Accept as Solution""."
Magnet
MagnetAuthor
Associate II
October 11, 2019

Hi TDK,

Case 2 does also comparison once, but no more! 

Additionally, value of "value" doesnt appear during debug in watch list.

i wonder that, either reading and writing SRAM address simultaneously is possible or not?