CANbus to Instruments etc. working

This forum is for the discussion of other projects on Megasquirt/Microsquirt hardware that don't fit into any of the other forums

Moderators: jsmcortina, muythaibxr

Post Reply
stevevp
Helpful MS/Extra'er
Posts: 40
Joined: Fri Nov 11, 2011 3:43 am
Location: Brisbane, Australia
Contact:

CANbus to Instruments etc. working

Post by stevevp »

I've been using a microsquirt to drive a CANbus instrument cluster and thought someone may be interested in the code changes to allow this to work.

As the MS CANbus implementation is at 500k and using extended 29 bit identifiers and most OEM driveline implementations are using 500k and 11 bit IDs, there is no reason why they can't coexist in a project vehicle.

Modifications to source code needed
{FIle}
{approximate Line Number} {code Change}

Code: Select all

  ms2_extra.h
   122  #define MSG_STD    11
   1174 void ECANSendMessage(unsigned int id, unsigned char *data, unsigned char dataLen, unsigned char msgType) FAR_TEXT39_ATTR;
   1221 void user_defined(long tmptime) FAR_TEXT3b_ATTR; // was 3a change to 3b

Code: Select all

  ms2_can_isr.c
   29            case MSG_STD:  // msg format 11 bit STD - do not let MSII mess with it
                    // reset identifier registers for standard ID
                    CAN_TB0_IDR0 = (unsigned char)(can[ix].cx_destvaroff[jx] >> 3);
                    // 8 high bits in IDR0, 3 low bits in IDR1
                    CAN_TB0_IDR1 = (unsigned char)((can[ix].cx_destvaroff[jx] & 0x0007) << 5);
                    CAN_TB0_IDR2 = (unsigned char)(0x00);
                    CAN_TB0_IDR3 = (unsigned char)(0x01);
                    CAN_TB0_DLR = (unsigned char) (can[ix].cx_varbyt[jx]);
                    for (kx = 0;kx < CAN_TB0_DLR;kx++)  {
                        *(&CAN_TB0_DSR0 + kx) = can[ix].cx_datbuf[jx][kx];
                    }
                    break;
Thats it for the changes to existing source code - the new function and main high level code can be held in ms2_extra_user.c as detailed in Part 2 below
Last edited by stevevp on Wed Mar 14, 2012 6:30 am, edited 1 time in total.
Steve
jsmcortina
Site Admin
Posts: 39585
Joined: Mon May 03, 2004 1:34 am
Location: Birmingham, UK
Contact:

Re: CANbus to Instruments etc. working

Post by jsmcortina »

How far have you got with this?

James
I can repair or upgrade Megasquirts in UK. http://www.jamesmurrayengineering.co.uk

My Success story: http://www.msextra.com/forums/viewtopic ... 04&t=34277
MSEXTRA documentation at: http://www.msextra.com/doc/index.html
New users, please read the "Forum Help Page".
stevevp
Helpful MS/Extra'er
Posts: 40
Joined: Fri Nov 11, 2011 3:43 am
Location: Brisbane, Australia
Contact:

Re: CANbus to Instruments etc. working

Post by stevevp »

Part 2 - I placed the function ECANSendMessage() at the end of ms2_extra_user.c and added the broadcast code to the function user_defined()

Code: Select all

/* $Id: ms2_extra_user.c,v 1.3 2010-10-01 00:32:46 jsmcortina Exp $ */
#include "ms2_extra.h"

/*
 modifications to files
 ms2_extra.h
   122  #define MSG_STD    11
   1174 void ECANSendMessage(unsigned int id, unsigned char *data, unsigned char dataLen, unsigned char msgType) FAR_TEXT39_ATTR;
   1221 void user_defined(long tmptime) FAR_TEXT3b_ATTR; // was 3a change to 3b
 ms2_can_isr.c
   29            case MSG_STD:  // msg format 11 bit STD - do not let MSII mess with it
                    // reset identifier registers for standard ID
                    CAN_TB0_IDR0 = (unsigned char)(can[ix].cx_destvaroff[jx] >> 3);
                    // 8 high bits in IDR0, 3 low bits in IDR1
                    CAN_TB0_IDR1 = (unsigned char)((can[ix].cx_destvaroff[jx] & 0x0007) << 5);
                    CAN_TB0_IDR2 = (unsigned char)(0x00);
                    CAN_TB0_IDR3 = (unsigned char)(0x01);
                    CAN_TB0_DLR = (unsigned char) (can[ix].cx_varbyt[jx]);
                    for (kx = 0;kx < CAN_TB0_DLR;kx++)  {
                        *(&CAN_TB0_DSR0 + kx) = can[ix].cx_datbuf[jx][kx];
                    }
                    break;
 */

