English | 中文
A lightweight, portable UDS Diagnostic Protocol Stack implementation based on ISO 14229-1 and ISO 15765-2 standards, supporting both Classic CAN and CAN FD.
Follow my WeChat Official Account for more technical content
publilc_code/
├── 03 CANFD_Origin_Release/ # NXP S32K144 Platform (CAN FD)
│ ├── Sources/
│ │ ├── main.c # Main entry point
│ │ ├── can_fd_wrapper.c/.h # CAN FD hardware abstraction layer
│ │ └── uds/ # UDS protocol stack core
│ │ ├── uds_core.h/.c # Core layer (initialization, message dispatch)
│ │ ├── uds_tp.h/.c # CAN TP transport layer
│ │ ├── uds_session.h/.c # Session management
│ │ ├── uds_security.h/.c # Security access management
│ │ ├── uds_routine.h/.c # Routine control
│ │ ├── uds_pending.h/.c # 0x78 ResponsePending
│ │ ├── uds_nrc.h/.c # Negative response handling
│ │ ├── services/ # UDS service implementations
│ │ │ ├── uds_svc_10.h/.c # Diagnostic Session Control (0x10)
│ │ │ ├── uds_svc_19.h/.c # Read DTC Information (0x19)
│ │ │ ├── uds_svc_22.h/.c # Read Data By Identifier (0x22)
│ │ │ ├── uds_svc_27.h/.c # Security Access (0x27)
│ │ │ ├── uds_svc_2e.h/.c # Write Data By Identifier (0x2E)
│ │ │ └── uds_svc_31.h/.c # Routine Control (0x31)
│ │ └── UDS_Design_Spec.md # Detailed design specification
│ └── SDK/ # NXP S32K SDK
│
└── UDS_CAN_STM32F4/ # STM32F4 Platform (Classic CAN)
├── Main/
├── USER/
│ ├── CAN/ # CAN driver
│ ├── UDS/ # UDS application layer
│ │ ├── iso15765_tp.h/.c # ISO 15765-2 TP layer
│ │ ├── uds_app.h/.c # UDS application layer
│ ├── KEY/ # Key/button driver
│ └── LCD/ # LCD display driver
├── STM32F4_FWLIB/ # STM32F4 Standard Peripheral Library
└── Project/ # Keil project files
- ISO 14229-1 (UDS) - Unified Diagnostic Services standard
- ISO 15765-2 (CAN TP) - CAN transport layer protocol
- Classic CAN (8-byte data frames)
- CAN FD (64-byte data frames)
- Multi-frame transmission (up to 4095 bytes)
| SID | Service Name | Description |
|---|---|---|
| 0x10 | Diagnostic Session Control | Session management (Default/Extended/Programming) |
| 0x19 | Read DTC Information | Read diagnostic trouble code info (0x02/0x0A/0x06) |
| 0x22 | Read Data By Identifier | Read data by DID |
| 0x27 | Security Access | Seed/key security mechanism |
| 0x2E | Write Data By Identifier | Write data by DID (DTC fault injection) |
| 0x31 | Routine Control | Start/Stop/Request routine results |
| 0x3E | Tester Present | Keep diagnostic session alive |
- Bitmap-based session permission control
- Multi-level security access management
- Seed-key algorithm (user-customizable)
- Attempt limit and delay lock mechanism
- Physical and Functional addressing support
- Suppress Positive Response (SPR) bit handling
- 0x78 ResponsePending mechanism
- Table-driven configuration architecture (easy to extend)
- Complete NRC (Negative Response Code) support
┌─────────────────────────────────────────────────────────────┐
│ Application Layer │
│ (User-defined diagnostic services) │
└─────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ UDS Service Layer │
│ ┌─────────────┐ ┌─────────────┐ ┌──────────────────────┐ │
│ │ Service │ │ Session │ │ Security │ │
│ │ Dispatcher │ │ Manager │ │ Manager │ │
│ └─────────────┘ └─────────────┘ └──────────────────────┘ │
└─────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ CAN TP Layer │
│ ┌─────────────┐ ┌─────────────┐ ┌──────────────────────┐ │
│ │ Single Frame│ │ First Frame │ │ Consecutive Frame │ │
│ │ (SF) │ │ (FF) │ │ (CF) │ │
│ └─────────────┘ └─────────────┘ └──────────────────────┘ │
│ ┌─────────────┐ ┌─────────────┐ │
│ │ Flow Ctrl │ │ Multi-frame│ │
│ │ (FC) │ │ Buffer │ │
│ └─────────────┘ └─────────────┘ │
└─────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ CAN Driver Layer │
│ (Platform-specific: S32K144 / STM32F4) │
└─────────────────────────────────────────────────────────────┘
git clone https://github.com/xiaozhaodebug/public_code.git
cd public_code- NXP S32K144: Open S32DS project in
03 CANFD_Origin_Release/ - STM32F4: Open Keil project in
UDS_CAN_STM32F4/Project/
#include "uds_stack.h"
void UdsStackInit(void)
{
/* Initialize TP layer */
UdsTpInit();
/* Initialize session management */
UdsSessionInit();
/* Initialize security access */
UdsSecurityInit();
/* Initialize UDS core */
UdsInit(gUdsServiceTable, serviceCount);
}void CAN_Rx_IRQHandler(void)
{
CanMessage rxMsg;
CAN_Receive(&rxMsg);
/* Call UDS receive indication */
UdsCanRxIndication(rxMsg.id, rxMsg.data, rxMsg.length);
}void TIM_IRQHandler(void)
{
/* UDS timer handling */
UdsTimerISR();
}int main(void)
{
SystemInit();
UdsStackInit();
while(1)
{
/* UDS main task */
UdsMainTask();
}
}| Type | ID (Hex) | Description |
|---|---|---|
| Physical Request | 0x74C | ECU receives diagnostic requests |
| Physical Response | 0x75C | ECU sends diagnostic responses |
| Functional | 0x7DF | Broadcast requests |
/* CAN TP layer timing */
#define UDS_TP_N_AR 1000u /* Receiver response timeout (ms) */
#define UDS_TP_N_CR 1000u /* Consecutive frame wait (ms) */
#define UDS_TP_ST_MIN 20u /* Minimum separation time (ms) */
/* UDS layer timing */
#define UDS_P2_SERVER_DEFAULT 50u /* Default response timeout (ms) */
#define UDS_P2_SERVER_EXTENDED 5000u /* Extended response timeout (ms) */
#define UDS_S3_SERVER_TIMEOUT 5000u /* Session timeout (ms) */- UDS Design Specification - Detailed architecture and API documentation (Chinese)
| Platform | IDE | Compiler |
|---|---|---|
| S32K144 | NXP S32 Design Studio | GCC ARM Embedded |
| STM32F4 | Keil MDK-ARM | ARM Compiler 5/6 |
Issues and Pull Requests are welcome!
This project is open-sourced under the MIT license.
- xiaozhaodebug - xiaozhaodebug
Follow my WeChat Official Account for more technical content:
Note: This is an educational UDS protocol stack implementation, suitable for learning UDS protocol principles and embedded diagnostic development.