Skip to content

Repository files navigation

STM32F407 AUTOSAR CAN Demo

δΈ­ζ–‡ | English

License: MIT Platform: STM32 Standard: AUTOSAR

An AUTOSAR communication stack demonstration project based on STM32F407, supporting CAN communication and UDS diagnostic services.

πŸ“‹ Table of Contents


✨ Features

  • AUTOSAR Stack: Integrated CanIf, PduR, Com, Dcm, EcuM modules
  • CAN Communication: Support standard and extended frames, 500Kbps baudrate
  • DBC Code Generator: Auto-generate CAN signal packing/unpacking code from Excel
  • UDS Diagnostics: Support 0x10, 0x11, 0x22, 0x2E, 0x27, 0x3E services
  • Serial Debug: USART1 115200bps with full debug logging
  • LED Control: Support 3 LED status indicators

🎬 Demo Video

Watch the project demonstration video to see the AUTOSAR CAN communication and UDS diagnostics in action!

Demo Video
πŸ“Ή Click to view demo video

Video Contents:

  • πŸ”Œ Hardware connection and setup
  • πŸ“‘ CAN message transmission and reception
  • πŸ” UDS diagnostic service demonstration
  • πŸ› οΈ DBC code generation workflow
  • πŸ“Š Real-time debugging with serial output

Note: The demo video (docs/videos/demo.mp4) is included in the repository. You can also download it from the Releases page.


πŸ”§ Hardware Platform

Main Controller

Parameter Specification
MCU STM32F407ZGT6 (Cortex-M4, 168MHz, 1MB Flash, 192KB RAM)
External Crystal 8MHz HSE
System Clock 168MHz

Peripheral List

Peripheral Configuration Pin Assignment Clock Source
CAN CAN1, 500Kbps PA11(RX), PA12(TX) APB1 (42MHz)
USART USART1, 115200bps PA9(TX), PA10(RX) APB2 (84MHz)
GPIO 3 LED outputs PE3, PE4, PG9 AHB (168MHz)
Timer SysTick Core timer HCLK (168MHz)

Hardware Connection Diagram

                    8MHz HSE
                       |
                       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚         β”‚      β”‚         β”‚      β”‚         β”‚
β”‚STM32F407│◄────►│   CAN   │◄────►│ PCAN-USBβ”‚
β”‚  PA11   β”‚      β”‚Tranceiverβ”‚      β”‚         β”‚
β”‚  PA12   β”‚      β”‚         β”‚      β”‚         β”‚
β”‚         β”‚      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚  PA9    β”‚
β”‚  PA10   │◄────► USB to Serial Module
β”‚         β”‚
β”‚  PE3    │◄────► LED0 (Run Indicator)
β”‚  PE4    │◄────► LED1 (Reserved)
β”‚  PG9    │◄────► LED2 (Reserved)
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Development Tools

Tool Model Purpose
Debugger J-Link Plus / ST-Link V2 Program and Debug
CAN Interface PCAN-USB / PEAK CAN CAN Communication
Serial Tool USB to TTL (CH340/CP2102) Log Output

πŸš€ Quick Start

1. Clone Repository

git clone https://github.com/your-username/STM32_AUTOSAR.git
cd STM32_AUTOSAR

2. Build Project

cd STM32_AUTOSAR
mkdir -p build && cd build
cmake ..
make -j$(nproc)

3. Flash Firmware

# Using J-Link (Default)
make flash

# Or using OpenOCD
openocd -f interface/jlink.cfg -f target/stm32f4x.cfg \
    -c "program STM32F407_CAN.elf verify reset exit"

4. Setup CAN Interface

# Load CAN drivers
sudo modprobe can
sudo modprobe can_raw
sudo modprobe peak_usb

# Create CAN interface
sudo ip link add dev can0 type can bitrate 500000
sudo ip link set can0 up

# Verify
ip link show can0

5. Monitor Communication

# Terminal 1: Monitor CAN frames
candump -ta can0

# Terminal 2: Send test frame
cansend can0 7DF#0227010000000000

