Serial Receive Callback Example
Overview
This NetBurner embedded application demonstrates how to register a custom callback function to intercept and handle serial port receive processing. The example shows how to bypass the default system serial receive handling and implement custom processing using a circular buffer.
Application Description
The application creates a serial receive callback system that:
- Opens serial port 1 at 115200 baud rate
- Registers a custom callback function to handle incoming serial data
- Uses a circular buffer to store received data
- Continuously processes and displays received characters
Key Features
Serial Receive Callback
- Function:
SerialReceiveCallback(int uartNum, unsigned char dataByte)
- Purpose: Intercepts serial data at the interrupt level
- Restrictions: Called from interrupt context - no I/O functions allowed, only RTOS post functions
Circular Buffer Implementation
- Size: 256 bytes (
RX_BUF_SIZE)
- Variables:
indexGet: Read pointer for the circular buffer
indexPut: Write pointer for the circular buffer
CircBufferArray: The actual buffer storage
- Buffer Management: Automatically wraps around when reaching buffer limits
Main Processing Loop
- Opens serial port 1 at 115200 baud
- Assigns the custom callback to UART1 receive processing
- Continuously reads from the circular buffer and displays characters
- Uses
OSTimeDly(1) for task scheduling
Hardware Requirements
- NetBurner embedded device with UART1 capability
- Serial connection to UART1 at 115200 baud rate
Code Structure
main.cpp
Headers and includes
Global variables (circular buffer)
SerialReceiveCallback() - Interrupt-level callback
UserMain() - Main application loop
Important Notes
Interrupt Context Limitations
The SerialReceiveCallback function operates in interrupt context, which means:
- No standard I/O functions (printf, iprintf, etc.) can be called
- Only RTOS post functions are safe to use
- Keep processing minimal and fast
Buffer Overflow Handling
The circular buffer implements a simple overflow strategy:
- When the buffer is full, the oldest data is overwritten
- Both read and write pointers automatically wrap around
System Diagnostics
The application enables system diagnostics with EnableSystemDiagnostics(). This should be removed for production code.
Usage
- Compile and flash the application to a NetBurner device
- Connect a serial terminal to UART1 at 115200 baud
- Send data to the serial port
- Observe the echoed characters in the application output
Build Information
The application displays build timestamp information on startup, showing when the code was compiled.