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:
- code written in the C programming language rather than assembly language,
- better, faster processor (HC9S12C32),
- much higher precision - in the µsec range,
- larger tuning tables (12x12),
- Wide Band O2 sensor closed loop feedback fuel control (with an external driver board),
- Idle Air Controller (IAC), and
- ignition control which assumes 1 coil and a mechanical distributor to distribute spark (or a Ford EDIS on GM DIS system).
- CAN enabled for communications between boards.
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:
- Power down MegaSquirt® EFI Controller,
- Put the boot jumper on both pins of the header marker B/LD (for "bootloader") on the MegaSquirt-II (not the main board),
- 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).
- Start the downloader program, and select the appropriate COM port,
- Select the appropriate .S19 file, and the downloader will read, write and verify the code to the processor in about 10 seconds or so.
- The process ends with a message like "Verification succeeded, 999 records total (4 skipped)."
- Shut down the downloader program.
- Remove power from MegaSquirt® (disconnect the stim).
- Remove the boot jumper (or put it on just one pin of the B/LD header for storage),
- 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.)
- 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:
- GCC make executable files,
- Steven Carrez's HCS12 toolchain.
- Phil Johnson's GCC MegaSquirt-II code to modify,
- Phil Johnson's makefile to ensure the compiler switches are set,
- Dev-C++ development environment and editor, and
- Eric Fahlgren's MS-II code downloader, and other specific MS-II tools.
To use Linux, please see the sourceforge MS-II project site.
To get and use these:
- 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).
- 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.
- Choose 'Install from Internet' when prompted.
- 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'.
- 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).
- 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.
- 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.
- 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.
- Download Dev C++ 4 from www.bloodshed.net/devcpp.html. The ZIP setup file is here: ftp1.sourceforge.net/dev-cpp/devcpp4.zip
- UnZIP the file ('Right-click, Extract to...'),
- Open the resulting 'devcpp4' folder, and click on the SETUP.EXE file.
- 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.
- 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:
- You can then delete the Dev-C++ installation folders (the original ZIP, and the folder you extracted it to),
- 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++),
- Open the MegaSquirt-II.dev project in the 'C:\MSII' folder under 'My Computer',
- Make the changes you want, generally to the main.c code,
- 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.
- 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.
- Download the modified file to MegaSquirt-II:
- Remove the power from MegaSquirt-II, place the bootloader jumper on JP5 (B/LD), and apply power again,
- Click on 'Tools/Download .S19 file to MS-II' in Dev-C++, this will open the downloader,
- Check the 'Settings', be sure they match your configuration (COM port, baud=115200, etc.),
- Open the msii.s19 file in the 'C:\MSII' folder, it will load to MS-II automatically,
- 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:
- one-letter command: The possibilities for the one letter command are:
- 'a' command. Send 'a', 0, <tble_idx>. This causes the 112 bytes from the outpc. structure in MS-II to be sent back to the sender of this command. What is special about this command is that the data is buffered as it is sent so that a word can not be changed between bytes. This isn't necessary with inputs, because they are not changed in the code except through the serial interface.
- 'r' command is a generic form of the 'a' command. It is used to read inputs and the returned bytes from MS II are not buffered. Send 'r', 0, . followed by 2 bytes specifying the offset from the array or structure corresponding to tble_idx, followed by 2 bytes specifying the total number of data bytes desired. The offset word and the number of bytes word must each be sent msb (most significant byte) first, then lsb (least significant byte). After all 7 bytes are received, MS II will start sending the requested number of bytes. Thus to duplicate the 'a' command, you could send 'r', 0, 6, 0, 0, 0 (msb of 112), 112.
- 'w' command is the reverse of the 'r' command - it writes to MS-II. The format of the command is identical to 'r', except send 'w' and follow the seventh byte with the nbytes of data to be written to MS-II RAM.
- 'e' (echo) command is the same as 'w', except that after MS-II has received all the bytes from the sender, it echoes them back for verification. Thus a sender who has sent n bytes to be written should expect n bytes back from MS-II starting after he has sent the last byte to be written.
- 'y' command has format 'y', 0, <tble_idx> and is used to verify that input data in a RAM block matches the corresponding flash block. This is typically used after a flash burn command to verify the data were correctly burned. MS-II sends back a 2 byte word, lsb first, that holds the number of times a ram and flash byte pair failed to match. So if it returns a zero, all is good.
- 'b' command has format 'b', 0, <tble_idx> and is used to tell MS II to burn the RAM copy of <tbl_idx> into the flash copy of it.
There are also some specialized commands for MS-II. These are:
- 't' command. This command is for re-flashing new coolant, MAT, EGO, and MAF tables. These tables are all 1024 word (2byte) tables. The 't' command is followed by <tble_idx> of 0 - 3, to identify the table of interest. At that point the MS-II erases the table flash sector entirely (that is why you do NOT want to be driving while this is going on). So you want to delay about 50 milliseconds to allow the flash to be erased before sending the the rest of the data, which consists of the table words. These are sent in order of ascending index, msb (most significant byte) byte first, then lsb (least significant byte) byte for each of the 1024 16-bit words.
- 'C' command, no other characters, causes the current value of the 16-bit seconds counter to be sent back. It is only used as a check that comms are working.
- 'Q' and 'S' commands (both caps) are used to retrieve two indicators of the MS-II code version. The first is the 20-byte ASCII string for the Rev Number of the code version, the second is for a 32-byte Signature string. The latter is changed in the code whenever a new feature is added, the first is changed in the code whenever there has been an input parameter or output variable added.
- There are also re-initialization, reboot and code reloading commands, all starting with '!'. The complete commands are:
- '!', 'x' causes MS-II to re-initialize various program values, for example the ncylinder-dependent coefficient for computing RPM, to be re-calculated. It should be used only when the following specific variables are changed:
- spark polarity,
- tach input polarity,
- ignition options. including dual spark options,
- warmup enrichment parameters or tables,
- baud rate,
- can_id.
- '!', '!', 'x' If rpm < crank_rpm, this causes the program to reboot itself and start running - not currently used.
- '!', '!', '!' If rpm < crank_rpm, this is the same as reboot, except the program comes up ready to load a new code version - not currently used.
- 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.
- <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:
- 0 = array of coolant temperatures vs ADC counts, 1024 signed 16 bit words; units are degx10, so a value of 1801 represents 180.1 degrees. These are in flash only and are downloaded from the 't' command described later.
- 1 = array of manifold air temperatures. It has the same format as the coolant temp table.
- 2 = EGO reading vs ADC counts array, 1024 signed 16 bit words (147 = 14.7 afr)
- 3 = MAF reading vs ADC counts array, 1024 unsigned 16 bit words, mg/secx10.
- 4 = first input ram structure (see the inpram. structure in the MS-II main.c source code and also see the MegaTune .INI file for offset address and size)
- 5 = second input ram structure (see the in2ram. structure in the MS-II main.c source code and also see the MegaTune .INI file for offset address and size)
- 6 = output ram structure (see the outpc. structure in MS-II and see the MegaTune .INI file for offset address and size)
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.