Thread: software for reading uart interface mcu - code understanding

  1. #1
    Registered User
    Join Date
    Feb 2016
    Posts
    2

    software for reading uart interface mcu - code understanding

    Hi,

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

    Code:
        /**
         * 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.

    Code:
    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;
        }
    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

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    >> * Perform a single character read from the UART interface.
    >> * This is a blocking synchronous call

    These are strange comments, since the 'A' in UART stands for "asynchronous". Ports that support both synchronous and asynchronous are usually referred to as USART.

    I would of thought that `ti_uart_read()` reads the character from the uart interface, hence why is there a need for `getcharacter()`?
    This appears to be a helper function, though I don't really see the value of it, apart from possibly readability. All it does is call "ti_uart_read()" and returns the character read if successful (and -1 otherwise). The return value still has to be checked, so instead of "getcharacter()", you could just call it yourself:

    Code:
    if(ti_uart_read(ti_UART_0, &c) == ti_RC_OK)
        // read was successful, 'c' contains a valid character
    Also why is it necessary to pass `&c` to the function? What is its function?
    'c' is the "Data to read from UART" - this is where the byte that is read will be stored. You pass its address (using the address-of operator '&') so that the function can update its value in the caller.

  3. #3
    Registered User
    Join Date
    Feb 2016
    Posts
    2
    Many thanks Matticus.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading data from SPI Interface
    By doppler75 in forum C Programming
    Replies: 2
    Last Post: 08-06-2009, 06:37 PM
  2. How do i interface a C code in GTK+ ?
    By spatika in forum C Programming
    Replies: 1
    Last Post: 06-15-2009, 07:56 AM
  3. Improving Code Interface
    By helloamuro in forum C Programming
    Replies: 20
    Last Post: 05-02-2008, 04:34 AM
  4. Reading from UDP socket on specific interface
    By uriel in forum Networking/Device Communication
    Replies: 2
    Last Post: 11-29-2007, 11:09 PM
  5. Help disabling network interface from code
    By maure5 in forum C Programming
    Replies: 3
    Last Post: 03-30-2005, 08:28 AM

Tags for this Thread