Click on our menu buttons immediately below to find MegaSquirt® information quickly:


GPIO DIY 'Template' Embedded Code

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):

TemplateCWproject_1101.zip
(Contains the Monitor.abs.s19 file you load to your GPIO, it is in the /BIN/ folder.)

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:

  1. Opening a new or exiting TunerStudio project that you will use for your template code,
  2. Using the menus to get to TunerStudio's 'Project -> Project Properties',
  3. Click on 'Show Advanced/Offline Setup'
  4. Click on 'Other/Browse',
  5. Locate the GPIO.INI file and click on it to have TunerStudio activate it for your Project.

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):

http://www.freescale.com/webapp/sps/site/prod_summary.jsp?code=CW-HCS12X

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:

C.htm, a HCS12 C-language primer

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:

MC9S12C/MC9S12GC Family Reference Manual



©2007, 2013 Al Grippo and Bruce Bowling - All rights reserved. MegaSquirt® and MicroSquirt® are registered trademarks.