Skip to content

Conversation

@AgentsLogic
Copy link

@AgentsLogic AgentsLogic commented Jan 3, 2026

This PR refactors the panda firmware codebase to use a proper source + header file structure, addressing bounty #2171.

Changes

  • Split all header-only files into separate .c (implementation) and .h (declarations) files
  • Moved function implementations from .h files to new .c files
  • Updated headers to contain only declarations, macros, and typedefs
  • Added #pragma once to all header files for cleaner include guards
  • Updated SConscript build system to compile all new .c files
  • Updated MISRA mutation tests to reference .c files instead of .h files
  • Deleted all *_declarations.h files as they are no longer needed

Benefits

  • Improved code organization: Clear separation between interface (headers) and implementation (source files)
  • Better MISRA C:2012 compliance: Proper source/header structure is a MISRA best practice
  • Reduced compilation time: Avoids redundant inline expansions across multiple translation units
  • Easier maintenance: Changes to implementations don't require recompiling all files that include the header

Files Converted

Board-level files:

  • board/critical.c/h, faults.c/h, power_saving.c/h

Drivers:

  • board/drivers/: bootkick, can_common, clock_source, fan, fdcan, gpio, harness, interrupts, led, pwm, registers, simple_watchdog, spi, uart, usb

STM32H7 low-level drivers:

  • board/stm32h7/: clock, lladc, llfan, llfdcan, llflash, lli2c, llspi, lluart, llusb

Testing

All changes maintain the exact same functionality - only the file organization has changed. The CI will verify:

  • All firmware targets build successfully (panda_h7, panda_jungle_h7, body_h7)
  • MISRA C compliance checks pass
  • Mutation tests pass

Closes #2171

- Split all header-only files into separate .c (implementation) and .h (declarations) files
- Moved function implementations from .h files to new .c files
- Updated headers to contain only declarations, macros, and typedefs
- Added #pragma once to all header files
- Updated SConscript build system to compile all new .c files
- Updated MISRA mutation tests to reference .c files instead of .h files
- Deleted all *_declarations.h files as they are no longer needed
- Improves code organization and MISRA C:2012 compliance
- Reduces compilation time by avoiding redundant inline expansions

Files converted:
- board/critical.c/h, faults.c/h, power_saving.c/h
- board/drivers/: bootkick, can_common, clock_source, fan, fdcan, gpio, harness, interrupts, led, pwm, registers, simple_watchdog, spi, uart, usb
- board/stm32h7/: clock, lladc, llfan, llfdcan, llflash, lli2c, llspi, lluart, llusb
- Add stdint.h to critical.h for uint8_t
- Add stdint.h and stdbool.h to faults.h for uint8_t, uint32_t, bool
- Add stdbool.h to power_saving.h for bool
@AgentsLogic AgentsLogic marked this pull request as draft January 3, 2026 16:08
- Add stdbool.h to critical.c for bool type
- Add board/drivers/uart.h to faults.c for print() and puth()
- Add stdint.h, harness.h, and board_declarations.h to power_saving.c
All .c files need to include board/config.h first to get platform headers (STM32 HAL, CMSIS intrinsics, etc.) before including their own headers.
power_saving.c uses the harness global variable which is declared in board/drivers/harness.h
harness.h uses adc_signal_t which is defined in board/stm32h7/lladc.h
power_saving.c uses board functions (enable_can_transceiver, set_ir_power) that don't exist in the body board struct. It's only included in board/main.c, not board/body/main.c, so it should only be compiled for panda and jungle builds.
The lladc.h header was missing declarations for adc_init(), adc_get_raw(), and adc_get_mV() which are defined in lladc.c. This caused compilation errors when other files (like board/boards/red.h) tried to use adc_get_mV().
uart_ring_debug and uart_ring_som_debug are defined in uart.c but were not declared as extern in uart.h, causing compilation errors when other files (like board/boards/tres.h) tried to use them.
can_rx_q, can_tx1_q, can_tx2_q, and can_tx3_q are defined in can_common.c but were not declared as extern in can_common.h, causing compilation errors when other files tried to use them.
…ivers, power_saving.c

- Fix board/body/can.h to include board/drivers/can_common.h instead of non-existent can_common_declarations.h
- Add enable_can_transceivers function declaration to power_saving.h
- Exclude power_saving.c from jungle build (only for panda, not jungle or body)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[$500 Bounty] Refactor panda to use source + header file structure

1 participant