#define Engine_RPM 	0x280 // 996 - Motorsteuerger�t

void user_defined() {
    /* 'user defined'
     *
     * So here is a place to put your code. The three variables are there ready
     * for you.
     * If you want to get data out to tuning software you can use outpc.user0 which is set
     * aside specially  (or outpc.status4 or outpc.istatus5 which are both free at time of
     * writing this note.)
     * Then those gauges can be enabled in Megatune or Tunerstudio.
     *
     * Other uses:
     * Make custom comparisons to turn outputs on/off. One way to simplify this somewhat
     * could be to use this code to change the value in status4 and then let the
     * existing "outputs" code that you configure in the tuning software actually
     * enable the output and turn it on or off - that might save a lot of customisation
     * and digging around in the code. Once you have status4 changing value, you are
     * nearly done.
     */
        unsigned long ultmp;
        DISABLE_INTERRUPTS;
        ultmp = lmms;
        ENABLE_INTERRUPTS;

    if ( (ultmp > cansendclk) && (flash10.user_conf & 0x01))  { // is our user defined feature enabled
                
        cansendclk = ultmp + 78;      // 10ms ( 1s = 7812 x .128 ms) clk
    
        // user_ulong = ????;  variables for your use
        // user_uint = ????;
        // user_uchar = ????;

        // flash10.user_conf   } These are the data 
        // flash10.user_value1 } you can prog to
        // flash10.user_value2 } flash from tuning software
               
        // typedef signed char int8_t; 
        // typedef unsigned char uint8_t; 
        // typedef int int16_t; 
        // typedef unsigned int uint16_t; 
        // typedef long int32_t;
        // typedef unsigned long uint32_t;
        // typedef long long int64_t;
        // typedef unsigned long long uint64_t;

        // Send Message Variables
        unsigned int id;
        unsigned char data[8];
        unsigned char dataLen;

        id = Engine_RPM; //0x280 - Motorsteuergerat 640 - engine control unit RPM is 4:1 ratio = outpc.rpm*4
        dataLen = 0x08;
        data[0] = 0x00;
        data[1] = 0x00;
        data[2] = (unsigned char)((outpc.rpm*4) & 0x00FF ); //byte 3 = RPM, L 
        data[3] = (unsigned char)((outpc.rpm*4) /256 );     //byte 4 = RPM, H 
        data[4] = 0x00;
        data[5] = 0x00;
        data[6] = 0x00;
        data[7] = 0x00;
        ECANSendMessage(id, data, dataLen, MSG_STD);


    }
    return;
}

void ECANSendMessage(unsigned int id, unsigned char *data, unsigned char dataLen, unsigned char msgType) {
    int ix;
    int kx;
    /* CAN message format:
     * Reg Bits: 7<------------------- 0
     * IDR0:    |----STD ID(11 bits)----|  (Header bits 28 <-- 21)
     * IDR1:    |cont'd 0 0 ------------|  (Header bits 20 <-- 15)
     * IDR2:    |-----------------------|  (Header bits 14 <--  7)
     * IDR3:    |----------------------0|  (Header bits  6 <-- 0,rtr)
     */  

    ix = can[0].cxno_in;

    // Set to Standard 11Bit ID Format 
    can[0].cx_msg_type[ix] = msgType; // MSG_STD = 11

    // set CANID and DLC of Message
    can[0].cx_destvaroff[ix] = id; // CANID
    can[0].cx_varbyt[ix] = dataLen; // 8 bytes DLC

    // load Message Data 
                    for (kx = 0;kx < dataLen ;kx++)  {
                        can[0].cx_datbuf[ix][kx] = data[kx];
                    }

    // set following to zero as not used in Standard CAN Header
    can[0].cx_destvarblk[ix] = 0;
    can[0].cx_dest[ix] = 0;

    // This is where (in xmt ring buffer) to put next message
    if (can[0].cxno_in < (NO_CANMSG - 1)) {
        can[0].cxno_in++;
    } else {
        can[0].cxno_in = 0; // overwrite oldest msg in queue
    }

    // increment counter
    if (can[0].cxno < NO_CANMSG) {
        can[0].cxno++;
    } else {
        can[0].cxno = NO_CANMSG;
    }
    if (!(CANTIER & 0x07)) {
        // Following will cause entry to TxIsr without sending msg
        // since when CANTIER = 0, CANTFLG left as buff empty(>0).
        // If CANTIER has at least 1 int buf enabled, will enter
        // TxIsr automatically.
        CANTBSEL = CANTFLG;
        CANTIER = CANTBSEL;
    }
}

as stated this has been running on a microsquirt and feeding a Porsche 996 CANbus instrument cluster with RPM / Vehicle Speed / Engine Temp and Warning Lights for 6 months now without problems.

I have not yet tested this with two MS units doing their data polling comms while running the 11bit broadcast but given the message type masks, the MS units should have no problems ignoring the 11 bit data, similarly the cluster is happily ignoring the 29bit data.

the code above in user defined has been edited to have one ID as a clean example. and should send a valid rpm to the tach of instruments expecting Bosch Motronic data (VW/Audi/Porsche)
Last edited by stevevp on Thu Mar 15, 2012 5:49 am, edited 2 times in total.
Steve
stevevp
Helpful MS/Extra'er
Posts: 40
Joined: Fri Nov 11, 2011 3:43 am
Location: Brisbane, Australia
Contact:

Re: CANbus to Instruments etc. working

Post by stevevp »

James,

Can I reserve #define MSG_STD 11 or have it added to future code releases, also If there are better memory blocks to use in the function declarations please feel free to edit
Steve
racingmini_mtl
Super MS/Extra'er
Posts: 9128
Joined: Sun May 02, 2004 6:51 am
Location: Quebec, Canada
Contact:

Re: CANbus to Instruments etc. working

Post by racingmini_mtl »

Nice! I'm curious to see if indeed there is no interference (other than bandwidth) between the 11-bit data and the 29-bit data.

You mention vehicle RPM / Vehicle Speed / Engine Temp and Warning Lights but I only see RPM in your code. Do you have more information about the other parameters? Do you have a link on this for more details?

Jean
jbperf.com Main site . . . . . . . . . . . . . . . . . . . . . . jbperf.com Forum
Image
jsmcortina
Site Admin
Posts: 39585
Joined: Mon May 03, 2004 1:34 am
Location: Birmingham, UK
Contact:

Re: CANbus to Instruments etc. working

Post by jsmcortina »

As Jean says, this is very interesting indeed. It seems like a worthy addition to the main code if you are willing to share - I'll contact you by PM about that.

James
I can repair or upgrade Megasquirts in UK. http://www.jamesmurrayengineering.co.uk

My Success story: http://www.msextra.com/forums/viewtopic ... 04&t=34277
MSEXTRA documentation at: http://www.msextra.com/doc/index.html
New users, please read the "Forum Help Page".
stevevp
Helpful MS/Extra'er
Posts: 40
Joined: Fri Nov 11, 2011 3:43 am
Location: Brisbane, Australia
Contact:

Re: CANbus to Instruments etc. working

Post by stevevp »

The code was cut down to show a reasonably clean example.

The ID's I'm currently using include

Code: Select all

#define Ignition_Status 0x271 //Audi
#define SteerCSM_Status 0x2C1 //Audi
#define CentElec_Status 0x531 //Audi
#define CentConv_Status 0x591 //Audi
#define DrivDoor_Status 0x381 //Audi
#define PassDoor_Status 0x3B5 //Audi
#define RRDoor_Status   0x4BD //Audi
#define LRDoor_Status   0x4B9 //Audi
#define MultiFnc_Status 0x5C3 //Audi
#define FIS1_TEXT_line1 0x265 //Audi
#define FIS1_TEXT_line2 0x263 //Audi
#define AT_Line2 	0x540 //Audi
#define VIN_IDENTIF 	0x65F //Audi

#define ABS_Status      0x1A0 // 996 - ABS              416 - ESP/ABS/Brakes
#define Engine_RPM 	0x280 // 996 - Motorsteuerger�t 640 - engine control unit RPM 1:1 ratio
#define Engine_Temp     0x289 // 996 - Motorsteuerger�t 649 - Engine Temp /y=46.14+1.67*x where x is degrees C
#define Vehicle_Speed 	0x2A8 // 996 - Motorsteuerger�t 680 - engine control unit Speed (78270+(47907*kmh))/1000
#define AT_Line1 	0x440 // 996 - Motorsteuerger�t 1088- Tiptronic Gear Position
#define TempoStat_ctl   0x510 // 996 - Cluster out     1296 - Instrument Cluster Tempostat control