# Terminal 3: View serial logs
minicom -D /dev/ttyACM0 -b 115200

πŸ“ Project Structure

STM32_AUTOSAR/
β”œβ”€β”€ src/                    # Source code
β”‚   β”œβ”€β”€ board/             # Board support
β”‚   β”‚   β”œβ”€β”€ main.c         # Main entry
β”‚   β”‚   β”œβ”€β”€ startup_stm32f407xx.s
β”‚   β”‚   └── system_stm32f4xx.c
β”‚   β”œβ”€β”€ drivers/           # Hardware drivers
β”‚   β”‚   └── CanDriver.c    # CAN driver
β”‚   └── utils/             # Utilities
β”‚       β”œβ”€β”€ DebugLog.c     # Debug logging
β”‚       └── DbcHandler.c   # DBC handling
β”œβ”€β”€ include/               # Header files
β”œβ”€β”€ ld/                    # Linker scripts
β”œβ”€β”€ tools/                 # Development tools
β”‚   β”œβ”€β”€ code_generators/   # Code generators
β”‚   β”‚   β”œβ”€β”€ dbc_generator.py
β”‚   β”‚   └── uds_generator/
β”‚   β”œβ”€β”€ build_tools/       # Build scripts
β”‚   β”œβ”€β”€ debug_tools/       # Debug utilities
β”‚   └── diagnostic_tools/  # Diagnostic tools
β”œβ”€β”€ AUTOSAR/               # AUTOSAR modules
β”‚   β”œβ”€β”€ Std/              # Standard types
β”‚   β”œβ”€β”€ CanIf/            # CAN Interface
β”‚   β”œβ”€β”€ PduR/             # PDU Router
β”‚   β”œβ”€β”€ Com/              # Communication
β”‚   β”œβ”€β”€ Dcm/              # Diagnostic
β”‚   β”œβ”€β”€ EcuM/             # ECU Manager
β”‚   └── UdsConfig/        # UDS configuration
β”œβ”€β”€ docs/                  # Documentation
β”‚   └── images/           # Images
β”œβ”€β”€ CMakeLists.txt         # CMake configuration
β”œβ”€β”€ LICENSE               # MIT License
└── README.md             # This file

πŸ› οΈ Build Instructions

Prerequisites

Tool Version Installation
CMake >= 3.20 sudo apt install cmake
GCC ARM >= 13.2 sudo apt install gcc-arm-none-eabi
OpenOCD >= 0.12.0 sudo apt install openocd
Python >= 3.6 sudo apt install python3 python3-pip
openpyxl - pip3 install openpyxl

Build Steps

# Generate build files
cd STM32_AUTOSAR
mkdir -p build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Debug

# Build
make -j$(nproc)

# Output files:
# - STM32F407_CAN.elf  (with debug info)
# - STM32F407_CAN.hex  (Intel Hex)
# - STM32F407_CAN.bin  (Binary)

Build Types

# Debug build (default)
cmake .. -DCMAKE_BUILD_TYPE=Debug

# Release build
cmake .. -DCMAKE_BUILD_TYPE=Release

Custom Build Options

# Disable AUTOSAR stack
cmake .. -DENABLE_AUTOSAR=OFF

# Use ST-Link instead of J-Link
cmake .. -DINTERFACE_CFG="interface/stlink.cfg"

πŸ“‘ CAN Tools Setup

Linux (Recommended)

# Install CAN utilities
sudo apt install can-utils

# Load drivers
sudo modprobe can can_raw peak_usb

# Setup interface
sudo ip link add dev can0 type can bitrate 500000
sudo ip link set can0 up

Common Commands

# Monitor all frames
candump can0

# Monitor specific ID
candump can0,123:7FF

# Send standard frame
cansend can0 123#1122334455667788

# Send extended frame (UDS request)
cansend can0 7DF#0227010000000000

# Send diagnostic request
cansend can0 735#0322F19000000000

# Data sniffing (show changed data)
cansniffer can0

Windows Tools


πŸ” UDS Diagnostics

Supported Services

