简体   繁体   中英

USART timeout interrupt for STM32F4 Discovery

My STM and Raspberry do successfully comunicate via USART. Rx and Tx word fine, until the Raspberry stops to send. Then the STM listens and listens for new chars. The code cycle is stuck until a char is received.
I want to implement a timeout interrupt.

This is what I found. STM32H7 documentation (page 21)
Unfortunatelly it's for the wrong board. The receive timeout cannot be found in the reference manual for the STMF4. So I guess this functionality is not given for my board?

I am using Keil µVision V5.28.0.0 and standard peripherals.

Here is a code snippet

#include "main.h"
#include <stdio.h>

USART_InitTypeDef USART_InitStruct;

volatile uint32_t msTicks=0;

void USART_Initialise(void);
int USART_putchar(char ch);     
void USART_puts(char str[]);
char USART_receive(void);

void Delay (int ms);

int main(void)
{
    char buffer[20];
    string test = "test";

    USART_Initialise();

 while (1)
 {
    char mode = USART_receive();

     if (mode == '+')
     {
         sprintf(buffer, "%.2lf",test);
         USART_puts(buffer);
     }
     Delay(300);    
 }//while
}//main

void USART_Initialise(void)
{
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
    RCC_APB1PeriphClockCmd (RCC_APB1Periph_USART3, ENABLE);

    GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_USART3);
    GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_USART3);

    GPIO_StructInit(&GPIO_InitStruct);
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;   //GPIO Pin 10 & 11
    GPIO_Init(GPIOC, &GPIO_InitStruct);

    USART_StructInit(&USART_InitStruct);
    USART_InitStruct.USART_BaudRate=9600;
    USART_InitStruct.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
    USART_InitStruct.USART_Mode=USART_Mode_Rx | USART_Mode_Tx;
    USART_InitStruct.USART_Parity=USART_Parity_No;
    USART_InitStruct.USART_StopBits=USART_StopBits_1;
    USART_InitStruct.USART_WordLength=USART_WordLength_8b;
    USART_Init(USART3, &USART_InitStruct);  
    USART_Cmd(USART3, ENABLE);
}

int USART_putchar(char ch)
{
    USART_SendData(USART3, (uint8_t) ch);
    while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET) 
  {}
  return ch;
}

void USART_puts(char str[])
{
    while (*str != 0)
    {
        USART_putchar(*str);
        str++;
    }
}

char USART_receive(void)
{
    while (USART_GetFlagStatus(USART3, USART_FLAG_RXNE) == RESET){};
    char rxd_receive = USART_ReceiveData(USART3);
    return rxd_receive;
}

void SysTick_Handler(void)   //für ISR kein Prototyp erforderlich
{
  msTicks--;
}

void Delay (int ms)
{
  msTicks = ms; 
  while (msTicks>0); 
}

Change your receive function into:

int USART_receive(void)
{
    if (USART_GetFlagStatus(USART3, USART_FLAG_RXNE) == RESET)
    {
        return EOF;
    }
    return USART_ReceiveData(USART3);
}

Then you can decide what to do on receiving a character in the upper layer.

You might also put the timeout in the function. Then it will return EOF if the timeout happens. Since you say the UART does not include an own timeout functionality, you need to use one of the timers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM