MegaSquirt-II Embedded Code

Note: MegaSquirt-IIs are generally shipped without embedded code loaded in them. Instead they just have the bootloader code installed. The bootloader allows you to load code through the serial connection (the same one used to communicate with MegaTune). In general, if the bootloader jumper on the MegaSquirt-II is across both the boot pins, no code is installed. If the jumper is on one pin only, code may be installed. The reason the MegaSquirt-IIs are shipped without code is to make sure every user starts with the latest code available. Instructions for loading code to your MegaSquirt-II are below, so keep reading.

The MegaSquirt-II code has all the basic capabilities of MegaSquirt® EFI Controller, however it also has:

Compiled Code for MS-II

There are two hardware versions of MegaSquirt-II, and two corresponding code versions.

The reason is simple: to install CAN on the new MS-II we had to move the fuel and idle pins, since they were using the CAN pins as I/O. This does NOT mean any changes in wiring, because the new fuel and idle go to the same place on the motherboard. We are also using the C64 version of the processor, which has 4K of RAM vs. the 2K on the C32 (the C32 is the one populated on the V1 version of MS-II). This has been the holdup for MS-II, we have been waiting all this time for the automotive-temp version of the C64 to arrive, and this is what we have populated on the V2 version of MS-II. The V2.0 code uses more than the 2K of RAM that the C32 had in order to get all of the CAN variables incorporated in the version 2 embedded code, so the C32 part used on the V1 version cannot handle this (without some condensing of variables, etc). This means you MUST install only v2.x software on the CAN MS2 and only v1.x software on non-CAN MS2.

The real issue between the first released boards (the 170 that were shipped in April 2005 ago) and the ones from August 2005 is that the old boards did not have the CAN transceiver, the new version (V2) has CAN. But to use CAN, we had to move the signal lines on the processor for the fuel pump and fast idle - these happen to be the ones used for the CAN signals (CANH and CANL). So, to be able to use CAN (on V2 version), we had to move the fuel pump and fast idle to other spare I/O pins. This also affected the bootloader - this code has to make sure the fuel pump, injectors, ignition, etc, are off during a bootloader operation. Since the pins moved, we also had to move these as well in the bootloader. In fact, we are hand-programming each and every MegaSquirt-II with the bootloader through the BDM, just to verify this operation and processor/board health. Users still need to put in the version 2 embedded code into the MS-II once they receive it (we do spot check boards with the full code).

The best way to perhaps determine the MS-II version of any board is that the original boards had green LPI solder mask, where as the new V2 boards are now blue solder mask. So MegaSquirt-II version V1 is green, and the latest V2 (with CAN and the extra memory) is blue. The boards also have the "V2.0" or "V2.1" silkscreened onto the V1 boards and "V3.0" silk screened onto the V2 boards, but colors are more fun

The most recent Codewarrior source file of the MegaSquirt-II code for the V2 (CAN) MS-II is here:

main_v2.890.c

The 'ready to load' .s19 file for uploading to MegaSquirt-II(CAN) is here:

Monitor_v2.890.abs.s19
(Right click and 'Save As')

The 2.889 INI file is here (MegaTune may include a 2.8 INI file - but it may be an older version. Please upgrade to the latest version below):

