├── .gitattributes ├── .gitignore ├── Backup └── xlCANdemo.sln ├── FMS.h ├── J1939.c ├── J1939.h ├── J1939_AL.c ├── J1939_AL.h ├── J1939_CFG.h ├── J1939_DL.c ├── J1939_DL.h ├── J1939_HAL.c ├── J1939_HAL.h ├── J1939_NML.c ├── J1939_NML.h ├── J1939_TL.c ├── J1939_TL.h ├── J1939_includes.h ├── J1939_stack.sln ├── J1939_stack.suo ├── J1939_stack_caret ├── J1939_stack_test.c ├── ReadMe.txt ├── UpgradeLog.htm ├── xlCANdemo.sdf ├── xlCANdemo.vcproj ├── xlCANdemo.vcxproj ├── xlCANdemo.vcxproj.filters └── xlCANdemo.vcxproj.user /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Object files 2 | *.o 3 | *.ko 4 | *.obj 5 | *.elf 6 | 7 | # Libraries 8 | *.lib 9 | *.a 10 | 11 | # Shared objects (inc. Windows DLLs) 12 | *.dll 13 | *.so 14 | *.so.* 15 | *.dylib 16 | 17 | # Executables 18 | *.exe 19 | *.out 20 | *.app 21 | *.i*86 22 | *.x86_64 23 | *.hex 24 | 25 | # ========================= 26 | # Operating System Files 27 | # ========================= 28 | 29 | # OSX 30 | # ========================= 31 | 32 | .DS_Store 33 | .AppleDouble 34 | .LSOverride 35 | 36 | # Icon must ends with two \r. 37 | Icon 38 | 39 | # Thumbnails 40 | ._* 41 | 42 | # Files that might appear on external disk 43 | .Spotlight-V100 44 | .Trashes 45 | 46 | # Windows 47 | # ========================= 48 | 49 | # Windows image file caches 50 | Thumbs.db 51 | ehthumbs.db 52 | 53 | # Folder config file 54 | Desktop.ini 55 | 56 | # Recycle Bin used on file shares 57 | $RECYCLE.BIN/ 58 | 59 | # Windows Installer files 60 | *.cab 61 | *.msi 62 | *.msm 63 | *.msp 64 | *.sdf 65 | *.suo 66 | .zedstate 67 | J1939_AL.c~ 68 | .J1939_AL.c.swp 69 | -------------------------------------------------------------------------------- /Backup/xlCANdemo.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2013 4 | VisualStudioVersion = 12.0.31101.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "xlCANdemo", "xlCANdemo.vcproj", "{304D975A-4066-454A-8019-C2BE0DDA996E}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Win32 = Debug|Win32 11 | Debug|x64 = Debug|x64 12 | Release|Win32 = Release|Win32 13 | Release|x64 = Release|x64 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {304D975A-4066-454A-8019-C2BE0DDA996E}.Debug|Win32.ActiveCfg = Debug|Win32 17 | {304D975A-4066-454A-8019-C2BE0DDA996E}.Debug|Win32.Build.0 = Debug|Win32 18 | {304D975A-4066-454A-8019-C2BE0DDA996E}.Debug|x64.ActiveCfg = Debug|x64 19 | {304D975A-4066-454A-8019-C2BE0DDA996E}.Debug|x64.Build.0 = Debug|x64 20 | {304D975A-4066-454A-8019-C2BE0DDA996E}.Release|Win32.ActiveCfg = Release|Win32 21 | {304D975A-4066-454A-8019-C2BE0DDA996E}.Release|Win32.Build.0 = Release|Win32 22 | {304D975A-4066-454A-8019-C2BE0DDA996E}.Release|x64.ActiveCfg = Release|x64 23 | {304D975A-4066-454A-8019-C2BE0DDA996E}.Release|x64.Build.0 = Release|x64 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | EndGlobal 29 | -------------------------------------------------------------------------------- /FMS.h: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////////////////// 2 | // File: FMS.h 3 | // Module: J1939 stack 4 | // Description: Defines for the FMS layer 5 | // Originator: MB 6 | // Derived from: N/A 7 | // Date created: 09 June 2014 8 | //--------------------------------------------------------------------------------------- 9 | // Revision Log: 10 | // $Log: $ 11 | // 12 | //--------------------------------------------------------------------------------------- 13 | // Notes: 14 | // 15 | //--------------------------------------------------------------------------------------- 16 | ///////////////////////////////////////////////////////////////////////////////////////// 17 | // Copyright (C)2014 by Timespace Technology Ltd. All rights reserved. 18 | // 19 | // This software is confidential. It is not to be copied or distributed in any form 20 | // to any third party. 21 | // 22 | // This software is provided by Timespace Technology on an 'as is' basis, and Timespace 23 | // Technology expressly disclaims any and all warranties, expressed or implied including, 24 | // without limitation, warranties of merchantability and fitness for a particular 25 | // purpose. In no event shall Timespace Technology be liable for any direct, indirect, 26 | // incidental, punitive or consequential damages - of any kind whatsoever - with respect 27 | // to the software. 28 | ///////////////////////////////////////////////////////////////////////////////////////// 29 | 30 | 31 | // FMS compliance options 32 | #define FMS_TRUCK_STANDARD 1 33 | #define FMS_BUS_STANDARD 2 34 | #define FMS_COMPLIANCE (FMS_TRUCK_STANDARD) 35 | 36 | // 1 Parameters for FMS gateway 37 | // always MSB (Most Significant Bit first) 38 | #if (FMS_COMPLIANCE & FMS_BUS_STANDARD || FMS_COMPLIANCE & FMS_TRUCK_STANDARD) 39 | // 1.1 Parameters for Bus and Truck FMS-Standard 40 | #define FMS_LFC 0x00FEE9 // 1.1.1 Fuel consumption, 0x00FEE9=65,257, 1000ms 41 | #define FMS_DD 0x00FEFC // 1.1.2 Dash Display, 0x00FEFC=65,276, 1000ms 42 | #define FMS_EEC1 0x00F004 // 1.1.3 Electronic Engine Controller #1, 0x00F004=61,444, 20ms 43 | #define FMS_HOURS 0x00FEE5 // 1.1.4 Engine Hours, Revolutions, 0x00FEE5=65,253, 1000ms 44 | #define FMS_VI 0x00FEEC // 1.1.5 Vehicle Identification, 0x00FEEC=65,260, 10,000ms 45 | #define FMS_FMS 0x00FDD1 // 1.1.6 FMS-standard Interface Identity, 0x00FDD1=64,977, 10,000ms 46 | #define FMS_VDHR 0x00FEC1 // 1.1.7 High Resolution Vehicle Distance, 0x00FEC1=65,217, 1000ms 47 | #define FMS_TCO1 0x00FE6C // 1.1.8 Tachograph, 0x00FE6C=65,132, 20ms/50ms 48 | #define FMS_ET1 0x00FEEE // 1.1.9 Engine Temperature 1, 0x00FEEE=65,262, 1000ms 49 | #define FMS_AMB 0x00FEF5 // 1.1.10 Ambient Conditions, 0x00FEF5=65,269, 1000ms 50 | #define FMS_DI 0x00FE6B // 1.1.11 Driver's Identification, 0x00FE6B=65,131, 10,000ms 51 | #define FMS_LFE 0x00FEF2 // 1.1.12 Fuel Economy, 0x00FEF2=65,266, 100ms 52 | #define FMS_AIR1 0x00FEAE // 1.1.13 Air Supply Pressure, 0x00FEAE=65,198, 1000ms 53 | #define FMS_HRLFC 0x00FD09 // 1.1.14 High Resolution Fuel Consumption (Liquid), 0x00FD09=64,777, 1000ms 54 | #define FMS_AT1T1I 0x00FE56 // 1.1.15 Aftertreatment 1 Diesel Exhaust Fluid Tank 1 Information, 0x00FE56=65,110, 1000ms 55 | #define FMS_FMS1 0x00FD7D // 1.1.16 FMS Tell Tale Status, 0x00FD7D=64,893, 1000ms 56 | #endif 57 | 58 | #if (FMS_COMPLIANCE & FMS_TRUCK_STANDARD) 59 | // 1.2 Parameters for Truck FMS-Standard 60 | #define FMS_CCVS 0x00FEF1 // 1.2.1 Cruise Control/Vehicle Speed, 0x00FEF1=65,265, 100ms 61 | #define FMS_EEC2 0x00F003 // 1.2.2 Electronic Engine Controller #2, 0x00F003=61,443, 50ms 62 | #define FMS_VW 0x00FEEA // 1.2.3 Vehicle Weight, 0x00FEEA=65,258, 1000ms 63 | #define FMS_SERV 0x00FEC0 // 1.2.4 Service Information, 0x00FEC0=65,216, 1000ms 64 | #define FMS_PTODE 0x00FDA4 // 1.2.5 PTO Drive Engagement, 0x00FDA4=64,932, 100ms 65 | #define FMS_CVW 0x00FE70 // 1.2.6 Combination Vehicle Weight, 0x00FE70=65,136, 10,000ms 66 | #define FMS_ERC1 0x00F000 // 1.2.7 Electronic Retarder Controller, 0x00F000=61,440, 100ms 67 | #endif 68 | 69 | #if (FMS_COMPLIANCE & FMS_BUS_STANDARD) 70 | // 1.3 Parameters for Bus FMS-Standard 71 | #define FMS_CCVS 0x00FEF1 // 1.3.1 Cruise Control/Vehicle Speed, 0x00FEF1=65,265, 100ms 72 | #define FMS_EEC2 0x00F003 // 1.3.2 Electronic Engine Controller #2, 0x00F003=61,443, 50ms 73 | #define FMS_DC1 0x00FE4E // 1.3.3 Door Control 1, 0x00FE4E=65,102, 100ms 74 | #define FMS_DC2 0x00FDA5 // 1.3.4 Door Control 2, 0x00FDA5=64,933, 100ms 75 | #define FMS_TD 0x00FEE6 // 1.3.5 Time/Date, 0x00FEE6=65,254, 1000ms 76 | #define FMS_AS 0x00FED5 // 1.3.6 Alternator Speed, 0x00FED5=65,237, 1000ms 77 | #define FMS_ETC2 0x00F00f // 1.3.7 Electronic Transmission Controller 2, 0x00F005=61,445, 1000ms 78 | #endif -------------------------------------------------------------------------------- /J1939.c: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////////////////// 2 | // File: J1939.c 3 | // Module: J1939 stack 4 | // Description: Main routines for the J1939 stack 5 | // Originator: MB 6 | // Derived from: Freescale J1939 stack 7 | // Date created: 09 June 2014 8 | //--------------------------------------------------------------------------------------- 9 | // Revision Log: 10 | // $Log: $ 11 | // 12 | //--------------------------------------------------------------------------------------- 13 | // Notes: 14 | // 15 | //--------------------------------------------------------------------------------------- 16 | ///////////////////////////////////////////////////////////////////////////////////////// 17 | // Copyright (C)2014 by Timespace Technology Ltd. All rights reserved. 18 | // 19 | // This software is confidential. It is not to be copied or distributed in any form 20 | // to any third party. 21 | // 22 | // This software is provided by Timespace Technology on an 'as is' basis, and Timespace 23 | // Technology expressly disclaims any and all warranties, expressed or implied including, 24 | // without limitation, warranties of merchantability and fitness for a particular 25 | // purpose. In no event shall Timespace Technology be liable for any direct, indirect, 26 | // incidental, punitive or consequential damages - of any kind whatsoever - with respect 27 | // to the software. 28 | ///////////////////////////////////////////////////////////////////////////////////////// 29 | 30 | 31 | #include "J1939_includes.h" 32 | 33 | //======================================================================================== 34 | //protocol stack Interface Functions 35 | //======================================================================================== 36 | 37 | void J1939_stk_init(void) 38 | { 39 | HAL_init(); 40 | DL_init(); 41 | TL_init(); 42 | NML_init(); 43 | AL_init(); 44 | } 45 | 46 | 47 | void J1939_stk_periodic(void) 48 | { 49 | HAL_periodic(); 50 | DL_periodic(); 51 | TL_periodic(); 52 | NML_periodic(); 53 | AL_periodic(); 54 | } -------------------------------------------------------------------------------- /J1939.h: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////////////////// 2 | // File: J1939.h 3 | // Module: J1939 stack 4 | // Description: Defines for the J1939 stack 5 | // Originator: MB 6 | // Derived from: Freescale J1939 stack 7 | // Date created: 09 June 2014 8 | //--------------------------------------------------------------------------------------- 9 | // Revision Log: 10 | // $Log: $ 11 | // 12 | //--------------------------------------------------------------------------------------- 13 | // Notes: 14 | // 15 | //--------------------------------------------------------------------------------------- 16 | ///////////////////////////////////////////////////////////////////////////////////////// 17 | // Copyright (C)2014 by Timespace Technology Ltd. All rights reserved. 18 | // 19 | // This software is confidential. It is not to be copied or distributed in any form 20 | // to any third party. 21 | // 22 | // This software is provided by Timespace Technology on an 'as is' basis, and Timespace 23 | // Technology expressly disclaims any and all warranties, expressed or implied including, 24 | // without limitation, warranties of merchantability and fitness for a particular 25 | // purpose. In no event shall Timespace Technology be liable for any direct, indirect, 26 | // incidental, punitive or consequential damages - of any kind whatsoever - with respect 27 | // to the software. 28 | ///////////////////////////////////////////////////////////////////////////////////////// 29 | 30 | 31 | #ifndef _J1939_H_ 32 | #define _J1939_H_ 33 | 34 | #include "J1939_includes.h" 35 | #define J1939STACK_VERSION 130 36 | 37 | // TODO - Command Request/Ack Parameter Group Numbers 38 | #define REQUEST_PGN 0x00EA00 // 0x00EA00=59904 39 | #define REQUEST_PGN_NUM 15 40 | #define ACK_PGN 0x00E800 // 0x00E800=59392 41 | #define ACK_PGN_ACK 0 42 | #define ACK_PGN_NACK 1 43 | #define ACK_PGN_ACCESS_DENIED 2 44 | #define ACK_PGN_CANNOT_RESPOND 3 45 | 46 | // TODO - Proprietary Parameter Group Numbers 47 | #define PROPRIETARY_A 0x00EF00 // 0x00EF00=61184 48 | #define PROPRIETARY_A2 0x01EF00 // 0x01EF00=126720 Note: requires 24 bit PGN capability 49 | #define PROPRIETARY_B_START 0x00FF00 // 0x00FF00=65280 50 | #define PROPRIETARY_B_END 0x00FFFF // 0x00FFFF=65535 51 | 52 | // Receive state machine 53 | #define WAIT_FOR_MESSAGE 0 // State Zero: Waiting for a BAM or RTS data packet to arrive 54 | #define INIT_REASSEMBLE_STRUCTURE 1 // State One: Message reassemble structure and message timer are initialized. 55 | #define CHECK_PACKET 2 // State Two: The incoming message is for this node. 56 | #define SEND_ABORT 3 // State Three: No more buffers to reassemble incoming message, Abort is sent. 57 | #define SEND_CTS_WITH_COUNT 4 // State Four: Buffers are available to reassemble incoming message, send CTS back to sender to indicate number of packets between next CTS (TRANSPORT_PACKET_COUNT). 58 | #define WAIT_FOR_DATA 5 // State Five: Waiting for DATA type packet from source. 59 | #define CHECK_TIMER 6 // State Six: Message timer is checked for expiration. 60 | #define RESET_REASSEMBLY_STRUCTURE 7 // State Seven: Message reassemble structure is reset. 61 | #define CHECK_DATA_PACKET 8 // State Eight: A DATA type packet has arrived and checked. 62 | #define SAVE_DATA 9 // State Nine: The data is saved in the message reassemble buffer 63 | #define SEND_EOM 10 // State Ten: EOM message is sent. 64 | #define SEND_CTS 11 // State Eleven: CTS is sent to trigger more data from the sender. 65 | #define FILL_USER_MESSAGE 12 // State Twelve: The J1939_RX_MESSAGE_T data structure is filled for user application. 66 | 67 | // Transport Protocol Layer 68 | #define TP_CM 0x00EC00 // 0x00EC00=60416 69 | #define TP_CM_RTS 16 70 | #define TP_CM_CTS 17 71 | #define TP_CM_END_OF_MSG_ACK 19 72 | #define TP_CM_CONN_ABORT 255 73 | #define TP_CM_BAM 32 74 | #define TP_DT 0x00EB00 // 0x00EB00=60160 75 | #define TP_NONE 0 76 | 77 | // TODO - Network Management Layer - PGNs 78 | #define REQUEST_FOR_ADDRESS_CLAIMED 0x00EA00 // 0x00EA00=59904 79 | #define ADDRESS_CLAIMED 0x00EE00 // 0x00EE00=60928 80 | #define CANNOT_CLAIM 0x00EE00 // 0x00EE00=60928 81 | #define COMMANDED_ADDRESS 0x00FED8 // 0x00FED8=65240 82 | 83 | // Diagnostic Trouble Code (DTC) 84 | #define J1939_DM1 0x00FECA // 0x00FECA=65226 85 | 86 | #define PRIMED 1 87 | #define NOTPRIMED 0 88 | 89 | #ifndef NULL 90 | #define NULL 0 91 | #endif 92 | 93 | #ifndef TRUE 94 | #define TRUE 1 95 | #endif 96 | 97 | #ifndef FALSE 98 | #define FALSE 0 99 | #endif 100 | 101 | typedef struct 102 | { 103 | PGN_T PGN; 104 | VU08 data[NUMBER_TRANS_TX_BUFFERS]; 105 | VU16 byte_count; 106 | VU08 priority; 107 | VU08 dest_addr; 108 | VS08 status; 109 | } J1939_TX_MESSAGE_T; 110 | 111 | typedef struct 112 | { 113 | PGN_T PGN; 114 | VU08 data[NUMBER_TRANS_RX_BUFFERS]; 115 | VU16 byte_count; 116 | VU08 source_addr; 117 | VU08 dest_addr; 118 | } J1939_RX_MESSAGE_T; 119 | 120 | typedef struct 121 | { 122 | PGN_T PGN; 123 | VU08 data[NUMBER_PDU_BUFFERS]; 124 | VU16 byte_count; 125 | VU08 source_addr; 126 | VU08 dest_addr; 127 | } J1939_RX_PDU_T; 128 | 129 | typedef struct 130 | { 131 | PGN_T PGN; 132 | VU08 status; 133 | VU08 packet_number; 134 | VU08 total_packet_number; 135 | VU16 byte_count; 136 | VU16 timer_counter; 137 | VU08 source_addr; 138 | VU08 dest_addr; 139 | VU08 TP; 140 | } J1939_RX_STATE_MACHINE_T; 141 | 142 | typedef struct 143 | { 144 | VU32 identifier; 145 | VU08 data[CAN_MAX_BYTE_COUNT]; 146 | VU08 byte_count; 147 | } CAN_PACKET_T; 148 | 149 | typedef struct 150 | { 151 | CAN_PACKET_T *buffer; 152 | VU16 head; 153 | VU16 tail; 154 | VU08 buffer_size; 155 | } RING_T; 156 | 157 | 158 | //======================================================================================== 159 | //J1939 protocol stack Interface Functions 160 | //======================================================================================== 161 | void J1939_stk_init(void); 162 | void J1939_stk_periodic(void); 163 | 164 | #endif 165 | -------------------------------------------------------------------------------- /J1939_AL.c: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////////////////// 2 | // File: J1939_AL.c 3 | // Module: J1939 stack 4 | // Description: J1939 stack Application Layer 5 | // Originator: MB 6 | // Derived from: Freescale J1939 stack 7 | // Date created: 09 June 2014 8 | //--------------------------------------------------------------------------------------- 9 | // Revision Log: 10 | // $Log: $ 11 | // 12 | //--------------------------------------------------------------------------------------- 13 | // Notes: 14 | // 15 | //--------------------------------------------------------------------------------------- 16 | ///////////////////////////////////////////////////////////////////////////////////////// 17 | // Copyright (C)2014 by Timespace Technology Ltd. All rights reserved. 18 | // 19 | // This software is confidential. It is not to be copied or distributed in any form 20 | // to any third party. 21 | // 22 | // This software is provided by Timespace Technology on an 'as is' basis, and Timespace 23 | // Technology expressly disclaims any and all warranties, expressed or implied including, 24 | // without limitation, warranties of merchantability and fitness for a particular 25 | // purpose. In no event shall Timespace Technology be liable for any direct, indirect, 26 | // incidental, punitive or consequential damages - of any kind whatsoever - with respect 27 | // to the software. 28 | ///////////////////////////////////////////////////////////////////////////////////////// 29 | 30 | 31 | #include "J1939_includes.h" 32 | #include "FMS.h" 33 | 34 | 35 | const PGN_T _pgn_table[PGN_NUM] = 36 | { 37 | #if (FMS_COMPLIANCE & FMS_BUS_STANDARD || FMS_COMPLIANCE & FMS_TRUCK_STANDARD) 38 | // 1.1 Parameters for Bus and Truck FMS-Standard 39 | FMS_LFC, // 1.1.1 Fuel consumption, 0x00FEE9=65,257, 1000ms 40 | FMS_DD, // 1.1.2 Dash Display, 0x00FEFC=65,276, 1000ms 41 | FMS_EEC1, // 1.1.3 Electronic Engine Controller #1, 0x00F004=61,444, 20ms 42 | FMS_HOURS, // 1.1.4 Engine Hours, Revolutions, 0x00FEE5=65,253, 1000ms 43 | FMS_VI, // 1.1.5 Vehicle Identification, 0x00FEEC=65,260, 10,000ms 44 | FMS_FMS, // 1.1.6 FMS-standard Interface Identity, 0x00FDD1=64,977, 10,000ms 45 | FMS_VDHR, // 1.1.7 High Resolution Vehicle Distance, 0x00FEC1=65,217, 1000ms 46 | FMS_TCO1, // 1.1.8 Tachograph, 0x00FE6C=65,132, 20ms/50ms 47 | FMS_ET1, // 1.1.9 Engine Temperature 1, 0x00FEEE=65,262, 1000ms 48 | FMS_AMB, // 1.1.10 Ambient Conditions, 0x00FEF5=65,269, 1000ms 49 | FMS_DI, // 1.1.11 Driver's Identification, 0x00FE6B=65,131, 10,000ms 50 | FMS_LFE, // 1.1.12 Fuel Economy, 0x00FEF2=65,266, 100ms 51 | FMS_AIR1, // 1.1.13 Air Supply Pressure, 0x00FEAE=65,198, 1000ms 52 | FMS_HRLFC, // 1.1.14 High Resolution Fuel Consumption (Liquid), 0x00FD09=64,777, 1000ms 53 | FMS_AT1T1I, // 1.1.15 Aftertreatment 1 Diesel Exhaust Fluid Tank 1 Information, 0x00FE56=65,110, 1000ms 54 | FMS_FMS1, // 1.1.16 FMS Tell Tale Status, 0x00FD7D=64,893, 1000ms 55 | #endif 56 | 57 | #if (FMS_COMPLIANCE & FMS_TRUCK_STANDARD) 58 | // 1.2 Parameters for Truck FMS-Standard 59 | FMS_CCVS, // 1.2.1 Cruise Control/Vehicle Speed, 0x00FEF1=65,265, 100ms 60 | FMS_EEC2, // 1.2.2 Electronic Engine Controller #2, 0x00F003=61,443, 50ms 61 | FMS_VW, // 1.2.3 Vehicle Weight, 0x00FEEA=65,258, 1000ms 62 | FMS_SERV, // 1.2.4 Service Information, 0x00FEC0=65,216, 1000ms 63 | FMS_PTODE, // 1.2.5 PTO Drive Engagement, 0x00FDA4=64,932, 100ms 64 | FMS_CVW, // 1.2.6 Combination Vehicle Weight, 0x00FE70=65,136, 10,000ms 65 | FMS_ERC1 // 1.2.7 Electronic Retarder Controller, 0x00F000=61,440, 100ms 66 | #endif 67 | 68 | #if (FMS_COMPLIANCE & FMS_BUS_STANDARD) 69 | // 1.3 Parameters for Bus FMS-Standard 70 | FMS_CCVS, // 1.3.1 Cruise Control/Vehicle Speed, 0x00FEF1=65,265, 100ms 71 | FMS_EEC2, // 1.3.2 Electronic Engine Controller #2, 0x00F003=61,443, 50ms 72 | FMS_DC1, // 1.3.3 Door Control 1, 0x00FE4E=65,102, 100ms 73 | FMS_DC2, // 1.3.4 Door Control 2, 0x00FDA5=64,933, 100ms 74 | FMS_TD, // 1.3.5 Time/Date, 0x00FEE6=65,254, 1000ms 75 | FMS_AS, // 1.3.6 Alternator Speed, 0x00FED5=65,237, 1000ms 76 | FMS_ETC2 // 1.3.7 Electronic Transmission Controller 2, 0x00F005=61,445, 1000ms 77 | #endif 78 | }; 79 | 80 | 81 | void *FuelConsumption_ptr; 82 | void *DashDisplay_ptr; 83 | void *EngSpeed_ptr; 84 | void *WatTemp_ptr; 85 | void *FaultCode_ptr; 86 | 87 | 88 | U16 FuelConsumption; 89 | U16 DashDisplay; 90 | U16 EngSpeed; 91 | U08 WatTemp; 92 | FAULT_CODE_T FaultCode; 93 | 94 | 95 | //======================================================================================== 96 | // Application Layer Interface Functions 97 | //======================================================================================== 98 | 99 | void AL_init(void) 100 | { 101 | U08 i; 102 | 103 | EngSpeed = 0; 104 | WatTemp = 0; 105 | 106 | FaultCode.count = 0; 107 | 108 | // initialise the fault code table 109 | for (i = 0; i < J1939_FAULT_NUM; i++) 110 | { 111 | FaultCode.DTC[i].LS = 0; 112 | FaultCode.DTC[i].FS = 0; 113 | FaultCode.DTC[i].SPN = 0; 114 | FaultCode.DTC[i].FMI = 0; 115 | FaultCode.DTC[i].OC = 0; 116 | FaultCode.DTC[i].CM = 0; 117 | } 118 | } 119 | 120 | 121 | void AL_periodic(void) 122 | { 123 | } 124 | 125 | 126 | void AL_process(J1939_RX_MESSAGE_T *msg_ptr) 127 | { 128 | int i, j; 129 | 130 | #if J1939_AL_DEBUG 131 | //printf("AL_process(RX) SA=%02X, DA=%02X, PGN=%8X\n", msg_ptr->source_addr, msg_ptr->dest_addr, msg_ptr->PGN); 132 | #endif 133 | 134 | switch (msg_ptr->PGN) 135 | { 136 | case FMS_LFC: 137 | FuelConsumption = (msg_ptr->data[4] << 8) + msg_ptr->data[3]; 138 | //FuelConsumption_ptr = &FuelConsumption; 139 | #if J1939_AL_DEBUG 140 | printf("FMS_LFC Fuel Consumption=%d\n", FuelConsumption); 141 | #endif 142 | break; 143 | 144 | case FMS_DD: 145 | DashDisplay = (msg_ptr->data[4] << 8) + msg_ptr->data[3]; 146 | //DashDisplay_ptr = &DashDisplay; 147 | #if J1939_AL_DEBUG 148 | printf("FMS_DD DashDisplay=%d\n", DashDisplay); 149 | #endif 150 | break; 151 | 152 | case FMS_EEC1: 153 | EngSpeed = (msg_ptr->data[4] << 8) + msg_ptr->data[3]; 154 | //EngSpeed_ptr = &EngSpeed; 155 | #if J1939_AL_DEBUG 156 | printf("FMS_EEC1 EngSpeed=%d\n", EngSpeed); 157 | #endif 158 | break; 159 | 160 | case FMS_EEC2: 161 | #if J1939_AL_DEBUG 162 | printf("FMS_EEC2\n"); 163 | #endif 164 | break; 165 | 166 | case FMS_ET1: 167 | WatTemp = msg_ptr->data[0]; 168 | //WatTemp_ptr = &WatTemp; 169 | #if J1939_AL_DEBUG 170 | printf("FMS_ET1 WatTemp=%d\n", WatTemp); 171 | #endif 172 | break; 173 | 174 | #if MUTIPLE_PACKAGE_TRANSPORT 175 | case J1939_DM1: 176 | FaultCode.count = (U08)((msg_ptr->byte_count - 2) / 4); 177 | 178 | if (FaultCode.count > J1939_FAULT_NUM) 179 | { 180 | j = J1939_FAULT_NUM; 181 | } 182 | else 183 | { 184 | j = FaultCode.count; 185 | } 186 | 187 | for (i = 0; i < j; i++) 188 | { 189 | FaultCode.DTC[i].LS = msg_ptr->data[0 + i * 4]; 190 | FaultCode.DTC[i].FS = msg_ptr->data[1 + i * 4]; 191 | FaultCode.DTC[i].SPN = msg_ptr->data[4 + i * 4] & 0xE0; 192 | FaultCode.DTC[i].SPN = FaultCode.DTC[i].SPN << 3; 193 | FaultCode.DTC[i].SPN += msg_ptr->data[3 + i * 4]; 194 | FaultCode.DTC[i].SPN = (FaultCode.DTC[i].SPN << 8) + msg_ptr->data[2 + i * 4]; 195 | FaultCode.DTC[i].FMI = msg_ptr->data[4 + i * 4] & 0x1F; 196 | FaultCode.DTC[i].OC = msg_ptr->data[5 + i * 4] & 0x7F; 197 | FaultCode.DTC[i].CM = (msg_ptr->data[5 + i * 4] & 0x80) >> 7; 198 | #if J1939_AL_DEBUG 199 | printf("DM1 SPN=%d\n", FaultCode.DTC[i].SPN); 200 | #endif 201 | } 202 | //FaultCode_ptr = &FaultCode; 203 | break; 204 | #endif 205 | 206 | default: 207 | break; 208 | } 209 | } 210 | 211 | 212 | U08 PGN_filter(PGN_T pgn) 213 | { 214 | U08 left = 0; 215 | U08 right = PGN_NUM - 1; 216 | U08 middle = 0; 217 | 218 | while (left <= right) 219 | { 220 | middle = (U08)((left + right) >> 1); 221 | 222 | if (pgn == _pgn_table[middle]) 223 | { 224 | return TRUE; 225 | } 226 | 227 | if (pgn > _pgn_table[middle]) 228 | { 229 | left = middle + 1; 230 | } 231 | else 232 | { 233 | right = middle - 1; 234 | } 235 | } 236 | return FALSE; 237 | } 238 | -------------------------------------------------------------------------------- /J1939_AL.h: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////////////////// 2 | // File: J1939_AL.h 3 | // Module: J1939 stack 4 | // Description: Defines for the J1939 stack Application Layer 5 | // Originator: MB 6 | // Derived from: Freescale J1939 stack 7 | // Date created: 09 June 2014 8 | //--------------------------------------------------------------------------------------- 9 | // Revision Log: 10 | // $Log: $ 11 | // 12 | //--------------------------------------------------------------------------------------- 13 | // Notes: 14 | // 15 | //--------------------------------------------------------------------------------------- 16 | ///////////////////////////////////////////////////////////////////////////////////////// 17 | // Copyright (C)2014 by Timespace Technology Ltd. All rights reserved. 18 | // 19 | // This software is confidential. It is not to be copied or distributed in any form 20 | // to any third party. 21 | // 22 | // This software is provided by Timespace Technology on an 'as is' basis, and Timespace 23 | // Technology expressly disclaims any and all warranties, expressed or implied including, 24 | // without limitation, warranties of merchantability and fitness for a particular 25 | // purpose. In no event shall Timespace Technology be liable for any direct, indirect, 26 | // incidental, punitive or consequential damages - of any kind whatsoever - with respect 27 | // to the software. 28 | ///////////////////////////////////////////////////////////////////////////////////////// 29 | 30 | 31 | #ifndef _J1939_AL_H_ 32 | #define _J1939_AL_H_ 33 | 34 | // DTC - Diagnostic Trouble Code 35 | typedef struct 36 | { 37 | U08 LS; // Lamp Status 38 | U08 FS; // Flash Status 39 | U32 SPN; // suspect Parameter Number 40 | U08 FMI; // Failure Mode Index 41 | U08 OC; // Occurrence Count 42 | U08 CM; // Conversion Method 43 | }DTC_T; 44 | 45 | typedef struct 46 | { 47 | DTC_T DTC[J1939_FAULT_NUM]; 48 | U08 count; 49 | }FAULT_CODE_T; 50 | 51 | typedef struct 52 | { 53 | U32 trip_data; 54 | U32 total_data; 55 | }VEH_DISTANCE_T; 56 | 57 | 58 | //======================================================================================== 59 | // Application Layer Interface Functions 60 | //======================================================================================== 61 | 62 | void AL_init(void); 63 | void AL_process(J1939_RX_MESSAGE_T *msg_ptr); 64 | void AL_periodic(void); 65 | 66 | U08 PGN_filter(PGN_T pgn); 67 | 68 | #endif -------------------------------------------------------------------------------- /J1939_CFG.h: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////////////////// 2 | // File: J1939_CFG.h 3 | // Module: J1939 stack 4 | // Description: Defines for the J1939 stack configuration 5 | // Originator: MB 6 | // Derived from: Freescale J1939 stack 7 | // Date created: 09 June 2014 8 | //--------------------------------------------------------------------------------------- 9 | // Revision Log: 10 | // $Log: $ 11 | // 12 | //--------------------------------------------------------------------------------------- 13 | // Notes: 14 | // 15 | //--------------------------------------------------------------------------------------- 16 | ///////////////////////////////////////////////////////////////////////////////////////// 17 | // Copyright (C)2014 by Timespace Technology Ltd. All rights reserved. 18 | // 19 | // This software is confidential. It is not to be copied or distributed in any form 20 | // to any third party. 21 | // 22 | // This software is provided by Timespace Technology on an 'as is' basis, and Timespace 23 | // Technology expressly disclaims any and all warranties, expressed or implied including, 24 | // without limitation, warranties of merchantability and fitness for a particular 25 | // purpose. In no event shall Timespace Technology be liable for any direct, indirect, 26 | // incidental, punitive or consequential damages - of any kind whatsoever - with respect 27 | // to the software. 28 | ///////////////////////////////////////////////////////////////////////////////////////// 29 | 30 | 31 | #ifndef _J1939_CFG_H_ 32 | #define _J1939_CFG_H_ 33 | 34 | // debug conditional compilation switches 35 | #define J1939_HAL_DEBUG 1 // debug switch for the Hardware Abstraction Layer 36 | #define J1939_DL_DEBUG 1 // debug switch for the Datalink Layer 37 | #define J1939_TL_DEBUG 1 // debug switch for the Transport Layer 38 | #define J1939_NML_DEBUG 1 // debug switch for the Network Management Layer 39 | #define J1939_AL_DEBUG 1 // debug switch for the Application Layer 40 | 41 | 42 | // code conditional compilation switches 43 | #define MUTIPLE_PACKAGE_TRANSPORT 1 // 1:enable; 0:disable 44 | #define PGN_24_BIT 1 // 1:enable; 0:disable 45 | #define PGN_FILTERING 0 // 1:enable; 0:disable 46 | 47 | #define PGN_NUM 23 48 | #define TICK 20 // 20ms 49 | #define NUMBER_TRANS_RX_MACHINES 50 | #define J1939_MAX_MESSAGE_LENGTH 1785 51 | #define NUMBER_TRANS_RX_BUFFERS 64 52 | #define NUMBER_TRANS_TX_BUFFERS 8 53 | #define NUMBER_PDU_BUFFERS 8 54 | #define BIG_E 0 // 1:Big-Endian 0:Little-Endian 55 | #define OUT_BUFFER_SIZE 21 56 | #define IN_BUFFER_SIZE 21 57 | #define CAN_MAX_BYTE_COUNT 8 58 | #define MIN_PDU2 240 59 | #define ADDR_NUM 2 60 | //#define J1939ParDaraNum 62 61 | 62 | #define J1939_FAULT_NUM (NUMBER_TRANS_RX_BUFFERS-2)/4 63 | 64 | #define NODEADDR 0x23 65 | #define GLOBADDR 0xFF 66 | #define NULLADDR 0xFE 67 | 68 | 69 | //================================ typedefs =============================== 70 | #if PGN_24_BIT // allow full 24 bit PGN 71 | typedef volatile unsigned long PGN_T; // unsigned 32 bit definition 72 | #else // allow only 16 bit PGN 73 | typedef volatile unsigned short PGN_T; // unsigned 16 bit definition 74 | #endif 75 | 76 | typedef unsigned char U08; // unsigned 8 bit definition 77 | typedef unsigned short U16; // unsigned 16 bit definition 78 | typedef unsigned long U32; // unsigned 32 bit definition 79 | 80 | typedef volatile unsigned char VU08; // volatile unsigned 8 bit definition 81 | typedef volatile unsigned short VU16; // volatile unsigned 16 bit definition 82 | typedef volatile unsigned long VU32; // volatile unsigned 32 bit definition 83 | 84 | typedef signed char S08; // signed 8 bit definition 85 | typedef signed short S16; // signed 16 bit definition 86 | typedef signed long S32; // signed 32 bit definition 87 | 88 | typedef volatile signed char VS08; // volatile signed 8 bit definition 89 | typedef volatile signed short VS16; // volatile signed 16 bit definition 90 | typedef volatile signed long VS32; // volatile signed 32 bit definition 91 | 92 | #endif 93 | -------------------------------------------------------------------------------- /J1939_DL.c: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////////////////// 2 | // File: J1939_DL.c 3 | // Module: J1939 stack 4 | // Description: J1939 stack Application Layer 5 | // Originator: MB 6 | // Derived from: Freescale J1939 stack 7 | // Date created: 09 June 2014 8 | //--------------------------------------------------------------------------------------- 9 | // Revision Log: 10 | // $Log: $ 11 | // 12 | //--------------------------------------------------------------------------------------- 13 | // Notes: 14 | // 15 | //--------------------------------------------------------------------------------------- 16 | ///////////////////////////////////////////////////////////////////////////////////////// 17 | // Copyright (C)2014 by Timespace Technology Ltd. All rights reserved. 18 | // 19 | // This software is confidential. It is not to be copied or distributed in any form 20 | // to any third party. 21 | // 22 | // This software is provided by Timespace Technology on an 'as is' basis, and Timespace 23 | // Technology expressly disclaims any and all warranties, expressed or implied including, 24 | // without limitation, warranties of merchantability and fitness for a particular 25 | // purpose. In no event shall Timespace Technology be liable for any direct, indirect, 26 | // incidental, punitive or consequential damages - of any kind whatsoever - with respect 27 | // to the software. 28 | ///////////////////////////////////////////////////////////////////////////////////////// 29 | 30 | 31 | #include "J1939_includes.h" 32 | 33 | 34 | CAN_PACKET_T in_buffer[IN_BUFFER_SIZE]; 35 | CAN_PACKET_T out_buffer[OUT_BUFFER_SIZE]; 36 | 37 | RING_T ring_in; 38 | RING_T ring_out; 39 | 40 | U08 DL_state; 41 | 42 | 43 | //======================================================================================== 44 | // Datalink Layer Interface Functions 45 | //======================================================================================== 46 | 47 | void DL_init(void) 48 | { 49 | U08 i, j; 50 | 51 | // initialise inbound buffer 52 | for (i = 0; i < IN_BUFFER_SIZE; i++) 53 | { 54 | in_buffer[i].byte_count = 0; 55 | in_buffer[i].identifier = 0; 56 | 57 | for (j = 0; j < CAN_MAX_BYTE_COUNT; j++) 58 | { 59 | in_buffer[i].data[j] = 0; 60 | } 61 | } 62 | 63 | // initialise outbound buffer 64 | for (i = 0; i < OUT_BUFFER_SIZE; i++) 65 | { 66 | out_buffer[i].byte_count = 0; 67 | out_buffer[i].identifier = 0; 68 | 69 | for (j = 0; j < CAN_MAX_BYTE_COUNT; j++) 70 | { 71 | out_buffer[i].data[j] = 0; 72 | } 73 | } 74 | 75 | // Initialise inbound ring buffer 76 | ring_in.buffer = &in_buffer[0]; 77 | ring_in.head = 0; 78 | ring_in.tail = 0; 79 | ring_in.buffer_size = IN_BUFFER_SIZE - 1; 80 | 81 | // Initialise outbound ring buffer 82 | ring_out.buffer = &out_buffer[0]; 83 | ring_out.head = 0; 84 | ring_out.tail = 0; 85 | ring_out.buffer_size = OUT_BUFFER_SIZE - 1; 86 | 87 | DL_state = NOTPRIMED; 88 | 89 | return; 90 | } 91 | 92 | 93 | //#pragma CODE_SEG NON_BANKED 94 | void DL_process(const CAN_PACKET_T *pkt_ptr) 95 | { 96 | ring_enqueue(*pkt_ptr, &ring_in); 97 | return; 98 | } 99 | 100 | 101 | //#pragma CODE_SEG NON_BANKED 102 | void DL_periodic(void) 103 | { 104 | U08 m_PRI, m_PF, m_PS, m_DA, m_SA, i; 105 | U32 m_ID; 106 | PGN_T m_PGN; 107 | CAN_PACKET_T *rpkt_ptr; 108 | CAN_PACKET_T *tpkt_ptr; 109 | J1939_RX_PDU_T pdu; 110 | 111 | m_DA = 0; 112 | m_SA = 0; 113 | m_PF = 0; 114 | m_ID = 0; 115 | m_PGN = 0; 116 | 117 | i = 0; 118 | 119 | // handle outbound packets 120 | tpkt_ptr = ring_dequeue(&ring_out); 121 | 122 | // if outbound ring buffer is not empty 123 | if (tpkt_ptr != NULL) 124 | { 125 | HAL_tx(tpkt_ptr); 126 | } 127 | 128 | 129 | // handle inbound packets 130 | while (TRUE) 131 | { 132 | rpkt_ptr = ring_dequeue(&ring_in); 133 | 134 | // if inbound ring buffer is empty 135 | if (rpkt_ptr == NULL) 136 | { 137 | break; 138 | } 139 | else 140 | { 141 | m_ID = rpkt_ptr->identifier; // extract full 29 bit identifier from CAN packet 142 | m_PRI = (PGN_T)((m_ID >> 26) & 0x7); // rotate and mask bits 28-26 of CAN ID - J1939 Priority 143 | #if PGN_24_BIT 144 | m_PGN = (PGN_T)((m_ID >> 8) & 0x3FFFF); // rotate and mask bits 25-8 of CAN ID and then add top 6 zero bits by extending to 32 bits - J1939 PGN 145 | #else 146 | m_PGN = (PGN_T)(m_ID >> 8); // rotate and mask bits 23-8 of CAN ID - J1939 PGN 147 | #endif 148 | m_PF = (U08)(m_ID >> 16); // rotate and mask bits 23-16 of CAN ID - J1939 PDU Format 149 | m_PS = (U08)(m_ID >> 8); // mask bits 15-8 of CAN ID - J1939 PDU Specific 150 | m_DA = m_PS; // J1939 Destination Address = J1939 PDU Specific 151 | m_SA = (U08)m_ID; // mask bits 7-0 of CAN ID - J1939 Source Address 152 | 153 | // Handle PDU1 154 | if (m_PF < 240) 155 | { 156 | if (m_DA == GLOBADDR) 157 | { 158 | pdu.PGN = m_PGN & 0xFF00; 159 | pdu.byte_count = rpkt_ptr->byte_count; 160 | pdu.dest_addr = GLOBADDR; 161 | pdu.source_addr = m_SA; 162 | 163 | for (i = 0; i < pdu.byte_count; i++) 164 | { 165 | pdu.data[i] = rpkt_ptr->data[i]; 166 | } 167 | } 168 | else if (m_DA == NODEADDR) 169 | { 170 | pdu.PGN = m_PGN & 0xFF00; 171 | pdu.byte_count = rpkt_ptr->byte_count; 172 | pdu.dest_addr = m_DA; 173 | pdu.source_addr = m_SA; 174 | 175 | for (i = 0; i < pdu.byte_count; i++) 176 | { 177 | pdu.data[i] = rpkt_ptr->data[i]; 178 | } 179 | } 180 | else 181 | { 182 | continue; 183 | } 184 | } 185 | // Handle PDU2 186 | else 187 | { 188 | pdu.PGN = m_PGN; 189 | pdu.byte_count = rpkt_ptr->byte_count; 190 | pdu.dest_addr = GLOBADDR; 191 | pdu.source_addr = m_SA; 192 | 193 | for (i = 0; i < pdu.byte_count; i++) 194 | { 195 | pdu.data[i] = rpkt_ptr->data[i]; 196 | } 197 | } 198 | } 199 | // send PDU up the stack to the Transport Layer 200 | TL_process(&pdu); 201 | } 202 | return; 203 | } 204 | 205 | 206 | const CAN_PACKET_T* Req_CANpkt(void) 207 | { 208 | CAN_PACKET_T *pkt_ptr; 209 | 210 | pkt_ptr = ring_dequeue(&ring_out); 211 | 212 | if (pkt_ptr == NULL) 213 | { 214 | DL_state = NOTPRIMED; 215 | return NULL; 216 | } 217 | else 218 | { 219 | DL_state = PRIMED; 220 | return pkt_ptr; 221 | } 222 | } 223 | 224 | 225 | void Build_CANpkt(J1939_TX_MESSAGE_T *msg_ptr, U08 tflag) 226 | { 227 | CAN_PACKET_T pkt_ptr; 228 | U08 i; 229 | 230 | if (tflag == 0) 231 | { 232 | pkt_ptr.byte_count = (U08)msg_ptr->byte_count; 233 | pkt_ptr.identifier = msg_ptr->priority; 234 | pkt_ptr.identifier = (pkt_ptr.identifier << 18) + msg_ptr->PGN; 235 | 236 | // PDU1 - Peer-to-Peer 237 | if (msg_ptr->PGN < 0xF000) 238 | { 239 | pkt_ptr.identifier = pkt_ptr.identifier + msg_ptr->dest_addr; 240 | } 241 | pkt_ptr.identifier = (pkt_ptr.identifier << 8) + NODEADDR; 242 | 243 | for (i = 0; i < pkt_ptr.byte_count; i++) 244 | { 245 | pkt_ptr.data[i] = msg_ptr->data[i]; 246 | } 247 | 248 | ring_enqueue(pkt_ptr, &ring_out); 249 | 250 | DL_state = PRIMED; 251 | } 252 | else 253 | { 254 | } 255 | return; 256 | } 257 | 258 | 259 | //#pragma CODE_SEG NON_BANKED 260 | void ring_enqueue(CAN_PACKET_T msg, RING_T *ring) 261 | { 262 | // if ring buffer is full less one 263 | if (((ring->tail+1) == ring->head) || ((ring->tail == ring->buffer_size) && (ring->head == 0))) 264 | { 265 | return; 266 | } 267 | else 268 | { 269 | ring->buffer[ring->tail] = msg; // enqueue CAN message to tail 270 | ring->tail++; 271 | 272 | // if tail pointer is at end of ring buffer 273 | if (ring->tail > ring->buffer_size) 274 | { 275 | ring->tail = 0; // wrap to start of ring buffer->0 276 | } 277 | return; 278 | } 279 | } 280 | 281 | 282 | //#pragma CODE_SEG NON_BANKED 283 | CAN_PACKET_T *ring_dequeue(RING_T *ring) 284 | { 285 | CAN_PACKET_T *temp; 286 | 287 | // if ring buffer is empty 288 | if (ring->head == ring->tail) 289 | { 290 | return NULL; 291 | } 292 | else 293 | { 294 | temp = &ring->buffer[ring->head]; // dequeue CAN message from head 295 | ring->head++; 296 | 297 | // if head pointer is at end of ring buffer 298 | if (ring->head > ring->buffer_size) 299 | { 300 | ring->head = 0; // wrap to start of ring buffer->0 301 | } 302 | return temp; 303 | } 304 | } 305 | -------------------------------------------------------------------------------- /J1939_DL.h: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////////////////// 2 | // File: J1939_DL.h 3 | // Module: J1939 stack 4 | // Description: Defines for the J1939 stack Datalink Layer 5 | // Originator: MB 6 | // Derived from: Freescale J1939 stack 7 | // Date created: 09 June 2014 8 | //--------------------------------------------------------------------------------------- 9 | // Revision Log: 10 | // $Log: $ 11 | // 12 | //--------------------------------------------------------------------------------------- 13 | // Notes: 14 | // 15 | //--------------------------------------------------------------------------------------- 16 | ///////////////////////////////////////////////////////////////////////////////////////// 17 | // Copyright (C)2014 by Timespace Technology Ltd. All rights reserved. 18 | // 19 | // This software is confidential. It is not to be copied or distributed in any form 20 | // to any third party. 21 | // 22 | // This software is provided by Timespace Technology on an 'as is' basis, and Timespace 23 | // Technology expressly disclaims any and all warranties, expressed or implied including, 24 | // without limitation, warranties of merchantability and fitness for a particular 25 | // purpose. In no event shall Timespace Technology be liable for any direct, indirect, 26 | // incidental, punitive or consequential damages - of any kind whatsoever - with respect 27 | // to the software. 28 | ///////////////////////////////////////////////////////////////////////////////////////// 29 | 30 | 31 | #ifndef _J1939_DL_H_ 32 | #define _J1939_DL_H_ 33 | 34 | 35 | //========================================================================================== 36 | // Datalink Layer Interface Functions 37 | //========================================================================================== 38 | 39 | void DL_init(void); 40 | void DL_process(const CAN_PACKET_T* pkt_ptr); 41 | void DL_periodic(void); 42 | 43 | const CAN_PACKET_T* Req_CANpkt(void); 44 | void Build_CANpkt(J1939_TX_MESSAGE_T *msg_ptr, U08 tflag); 45 | 46 | 47 | //======================================================================================== 48 | //Datalink Module Data Objects 49 | //======================================================================================== 50 | void ring_enqueue(CAN_PACKET_T msg, RING_T *ring); 51 | CAN_PACKET_T *ring_dequeue(RING_T *ring); 52 | 53 | #endif -------------------------------------------------------------------------------- /J1939_HAL.c: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////////////////// 2 | // File: J1939_HAL.c 3 | // Module: J1939 stack 4 | // Description: J1939 stack Hardware Abstraction Layer 5 | // Originator: MB 6 | // Derived from: Freescale J1939 stack 7 | // Date created: 09 June 2014 8 | //--------------------------------------------------------------------------------------- 9 | // Revision Log: 10 | // $Log: $ 11 | // 12 | //--------------------------------------------------------------------------------------- 13 | // Notes: 14 | // 15 | //--------------------------------------------------------------------------------------- 16 | ///////////////////////////////////////////////////////////////////////////////////////// 17 | // Copyright (C)2014 by Timespace Technology Ltd. All rights reserved. 18 | // 19 | // This software is confidential. It is not to be copied or distributed in any form 20 | // to any third party. 21 | // 22 | // This software is provided by Timespace Technology on an 'as is' basis, and Timespace 23 | // Technology expressly disclaims any and all warranties, expressed or implied including, 24 | // without limitation, warranties of merchantability and fitness for a particular 25 | // purpose. In no event shall Timespace Technology be liable for any direct, indirect, 26 | // incidental, punitive or consequential damages - of any kind whatsoever - with respect 27 | // to the software. 28 | ///////////////////////////////////////////////////////////////////////////////////////// 29 | 30 | 31 | #include "J1939_includes.h" 32 | 33 | // this module is just a wrapper to the real HAL layer 34 | // select which HAL layer to use 35 | #define HAL_MODE_PC 1 36 | #define HAL_MODE_X300 2 37 | #define HAL_MODE_V400 3 38 | #define HAL_MODE HAL_MODE_PC 39 | 40 | CAN_PACKET_T rev_can_packet; 41 | 42 | #if (HAL_MODE == HAL_MODE_PC) 43 | #if defined(_Windows) || defined(_MSC_VER) || defined (__GNUC__) 44 | #define STRICT 45 | #include 46 | #endif 47 | 48 | #include 49 | 50 | #define UNUSED_PARAM(a) { a=a; } 51 | 52 | #define RECEIVE_EVENT_SIZE 1 // DO NOT EDIT! Currently 1 is supported only 53 | #define RX_QUEUE_SIZE 4096 // internal driver queue size in CAN events 54 | 55 | 56 | #include "vxlapi.h" 57 | 58 | ///////////////////////////////////////////////////////////////////////////// 59 | // globals 60 | 61 | extern char g_AppName[XL_MAX_LENGTH + 1]; // Application name which is displayed in VHWconf 62 | extern XLportHandle g_xlPortHandle; // Global porthandle (we use only one!) 63 | extern XLdriverConfig g_xlDrvConfig; // Contains the actual hardware configuration 64 | extern XLaccess g_xlChannelMask; // Global channelmask (includes all founded channels) 65 | extern XLaccess g_xlPermissionMask; // Global permissionmask (includes all founded channels) 66 | extern unsigned int g_BaudRate; // Default baudrate 67 | extern int g_silent; // flag to visualize the message events (on/off) 68 | extern unsigned int g_TimerRate; // Global timerrate (to toggel) 69 | extern XLaccess g_xlChanMaskTx; // Global transmit channel mask 70 | 71 | // thread variables 72 | extern XLhandle g_hMsgEvent; // notification handle for the receive queue 73 | extern HANDLE g_hRXThread; // thread handle (RX) 74 | extern HANDLE g_hTXThread; // thread handle (TX) 75 | extern int g_RXThreadRun; // flag to start/stop the RX thread 76 | extern int g_TXThreadRun; // flag to start/stop the TX thread (for the transmission burst) 77 | #elif (HAL_MODE == HAL_MODE_X300) 78 | #elif (HAL_MODE == HAL_MODE_V400) 79 | #endif 80 | 81 | 82 | //========================================================================================== 83 | // Hardware Abstraction Layer Interface Functions 84 | //========================================================================================== 85 | 86 | void HAL_init(void) 87 | { 88 | #if HAL_MODE == HAL_MODE_PC 89 | #elif HAL_MODE == HAL_MODE_X300 90 | BSP_CAN_init(); 91 | #elif HAL_MODE == HAL_MODE_V400 92 | BSP_CAN_init(); 93 | #else 94 | #error HAL layer not selected 95 | #endif 96 | } 97 | 98 | 99 | void HAL_periodic(void) 100 | { 101 | // forward declaration 102 | void HAL_rx(void); 103 | // poll the receive port 104 | HAL_rx(); 105 | } 106 | 107 | 108 | void HAL_tx(const CAN_PACKET_T *pkt_ptr) 109 | { 110 | #if HAL_MODE == HAL_MODE_PC 111 | static XLevent xlEvent; 112 | XLstatus xlStatus; 113 | unsigned int messageCount = 1; 114 | VU32 id; 115 | 116 | memset(&xlEvent, 0, sizeof(xlEvent)); 117 | 118 | id = pkt_ptr->identifier; 119 | if (id > 0x7FF) // if identifier is greater than 11 bits 120 | id |= 0x80000000; // then set bit 31 of identifier to tell HAL layer to send extended frame 121 | 122 | xlEvent.tag = XL_TRANSMIT_MSG; 123 | xlEvent.tagData.msg.id = id; 124 | xlEvent.tagData.msg.dlc = pkt_ptr->byte_count; 125 | xlEvent.tagData.msg.flags = 0; 126 | xlEvent.tagData.msg.data[0] = pkt_ptr->data[0]; 127 | xlEvent.tagData.msg.data[1] = pkt_ptr->data[1]; 128 | xlEvent.tagData.msg.data[2] = pkt_ptr->data[2]; 129 | xlEvent.tagData.msg.data[3] = pkt_ptr->data[3]; 130 | xlEvent.tagData.msg.data[4] = pkt_ptr->data[4]; 131 | xlEvent.tagData.msg.data[5] = pkt_ptr->data[5]; 132 | xlEvent.tagData.msg.data[6] = pkt_ptr->data[6]; 133 | xlEvent.tagData.msg.data[7] = pkt_ptr->data[7]; 134 | g_TXThreadRun = 1; 135 | 136 | xlStatus = xlCanTransmit(g_xlPortHandle, g_xlChanMaskTx, &messageCount, &xlEvent); 137 | 138 | #elif HAL_MODE == HAL_MODE_X300 139 | #elif HAL_MODE == HAL_MODE_V400 140 | #endif 141 | 142 | #if J1939_HAL_DEBUG 143 | printf("- Transmit : CM(0x%I64x), %s\n", g_xlChanMaskTx, xlGetErrorString(xlStatus)); 144 | #endif 145 | } 146 | 147 | 148 | // MB removed - #pragma TRAP_PROC 149 | void HAL_rx(void) 150 | { 151 | #if HAL_MODE == HAL_MODE_PC 152 | XLstatus xlStatus; 153 | 154 | unsigned int msgsrx = RECEIVE_EVENT_SIZE; 155 | XLevent xlEvent; 156 | 157 | g_RXThreadRun = 1; 158 | 159 | //while (g_RXThreadRun) 160 | { 161 | WaitForSingleObject(g_hMsgEvent,10); 162 | 163 | xlStatus = XL_SUCCESS; 164 | 165 | while (!xlStatus) 166 | { 167 | msgsrx = RECEIVE_EVENT_SIZE; 168 | xlStatus = xlReceive(g_xlPortHandle, &msgsrx, &xlEvent); 169 | 170 | if (xlStatus != XL_ERR_QUEUE_IS_EMPTY) 171 | { 172 | CAN_PACKET_T rev_can_packet; 173 | 174 | #if J1939_HAL_DEBUG 175 | if (!g_silent) 176 | { 177 | printf("%s\n", xlGetEventString(&xlEvent)); 178 | } 179 | #endif 180 | 181 | rev_can_packet.identifier = xlEvent.tagData.msg.id; 182 | rev_can_packet.data[0] = xlEvent.tagData.msg.data[0]; 183 | rev_can_packet.data[1] = xlEvent.tagData.msg.data[1]; 184 | rev_can_packet.data[2] = xlEvent.tagData.msg.data[2]; 185 | rev_can_packet.data[3] = xlEvent.tagData.msg.data[3]; 186 | rev_can_packet.data[4] = xlEvent.tagData.msg.data[4]; 187 | rev_can_packet.data[5] = xlEvent.tagData.msg.data[5]; 188 | rev_can_packet.data[6] = xlEvent.tagData.msg.data[6]; 189 | rev_can_packet.data[7] = xlEvent.tagData.msg.data[7]; 190 | rev_can_packet.byte_count = (VU08)(xlEvent.tagData.msg.dlc); 191 | DL_process(&rev_can_packet); 192 | //ResetEvent(g_hMsgEvent); 193 | } 194 | } 195 | } 196 | #elif HAL_MODE == HAL_MODE_X300 197 | #elif HAL_MODE == HAL_MODE_V400 198 | #endif 199 | } 200 | 201 | // MB removed - #pragma TRAP_PROC 202 | void HAL_CAN_error(void) 203 | { 204 | #if NOT_YET 205 | //DisableInterrupts; 206 | //OS_PUSH_PPAGE(); 207 | OSIntEnter(); 208 | OS_SAVE_SP(); 209 | 210 | if(CAN1RFLG_CSCIF!=0) 211 | { 212 | CAN1RFLG_CSCIF = 1; 213 | PTL = 0xFF; 214 | HAL_init(); 215 | } 216 | else if(CAN1RFLG_OVRIF!=0) 217 | { 218 | CAN1RFLG_OVRIF = 1; 219 | PTL = 0xFF; 220 | HAL_init(); 221 | } 222 | 223 | OSIntExit(); 224 | //OS_POP_PPAGE(); 225 | #endif 226 | } 227 | -------------------------------------------------------------------------------- /J1939_HAL.h: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////////////////// 2 | // File: J1939_HAL.h 3 | // Module: J1939 stack 4 | // Description: Defines for the J1939 stack Hardware Abstraction Layer 5 | // Originator: MB 6 | // Derived from: Freescale J1939 stack 7 | // Date created: 09 June 2014 8 | //--------------------------------------------------------------------------------------- 9 | // Revision Log: 10 | // $Log: $ 11 | // 12 | //--------------------------------------------------------------------------------------- 13 | // Notes: 14 | // 15 | //--------------------------------------------------------------------------------------- 16 | ///////////////////////////////////////////////////////////////////////////////////////// 17 | // Copyright (C)2014 by Timespace Technology Ltd. All rights reserved. 18 | // 19 | // This software is confidential. It is not to be copied or distributed in any form 20 | // to any third party. 21 | // 22 | // This software is provided by Timespace Technology on an 'as is' basis, and Timespace 23 | // Technology expressly disclaims any and all warranties, expressed or implied including, 24 | // without limitation, warranties of merchantability and fitness for a particular 25 | // purpose. In no event shall Timespace Technology be liable for any direct, indirect, 26 | // incidental, punitive or consequential damages - of any kind whatsoever - with respect 27 | // to the software. 28 | ///////////////////////////////////////////////////////////////////////////////////////// 29 | 30 | 31 | #ifndef _J1939_HAL_H_ 32 | #define _J1939_HAL_H_ 33 | 34 | //========================================================================================== 35 | // Hardware Abstraction Layer Interface Functions 36 | //========================================================================================== 37 | 38 | void HAL_init(void); 39 | void HAL_periodic(void); 40 | 41 | void HAL_tx(const CAN_PACKET_T *pkt_ptr); 42 | 43 | #endif -------------------------------------------------------------------------------- /J1939_NML.c: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////////////////// 2 | // File: J1939_NML.c 3 | // Module: J1939 stack 4 | // Description: J1939 stack Network Management Layer 5 | // Originator: MB 6 | // Derived from: Freescale J1939 stack 7 | // Date created: 09 June 2014 8 | //--------------------------------------------------------------------------------------- 9 | // Revision Log: 10 | // $Log: $ 11 | // 12 | //--------------------------------------------------------------------------------------- 13 | // Notes: 14 | // 15 | //--------------------------------------------------------------------------------------- 16 | ///////////////////////////////////////////////////////////////////////////////////////// 17 | // Copyright (C)2014 by Timespace Technology Ltd. All rights reserved. 18 | // 19 | // This software is confidential. It is not to be copied or distributed in any form 20 | // to any third party. 21 | // 22 | // This software is provided by Timespace Technology on an 'as is' basis, and Timespace 23 | // Technology expressly disclaims any and all warranties, expressed or implied including, 24 | // without limitation, warranties of merchantability and fitness for a particular 25 | // purpose. In no event shall Timespace Technology be liable for any direct, indirect, 26 | // incidental, punitive or consequential damages - of any kind whatsoever - with respect 27 | // to the software. 28 | ///////////////////////////////////////////////////////////////////////////////////////// 29 | 30 | 31 | #include "J1939_includes.h" 32 | 33 | 34 | //========================================================================================== 35 | // Network Management Layer Interface Functions 36 | //========================================================================================== 37 | 38 | void NML_init(void) 39 | { 40 | } 41 | 42 | 43 | void NML_process(J1939_RX_MESSAGE_T *msg_ptr) 44 | { 45 | if (msg_ptr->PGN == REQUEST_FOR_ADDRESS_CLAIMED) 46 | { 47 | } 48 | else if (msg_ptr->PGN == ADDRESS_CLAIMED) 49 | { 50 | } 51 | else if (msg_ptr->PGN == COMMANDED_ADDRESS) 52 | { 53 | } 54 | else 55 | { 56 | // send message up the stack to the Application Layer 57 | AL_process(msg_ptr); 58 | } 59 | } 60 | 61 | 62 | void NML_periodic(void) 63 | { 64 | } -------------------------------------------------------------------------------- /J1939_NML.h: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////////////////// 2 | // File: J1939_NML.h 3 | // Module: J1939 stack 4 | // Description: Defines for the J1939 stack Network Management Layer 5 | // Originator: MB 6 | // Derived from: Freescale J1939 stack 7 | // Date created: 09 June 2014 8 | //--------------------------------------------------------------------------------------- 9 | // Revision Log: 10 | // $Log: $ 11 | // 12 | //--------------------------------------------------------------------------------------- 13 | // Notes: 14 | // 15 | //--------------------------------------------------------------------------------------- 16 | ///////////////////////////////////////////////////////////////////////////////////////// 17 | // Copyright (C)2014 by Timespace Technology Ltd. All rights reserved. 18 | // 19 | // This software is confidential. It is not to be copied or distributed in any form 20 | // to any third party. 21 | // 22 | // This software is provided by Timespace Technology on an 'as is' basis, and Timespace 23 | // Technology expressly disclaims any and all warranties, expressed or implied including, 24 | // without limitation, warranties of merchantability and fitness for a particular 25 | // purpose. In no event shall Timespace Technology be liable for any direct, indirect, 26 | // incidental, punitive or consequential damages - of any kind whatsoever - with respect 27 | // to the software. 28 | ///////////////////////////////////////////////////////////////////////////////////////// 29 | 30 | 31 | #ifndef _J1939_NML_H_ 32 | #define _J1939_NML_H_ 33 | 34 | 35 | //========================================================================================== 36 | // Network Management Layer Interface Functions 37 | //========================================================================================== 38 | 39 | void NML_init(void); 40 | void NML_process(J1939_RX_MESSAGE_T *msg_ptr); 41 | void NML_periodic(void); 42 | 43 | #endif -------------------------------------------------------------------------------- /J1939_TL.c: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////////////////// 2 | // File: J1939_TL.c 3 | // Module: J1939 stack 4 | // Description: J1939 stack Transport Layer 5 | // Originator: MB 6 | // Derived from: Freescale J1939 stack 7 | // Date created: 09 June 2014 8 | //--------------------------------------------------------------------------------------- 9 | // Revision Log: 10 | // $Log: $ 11 | // 12 | //--------------------------------------------------------------------------------------- 13 | // Notes: 14 | // 15 | //--------------------------------------------------------------------------------------- 16 | ///////////////////////////////////////////////////////////////////////////////////////// 17 | // Copyright (C)2014 by Timespace Technology Ltd. All rights reserved. 18 | // 19 | // This software is confidential. It is not to be copied or distributed in any form 20 | // to any third party. 21 | // 22 | // This software is provided by Timespace Technology on an 'as is' basis, and Timespace 23 | // Technology expressly disclaims any and all warranties, expressed or implied including, 24 | // without limitation, warranties of merchantability and fitness for a particular 25 | // purpose. In no event shall Timespace Technology be liable for any direct, indirect, 26 | // incidental, punitive or consequential damages - of any kind whatsoever - with respect 27 | // to the software. 28 | ///////////////////////////////////////////////////////////////////////////////////////// 29 | 30 | 31 | #include "J1939_includes.h" 32 | 33 | 34 | static J1939_RX_MESSAGE_T J1939_rx_message; 35 | static J1939_RX_STATE_MACHINE_T J1939_rx_state_machine; 36 | static J1939_RX_PDU_T J1939_rx_pdu; 37 | 38 | 39 | //========================================================================================== 40 | // Transport Layer Interface Functions 41 | //========================================================================================== 42 | 43 | void TL_init(void) 44 | { 45 | U08 i; 46 | 47 | J1939_rx_message.PGN = 0; 48 | J1939_rx_message.dest_addr = 0; 49 | J1939_rx_message.source_addr = 0; 50 | J1939_rx_message.byte_count = 0; 51 | 52 | J1939_rx_pdu.PGN = 0; 53 | J1939_rx_pdu.dest_addr = 0; 54 | J1939_rx_pdu.source_addr = 0; 55 | J1939_rx_pdu.byte_count = 0; 56 | 57 | J1939_rx_state_machine.PGN = 0; 58 | J1939_rx_state_machine.dest_addr = 0; 59 | J1939_rx_state_machine.source_addr = 0; 60 | J1939_rx_state_machine.packet_number = 0; 61 | J1939_rx_state_machine.timer_counter = 0; 62 | J1939_rx_state_machine.total_packet_number = 0; 63 | J1939_rx_state_machine.byte_count = 0; 64 | J1939_rx_state_machine.status = WAIT_FOR_MESSAGE; 65 | J1939_rx_state_machine.TP = TP_NONE; 66 | 67 | 68 | for (i = 0; i < NUMBER_TRANS_RX_BUFFERS; i++) 69 | { 70 | J1939_rx_message.data[i] = 0; 71 | } 72 | 73 | for (i = 0; i < NUMBER_PDU_BUFFERS; i++) 74 | { 75 | J1939_rx_pdu.data[i] = 0; 76 | } 77 | } 78 | 79 | 80 | void TL_process(J1939_RX_PDU_T *pdu_ptr) 81 | { 82 | U08 i; 83 | 84 | if ((pdu_ptr->PGN == TP_CM) || (pdu_ptr->PGN == TP_DT)) 85 | { 86 | J1939_rx_pdu.PGN = pdu_ptr->PGN; 87 | J1939_rx_pdu.dest_addr = pdu_ptr->dest_addr; 88 | J1939_rx_pdu.source_addr = pdu_ptr->source_addr; 89 | J1939_rx_pdu.byte_count = pdu_ptr->byte_count; 90 | 91 | for (i = 0; i < NUMBER_PDU_BUFFERS; i++) 92 | { 93 | J1939_rx_pdu.data[i] = pdu_ptr->data[i]; 94 | } 95 | } 96 | else 97 | { 98 | J1939_RX_MESSAGE_T msg; 99 | 100 | msg.PGN = pdu_ptr->PGN; 101 | msg.dest_addr = pdu_ptr->dest_addr; 102 | msg.source_addr = pdu_ptr->source_addr; 103 | msg.byte_count = pdu_ptr->byte_count; 104 | 105 | //for (i = 0; i < NUMBER_TRANS_RX_BUFFERS; i++) 106 | for (i = 0; i < NUMBER_PDU_BUFFERS; i++) 107 | { 108 | msg.data[i] = pdu_ptr->data[i]; 109 | } 110 | // send message up the stack to the Network Management Layer 111 | NML_process(&msg); 112 | } 113 | 114 | return; 115 | } 116 | 117 | 118 | void TL_periodic(void) 119 | { 120 | U08 i; 121 | U08 go_on; 122 | 123 | go_on = TRUE; 124 | J1939_rx_state_machine.timer_counter++; 125 | 126 | while (go_on) 127 | { 128 | switch (J1939_rx_state_machine.status) 129 | { 130 | case WAIT_FOR_MESSAGE: 131 | if (J1939_rx_pdu.PGN == TP_CM && J1939_rx_pdu.dest_addr == GLOBADDR || J1939_rx_pdu.dest_addr == NODEADDR) 132 | { 133 | if (J1939_rx_pdu.data[0] == TP_CM_RTS) 134 | { 135 | J1939_rx_state_machine.PGN = J1939_rx_pdu.data[6]; 136 | J1939_rx_state_machine.PGN <<= 8; 137 | J1939_rx_state_machine.PGN += J1939_rx_pdu.data[5]; 138 | J1939_rx_state_machine.dest_addr = J1939_rx_pdu.dest_addr; 139 | J1939_rx_state_machine.source_addr = J1939_rx_pdu.source_addr; 140 | J1939_rx_state_machine.packet_number = 0; 141 | J1939_rx_state_machine.timer_counter = 0; 142 | J1939_rx_state_machine.total_packet_number = J1939_rx_pdu.data[3]; 143 | J1939_rx_state_machine.byte_count = J1939_rx_pdu.data[2]; 144 | J1939_rx_state_machine.byte_count <<= 8; 145 | J1939_rx_state_machine.byte_count += J1939_rx_pdu.data[1]; 146 | J1939_rx_state_machine.status = INIT_REASSEMBLE_STRUCTURE; 147 | J1939_rx_state_machine.TP = TP_CM_RTS; 148 | } 149 | else if (J1939_rx_pdu.data[0] == TP_CM_BAM) 150 | { 151 | J1939_rx_state_machine.PGN = J1939_rx_pdu.data[6]; 152 | J1939_rx_state_machine.PGN <<= 8; 153 | J1939_rx_state_machine.PGN += J1939_rx_pdu.data[5]; 154 | J1939_rx_state_machine.dest_addr = J1939_rx_pdu.dest_addr; 155 | J1939_rx_state_machine.source_addr = J1939_rx_pdu.source_addr; 156 | J1939_rx_state_machine.packet_number = 0; 157 | J1939_rx_state_machine.timer_counter = 0; 158 | J1939_rx_state_machine.total_packet_number = J1939_rx_pdu.data[3]; 159 | J1939_rx_state_machine.byte_count = J1939_rx_pdu.data[2]; 160 | J1939_rx_state_machine.byte_count <<= 8; 161 | J1939_rx_state_machine.byte_count += J1939_rx_pdu.data[1]; 162 | J1939_rx_state_machine.status = INIT_REASSEMBLE_STRUCTURE; 163 | J1939_rx_state_machine.TP = TP_CM_BAM; 164 | } 165 | else 166 | { 167 | go_on = FALSE;//break; 168 | } 169 | } 170 | else 171 | { 172 | go_on = FALSE;//break; 173 | } 174 | break; 175 | 176 | case INIT_REASSEMBLE_STRUCTURE: 177 | if (J1939_rx_state_machine.TP == TP_CM_RTS) 178 | { 179 | } 180 | else if (J1939_rx_state_machine.TP == TP_CM_BAM) 181 | { 182 | if (J1939_rx_state_machine.byte_count < NUMBER_TRANS_RX_BUFFERS) 183 | { 184 | J1939_rx_message.PGN = J1939_rx_state_machine.PGN; 185 | J1939_rx_message.dest_addr = J1939_rx_state_machine.dest_addr; 186 | J1939_rx_message.source_addr = J1939_rx_state_machine.source_addr; 187 | J1939_rx_message.byte_count = J1939_rx_state_machine.byte_count; 188 | 189 | for (i = 0; i < NUMBER_TRANS_RX_BUFFERS; i++) 190 | { 191 | J1939_rx_message.data[i] = 0; 192 | } 193 | J1939_rx_state_machine.status = WAIT_FOR_DATA; 194 | } 195 | else 196 | { 197 | J1939_rx_state_machine.status= WAIT_FOR_MESSAGE; 198 | J1939_rx_state_machine.timer_counter = 0; 199 | go_on = FALSE; 200 | //break; 201 | } 202 | } 203 | else 204 | { 205 | J1939_rx_state_machine.status = WAIT_FOR_MESSAGE; 206 | J1939_rx_state_machine.timer_counter = 0; 207 | go_on = FALSE; 208 | //break; 209 | } 210 | break; 211 | 212 | case CHECK_PACKET: 213 | break; 214 | 215 | case SEND_ABORT: 216 | break; 217 | 218 | case SEND_CTS_WITH_COUNT: 219 | break; 220 | 221 | case WAIT_FOR_DATA: 222 | if (J1939_rx_pdu.PGN == TP_DT) 223 | { 224 | J1939_rx_state_machine.status = CHECK_DATA_PACKET; 225 | //break; 226 | } 227 | else 228 | { 229 | J1939_rx_state_machine.status = CHECK_TIMER; 230 | //break; 231 | } 232 | break; 233 | 234 | case CHECK_TIMER: 235 | if (J1939_rx_state_machine.timer_counter > (750/TICK)) // 750ms 236 | { 237 | J1939_rx_state_machine.status = RESET_REASSEMBLY_STRUCTURE; 238 | //J1939_rx_state_machine.timer_counter = 0; 239 | //break; 240 | } 241 | else 242 | { 243 | J1939_rx_state_machine.status = WAIT_FOR_DATA; 244 | go_on = FALSE;//break; 245 | } 246 | break; 247 | 248 | case RESET_REASSEMBLY_STRUCTURE: 249 | J1939_rx_state_machine.PGN = 0; 250 | J1939_rx_state_machine.dest_addr = 0; 251 | J1939_rx_state_machine.source_addr = 0; 252 | J1939_rx_state_machine.packet_number = 0; 253 | J1939_rx_state_machine.timer_counter = 0; 254 | J1939_rx_state_machine.total_packet_number = 0; 255 | J1939_rx_state_machine.byte_count = 0; 256 | J1939_rx_message.PGN = 0; 257 | J1939_rx_message.dest_addr = 0; 258 | J1939_rx_message.source_addr = 0; 259 | J1939_rx_message.byte_count = 0; 260 | 261 | for (i = 0; i < NUMBER_TRANS_RX_BUFFERS; i++) 262 | { 263 | J1939_rx_message.data[i] = 0; 264 | } 265 | 266 | J1939_rx_state_machine.status = WAIT_FOR_MESSAGE; 267 | go_on = FALSE; 268 | break; 269 | 270 | case CHECK_DATA_PACKET: 271 | if (J1939_rx_state_machine.TP == TP_CM_BAM) 272 | { 273 | if (J1939_rx_pdu.source_addr == J1939_rx_state_machine.source_addr && 274 | J1939_rx_pdu.dest_addr == J1939_rx_state_machine.dest_addr) 275 | { 276 | if (J1939_rx_state_machine.packet_number == J1939_rx_pdu.data[0]-1) 277 | { 278 | J1939_rx_state_machine.status = SAVE_DATA; 279 | J1939_rx_state_machine.timer_counter = 0; 280 | J1939_rx_pdu.data[0] = 0; 281 | } 282 | else if(J1939_rx_pdu.data[0] != 0) 283 | { 284 | J1939_rx_state_machine.status = RESET_REASSEMBLY_STRUCTURE; 285 | } 286 | else 287 | { 288 | J1939_rx_state_machine.status = WAIT_FOR_DATA; 289 | go_on = FALSE; 290 | } 291 | } 292 | else 293 | { 294 | J1939_rx_state_machine.status = WAIT_FOR_DATA; 295 | go_on = FALSE; 296 | } 297 | } 298 | else if (J1939_rx_state_machine.TP == TP_CM_RTS) 299 | {} 300 | else 301 | {} 302 | break; 303 | 304 | case SAVE_DATA: 305 | i=0; 306 | 307 | while ((i < 7) && ((J1939_rx_state_machine.packet_number*7+i) <= J1939_rx_state_machine.byte_count)) 308 | { 309 | J1939_rx_message.data[J1939_rx_state_machine.packet_number*7+i] = J1939_rx_pdu.data[i+1]; 310 | J1939_rx_pdu.data[i+1] = 0; 311 | i++; 312 | } 313 | J1939_rx_state_machine.packet_number++; 314 | 315 | if (J1939_rx_state_machine.packet_number == J1939_rx_state_machine.total_packet_number) 316 | { 317 | J1939_rx_state_machine.status = FILL_USER_MESSAGE; 318 | } 319 | else 320 | { 321 | J1939_rx_state_machine.status = WAIT_FOR_DATA; 322 | go_on = FALSE; 323 | } 324 | break; 325 | 326 | case SEND_EOM: 327 | 328 | case SEND_CTS: 329 | 330 | case FILL_USER_MESSAGE: 331 | J1939_rx_state_machine.status = RESET_REASSEMBLY_STRUCTURE; 332 | J1939_rx_state_machine.timer_counter = 0; 333 | 334 | // send message up the stack to the Network Management Layer 335 | NML_process(&J1939_rx_message); 336 | 337 | //go_on = FALSE; 338 | break; 339 | 340 | 341 | default: 342 | go_on = FALSE; 343 | break; 344 | 345 | } // end switch 346 | } // end while 347 | } 348 | 349 | 350 | U08 Transmit_J1939msg(J1939_TX_MESSAGE_T *msg_ptr) 351 | { 352 | if (msg_ptr->byte_count > 8) 353 | { 354 | #if NOT_YET 355 | // Transport layer for transmission of J1939 messages > 8 bytes not implemented yet 356 | #endif 357 | } 358 | else 359 | { 360 | Build_CANpkt(msg_ptr, 0); 361 | } 362 | 363 | return TRUE; 364 | } 365 | -------------------------------------------------------------------------------- /J1939_TL.h: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////////////////// 2 | // File: J1939_TL.h 3 | // Module: J1939 stack 4 | // Description: Defines for the J1939 stack Transport Layer 5 | // Originator: MB 6 | // Derived from: Freescale J1939 stack 7 | // Date created: 09 June 2014 8 | //--------------------------------------------------------------------------------------- 9 | // Revision Log: 10 | // $Log: $ 11 | // 12 | //--------------------------------------------------------------------------------------- 13 | // Notes: 14 | // 15 | //--------------------------------------------------------------------------------------- 16 | ///////////////////////////////////////////////////////////////////////////////////////// 17 | // Copyright (C)2014 by Timespace Technology Ltd. All rights reserved. 18 | // 19 | // This software is confidential. It is not to be copied or distributed in any form 20 | // to any third party. 21 | // 22 | // This software is provided by Timespace Technology on an 'as is' basis, and Timespace 23 | // Technology expressly disclaims any and all warranties, expressed or implied including, 24 | // without limitation, warranties of merchantability and fitness for a particular 25 | // purpose. In no event shall Timespace Technology be liable for any direct, indirect, 26 | // incidental, punitive or consequential damages - of any kind whatsoever - with respect 27 | // to the software. 28 | ///////////////////////////////////////////////////////////////////////////////////////// 29 | 30 | 31 | #ifndef _J1939_TL_H_ 32 | #define _J1939_TL_H_ 33 | 34 | //========================================================================================== 35 | // Transport Layer Interface Functions 36 | //========================================================================================== 37 | 38 | void TL_init(void); 39 | void TL_process(J1939_RX_PDU_T *msg_ptr); 40 | void TL_periodic(void); 41 | 42 | U08 Transmit_J1939msg(J1939_TX_MESSAGE_T *msg_ptr); 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /J1939_includes.h: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////////////////// 2 | // File: J1939_includes.h 3 | // Module: J1939 stack 4 | // Description: Defines all required includes for the J1939 stack 5 | // Originator: MB 6 | // Derived from: Freescale J1939 stack 7 | // Date created: 09 June 2014 8 | //--------------------------------------------------------------------------------------- 9 | // Revision Log: 10 | // $Log: $ 11 | // 12 | //--------------------------------------------------------------------------------------- 13 | // Notes: 14 | // 15 | //--------------------------------------------------------------------------------------- 16 | ///////////////////////////////////////////////////////////////////////////////////////// 17 | // Copyright (C)2014 by Timespace Technology Ltd. All rights reserved. 18 | // 19 | // This software is confidential. It is not to be copied or distributed in any form 20 | // to any third party. 21 | // 22 | // This software is provided by Timespace Technology on an 'as is' basis, and Timespace 23 | // Technology expressly disclaims any and all warranties, expressed or implied including, 24 | // without limitation, warranties of merchantability and fitness for a particular 25 | // purpose. In no event shall Timespace Technology be liable for any direct, indirect, 26 | // incidental, punitive or consequential damages - of any kind whatsoever - with respect 27 | // to the software. 28 | ///////////////////////////////////////////////////////////////////////////////////////// 29 | 30 | 31 | #ifndef _J1939_INCLUDES_H 32 | #define _J1939_INCLUDES_H 33 | 34 | #include "J1939_CFG.h" 35 | #include "J1939.h" 36 | #include "J1939_HAL.h" 37 | #include "J1939_DL.h" 38 | #include "J1939_TL.h" 39 | #include "J1939_NML.h" 40 | #include "J1939_AL.h" 41 | 42 | #endif -------------------------------------------------------------------------------- /J1939_stack.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Express 2013 for Windows Desktop 4 | VisualStudioVersion = 12.0.21005.1 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "xlCANdemo", "xlCANdemo.vcxproj", "{304D975A-4066-454A-8019-C2BE0DDA996E}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Win32 = Debug|Win32 11 | Debug|x64 = Debug|x64 12 | Release|Win32 = Release|Win32 13 | Release|x64 = Release|x64 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {304D975A-4066-454A-8019-C2BE0DDA996E}.Debug|Win32.ActiveCfg = Debug|Win32 17 | {304D975A-4066-454A-8019-C2BE0DDA996E}.Debug|Win32.Build.0 = Debug|Win32 18 | {304D975A-4066-454A-8019-C2BE0DDA996E}.Debug|x64.ActiveCfg = Debug|x64 19 | {304D975A-4066-454A-8019-C2BE0DDA996E}.Debug|x64.Build.0 = Debug|x64 20 | {304D975A-4066-454A-8019-C2BE0DDA996E}.Release|Win32.ActiveCfg = Release|Win32 21 | {304D975A-4066-454A-8019-C2BE0DDA996E}.Release|Win32.Build.0 = Release|Win32 22 | {304D975A-4066-454A-8019-C2BE0DDA996E}.Release|x64.ActiveCfg = Release|x64 23 | {304D975A-4066-454A-8019-C2BE0DDA996E}.Release|x64.Build.0 = Release|x64 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | EndGlobal 29 | -------------------------------------------------------------------------------- /J1939_stack.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mikebailey61/J1939_stack/37a5addfb458c225155f5e9e4c708c325f21c167/J1939_stack.suo -------------------------------------------------------------------------------- /J1939_stack_caret: -------------------------------------------------------------------------------- 1 | { 2 | "folders": [ 3 | { 4 | "retained": "E25A4BB65B7F3AFFD7F7CE16B22C5281:J1939_stack", 5 | "path": "/J1939_stack" 6 | } 7 | ] 8 | } -------------------------------------------------------------------------------- /J1939_stack_test.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mikebailey61/J1939_stack/37a5addfb458c225155f5e9e4c708c325f21c167/J1939_stack_test.c -------------------------------------------------------------------------------- /ReadMe.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mikebailey61/J1939_stack/37a5addfb458c225155f5e9e4c708c325f21c167/ReadMe.txt -------------------------------------------------------------------------------- /UpgradeLog.htm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mikebailey61/J1939_stack/37a5addfb458c225155f5e9e4c708c325f21c167/UpgradeLog.htm -------------------------------------------------------------------------------- /xlCANdemo.sdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mikebailey61/J1939_stack/37a5addfb458c225155f5e9e4c708c325f21c167/xlCANdemo.sdf -------------------------------------------------------------------------------- /xlCANdemo.vcproj: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 14 | 17 | 18 | 19 | 20 | 21 | 31 | 34 | 37 | 40 | 43 | 48 | 65 | 68 | 73 | 76 | 90 | 93 | 96 | 99 | 104 | 107 | 110 | 113 | 114 | 124 | 127 | 130 | 133 | 136 | 142 | 159 | 162 | 167 | 170 | 184 | 187 | 190 | 193 | 198 | 201 | 204 | 207 | 208 | 218 | 221 | 224 | 227 | 230 | 235 | 252 | 255 | 260 | 263 | 276 | 279 | 282 | 285 | 290 | 293 | 296 | 299 | 300 | 310 | 313 | 316 | 319 | 322 | 328 | 345 | 348 | 353 | 356 | 369 | 372 | 375 | 378 | 383 | 386 | 389 | 392 | 393 | 394 | 395 | 396 | 397 | 401 | 404 | 407 | 412 | 413 | 416 | 421 | 422 | 425 | 430 | 431 | 434 | 439 | 440 | 441 | 442 | 446 | 449 | 450 | 451 | 454 | 455 | 456 | 457 | 458 | 459 | -------------------------------------------------------------------------------- /xlCANdemo.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Debug 10 | x64 11 | 12 | 13 | Release 14 | Win32 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | {304D975A-4066-454A-8019-C2BE0DDA996E} 23 | xlCANdemo 24 | J1939_stack 25 | 26 | 27 | 28 | Application 29 | v120 30 | false 31 | MultiByte 32 | 33 | 34 | Application 35 | v120 36 | false 37 | MultiByte 38 | 39 | 40 | Application 41 | v120 42 | false 43 | MultiByte 44 | 45 | 46 | Application 47 | v120 48 | false 49 | MultiByte 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | <_ProjectFileVersion>12.0.21005.1 73 | 74 | 75 | ..\..\exec\debug\ 76 | ..\..\obj\$(ProjectName)\$(Configuration)\ 77 | true 78 | 79 | 80 | .\Debug64\ 81 | .\Debug64\ 82 | true 83 | 84 | 85 | ..\..\exec\ 86 | ..\..\obj\$(ProjectName)\$(Configuration)\ 87 | false 88 | 89 | 90 | .\Release64\ 91 | .\Release64\ 92 | false 93 | 94 | 95 | 96 | .\Debug/xlCANdemo.tlb 97 | 98 | 99 | 100 | Disabled 101 | ..\..\bin;%(AdditionalIncludeDirectories) 102 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 103 | true 104 | EnableFastChecks 105 | MultiThreadedDebug 106 | stdafx.h 107 | 108 | $(IntDir) 109 | $(IntDir)vc90.pdb 110 | 111 | Level4 112 | true 113 | EditAndContinue 114 | 115 | 116 | _DEBUG;%(PreprocessorDefinitions) 117 | 0x0407 118 | 119 | 120 | ../../bin/vxlapi.lib;%(AdditionalDependencies) 121 | $(OutDir)$(ProjectName).exe 122 | true 123 | true 124 | $(TargetDir)$(TargetName).pdb 125 | Console 126 | false 127 | 128 | MachineX86 129 | 130 | 131 | true 132 | .\Debug/xlCANdemo.bsc 133 | 134 | 135 | 136 | 137 | X64 138 | .\Debug/xlCANdemo.tlb 139 | 140 | 141 | 142 | Disabled 143 | ..\..\bin;%(AdditionalIncludeDirectories) 144 | WIN64;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 145 | true 146 | EnableFastChecks 147 | MultiThreadedDebug 148 | .\Debug/xlCANdemo.pch 149 | .\Debug/ 150 | .\Debug/ 151 | .\Debug/ 152 | true 153 | Level3 154 | true 155 | ProgramDatabase 156 | 157 | 158 | _DEBUG;%(PreprocessorDefinitions) 159 | 0x0407 160 | 161 | 162 | ../../bin/vxlapi64.lib;%(AdditionalDependencies) 163 | .\Debug64/xlCANdemo_x64.exe 164 | true 165 | true 166 | xlCANdemo.pdb 167 | Console 168 | false 169 | 170 | MachineX64 171 | 172 | 173 | true 174 | .\Debug/xlCANdemo.bsc 175 | 176 | 177 | 178 | 179 | .\Release/xlCANdemo.tlb 180 | 181 | 182 | 183 | MaxSpeed 184 | OnlyExplicitInline 185 | ..\..\bin;%(AdditionalIncludeDirectories) 186 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 187 | true 188 | MultiThreaded 189 | true 190 | stdafx.h 191 | 192 | $(IntDir) 193 | $(IntDir)vc90.pdb 194 | 195 | Level4 196 | true 197 | 198 | 199 | NDEBUG;%(PreprocessorDefinitions) 200 | 0x0407 201 | 202 | 203 | ../../bin/vxlapi.lib;%(AdditionalDependencies) 204 | $(OutDir)$(ProjectName).exe 205 | true 206 | 207 | Console 208 | false 209 | 210 | MachineX86 211 | 212 | 213 | true 214 | .\Release/xlCANdemo.bsc 215 | 216 | 217 | 218 | 219 | X64 220 | .\Release/xlCANdemo.tlb 221 | 222 | 223 | 224 | MaxSpeed 225 | OnlyExplicitInline 226 | ..\..\bin;%(AdditionalIncludeDirectories) 227 | WIN64;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 228 | true 229 | MultiThreaded 230 | true 231 | .\Release/xlCANdemo.pch 232 | .\Release/ 233 | .\Release/ 234 | .\Release/ 235 | true 236 | Level3 237 | true 238 | 239 | 240 | NDEBUG;%(PreprocessorDefinitions) 241 | 0x0407 242 | 243 | 244 | ../../bin/vxlapi64.lib;%(AdditionalDependencies) 245 | ../../exec/xlCANdemo_x64.exe 246 | true 247 | .\Release/xlCANdemo.pdb 248 | Console 249 | false 250 | 251 | MachineX64 252 | 253 | 254 | true 255 | .\Release/xlCANdemo.bsc 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | -------------------------------------------------------------------------------- /xlCANdemo.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {0e19d5e9-f4d1-42f4-b33f-f74f6e4678d1} 6 | cpp;c;cxx;rc;def;r;odl;idl;hpj;bat 7 | 8 | 9 | {d64a8ce6-4a99-4d6b-a395-18c7a8fda429} 10 | h;hpp;hxx;hm;inl 11 | 12 | 13 | 14 | 15 | Header Files 16 | 17 | 18 | Source Files 19 | 20 | 21 | Source Files 22 | 23 | 24 | Source Files 25 | 26 | 27 | Source Files 28 | 29 | 30 | Source Files 31 | 32 | 33 | Source Files 34 | 35 | 36 | Source Files 37 | 38 | 39 | Source Files 40 | 41 | 42 | Source Files 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | Source Files 51 | 52 | 53 | Source Files 54 | 55 | 56 | Source Files 57 | 58 | 59 | Source Files 60 | 61 | 62 | Source Files 63 | 64 | 65 | Source Files 66 | 67 | 68 | Source Files 69 | 70 | 71 | -------------------------------------------------------------------------------- /xlCANdemo.vcxproj.user: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | --------------------------------------------------------------------------------