/**
 * \internal
 * \brief Helper function to read a byte from an arbitrary interface
 *
 * This function is used to hide what interface is used by the driver, e.g.
 * the driver does not need to know if USART in SPI mode is used or the native
 * SPI module.
 */
__always_inline static uint8_t hx8347a_read_byte(void)
{
	uint8_t data;
#if defined(CONF_HX8347A_USART_SPI)
#  if UC3

	/* A workaround for optimizing data transfer had to be done for the
	 * XMEGA in order for the display to work correctly at all SPI clock
	 * clock speeds */

	/* This function could also be used for XMEGA but results in a very slow
	 * framerate, hence a workaround has been implemented for the XMEGA */
	usart_spi_read_packet(CONF_HX8347A_USART_SPI, &data, 1);

#  elif XMEGA
	usart_spi_read_single(CONF_HX8347A_USART_SPI, &data);

#  endif
#elif defined(CONF_HX8347A_SPI)
	spi_write_single(CONF_HX8347A_SPI, 0xFF);

	/* Wait for RX to complete */
	while (!spi_is_rx_full(CONF_HX8347A_SPI)) {
		/* Do nothing */
	}

	spi_read_single(CONF_HX8347A_SPI, &data);
#endif
	return data;
}
/**
 * \internal
 * \brief Helper function to read a byte from an arbitrary interface
 *
 * This function is used to hide what interface is used by the component
 * driver, e.g.  the component driver does not need to know if USART in SPI
 * mode is used or the native SPI module.
 *
 * \retval uint8_t Byte of data read from the display controller
 */
__always_inline static uint8_t ili9341_read_byte(void)
{
	uint8_t data;

#if defined(CONF_ILI9341_USART_SPI)
#  if XMEGA
	/* Workaround for clearing the RXCIF for XMEGA */
	usart_rx_enable(CONF_ILI9341_USART_SPI);

	usart_put(CONF_ILI9341_USART_SPI, 0xFF);
	while (!usart_rx_is_complete(CONF_ILI9341_USART_SPI)) {
		/* Do nothing */
	}
	data = usart_get(CONF_ILI9341_USART_SPI);

	/* Workaround for clearing the RXCIF for XMEGA */
	usart_rx_disable(CONF_ILI9341_USART_SPI);
#  else
	usart_spi_read_single(CONF_ILI9341_USART_SPI, &data);
#  endif
#elif defined(CONF_ILI9341_SPI)
	spi_write_single(CONF_ILI9341_SPI, 0xFF);

	ili9341_wait_for_send_done();

	/* Wait for RX to complete */
	while (!spi_is_rx_full(CONF_ILI9341_SPI)) {
		/* Do nothing */
	}

	spi_read_single(CONF_ILI9341_SPI, &data);
#endif

	return data;
}