Core components
The BlueBus architecture consists of several key components that work together to provide seamless Bluetooth integration:Main application loop
The main application loop runs on a PIC24FJ microcontroller and orchestrates all system operations. Located inmain.c:152-157, the loop continuously processes three main tasks:
- BTProcess: Handles Bluetooth module communication and events
- IBusProcess: Processes I-Bus messages and maintains vehicle state
- TimerProcessScheduledTasks: Executes scheduled operations like power management
- CLIProcess: Manages debug command-line interface
Handler subsystem
The handler acts as the central coordinator between Bluetooth and I-Bus subsystems. It maintains a context structure (HandlerContext_t) that tracks:
- Bluetooth connection status and selected device
- I-Bus module status (GT, BMBT, MID, etc.)
- Vehicle state (ignition, power, telephone status)
- User interface mode and playback state
handler.c:24-82, the handler registers event callbacks and scheduled tasks to respond to system events.
Data flow
Data flows between multiple subsystems through a event-driven architecture:I-Bus to Bluetooth flow
- Message reception: I-Bus UART receives messages at 9600 baud with even parity
- Message parsing:
IBusProcess()validates checksums and extracts packet data - Module handlers: Dedicated handlers process messages from each I-Bus device (IKE, RAD, GT, etc.)
- Event triggering: Handlers trigger callbacks registered by other subsystems
- Bluetooth commands: Handler translates I-Bus commands to Bluetooth operations
Bluetooth to I-Bus flow
- UART reception: Bluetooth module sends events via UART at 115200 baud
- Module processing: BC127 or BM83 specific processor parses responses
- State updates: Bluetooth context updated with metadata, connection status
- Event callbacks: Events trigger registered handlers
- I-Bus transmission: Handler sends appropriate I-Bus messages to vehicle modules
The system uses two different Bluetooth modules depending on hardware version:
- BC127 (Sierra Wireless) for hardware v1.x
- BM83 (Microchip) for hardware v2.x
bt.c.Component initialization
System components initialize in a specific order inmain.c:116-150:
Low-level initialization
Protocol initialization
- UART configuration and buffers
- State tracking variables
- Transmit and receive queues
Audio subsystem
Depending on hardware version:- v1.x: WM8804 S/PDIF receiver + PCM5122 DAC
- v2.x: DIT4096 S/PDIF transmitter + PCM5122 DAC
Handler and UI initialization
The handler initializes last and sets up the appropriate UI mode:Event system
The event system enables decoupled communication between components. It supports up to 192 registered callbacks (event.h:9).
Event registration
Components register callbacks for specific events:Event triggering
When an event occurs, all registered callbacks execute:- I-Bus message handling (70+ events)
- Bluetooth state changes (20+ events)
- UI interactions
- Timer-based callbacks
Memory architecture
The system uses static memory allocation to avoid heap fragmentation:Circular buffers
- I-Bus RX: 255-byte circular buffer for incoming messages
- I-Bus TX: 24-slot ring buffer for outgoing messages
- UART queues: Separate RX/TX queues for each UART module
Message buffers
I-Bus messages use a structured packet format:IBusProcess() which handles:
- Buffer timeout (70ms)
- Checksum validation
- Message routing to appropriate handlers
Power management
The system includes automatic power management inhandler.c:141-162:
- Disables the UART module
- Powers down the TH3122 I-Bus transceiver
- Reduces power consumption
Error handling
The system includes comprehensive error handling:Trap handlers
Hardware traps are caught and logged inmain.c:163-217:
- Oscillator failure
- Address errors
- Stack errors
- Math errors
- NVM errors
I-Bus error handling
- Checksum validation: Invalid messages are logged and discarded
- Buffer overflow: Prevents buffer overrun with size checks
- Timeout handling: Clears stale data from receive buffers
Bluetooth error handling
- Connection timeout: 15-second timeout for connection attempts
- Pairing error tracking: Maintains error count for problematic devices
- Retry logic: Automatic reconnection attempts with backoff