Skip to main content

Subsection 15.2.2 Part 2: Your USART1 Driver, and the Passthrough Test

Lab 9 asks for your own driver for USART1, and weโ€™ll start writing it now. You can mimic the USART2 driver we gave you in the UART chapter. The one piece with no model in that driver is the availability check: uart2_read() waits until a byte arrives, which is not an appropriate way to handle incoming data in this project. Your program will be running normally even in the absence of input arriving from your phone, and will react to input if any arrives, so youโ€™ll need a way to ask whether a byte has arrived without waiting for one. You made this kind of non-blocking check once before, in the UART chapterโ€™s keyboard-counter exercise, where you polled the RXNE (receive data register not empty) flag yourself instead of calling uart2_read().

Activity 15.2.2. Write your USART1 driver.

(a)

Write uart1_init(). You can start with uart2_init() and modify it to use the pins that you decided on previously. Also remember that you need to enable hardware flow control.

(c)

In the final system, you wonโ€™t know which USART will transmit first, so you must devise a way to determine whether there are any characters in the receive data register. Do not use the existing read function unless you already know there is a byte waiting; otherwise, you may block indefinitely.
Hint: define a function uart1_rx_avail() that says whether a byte is waiting, and call your read function only when it does. The keyboard counter made the same check without a function, so look at how it used the RXNE flag.
To test your driver before the Bluefruit is ever wired, youโ€™ll write a small program which connects the transmitter of USART2 (CoolTerm) to the receiver of USART1, and vice versa: whatever arrives on one USART is transmitted out the other. Using the two UARTs in this manner is known as passthrough. If characters cross in both directions, your transmit, your receive and your availability checks work. The program is short: initialize both drivers, and in the main loop use your two availability checks so that neither direction ever blocks the other.
Nothing is connected to USART1 yet, so weโ€™ll use the AD2 as a device that can temporarily fill this role. The Protocol tab in the main Waveforms menu is new to us: in it the AD2 acts as another UART device, one that can both send characters into USART1โ€™s receiver and show what USART1 transmits. The AD2 does not support hardware flow control, so the passthrough test does not exercise the flow-control wires; those are first used once the Bluefruit module is connected.

Activity 15.2.3. Test your USART1 via passthrough.

(b)

Decide which DIO wire will act as the AD2โ€™s Tx and which as its Rx. Wire your Tx choice to your USART1 receive pin and your Rx choice to your USART1 transmit pin, then set the Protocol tabโ€™s Tx and Rx to those same DIO numbers, with the baud rate at 9600 to match your driver.

(c)

Type into CoolTerm. Each character should make the full trip: CoolTerm sends it to USART2โ€™s receiver, your passthrough program passes it on to USART1โ€™s transmitter, and the AD2 reads it on its Rx DIO pin and shows it. If a character does not show up on the AD2, either something is incorrectly wired or set in Waveforms, or the passthrough program is not quite right yet.

(d)

Send characters from the AD2, one at a time (the AD2 does not support hardware flow control). Each character should make the trip in reverse: the AD2 sends it from its Tx DIO pin to USART1โ€™s receiver, your program passes it on to USART2โ€™s transmitter, and it appears in CoolTerm.