# How to check overrun?

I'm using an STM32F103C8T6 (aka blue pill) with Eclipse/System Workbench and HAL.

The following code:

 volatile HAL_StatusTypeDef result =
HAL_UART_Transmit(&huart1, bytesToSend + currentSendIndex,
bytesPerMessageToSend, 100);

volatile uint32_t error = HAL_UART_GetError(&huart1);


works fine when sending 1 byte, and (later) receiving 1 byte and iterating this.

However, when sending 3 bytes, I noticed, I only receive the first byte, and the next calls for HAL_UART_Receive result in a timeout.

This seems logical, because probably there is only 1 byte buffer in the UART.

However, what I don't understand, why does transmitting multiple bytes in the code mentioned, returns HAL_OK, and also the call to HAL_UART_GetError returns 0.

I assume an overrun has occurred for the 2nd and consecutive bytes... is this true, and if yes, how can I check this?

• Aren't you using TXempty for IRQ? What data rate? Sep 3, 2017 at 22:06
• Using a timer is possible to fill 8 byte data buffer but IRQ is better. Before STM32 which have many std UARTS, I would use IRQ with DMA to send bursts of data, so CPU could run processes, and do housekeeping and delgate the DMA. Make sure you use parity, and check for all errors, NE ORE or FE and test signal integrity for glitches. Sep 4, 2017 at 12:20
• For MIDI , I once just used the Joystick port to communicate over MIDI on WIN98 Sep 4, 2017 at 13:24
• @Michel Keijzers if you want to learn microcontrollers forget HAL and use bare registers. Sep 4, 2017 at 15:46
• I bet that learning those ridiciolous HAL structures will be much more complicated and will take longer (now you have problem with very simple peripheral) , than learning the hardware itself. I cant help you as I know how the hardware works, but I have no clues what HAL does). I use some HAL libraries (strongly modified by me) especially for USB & Ethernet. Sep 4, 2017 at 17:05

You can configure an interrupt to detect overrun errors. Actually a lot of interrupt are available to detect errors. From the reference manual:

Just for sure you could use the "Transmit data register empty" one as Tony has suggested.

As far as I know HAL IRQ handlers and IRQ calls for UART use all of these interrupts by default. The _IT versions of the UART calls enable all these interrupts.

• Thanks ... I already knew interrupts would be better, but since I'm quite a newbie I thought it would be easier to start with the non-interrupt solution... But I guess I already found it's limitation. I will check interrupts, and yes, the HAL supports interrupts for UART. Sep 4, 2017 at 7:53

Your solution may end up duplicating existing products.

http://www.ucapps.de/mbhp_core_lpc17.html

• I know such things exist, but first, I want to learn more about microcontrollers, but also I want eventually (at least) 4 inputs, 4 outputs and 4 thrus. Probably I could stack 4 of those. Sep 4, 2017 at 14:22
• Before you can design, you must know how everything works and fails, and learning curve depends on user. So start with something that works. Sep 4, 2017 at 14:28
• Well I tried already a 'proof of concept' with 3 MIDI in/out/thrus on an Arduino and worked fine... but I need more memory, and possibly more processing power, so I am converting my proof of concept to STM32 ... Sep 4, 2017 at 14:31
• then you must use DMA for thruput in order to have useful functions. Sep 4, 2017 at 14:35
• Yes, but since I'm new I thought I start simple with polling, but that doesn't seem to work out well, so I will do IRQ/interrupts, and next step is using DMA. Sep 4, 2017 at 15:11