|Click on our menu buttons immediately below to find MegaSquirt® information quickly:|
Below is a 'template' framework for code for the GPIO hardware, in fact the entire Codewarrior project, that can be used to generate your own code for controlling a GPIO board's inputs and outputs for your own custom functions. These can be as complex or as simple as you like.
The C language code project below is designed with the novice programmer in mind, and the configuration and logic is very thoroughly commented. The CAN communications, serial communications, and port set-up is completely finished. The analog-digital converters (ADCs) are set up on some pins/circuits, and there are some timers set up (for things like VR inputs). There are also examples of how to configure outputs.
In most cases, users will need to modify the code to suit their particular needs, and the code is designed to make this possible for the programming neophyte.
There are many hints in the comments included in the source code that explain how to change the existing configuration to suit your needs if you wish. The code has a number of example snippets on how to configure the inputs and outputs based on sample parameters to suit your application. In short, it is a quick step towards writing your own GPIO programs in the C programming language.
By writing your code in the C language, you will learn embedded programming. This is ultimately what the GPIO board and template code project are intended to encourage.
This template code project has:
This program reads all the runtime variables from MS-II™/MicroSquirt®/other GPIO boards, etc. over CAN many times per second and allows you to input logic conditions based on any of the CAN variables as well as the 12 on-board GPIO input channels (6 ADCs, 4 digital, and 2 timer channels), and allows you to use these to control 12 outputs (4 PWM and 8 on/off):
When you use this template project, the first thing to do is to load the S19 in the /BIN/ folder. Set up a project in TunerStudio using the included GPIO.INI file. Load and run the code to verify that the CAN comms are working (there are gauges for CAN Receive and Transmit - Tx should be twice Rx when the GPIO is grabbing the outpc data from the engine ECU). For instructions (and a video) on setting up the CAN pass-through capability, see: the CAN Pass-Through web page.
Then compile the default project code without any changes. Load the resulting S19 file to the controller and test the serial and CAN comms. If that doesn't work, you need to figure out why it doesn't compile properly on your computer (since we know it works on other computers).
You load the template code like any other MShift/MicroSquirt code; the instructions are here: Bootloader Instructions
There is a compatible INI file (GPIO.ini) file for TunerStudio in the main project folder of the zip file above. You can get TunerStudio to activate this file for your template project by:
This code was developed on Freescale's Codewarrior 4.7 software. The updated compiler (technically a "cross-compiler/linker/locator") is available for free as a Special Edition from Freescale (which used to be called Motorola):
You have to register to get the software. (Note that the link may eventually become stale. If it does, start at the Codewarrior page, or the www.freescale.com home page and search for 'codewarrior HCS12(X) special edition')
The 'Special Edition' of Codewarrior has a 32 Kbyte code compile limit, and a maximum of 32 files (the number of files and code size are listed on the bottom left side of the Freescale Codewarrior IDE - integrated development environment). This code is well under those limits at 23 files and ~18 Kbytes.
In addition to the many hints, tips, and examples in the source code itself, there is a draft introduction to programming in C for HCS12 embedded applications on B&G hardware here:
as well as many, many others on the internet.
You will also want the MC9S12C Family Reference Manual, it contains much of the information you will need when programming the GPIO's processor (things like register definitions, etc.). You can get the reference manual here: