MegaSquirt® Embedded Code

Q. What is the latest version of the embedded code for MegaSquirt-I?

A. Version 2.98 is a "beta" version for MegaSquirt, which has been re-released as version 3.000. The changes to version 2.98/3.000 over 2.00 are:

; Version 2.98
; Modified boot_r12 file to raise the LVI trip point.
; Fill unused RAM locations with $32, an illegal opcode to force a reset. Done
; to prevent flash erasure if code ever gets into a runaway state - for protection only.
; Put in interlocks to prevent VE/Constant section flash erasure unless specifically
; invoked by a flash burn command.
; Put in Odd-fire RPM averaging (Willette) for odd-fire engines.
; Added new calculation structure to code - same calculation as HI-RES code, but results
; are in 0.1 millisecond units. Calculation overflow fixed.
; Flyback damping code installed - jumper port X0 for INJ1 and jumper port X1 for INJ2.
; Fixed display of WARMCOR variable (no flickering).
; Inhibit PWM mode while cranking.
; Made "ADC" into "ADD in ADC interrupt section (Tom D) to properly perform the sum
; without a carry.
; Perform 12 ms time compare for re-enabling of tach IRQ interrupt (fix for random tach problem) -
; original trial time of 20 ms too long (Magnus Bjelk fix - also for high/low byte check).
; Moved after-start enrichment to increment after "N" tach pulses have occurred,
; with N being the number of cylinders. This will lengthen the afterstart enrichment
; time by an amount scaled by the number of cylinders (Tom).
; Fix display of barometer correction (no screen flicker if 100% barometer is selected).
; Fixed IRQ counter to work properly for NCYL above 8 cylinders.

The version 2.98 code is at:

You really only need the last file (megasquirt.s19) if you are not modifying the code.

Then grab Eric's program download.exe, included in the file in the Files section of the MegaSquirt Forums.

Unzip the file to a convenient directory. Instructions for using download.exe are in DOWNLOAD.TXT in the zip file.

Note that V2.98 is required if you are using the new FlyBack Board.

There are a number of other specialized versions of embedded code for MegaSquirt. See Colin Gebhart's guide for compatibility and files.

MegaSquirt-II has it's own code, and there are two version, depending on your particular MS-II. Find out more here.

What is the latest version of the code for MegaSquirt-II?

A. Find it here:

Q. Q. How can I tell which version of the embedded code I have in my MegaSquirt?

A. If you get into Mini-Term in MegaTune (or hyperterm), and type "S" (must be upper case) with MegaSquirt® running and connected, you should get the 'signature' returned that tells you the code version.

Q. Is there an available description of the serial protocol(s) between the MegaSquirt and MegaTune?

A. To communicate with MegaSquirt-I, you need to set the serial port parameters to 9600, No Parity, 8 bit, 1 stopbit (i.e. 9600,N,8,1). This is done automatically by MegaTune. For MegaSquirt-II, the parameters are (115200,N,8,1) Look at the MegaSquirt assembly code near the section below for complete details on the mode commands.

** SCI Communications
** Communications is established when the PC communications program sends
** a command character - the particular character sets the mode:
** "A" = send all of the realtime variables via txport.
** "V" = send the VE table and constants via txport (128 bytes)
** "W"+{offset}+{newbyte} = receive new VE or constant byte value and
** store in offset location
** "B" = jump to flash burner routine and burn VE/constant values in RAM into flash
** "C" = Test communications - echo back SECL
** "Q" = Send over Embedded Code Revision Number (divide number by 10 - i.e. $21T is rev 2.1)

Q. The Bowling & Grippo hardware is at V3 of the PCB. There was earlier 1.01 and 2.2 versions. Are they firmware compatible?

A. Yes. See the Code section of the manual for details on how to upgrade the firmware.

Q. What are all the files that are used to program my MegaSquirt?

A. Unless you want to edit and recompile the source code, you can upgrade to any version of the code by uploading the corresponding .s19 file.

