Fading Coder

One Final Commit for the Last Sprint

Home > Notes > Content

Implementing UART Communication in BasicRF Point-to-Point Networks

Notes May 17 2

Hardware Reqiurements

  • Two development boards
  • Two antennas
  • USB-to-serial converter
  • Debugger
  • Jumper wires
  • USB cables
  • Power cables

Code Implementation (rf_config.c)

#include "hal_defs.h"
#include "hal_cc8051.h"
#include "hal_int.h"
#include "hal_mcu.h"
#include "hal_board.h"
#include "hal_led.h"
#include "hal_rf.h"
#include "basic_rf.h"
#include "hal_uart.h"
#include <stdio.h>
#include <string.h>
#include <stdarg.h>

// Network configuration
#define RF_FREQUENCY        20       // Channel 11-26
#define NETWORK_ID          0x1A2C   // PAN identifier

#define DEVICE_A 1

#if DEVICE_A
    #define LOCAL_ADDRESS     0x105C
    #define DEST_ADDRESS      0x2F1B
#else
    #define LOCAL_ADDRESS     0x2F1B
    #define DEST_ADDRESS      0x105C
#endif

// GPIO definitions
#define LED1 P1_3
#define LED2 P1_1
#define LED3 P1_0
#define LED4 P1_4

static basicRfCfg_t rfConfig;

uint8_t receivedData;
uint8_t transmitData;
uint8_t dataReadyFlag;

void initializeRfModule() {
    rfConfig.panId = NETWORK_ID;
    rfConfig.channel = RF_FREQUENCY;
    rfConfig.myAddr = LOCAL_ADDRESS;
    rfConfig.ackRequest = TRUE;
    
    while(basicRfInit(&rfConfig) == FAILED);
    basicRfReceiveOn();
}

void configureGpio() {
    P1DIR |= 0x1B;
    LED1 = 0;
    LED2 = 0;
    LED3 = 0;
    LED4 = 0;
}

void sendUartByte(uint8_t data) {
    U0DBUF = data;
    while(UTX0IF == 0);
    UTX0IF = 0;
}

void sendUartString(uint8_t *str) {
    while(*str != '\0') {
        sendUartByte(*str++);
    }
}

#pragma vector = URX0_VECTOR
__interrupt void uartRxIsr() {
    transmitData = U0DBUF;
    dataReadyFlag = 1;
}

void main() {
    halBoardInit();
    initializeRfModule();
    configureGpio();
    
    #if DEVICE_A
        halUartInit(9600);
        sendUartString("System Ready\n");
    #endif
    
    while(1) {
        #if DEVICE_A
            if(dataReadyFlag) {
                dataReadyFlag = 0;
                basicRfSendPacket(DEST_ADDRESS, &transmitData, 1);
            }
        #endif
        
        if(basicRfPacketIsReady()) {
            basicRfReceive(&receivedData, 1, NULL);
            
            switch(receivedData) {
                case 0xD3: LED1 = ~LED1; break;
                case 0xD4: LED2 = ~LED2; break;
                case 0xD5: LED3 = ~LED3; break;
                case 0xD6: LED4 = ~LED4; break;
            }
        }
    }
}

Setup Instructions

  1. For Device A:

    • Connect debugger
    • Flash firmware with DEVICE_A defined as 1
  2. For Device B:

    • Connect debugger
    • Flash firmware with DEVICE_A commented out
  3. Configure serial terminal:

    • Select correct COM port
    • Set baud rate to 9600
    • Send commands (D3, D4, D5, D6) to control LEDs individually

Notes

  • Current implementation supports single-command LED control
  • Commands are case-sensitive
  • Ensure proper antenna placement for reliable RF communication

Related Articles

Designing Alertmanager Templates for Prometheus Notifications

How to craft Alertmanager templates to format alert messages, improving clarity and presentation. Alertmanager uses Go’s text/template engine with additional helper functions. Alerting rules referenc...

Deploying a Maven Web Application to Tomcat 9 Using the Tomcat Manager

Tomcat 9 does not provide a dedicated Maven plugin. The Tomcat Manager interface, however, is backward-compatible, so the Tomcat 7 Maven Plugin can be used to deploy to Tomcat 9. This guide shows two...

Skipping Errors in MySQL Asynchronous Replication

When a replica halts because the SQL thread encounters an error, you can resume replication by skipping the problematic event(s). Two common approaches are available. Methods to Skip Errors 1) Skip a...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.