megasquirt-II.ini.2.890
(Right click and 'Save as' the target to your project folder
(usually 'C:Program Files\MegaSquirt\Car1\mtCfg\') then rename it to: megasquirt-II.ini
- ignore warnings about changing file extensions
)

You will also want to copy the settings.ini file to your project's \mtCfg\ folder (usually 'C:Program Files\MegaSquirt\Car1\mtCfg\'). It will solve the "Conditional check references undefined value n2o" and "Conditional check references undefined value 'EXPANDED_CLT_TEMP'" errors.

Note that for 2.88x code, the first thing you must do is set the ECU Type (under 'Fuel Set-Up/General') to match your hardware (MS-II or MicroSquirt™), MegaTune will not let you change anything else until you do this. Do not change settings, expect the stim to work, load an MSQ, etc., until you have set the ECU Type (MS-II = 1, MicroSquirt® = 2, the MS-II Sequencer will be 3). This setting applies to MS-II derivatives with code 2.88 or higher code only.

ECU Type setting was inserted to head off the potential MicroSquirt® problems due to the coils being turned on with the old default configuration. As of the 2.88 code, if ECU Type is not set, the code will put the ignition outputs in a safe state (cycling the rpm from 0 to 8000 rpm, and also flashing the fuel pump LED) and wait until a known ECU type is put in by the user.

Alternatively, if you have the V2.00 Downloader, you can use the 'file/update' function to download both the S19 and INI from the internet ftp site ftp.megamanual.com/updates/MS-II/ and install them to the appropriate locations. Then you don't need to manually download and copy/rename the above files. See www.megamanual.com/ms2/downl2.htm for more info.

Note that you will need to create a new MSQ by hand rather than rely on MegaTune to correctly interpret an old version (you can export/import the tables to save a lot of effort retyping the MSQ settings). Here is the help file for the 2.88 code.

The CodeWarrior project files are here: MSIIC_V2_Proj.zip. You will need the above main.c file, and the hcs12def.h, maffactor.inc, FLASH.H files as well.

The most recent Codewarrior source file of the MegaSquirt-II code for the V1 MS-II is here:

main_v1.34.c

As well, a 'ready to load' .s19 file for uploading to V1 MegaSquirt-II is here:

Monitor_v1.34.abs.s19
(Right click and 'Save As')

The INI file for 1.34 code is numbered 1.31, and is found here: megasquirt-II.ini.1.31

The complete Codewarrior project file is here: MSII_V1_Proj.zip

To load a 'ready to load' .S19 file to your MegaSquirt-II over the serial port, you need to use Eric Fahlgren's MegaSquirt-II downloader program (click the link to download), which works from either a window or from the command line.

To use the downloader.exe:

  1. Power down MegaSquirt® EFI Controller,

  2. Put the boot jumper on both pins of the header marker B/LD (for "bootloader") on the MegaSquirt-II (not the main board),

  3. Power up MegaSquirt® (plug in the stim). The LEDs on MegaSquirt® will flash very briefly (if installed), then go out. This is proper response for the LEDs when the MegaSquirt-II goes into bootload mode (i.e. when the bootload jumper is on).

  4. Start the downloader program, and select the appropriate COM port,

  5. Select the appropriate .S19 file, and the downloader will read, write and verify the code to the processor in about 10 seconds or so.

  6. The process ends with a message like "Verification succeeded, 999 records total (4 skipped)."

  7. Shut down the downloader program.

  8. Remove power from MegaSquirt® (disconnect the stim).

  9. Remove the boot jumper (or put it on just one pin of the B/LD header for storage),

  10. Start MegaTune and set the COM port and speed if necessary. (Make sure you have configured MegaTune to interface with MS-II using the mtCfg.exe utility in the MegaTune folder.)

  11. If you have changed code version (or loaded code for the first time), you may need to change the MegaTune INI file to match the new code. These are located in the MegaTune225/mtCfg/ folder. The files have names like "megasquirt-II.ini.2.0", where 2.0 indicates the code it is to be used with. Start up the MT Configurator (mtCfg.exe), which opens automatically during the installation process (it can be opened directly from the MegaTune folder or the start menu at any later time). Open the MegaTune2.25 tree item, and you'll see a number of megasquirt-II.ini.N.N files. Click on the one that matches the version of embedded code you are running to highlight the .ini file and execute File -> Activate. This will rename it to be "megasquirt-II.ini" (deleting the existing megasquirt-II.ini file first). It will then be used by MegaTune when it starts up.
    (Note that all 2.XY use the same 2.X INI file. For example, the 2.35 code uses the 2.3 INI file. This is the way the code upgrades are designed. Major revisions are incremented by +0.1 and require a new INI file, minor revisions (+0.01) use the same INI file.)

See the readme file in the package for more details.

To use the v1.31 or v2.X code, see the configuration instructions for MegaTune here: www.megamanual.com/ms2/configure.htm

GCC Code for MegaSquirt-II

Users can use the freeware GCC compiler to create and run code customized for particular applications on the MegaSquirt-II and share them with others (think of things like the Dual Table code and MegaSquirt'nSpark-Extra for MegaSquirt). (Note that the 'standard' code above was created on the commercial Codewarrior compiler - which may be available free, but with some limitations.) To use GCC with Windows to write and compile new code for MegaSquirt-II, we need:

To use Linux, please see the sourceforge MS-II project site.

To get and use these:

  1. Download and install the GCC cross-compiler (it is called this because we are writing a program to be executed on one type of processor - the M68HCS12, but using another type of processor to do the compiling, etc. - probably a Pentium).
    1. Install the Windows port of the compiler from cygwin.org. Use the 'Install cygwin now' icon in the upper right (the link takes you directly to the executable setup program). Follow the directions that come up on the screen (i.e., select 'C:\cygwin' as the install directory, 'Run' the file, etc.). Note that if you already have GCC from a previous install, it may not work with the tools below. Install a fresh version before continuing.
    2. Choose 'Install from Internet' when prompted.
    3. When you are asked which packages to install in the cygwin setup window, go to Devel and select make, and 'Install'. Click on 'default' to change the choice to 'Uninstall' for all the rest of the 'packages'.
    4. The download and installation will download and install files for quite a while, this can be a large download (up to an hour with a cable modem at ~90kb/sec).
    5. When it stops, you will be asked to create icons on the desktop and start menu, 'unclick' the boxes so this doesn't happen (you will run the files from the DevC++ IDE or command line instead).

    Note: If you are using MegaSquirt-II V2 (blue PCB) you will need Phil's latest code, the project files are here: www.megamanual.com/ms2/msii_gcc_v23e.zip

    Assuming you used all the default locations and options, you should have a folder called "C:\cygwin\bin\..." on your hard-drive. There should be a file called 'make.exe' there, as well as a number of other files.

  2. Download and install Steven Carrez's HCS12 toolchain. The file is here:

    gnu-68hc1x-3.0.exe
    .
    Accept the license agreement, and install to the default 'C:\usr' directory. Choose a 'full installation' and any other defaults that come up. You can read more about the m68hc12 development chain at www.gnu-m68hc11.org/. There is also a Yahoo! discussion group here: http://groups.yahoo.com/group/gnu-m68hc11/.

    Assuming you used all the default locations and options, you should have a folder called "C:\usr\bin\..." on your hard-drive. There should be files called 'm6811-elf-gcc.exe', 'm6811-elf-objcopy.exe', and 'm6811-elf-objdump.exe' there, as well as a number of other files.
    You can build the compiler from the source if you prefer. Go to http://stephane.carrez.free.fr/m68hc11_inst_ptc.php and follow the instructions to build Pre-Release 3.0, but replace the file 'gcc/config/udivmodsi4.c' (click the link and 'Save As'). This toolchain is covered under the GNU General Public License.

  3. Edit the code to suit your application and interests. The code is written in the C programming language. You need a text editor to modify the files (the code itself main.c, and the header files flash.h and hcs12.h).While you can use notepad.exe, it won't be very convenient. A useful programming tool is a text editor that highlights the syntax using different colors and has other features specifically designed to make programming easier.

    1. Download Dev C++ 4 from www.bloodshed.net/devcpp.html. The ZIP setup file is here: ftp1.sourceforge.net/dev-cpp/devcpp4.zip
    2. UnZIP the file ('Right-click, Extract to...'),
    3. Open the resulting 'devcpp4' folder, and click on the SETUP.EXE file.
    4. Follow the instructions in the installer, choose the defaults whenever possible. This Dev-C++ should install to the C:\Dev-C++ folder, there should be an executable file called DevCpp.exe in this folder.
    5. Download and run the 'MS-II Tools set-up file'. This installs:
      • Phil Johnson's C source code and Makefile (in C:\MSII\),
      • Eric Fahlgren's downloader (in C:\MSII\),
      • Dev-C++ Tools and default files (in C:\Dev-C++), and
      • sets the PATH via a batch file which is run by the 'Make MS-II .S19 file' tool before it runs the makefile.

      Note that if you have installed the files anywhere other than the default locations, you may have to edit the first few lines of the makefile in the \MSII directory. This applies even if you installed it in a drive other than C:
    6. You can then delete the Dev-C++ installation folders (the original ZIP, and the folder you extracted it to),
    7. Open Dev-C++ by clicking on Dev-C++ under 'Start/All Programs/Dev-C++'. Select the options you want as defaults (this only happens the first time you start Dev-C++),
    8. Open the MegaSquirt-II.dev project in the 'C:\MSII' folder under 'My Computer',
    9. Make the changes you want, generally to the main.c code,
    10. Always save your modified files (called 'units' in Dev-C++) under the same name, main.c, otherwise the Makefile will not recognize your changes. It's a good idea to make back-up copies under other names (ex. main_sept13.c) for reference.

    Note that you can also run the compiler from the command line. Click the link to find out more.

  4. Click on 'Tools/Make MS-II .S19 file' in Dev-C++, this will create the downloadable .S19 file, (DO NOT use the 'Execute/Compile' function, it won't work.)

    Note that the compiler checks to see which files have changed (this is controlled by the makefile), and only compiles the the files that are necessary. If nothing has changed, nothing is done.

  5. Download the modified file to MegaSquirt-II:
    1. Remove the power from MegaSquirt-II, place the bootloader jumper on JP5 (B/LD), and apply power again,
    2. Click on 'Tools/Download .S19 file to MS-II' in Dev-C++, this will open the downloader,
    3. Check the 'Settings', be sure they match your configuration (COM port, baud=115200, etc.),
    4. Open the msii.s19 file in the 'C:\MSII' folder, it will load to MS-II automatically,
    5. Remove power from MegaSquirt-II, remove the boot jumper, then reapply the power and the code should be loaded.

Congratulations, you are now a MegaSquirt-II programmer!

You may also want to have a 'differencing tool'. This will highlight the differences between any two files (C source code, asm, .S19, etc., etc.) and can be very useful for determining what changes have be made, and whether two files are identical. One suitable Windows based differencing tool is WinMerge. The set-up file is WinMerge-2.2.0-Setup.exe, and you can read the documentation for it at http://winmerge.sourceforge.net/. The tools for Dev-C++ above include a link to WinMerge (you can delete that tool in Dev-C++ if you don't use WinMerge).

If you want to co-operate with other programmers in the development of MegaSquirt-II code, visit the sourceforge MS-II project site. Here are instructions on how the CVS (Concurrent Versions System) on Sourceforge works.

MS II Serial Interface Description

MegaSquirt-II uses a baud rate of 115200 to communicate with MegaTune. Communication is established when the MegaTune sends a command character - the particular character sets the mode:

The primary commands for sending and receiving data from MS II via the RS232 serial connection are all constructed using 3 initial bytes as follows:

  1. one-letter command: The possibilities for the one letter command are:

  2. CANid - a binary integer byte from 0-15. Use 0 for MS II, for other (GPIO) devices it is arbitrary depending on how a user has configured the devices in his network. Just set to 0 for communication between the PC and MS-II.

  3. <tble_idx> - a binary integer byte from 0-15 that specifies the block of data from which to get/send data. The variable blocks in MS-II corresponding to <tble_idx> 0-6 (only ones at present) are as follows:

For more details, see the source code.



MegaSquirt® and MicroSquirt® controllers are experimental devices intended for educational purposes.
MegaSquirt® and MicroSquirt® controllers are not for sale or use on pollution controlled vehicles. Check the laws that apply in your locality to determine if using a MegaSquirt® or MicroSquirt® controller is legal for your application.
©2004, 2008 Bruce Bowling and Al Grippo. All rights reserved. MegaSquirt® and MicroSquirt® are registered trademarks.