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

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 »

NArcher wrote:Sorry, another CAN question.

I have an ABS computer that I would like to receive the data from and pass to the vss to get speedo to work. Which part of the code do I have to modify to make this work. I am [also] a poor programmer :cry: but am continuously learning! :RTFM:
Unless you actually removed the surface mount resistor from the board, your MS3 should have the terminating 120 Ohm resistor.

As for your second request, there is no support for receiving data from a device that is not using the MS CAN protocol (nor are there plans for it at this time as far as I know). So you would need to program that in and it's not a beginner project. And even then that assumes you actually know what the protocol and data format from the ABS computer are. If not then it is even more advanced and needs reverse engineering the CAN protocol.

Jean
jbperf.com Main site . . . . . . . . . . . . . . . . . . . . . . jbperf.com Forum
Image
NArcher
Helpful MS/Extra'er
Posts: 55
Joined: Wed Mar 23, 2011 1:38 pm

Re: CANbus to Instruments etc. working

Post by NArcher »

Sorry, I didnt explain the situation clearly. The ABS computer is on the original CAN network of the car. I have the correct ID's and information to extract from the computer, I just don't know how to allow the megasquirt to receive the incoming ID. I do not have to poll for data as the ABS computer is constantly sending the information out on the network.
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 »

Firstly you have to open up the receive buffers to allow all can message in

in CanInit()

Code: Select all

	/* Set identifier acceptance and mask registers to accept 11 and 29 bit messages from all devices */
	CANIDMR0 = 0xFF;       // anything ok in IDR0(var offset)
	CANIDMR1 = 0xFF;       //was 0xE7;		   // anything ok for var_off cont'd, msgtype
	CANIDMR2 = 0xFF;       //was 0xF0;			 // can be from any other device
	CANIDMR3 = 0xFF;       // any var_blk, spare, rtr
	CANIDAR1 = 0x00;       //was 0x18;       // 0,0,0,SRR=IDE=1
	CANIDAR2 = 0x00;       //was can_id;     // rcv msg must be to can_id, but now accepts any
	/* 2nd 32-bit filter bank */
	CANIDMR4 = 0xFF;       // anything ok in IDR0(var offset)
	CANIDMR5 = 0xFF;       //0xE7;		   // anything ok for var_off cont'd, msgtype
	CANIDMR6 = 0xFF;       //0xF0;			 // can be from any other device
	CANIDMR7 = 0xFF;       // any var_blk, spare, rtr
	CANIDAR5 = 0x00;       //0x18;       // 0,0,0,SRR=IDE=1
	CANIDAR6 = 0x00;       //0x0F;			 // rcv msg can be to everyone (id=15), and
then the interrupt routine needs to handle MSG_STD
in CanRxIsr()

