cancel
Showing results for 
Search instead for 
Did you mean: 

Are these Memory segmentation Problems even possible?

PPULA.1
Associate II

Hi, I am new to using 32 bit microcontrollers and have been using 8 bit controllers previously. In my 8 bit controllers I had several uint8_t and uint16_t variables.

Does a 32 bit microcontroller access 4 bytes of data in memory at once?

If yes, can there be memory accessing issues when data organized together is manipulated in an interrupt? Say for example,

There are two variables a and b of uint8_t type stored in consecutive memory locations 0x20000001 and 0x2000002 and variable 'a' is volatile . Say the execution of code follows the below order.

  1. Some code in main loop wants to change 'b' from 0 to 15.
  2. Code is executing line1, it would first have to read the 32bit memory location since the location contains other variables too (like 'a'). (next step would have been writing the new value on the 32 bit location.
  3. But the interrupt occurred before it could do a write to variable 'b'. Inside the interrupt line2 will update the variable a to 10.
  4. After exiting the interrupt the write that was pending (to complete code execution in line 1) occurs. This would restore value of 'a' to 11 even though it was changed by the interrupt.

I think that memory access should not be in 32 bits to avoid the above issue or there should be some way of doing "b=15" as an atomic operation. Please help.

Any links to materials regarding above would be much appreciated.

Thanks in advance

volatile int a=11;//mem_loc= 0x2000001
int b=0;                //mem_loc= 0x2000002
int main(void){
 
while(1){
b=15;       //line1
 
}
}
 
void USART2_IRQHandler(void){
a=10;       //line2
}

1 REPLY 1
KnarfB
Principal III

There are different machine instructions accessing 8-, 16- and 32-bit data (LDR, LDRB and LDRH, STR, STRB and STRH). See PM0215 "STM32F0xxx Cortex-M0 programming manual". and releated.

Moreover, int is 32-bit and your above variable declarations will cause the 32-bit variables to be placed in 32-bit aligned adresses. So int a might occupy 0x2000000..3, b 0x2000004..7 and so on.

Josph Yiu's "Definitve guide..." books are a good read and highly recommended. Start with Cortex-M0 its the simplest. Somehow a subset of all others.

Also the Insider's Guide STM32 - Hitex gives a good overview.

hth

KnarfB