Subsection15.2.2Part 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().
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.
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.
uart2_read() from uart.c waits in its while loop on the RXNE flag (receive data register not empty) until a byte arrives. If nothing ever arrives, it waits forever, and your program never gets to check the other USART.
/* UART2 read function */
int uart2_read() {
/* Make sure the receive data register is non-empty */
while(!(USART2->ISR & USART_ISR_RXNE_RXFNE)){} // <-- blocks here until a byte arrives
/* Return data register */
return(USART2->RDR & 0xFF);
}
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.
To test your USART1 driver, write a small program which connects the transmitter of USART2 (CoolTerm) to the receiver of USART1, and vice versa. Using the two UARTs in this manner is known as passthrough.
The program is short. Initialize both drivers. Then, in an endless loop: if a character is available on USART1, read it and write it to USART2; if a character is available on USART2, read it and write it to USART1. Your availability checks keep either read from blocking the other.
If characters cross in both directions, your transmit, your receive and your availability checks all work. A problem that appears after the Bluefruit is connected is then in the wiring or the module, not in your driver.
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.
You can use your AD2 to observe the pins on USART1, and to send characters to it, confirming that it is working properly: use the Protocol tab in the main Waveforms menu (on the left-hand side of the welcome tab).
Sending from CoolTerm: CoolTerm sends to USART2โs receiver, your passthrough program passes the character on to USART1โs transmitter, and the AD2 reads it on its Rx DIO pin.
Sending from the AD2: the character arrives at USART1โs receiver, your program passes it on to USART2โs transmitter, and you will see it in CoolTerm.
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.
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.
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.