Innovate MTS to Megasquirt MSCAN

Dependencies:   CANnucleo mbed

Fork of CANnucleo_Hello by Zoltan Hudak

INNOVATE MTS SERIAL STREAM TO MEGASQUIRT MSCAN ANALOG

This was tested on the olimex olimexino-stm32. code comes with no warrenty, always confirm AFR readings at the device.

I haven't tested this on an ECU or with an LM2 yet this has all been done with simulations, so there may be real world bugs.

code was based off http://developer.mbed.org/users/hudakz/code/CAN_Nucleo_Hello/ mts datasheet http://www.innovatemotorsports.com/support/downloads/Seriallog-2.pdf MSCAN http://www.msextra.com/doc/pdf/Megasquirt_CAN_Broadcast.pdf

** PLEASE NOTE ** MSCAN has table and data offsets to choose what data it requires, this code hasn't implimented any of that so regardless of them it just sends lambda for the first analog value and the mts function value in the second

MTS Serial is RS232 not ttl so you will need an RS-232 Transceiver

main.cpp

Committer:
hudakz
Date:
2015-07-24
Revision:
3:464b06c16d24
Parent:
2:49c9430860d1
Child:
4:ccf4ac2deac8

File content as of revision 3:464b06c16d24:

/*
 * An example showing how to use the CANnucleo library:
 *
 * Two NUCLEO boards are connected to the same CAN bus via CAN transceivers (MCP2551 or TJA1040, or etc.).
 * Transceivers are not part of the NUCLEO boards, therefore must be added by you.
 * Remember also that CAN bus must be terminated with 120 Ohm resitors on both ends.
 * See <https://developer.mbed.org/users/WiredHome/notebook/can---getting-started/>
 * The same source code is used for both NUCLEO boards, but:
 *      For board #1 compile the example without any change.
 *      For board #2 set BOARD1 to 0
 *
 * Note:
 *  To simplify adding/getting data to/from a CAN message
 *  inserter "<<" and extractor ">>" operators have been defined.
 *  Please be aware that CAN message maximum data length is limited to eight bytes.
 *  To make sure this limitation is not violated I recommend to first compile
 *  your application with DEBUG enabled in "CAN.h" file.
 *  Then run it and check for error messages.
 */ 

#include "mbed.h"
#include "CAN.h"

#define BOARD1    1     // please comment out this line when compiling for board #2

#if defined(BOARD1)
    #define RX_ID   0x100
    #define TX_ID   0x101
#else
    #define RX_ID   0x101
    #define TX_ID   0x100
#endif

DigitalOut      led(LED1);
int             ledTarget;
Timer           timer;
CAN             can(PA_11, PA_12);  // rx, tx
CANMessage      rxMsg;
CANMessage      txMsg;
long int        counter = 0;
volatile bool   msgAvailable = false;

/**
 * @brief   'CAN receive-complete' interrup handler.
 * @note    Called on arrival of new CAN message.
 *          Keep it as short as possible.
 * @param   
 * @retval  
 */
void onMsgReceived() {
    msgAvailable = true;
}

/**
 * @brief   Main
 * @note
 * @param 
 * @retval
 */
int main() {
    can.frequency(500000);                      // Initialize CAN and set bit rate to 500kbs
    can.attach(&onMsgReceived, CAN::RxIrq);     // attach 'CAN receive complete' interrupt handler
    timer.reset();
#if defined(BOARD1)
    led = 1;
    timer.start();
#else
    led = 0;
#endif

    while(1) {
        if(timer.read() >= 2.0) {               // check for timeout
            timer.stop();                       // stop timer
            timer.reset();                      // reset timer (to avaoid repeated send)
            counter++;                          // increment counter
            txMsg.clear();                      // clear Tx message storage
            txMsg.id = TX_ID;                   // set ID
            txMsg << counter;                   // append first data item (always make sure that CAN message total data lenght <= 8 bytes!)
            txMsg << led.read();                // append second data item (always make sure that CAN message total data lenght <= 8 bytes!)
            can.write(txMsg);                   // transmit message
            printf("CAN message sent\r\n");
            led = 0;                            // turn off LED
        }
        if(msgAvailable) {
            msgAvailable = false;               // reset flag for next use
            can.read(rxMsg);                    // read message into Rx message storage
            printf("CAN message received:\r\n");
            printf("  ID     = %#x\r\n", rxMsg.id);
            printf("  Type   = %d\r\n", rxMsg.type);
            printf("  Format = %d\r\n", rxMsg.format);
            printf("  Length = %d\r\n", rxMsg.len);
            printf("  Data   =");            
            for(int i = 0; i < rxMsg.len; i++)
                printf(" %x", rxMsg.data[i]);
            printf("\r\n");            
            if(rxMsg.id == RX_ID) {             // if ID matches
                rxMsg >> counter;               // extract first data item
                rxMsg >> ledTarget;             // extract second data item
                led = ledTarget;                // set LED
                printf("counter = %d\r\n", counter);
                timer.start();
            }
        }
    }
}