The .asm file is the human readable source code. Open it up with Notepad and look it over. You may not understand it but it is very well commented.

The .inc files are included by the compiler directed by command in the .asm file. In this case it is used to load tables. This can be read with notepad.

The .h file is called a header file. It contains 'defines' and 'equates' (constants). Open it up with Notepad.

The .s19 is called the 'source record' or S record for short. This is the result of running the .asm through the compiler. This is not the program in binary form but it is a text format that tells the loader where and what to write to the device's memory. You can read this file if you understand Hex and op-codes.

All you really have to know is that you have to upload the .s19 file. See the Code section of the manual for details on how to upgrade the firmware.

Q. What do I need to know to write modifications to the MegaSquirt assembly language code?


Q. What are the processing bottlenecks in MegaSquirt?

The MegaSquirt processor is clocked at 8 MHz. Here are some comparisons:

1) GM ECUs use the HC11 processor, running at either 1 or 2 MHz bus speeds (not crystal, but instruction clock speed). The MegaSquirt 68HC908 processor clocks at either 4 or 8 times faster. The MegaSquirt embedded code is about 1/3 the size of the GM code. In addition, the GM code maintains three timer loops. And, it does ignition function as well.

2) Chrysler systems use 1 MHz HC11 as well. FI, ignition, and alternator control.

3) Electromotive uses the 2 MHz HC11 (they use an ASIC for timing and spark).

4) This one is interesting: the PerfectPower MIC3 computer uses a Microchip PIC17C42, which is a nice part. The clock it at 25 MHz external (crystal) *but* the frequency is divided by 4 (internal) to yield a bus frequency of 6.24 MHz. The MIC3 computer does FI and 4-channel DIS ignition control (wasted spark).

Here is the interesting part - looking at their schematic (which they put in their manual), the ignition triggers come directly off of port pins 7, 15, 18, and 30 (for each channel). If you look at the datasheet for the 17C42, you will see that these are not output-compare ports, but regular I/O ports. So, these triggers must be controlled using software - a timer loop of some sort. A while back on-list, we went over the feasibility of bit-banging ignition triggers, and the resultant accuracy - it wasn't very good with a 0.1 ms timer control at higher RPMs. We don't know what loop rate the MIC3 is running for the timer - but imagine that it is higher than 0.1 ms.

Q. I want to install a switch on the MegaSquirt casing so I can enter bootloader mode without having to open the box. Can I connect a wire to each side of the R6 loops, so that when I press the switch it would "bypass" R6?

A. Yes. Use a normally open push button switch connected across R6, so you can simply enter the bootloader by holding the button and applying power to the MegaSquirt. Since MegaSquirt only watches the voltage across R6 at boot up (for this purpose, anyhow), releasing the button leaves it in bootloader mode until you type "X" to enter to MegaSquirt controller code.

You may want to use a locking toggle switch - just to make sure you do not accidentally flip the switch while in the car. The software only checks for bootloader mode at power up, but if you had the switch closed and did not know it, you may spend a lot of time hunting down a problem that is due to a switch closure.

Q. What is 'Kurt staging'?

A. Those of you wanting to do staged injection you have also got an alternative way, using the standard MegaSquirt® hardware and code. 'Kurt staging' (named after the originator, Kurt Heintz) is a method of improving the dynamic range of a set of injectors. It takes advantage of the fact that the injectors timers are independent, and thus they can overlap. This means that one injection event does not have to finish before the next starts.

To implement Kurt staging, you can use a second set of identical injectors that feed each cylinder and hook the INJ driver #1 to the first set and INJ driver #2 to the second set. What will happen is at idle the injectors will alternate their pulses but as your VE values increase they will start to overlap pulses and allow full flow from both injectors at the same time. In essence, you progress from alternating to simultaneous as pulse widths increase.

The available time, in seconds, to inject (for a 4 stroke cycle) is:

time = 120/RPM * duty_cycle

Whenever the pulse width is greater than of this, Kurt staging will be in effect.

