The point concerning the HAL GPIO, is confirmed. Otherwise for USART or other communication IPs, we provide within the HAL drivers some APIs that receive an amount of data in Polling, Interrupt and DMA modes, where we already fix the parameter Size: Amount of data to be received. You've just to customize the available APIs for your personal usage, since the provided APIs are generic for general use. You can refer to this [DEAD LINK /public/STe2ecommunities/mcu/Lists/STM32Java/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Java/Using%20STM32FCubeMX%20HAL%20Drivers%20for%20UART%20RX%20with%20unknown%20size%20data&currentviews=324]post, that my shed some light on what are you looking for.