I'm using outpc.rpm and outpc.clt so it's a relatively simple matter to repeat blocks and adjust to suit

Code: Select all

        id = Engine_Temp; //289 - Engine Temp
        // quadratic to match guage position curve y = -.0336*x*x + 7.7986*x + -225.2571
        temp = (((outpc.clt - 320) * 5) / 9)/10;
        if (temp > 74) {
        temp = ((-336*temp*temp) + (77986*temp) + -2252571)/10000 ;
        }
        else {
            temp = (5447+154*temp)/100;
        }
        dataLen = 0x08;
        data[0] = 0x00;
        data[1] = (unsigned char)(temp); 
        data[2] = 0x00;
        data[3] = 0x00;
        data[4] = 0x00;
        data[5] = 0x00;
        data[6] = 0x00;
        data[7] = 0x00;
        ECANSendMessage(id, data, dataLen, MSG_STD);
as well as the Flex input for vehicle speed

Code: Select all

                user_ulong = ((7812 / FSens_Pd) * 100); // Hz*100 for VSS in CAN
                kmh = user_ulong * 36 / flash10.user_value1 ; // KMh/3600 = M/Sec    m/sec = distperpulse*hz*3600   pulses per km 3950 stored in flash10.user_value1
As vehicle specific data is reverse engineered, documentation is very hard to come by and guarded if found :)
Jean what were you looking for in regards to links?
Last edited by stevevp on Thu Mar 15, 2012 5:53 am, edited 1 time in total.
Steve
jsmcortina
Site Admin
Posts: 39585
Joined: Mon May 03, 2004 1:34 am
Location: Birmingham, UK
Contact:

Re: CANbus to Instruments etc. working

Post by jsmcortina »

stevevp wrote:As vehicle specific data is reverse engineered, documentation is very hard to come by and guarded if found :)
Jean what were you looking for in regards to links?
I guess he was wondering the same thing as me "how did you work out how to do that?"

James
I can repair or upgrade Megasquirts in UK. http://www.jamesmurrayengineering.co.uk

My Success story: http://www.msextra.com/forums/viewtopic ... 04&t=34277
MSEXTRA documentation at: http://www.msextra.com/doc/index.html
New users, please read the "Forum Help Page".
racingmini_mtl
Super MS/Extra'er
Posts: 9128
Joined: Sun May 02, 2004 6:51 am
Location: Quebec, Canada
Contact:

Re: CANbus to Instruments etc. working

Post by racingmini_mtl »

jsmcortina wrote:
stevevp wrote:As vehicle specific data is reverse engineered, documentation is very hard to come by and guarded if found :)
Jean what were you looking for in regards to links?
I guess he was wondering the same thing as me "how did you work out how to do that?"

James
Exactly.

I wanted to know where you got the message ID and format from. And if there's more information available. It would be nice to also have the same information for other OEMs so that MS2/Extra and MS3 could be used with as many OEM CAN-driven instrument clusters as possible.

Jean
jbperf.com Main site . . . . . . . . . . . . . . . . . . . . . . jbperf.com Forum
Image
stevevp
Helpful MS/Extra'er
Posts: 40
Joined: Fri Nov 11, 2011 3:43 am
Location: Brisbane, Australia
Contact:

Re: CANbus to Instruments etc. working

Post by stevevp »

jsmcortina wrote:I guess he was wondering the same thing as me "how did you work out how to do that?"
James
lots of expensive equipment and many many moons of data capture and dissection :yeah!: I've been doing automotive comms for a few years now and it does seem to get easier.
The hardest part with this one was getting my head around the MS2 specific hybrid usage of the 29bit header. Once that made sense it was fairly easy to revert the interrupt code back to a normal transmit block exception.

Verifying either the message masks in the init or the receive interrrupts are 11bit safe is probably all that is needed now.
Steve
Matt Cramer
Super MS/Extra'er
Posts: 17499
Joined: Thu Apr 16, 2009 8:08 pm

Re: CANbus to Instruments etc. working

Post by Matt Cramer »

