AnsweredAssumed Answered

USART receive string STM32f429

Question asked by gieroba.maria on Oct 15, 2015
Latest reply on Oct 16, 2015 by gieroba.maria
Hi! 
I have to receive string from USART and them interpret it and make other function(s) depends of string. I saw Clive's example but could someone tell me how can I interpret string ? Please help me :(  
#include <string.h> // memcpy()
  
//**************************************************************************************
  
#define LINEMAX 100 // Maximal allowed/expected line length
  
volatile char line_buffer[LINEMAX + 1]; // Holding buffer with space for terminating NUL
volatile int line_valid = 0;
  
//**************************************************************************************
  
void USART1_IRQHandler(void)
{
  static char rx_buffer[LINEMAX];   // Local holding buffer to build line
  static int rx_index = 0;
  
  if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) // Received character?
  {
    char rx =  USART_ReceiveData(USART1);
  
    if ((rx == '\r') || (rx == '\n')) // Is this an end-of-line condition, either will suffice?
    {
        if (rx_index != 0) // Line has some content?
        {
                memcpy((void *)line_buffer, rx_buffer, rx_index); // Copy to static line buffer from dynamic receive buffer
                line_buffer[rx_index] = 0; // Add terminating NUL
                line_valid = 1; // flag new line valid for processing
  
            rx_index = 0; // Reset content pointer
        }
    }
    else
    {
        if (rx_index == LINEMAX) // If overflows pull back to start
            rx_index = 0;
  
        rx_buffer[rx_index++] = rx; // Copy to buffer and increment
    }
  }
}
  
//**************************************************************************************
  
int main(void)
{
  USART_InitTypeDef USART_InitStructure;
  GPIO_InitTypeDef GPIO_InitStructure;
  NVIC_InitTypeDef NVIC_InitStructure;
  
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);   // Enable GPIO A bank clock
  
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); // Enable USART1 clock
  
  /* Configure Alternate Function pin muxing fabric to escape USART1 Rx and Tx */
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);
  
  /* Configure USART1 pins:  Rx and Tx */
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_9 | GPIO_Pin_10;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  
  /* Enable USART1 IRQ */
  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
  
  /* Configure USART1 settings */
  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); // Enable USART1
  
  USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // Enable USART1 Receive Interrupt
  
  while(1) // Don't want to exit
  {
    __WFI(); // Wait for an interrupt rather than grind endlessly
  
        if (line_valid) // A new line has arrived
        {
            // ProcessLine(line_buffer); // Do something with the line
  
            line_valid = 0; // clear pending flag
        }
  }
}

Outcomes