For example, at 6,000 RPM on a with port injection you have a maximum of 85% of 720 to inject, or 17 milliseconds. If you change this to 2 squirts you have 8.5ms per squirt (ignore inj_on). For example, if you had two 19lb injectors per cylinder and suppose VE=100 when the pulswidth was 8.5ms. Up to this point each injector is on separately, i.e. one squirts for 8.5ms, then the other squirts for 8.5 ms. Above this they start overlapping, up to the point that, at VE=200 you have 17ms of injection at 38Lbs. Kurt staging gives twice the dynamic range of a single large injector on standard code.

The main criteria is that the engine must be set up so that it could conceivably run on either bank. For port injection, you must use two injectors per cylinder. For throttle body injection, you don't have any special hardware requirements. All injectors must have the same flow rating. They are then wired up, half to INJ1 bank, and the other half to INJ2 bank.

The injectors are set up to run alternately with the 'injector staging' parameter in MegaTune. For example, on an independent runner 4 cylinder engine you set the MegaSquirt® to 1 squirt/alternating. Each runner will have 1 injector from each bank squirting into it. This will in effect squirt once per cycle into each runner, first from one injector, then from the other on the next cycle. But, these squirts can overlap, so at higher revs, you can have both injectors squirting during the same cycle, injecting more fuel than one injector could.

Kurt staging works with the MegaSquirt® as is, requiring no additional coding. You will need to have all the same size injectors placed as close as you can get them to each other and might need to alternate the injector banks for each INJ driver to get a smooth idle (IE. INJ driver #1 drives "A" injector for cyl #1, drives B injector for cyl #2, drives A injector for cyl #3 and drives B injector for cyl #4; INJ driver #2 would drive the opposite injector as the first INJ driver).

So if you have two equal injectors feeding the same cylinder (with a port injection set-up - TBI are almost always the same size injectors) you can use the above mentioned method, if you are running an extra set of different injectors or up stream injector/injectors you will need to use the dual table code, rather than Kurt staging.

Kurt staging is useful for turbos and also for high revving motors, as well as when it is more convenient to get another set of small injectors to get desired fuel flow (IE. Doubled up 19# injectors on a high powered 351W might be more cost effective than a set of 40#'ers.

Q. My engine turns over quickly during cranking, and it is hard to start. What can I do?

A. You may be cranking faster than the 'cranking enrichment' cut-off speed of 300 rpm, and running off of the VE table while cranking. This can make it difficult to start your engine.

The cranking speed can be changed in the code to suit your application quite easily. MegaSquirt-II allows you to do this as a user setting in MegaTune ('Settings/General Settings' from the main menu).

For the standard MegaSquirt-I code, you need to edit and recompile the code. Find the section of the code that has the following lines:

cmp #$03 ; Check if we are cranking

This is where the cranking rpm check occurs. If rpm exceeds 300 (indicated by #$03), then it branches to the warmup enrichment calculations, and assumes the engine is running. Change the $03 number to suit your cranking speed, and you've got your own custom code version with a higher cranking/running transition point.

Q. I've been told that I need to change something in the MegaSquirt-I code to tell it to go open loop under boost, as I don't want my car running stoichiometric at 10psi, which I've been known to get at throttle. I figure 90kPa is a good place to go open loop. How do I do this?

A. With MegaSquirt-II, you can set this with the 'Settings/EGO Control/Active Below MAP' parameter. For the standard MegaSquirt-I, if you know how to mess around in the .ASM and recompile, here's what you do:

Find the section entitled "MAE"

remark out the following lines by including the semicolon before it:

;lda tps
;cmp #$B2
;bhi SKIPO2

directly under that, add the following 3 lines:

lda kpa
cmp #90T
bhi SkipO2

Recompile the code, load it onto your processor and go...

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, 2005 Bruce Bowling and Al Grippo. All rights reserved. MegaSquirt® and MicroSquirt® are registered trademarks. This document is solely for the support of MegaSquirt® boards from Bowling and Grippo.