cancel
Showing results for 
Search instead for 
Did you mean: 

read continuous data through UART

dixon
Associate II
Posted on November 02, 2016 at 10:52

I am a beginner, I have the Uart dma example code. How can i change the program to receive the continuous data obtained from the TX in RX  in a simple way. Here is my main fucntion

&sharpinclude ''main.h''

&sharpinclude ''stm32f4xx_hal.h''

&sharpinclude ''dma.h''

&sharpinclude ''usart.h''

&sharpinclude ''gpio.h''

/* USER CODE BEGIN Includes */

&sharpdefine TXSTARTMESSAGESIZE               (COUNTOF(aTxStartMessage) - 1)

&sharpdefine TXENDMESSAGESIZE                 (COUNTOF(aTxEndMessage) - 1)

/* Size of Reception buffer */

&sharpdefine RXBUFFERSIZE                     10

/* Exported macro ------------------------------------------------------------*/

&sharpdefine COUNTOF(__BUFFER__)   (sizeof(__BUFFER__) / sizeof(*(__BUFFER__)))

/* USER CODE END Includes */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* Private variables ---------------------------------------------------------*/

/* USER CODE END PV */

/** @addtogroup UART_Hyperterminal_DMA

  * @{

  */ 

/* Private typedef -----------------------------------------------------------*/

/* Private define ------------------------------------------------------------*/

/* Private macro -------------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/

/* UART handler declaration */

/* Buffer used for transmission */

uint8_t aTxStartMessage[] = ''\n\r ****UART-Hyperterminal communication based on DMA ****\n\r Enter 10 characters using keyboard :\n\r'';

uint8_t aTxEndMessage[] = ''\n\r Example Finished\n\r'';

/* Buffer used for reception */

uint8_t aRxBuffer[RXBUFFERSIZE];

/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

void Error_Handler(void);

/* USER CODE BEGIN PFP */

/* Private function prototypes -----------------------------------------------*/

/* USER CODE END PFP */

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

int main(void)

{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration----------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */

  HAL_Init();

  /* Configure the system clock */

  SystemClock_Config();

  /* Initialize all configured peripherals */

  MX_GPIO_Init();

  MX_DMA_Init();

  MX_UART8_Init();

HAL_UART_MspInit(&huart8);

  /* USER CODE BEGIN 2 */

/*♯♯-2- Start the transmission process ♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯*/  

  /* User start transmission data through ''TxBuffer'' buffer */

  if(HAL_UART_Transmit_DMA(&huart8, (uint8_t*)aTxStartMessage, TXSTARTMESSAGESIZE)!= HAL_OK)

  {

    /* Transfer error in transmission process */

    Error_Handler();  

  }

  

  /*♯♯-3- Put UART peripheral in reception process ♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯*/  

  /* Any data received will be stored in ''RxBuffer'' buffer : the number max of 

     data received is 10 */

  if(HAL_UART_Receive_DMA(&huart8, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK)

  {

    /* Transfer error in reception process */

    Error_Handler();     

  }

  /*♯♯-4- Wait for the end of the transfer ♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯*/  

  /*  Before starting a new communication transfer, you need to check the current    

      state of the peripheral; if it�s busy you need to wait for the end of current

      transfer before starting a new one.

      For simplicity reasons, this example is just waiting till the end of the 

      transfer, but application may perform other tasks while transfer operation

      is ongoing. */  

  while (HAL_UART_GetState(&huart8) != HAL_UART_STATE_READY)

  {

  } 

 

  /*♯♯-5- Send the received Buffer ♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯*/  

  if(HAL_UART_Transmit_DMA(&huart8, (uint8_t*)aRxBuffer, RXBUFFERSIZE)!= HAL_OK)

  {

    /* Transfer error in transmission process */

    Error_Handler();    

  }

  

  /*♯♯-6- Wait for the end of the transfer ♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯*/  

  while (HAL_UART_GetState(&huart8) != HAL_UART_STATE_READY)

  {

  }

 

  /*♯♯-7- Send the End Message ♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯*/  

  if(HAL_UART_Transmit_DMA(&huart8, (uint8_t*)aTxEndMessage, TXENDMESSAGESIZE)!= HAL_OK)

  {

    /* Turn LED3 on: Transfer error in transmission process */

   // BSP_LED_On(LED3);

    while(1)

    {

    }      

  }

  

  /*♯♯-8- Wait for the end of the transfer ♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯*/  

  while (HAL_UART_GetState(&huart8) != HAL_UART_STATE_READY)

  {

  }

  /* Infinite loop */  

  while (1)

  {

  }

}

  /* USER CODE END 2 */

  /* Infinite loop */

  /* USER CODE BEGIN WHILE */

 

This is configured for the MCU by taking UART8. Help me its urgent 

#no-hablo-hal
4 REPLIES 4
Amel NASRI
ST Employee
Posted on November 15, 2016 at 11:44

Hi sunny.dixon_p,

What are you trying to do exactly?

What is going wrong with the code you provided?

-Mayla-

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

AvaTar
Lead
Posted on November 15, 2016 at 14:58

Why are yo polling, and not using interrupts ?

They are designed as immediate responses to external events.

> Help me its urgent

Do you honestly expect someone else to take the heat for you ????

Posted on November 15, 2016 at 16:02

What is going wrong with the code you provided?

The OP expressed the desire for it to support reception of data also.

http://www.catb.org/~esr/faqs/smart-questions.html#urgent

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Alan Chambers
Associate II
Posted on November 15, 2016 at 17:04

My USART DMA driver does the following:

For TX:

- Maintains a queue of pending DMA transfers (of variable length).

- I cache the actual data in a TX ring buffer (a little care is need when this wraps - split into

- two transfers).

- On TC interrupt fetch the next transfer off the queue, set MOAR and NDTR, and re-enable the stream.

For RX:

- I use a circular buffer for the DMA.

- Don't interrupt (I might wait forever for enough data to cause the interrupt).

- Poll frequently (SysTick at 1kHz) and test NDTR to see if any new data has been received.

- Copy new data into an RX ring buffer (watch the wrapping).

- Emit an event to my scheduler to indicate to whoever cares that there is new data to be consumed.

 

No doubt there are better solutions. But it seems pretty robust.

I use a scheduler to decouple subsystems, but a synchronous callback would do just as well as an asynchronous event.