cancel
Showing results for 
Search instead for 
Did you mean: 

Polling sample code for USART

bhamidian
Associate II
Posted on August 14, 2008 at 02:10

Polling sample code for USART

2 REPLIES 2
bhamidian
Associate II
Posted on May 17, 2011 at 12:41

Dear Experts,

Since I am new to this MCU, please accept my apologies if my question is very basic.

I am currently trying to run the example codes of ''STM32F10x Firmware Library'' on ''STM3210B-EVAL'' evaluation board. The code sample that I have problem with is ''Polling'' sample for USART. I successfully compiled and loaded the code to the target. but surprisingly it dosen't work.This is the fragment of the code that I found the source of problem in it:

while(TxCounter < TxBufferSize)

{

/* Send one byte from USART1 to USART2 */

USART_SendData(USART1, TxBuffer[TxCounter++]);

/* Loop until the end of transmit */ /*THIS WILL BECAME INFINTE LOOP*/

while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET )

{

}

/* Loop until the USART2 Receive Data Register is not empty */

while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET)

{

}

/* Store the received byte in RxBuffer */

RxBuffer[RxCounter++] = (USART_ReceiveData(USART2) & 0x7F);

}

After trying to debug it, I found out that USART1 dose not transmit the data when data is written to it by USART_SendData function. and USART_GetFlagStatus(USART1, USART_FLAG_TC) always returns 1, so the code gets stuck in the first while loop infinitely.

I have verified that the data gets in the data register of the USART1 but I wonder why the USART dosent transmit it. Since it is an example code from STMicroelectronices I am really shocked that it dosen't work.

I even tried the another USART example code (HyperTerminal_HwFlowControl), but I faced the same problem with it.

I paste the whole main file of this example code here:

int main(void)

{

#ifdef DEBUG

debug();

#endif

/* System Clocks Configuration */

RCC_Configuration();

/* NVIC configuration */

NVIC_Configuration();

/* Configure the GPIO ports */

GPIO_Configuration();

/* USART1 and USART2 configuration ------------------------------------------------------*/

/* USART and USART2 configured as follow:

- BaudRate = 230400 baud

- Word Length = 8 Bits

- One Stop Bit

- Even parity

- Hardware flow control disabled (RTS and CTS signals)

- Receive and transmit enabled

*/

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_Even;

USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

/* Configure USART1 */

USART_Init(USART1, &USART_InitStructure);

/* Configure USART2 */

USART_Init(USART2, &USART_InitStructure);

/* Enable the USART1 */

USART_Cmd(USART1, ENABLE);

/* Enable the USART2 */

USART_Cmd(USART2, ENABLE);

while(TxCounter < TxBufferSize)

{

/* Send one byte from USART1 to USART2 */

USART_SendData(USART1, TxBuffer[TxCounter++]);

/* Loop until the end of transmit */

while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET )

{

}

/* Loop until the USART2 Receive Data Register is not empty */

while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET)

{

}

/* Store the received byte in RxBuffer */

RxBuffer[RxCounter++] = (USART_ReceiveData(USART2) & 0x7F);

}

/* Check the received data with the send ones */

TransferStatus = Buffercmp(TxBuffer, RxBuffer, TxBufferSize);

/* TransferStatus = PASSED, if the data transmitted from USART1 and

received by USART2 are the same */

/* TransferStatus = FAILED, if the data transmitted from USART1 and

received by USART2 are different */

while (1)

{

}

}

thanks in advance,

Behrooz

jj
Associate II
Posted on May 17, 2011 at 12:41

Greetings/Welcome-

(don't know if ''expert'' applies but will offer logic...)

You appear to have included the proper code - getting it to compile and load is also positive. Don't know your toolchain - some do NOT automatically ''flash'' the code to your micro. (In IAR you must tick a small box w/in options) Check this first.

I get best results by starting very simply - and proceeding with small, discrete steps. Suggest that you ''prove'' that your code IS getting into the STM by loading the simple ''blinky'' program. This will prove that you, your toolchain and your micro + board all are working together.

There is a detailed ''readme'' file included with most STM examples - always read these. Yours states that a ''Null Modem'' fem-fem cable is required to connect CN5-CN6. Is your cable proper/present?

How do you know that USART1 is NOT transmitting? Have you scoped or otherwise monitored this output?

If your hardware is ok - why not simplify the code by temporarily replacing the while (TxCounter < TxBufferSize) with a simpler 2-3 byte cascaded transmission. See if this gets the USART to emit. To insure that this works - comment out the while (USART_GetFlagStatus) function.

Once you get a few bytes to transmit you should be able to systematically reconstruct your code - and isolate exactly where/why you're hanging.

Good luck...