coffeelover February 2016

reading uart software on mcu - code understanding

Below is code in a uart driver header file for performing a non blocking read on the uart interface.

/**
 * Perform a single character read from the UART interface.
 * This is a blocking synchronous call.
 *
 * @brief UART character data read.
 * @param [in] uart UART index.
 * @param [out] data Data to read from UART.
 * @return ti_uart_status_t Returns UART specific return code.
 */
 ti_uart_status_t ti_uart_read(const ti_uart_t uart, uint8_t *data);

Below is example code that uses the above function to get the character from the uart interface. I'm trying to understand it.

I would of thought that ti_uart_read() reads the character from the uart interface, hence why is there a need for getcharacter()?

Also why is it necessary to pass &c to the function? What is its function?

Many thanks

int getcharacter(void) {
uint8_t c = 0;
int ret;
  ret = ti_uart_read(ti_UART_0, &c);
  if (ti_RC_OK == ret) {
    return ((int) c);
  }
  return -1;
}

Answers


Weather Vane February 2016

The function getcharacter is a wrapper for the the UART function ti_uart_read, that handles the operation's status as well as getting the byte. ti_uart_read supplies two items of data: the status of the operation, and the value read. To acheive that the status was returned as the function value, and the data was set by a pointer. The reason it is done that way round, is that smoother and more idiomatic program logic can be achieved if you are checking a function's return value. Some library functions manage this all in one; fgetc is one example. getcharacter works in a similar way: it returns -1 (data you didn't expect) in the case of an error.

Post Status

Asked in February 2016
Viewed 3,271 times
Voted 11
Answered 1 times

Search




Leave an answer