Code: Select all

    if(CANRFLG & 0x01)  {
    
        var_off = ((unsigned short)CAN_RB_IDR0 << 3) |
                       ((CAN_RB_IDR1 & 0xE0) >> 5);
        msg_IDE = (CAN_RB_IDR1 & 0x08);
        msg_type = MSG_CMD;
        if (msg_IDE) {
          if ((CAN_RB_DLR & 0x0F)==15){// &0x0F = 00001111, get first 4 bits
            msg_type = (CAN_RB_IDR1 & 0x07);
          }
        } else {
          msg_type = MSG_STD;
        }
        rcv_id = CAN_RB_IDR2 >> 4;		// message from device rcv_id
        var_blk = CAN_RB_IDR3 >> 4;
        var_byt = (CAN_RB_DLR & 0x0F); // &0x0F = 00001111, get first 4 bits

        switch(msg_type)  {
        
          case MSG_STD:  // message to be processed
                msgID = (CAN_RB_IDR0 << 3);
                msgID = msgID + (CAN_RB_IDR1 >> 5);
                        // 8 high bits in IDR0, 3 low bits in IDR1      
              // update variable value with received data
                data[0]  =  CAN_RB_DSR0;
                data[1]  =  CAN_RB_DSR1;
                data[2]  =  CAN_RB_DSR2;
                data[3]  =  CAN_RB_DSR3;
                data[4]  =  CAN_RB_DSR4;
                data[5]  =  CAN_RB_DSR5;
                data[6]  =  CAN_RB_DSR6;
                data[7]  =  CAN_RB_DSR7;
                STD11_receive_CAN(msgID,data,var_byt,MSG_STD) ;
                
          case MSG_CMD:  // message for this ecu to set a variable to value in message
        	  break;
then we need to process the STD 11 bit message

Code: Select all

void STD11_receive_CAN(unsigned int Rx_id, unsigned char *Rx_data, unsigned char Rx_dataLen, unsigned char flags)
{
	// Receive Message Variables
  //    unsigned int Rx_id;
  //    unsigned char Rx_data[8];
  //    unsigned char Rx_dataLen;

// enable the following for debug if required
//	ECANSendMessage(Rx_id+1, Rx_data, Rx_dataLen, MSG_STD);

    if (flags == MSG_STD) {
				flags = MSG_STD;
    }

		if (Rx_id == 0x280) // 4th byte of 0x280 is data[3] = (unsigned char)((outpc.rpm*4) /256 ); //byte 4 = RPM, H 
  		{
  		outpc.engine_rpm = ((Rx_data[3])*64);
  		}

		if (Rx_id == 0x520) // 2nd byte of 0x520 is lights and dimming 58d from 996 cluster
  		{
  		inpram.Dimming_58d = Rx_data[1];
  		}

    Rx_dataLen = 8;

		if (Rx_id == 0x661)
		{
			//h661 Radio/Navi, 8 byte, 100ms 
			//beim RNS-E wird hier zum Beispiel angezeigt, in welchem Mode das Geraet ist 
			//-h 81 01 12 A0 00 00 00 00 FM/AM 
			//-h 81 01 12 2F 00 00 00 00 SAT 
			//-h 81 01 12 A5 00 00 00 00 CD 
			//-h 81 01 12 A4 00 00 00 00 MP3 
			//-h 81 01 12 37 00 00 00 00 TV 
			//-h 81 01 12 3A 00 00 00 00 PHONE 
			if (Rx_data[3] == 0xA0) //radio active
			{
				//Set Relay On
					PORTE |= 0x01;
			}
			else
			{
				//Set Relay Off
				PORTE &= ~0x01;
			}
		}
}
Last edited by stevevp on Wed Jan 09, 2013 5:42 pm, 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 »

Yes, but ...
racingmini_mtl wrote:there is no support for receiving data from a device that is not using the MS CAN protocol
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 »

There is now :D
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:There is now :D
Ha... true, you contributed some code (thanks.)

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".
NArcher
Helpful MS/Extra'er
Posts: 55
Joined: Wed Mar 23, 2011 1:38 pm

Re: CANbus to Instruments etc. working

Post by NArcher »

Thank you Steve! Exactly what I was looking for!
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:There is now :D
Ha... true, you contributed some code (thanks.)

James
Except that this breaks the MS CAN protocol...
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 »

Agreed, which is why the receive code was not posted with the original transmit examples.
For a single MS device interfaced to the vehicle canbus only it will be fine

It could possibly work with one register set to 11 bit only and the other to the MS 29bit implementation with the device CanID but broadcast messages to all nodes on MS CanID 15 would still be excluded.
A better solution would be a coded filter in the receive interrupt routine for any message identified as 29bit and specifically rejecting messages without the MS CanID equal to 15 or the current device ID
So for anybody running multiple MS devices and MSquirt_CAN the receive mods above will not work without extra coding to re-implement the MS Can Filters
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 »

stevevp wrote:Agreed, which is why the receive code was not posted with the original transmit examples.
For a single MS device interfaced to the vehicle canbus only it will be fine

It could possibly work with one register set to 11 bit only and the other to the MS 29bit implementation with the device CanID but broadcast messages to all nodes on MS CanID 15 would still be excluded.
A better solution would be a coded filter in the receive interrupt routine for any message identified as 29bit and specifically rejecting messages without the MS CanID equal to 15 or the current device ID
So for anybody running multiple MS devices and MSquirt_CAN the receive mods above will not work without extra coding to re-implement the MS Can Filters
That could work but that could increase the number of interrupts significantly. All messages on the bus would create an interrupt. I'd have to check if even the messages sent by the MS would create a receive interrupt when configured with no hardware filtering as per the code above.

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 »

TX from the device will not be received by the same device unless loopback is configured

the following is pseudocode but gives a reasonable example

the address in MScan is here

Code: Select all

	  IDR2:    |---From ID--|--To ID---|  (Header bits 14 <--  7)
The filter could conceivably be placed in CanRxIsr

Code: Select all

          if ((CAN_RB_DLR & 0x0F)==15){// &0x0F = 00001111, get first 4 bits
            msg_type = (CAN_RB_IDR1 & 0x07);
          }
could become

Code: Select all

          if ((CAN_RB_DLR & 0x0F)==15){// &0x0F = 00001111, get first 4 bits
            if ((CAN_RB_IDR2 & 0x0F)==15 || (CAN_RB_IDR2 & 0x0F)==flash4.mycan_id) {     
                    msg_type = (CAN_RB_IDR1 & 0x07);
            } else {
                    msg_type = 99; // invalid and should cause a quick loop
            }
          }
Steve
NArcher
Helpful MS/Extra'er
Posts: 55
Joined: Wed Mar 23, 2011 1:38 pm

Re: CANbus to Instruments etc. working

Post by NArcher »

When I got the CAN ID's for the Mazda 3, the guy also gave me the bitmask for the ID's. What would these bismasks be used for? And does it relate to my implementation? The reason I ask is because I cannot get the engine temp gauge to function. The value of it should be X /4 where x is degrees C. Is the Engine_Temp variable in degree C?
NArcher
Helpful MS/Extra'er
Posts: 55
Joined: Wed Mar 23, 2011 1:38 pm

Re: CANbus to Instruments etc. working

Post by NArcher »

NArcher wrote:When I got the CAN ID's for the Mazda 3, the guy also gave me the bitmask for the ID's. What would these bismasks be used for? And does it relate to my implementation? The reason I ask is because I cannot get the engine temp gauge to function. The value of it should be X /4 where x is degrees C. Is the Engine_Temp variable in degree C?
Disregard, I figured it out! :roll:
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,

Where do I put the third part of your code to rcv STD 11 msgs? Do I put it under the CANRxIsr()? Thanks
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
@jsmcortina
This is where I put the second part of the code, can someone tell me if it is in the correct place (Shown towards bottom of MS3 code)?

} else if (msg_type == MSG_CRC) { // msg to calc CRC32 of page - actually done in misc
if ((var_blk > 3) && (var_blk < 32)) {
flagbyte4 |= flagbyte4_cantuning;
tble_idx = var_blk;
flagbyte9 |= FLAGBYTE9_CRC_CAN; // set up to do the biz from the mainloop
// txbuf seems a fair place to store this data. Hoping nothing else tries to write it.
canbuf[4] = CAN0_RB_DSR1; // dest var blk
canbuf[5] = rcv_id;
}

} // end msg_type switch
can_status &= CLR_RCV_ERR;
}
else {
msg_type = MSG_STD;}
switch(msg_type) {

case MSG_STD: // message to be processed
msgID = (CAN_RB_IDR0 << 3);
msgID = msgID + (CAN_RB_IDR1 >> 5);
// 8 high bits in IDR0, 3 low bits in IDR1
// update variable value with received data
data[0] = CAN_RB_DSR0;
data[1] = CAN_RB_DSR1;
data[2] = CAN_RB_DSR2;
data[3] = CAN_RB_DSR3;
data[4] = CAN_RB_DSR4;
data[5] = CAN_RB_DSR5;
data[6] = CAN_RB_DSR6;
data[7] = CAN_RB_DSR7;
STD11_receive_CAN(msgID,data,var_byt,MSG_STD) ;

case MSG_CMD: // message for this ecu to set a variable to value in message
break;
}
if ((CAN0RFLG & 0x72) != 0) {
// Rcv error or overrun on receive
can_status |= RCV_ERR;
//can_reset = 1;
}
/* clear RX buf full, err flags to ready for next rcv int */
/* (Note: can't clear err count bits) */
CAN0RFLG = 0xC3;

