cancel
Showing results for 
Search instead for 
Did you mean: 

Problem With USART

bhavesh
Associate II
Posted on March 22, 2017 at 11:55

Hi,

I am using 3 USART in my code, But I faced a one ridiculous problem with them.

USART2 configured on interrupt and working fine I have No problem with this one. 

I Initialize the USART3(POL) first and send some data at this time its working fine and after I initialize USART2(INT) is also working fine and in last USART1(POL) also working fine.

Now let's come to problem.

I initialize all USART but I don't no what happen code stuck in usart GET STATUS flag function.

here is my main function.

int main(void)

{

   SystemInit();

   SysTick_Config(SystemCoreClock / 1000);

   GPIO_Init__();

   GPIO_Initialize_Conf();

   Module_switch();

   USART1_CONFIG();  //USART1 initialize for Bluetooth

   USART3_

CONFIG

();

   USART3_Send('$PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28\r\n');

   delay____(1);  // 1 ms

   GPRS_Config();  //USART2 initialize here with INT

   SENSOR_Init(); //Initialize I2C1 & I2C2

   RTC_NVIC_ALR(00:00:00, WFI, 1); //Initialize RTC

   while (1)

   {

      for(volatile int i = 1;  i <= 10;  i++)

      {

         

USART1_CONFIG

();  // I must have to re-initialize USART1 here again and again

         Resolve_ERROR();  //USART1 used in this function. also work for OTA update over Bluetooth.

                                            //USART2 also used in this function for reconnection with server and OTA update from server.

         GET_Sensor(Sensor); //Get sensor data from I2C1 & I2C2

         USART3_

CONFIG

();  // I must have to re-initialize USART3 here again and again

         GET_GPS(read_gps);

         

USART2_Send

(Data, '\n'); 

      }

      SysTick -> CTRL = 0;

      Sleep_(00:01:00); //Sleep for 1 Min.

   }

}

I want to need some proper solution which can save my controller cycle which currently totally waste by this two USART

If I remove USART1 and USART3 config function from the 'FOR' loop, code stuck in 'USART_GetFlagStatus', I cant understand whats going on. 

#usart2 #interrupt #usart #int
5 REPLIES 5
Posted on March 22, 2017 at 13:44

Problem not in code shown.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Imen.D
ST Employee
Posted on March 22, 2017 at 15:36

Hello,

Which hardware you have used ? You should e

nsure that you have correctly configure the USART.

Aslo, 

check that the clock settings for USART bus are correct.

I suggest you to refer and run one of UART example provided within ST package relevant to the device that you are using. This can help you to develop your application.

Imen

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen
Posted on March 22, 2017 at 21:07

OK, I explain in brief.

Currently you can show the USART initialization inside the for loop. 'USART1 and USART3' and both of them are already initialized out side of 'while loop'. now in every cycle of 'FOR loop' both USART will initialize again and again. which is not good for product based program.

And if I comment out this  USART1/3 from 'FOR loop',  USART2  '

USART2_Send

(Data, '\n');

function will not execute and in debugging mode if I stop the execution I found every time code stuck in 

'

USART_GetFlagStatus

' function. 

USART2 was initialized out side of 'while loop', USART2 communicate with the M95 GPRS module so I can't Initialize USART2 inside the 'while/for loop'.

I cross check with disabling sleep mode as well but problem was not solved. 

Note: I have problem with usart2 why it stuck in that function when i comment out usart1 and usart3 inside of for loop.

void

USART2_CONFIG

(void)

{

GPIO_InitTypeDef GPIO_InitStructure;

USART_InitTypeDef USART_InitStructure;

USART_DeInit(USART2);

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

GPIO_PinAFConfig(GPIOA,GPIO_PinSource2 , GPIO_AF_1);

GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;

GPIO_Init(GPIOA, &GPIO_InitStructure);

USART_InitStructure.USART_BaudRate = 9600;

USART_InitStructure.USART_WordLength = USART_WordLength_8b;

USART_InitStructure.USART_StopBits = USART_StopBits_1;

USART_InitStructure.USART_Parity = USART_Parity_No ;

USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

USART_Init(USART2, &USART_InitStructure);

USART_Cmd(USART2, ENABLE);

}

void

USART3_

CONFIG

(void)

{

GPIO_InitTypeDef GPIO_InitStructure;

USART_InitTypeDef USART_InitStructure;

USART_DeInit(USART3);

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);

RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);

GPIO_PinAFConfig(GPIOC,GPIO_PinSource10 , GPIO_AF_1);

GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_1);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

GPIO_Init(GPIOC, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;

GPIO_Init(GPIOC, &GPIO_InitStructure);

USART_InitStructure.USART_BaudRate = 9600;

USART_InitStructure.USART_WordLength = USART_WordLength_8b;

USART_InitStructure.USART_StopBits = USART_StopBits_1;

USART_InitStructure.USART_Parity = USART_Parity_No ;

USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

USART_InitStructure.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;

USART_Init(USART3, &USART_InitStructure);

USART_Cmd(USART3, ENABLE);

}

void

USART1_CONFIG

(void)

{

GPIO_InitTypeDef GPIO_InitStructure;

USART_InitTypeDef USART_InitStructure;

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

GPIO_PinAFConfig(GPIOA,GPIO_PinSource9 , GPIO_AF_1);

GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

GPIO_Init(GPIOA, &GPIO_InitStructure);

USART_InitStructure.USART_BaudRate = 9600;

USART_InitStructure.USART_WordLength = USART_WordLength_8b;

USART_InitStructure.USART_StopBits = USART_StopBits_1;

USART_InitStructure.USART_Parity = USART_Parity_No ;

USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

USART_Init(USART1, &USART_InitStructure);

USART_Cmd(USART1, ENABLE);

}

/*Usart Interrupt configuration*/

void NVIC_Config_Init(void)

{

NVIC_InitTypeDef NVIC_InitStructure;

NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPriority = 0;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStructure);

}

void NVIC_Config_Deinit(void)

{

NVIC_InitTypeDef NVIC_InitStructure;

NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPriority = 0;

NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;

NVIC_Init(&NVIC_InitStructure);

}

Thank you for your time.

Posted on March 23, 2017 at 00:56

Ok, you're still over thinking this, ideally present a minimal functioning example illustrating the problem that will compile. I'm not going to spend hours trying to replicate the scenario, I want to walk the code that executes and do a quick static analysis. It is VERY difficult to do this when key parts, unique to you, are not presented.

So you don't have the non-library routines like USART2_Send(), we also don't have the IRQ Handler and what it is doing.

You shouldn't ever need to reinitialize the USARTs in a loop like this, ever. If you have framing or parity errors, etc, you might need to clear those by reading the DR (Data Register) when they assert. So look at SR (Status Register) in these conditions. Note that looking at these register in the debugger can alter the internal state, so load them into variables you can examine instead.

Something pretty stupid is happening in the code executed within the loop for the USARTs to get totally hosed up. Focus on what it is, not pulling more stuff into the loop to remedy it. Doing that is a distraction/diversion, focus on what is actually wrong

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on March 23, 2017 at 00:58

Also what part are you using? Are you sure the USARTs are all using AF_1?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..