C.02. command 219: read uart byte, C.03. command 220: send uart byte – Pololu Orangutan X2 User Manual

Page 17

Advertising
background image

data byte 1 = (permanent program mode bit << 6) | (stop-bits bit << 5) | (2x-speed bit << 4) | (UBRRH << 1) |
MSB of UBRRL

data byte 2 = seven lowest bits of UBRRL

If you want to enable permanent programming mode, you only need to send:

command byte = 200
data byte 1 = 64
data byte 2 = 0

3.c.02. Command 219: Read UART Byte

Effect: If there is a byte in the UART read buffer, this command will remove it from the buffer and load it into
the mega168’s SPDR (SPI data register). Sending a second byte over the SPI once the value is loaded (either the
NULL command or some junk data byte) will transfer the UART byte to to the 644’s SPDR. It is recommended
you do not begin transmission of this second byte until 3 us after the transmission of the command byte has
completed. This is to make sure the mega168 has time to load the SPDR.

The UART read buffer is a 32-byte circular buffer that is automatically populated by the mega168 as data comes
in over the UART. It is important you verify the read buffer actually has data before initiating a read as reading
from an empty buffer will simply return junk. When the read buffer contains a certain amount of bytes, the
mega168 status byte has its STATUS_UART_READ_READY bit set and the attention line is set. The number of
bytes it takes to trigger this status change defaults to one and can be set by the Set Read Ready Size command
(227—

Section 3.c.06

). The number of unread bytes in the read buffer can be obtained at any time by sending the

Get Number of Bytes in Read Buffer command (223—

Section 3.c.05

).

If a read error occurs when the mega168 is attempting to read a packet over the UART, the mega168’s status
byte has its STATUS_UART_ERROR bit set and the attention line is set. The UART error byte is set to reflect
the nature of the read error: read buffer overrun, frame error, data overrun, or parity error. A read buffer overrun
occurs if a byte comes in over the UART when the 32-byte read buffer is full. The other errors are generated
automatically by the UART and obtained from the UART control and status register UCSR0A. The UART error
byte can be read by sending the Get UART Error command (252—

Section 3.c.07

).

Values sent: none

command byte = 219

3.c.03. Command 220: Send UART Byte

Effect: This command sends a byte to the mega168 so it can be transmitted by the mega168 over the UART.
The mega168 puts the received byte into a 32-byte circular UART send buffer. It then transmits the bytes in the
send buffer in the order they were received over the UART (and removes them from the buffer) as the mega168
executes the UART-handling portion of its main loop. When the send buffer is full, the mega168’s status byte
has its STATUS_UART_SEND_FULL bit set and the attention line is set to indicate that the mega644 should not
transmit any more UART bytes until there is more room in the send buffer. The amount of free space in the send
buffer can be obtained at any time by sending the Get Send Free Space in Send Buffer command (222—

Section

3.c.04

).

If you attempt to send a byte while the send buffer is full, you will cause a send buffer overrun error, which
will be reflected in the UART error byte. The UART error byte can be read by sending the Get UART Error
command (252—

Section 3.c.07

). The mega168’s status byte will have its STATUS_UART_ERROR bit set and

the attention line is set to reflect the presence of this UART error.

Orangutan X2 Command Documentation v1.01

© 2001–2010 Pololu Corporation

3. Low-Level SPI Commands

Page 17 of 27

Advertising