cancel
Showing results for 
Search instead for 
Did you mean: 

While loop stucks

BG1
Senior
Posted on December 04, 2017 at 18:21

Hello ..

I am having a very weird issue with my code piece ..

The MCU is STM32F407VET6 .. What I'm trying to do is sending a string via USART1 peripheral module .. In order check if the character in the USART1 is sent out of the USART1->DR register I am continuously checking the 'Transmit Complete Bit of the usart1 status register

The code below works well ...

TC1 is defined as USART1_SR_TC register bitband alias ...

#define TC1 *((uint32_t *)(0x42220018)) //Previously defined at the top of the main file

void EXTI4_IRQHandler(void)

{

uint8_t i;

char x[6]='BURAK\n';

EXTI_PR_Bit4=1; // Cleared by writing 1

// ISR code

TC1=0;

for(i=0;i<7;i++)

{

USART1->DR=x[i];

while(!(USART1->SR&USART_SR_TC));// Transmit Complete ?

}

TC1=0;

}

BUT !

When I replace

while(!(USART1->SR&USART_SR_TC));

with

while(TC1==0);

The code stucks.. freezes ... and stops ...Why does it happen ?? Are Bit-Band Aliases not reliable things to use with while ,if , for statements ?? Does it have to be read/modify/write sequence always ??

Thanks in advance ..

#flag-check #bitband #while #stuck

Note: this post was migrated and contained many threaded conversations, some content may be missing.
23 REPLIES 23
Posted on December 07, 2017 at 14:08

How about your attitudes ?? which is completely out of topic ..

Posted on December 07, 2017 at 14:10

And someone else asking for free may not want to click or use the link ... Seriously .. I do not want to listen your suggestions .. Everything else except knowledge is not welcome ... because you are speaking to somebody who knows to thank to the contributors .. Please stay out of my topic , you are disturbing it ...

Posted on December 07, 2017 at 14:21

It ignores details, and the details are important.

Peripheral registers are not memory cells, reading and writing at the same address may act on a different register, and registers might act dynamically to the writing of specific bits, or might changing state within the ~8-cycle clock window you're fiddling with them. For example TIM->CNT += 8 doesn't advance the counter if it is clocking at the speed of the processor.

Behind all the smoke and mirrors you're not actually writing a single bit, and while programatically this might appear more efficient, frequently it isn't

Using it as a bit-band read works most of the time, provided the register doesn't differentiate between an 8, 16 or 32-bit wide read operation. ie doing an operation on CRC->DR for example.

Bit-banding action on TIM->SR is problematic because the write infers an AND operation, and when you read bits as zero that subsequently change to one this AND operation clears them, resulting in you never seeing the bits assert

Now say you can't use bit-banding, just than blind application will lead to failure and consequence. ie your three phase motor gets a spanner thrown into it.

Does writing the TC bit on USART->SR clear it? What if I try to clear the LBD or CTS bits?

Apply the use conservatively, where you understand the interaction.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on December 07, 2017 at 15:08

That really makes it crazy clear !! Thank you Clive !! It is an amazing answer .. Now the point of view at my side completely changed ... Thank you millions of times ..