cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 USART PROBLEM

ran2
Associate III
Posted on August 09, 2013 at 20:18

Hello, everyone.

 I am currently playing around with the USART without standard peripheral lib provided by ST (using CMSIS only) and my code strangely works in one MDK project and does not work in another MDK project (Same code!) 

So if you are interested, please have a look at these:

int main(void)

{

    USART2->BRR=0x330;

    

    RCC->AHB1ENR|=RCC_AHB1ENR_GPIOAEN;

    RCC->APB1ENR|=RCC_APB1ENR_USART2EN;

 

    GPIOA->MODER|=(

                                GPIO_MODER_MODER2_1|

                                GPIO_MODER_MODER4_1);

        GPIOA->PUPDR|=(

                                GPIO_PUPDR_PUPDR2_0|

                                GPIO_PUPDR_PUPDR4_0);

    GPIOA->AFR[0]|=0x00077777;

    

    USART2->CR1|=USART_CR1_TE;

    USART2->CR1|=USART_CR1_UE;

    USART2->SR&=~(USART_SR_TC);

    USART2->DR=0x0000;

    

}

Any comment or idea will be appreciated!

#stm32f4 #usart
4 REPLIES 4
Posted on August 12, 2013 at 00:02

Make sure to enable clocks prior to writing to peripherals (BRR), and mask bits on to configuration registers in a manner that is immune to initial conditions.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
ran2
Associate III
Posted on August 12, 2013 at 10:16

Cheers, Clive!!

I am trying your suggestions. And when I put them in a project with Peripheral Standard library, though not using any of the library functions, the above code worked!

So I am really confused.

ran2
Associate III
Posted on August 12, 2013 at 10:25

Especially when it contains the following:

If I comment them, the code will not work anymore. However, I see no way I am using these codes in the main file. The main files are exactly the same.

#pragma import(__use_no_semihosting)                                     

 _sys_exit(int x)

 {

     x = x;

 }

ran2
Associate III
Posted on August 12, 2013 at 11:14

Well, it is confirmed that  this piece of code is crucial:

#if 1

#pragma import(__use_no_semihosting)                              

       

 _sys_exit(int x)

 {

     x = x;

 }

#endif

I have to put it somewhere else in another .c file instead of the main.c file.

Really confusing what that is for and how it works.