return;
}

To clarify, I am trying to get the abs wheel sensor data into MS3 to get speedometer. Then I want to send a signal to my instrument cluster to get speedo working. Any help is greatly appreciated!!! :D
NArcher
Helpful MS/Extra'er
Posts: 55
Joined: Wed Mar 23, 2011 1:38 pm

Re: CANbus to Instruments etc. working

Post by NArcher »

If I am trying to get the speed sensor signal to show up in, say CAN ID 1, can I use outpc.canin[0]?
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 »

That field is intended for remote on/off inputs.

The VSS code expects remote VSS information to be deposited into datax1 in one of these fields:

Code: Select all

    union {
        unsigned int pwmin16[4];
        unsigned long pwmin32[4];
    };
    union {
        unsigned int vss1_16;
        unsigned char vss1_8;
    };
    union {
        unsigned int vss2_16;
        unsigned char vss2_8;
    };
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".
NArcher
Helpful MS/Extra'er
Posts: 55
Joined: Wed Mar 23, 2011 1:38 pm

Re: CANbus to Instruments etc. working

Post by NArcher »

jsmcortina wrote:That field is intended for remote on/off inputs.

The VSS code expects remote VSS information to be deposited into datax1 in one of these fields:

Code: Select all

    union {
        unsigned int pwmin16[4];
        unsigned long pwmin32[4];
    };
    union {
        unsigned int vss1_16;
        unsigned char vss1_8;
    };
    union {
        unsigned int vss2_16;
        unsigned char vss2_8;
    };
James
Thanks James!
I know you're busy with the new 1.3, so I appreciate you getting back to me on this. Thanks again!
NArcher
Helpful MS/Extra'er
Posts: 55
Joined: Wed Mar 23, 2011 1:38 pm

