Innovate MTS to Megasquirt MSCAN
Fork of CANnucleo_Hello by
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
Diff: main.cpp
- Revision:
- 2:49c9430860d1
- Parent:
- 1:267d6288df33
- Child:
- 3:464b06c16d24
--- a/main.cpp Sun Jul 19 14:12:34 2015 +0000 +++ b/main.cpp Thu Jul 23 13:11:08 2015 +0000 @@ -1,7 +1,7 @@ /* * An example showing how to use the CANnucleo library: * - * Two NUCLEO boards are connected to the same CAN bus via CAN transceivers (MPC2551 or TJA1040, or etc.). + * 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/> @@ -36,7 +36,7 @@ CAN can(PA_11, PA_12); // rx, tx CANMessage rxMsg; CANMessage txMsg; -long int counter; +long int counter = 0; volatile bool msgAvailable = false; /** @@ -61,10 +61,10 @@ can.attach(&onMsgReceived, CAN::RxIrq); // attach 'CAN receive complete' interrupt handler timer.reset(); #if defined(BOARD1) - led = 1; - timer.start(); + led = 1; + timer.start(); #else - led = 0; + led = 0; #endif while(1) { @@ -78,15 +78,23 @@ 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 + 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 with ID = %x received\r\n", rxMsg.id); + 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 >> led; // extract second data item (and set led status) + rxMsg >> led; // extract second data item (and set LED status) printf("counter = %d\r\n", counter); timer.start(); }