SID Service Description
0x10 DiagnosticSessionControl Switch diagnostic sessions
0x11 ECUReset Reset ECU
0x22 ReadDataByIdentifier Read DID data
0x2E WriteDataByIdentifier Write DID data
0x27 SecurityAccess Security access
0x3E TesterPresent Keep session alive

Test Examples

# 0x3E - TesterPresent
cansend can0 7DF#023E800000000000
# Response: 7E 00

# 0x10 - Default Session
cansend can0 7DF#0210010000000000
# Response: 50 01 00 32 13 88

# 0x22 - Read VIN (0xF190)
cansend can0 7DF#0322F19000000000
# Response: 62 F1 90 31 32 33...

# 0x22 - Read ECU Name (0xF197)
cansend can0 735#0322F19700000000
# Response: 62 F1 97 53 54 4D...

Diagnostic IDs (DBC Matrix)

ID Name Direction Description
0x7DF Meg_FunDiag RX Functional diagnostic request
0x735 Meg_PhyDiag RX Physical diagnostic request
0x73D Meg_ResDiag TX Diagnostic response

Automated Testing

# Run diagnostic test suite
cd tools/diagnostic_tools
python3 diag_test.py can0

🎨 Code Generators

DBC Generator

Generate C code from Excel DBC configuration:

cd tools/code_generators
./generate_dbc.sh

Input: tools/config/XZ_CAN_V2.xlsx

Output:

  • include/DbcConfig.h
  • src/utils/DbcHandler.c

UDS Configuration Generator

Generate UDS diagnostic configuration from Excel:

cd tools/code_generators/uds_generator

# Create example config
./generate_uds.sh --example

# Edit UDS_Config_Example.xlsx, then generate
./generate_uds.sh UDS_Config_Example.xlsx

Configuration Sheets:

  • SessionConfig: Session parameters (0x10 service)
  • DIDConfig: Data identifiers (0x22/0x2E services)
  • SecurityConfig: Security levels (0x27 service)
  • RoutineConfig: Routines (0x31 service)
  • DTCConfig: DTC definitions (0x19 service)

πŸ”§ Troubleshooting

CAN Issues

Q: can0: No such device

# Check if drivers are loaded
lsmod | grep can

# Reload drivers
sudo modprobe -r peak_usb
sudo modprobe peak_usb

# Verify USB device
lsusb | grep -i peak

Q: CAN frames not received

# Check interface status
ip -details link show can0

# Verify bitrate matches
# Hardware: 500Kbps, Software: 500Kbps

# Check statistics
ip -s link show can0

Flash Issues

Q: Error: J-Link not found

# Check USB connection
lsusb | grep -i jlink

# Check permissions
sudo usermod -a -G dialout $USER

# Or use ST-Link
cmake .. -DINTERFACE_CFG="interface/stlink.cfg"

Serial Issues

Q: /dev/ttyACM0: Permission denied

# Add user to dialout group
sudo usermod -a -G dialout $USER

# Temporary fix
sudo chmod 666 /dev/ttyACM0

Q: Garbled serial output

# Check baudrate
stty -F /dev/ttyACM0

# Reconfigure
stty -F /dev/ttyACM0 115200 cs8 -cstopb -parenb raw

πŸ“œ License

This project is licensed under the MIT License - see the LICENSE file for details.


🀝 Contributing

Contributions are welcome! Please read CONTRIBUTING.md for details on our code of conduct and the process for submitting pull requests.


πŸ“± Follow Us

Follow our WeChat Official Account for more AUTOSAR and embedded development content!

WeChat QR Code
Scan to follow and learn together!

Content Preview

  • πŸ”§ AUTOSAR Tutorials - From beginner to advanced
  • πŸ“‘ CAN/LIN Bus Technology - Protocol deep dive
  • πŸ’‘ Embedded Tips - Practical experience sharing
  • πŸ“° Industry News - Automotive electronics trends

Made with ❀️ by STM32F407 AUTOSAR Project Contributors

About

STM32F407 AUTOSAR CAN Communication Stack Demo with DBC Generator and UDS Diagnostics

Resources

Contributing

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages