Are these Memory segmentation Problems even possible?
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.
- Some code in main loop wants to change 'b' from 0 to 15.
- 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.
- But the interrupt occurred before it could do a write to variable 'b'. Inside the interrupt line2 will update the variable a to 10.
- 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
}