Hardware Register Configuration
The CC2530 provides flexible USART configuration through several key registers. Pin mapping selection uses PERCFG, where clearing bit 0 selects Alternative Location 1 for USART0 (RX on P0_2, TX on P0_3). Setting P0SEL bits 2 and 3 enables peripheral functionality for these pins rather than general-purpose I/O.
Baud rate generation requires coordinated settings in U0BAUD and U0GCR. For 9600 bps operation with a 32 MHz system clock, load 59 into U0BAUD and 8 into U0GCR. The U0CSR register configures the operating mode—setting bit 7 enables UART mode while bit 6 activates the receiver. Data transfers occur through U0DBUF, which serves as both the transmit and receive buffer.
Interrupt handling requires enabling URX0IE in IEN0 for receive events, alongside the global EA flag. The U0UCR register provides control over frame formatting and allows buffer flushing via its most significant bit.
Implementation Approach
The following implementation establishes a 32-byte buffer for incoming data. Reception terminates upon detecting a null character or reaching buffer capacity. Up on completion, the system toggles a status endicator and echoes the received payload back to the host.
#include <iocc2530.h>
#define BUFFER_SIZE 32
#define STATUS_LED P1_1
unsigned char rxBuffer[BUFFER_SIZE];
unsigned char writeIndex = 0;
void configureClock(void)
{
CLKCONCMD &= ~0x47;
while (CLKCONSTA & 0x40);
}
void configurePins(void)
{
P1SEL &= ~0x02;
P1DIR |= 0x02;
P1 &= ~0x02;
PERCFG &= ~0x01;
P0SEL |= 0x0C;
}
void configureUart(void)
{
U0BAUD = 59;
U0GCR = 8;
U0UCR |= 0x80;
UTX0IF = 0;
URX0IF = 0;
URX0IE = 1;
EA = 1;
U0CSR |= 0xC0;
}
void uartTxByte(unsigned char byte)
{
U0DBUF = byte;
while (!UTX0IF);
UTX0IF = 0;
}
void uartTxString(unsigned char *string)
{
while (*string != '\0')
{
uartTxByte(*string++);
}
}
#pragma vector=URX0_VECTOR
__interrupt void serialInterruptHandler(void)
{
unsigned char incomingByte = U0DBUF;
if (incomingByte != '\0' && writeIndex < BUFFER_SIZE - 1)
{
rxBuffer[writeIndex++] = incomingByte;
}
else
{
rxBuffer[writeIndex] = incomingByte;
writeIndex = 0;
STATUS_LED ^= 1;
uartTxString(rxBuffer);
}
}
void main(void)
{
configureClock();
configurePins();
configureUart();
while (1)
{
// Background loop
}
}
Operational Notes
When testing with serial terminal software, configure the connection for 9600 baud, 8 data bits, no parity, and 1 stop bit. To transmit strings from the host, append a null terminator (0x00) to the ASCII payload. HEX mode transmission allows precise control over the terminator byte by sending the character codes followed by 00.
The receive interrupt accumulates bytes until encountering the null character or filling the buffer. Upon termination detection, the firmware inverts the LED state and returns the complete message to the host. The system supports both standard alphanumeric characters and extended character sets, provided the terminal application uses compatible encoding.