Skip to content

refactor: split header-only drivers into source + header files#2368

Open
xunwen-art wants to merge 45 commits intocommaai:masterfrom
xunwen-art:master
Open

refactor: split header-only drivers into source + header files#2368
xunwen-art wants to merge 45 commits intocommaai:masterfrom
xunwen-art:master

Conversation

@xunwen-art
Copy link
Copy Markdown

Summary

Refactor all header-only driver files into separate source (.c) and header (.h) files.

Changes

Split 17 driver files:

  • bootkick, can_common, clock_source, fake_siren, fan, fdcan
  • gpio, harness, interrupts, led, pwm, registers
  • simple_watchdog, spi, timers, uart, usb

Benefits

  • Cleaner code organization
  • Easier to add new drivers
  • Better separation of interface and implementation
  • Reduced compilation dependencies

Testing

  • Minimal changes to function signatures
  • All function implementations preserved
  • Header files now contain only declarations and #define

Fixes #2171

xunwen-art added 5 commits March 5, 2026 01:36
Split all header-only driver files into separate .c and .h files:
- bootkick, can_common, clock_source, fake_siren, fan, fdcan
- gpio, harness, interrupts, led, pwm, registers
- simple_watchdog, spi, timers, uart, usb

Changes:
- Move function implementations to .c files
- Keep function declarations and #define in .h files
- Add proper include guards to all header files
- Add necessary #include directives

Addresses commaai#2171
- Move fan_state_t struct and extern fan_state to fan.h
- Move REGISTER_INTERRUPT macro and interrupt struct to interrupts.h
- This fixes compilation errors where llfan.h and llusb.h
  couldn't find these definitions
- Move REGISTER_INTERRUPT macro to interrupts.h
- Add extern declarations for uart_ring buffers in uart.h
- Remove duplicate interrupt definitions from drivers.h
…larations

- Add driver_sources list to SConscript for both bootstub and main builds
- Add harness_t struct and extern harness to harness.h
- Add SPI_IRQ_RATE, SPI_BUF_SIZE and extern declarations to spi.h
@xunwen-art
Copy link
Copy Markdown
Author

这个重构比预期更复杂,需要更多时间来修复构建系统问题。原始代码使用 header-only 模式,所有 driver 代码在编译时通过 bootstub.c 或 main.c 直接 include。改为 source + header 模式需要修改 SConscript 来正确编译 .c 文件,并处理 scons 的对象文件命名冲突。

我会继续研究这个问题,并在准备好后重新提交。

@xunwen-art xunwen-art closed this Mar 5, 2026
@xunwen-art xunwen-art reopened this Mar 5, 2026
xunwen-art and others added 22 commits March 5, 2026 15:33
SCons was complaining about 'Two environments with different actions'
because bootstub and main both compile driver sources with different
flags (-DBOOTSTUB vs no flag). Now bootstub uses a separate output
directory for its driver object files.
Now these headers include drivers.h which contains the struct definitions
- Add extern declaration for can_rx_q in can_comms.h
- Add __attribute__((unused)) to sound_stop_dac to suppress warning
The driver .c files need to include board/config.h first to get
stm32h7xx.h (which defines ADC_TypeDef) before including drivers.h.

This fixes the build error:
  lladc_declarations.h:29:3: error: unknown type name 'ADC_TypeDef'
- Include board/boards/board_declarations.h for BootState type
- Add refresh_can_tx_slots_available() declaration that was lost during refactoring

This fixes build errors:
- unknown type name 'BootState'
- implicit declaration of function 'refresh_can_tx_slots_available'
- Create board/boot_state.h with BootState enum definition
- Include boot_state.h in drivers.h instead of board_declarations.h
  (avoiding struct board redefinition conflict with body/boards)
- Add refresh_can_tx_slots_available() declaration to can_common.h

This fixes:
- 'unknown type name BootState' in bootkick.c
- 'redefinition of struct board' when building for PANDA_BODY
- 'implicit declaration of refresh_can_tx_slots_available' in can_common.c
- Create board/drivers/boot_state.h with just the BootState enum
- Include it from board_declarations.h and drivers.h
- Fix include path (was board/boot_state.h, now board/drivers/boot_state.h)

This breaks the circular dependency:
- drivers.h needs BootState for bootkick_tick declaration
- board_declarations.h defines BootState but also struct board
- Different board types (body/jungle/regular) have different struct board

Now BootState can be included early without pulling in struct board.
Use board/drivers/boot_state.h instead (which already exists in PR)
- SConscript: Skip bootkick.c for PANDA_BODY/JUNGLE builds (no SOM support)
- can_common.c: Include opendbc/safety/safety.h for safety_tx_hook declaration
- Wrap bootkick_tick and bootkick_reset_triggered in #if !PANDA_BODY && !PANDA_JUNGLE
- These symbols don't exist for BODY/JUNGLE builds since struct board is different
xunwen-art added 18 commits March 9, 2026 01:19
- struct interrupt was missing, causing redefinition errors
- Also added REGISTER_INTERRUPT macro and related function declarations
- These are needed by interrupts.h which includes drivers.h
- struct interrupt is now defined in drivers.h
- interrupts.h just includes drivers.h and provides implementation
- This fixes the redefinition error
- Added uint32_t time declaration
- Fixed the time tracking logic in handle_interrupt
These are needed by lluart.h and other header files that reference them.
- Include llfdcan.h and llfdcan_declarations.h for llcan_set_speed
- Wrap in #ifndef BOOTSTUB since FDCAN only used in main builds
interrupts.h should only have declarations, not implementations.
Implementations are in interrupts.c.
BOOTSTUB builds use stub print functions from bootstub_declarations.h.
Main builds use full UART implementation.
llfdcan.h is already included by stm32h7_config.h.
Including it again causes redefinition of speeds/data_speeds arrays.
fdcan.c needs access to can_rx_q, can_tx1_q, etc.
sound.h is already included via board.h -> stm32h7_config.h
- Add static inline to functions in llspi.h, interrupt_handlers.h
- Add static const to arrays in llfdcan.h
- Add extern declarations and move global definitions to .c files:
  - USBx moved from llusb.h to usb.c
  - Global state moved from faults.h/critical.h/main_definitions.h to sys.c
  - early_gpio_float moved from stm32h7_config.h to gpio.c
- Properly declare hw_type/current_board extern in bootstub_declarations.h

This fixes linker errors when driver source files are compiled separately
for bootstub and main builds.
The opendbc module exports DBC_PATH, not INCLUDE_PATH.
This fixes SCons build configuration errors.
- Remove forward declarations from sys.h (now static inline in critical.h)
- Declare interrupts_enabled as extern in sys.h
- Define interrupts_enabled in sys.c
- sys.c doesn't need can.h definitions
- This fixes the opendbc header not found error in CI
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