System Configuration
To establish a robust wireless link, the radio parameters must be synchronized across devices. This implementation operates on Channel 22 with a designated Personal Area Network (PAN) ID of 0x8888. The code supports two distinct hardware roles defined by a preprocessor macro, allowing the same binary image to be used for both the transmitter and the receiver by simply toggling a single definition.
Source Code Implementation
#include "hal_defs.h"
#include "hal_cc8051.h"
#include "hal_mcu.h"
#include "hal_board.h"
#include "hal_led.h"
#include "hal_rf.h"
#include "basic_rf.h"
#include
/* --- Radio Configuration Constants --- */
#define APP_CHANNEL 22 // 2.4 GHz Channel: 11-26
#define APP_PAN_ID 0x8888 // Network Identifier
/* --- Address Configuration --- */
// Define this macro to configure as Device A, comment out for Device B
#define DEVICE_IS_ROLE_A
#ifdef DEVICE_IS_ROLE_A
#define LOCAL_ADDRESS 0x1A2B
#define PEER_ADDRESS 0x3C4D
#else
#define LOCAL_ADDRESS 0x3C4D
#define PEER_ADDRESS 0x1A2B
#endif
/* --- Hardware Pin Definitions --- */
#define USER_BTN P0_1 // Input push-button
#define STATUS_LED P1_4 // Output status LED
static basicRfCfg_t pRadioConfig;
static uint8_t txPayload = 0x01;
/* --- Hardware Initialization --- */
void Peripherals_Init(void)
{
P1DIR |= 0x10; // Configure LED pin as output
}
/* --- Radio Stack Initialization --- */
void Wireless_Init(void)
{
pRadioConfig.panId = APP_PAN_ID;
pRadioConfig.channel = APP_CHANNEL;
pRadioConfig.myAddr = LOCAL_ADDRESS;
pRadioConfig.ackRequest = TRUE;
// Initialize radio; wait until success
while(basicRfInit(&pRadioConfig) == FAILED);
// Enable receiver
basicRfReceiveOn();
}
/* --- Delay Routine --- */
void BlockingDelay(uint16_t ms)
{
volatile uint16_t i, j;
for(i = 0; i < ms; i++)
for(j = 0; j < 535; j++);
}
/* --- Main Application Loop --- */
void main(void)
{
halBoardInit(); // Standard HAL startup
Wireless_Init(); // Configure RF frontend
Peripherals_Init(); // Configure GPIO
while(1)
{
/* --- Transmission Handler --- */
if(USER_BTN == LOW)
{
BlockingDelay(10); // Simple debounce
if(USER_BTN == LOW)
{
// Wait for button release
while(USER_BTN == LOW);
// Transmit payload to peer
basicRfSendPacket(PEER_ADDRESS, &txPayload, 1);
}
}
/* --- Reception Handler --- */
if(basicRfPacketIsReady())
{
uint8_t rxBuffer;
basicRfReceive(&rxBuffer, 1, NULL);
// Toggle LED on valid packet receipt
if(rxBuffer == 0x01) {
STATUS_LED = ~STATUS_LED;
}
}
}
}
Deployment and Validation
Load the firmware onto two separate CC2530 development boards. To differentiate the devices during testing, the preprocessor definition DEVICE_IS_ROLE_A must be enabled in the project settings for one board and disabled for the other before compilation. Once programmed, pressing the tactile switch connected to P0.1 on one device will transmit a signal that toggles the state of the LED connected to P1.4 on the remote device.