Re: CANbus to Instruments etc. working

Post by NArcher »

@jsmcortina
after correcting the errors and compiling my code for vss CAN, I get a "recipe for ms3.elf" failed. Can you explain what this type of error means? Thanks

*EDIT: Got it to compile. It was an error on my part. :oops:
NArcher
Helpful MS/Extra'er
Posts: 55
Joined: Wed Mar 23, 2011 1:38 pm

Re: CANbus to Instruments etc. working

Post by NArcher »

Can someone tell me if this looks right:

Code: Select all

INTERRUPT void CanRxIsr(void)
{
    unsigned char msgID, rcv_id, msg_type, var_blk, var_byt;
    unsigned short var_off, dvar_off, jx, kx;
    unsigned char data[8];

    /* CAN0 Recv Interrupt */

    if (CAN0RFLG & 0x01)

        {
        var_off = ((unsigned short)CAN0_RB_IDR0 << 3) | ((CAN0_RB_IDR1 & 0xE0) >> 5);
        msgID = (CAN0_RB_IDR1 & 0x08);
        msg_type = MSG_CMD;
        if (msgID)
            {
            if ((CAN0_RB_DLR & 0x0F)==15)
                {// &0x0F = 00001111, get first 4 bits
                msg_type = (CAN0_RB_IDR1 & 0x07);
                }
            }
        else
            {
            msg_type = MSG_STD;
            }
        rcv_id = CAN0_RB_IDR2 >> 4;     // message from device rcv_id
        //var_blk = CAN0_RB_IDR3 >> 4;
        var_blk = CAN0_RB_IDR3 >> 4;
        var_byt = CAN0_RB_DLR & 0x0F;
        switch (msg_type)
            {
            case MSG_STD:  // message to be processed
            msgID = (CAN0_RB_IDR0 << 3);
            msgID = msgID + (CAN0_RB_IDR1 >> 5);
            // 8 high bits in IDR0, 3 low bits in IDR1
            // update variable value with received data
            data[0]  =  CAN0_RB_DSR0;
            data[1]  =  CAN0_RB_DSR1;
            data[2]  =  CAN0_RB_DSR2;
            data[3]  =  CAN0_RB_DSR3;
            data[4]  =  CAN0_RB_DSR4;
            data[5]  =  CAN0_RB_DSR5;
            data[6]  =  CAN0_RB_DSR6;
            data[7]  =  CAN0_RB_DSR7;
            STD11_receive_CAN(msgID,data,var_byt,MSG_STD);
            break;
            }

void STD11_receive_CAN(unsigned int Rx_id, unsigned char *Rx_data, unsigned char Rx_dataLen, unsigned char flags)

    {
    // Receive Message Variables
    //unsigned int Rx_id;
    //unsigned char Rx_data[8];
    //unsigned char Rx_dataLen;

    // enable the following for debug if required
    //   ECANSendMessage(Rx_id+1, Rx_data, Rx_dataLen, MSG_STD);
    
    if (flags == MSG_STD)
        {
        flags = MSG_STD;
        }

    if (Rx_id == 0x4B0)
        {// 4th byte of 0x280 is data[3] = (unsigned char)((outpc.rpm*4) /256 ); //byte 4 = RPM, H
        datax1.pwmin16[0] = ((Rx_data[1]) /100);
        datax1.pwmin16[1] = ((Rx_data[0]) /100);
        datax1.pwmin32[2] = ((Rx_data[3]) /10);
        datax1.pwmin32[3] = ((Rx_data[2]) /10);

        }

    /*if (Rx_id == 0x201) // 2nd byte of 0x520 is lights and dimming 58d from 996 cluster
        {
        datax1.vss1_16 = (Rx_data[0]);
        datax1.vss1_8 = (Rx_data[0]);
        }

    Rx_dataLen = 8;

    if (Rx_id == 0x661)
        {
        //h661 Radio/Navi, 8 byte, 100ms
        //beim RNS-E wird hier zum Beispiel angezeigt, in welchem Mode das Geraet ist
        //-h 81 01 12 A0 00 00 00 00 FM/AM
        //-h 81 01 12 2F 00 00 00 00 SAT
        //-h 81 01 12 A5 00 00 00 00 CD
        //-h 81 01 12 A4 00 00 00 00 MP3
        //-h 81 01 12 37 00 00 00 00 TV
        //-h 81 01 12 3A 00 00 00 00 PHONE
    if (Rx_data[3] == 0xA0) //radio active
        {
        //Set Relay On
        PORTE |= 0x01;
        }
    else
        {
        //Set Relay Off
        PORTE &= ~0x01;
        } */
      }
I've been working on it for about a week trying to get the abs signals to vss1 and vss2 with no luck. Thanks
Post Reply