Guidelines for Developing a Nios II HAL Device Driver

ID 683146
Date 6/12/2015
Public
Document Table of Contents

1.4. The BitBangUartTransmit() Function

This section examines the BitBangUartTransmit() function in bit_bang_uart.c. The BitBangUartTransmit() function demonstrates transmission of characters over the UART.

In the Nios II SBT for Eclipse, step over the BitBangUartTransmit() function. The characters BIT BANH appear in Tera Term, as shown in the "Stepping Over the BitBangUartTransmit() Function" figure below. The following steps show why the string appears as it does.

Figure 12. Stepping Over the BitBangUartTransmit() Function

To begin analyzing BitBangUartTransmit(), perform the following steps:

  1. Restart the debugging session as follows:
    1. Click Terminate to stop the current debugging session.
    2. In the Run menu, click Debug Configurations.
    3. With the neek_uart debug configuration selected (the default), click Debug.
  2. Click Step Over to step to the call to the BitBangUartTransmit() function.
  3. Click Step Into to step into the BitBangUartTransmit() function.
  4. Click Step Over to execute one line at a time until the string BIT BANGBASH appears in Tera Term, as shown in the Transmitting BIT BANGBASH by Stepping Through the Function figure below.

bit_bang_uart.c writes a value of zero to the status register to clear any existing errors on the UART. The IOWR() macro accomplishes this step by writing to UART1_BASE.

Next, a loop cycles through the bitbang[] array, printing out the characters BIT BANG to the UART. To prevent overruns, the loop checks the transmit ready bit before each subsequent character transmission. Immediately after the loop, the software transmits characters BASH one after the other without checking the transmit ready bit.

If you step through each line to the end of the BitBangUartTransmit() function, the software transmits the characters BIT BANGBASH through the UART. These characters appear in Tera Term, as shown in the Transmitting BIT BANGBASH by Stepping Through the Function figure below. There is no transmitter overrun, because the UART transmits each character much faster than you can single-step.

Figure 13. Transmitting BIT BANGBASH by Stepping Through the Function

To observe BitBangUartTransmit()’s real-time behavior, perform the following steps:

  1. Restart the debugging session, as in the above Step 1 begin Analyzing BitBangUartTransmit().
  2. Place a breakpoint in BitBangUartTransmit(), on the following statement:

    uart_status = IORD (UART1_BASE, 2);

    To set a breakpoint, double-click in the gray area left of the line, as shown in the Setting a Breakpoint in BitBangUartTransmit() figure below.

  3. Click the Resume button . The program runs until it reaches the breakpoint.
  4. In the Variables view, right-click the uart_status variable name, point to Format, and click Hexadecimal.
    Figure 14. Setting a Breakpoint in BitBangUartTransmit()
  5. Step over the assignment of uart_status. The Variables view shows that the value of uart_status has changed to 0x170, as shown in the figure below.
    Figure 15. Value of uart_status Variable is 0x170

The register map for the Altera Avalon UART core shows that the status register's value of 0x170 means that the following bits are set:

  • Bit 8, the exception (E) bit
  • Bit 4, the transmitter overrun error (TOE) bit

The register map for the Altera Avalon UART core is described in the UART Core chapter in Embedded Peripherals IP User Guide.

Because the software does not wait for the transmitter to be ready before writing the final characters (GBASH), the transmitter is overrun and only the last character, H, is transmitted, as shown in the Transmitter Overrun figure below.

Figure 16. Transmitter Overrun