Skip to main content
mosine
Associate III
July 31, 2013
Question

USART and Hyperterminal

  • July 31, 2013
  • 6 replies
  • 916 views
Posted on July 31, 2013 at 11:55

Hi,

I create on my stm3220G evaluation board an example with USART. The code:

#include <
stm32f2xx.h
>
#include <
misc.h
> // I recommend you have a look at these in the ST firmware folder
#include <
stm32f2xx_usart.h
> // under Libraries/STM32F4xx_StdPeriph_Driver/inc and src
#include ''stm32f2xx_gpio.h''
volatile char StringLoop[] = ''The quick brown fox jumps over the lazy dog\r\n'';
//**************************************************************************************
int main(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); 
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
/* USART1 Rx */ 
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; 
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; 
GPIO_Init(GPIOA, &GPIO_InitStructure); 
/* USART1 Tx */ 
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; 
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Enable USART1 IRQ */
NVIC_ClearPendingIRQ(USART1_IRQn);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_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_ITConfig(USART1, USART_IT_TXE, ENABLE);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1,ENABLE);
while(1); // Don't want to exit
}
//**************************************************************************************
void USART1_IRQHandler(void)
{
static int tx_index = 0;
static int rx_index = 0;
if (USART_GetITStatus(USART1, USART_IT_TXE) != RESET) // Transmit the string in a loop
{
USART_SendData(USART1, StringLoop[tx_index++]);
if (tx_index >= (sizeof(StringLoop) - 1))
tx_index = 0;
}
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) // Received characters modify string
{
StringLoop[rx_index++] = USART_ReceiveData(USART1);
if (rx_index >= (sizeof(StringLoop) - 1))
rx_index = 0;
}
}

When I use hyperterminal, I don't see anything. why?? I set hyperterminal with the same properties of USART. THANKS
    This topic has been closed for replies.

    6 replies

    Tesla DeLorean
    Guru
    July 31, 2013
    Posted on July 31, 2013 at 14:00

    I create on my stm3220G evaluation board an example with USART. When I use hyperterminal, I don't see anything. why?? I set hyperterminal with the same properties of USART.

    Probably because the board uses USART3 on PC10/11?
    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    mosine
    mosineAuthor
    Associate III
    July 31, 2013
    Posted on July 31, 2013 at 14:25

    THANKS clive1. I changed and it works. GPIOC9 RX e GPIOC10 TX. 

    Tesla DeLorean
    Guru
    July 31, 2013
    Posted on July 31, 2013 at 14:34

    THANKS clive1. I changed and it works. GPIOC9 RX e GPIOC10 TX. 

    USART3_RX is PC11

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    mosine
    mosineAuthor
    Associate III
    July 31, 2013
    Posted on July 31, 2013 at 14:59

    I was going to write. But I don't receive anything. Is there a way to send packet to board's usart (for example: length of packet)?

    mosine
    mosineAuthor
    Associate III
    July 31, 2013
    Posted on July 31, 2013 at 16:40

    I resolve it, but I don't understand. I changed GPIOC11 fromGPIO_Mode_IN toGPIO_Mode_AF and IRQ receive:

    from if (USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) to 
    if (USART_GetFlagStatus(USART3, USART_FLAG_RXNE) != RESET)

    what's the difference between

    USART_GetITStatus and

    USART_GetFlagStatus?

    Tesla DeLorean
    Guru
    July 31, 2013
    Posted on July 31, 2013 at 16:55

    Yes, for F2/F4 designs the peripheral pins need to be in AF Mode.

    If you want to send/receive multiple bytes you'll need to create some buffering, my preference is for ring/fifo buffers handled under interrupt.

    The USART has flags in in status register reflecting the state of internal registers and buffers, there is a secondary set of flags related to interrupt enabling, and status. These are covered in the Reference Manual for the parts.
    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..