Very, very impressive job! I don't have much to add, but I just wanted to stand around and gawk. :)
Matt Cramer -1966 Dodge Dart slant six running on MS3X
stevevp
Helpful MS/Extra'er
Posts: 40
Joined: Fri Nov 11, 2011 3:43 am
Location: Brisbane, Australia
Contact:

Re: CANbus to Instruments etc. working

Post by stevevp »

a request if possible - for the future MS3 - can a second CAN port be included in the design in the next revision.
The cpu supports it and the scope as an entire vehicle gateway between engine (500k) and body control (100k) would be immense. Not to mention the flexibility of 2 MS2 channels
Steve
jsmcortina
Site Admin
Posts: 39585
Joined: Mon May 03, 2004 1:34 am
Location: Birmingham, UK
Contact:

Re: CANbus to Instruments etc. working

Post by jsmcortina »

I've added this code (tweaked a little) to MS2/Extra and MS3 dev codes.

James
I can repair or upgrade Megasquirts in UK. http://www.jamesmurrayengineering.co.uk

My Success story: http://www.msextra.com/forums/viewtopic ... 04&t=34277
MSEXTRA documentation at: http://www.msextra.com/doc/index.html
New users, please read the "Forum Help Page".
stevevp
Helpful MS/Extra'er
Posts: 40
Joined: Fri Nov 11, 2011 3:43 am
Location: Brisbane, Australia
Contact:

Re: CANbus to Instruments etc. working

Post by stevevp »

Cool, Thanks James
Steve
piledriver
Super MS/Extra'er
Posts: 1679
Joined: Tue Oct 27, 2009 6:24 am
Location: Van Alstyne, Texas

Re: CANbus to Instruments etc. working

Post by piledriver »

Cool, this opens up many options, not just for new cars but for moden instrument adaption into older cars.
Always doing things the hard way, MS2 sequential w/ v1.01 mainboard, LS2 coils. 80 mile/day commuter status.
fixmann
Helpful MS/Extra'er
Posts: 135
Joined: Tue Jun 02, 2009 12:52 pm
Location: Norway

Re: CANbus to Instruments etc. working

Post by fixmann »

Hi
This dude seems to know a thing or two about can http://www.canbushack.com/blog/index.php

Øyvind
Paul_VR6
Super MS/Extra'er
Posts: 1978
Joined: Wed Apr 15, 2009 11:31 am
Location: Oxford, PA
Contact:

Re: CANbus to Instruments etc. working

Post by Paul_VR6 »

I am can-stupid, but can this be used to send generic different format can messages to an aftermarket digital dash?
-Paul
1992 Corrado SLC 3.6 VR6 11.38@120 - MS3 Pro Ultimate - Microsquirt I/O - Can EGT - Racepak IQ3s
kptuned.com - Megasquirting the World! Megasquirt Sales, Service, Tuning and More!
Image
stevevp
Helpful MS/Extra'er
Posts: 40
Joined: Fri Nov 11, 2011 3:43 am
Location: Brisbane, Australia
Contact:

Re: CANbus to Instruments etc. working

Post by stevevp »

basically yes, although the firmware will need to be modified with any additional message/data sets over the RPM @ 0x280h that james has added to the code
Steve
Matt Cramer
Super MS/Extra'er
Posts: 17499
Joined: Thu Apr 16, 2009 8:08 pm

Re: CANbus to Instruments etc. working

Post by Matt Cramer »

Paul_VR6 wrote:I am can-stupid, but can this be used to send generic different format can messages to an aftermarket digital dash?
AIM has already set up their MXL series digital dashes to talk to MegaSquirt CAN.
Matt Cramer -1966 Dodge Dart slant six running on MS3X
NArcher
Helpful MS/Extra'er
Posts: 55
Joined: Wed Mar 23, 2011 1:38 pm

Re: CANbus to Instruments etc. working

Post by NArcher »

@ stevevp
I would like to get my instrument cluster to talk with my MS3. I have a Mazda 3 that I recently installed MS3+MS3X on, but do not have rpm, speed, etc., because I could not figure out how to communicate with instrument cluster. I have the hex ID's for most of the signals that the Mazda 3 uses on CAN, but I'm only a novice programmer at best and cannot figure out how to implement the signals to allow MS to communicate with instrument cluster. Maybe someone can help me on this? Any help is much appreciated! I can also make a donation if need be for this to happen.
Post Reply