EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April 2009 Abstract The Wireless ECU Monitoring System (WEMS) will provide a wireless interface to a car's electronic control unit (ECU) to monitor vital engine parameters and performance statistics. WEMS will automatically log ECU parameters for later retrieval on a home PC through a wireless connection. WEMS will log ECU parameter values such as speed, and derive values such as distance traveled, average acceleration and deceleration in order to provide the user with a clear view of driving habits and automobile performance. The WEMS system will use the logged parameters to automatically notify the user when and why maintenance is required. Team Members: Name: Kirchgessner, Robert Email: [email protected]Phone: 407-267-8043 Name: Houghton, Nathaniel Email: [email protected]Phone: 352-216-3394
174
Embed
Final Report A Wireless ECU Monitoring System Team WEMS
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
EEL 4924 Electrical Engineering Design (Senior Design)
Final ReportA Wireless ECU Monitoring System
Team WEMS21 April 2009
Abstract The Wireless ECU Monitoring System (WEMS) will provide a wireless interface to a car's electronic control unit (ECU) to monitor vital engine parameters and performance statistics. WEMS will automatically log ECU parameters for later retrieval on a home PC through a wireless connection. WEMS will log ECU parameter values such as speed, and derive values such as distance traveled, average acceleration and deceleration in order to provide the user with a clear view of driving habits and automobile performance. The WEMS system will use the logged parameters to automatically notify the user when and why maintenance is required.
Team Members:Name: Kirchgessner, RobertEmail: [email protected]: 407-267-8043
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
Table of ContentsIntroduction................................................................................................................................................4Project Features and Objectives.................................................................................................................5
Hardware Features ................................................................................................................................6Logging and tracking of ECU parameters........................................................................................6Real Time Monitoring of ECU parameters over wireless interface.................................................6Easy ECU interfacing via. ODB-II connector..................................................................................6Retrieval of Error codes....................................................................................................................7Clearing of Error Codes....................................................................................................................7Provide automatic partitioning of non-volatile memory into “sessions”..........................................7
Software Features (PC).........................................................................................................................7Error Code Clearing..........................................................................................................................8Real-time polling of ECU Parameters..............................................................................................8Downloading / Clearing of Logged Parameters...............................................................................8Creation and Uploading of Configuration Files...............................................................................8Calculation of Logged Data Statistics..............................................................................................8
WEMS PC Module..............................................................................................................................12WEMS Car Module.............................................................................................................................13
Challenges................................................................................................................................................13Powering the WEMS Device...............................................................................................................13Interfacing with the ECU ...................................................................................................................14Testing and Debugging........................................................................................................................14
Bill of Materials.......................................................................................................................................15WEMS PC Module..............................................................................................................................15WEMS Car Module.............................................................................................................................15WEMS UI Module..............................................................................................................................16
Division of Labor and Timeline...............................................................................................................17Appendix A – Schematics........................................................................................................................22Appendix B – PCB...................................................................................................................................26Appendix C – Source Code......................................................................................................................29
2
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
Index of FiguresIllustration 1: Software Flow Part 1...........................................................................................................9Illustration 2: Software Flow Part 2.........................................................................................................10Illustration 3: WEMS PC Module............................................................................................................12Illustration 4: WEMS Car Module...........................................................................................................12
Index of TablesTable 1: Overview of Device Features.......................................................................................................5
Index of SchematicsSchematic 1: WEMS Car Module............................................................................................................22Schematic 2: WEMS Car Power Supply..................................................................................................23Schematic 3: WEMS UI...........................................................................................................................24Schematic 4: WEMS PC Module.............................................................................................................25
Index of PCBPCB 1: WEMS Car Module.....................................................................................................................26PCB 2: WEMS UI....................................................................................................................................27PCB 3: WEMS PC Module......................................................................................................................28
3
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
IntroductionModern automobiles use a computer known as an electronic control unit (ECU) to
monitor and control engine parameters in order to optimize engine function and notify the user
(via the check engine light) when a possible malfunction has occurred. Although automobile
maintenance facilities have devices to interface with the ECU, consumers rarely have
access to or have any idea that the ECU exists. The Wireless ECU Monitoring System will
provide the user with access to ECU parameters and engine status via a personal home
computer. This would allow a user to be notified as a car's performance degrades, so the
owner can take action before a major problem occurs. A WEMS module would allow a user to
monitor how their car is performing, and keep track of when it is time to bring their car in for
maintenance.
Products exist that interface with the ECU through the OBD-II interface. These
products, however, are most often connected to a car only during maintenance and tune-ups.
The ECU does not generally keep track of distance traveled, and does not provide access to
a car's odometer reading. Although the distance traveled could be derived from an
automobiles speed over time, an automobile is generally not driven while the ECU device is
attached. By having the WEMS continuously interfaced with the ECU, we will be able to
monitor parameters over time and derive values such as distance traveled, average
acceleration and average deceleration and store them for later retrieval. This will provide
the user with a better view of how their car is performing over time, and even give the user
access to data about driving habits.
One product that offers a wireless Bluetooth connection exists, and is called the
One major difference between this device and the WEMS project is that the WEMS will be
able to log data for retrieval at a later time. The ElmScan 5 Bluetooth adapter simply allows a
real time display of statistics from the ECU.
4
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
Project Features and ObjectivesThe WEMS system will be composed of three major components: a hardware
monitoring system that will be interfaced with an automobiles ECU, a USB device that will be
connected to a home PC, and a software suite that will download and process data from the
WEMS system when the device is in range.
5
Feature Description
Data Logging The WEMS device is capable of logging 9 ECU parameters every half second for up to 8 hours. The data is stored to a 1 Megabyte flash device.
Real Time Data The WEMS device capable of transmitting statistics in real time over its wireless interface instead of logging them to flash. The data is displayed in a Windows application.
Multiple Operation Modes The WEMS device is capable of operating in several modes including:
• An ELM pass through mode.• Logging mode.• Real time statistics mode.• Configuration mode.
Customizable parameter selection The WEMS device allows the user to select which values to request from the engine ECU, and also the time interval between each of these requests.
Graphical and command line tools The configuration file for the WEMS device can be created using command line tools or a graphical user interface. The logged statistics are also downloaded using custom command line tools.
Plotting of logged data A program is provided that allows a gnuplot script to be automatically generated from the logged data. The program tries to select which parameter sets should be displayed on each graph axis.
Derivation of statistics from logged data A statistics program is provide that allows the user to determine several things from the data they have logged, such as:
• The number of stops they made in their vehicle.• The amount of time they spent stopped.• The minimum and maximum of each logged
parameter.
Retrieval of ECU trouble codes The WEMS device allows the trouble codes from your ECU to be retrieved and displayed either through the wireless interface or by using the LCD user interface.
Clearing ECU trouble codes The WEMS device allows the ECU trouble codes to be cleared through the LCD user interface.
Table 1: Overview of Device Features
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
Hardware Features The hardware components of the WEMS system will have the following features:
• Logging and tracking of ECU parameters.
• Real Time Monitoring of ECU parameters over wireless interface (XBee.)
• Easy ECU interfacing via. ODB-II connector.
◦ Support of ISO 9141-2 and ISO 15765 CAN ODB-II standards.
• Retrieval of Error codes.
• Clearing of Error Codes (turning off “Check Engine” light.)
• Provide automatic partitioning of non-volatile memory into “sessions.”
Logging and tracking of ECU parameters.
The WEMS hardware platform will automatically begin logging ECU parameters when
an automobiles ECU is detected and the ignition switch is on. The WEMS system will
periodically store the value of each ECU parameter in non-volatile memory.
Real Time Monitoring of ECU parameters over wireless interface.
Since each parameter is being logged automatically by the WEMS device, access to all
the recorded parameter values will be provided at any time over the wireless XBee interface.
Software will be provided to automatically collect data from the WEMS unit while in real time
mode, and automatically display the values in a formatted table.
Easy ECU interfacing via. ODB-II connector.
The WEMS unit will connect easily to modern automobile's ODB-II connector, and
operate without user intervention once connected. The WEMS device will support two major
ODB-II standards: ISO 9141-2 and ISO 15765 CAN. These standards were chosen because
they are the most widely used standards today.
6
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
Retrieval of Error codes.
The WEMS unit will automatically detect error codes when the ECU reports them, and
display on the codes reported by the ECU on the LCD. The LCD interface provides an easy to
use menu to scroll through the different trouble codes reported.
Clearing of Error Codes
The WEMS unit will provide a menu option to automatically reset any ECU error codes.
This will provide the consumer with a method of checking if the problem has been resolved if
maintenance had to be performed “in the field.”
Provide automatic partitioning of non-volatile memory into “sessions”
Since the WEMS module will typically be utilized for short trips, the hardware will
provide automatic partitioning of logged data in non-volatile memory. By storing these
sessions in memory, it will be possible to download a particular session of a long trip, such as
driving to the supermarket, or driving back home.
Software Features (PC)The computer software component of the WEMS system will have several major
features, including:
• Clearing of Error Codes.
• Real time polling of ECU statistics.
• Downloading / Clearing of logged data.
• Creation and uploading of configuration files.
• Calculation of trip-statistics on logged data.
7
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
Error Code Clearing
After error codes have been received and the vehicle has been serviced appropriately,
the “Check Engine” light may not automatically turn off. In this case, the PC software can be
used to signal the WEMS module to clear the vehicle's ECU error codes. If the problem was
successfully repaired, the check engine light will remain off.
Real-time polling of ECU Parameters
Although one of the main advantages of using WEMS is the ability to have data logged
throughout vehicle operation, real-time polling of ECU statistics will also be possible. Our
software will at least provide a real-time tabular view of the vehicle's ECU statistics.
Downloading / Clearing of Logged Parameters
The statistics logged during vehicle operation can be downloaded through the PC
software any time a vehicle with a WEMS is in wireless range. After the statistics have been
downloaded the software will also provide a way for the user to erase the non-volatile
memory. The amount of flash memory available on the WEMS module will not be expansive
so periodic clearing of logged statistics will be necessary. The WEMS device was engineered
to provide consistent logging at half-second intervals for nine engine parameters for 8 hours.
Creation and Uploading of Configuration Files
The WEMS system provides PC software in order to create and upload configuration
files to the WEMS device wirelessly. The configuration file contains information about which
engine parameters are being logged, at what interval they are being logged and the current
flash address.
Calculation of Logged Data Statistics
The WEMS PC software suite includes tools to provide statistical information about
particular sessions during a trip. Depending on the logged parameters, the statistics utility will
determine the number of stops, the minimum and maximum values for each parameter and
8
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
the total time stopped during the session.
9
Illustration 1: Software Flow Part 1
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
Concept/TechnologyThe hardware chosen for prototyping the WEMS device was chosen in order to
accomplish the goals for the project in a 4 month time-frame. There are two major hardware
modules, the PC module and Car module.
The PC module consists of six major components, the regulated supply for deriving
power from the +12V OBD bus, the wireless interface (XBee Pro), a microprocessor for
performing all of the device functions, a NVM chip for storing logged data and the ELM
interface to communicate the the car ECU. The Car module also has a detachable interface
that consists of a character LCD, three push button switches for menu navigation and an
ignition switch for entering logging mode.
10
Illustration 2: Software Flow Part 2
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
The ATMega 644P microprocessor was chosen due to the larger program capacity and
dual UARTs, which is important for linking between the ELM327 and XBee.
For linking to the car's ODB-II interface, we had several options to choose from. One
possibility is to construct the entire interface circuit from discrete components. This was
quickly eliminated as a possibility, especially as we researched and learned that would not be
implementing one interface, but rather six [2]. Also, since our device will be interfacing to
expensive equipment, we decided it would be better to go with a tested solution that would
allow us to focus on the features of WEMS that we find interesting and useful. As a result, we
have picked the ELM327 chip, which supports all six ODB-II protocols. It requires some
discrete support circuitry, but not nearly as much as would be required to implement its
features on our own. The ELM327 provides a serial link for communication with a computer or
microprocessor.
For our wireless interface there several solutions were considered. These include the
XBee, XBee Pro, and the Nordic nRF24 series. The XBee Pro was chosen for the ease of
integration and support of serial communication with flow control. The XBee Pro was simple
to implement in our system, but packet loss becomes an issue at long ranges, or inside
buildings with thickly insulated walls. In testing, the XBee Pro allowed for wireless
communication without packet loss of about 30 feet through a wall, and up to around 100 feet
line of sight.
For our PC Module we selected the FTDI FT232R. This chip was selected because of
its ease of use, our familiarity with it, and the excellent driver support.
11
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
Project Architecture
WEMS PC ModuleThe block diagram for the WEMS PC module can be seen in Illustration 1. The PC
module derives its power from the host PC's USB bus. To avoid drawing too much current
before notifying the PC, a power MOSFET is used to turn on/off the 5V to 3.3V linear voltage
regulator. To communicate wirelessly with the WEMS car module, the PC module is equipped
12
Illustration 3: WEMS PC Module
Illustration 4: WEMS Car Module
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
with a XBee Pro Series 1 wireless module. The XBee' FTDI FT232R serial to USB IC allows
the PC to communicate with the XBee. The FT232R is also responsible for requesting that the
PC provide us with enough current for the XBee, and after a successful request turns the
power MOSFET on so that the linear voltage regulator and XBee are enabled.
WEMS Car ModuleThe block diagram for the WEMS car module can be seen in Illustration 2. The WEMS
car module derives its power from the car that it is connected to. The OBD-II port provides
direct access to the battery voltage, which is connected to a 12V to 5V switching voltage
regulator. A switching regulator was chosen over a linear voltage regulator because of its
much higher efficiency. Since some of the devices in the car module also require 3.3 volts to
operate, a linear voltage regulator is used to step the 5V from the switching regulator down to
3.3V.
The WEMS car module is controlled by the Atmega 644P microprocessor. This
processor is used to control the overall operation of the WEMS car module. It is responsible
for communicating to the car ECU through the ELM327 chip, communicating with the WEMS
PC module through the XBee, and providing a user interface by means of a two line LCD and
multiple input buttons. All interactions with our device are processed by the Atmega.
The ELM327 requires some simple support circuitry as well as an IC to communicate
on the CAN bus. Refer to the schematic in the Appendix for more details.
ChallengesIn development of the WEMS system, it was expected that we will experience two
major technical challenges: powering the WEMS device and communication with the ECU. An
unexpected challenge was the difficulty of testing and debugging our device.
Powering the WEMS DeviceThe primary source of power to a car's electrical system is a 12V DC battery that is
13
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
used to provide power to an engine's starter, air conditioning, stereo, computer systems,
window defoggers, headlights, brake lights etc. When the current demand from components
is too heavy, or the battery voltage begins to drop, the alternator (which acts as a generator)
switches on to assist with current demand and recharge the 12V battery [1]. Due to all the
high-power systems connected to a car's battery, there is a great deal of noise and power
fluctuations in a cars 12V supply. Since the WEMS device will be driven directly by the 12V
power lines connected via the ODB-II connector, it will be necessary to have the appropriate
power circuitry to protect our device from current and voltage spikes spikes as well as current
and voltage drops during normal car operation. By utilizing a high efficiency buck/boost
switching regulator with a large input voltage range, the problem of powering our device was
eliminated.
Interfacing with the ECU Interfacing to the ECU via the ODB-II interface was difficult due to the large number of
different standards utilized in the automobile industry. The ELM chip, while providing a simple
to use hardware interface to the car's OBD bus, had a large variety of data output formats and
different behaviors for different protocols. Also, depending on the amount of data and the
protocol being used, the output formatting changed significantly, making parsing the data on a
microprocessor with limited buffering a nightmare. In order to make the task of parsing
simpler, routines were made to fetch the data from a character buffer and store the values in a
byte array, making using the hex data returned from the elm simpler to parse. Unfortunately,
this only worked some times, since there are some cases when the elm outputs an odd
number of hexadecimal digits, preventing any typical hex string to integer parsers from
operating correctly.
Testing and DebuggingThe most difficult problem that we encountered in developing the device was testing
and debugging our code on an actual car. Since debugging our device required that we turn
the cars engine on and off multiple times in a short period, we successfully drained two car
batteries during our development.
14
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
PCB Population Kirchgessner, RobertHoughton, Nathaniel
Populate the PCB with components.
18
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
Final Testing Kirchgessner, RobertHoughton, Nathaniel
Testing the device in a car to ensure that the device functions properly. During this stage we will also find out the device limitations, such as wireless range.
Final Presentation/Report
Kirchgessner, RobertHoughton, Nathaniel
The final presentation where we will demonstrate our device (possibly including a video of the device in use.
19
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
References[1] “Understanding Your Import Car's Electrical System,”Autohaus Arizona, Inc . 25 January
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
47 update_crc(&config); 48 if (!valid_config(&config)) { 49 fprintf(stderr, "error: error creating config.\n"); 50 exit(1); 51 } 52 53 dump_config(&config); 54 55 fp = fopen(out_file, "w+b"); 56 if (fp == NULL) { 57 fprintf(stderr, "error: could not open %s\n", out_file); 58 exit(1); 59 } 60 61 r = fwrite(&config, 1, sizeof(config), fp); 62 if (r != sizeof(config)) { 63 fprintf(stderr, "error: could not write config\n"); 64 exit(1); 65 } 66 67 fclose(fp); 68 69 return 0; 70 } 71 72 void 73 update_crc(struct config *conf) 74 { 75 uint8_t *p; 76 uint16_t crc; 77 int i; 78 79 p = (uint8_t *) conf; 80 81 conf->crc = 0; 82 crc = 0xffff; 83 for (i = 0; i < sizeof(*conf); ++i) { 84 crc = crc16_update(crc, *p); 85 ++p; 86 } 87 88 conf->crc = crc; 89 }
<<<<< END pc/mkconfig.c <<<<<
>>>>> BEG pc/obd_pid.c >>>>> 1 /* 2 * Copyright (c) 2009 Nathaniel Houghton <[email protected]> 3 * Robert Kirchgessner <[email protected]> 4 * 5 * Permission to use, copy, modify, and distribute this software for 6 * any purpose with or without fee is hereby granted, provided that 7 * the above copyright notice and this permission notice appear in all 8 * copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA 15 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
38
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 * 19 */ 20 21 #include <stdio.h> 22 23 #include "obd_pid.h" 24 25 const char pid_length[] = {LEN_M0100, LEN_M0101, LEN_M0102, LEN_M0103, LEN_M0104, LEN_M0105, LEN_M0106, LEN_M0107, LEN_M0108, LEN_M0109, LEN_M010A, LEN_M010B, LEN_M010C, LEN_M010D, LEN_M010E, LEN_M010F, 26 LEN_M0110, LEN_M0111, LEN_M0112, LEN_M0113, LEN_M0114, LEN_M0115, LEN_M0116, LEN_M0117, LEN_M0118, LEN_M0119, LEN_M011A, LEN_M011B, LEN_M011C, LEN_M011D, LEN_M011E, LEN_M011F, 27 LEN_M0120, LEN_M0121, LEN_M0122, LEN_M0123, LEN_M0124, LEN_M0125, LEN_M0126, LEN_M0127, LEN_M0128, LEN_M0129, LEN_M012A, LEN_M012B, LEN_M012C, LEN_M012D, LEN_M012E, LEN_M012F, 28 LEN_M0130, LEN_M0131, LEN_M0132, LEN_M0133, LEN_M0134, LEN_M0135, LEN_M0136, LEN_M0137, LEN_M0138, LEN_M0139, LEN_M013A, LEN_M013B, LEN_M013C, LEN_M013D, LEN_M013E, LEN_M013F, 29 LEN_M0140, LEN_M0141, LEN_M0142, LEN_M0143, LEN_M0144, LEN_M0145, LEN_M0146, LEN_M0147, LEN_M0148, LEN_M0149, LEN_M014A, LEN_M014B, LEN_M014C, LEN_M014D, LEN_M014E, LEN_M014F, 30 LEN_M0150, LEN_M0151, LEN_M0152 }; 31 32 33 const char *pid_name[] = { 34 "PIDs supported", 35 "Monitor status since DTCs cleared", 36 "Freeze DTC", 37 "Fuel System Status", 38 "Engine Load", 39 "Engine Coolant Temperature", 40 "Short term fuel % trim - Bank 1", 41 "Long term fuel % trim - Bank 1", 42 "Short term fuel % trim - Bank 2", 43 "Long term fuel % trim - Bank 2", 44 "Fuel Pressure", 45 "Intake Manifold Pressure", 46 "Engine RPM", 47 "Vehicle Speed", 48 "Timing Advance", 49 "Intake Air Temperature", 50 "MAF Air Flow Rate", 51 "Throttle Position", 52 "Commanded Secondary Air Status", 53 "Oxygen Sensors Present", 54 "Bank 1, Sensor 1: Oxygen Sensor Voltage, Short Term Fuel Trim", 55 "Bank 1, Sensor 2: Oxygen Sensor Voltage, Short Term Fuel Trim", 56 "Bank 1, Sensor 3: Oxygen Sensor Voltage, Short Term Fuel Trim", 57 "Bank 1, Sensor 4: Oxygen Sensor Voltage, Short Term Fuel Trim", 58 "Bank 2, Sensor 1: Oxygen Sensor Voltage, Short Term Fuel Trim", 59 "Bank 2, Sensor 2: Oxygen Sensor Voltage, Short Term Fuel Trim", 60 "Bank 2, Sensor 3: Oxygen Sensor Voltage, Short Term Fuel Trim", 61 "Bank 2, Sensor 4: Oxygen Sensor Voltage, Short Term Fuel Trim", 62 "OBD Standard", 63 "Oxygen Sensors Present", 64 "Auxiliary Input Status", 65 "Run time since Engine Start", 66 "PIDs Supported 21-40", 67 "Distance traveled with Malfunction Indicator Lamp On",
39
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
68 "Fuel Rail Pressure", 69 "Fuel Rail Pressure (Diesel)", 70 "O2S1_WR_lamda(1): Equivalence Ratio Voltage", 71 "O2S2_WR_lamda(1): Equivalence Ratio Voltage", 72 "O2S3_WR_lamda(1): Equivalence Ratio Voltage", 73 "O2S4_WR_lamda(1): Equivalence Ratio Voltage", 74 "O2S5_WR_lamda(1): Equivalence Ratio Voltage", 75 "O2S6_WR_lamda(1): Equivalence Ratio Voltage", 76 "O2S7_WR_lamda(1): Equivalence Ratio Voltage", 77 "O2S8_WR_lamda(1): Equivalence Ratio Voltage", 78 "Commanded EGR", 79 "EGR Error", 80 "Commanded Evaporative Purge", 81 "Fuel Level Input", 82 "# of Warm-ups since Codes Cleared", 83 "Distance Traveled since Codes Cleared", 84 "Evap. System Vapor Pressure", 85 "Barometric Pressure", 86 "O2S1_WR_lamda(1): Equivalence Ratio Current", 87 "O2S2_WR_lamda(1): Equivalence Ratio Current", 88 "O2S3_WR_lamda(1): Equivalence Ratio Current", 89 "O2S4_WR_lamda(1): Equivalence Ratio Current", 90 "O2S5_WR_lamda(1): Equivalence Ratio Current", 91 "O2S6_WR_lamda(1): Equivalence Ratio Current", 92 "O2S7_WR_lamda(1): Equivalence Ratio Current", 93 "O2S8_WR_lamda(1): Equivalence Ratio Current", 94 "Catalyst Temperature Bank 1, Sensor 1", 95 "Catalyst Temperature Bank 2, Sensor 1", 96 "Catalyst Temperature Bank 1, Sensor 2", 97 "Catalyst Temperature Bank 2, Sensor 2", 98 "PIDs supported 41-60", 99 "Monitor Status this Drive Cycle", 100 "Control Module Voltage", 101 "Absolute Load Value", 102 "Command Equivalence Ratio", 103 "Relative Throttle Position", 104 "Ambient Air Temperature", 105 "Absolute Throttle Position B", 106 "Absolute Throttle Position C", 107 "Accelerator Pedal Position D", 108 "Accelerator Pedal Position E", 109 "Accelerator Pedal Position F", 110 "Commanded Throttle Actuator", 111 "Time Run with MIL On", 112 "Time Since Trouble Codes Cleared", 113 "Fuel Type", 114 "Ethanol Fuel %", 115 NULL 116 };
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
15 p = (uint8_t *) conf; 16 orig_crc = conf->crc; 17 18 conf->crc = 0; 19 crc = 0xffff; 20 for (i = 0; i < sizeof(*conf); ++i) { 21 crc = crc16_update(crc, *p); 22 ++p; 23 } 24 25 conf->crc = orig_crc; 26 27 if (crc == orig_crc) 28 return 1; 29 30 return 0; 31 } 32 33 int 34 valid_crc(void *vbuf, int len) 35 { 36 uint8_t *p; 37 uint16_t crc, orig_crc; 38 int i; 39 40 p = (uint8_t *) vbuf; 41 memcpy(&orig_crc, p + len - sizeof(crc), sizeof(crc)); 42 memset(p + len - sizeof(crc), 0, sizeof(crc)); 43 44 crc = 0xffff; 45 for (i = 0; i < len; ++i) 46 crc = crc16_update(crc, p[i]); 47 48 if (crc == orig_crc) 49 return 1; 50 51 return 0; 52 } 53 54 uint16_t 55 crc16_update(uint16_t crc, uint8_t a) 56 { 57 int i; 58 59 crc ^= a; 60 for (i = 0; i < 8; ++i) { 61 if (crc & 1) 62 crc = (crc >> 1) ^ 0xA001; 63 else 64 crc = (crc >> 1); 65 } 66 67 return crc; 68 } 69 70 void 71 dump_config(struct config *conf) 72 { 73 int i; 74 int count; 75 76 count = 0;
53
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
77 for (i = 0; i < NUM_LOG_SLOTS; ++i) { 78 if (conf->event[i].interval > 0) { 79 printf("Event %d:\n", count++); 80 printf("\tInterval: %d (%g seconds)\n", 81 conf->event[i].interval, 82 conf->event[i].interval / 2.0); 83 printf("\tCount: %d\n", conf->event[i].count); 84 printf("\tAction: %s\n",conf->event[i].action ? 85 "ACTION_FLASH" : "ACTION_ELM"); 86 printf("\tMode: %02x\n", conf->event[i].mode); 87 88 if (conf->event[i].mode == 1) { 89 printf("\tPID: %s (0x%02x)\n", 90 pid_name[conf->event[i].pid], 91 conf->event[i].pid); 92 } else { 93 printf("\tPID: 0x%02x\n", 94 conf->event[i].pid); 95 } 96 } 97 } 98 99 printf("NVM Start Address: %08x\n", conf->nvm_start_addr); 100 } 101 102 char * 103 pid_units(uint8_t pid) 104 { 105 switch (pid) { 106 case ENGINE_LOAD: 107 case SHORT_TERM_FUEL_TRIM_B1: 108 case LONG_TERM_FUEL_TRIM_B1: 109 case SHORT_TERM_FUEL_TRIM_B2: 110 case LONG_TERM_FUEL_TRIM_B2: 111 case THROTTLE_POS: 112 case FUEL_LEVEL: 113 case FUEL_ETH_PER: 114 return "%"; 115 break; 116 117 case ENGINE_TEMP: 118 case INTAKE_TEMP: 119 return "deg C"; 120 break; 121 122 case PRESSURE_FUEL: 123 case PRESSURE_MANIFOLD: 124 case PRESS_FUELRAIL: 125 return "kPa"; 126 break; 127 128 case ENGINE_RPM: 129 return "RPM"; 130 break; 131 132 case VEHICLE_SPEED: 133 return "mph"; 134 break; 135 136 case TIMING_ADVANCE: 137 return "deg relative to #1 cylinder"; 138 break;
54
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
139 140 case MAF_AIRFLOW: 141 return "g/s"; 142 break; 143 144 case ENGINE_RUNTIME: 145 return "sec"; 146 break; 147 148 default: 149 return "RAW"; 150 break; 151 } 152 } 153 154 double 155 conv_pid_data(uint8_t pid, uint32_t data) 156 { 157 double val; 158 159 switch (pid) { 160 case ENGINE_LOAD: 161 val = CONV_ENGINE_LOAD(data); 162 break; 163 164 case ENGINE_TEMP: 165 val = CONV_ENGINE_TEMP(data); 166 break; 167 168 case SHORT_TERM_FUEL_TRIM_B1: 169 val = CONV_SHORT_TERM_FUEL_TRIM_B1(data); 170 break; 171 172 case LONG_TERM_FUEL_TRIM_B1: 173 val = CONV_LONG_TERM_FUEL_TRIM_B1(data); 174 break; 175 176 case SHORT_TERM_FUEL_TRIM_B2: 177 val = CONV_SHORT_TERM_FUEL_TRIM_B2(data); 178 break; 179 180 case LONG_TERM_FUEL_TRIM_B2: 181 val = CONV_LONG_TERM_FUEL_TRIM_B2(data); 182 break; 183 184 case PRESSURE_FUEL: 185 val = CONV_PRESSURE_FUEL(data); 186 break; 187 188 case PRESSURE_MANIFOLD: 189 val = CONV_PRESSURE_MANIFOLD(data); 190 break; 191 192 case ENGINE_RPM: 193 val = CONV_ENGINE_RPM(data); 194 break; 195 196 case VEHICLE_SPEED: 197 val = CONV_VEHICLE_SPEED(data) * 0.621371192; 198 break; 199 200 case TIMING_ADVANCE:
55
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
201 val = CONV_TIMING_ADVANCE(data); 202 break; 203 204 case INTAKE_TEMP: 205 val = CONV_INTAKE_TEMP(data); 206 break; 207 208 case MAF_AIRFLOW: 209 val = CONV_MAF_AIRFLOW(data); 210 break; 211 212 case THROTTLE_POS: 213 val = CONV_THROTTLE_POS(data); 214 break; 215 216 case ENGINE_RUNTIME: 217 val = CONV_ENGINE_RUNTIME(data); 218 break; 219 220 case PRESS_FUELRAIL: 221 val = CONV_PRESS_FUELRAIL(data); 222 break; 223 224 case PRESS_FUELDIESEL: 225 val = CONV_PRESS_FUELDIESEL(data); 226 break; 227 228 case FUEL_LEVEL: 229 val = CONV_FUEL_LEVEL(data); 230 break; 231 232 case FUEL_ETH_PER: 233 val = CONV_FUEL_ETH_PER(data); 234 break; 235 } 236 237 return val; 238 }
<<<<< END pc/util.c <<<<<
>>>>> BEG software/cmd.c >>>>> 1 /* 2 * Copyright (c) 2009 Nathaniel Houghton <[email protected]> 3 * Robert Kirchgessner <[email protected]> 4 * 5 * Permission to use, copy, modify, and distribute this software for 6 * any purpose with or without fee is hereby granted, provided that 7 * the above copyright notice and this permission notice appear in all 8 * copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA 15 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 * 19 */ 20
56
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
269 270 while ((c = usart_getc(0)) != '\r') { 271 if (isspace(c)) 272 continue; 273 274 i = 0; 275 v->argv[v->argc] = parse_buf[v->argc]; 276 do { 277 v->argv[v->argc][i] = c; 278 c = usart_getc(0); 279 i++; 280 } while (!isspace(c) && i < WORD_LEN - 1); 281 v->argv[v->argc][i] = '\0'; 282 283 /* clean up rest of overflowing word */ 284 while (!isspace(c)) 285 c = usart_getc(0); 286 287 ++v->argc; 288 289 if (v->argc == WC) { 290 /* clean up rest of line */ 291 while (c != '\r') 292 c = usart_getc(0); 293 break; 294 } 295 296 if (c == '\r') 297 break; 298 } 299 }
<<<<< END software/cmd.c <<<<<
>>>>> BEG software/config.c >>>>> 1 /* 2 * Copyright (c) 2009 Nathaniel Houghton <[email protected]> 3 * Robert Kirchgessner <[email protected]> 4 * 5 * Permission to use, copy, modify, and distribute this software for 6 * any purpose with or without fee is hereby granted, provided that 7 * the above copyright notice and this permission notice appear in all 8 * copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA 15 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 * 19 */ 20 21 #include <stdint.h> 22 #include <stdio.h> 23 24 #include <avr/eeprom.h> 25 #include <util/crc16.h> 26 #include <util/delay.h> 27
61
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
<<<<< END software/config.c <<<<<
>>>>> BEG software/elm.c >>>>> 1 /* 2 * Copyright (c) 2009 Nathaniel Houghton <[email protected]> 3 * Robert Kirchgessner <[email protected]> 4 * 5 * Permission to use, copy, modify, and distribute this software for 6 * any purpose with or without fee is hereby granted, provided that 7 * the above copyright notice and this permission notice appear in all 8 * copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA 15 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 * 19 */ 20 21 #include <stdio.h> 22 #include <stdint.h> 23 #include <ctype.h> 24 #include <string.h> 25 26 #include <util/delay.h> 27 28 #include "elm.h" 29 #include "obd_pid.h" 30 #include "util.h" 31 #include "usart.h" 32 #include "ui.h" 33 34 /*Globals*/ 35 uint8_t get_pid_error; /*Error flag. Indicates that there was atleast 1 PID read that had the incorrect value returned*/ 36 elm_dev myelm; 37 38 uint8_t 39 elm_get_protocol(void) 40 { 41 char buf[12]; 42 uint8_t prot; 43 44 snprintf(buf,sizeof(buf), "AT DPN\r" ); 45 usart_puts(DEV_ELM, buf); 46 47 elm_gets(buf, sizeof(buf)); 48 49 if( isalpha( buf[1] ) ) 50 prot = buf[1] + 0xA - 'A'; 51 else 52 prot = buf[1] - '0'; 53 54 elm_flush('>'); 55 56 return prot; 57 }
63
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
58 59 void 60 elm_set_headers( uint8_t val ) 61 { 62 char buf[12]; 63 64 if(val > 1 ) 65 val = 1; 66 67 snprintf(buf,sizeof(buf),"AT H%d\r", val); 68 usart_puts(DEV_ELM, buf); 69 70 elm_flush('>'); 71 72 } 73 74 void 75 elm_select_source( uint8_t source ) /*Supports only 29 bit header*/ 76 { 77 char buf[32]; 78 79 snprintf(buf, sizeof(buf),"AT CF 00 00 00 %02x\r", source ); 80 usart_puts(DEV_ELM, buf); 81 elm_flush('>'); 82 83 snprintf(buf, sizeof(buf),"AT CM 00 00 00 FF\r"); 84 usart_puts(DEV_ELM, buf); 85 elm_flush('>'); 86 } 87 88 #define CAN29_SOURCE_ADDRESS 3 89 90 uint8_t 91 elm_get_eid(void) /*get the engine id, currently only supports 29 bit headers*/ 92 { 93 char buf[32]; 94 uint8_t eid; 95 96 snprintf(buf,sizeof(buf),"01 %02x 1\r", TIMING_ADVANCE ); 97 usart_puts(DEV_ELM, buf); 98 99 elm_readdata( (uint8_t *) buf, sizeof(buf) ); 100 101 eid = buf[CAN29_SOURCE_ADDRESS]; 102 103 elm_flush('>'); 104 105 return eid; 106 } 107 108 /*This function disables echo, sets the protocol, checks to see if it can connect to the bus (and returns 0 if it cannot). It also sets the PID supported values*/ 109 uint8_t 110 elm_init( elm_dev *dev, uint8_t prot ) 111 { 112 char buf[25]; 113 get_pid_error = 0; /*Initialize the error flag to 0*/ 114 115 usart_puts(0, "Initializing ELM...\r\n"); 116 snprintf(buf, sizeof(buf), "Initializing ELM..."); 117 lcd_update(&lcd, 0, buf); 118 lcd_update(&lcd, 1, "");
64
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
119 120 elm_setecho(0); /*Turn off echo*/ 121 122 elm_set_protocol( prot ); /*Set the protocol*/ 123 124 snprintf( buf, sizeof(buf), "01 %2.2x 1\r", PID_SUPPORTED ); /*Perform the PID supported check*/ 125 usart_puts( DEV_ELM, buf ); 126 127 elm_flush('>'); /*Find the prompt*/ 128 129 elm_getpid( PID_SUPPORTED , (uint8_t *) buf , sizeof(buf) ); /*Get the PID again, since last time we just did some error checking.*/ 130 131 if( get_pid_error == 1 ) /*Failed at initializing the ELM*/ 132 { 133 dev->initd = 0; /*Device initialization failed*/ 134 usart_puts(0, "ELM INIT FAILED!\r\n"); 135 lcd_update(&lcd, 0, "Error:"); 136 lcd_update(&lcd, 1, " ELM INIT FAILED!"); 137 _delay_ms(1000); 138 lcd_update(&lcd, 0, ""); 139 lcd_update(&lcd, 1, ""); 140 return 0; 141 } 142 143 memcpy(&dev->caps[0], buf, sizeof(uint32_t)); /*Copy in the bytes returned from the ELM.. in big endian*/ 144 145 dev->caps[1] = 0x0; /*Set the other lists to 0 before checking if they are supported*/ 146 dev->caps[2] = 0x0; 147 148 if( buf[3] & 0x1 ) /*Check if the second set of PIDs are supported. First bit of the last byte is the next PID set flag*/ 149 { 150 elm_getpid( PID_SUPPORTED2 , (uint8_t *) buf, sizeof(buf) ); /*Check if the second set of PIDs are supported. First bit of the last byte is the next PID set flag*/ 151 memcpy(&dev->caps[1], buf, sizeof(uint32_t)); 152 153 if( buf[3] & 0x1 ) /*Chek if the third set of PIDs are supported*/ 154 { 155 elm_getpid( PID_SUPPORTED3 , (uint8_t *) buf, sizeof(buf) ); /*Get the PID again, since last time we just did some error checking.*/ 156 memcpy(&dev->caps[2], buf, sizeof(uint32_t)); 157 dev->caps[2] = byte_swap(dev->caps[2]); /*Set the first set of device capabilities.*/ 158 } 159 } 160 161 dev->prot = elm_get_protocol(); /*Get the protocol because some cars like to mix things up a bit.*/ 162 163 if( PROTCAN( dev->prot ) ) /*If we are in the can protocol, we need to get the eid and set the mask*/ 164 { 165 elm_set_headers(1); 166 167 dev->eid = elm_get_eid(); 168 elm_select_source(dev->eid); 169 170 elm_set_headers(0);
65
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
171 } 172 173 dev->initd = 1; /*Device initialized successfully*/ 174 175 /*Return 1 on success*/ 176 return 1; 177 } 178 179 uint8_t 180 elm_init_retry(elm_dev *dev, uint8_t prot ) 181 { 182 int i; 183 184 for (i = 0; i < ELM_INIT_TRIES; ++i) { 185 if(!elm_init( &myelm, prot) ) 186 elm_reset(); 187 else 188 break; 189 } 190 191 return myelm.initd; 192 } 193 194 /*This function is used to get the value for a PID as a byte array and store it into the buffer. Returns the number of bytes read.*/ 195 /*This function checks that the correct response is returned. If it is not returned, dummy data is returned with the expected number of bytes.*/ 196 uint8_t 197 elm_getpid( uint8_t pid, uint8_t *buf, uint16_t len ) 198 { 199 char tmp[25]; /*The minimum number of characters needed*/ 200 uint8_t read; /*Number of bytes read.*/ 201 uint8_t expected; /*Number of bytes that should be read*/ 202 203 snprintf( tmp, sizeof(tmp), "01 %02X 1\r", pid ); /*Generate MODE01 request string.*/ 204 usart_puts( DEV_ELM, tmp ); /*Send the MODE01 request for the PID.*/ 205 206 elm_readdata( buf, 2 ); /*Read the first two bytes (the tag/PID).*/ 207 208 expected = PID_LENGTH( pid ); /*Get the expected return length from the PID lookup table.*/ 209 210 #if 0 211 usart_puts(0, "\r\n"); 212 snprintf(tmp, sizeof(tmp), "%d\n", expected); 213 usart_puts(0, tmp); 214 usart_puts(0, "\r\n"); 215 #endif 216 217 if( (buf[0]<<8 | buf[1]) == ((0x41 << 8)|pid) ) { /*If a correct response header was read.*/ 218 read = elm_readdata( buf, len ); 219 220 if( read != expected ) { /*Check that the expected number of bytes were read, if not, return the number of bytes that should have been read.*/ 221 read = expected; 222 get_pid_error = 1; /*Set error flag*/ 223 memset( (void*)buf,0x00, read*sizeof(uint8_t) ); /*Set buffer to 0x00*/ 224 } 225 }
66
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
226 else /*Incorrect header response*/ 227 { 228 read = expected; 229 get_pid_error = 1; /*Set error flag*/ 230 memset( (void*)buf,0x00, read*sizeof(uint8_t) ); /*Set buffer to 0x00*/ 231 } 232 233 elm_flush('>'); /*Find the prompt*/ 234 235 return read; 236 237 } 238 239 /*This reads a line of hex from the ELM chip, and puts it into a byte array*/ 240 uint8_t 241 elm_readdata(uint8_t *buf, uint16_t len) 242 { 243 uint8_t c; 244 uint8_t index; 245 uint8_t state; /*Used to detect if we are storing the upper or lower nibble*/ 246 247 index = 0; 248 state = 0; 249 250 while( (c=usart_getc(DEV_ELM)) != '\n' ) { 251 252 if( index == len )/*Incase the buffer gets full.*/ 253 break; 254 255 if( isspace(c) ) 256 continue; 257 258 if( c == '\0' ) 259 continue; 260 261 if( c<='F' && c>='A' ) /*Check if we get a character or not*/ 262 c = c-'A'+10; 263 else 264 c = c-'0'; 265 266 if(state == 0) { /*Upper Nisbble*/ 267 buf[index] = c<<4; 268 state=1; 269 } 270 else { /*Lower Nibble*/ 271 buf[index++] += c; 272 state=0; 273 } 274 } 275 276 return index; 277 } 278 279 /*This reads a string from the ELM chip*/ 280 uint8_t 281 elm_gets(char *buf, uint16_t len) 282 { 283 int c; 284 int index; 285 286 index=0; 287
67
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
288 while( (c=usart_getc(DEV_ELM)) != '\n') 289 { 290 if( c == '\0' ) 291 continue; 292 293 buf[ index++ ] = c; 294 295 if( index == len - 1) 296 break; 297 } 298 299 buf[ index ] = '\0'; 300 301 return index; 302 } 303 304 /*This function toggles echo. 0 = ECHO OFF, 1 = ECHO ON.*/ 305 void 306 elm_setecho( uint8_t val ) 307 { 308 char buf[10]; 309 310 if(val) 311 strlcpy(buf, "AT E1\r", sizeof(buf)); /*Turn echo on*/ 312 else 313 strlcpy(buf, "AT E0\r", sizeof(buf)); /*Turn echo off*/ 314 315 usart_puts( DEV_ELM, buf ); 316 317 elm_flush('>'); /*Wait for prompt*/ 318 319 } 320 321 /*This function sets which protocol to use.*/ 322 void 323 elm_set_protocol( uint8_t protocol ) 324 { 325 char buf[10]; 326 snprintf( buf, sizeof(buf), "AT SP A%1.1x\r", protocol ); /*This searches for the specified prot. If not found, auto searches*/ 327 usart_puts( DEV_ELM, buf ); 328 elm_flush('>'); /*Wait for prompt*/ 329 } 330 331 /*This is used to flush until a character, typically the command prompt carrot ">"*/ 332 void elm_flush(uint8_t ch) 333 { 334 uint8_t c; 335 336 while( (c=usart_getc(DEV_ELM)) != ch ) 337 ; 338 } 339 340 void elm_reset() 341 { 342 char buf[] = "AT Z\r"; 343 usart_puts( DEV_ELM, buf ); 344 elm_flush('>'); 345 }
<<<<< END software/elm.c <<<<<
68
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
>>>>> BEG software/elm_dtc.c >>>>> 1 /* 2 * Copyright (c) 2009 Nathaniel Houghton <[email protected]> 3 * Robert Kirchgessner <[email protected]> 4 * 5 * Permission to use, copy, modify, and distribute this software for 6 * any purpose with or without fee is hereby granted, provided that 7 * the above copyright notice and this permission notice appear in all 8 * copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA 15 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 * 19 */ 20 21 #include <ctype.h> 22 #include <stdio.h> 23 #include <stdint.h> 24 #include <stdlib.h> 25 #include <math.h> 26 27 #include <util/delay.h> 28 29 #include "usart.h" 30 #include "elm.h" 31 #include "obd_pid.h" 32 #include "elm_dtc.h" 33 #include "util.h" 34 #include "ui.h" 35 36 #define DTC_PAIRS_PER_RESPONSE 3 37 38 #define MAX_DTC_CODES 127 39 40 static uint16_t dtc_codes[MAX_DTC_CODES]; 41 static uint8_t curr; 42 static uint8_t milval; /*Value returned from PID01*/ 43 44 uint16_t elm_dtc_get( uint8_t reset ); /*Get the actual DTCs*/ 45 uint8_t elm_dtc_peek(); /*Asks the car for PID01*/ 46 uint16_t elm_dtc_get_single(uint8_t reset); 47 uint16_t elm_dtc_get_multi(uint8_t reset); 48 49 50 uint8_t 51 elm_dtc_count() /*Gets the number of trouble codes detected*/ 52 { 53 return (milval & DTC_COUNT_MSK); 54 } 55 56 uint8_t 57 elm_dtc_mil() /*Gets whether the check engine light is on */ 58 { 59 return (milval & CHECK_ENGINE_MSK); 60 } 61
69
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
62 uint8_t 63 elm_dtc_peek() /*Gets the number of trouble codes detected, and sets check_engine to 1 if the check enchne light is on*/ 64 { 65 uint8_t buf[4]; 66 67 elm_getpid( STATUS_MILDTC, buf, sizeof(buf) ); /*Get the MIL/DTC code*/ 68 milval = buf[0]; 69 70 return buf[0]; /*Return the first byte. MSB is the check engine light flag. The rest is the #DTC codes.*/ 71 } 72 73 void 74 elm_dtc_update() /*Get new DTC list from the car*/ 75 { 76 char buf[] = "03\r"; /*Mode 3 gets the trouble codes*/ 77 uint8_t i; 78 uint8_t num_dtc; /*Current number of DTC*/ 79 80 elm_dtc_peek(); /*Set the milval variable through a mode 1 request*/ 81 82 num_dtc = elm_dtc_count(); /*Get the number of DTCs*/ 83 84 elm_dtc_get(1); /*Reset the counter, doesnt grab any data. Just resets a static variable*/ 85 86 usart_puts( DEV_ELM, buf ); /*Send the MODE03 request to get the DTC*/ 87 88 for( i=0; i<MAX_DTC_CODES && i<num_dtc; ++i ) /*Fill out the static DTC array*/ 89 { 90 dtc_codes[i] = elm_dtc_get(0); /*Get the next DTC*/ 91 92 if(dtc_codes[i] == 0 ) /*Multiline response fail*/ 93 break; 94 } 95 96 curr = 0; /*Reset the current position*/ 97 98 elm_flush('>'); 99 100 return; 101 } 102 103 uint16_t 104 elm_dtc_next() /*Get the next dtc*/ 105 { 106 uint8_t num_dtc; 107 108 num_dtc = elm_dtc_count(); 109 110 if( curr == num_dtc-1 ) /*If we are at the end, goto the beginning*/ 111 curr = 0; 112 else 113 curr = curr+1; /*Otherwise just move to the next element*/ 114 115 return dtc_codes[curr]; 116 } 117 118 uint16_t 119 elm_dtc_current() /*Get the current dtc*/ 120 {
70
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
121 return dtc_codes[curr]; 122 } 123 124 uint16_t 125 elm_dtc_prev() /*Get previous dtc*/ 126 { 127 uint8_t num_dtc; 128 129 num_dtc = elm_dtc_count(); 130 131 if( curr == 0 ) /*If we are at the end, goto the beginning*/ 132 curr = num_dtc-1; 133 else 134 curr = curr-1; /*Otherwise just move to the next element*/ 135 136 return dtc_codes[curr]; 137 } 138 139 void 140 elm_dtc_clear() /*Clear the trouble codes*/ 141 { 142 char buf[] = "04\r"; 143 144 /*Need to do some cleanup for the DRC structure here */ 145 146 /*Clearing the trouble codes is as easy as sending a MODE04 request*/ 147 usart_puts( DEV_ELM, buf ); /*Send the MODE04 request to clear the DTC*/ 148 149 elm_flush('>'); /*Find the prompt*/ 150 } 151 152 uint16_t 153 elm_dtc_get( uint8_t reset ) /*Get the actual DTCs*/ 154 { 155 uint16_t r; 156 157 if( PROTCAN(myelm.prot) && elm_dtc_count() > 2 ) 158 r = elm_dtc_get_multi(reset); 159 else 160 r = elm_dtc_get_single(reset); 161 162 return r; 163 } 164 165 uint16_t 166 elm_dtc_get_single(uint8_t reset) 167 { 168 uint8_t buf[2]; 169 static uint8_t cnt; /*Keep track of how many DTC we read, so we know when to expect the next tag.*/ 170 static uint8_t resp_len; 171 172 if( reset == 1 ) 173 { 174 cnt = 0; 175 return 0; 176 } 177 178 if(cnt == 0 ) 179 { 180 if( PROTCAN(myelm.prot) ) 181 {
71
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
182 elm_readdata( buf, 2 ); /*Read the DTC response bytes */ 183 resp_len = buf[1]; /*The second byte returned (for CAN) is the count of DTCs on that line*/ 184 } 185 else 186 { 187 elm_readdata( buf, 1 ); /*Read the DTC response byte */ 188 resp_len = DTC_PAIRS_PER_RESPONSE; /*Non-can cars have 3 responses per line..*/ 189 } 190 } 191 192 elm_readdata( buf, 2 ); /*Read the DTC in 2 byte pairs until we get the one we want */ 193 cnt++; /*Increment the DTC byte pair counter*/ 194 195 if( cnt == resp_len ) /*End of the current DTC line*/ 196 { 197 cnt = 0; 198 } 199 200 return buf[1]<<8| buf[0]; 201 } 202 203 uint8_t btoh(uint8_t *, char *, uint8_t); 204 205 uint8_t 206 multi_linenum() 207 { 208 char buf[4]; 209 elm_gets(buf, 4); /*Should get the line number, 1 hex digit, a colon and a space.*/ 210 211 buf[0] = toupper(buf[0]); 212 213 hexdump(buf, 3); 214 215 if(buf[1] != ':') { 216 _delay_ms(800); 217 fatal("OMG NO COLON"); 218 } 219 220 if(buf[0]>='A' && buf[0]<='F') 221 return buf[0]-'A' + 0x10; 222 else 223 return buf[0]-'0'; 224 225 } 226 227 #define MULTI_LINE_PACKET_LENGTH 7 228 229 uint16_t 230 elm_dtc_get_multi(uint8_t reset) 231 { 232 static uint16_t line_count; 233 static uint8_t cnt; 234 235 uint16_t code; 236 uint8_t i; 237 uint8_t state; 238 239 uint16_t line;
72
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
>>>>> BEG software/event.c >>>>> 1 /* 2 * Copyright (c) 2009 Nathaniel Houghton <[email protected]> 3 * Robert Kirchgessner <[email protected]> 4 * 5 * Permission to use, copy, modify, and distribute this software for 6 * any purpose with or without fee is hereby granted, provided that 7 * the above copyright notice and this permission notice appear in all 8 * copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA 15 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 * 19 */ 20 21 #include <stdio.h> 22 #include <string.h> 23 24 #include <avr/interrupt.h> 25 #include <util/delay.h> 26 27 #include "usart.h" 28 #include "config.h" 29 #include "event.h" 30 #include "util.h" 31 #include "nvm.h" 32 #include "elm.h" 33 #include "mode.h" 34 #include "ui.h" 35 36 static struct event event[MAX_EVENTS]; 37 static int events_used; 38 39 uint8_t live_mode; 40 41 static volatile struct timer timer; 42 43 /* log delay in seconds */ 44 #define LOG_DELAY 10 45 static uint8_t log_buf[38 * LOG_DELAY]; 46 static uint16_t log_buf_used; 47 static uint32_t cur_nvm_addr; 48 49 #define FLASH_INTERVAL (LOG_DELAY * 2)
74
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
>>>>> BEG software/lcd.c >>>>> 1 //Filename : lcd.c 2 3 //Author : Thomas A. McDonley ([email protected], [email protected]) 4 //Description: 5 //Copyright : (C) 2007 6 //EULA : GNU 7 8 /* LCD Hitachi HD44780u in 4bit mode 9 Copyright (C) 2007 Thomas A. McDonley 10 11 This program is free software; you can redistribute it and/or 12 modify it under the terms of the GNU General Public License 13 as published by the Free Software Foundation; either version 2 14 of the License, or (at your option) any later version. 15 16 This program is distributed in the hope that it will be useful, 17 but WITHOUT ANY WARRANTY; without even the implied warranty of 18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 GNU General Public License for more details. 20 21 You should have received a copy of the GNU General Public License 22 along with this program; if not, write to the Free Software 23 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 24 */ 25 26 #include <avr/io.h> 27 #include <util/delay.h> 28 29 #include "lcd.h" 30 31 //================================================================================================= 32 //Description: 33 //Precondition: 34 //Postcondition: 35 //Returns: 36 void 37 lcd_init(void) 38 { 39 DDRC |= 0xFF; //PORTC(6) is Enable 40 //PORTC 5-0 = R/W | RS | DB7 | DB6 | DB5 | DB4 41 /* Delay for 15ms to allow VCC to settle */ 42 _delay_ms(15); 43
79
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
44 /* Manual Initialization of LCD Driver */ 45 lcd_writeCmd(0x33, 5, 1); 46 47 /* Four bit operation */ 48 lcd_writeCmd(0x32, 5, 2); 49 50 /* Enable 2 lines */ 51 lcd_writeCmd(0x28, 1, 2); 52 53 /* Display on; Cursor On; Blink On */ 54 lcd_writeCmd(0x0C, 1, 2); 55 56 /* Clear Screen; Cursor Home */ 57 lcd_writeCmd(0x01, 1, 2); 58 } 59 //================================================================================================= 60 61 62 //================================================================================================= 63 //Description: Abstracts how commands are written to the LCD driver 64 //Precondition: The lcd has been init and is in 4 bit mode 65 //Postcondition: The command has been executed 66 //Returns: 67 void lcd_writeCmd(uint8_t cmd, uint8_t wdelay, uint8_t edelay) 68 { 69 PORTC = ( (cmd & 0xF0) >> 4) | 0x40; //Write upper nibble to cmd register 70 PORTC ^= 0x40; // Enable = 0... Falling edge signals to read bus 71 72 _delay_ms(wdelay); //Delay 1ms for write to settle [Hold time > 100us] 73 74 PORTC = ( (cmd & 0x0F) ) | 0x40; //Write lower nibble to cmd register 75 PORTC ^= 0x40; 76 77 _delay_ms(edelay); //Delay 2ms (Wait for instruction to execute) 78 } 79 //================================================================================================= 80 81 //================================================================================================= 82 //Description: Abstracts how data is written to the LCD 83 //Precondition: the lcd has been init and is in 4 bit mode 84 //Postcondition: The character appears on the screen at the current cursor posistion 85 //Returns: 86 void lcd_writeData(uint8_t data, uint8_t wdelay, uint8_t edelay) 87 { 88 PORTC = ( (data & 0xF0) >> 4) | 0x40 | 0x10;//Write upper nibble to cmd register... Enable = 1... RS = Data register 89 PORTC ^= 0x40; // Enable = 0... Falling edge signals to read bus 90 91 _delay_ms(wdelay); //Delay 1ms for write to settle [Hold time > 100us] 92 93 PORTC = ( (data & 0x0F) ) | 0x40 | 0x10; //Write lower nibble to cmd register 94 PORTC ^= 0x40;
80
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
95 96 _delay_ms(edelay); //Delay 2ms (Wait for instruction to execute) 97 } 98 //================================================================================================= 99 100 101 //================================================================================================= 102 //Description: Shifts the display either right or left 103 //Precondition: dir is equal to 0 to shift left and 1 to shift right 104 //Postcondition: display has been shifted 105 //Returns: 106 void lcd_shiftDisplay(uint8_t dir) 107 { 108 if(dir == 0) 109 lcd_writeCmd(0x1C, 1, 2); 110 else 111 lcd_writeCmd(0x18, 1, 2); 112 113 } 114 //================================================================================================= 115 116 117 //================================================================================================= 118 //Description: Turns the lcd display off 119 //Precondition: 120 //Postcondition: The display is off 121 //Returns: 122 void lcd_off() 123 { 124 lcd_writeCmd(0x08, 1, 2); 125 } 126 //================================================================================================= 127 128 129 //================================================================================================= 130 //Description: Turns the lcd display on 131 //Precondition: Cursor and blink are set according to what is desired to be on 132 //Postcondition: The display is on 133 //Returns: 134 void lcd_on(uint8_t cursor, uint8_t blink) 135 { 136 if(!cursor && !blink) lcd_writeCmd(0x0C, 1, 2); 137 else if(cursor && blink) lcd_writeCmd(0x0F, 1, 2); 138 else lcd_writeCmd(0x0E, 1, 2); 139 } 140 //================================================================================================= 141 142 143 //================================================================================================= 144 //Description: Take the cursor to the beginning of a line 145 //Precondition: home = 0 for first line, 1 for second 146 //Postcondition: The cursor is at the beginning of a line
81
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
147 //Returns: 148 void lcd_home(uint8_t home) 149 { 150 if (home == 0) 151 lcd_writeCmd(0x02, 1, 2); 152 else 153 lcd_writeCmd(0xC0, 1, 2); 154 } 155 //================================================================================================= 156 157 158 //================================================================================================= 159 //Description: Prints a null terminated character array to the LCD. '\n' will cause it to goto line 2 160 //Precondition: string != NULL and is a pointer to a character array. ex: lcd_print("Hello /nWorld!") 161 //Postcondition: 162 //Returns: 163 void 164 lcd_print(char *string) 165 { 166 while (*string) { 167 if (*string == '\n') 168 lcd_home(1); 169 else 170 lcd_writeData(*string, 0, 1); 171 172 ++string; 173 } 174 } 175 //================================================================================================= 176 177 //================================================================================================= 178 //Description: Clears the LCD screen and moves the cursor to line0 home 179 //Precondition: 180 //Postcondition: 181 //Returns: 182 void 183 lcd_clrscr(void) 184 { 185 lcd_writeCmd(0x01, 0, 2); 186 } 187 //=================================================================================================
<<<<< END software/lcd.c <<<<<
>>>>> BEG software/license.c >>>>> 1 /* 2 * Copyright (c) 2009 Nathaniel Houghton <[email protected]> 3 * Robert Kirchgessner <[email protected]> 4 * 5 * Permission to use, copy, modify, and distribute this software for 6 * any purpose with or without fee is hereby granted, provided that 7 * the above copyright notice and this permission notice appear in all 8 * copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
82
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA 15 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 * 19 */ 20
<<<<< END software/license.c <<<<<
>>>>> BEG software/mode.c >>>>> 1 /* 2 * Copyright (c) 2009 Nathaniel Houghton <[email protected]> 3 * Robert Kirchgessner <[email protected]> 4 * 5 * Permission to use, copy, modify, and distribute this software for 6 * any purpose with or without fee is hereby granted, provided that
7 * the above copyright notice and this permission notice appear in all 8 * copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA 15 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 * 19 */ 20 #include <string.h> 21 #include <stdio.h> 22 #include <stdlib.h> 23 #include <ctype.h> 24 25 #include "usart.h" 26 #include "ui.h" 27 #include "cmd.h" 28 #include "mode.h" 29 30 char *mode_str[] = { 31 "ELM Pass-Thru", 32 "Logging", 33 "Configuration" 34 }; 35 36 uint8_t 37 change_mode(struct mode *mode, uint8_t newmode) 38 { 39 if (mode->mode == newmode) 40 return 1; 41 42 mode->new_mode = newmode; 43 44 if (mode->mode_update == 0) 45 mode->mode_update = 1; 46 47 return 1;
83
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
110 !strcmp(v->argv[1], "mode")) { 111 if (!strcmp(v->argv[2], "elm")) { 112 print_ok = 1; 113 r = change_mode(mode, MODE_ELM); 114 } else if (!strcmp(v->argv[2], "log")) { 115 print_ok = 1; 116 r = change_mode(mode, MODE_LOG); 117 } else if (!strcmp(v->argv[2], "config")) { 118 print_ok = 1; 119 r = change_mode(mode, MODE_CONFIG); 120 } 121 } else if (v->argc == 1 && !strcmp(v->argv[0], "!!!")) { 122 print_ok = 1; 123 r = change_mode(mode, MODE_CONFIG); 124 } 125 126 if (print_ok) 127 usart_puts(0, "OK\r\n"); 128 129 return r; 130 }
<<<<< END software/mode.c <<<<<
>>>>> BEG software/mode_config.c >>>>> 1 /* 2 * Copyright (c) 2009 Nathaniel Houghton <[email protected]> 3 * Robert Kirchgessner <[email protected]> 4 * 5 * Permission to use, copy, modify, and distribute this software for 6 * any purpose with or without fee is hereby granted, provided that 7 * the above copyright notice and this permission notice appear in all 8 * copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA 15 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 * 19 */ 20 #include <string.h> 21 #include <stdio.h> 22 #include <stdlib.h> 23 #include <ctype.h> 24 25 #include "usart.h" 26 #include "ui.h" 27 #include "ui.h" 28 #include "mode.h" 29 #include "cmd.h" 30 31 32 void 33 config_mode(struct mode *mode) 34 { 35 struct cmd cmd; 36 37 if (mode->state != STATE_FF_END) {
85
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
38 if (ui_mode_change(mode)) 39 return; 40 41 parse_line(&cmd); 42 if (ser_mode_change(mode, &cmd)) 43 return; 44 } 45 46 switch (mode->state) { 47 case STATE_0_INIT: 48 menu(mode, 1); 49 change_state(mode, 1); 50 break; 51 52 case 1: 53 menu(mode, 0); 54 config_mode_cmd(&cmd); 55 break; 56 57 case STATE_FF_END: 58 break; 59 } 60 } 61 62 void 63 config_mode_cmd(struct cmd *cmd) 64 { 65 if (cmd->argc < 1) 66 return; 67 68 if (!strcasecmp(cmd->argv[0], "erase_nvm")) 69 erase_nvm_cmd(); 70 else if (!strcasecmp(cmd->argv[0], "readn")) 71 read_cmd(cmd); 72 else if (!strcasecmp(cmd->argv[0], "reade")) 73 read_cmd(cmd); 74 else if (!strcasecmp(cmd->argv[0], "echo")) 75 echo_cmd(cmd); 76 else if (!strcasecmp(cmd->argv[0], "read_cfg")) 77 read_cfg_cmd(cmd); 78 else if (!strcasecmp(cmd->argv[0], "write_cfg")) 79 write_cfg_cmd(cmd); 80 else if (!strcasecmp(cmd->argv[0], "read_log")) 81 read_log_cmd(cmd); 82 else if (!strcasecmp(cmd->argv[0], "read_dtc")) 83 num_dtc_cmd(cmd); 84 else if (!strcasecmp(cmd->argv[0], "set_livemode")) 85 set_livemode_cmd(cmd); 86 else 87 usart_puts(0, "Invalid command.\r\n"); 88 }
<<<<< END software/mode_config.c <<<<<
>>>>> BEG software/mode_elm.c >>>>> 1 /* 2 * Copyright (c) 2009 Nathaniel Houghton <[email protected]> 3 * Robert Kirchgessner <[email protected]> 4 * 5 * Permission to use, copy, modify, and distribute this software for 6 * any purpose with or without fee is hereby granted, provided that 7 * the above copyright notice and this permission notice appear in all
86
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
8 * copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA 15 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 * 19 */ 20 #include <string.h> 21 #include <stdio.h> 22 #include <stdlib.h> 23 #include <ctype.h> 24 25 #include "ui.h" 26 #include "usart.h" 27 #include "mode.h" 28 29 void 30 elm_mode(struct mode *mode) 31 { 32 int c; 33 static int escape_count; 34 char buf[32]; 35 36 switch (mode->state) { 37 case STATE_0_INIT: 38 escape_count = 0; 39 usart_echo(0, 0); 40 snprintf(buf, sizeof(buf), "Mode: %s", 41 mode_str[mode->mode]); 42 lcd_update(&lcd, 0, buf); 43 lcd_update(&lcd, 1, ""); 44 change_state(mode, 1); 45 break; 46 47 case 1: 48 while (usart_rxused(0) > 0) { 49 c = usart_getc(0); 50 if (c == '!') 51 ++escape_count; 52 else { 53 usart_putc(1, c); 54 escape_count = 0; 55 } 56 } 57 58 while (usart_rxused(1) > 0) { 59 c = usart_getc(1); 60 usart_putc(0, c); 61 } 62 63 if (escape_count == 3) 64 change_mode(mode, MODE_CONFIG); 65 break; 66 67 case STATE_FF_END: 68 usart_echo(0, 1); 69 break;
87
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
70 } 71 }
<<<<< END software/mode_elm.c <<<<<
>>>>> BEG software/mode_log.c >>>>> 1 /* 2 * Copyright (c) 2009 Nathaniel Houghton <[email protected]> 3 * Robert Kirchgessner <[email protected]> 4 * 5 * Permission to use, copy, modify, and distribute this software for 6 * any purpose with or without fee is hereby granted, provided that 7 * the above copyright notice and this permission notice appear in all 8 * copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA 15 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 * 19 */ 20 21 #include <string.h> 22 #include <stdio.h> 23 #include <stdlib.h> 24 #include <ctype.h> 25 26 #include "config.h" 27 #include "usart.h" 28 #include "ui.h" 29 #include "event.h" 30 #include "elm.h" 31 #include "mode.h" 32 33 void 34 log_mode(struct mode *mode) 35 { 36 int r; 37 struct cmd cmd; 38 char buf[32]; 39 40 if (mode->state != STATE_FF_END) { 41 if (ui_mode_change(mode)) 42 return; 43 44 parse_line(&cmd); 45 if (ser_mode_change(mode, &cmd)) 46 return; 47 } 48 49 switch (mode->state) { 50 case STATE_0_INIT: 51 /* 52 * Initialize the ELM with ISO9141 as starting 53 * protocol. 54 */ 55 r = elm_init( &myelm, PROT_ISO9141 ); 56 if (!r) {
88
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
>>>>> BEG software/nvm.c >>>>> 1 /* 2 * Copyright (c) 2009 Nathaniel Houghton <[email protected]> 3 * Robert Kirchgessner <[email protected]> 4 * 5 * Permission to use, copy, modify, and distribute this software for 6 * any purpose with or without fee is hereby granted, provided that 7 * the above copyright notice and this permission notice appear in all 8 * copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA 15 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
89
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
>>>>> BEG software/obd_pid.c >>>>> 1 /* 2 * Copyright (c) 2009 Nathaniel Houghton <[email protected]> 3 * Robert Kirchgessner <[email protected]> 4 * 5 * Permission to use, copy, modify, and distribute this software for 6 * any purpose with or without fee is hereby granted, provided that 7 * the above copyright notice and this permission notice appear in all 8 * copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA 15 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 * 19 */ 20 21 #include "obd_pid.h" 22 23 const char pid_length[] = {LEN_M0100, LEN_M0101, LEN_M0102, LEN_M0103, LEN_M0104, LEN_M0105, LEN_M0106, LEN_M0107, LEN_M0108, LEN_M0109, LEN_M010A, LEN_M010B, LEN_M010C, LEN_M010D, LEN_M010E, LEN_M010F, 24 LEN_M0110, LEN_M0111, LEN_M0112, LEN_M0113, LEN_M0114, LEN_M0115, LEN_M0116, LEN_M0117, LEN_M0118, LEN_M0119, LEN_M011A, LEN_M011B, LEN_M011C, LEN_M011D, LEN_M011E, LEN_M011F, 25 LEN_M0120, LEN_M0121, LEN_M0122, LEN_M0123, LEN_M0124, LEN_M0125, LEN_M0126, LEN_M0127, LEN_M0128, LEN_M0129, LEN_M012A, LEN_M012B, LEN_M012C, LEN_M012D, LEN_M012E, LEN_M012F, 26 LEN_M0130, LEN_M0131, LEN_M0132, LEN_M0133, LEN_M0134, LEN_M0135, LEN_M0136, LEN_M0137, LEN_M0138, LEN_M0139, LEN_M013A, LEN_M013B, LEN_M013C, LEN_M013D, LEN_M013E, LEN_M013F, 27 LEN_M0140, LEN_M0141, LEN_M0142, LEN_M0143, LEN_M0144, LEN_M0145, LEN_M0146, LEN_M0147, LEN_M0148, LEN_M0149, LEN_M014A, LEN_M014B, LEN_M014C, LEN_M014D, LEN_M014E, LEN_M014F, 28 LEN_M0150, LEN_M0151, LEN_M0152 };
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
5 * Permission to use, copy, modify, and distribute this software for 6 * any purpose with or without fee is hereby granted, provided that 7 * the above copyright notice and this permission notice appear in all 8 * copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA 15 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 * 19 */ 20 21 #include <stdint.h> 22 23 #include <avr/io.h> 24 #include <util/delay.h> 25 26 void 27 spi_init(void) 28 { 29 /* 30 * PB7 - SCK, PB6 - MISO, PB5 - MOSI, PB4 - /SS. 31 */ 32 /* DDRB = 10110000 */ 33 DDRB = 0xB0; 34 35 /* SPCR = 01011100; pg. 169 */ 36 SPCR = 0x5C; 37 38 /* Enable double speed SPI */ 39 SPSR = 0x01; 40 } 41 42 void 43 spi_tx(const uint8_t *buf, uint8_t len) 44 { 45 uint8_t i; 46 47 for (i = 0; i < len; ++i) { 48 SPDR = buf[i]; 49 50 while (!(SPSR & (1 << SPIF))) 51 ; 52 } 53 } 54 55 void 56 spi_rx(uint8_t *buf, uint8_t len) 57 { 58 uint8_t i; 59 60 for (i = 0; i < len; ++i) { 61 SPDR = 0xFF; 62 63 while (!(SPSR & (1 << SPIF))) 64 ; 65 66 buf[i] = SPDR;
94
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
67 } 68 }
<<<<< END software/spi.c <<<<<
>>>>> BEG software/ui.c >>>>> 1 /* 2 * Copyright (c) 2009 Nathaniel Houghton <[email protected]> 3 * Robert Kirchgessner <[email protected]> 4 * 5 * Permission to use, copy, modify, and distribute this software for 6 * any purpose with or without fee is hereby granted, provided that 7 * the above copyright notice and this permission notice appear in all 8 * copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA 15 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 * 19 */ 20 21 #include <stdio.h> 22 #include <string.h> 23 #include <stdlib.h> 24 25 #include <avr/io.h> 26 #include <avr/interrupt.h> 27 #include <util/delay.h> 28 29 #include "config.h" 30 #include "elm_dtc.h" 31 #include "nvm.h" 32 #include "elm.h" 33 #include "lcd.h" 34 #include "mode.h" 35 #include "obd_pid.h" 36 #include "ui.h" 37 #include "util.h" 38 39 #define DEBOUNCE_DELAY 20 40 41 #define SW_MSK 0x1E 42 #define LCD_MSK 0x01 43 44 /*First Level Menu*/ 45 MENU_RES_DYNAMIC(MENU_ID_ROOT, menu_dynamic_root, MENU_ID_CHECK_DTC, MENU_ID_CHECK_DTC, MENU_ID_CHECK_DTC, NULL ); 46 47 /*First Level Menu*/ 48 MENU_RES_STATIC(MENU_ID_CHECK_DTC, "\x7F Check DTCs \x7E", MENU_ID_NVM_ERASE, MENU_ID_FLASH_STATUS, MENU_ID_DTCMIL, menu_dtc_update ); 49 MENU_RES_DYNAMIC(MENU_ID_FLASH_STATUS, menu_dynamic_flash, MENU_ID_CHECK_DTC, MENU_ID_NVM_ERASE, MENU_ID_FLASH_STATUS, NULL); 50 MENU_RES_STATIC(MENU_ID_NVM_ERASE, "\x7F Erase Flash \x7E", MENU_ID_FLASH_STATUS, MENU_ID_CHECK_DTC, MENU_ID_ROOT, erase_nvm_cmd ); 51 52 /*Second Level Menus*/
95
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
>>>>> BEG software/usart.c >>>>> 1 /* 2 * Copyright (c) 2009 Nathaniel Houghton <[email protected]> 3 * Robert Kirchgessner <[email protected]> 4 * 5 * Permission to use, copy, modify, and distribute this software for 6 * any purpose with or without fee is hereby granted, provided that 7 * the above copyright notice and this permission notice appear in all 8 * copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA 15 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 * 19 */ 20 21 #include <stdio.h> 22 #include <stdint.h> 23 #include <string.h> 24 25 #include <avr/interrupt.h> 26 #include <avr/io.h> 27 #include <util/delay.h> 28
100
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
5 * Permission to use, copy, modify, and distribute this software for 6 * any purpose with or without fee is hereby granted, provided that 7 * the above copyright notice and this permission notice appear in all 8 * copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA 15 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 * 19 */ 20 21 #include <ctype.h> 22 #include <stdint.h> 23 #include <stdio.h> 24 #include <stdlib.h> 25 26 #include "lcd.h" 27 #include "util.h" 28 #include "usart.h" 29 30 /* this thing is crap, but it's not called often... */ 31 uint32_t 32 byte_swap(uint32_t bits) 33 { 34 uint8_t *in, *out; 35 uint32_t ret; 36 37 in = (uint8_t *) &bits; 38 out = (uint8_t *) &ret; 39 40 out[0] = in[3]; 41 out[1] = in[2]; 42 out[2] = in[1]; 43 out[3] = in[0]; 44 45 return ret; 46 } 47 48 void 49 fatal(char *str) 50 { 51 cli(); 52 lcd_clrscr(); 53 lcd_home(0); 54 lcd_print("Terminated:"); 55 lcd_home(1); 56 lcd_print(" "); 57 lcd_print(str); 58 59 for (;;); 60 } 61 62 uint32_t 63 hexstr(char *buf, uint8_t len) 64 { 65 uint32_t n; 66 uint32_t digit = 0;
108
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
67 int pow; 68 int i; 69 70 n = 0; 71 pow = len - 1; 72 while (*buf && pow >= 0) { 73 char c = toupper(*buf); 74 if (c >= '0' && c <= '9') 75 digit = c - '0'; 76 else if (c >= 'A' && c <= 'F') 77 digit = c - 'A' + 10; 78 else 79 fatal("invalid hex digit."); 80 81 for (i = pow; i; --i) 82 digit *= 16; 83 84 n += digit; 85 ++buf; 86 --pow; 87 } 88 89 if (*buf == '\0' && pow >= 0) 90 fatal("hexstr string too short."); 91 92 return n; 93 } 94 95 void 96 hexdump(void *buf, uint16_t len) 97 { 98 char str[4]; 99 uint16_t i; 100 uint8_t *p; 101 102 p = buf; 103 104 for (i = 0; i < len; ++i) { 105 sprintf(str, "%02x ", *p); 106 usart_puts(0, str); 107 ++p; 108 } 109 110 usart_puts(0, "\r\n"); 111 } 112 113 void * 114 xmalloc(size_t n) 115 { 116 void *r; 117 118 r = malloc(n); 119 120 if (r == NULL) 121 fatal("Malloc failure."); 122 123 return r; 124 }
<<<<< END software/util.c <<<<<
>>>>> BEG pc/config.h >>>>>
109
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
1 /* 2 * Copyright (c) 2009 Nathaniel Houghton <[email protected]> 3 * Robert Kirchgessner <[email protected]> 4 * 5 * Permission to use, copy, modify, and distribute this software for 6 * any purpose with or without fee is hereby granted, provided that 7 * the above copyright notice and this permission notice appear in all 8 * copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA 15 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 * 19 */ 20 21 #ifndef CONFIG_HEADER 22 #define CONFIG_HEADER 23 24 #include <stdint.h> 25 26 #include "event.h" 27 28 /* 29 * Can only store 9 parameters every half second for 8 hours 30 * (~36 bytes / sec to NVM). 31 */ 32 #define NUM_LOG_SLOTS 9 33 34 struct config { 35 struct event event[NUM_LOG_SLOTS]; 36 37 /* The address we started logging at. */ 38 uint32_t nvm_start_addr; 39 40 #if 0 41 /* 42 * Disable all logging (including to EEPROM). Useful if the 43 * device is primarily used for diagnostics. 44 */ 45 uint8_t logging_en; 46 47 /* 48 * Byte set before erasing nvm, and cleared after erasing 49 * finishes. If on startup this byte is set in EEPROM, erase 50 * did not complete successfully. Can be expanded to a generic 51 * "status" byte if needed (status could be logging, erasing, 52 * etc.) 53 */ 54 uint8_t erasing_nvm; 55 #endif 56 57 uint16_t crc; 58 } __attribute__((__packed__)); 59 60 void write_config(struct config *); 61 int8_t read_config(struct config *); 62
110
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
63 #endif
<<<<< END pc/config.h <<<<<
>>>>> BEG pc/event.h >>>>> 1 /* 2 * Copyright (c) 2009 Nathaniel Houghton <[email protected]> 3 * Robert Kirchgessner <[email protected]> 4 * 5 * Permission to use, copy, modify, and distribute this software for 6 * any purpose with or without fee is hereby granted, provided that 7 * the above copyright notice and this permission notice appear in all 8 * copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA 15 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 * 19 */ 20 21 #ifndef EVENT_HEADER 22 #define EVENT_HEADER 23 24 #include <stdint.h> 25 26 #include "config.h" 27 28 #define MAX_EVENTS 15 29 #define ACTION_ELM 0 30 #define ACTION_FLASH 1 31 32 struct event { 33 uint8_t interval; 34 uint8_t count; 35 uint8_t action; 36 37 uint8_t mode; 38 uint8_t pid; 39 } __attribute__((__packed__)); 40 41 struct timer { 42 uint8_t ding; 43 uint8_t overflow; 44 }; 45 46 void timer_start(void); 47 void timer_stop(void); 48 int8_t event_init(struct event *); 49 int8_t event_add(struct event *); 50 void events_start(uint32_t); 51 uint8_t events_end(uint32_t *); 52 void events(void); 53 void elm_event(struct event *); 54 void handle_event(struct event *); 55 56 #endif
111
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
<<<<< END pc/event.h <<<<<
>>>>> BEG pc/obd_pid.h >>>>> 1 /* 2 * Copyright (c) 2009 Nathaniel Houghton <[email protected]> 3 * Robert Kirchgessner <[email protected]> 4 * 5 * Permission to use, copy, modify, and distribute this software for 6 * any purpose with or without fee is hereby granted, provided that 7 * the above copyright notice and this permission notice appear in all 8 * copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA 15 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 * 19 */ 20 21 #ifndef OBD_PID_H 22 #define OBD_PID_H 23 24 /*To use the PID length lookup table.*/ 25 extern const char pid_length[]; 26 extern const char *pid_name[]; 27 28 #define MAX_PID 0x52 29 30 /*The following statement returns the length of PID A fromthe PID lookup table.*/ 31 #define PID_LENGTH(A) ( (MAX_PID >= A)?pid_length[A]:0 ) 32 33 /*OBD-II PIDs*/ 34 35 //*Mode 1 PIDs*/ 36 #define PID_SUPPORTED 0x00 //Which PIDs are supported. Bit encoded. PIDs 0x01-0x20 37 #define STATUS_MILDTC 0x01 //Monitor status for MIL and DTCs. Bit encoded. 38 #define FREEZE_DTC 0x02 //Diagnostic Trouble codes 39 #define STATUS_FUELSYS 0x03 //Current fuel system status. Bit encoded. 40 #define ENGINE_LOAD 0x04 //Engine load value. 41 #define ENGINE_TEMP 0x05 //The engine coolant temperature. 42 43 #define SHORT_TERM_FUEL_TRIM_B1 0x06 //Short term fuel % trim, Bank 1. 44 #define LONG_TERM_FUEL_TRIM_B1 0x07 //Long term fuel % trim ,Bank 1. 45 #define SHORT_TERM_FUEL_TRIM_B2 0x08 //Short term fuel % trim, Bank 2. 46 #define LONG_TERM_FUEL_TRIM_B2 0x09 //Long term fuel % trim ,Bank 2. 47 48 #define PRESSURE_FUEL 0x0A //Fuel pressure. 49 #define PRESSURE_MANIFOLD 0x0B //Intake manifold pressure. 50 51 #define ENGINE_RPM 0x0C //Engine RPMs. 52 #define VEHICLE_SPEED 0x0D //Speed of the vehicle. 53 #define TIMING_ADVANCE 0x0E //Engine timing, relative to cylinder 1. 54 #define INTAKE_TEMP 0x0F //Intake air temperature. 55 #define MAF_AIRFLOW 0x10 //MAF Air flow rate. 56 #define THROTTLE_POS 0x11 //Throttle position. 57 #define STATUS_CSA 0x12 //Secondary commanded air status (bit encoded.) 58
112
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
59 #define OXY_PRESENT 0x13 //If oxygen sensors are present. 60 #define OXY_B1_S1 0x14 //Bank1, Sensor 1 61 #define OXY_B1_S2 0x15 //Bank1, Sensor 2 62 #define OXY_B1_S3 0x16 //Bank1, Sensor 3 63 #define OXY_B1_S4 0x17 //Bank1, Sensor 4 64 #define OXY_B2_S1 0x18 //Bank2, Sensor 1 65 #define OXY_B2_S2 0x19 //Bank2, Sensor 2 66 #define OXY_B2_S3 0x1A //Bank3, Sensor 3 67 #define OXY_B2_S4 0x1B //Bank3, Sensor 4 68 69 #define OBD_STANDARD0x1C //Which OBD standard this vehicle supports. 70 #define OXY_PRESENT2 0x1D //Similar to PID 0x13, but bit order changed. 71 72 #define STATUS_AUX 0x1E //Auxilary input status. 73 #define ENGINE_RUNTIME 0x1F //Run time since engine start. 74 75 #define PID_SUPPORTED2 0x20 //Which PIDs are supported. Bit encoded. PIDs 0x21-0x40 76 #define DISTANCE_WMIL 0x21 //Distance travelled with MIL on. 77 #define PRESS_FUELRAIL 0x22 //Fuel rail pressure relative to manifold vacuum. 78 #define PRESS_FUELDIESEL 0x23 //Fuel rail pressure (Diesel) 79 80 #define OS2S1_WR_LAMBDA 0x24 //Equivalence ratio voltage 81 #define OS2S2_WR_LAMBDA 0x25 //Equivalence ratio voltage 82 #define OS2S3_WR_LAMBDA 0x26 //Equivalence ratio voltage 83 #define OS2S4_WR_LAMBDA 0x27 //Equivalence ratio voltage 84 #define OS2S5_WR_LAMBDA 0x28 //Equivalence ratio voltage 85 #define OS2S6_WR_LAMBDA 0x29 //Equivalence ratio voltage 86 #define OS2S7_WR_LAMBDA 0x2A //Equivalence ratio voltage 87 #define OS2S8_WR_LAMBDA 0x2B //Equivalence ratio voltage 88 89 #define COMMANDED_EGR 0x2C //Commanded EGR. 90 #define ERROR_EGR 0x2D //EGR Error. 91 #define FUEL_LEVEL 0x2F //Fuel level input. % 92 93 #define PID_SUPPORTED3 0x40 //PIDs supported 21-60. 94 #define FUEL_TYPE 0x51 //Type of fuel. 01-Gasoline, 02-Methanol, 03-Ethanol, 04-Diesel (10+Gasoline, etc. is electric hybrid. I.e. 11 is gasoline hybrid.) 95 #define FUEL_ETH_PER0x52 //Ethanol fuel %. 96 97 /*Mode 2 PIDs*/ 98 99 #define FREEZE_FRAME_DTC 0x02 100 101 /*Mode 3 PIDs 102 No PIDs required. Mode 3 returns BCD encoded DTC values. 103 Returns 6 byte packets. 104 Each DTC takes 2 bytes. The number of packets is the number of 105 codes divided by 3. 106 107 Each DTC is composed of 5 characters 108 First character - 2 bit encoded Byte0[7..6] (P,C,B,U) 109 Second character - 2 bit encoded Byte0[5..4] (0,1,2,3) 110 Third character - 4 bit encoded Byte0[3..0] (1,2,3,4,5,6,7,8,9) 111 Fourth character - 4 bit encoded Byte1[7..4] (1,2,3,4,5,6,7,8,9) 112 Fifth character - 4 bit encoded Byte1[3..0] (1,2,3,4,5,6,7,8,9); 113 */ 114 115 /* [A] is expected to be in memory as [Byte1][Byte0] . BUF is a buffer that has atleast 6 characters (5 character + null character.)*/ 116 #define DTC_CODE(BUF,A) sprintf(BUF, "%c%d%d%d%d", ( (((A>>6)&0x3)==0)?'P':( (((A>>6)&0x3)==1)?'C':( (((A>>6)&0x3)==2)?'B':( (((A>>6)&0x3)==3)?'U':'?') ) ) ), \
113
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
>>>>> BEG software/cmd.h >>>>> 1 /* 2 * Copyright (c) 2009 Nathaniel Houghton <[email protected]> 3 * Robert Kirchgessner <[email protected]> 4 * 5 * Permission to use, copy, modify, and distribute this software for 6 * any purpose with or without fee is hereby granted, provided that 7 * the above copyright notice and this permission notice appear in all 8 * copies. 9 *
116
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA 15 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 * 19 */ 20 21 #ifndef CMD_H 22 #define CMD_H 23 24 #define WORD_LEN 20 25 #define WC 5 26 27 struct cmd { 28 int argc; 29 char *argv[WC]; 30 }; 31 32 void parse_line(struct cmd *); 33 void config_mode_cmd(struct cmd *); 34 void read_cmd(struct cmd *); 35 void erase_nvm_cmd(void); 36 void echo_cmd(struct cmd *); 37 void read_cfg_cmd(struct cmd *); 38 void write_cfg_cmd(struct cmd *); 39 void read_log_cmd(struct cmd *); 40 void num_dtc_cmd(struct cmd *); 41 void set_livemode_cmd(struct cmd *); 42 43 #endif
<<<<< END software/cmd.h <<<<<
>>>>> BEG software/config.h >>>>> 1 /* 2 * Copyright (c) 2009 Nathaniel Houghton <[email protected]> 3 * Robert Kirchgessner <[email protected]> 4 * 5 * Permission to use, copy, modify, and distribute this software for 6 * any purpose with or without fee is hereby granted, provided that 7 * the above copyright notice and this permission notice appear in all 8 * copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA 15 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 * 19 */ 20 21 #ifndef CONFIG_HEADER 22 #define CONFIG_HEADER 23 24 #include <stdint.h>
117
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
25 26 #include "event.h" 27 28 /* 29 * Can only store 9 parameters every half second for 8 hours 30 * (~36 bytes / sec to NVM). 31 */ 32 #define NUM_LOG_SLOTS 9 33 34 35 struct config { 36 struct event event[NUM_LOG_SLOTS]; 37 38 /* The address we started logging at. */ 39 uint32_t nvm_start_addr; 40 41 #if 0 42 /* 43 * Disable all logging (including to EEPROM). Useful if the 44 * device is primarily used for diagnostics. 45 */ 46 uint8_t logging_en; 47 48 /* 49 * Byte set before erasing nvm, and cleared after erasing 50 * finishes. If on startup this byte is set in EEPROM, erase 51 * did not complete successfully. Can be expanded to a generic 52 * "status" byte if needed (status could be logging, erasing, 53 * etc.) 54 */ 55 uint8_t erasing_nvm; 56 #endif 57 58 uint16_t crc; 59 } __attribute__((__packed__)); 60 61 extern struct config cfg; 62 63 void write_config(struct config *); 64 int8_t read_config(struct config *); 65 uint8_t valid_config(struct config *); 66 67 #endif
<<<<< END software/config.h <<<<<
>>>>> BEG software/elm.h >>>>> 1 /* 2 * Copyright (c) 2009 Nathaniel Houghton <[email protected]> 3 * Robert Kirchgessner <[email protected]> 4 * 5 * Permission to use, copy, modify, and distribute this software for 6 * any purpose with or without fee is hereby granted, provided that 7 * the above copyright notice and this permission notice appear in all 8 * copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA 15 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
118
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 * 19 */ 20 21 #ifndef ELM_H 22 #define ELM_H 23 24 #include <stdint.h> 25 #include <stdio.h> 26 27 #include "elm_prot.h" /*Contains the different protocols supported by the ELM chip.*/ 28 29 #define DEV_ELM 1 /*The ELM is usart device 1*/ 30 31 #define elm_pwrwait(void) elm_flush('>') 32 33 typedef struct { 34 uint32_t caps[3]; /*Which pids are supported. There are 3 sets of PIDs.*/ 35 uint8_t prot; /*The current protocol set*/ 36 uint8_t eid; /*For canbus*/ 37 uint8_t initd; /*Has the interface been initialized*/ 38 } elm_dev; 39 40 typedef struct { 41 uint16_t code; /*Current DRC code*/ 42 } drc; 43 44 extern uint8_t get_pid_error; 45 extern elm_dev myelm; 46 47 #define ELM_INIT_TRIES 2 48 49 uint8_t elm_init( elm_dev *dev, uint8_t prot ); /*Initialize the ELM device. Returns 1 if success.*/ 50 uint8_t elm_init_retry(elm_dev *dev, uint8_t prot ); /*Do init with retries. For Nathans ancient car!*/ 51 uint8_t elm_getpid( uint8_t pid, uint8_t *buf, uint16_t len ); /*Gets the requested PID. Current version does not verify that the PID is supported (up to host software*/ 52 void elm_reset(); 53 void elm_setecho( uint8_t val ); /*Set echo on/off*/ 54 55 /*Generic functions*/ 56 uint8_t elm_gets(char *buf, uint16_t len); /*Reads a string*/ 57 uint8_t elm_readdata( uint8_t *buf, uint16_t len ); /*Read a hex string into a byte array*/ 58 void elm_flush( uint8_t ch ); /*Flush till character*/ 59 60 /*Inline functions*/ 61 void elm_set_protocol( uint8_t protocol ); /*Set the protocol with auto search*/ 62 63 #endif
<<<<< END software/elm.h <<<<<
>>>>> BEG software/elm_dtc.h >>>>> 1 /* 2 * Copyright (c) 2009 Nathaniel Houghton <[email protected]> 3 * Robert Kirchgessner <[email protected]> 4 * 5 * Permission to use, copy, modify, and distribute this software for
119
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
6 * any purpose with or without fee is hereby granted, provided that 7 * the above copyright notice and this permission notice appear in all 8 * copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA 15 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 * 19 */ 20 21 #ifndef ELM_DTC_H 22 #define ELM_DTC_H 23 24 #include <stdint.h> 25 26 #define CHECK_ENGINE_MSK 0x80 27 #define DTC_COUNT_MSK 0x7F 28 29 /*elm_dtc_peek returns 1 byte. The MSB is the check engine flag. The least sig. 7 bits are the # dtc.*/ 30 void elm_dtc_update(); /*Updates the DTC linked list*/ 31 32 uint8_t elm_dtc_count(); /*Gets the number of trouble codes detected*/ 33 uint8_t elm_dtc_mil(); /*Gets whether the check engine light is on */ 34 35 uint16_t elm_dtc_current(); /*Get the current dtc*/ 36 uint16_t elm_dtc_next(); /*Get the next dtc*/ 37 uint16_t elm_dtc_prev(); /*Get previous dtc*/ 38 39 void elm_dtc_clear(); /*Clear the trouble codes*/ 40 41 #endif
<<<<< END software/elm_dtc.h <<<<<
>>>>> BEG software/elm_prot.h >>>>> 1 /* 2 * Copyright (c) 2009 Nathaniel Houghton <[email protected]> 3 * Robert Kirchgessner <[email protected]> 4 * 5 * Permission to use, copy, modify, and distribute this software for 6 * any purpose with or without fee is hereby granted, provided that 7 * the above copyright notice and this permission notice appear in all 8 * copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA 15 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 * 19 */ 20 21 #ifndef ELM_PROT_H
120
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
>>>>> BEG software/event.h >>>>> 1 /* 2 * Copyright (c) 2009 Nathaniel Houghton <[email protected]> 3 * Robert Kirchgessner <[email protected]> 4 * 5 * Permission to use, copy, modify, and distribute this software for 6 * any purpose with or without fee is hereby granted, provided that 7 * the above copyright notice and this permission notice appear in all 8 * copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA 15 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 * 19 */ 20 21 #ifndef EVENT_HEADER 22 #define EVENT_HEADER 23 24 #include <stdint.h> 25 26 #include "mode.h" 27 28 #define MAX_EVENTS 15 29 #define ACTION_ELM 0 30 #define ACTION_FLASH 1 31 32 extern uint8_t live_mode; 33 34 struct event { 35 uint8_t interval; /*Logging interval for the event */ 36 uint8_t count; 37 uint8_t action; /*What action is to be performed*/ 38 39 uint8_t mode;
121
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
>>>>> BEG software/lcd.h >>>>> 1 //Filename : lcd.h 2 3 //Author : Thomas A. McDonley ([email protected], [email protected]) 4 //Description: 5 //Copyright : (C) 2007 6 //EULA : GNU 7 8 /* LCD Hitachi HD44780u or LC7985 compatiable in 4bit mode 9 Copyright (C) 2007 Thomas A. McDonley 10 11 This program is free software; you can redistribute it and/or 12 modify it under the terms of the GNU General Public License 13 as published by the Free Software Foundation; either version 2 14 of the License, or (at your option) any later version. 15 16 This program is distributed in the hope that it will be useful, 17 but WITHOUT ANY WARRANTY; without even the implied warranty of 18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 GNU General Public License for more details. 20 21 You should have received a copy of the GNU General Public License 22 along with this program; if not, write to the Free Software 23 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 24 */ 25 26 #ifndef LCD_H 27 #define LCD_H 28 29 #include <stdint.h> 30 31 #include <avr/io.h> 32 #include <avr/interrupt.h> 33 34 void lcd_init(void); 35 void lcd_writeCmd(uint8_t, uint8_t, uint8_t); 36 void lcd_writeData(uint8_t, uint8_t, uint8_t); 37 38 void lcd_shiftDisplay(uint8_t); 39 void lcd_off(void);
122
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
>>>>> BEG software/mode.h >>>>> 1 /* 2 * Copyright (c) 2009 Nathaniel Houghton <[email protected]> 3 * Robert Kirchgessner <[email protected]> 4 * 5 * Permission to use, copy, modify, and distribute this software for 6 * any purpose with or without fee is hereby granted, provided that 7 * the above copyright notice and this permission notice appear in all 8 * copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA 15 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 * 19 */ 20 21 #ifndef MODE_H 22 #define MODE_H 23 24 #include "cmd.h" 25 26 #define MODE_ELM 0 27 #define MODE_LOG 1 28 #define MODE_CONFIG 2 29 30 #define STATE_0_INIT 0x00 31 #define STATE_FF_END 0xFF 32 33 extern char *mode_str[]; 34 35 struct mode { 36 uint8_t mode; 37 uint8_t state; 38 39 uint8_t new_mode; 40 uint8_t mode_update; 41 42 uint8_t new_state; 43 uint8_t state_update; 44 }; 45 46 uint8_t change_mode(struct mode *mode, uint8_t newmode); 47 void change_state(struct mode *mode, uint8_t newstate); 48 void mode_seq(struct mode *); 49 void mode_handle(struct mode *); 50 uint8_t ser_mode_change(struct mode *, struct cmd *); 51 uint8_t ui_mode_change(struct mode *);
123
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
>>>>> BEG software/nvm.h >>>>> 1 /* 2 * Copyright (c) 2009 Nathaniel Houghton <[email protected]> 3 * Robert Kirchgessner <[email protected]> 4 * 5 * Permission to use, copy, modify, and distribute this software for 6 * any purpose with or without fee is hereby granted, provided that 7 * the above copyright notice and this permission notice appear in all 8 * copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA 15 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 * 19 */ 20 21 #ifndef NVM_HEADER 22 #define NVM_HEADER 23 24 #include <stdint.h> 25 26 #define NVM_READ 0x00 27 #define NVM_WRITE 0x01 28 29 #define NVM_SET 0x00 30 #define NVM_CLR 0x01 31 32 #define nvm_unlock() (nvm_status(NVM_WRITE, 0x00)) 33 34 #define NVM_PAGE_SIZE 256 35 #define NVM_NUM_PAGES 4096 36 #define NVM_LAST_ADDR (NVM_PAGE_SIZE * NVM_NUM_PAGES - 1) 37 38 void nvm_read(void *, uint32_t, uint8_t); 39 uint8_t nvm_status(uint8_t, uint8_t); 40 void nvm_wel(uint8_t); 41 void nvm_write(uint32_t, void *, uint16_t); 42 int nvm_erase(void); 43 44 #endif
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
5 * Permission to use, copy, modify, and distribute this software for 6 * any purpose with or without fee is hereby granted, provided that 7 * the above copyright notice and this permission notice appear in all 8 * copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA 15 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 * 19 */ 20 21 #ifndef OBD_PID_H 22 #define OBD_PID_H 23 24 /*To use the PID length lookup table.*/ 25 extern const char pid_length[]; 26 27 #define MAX_PID 0x52 28 29 /*The following statement returns the length of PID A fromthe PID lookup table.*/ 30 #define PID_LENGTH(A) ( (MAX_PID >= A)?pid_length[A]:0 ) 31 32 /*OBD-II PIDs*/ 33 34 //*Mode 1 PIDs*/ 35 #define PID_SUPPORTED 0x00 //Which PIDs are supported. Bit encoded. PIDs 0x01-0x20 36 #define STATUS_MILDTC 0x01 //Monitor status for MIL and DTCs. Bit encoded. 37 #define FREEZE_DTC 0x02 //Diagnostic Trouble codes 38 #define STATUS_FUELSYS 0x03 //Current fuel system status. Bit encoded. 39 #define ENGINE_LOAD 0x04 //Engine load value. 40 #define ENGINE_TEMP 0x05 //The engine coolant temperature. 41 42 #define SHORT_TERM_FUEL_TRIM_B1 0x06 //Short term fuel % trim, Bank 1. 43 #define LONG_TERM_FUEL_TRIM_B1 0x07 //Long term fuel % trim ,Bank 1. 44 #define SHORT_TERM_FUEL_TRIM_B2 0x08 //Short term fuel % trim, Bank 2. 45 #define LONG_TERM_FUEL_TRIM_B2 0x09 //Long term fuel % trim ,Bank 2. 46 47 #define PRESSURE_FUEL 0x0A //Fuel pressure. 48 #define PRESSURE_MANIFOLD 0x0B //Intake manifold pressure. 49 50 #define ENGINE_RPM 0x0C //Engine RPMs. 51 #define VEHICLE_SPEED 0x0D //Speed of the vehicle. 52 #define TIMING_ADVANCE 0x0E //Engine timing, relative to cylinder 1. 53 #define INTAKE_TEMP 0x0F //Intake air temperature. 54 #define MAF_AIRFLOW 0x10 //MAF Air flow rate. 55 #define THROTTLE_POS 0x11 //Throttle position. 56 #define STATUS_CSA 0x12 //Secondary commanded air status (bit encoded.) 57 58 #define OXY_PRESENT 0x13 //If oxygen sensors are present. 59 #define OXY_B1_S1 0x14 //Bank1, Sensor 1 60 #define OXY_B1_S2 0x15 //Bank1, Sensor 2 61 #define OXY_B1_S3 0x16 //Bank1, Sensor 3 62 #define OXY_B1_S4 0x17 //Bank1, Sensor 4 63 #define OXY_B2_S1 0x18 //Bank2, Sensor 1 64 #define OXY_B2_S2 0x19 //Bank2, Sensor 2 65 #define OXY_B2_S3 0x1A //Bank3, Sensor 3
125
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
66 #define OXY_B2_S4 0x1B //Bank3, Sensor 4 67 68 #define OBD_STANDARD0x1C //Which OBD standard this vehicle supports. 69 #define OXY_PRESENT2 0x1D //Similar to PID 0x13, but bit order changed. 70 71 #define STATUS_AUX 0x1E //Auxilary input status. 72 #define ENGINE_RUNTIME 0x1F //Run time since engine start. 73 74 #define PID_SUPPORTED2 0x20 //Which PIDs are supported. Bit encoded. PIDs 0x21-0x40 75 #define DISTANCE_WMIL 0x21 //Distance travelled with MIL on. 76 #define PRESS_FUELRAIL 0x22 //Fuel rail pressure relative to manifold vacuum. 77 #define PRESS_FUELDIESL 0x23 //Fuel rail pressure (Diesel) 78 79 #define OS2S1_WR_LAMBDA 0x24 //Equivalence ratio voltage 80 #define OS2S2_WR_LAMBDA 0x25 //Equivalence ratio voltage 81 #define OS2S3_WR_LAMBDA 0x26 //Equivalence ratio voltage 82 #define OS2S4_WR_LAMBDA 0x27 //Equivalence ratio voltage 83 #define OS2S5_WR_LAMBDA 0x28 //Equivalence ratio voltage 84 #define OS2S6_WR_LAMBDA 0x29 //Equivalence ratio voltage 85 #define OS2S7_WR_LAMBDA 0x2A //Equivalence ratio voltage 86 #define OS2S8_WR_LAMBDA 0x2B //Equivalence ratio voltage 87 88 #define COMMANDED_EGR 0x2C //Commanded EGR. 89 #define ERROR_EGR 0x2D //EGR Error. 90 91 #define PID_SUPPORTED3 0x40 //PIDs supported 21-60. 92 #define FUEL_TYPE 0x51 //Type of fuel. 01-Gasoline, 02-Methanol, 03-Ethanol, 04-Diesel (10+Gasoline, etc. is electric hybrid. I.e. 11 is gasoline hybrid.) 93 #define FUEL_ETH_PER0x52 //Ethanol fuel %. 94 95 /*Mode 2 PIDs*/ 96 97 #define FREEZE_FRAME_DTC 0x02 98 99 /*Mode 3 PIDs 100 No PIDs required. Mode 3 returns BCD encoded DTC values. 101 Returns 6 byte packets. 102 Each DTC takes 2 bytes. The number of packets is the number of 103 codes divided by 3. 104 105 Each DTC is composed of 5 characters 106 First character - 2 bit encoded Byte0[7..6] (P,C,B,U) 107 Second character - 2 bit encoded Byte0[5..4] (0,1,2,3) 108 Third character - 4 bit encoded Byte0[3..0] (1,2,3,4,5,6,7,8,9) 109 Fourth character - 4 bit encoded Byte1[7..4] (1,2,3,4,5,6,7,8,9) 110 Fifth character - 4 bit encoded Byte1[3..0] (1,2,3,4,5,6,7,8,9); 111 */ 112 113 /* [A] is expected to be in memory as [Byte1][Byte0] . BUF is a buffer that has atleast 6 characters (5 character + null character.)*/ 114 #define DTC_CODE(BUF,A) snprintf(BUF,sizeof(BUF), "%c%d%d%d%d",
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
1 /* 2 * Copyright (c) 2009 Nathaniel Houghton <[email protected]> 3 * Robert Kirchgessner <[email protected]> 4 * 5 * Permission to use, copy, modify, and distribute this software for 6 * any purpose with or without fee is hereby granted, provided that 7 * the above copyright notice and this permission notice appear in all 8 * copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA 15 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 * 19 */ 20 21 #ifndef SPI_HEADER 22 #define SPI_HEADER 23 24 #define SPI_SEL(void) PORTB &= 0xEF; 25 #define SPI_DSEL(void) PORTB |= 0x10; 26 27 28 void spi_init(void); 29 void spi_tx(const uint8_t *, uint8_t); 30 void spi_rx(uint8_t *, uint8_t); 31 32 #endif
<<<<< END software/spi.h <<<<<
>>>>> BEG software/ui.h >>>>> 1 /* 2 * Copyright (c) 2009 Nathaniel Houghton <[email protected]> 3 * Robert Kirchgessner <[email protected]> 4 * 5 * Permission to use, copy, modify, and distribute this software for 6 * any purpose with or without fee is hereby granted, provided that 7 * the above copyright notice and this permission notice appear in all 8 * copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA 15 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 * 19 */ 20 21 #ifndef UI_H 22 #define UI_H 23 24 #include <stdint.h> 25 26 #include "mode.h"
129
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
27 28 #define IGNITION_MSK 0x10 29 30 #define BTN_1 0x10 31 #define BTN_2 0x08 32 #define BTN_3 0x04 33 #define BTN_IGNITION 0x02 34 #define BTN_LCDPRESENT 0x01 35 36 #define MENU_ENTRY_STATIC 0x0 37 #define MENU_ENTRY_DYNAMIC 0x1 38 39 #define LCD_LINE_LENGTH 20 40 41 #define BUTTON_LEFT BTN_3 42 #define BUTTON_RIGHT BTN_1 43 #define BUTTON_ENTER BTN_2 44 45 #define MENU_ENTRY_COUNT 13 /*Remember to update me*/ 46 #define MENU_ID_ROOT 0x00 47 #define MENU_ID_CHECK_DTC 0x01 48 #define MENU_ID_DTCMIL 0x02 49 #define MENU_ID_VIEWDTC 0x03 50 #define MENU_ID_CLEARDTC 0x04 51 #define MENU_ID_BACK 0x05 52 #define MENU_ID_DTC 0x06 53 #define MENU_ID_DTCRIGHT 0x07 54 #define MENU_ID_DTCLEFT 0x08 55 #define MENU_ID_FLASH_STATUS 0x09 56 #define MENU_ID_NVM_ERASE 0x0A 57 #define MENU_ID_ERASED 0x0B 58 #define MENU_ID_CLEAREDDTC 0x0C 59 60 extern struct lcd lcd; 61 62 struct lcd { 63 uint8_t initd; 64 char line0[LCD_LINE_LENGTH+1]; 65 char line1[LCD_LINE_LENGTH+1]; 66 }; 67 68 struct menu_entry { 69 uint8_t type; /*Type of menu entry*/ 70 uint8_t id; 71 uint8_t left; /*Integer identifier to the next menu to display when the left button is pressed*/ 72 uint8_t right; /*Integer identifier to the next menu to display when the right button is pressed*/ 73 uint8_t enter; /*Integer identifier to next menu to display*/ 74 void (*fp)(void); /*Pointer to the function to be run if enter pressed*/ 75 }; 76 77 struct static_menu_entry { 78 uint8_t type; /*Type of menu entry*/ 79 uint8_t id; 80 uint8_t left; /*Integer identifier to the next menu to display when the left button is pressed*/ 81 uint8_t right; /*Integer identifier to the next menu to display when the right button is pressed*/ 82 uint8_t enter; /*Integer identifier to next menu to display*/ 83 84 void (*fp)(void); /*Pointer to the function to be run if enter pressed*/
130
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
85 86 char disp[]; /*Pointer to string to display*/ 87 }; 88 89 struct dynamic_menu_entry { 90 uint8_t type; /*Type of menu entry*/ 91 uint8_t id; 92 uint8_t left; /*Integer identifier to the next menu to display when the left button is pressed*/ 93 uint8_t right; /*Integer identifier to the next menu to display when the right button is pressed*/ 94 uint8_t enter; /*Integer identifier to next menu to display*/ 95 96 void (*fp)(void); /*Pointer to the function to be run if enter pressed*/ 97 98 void (*disp)(void); /*Pointer to the function that will generate the message*/ 99 }; 100 101 struct menu { 102 uint8_t current; /*current menu being displayed*/ 103 104 struct menu_entry *menu_entries[]; /*All the menu entries in the menu*/ 105 }; 106 107 #define MENU_RES_NAME(ID) _RES_##ID 108 #define MENU_ENTRY(ID) (struct menu_entry*)&_RES_##ID 109 #define MENU_RES_STATIC(ID, STR, LEFT, RIGHT, ENTER, FP) const struct static_menu_entry _RES_##ID = { MENU_ENTRY_STATIC, ID, LEFT, RIGHT, ENTER, FP, STR } 110 #define MENU_RES_DYNAMIC(ID, DISP, LEFT, RIGHT, ENTER, FP) const struct dynamic_menu_entry _RES_##ID = { MENU_ENTRY_DYNAMIC, ID, LEFT, RIGHT, ENTER, FP, DISP } 111 112 void lcd_update(struct lcd *, uint8_t, char *); 113 void init_ui(void); 114 void menu(struct mode *, uint8_t); 115 uint8_t btn_down(uint8_t); 116 void menu_repaint(void); 117 118 void menu_dynamic_root(void); 119 void menu_dynamic_dtcmil(void); 120 void menu_dtc_update(void); 121 void menu_dtc_current(void); 122 void menu_dynamic_flash(void); 123 void menu_set_current( uint8_t ); 124 125 #endif
<<<<< END software/ui.h <<<<<
>>>>> BEG software/usart.h >>>>> 1 /* 2 * Copyright (c) 2009 Nathaniel Houghton <[email protected]> 3 * Robert Kirchgessner <[email protected]> 4 * 5 * Permission to use, copy, modify, and distribute this software for 6 * any purpose with or without fee is hereby granted, provided that 7 * the above copyright notice and this permission notice appear in all 8 * copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
131
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA 15 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 * 19 */ 20 21 #ifndef USART_HEADER 22 #define USART_HEADER 23 24 #define USART_RXBUFSIZE 256 25 #define USART_TXBUFSIZE 64 26 27 #define USART_CTS_MAX (USART_RXBUFSIZE * 0.75) 28 #define USART_CTS_MIN (USART_RXBUFSIZE * 0.66) 29 30 struct usart_txbuf { 31 uint8_t buf[USART_TXBUFSIZE]; 32 uint16_t head; 33 uint16_t tail; 34 uint16_t used; 35 36 uint8_t inuse; 37 uint8_t echo; 38 }; 39 40 struct usart_rxbuf { 41 uint8_t buf[USART_RXBUFSIZE]; 42 uint16_t head; 43 uint16_t tail; 44 uint16_t used; 45 46 uint8_t inuse; 47 uint8_t echo; 48 }; 49 50 void usart_init(void); 51 uint16_t usart_tx(uint8_t, uint8_t *, uint16_t); 52 uint16_t usart_rx(uint8_t, uint8_t *, uint16_t); 53 uint16_t buf_cat(uint8_t, uint8_t *, uint16_t); 54 uint16_t buf_rem(uint8_t, uint8_t *, uint16_t); 55 void usart_puts(uint8_t, const char *); 56 void usart_put(uint8_t, const void *, uint16_t); 57 int usart_getc(uint8_t); 58 void usart_putc(uint8_t, int); 59 int8_t usart_peek(uint8_t, char); 60 void usart_flush(uint8_t); 61 uint16_t usart_rxused(uint8_t); 62 void usart_echo(uint8_t, uint8_t); 63 uint8_t userland_wait(void); 64 65 #endif
<<<<< END software/usart.h <<<<<
>>>>> BEG software/util.h >>>>> 1 /* 2 * Copyright (c) 2009 Nathaniel Houghton <[email protected]> 3 * Robert Kirchgessner <[email protected]> 4 * 5 * Permission to use, copy, modify, and distribute this software for 6 * any purpose with or without fee is hereby granted, provided that
132
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
7 * the above copyright notice and this permission notice appear in all 8 * copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA 15 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 * 19 */ 20 21 #ifndef UTIL_HEADER 22 #define UTIL_HEADER 23 24 uint32_t byte_swap(uint32_t); 25 void fatal(char *); 26 uint32_t hexstr(char *, uint8_t); 27 void hexdump(void *, uint16_t); 28 void *xmalloc(size_t); 29 30 #endif
<<<<< END software/util.h <<<<<
>>>>> BEG pc/livestats/wemslivestats.c >>>>> 1 #include <stdint.h> 2 #include <string.h> 3 #include <stdio.h> 4 #include <windows.h> 5 6 #include "resource.h" 7 8 #include "../serial.h" 9 #include "../config.h" 10 #include "../obd_pid.h" 11 #include "../util.h" 12 13 #define START_MSG "Enter COM port number and click Set Live Mode to begin." 14 #define DELAY_MS 15 15 #define IDT_TIMER1 1 16 17 struct disp_info { 18 int pid[NUM_LOG_SLOTS]; 19 int num_pids; 20 }; 21 22 BOOL CALLBACK callback(HWND, UINT, WPARAM, LPARAM); 23 void clear_display(HWND); 24 int update_display(HWND, struct serial_dev *, struct disp_info *); 25 void status_msg(HWND, char *); 26 int set_livemode(HWND, struct serial_dev *); 27 28 int WINAPI 29 WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, 30 int iCmdShow) 31 { 32 MSG msg; 33 HWND hDlg; 34
133
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
>>>>> BEG software/elm_test/elm.c >>>>> 1 /* 2 * Copyright (c) 2009 Nathaniel Houghton <[email protected]> 3 * Robert Kirchgessner <[email protected]> 4 * 5 * Permission to use, copy, modify, and distribute this software for 6 * any purpose with or without fee is hereby granted, provided that 7 * the above copyright notice and this permission notice appear in all 8 * copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA 15 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 * 19 */ 20 21 #include <stdio.h> 22 #include <stdint.h> 23 #include <ctype.h> 24 #include <string.h> 25 26 #define WINDOWS 27 28 #include "elm.h" 29 #include "obd_pid.h" 30 31 /*Generic functions*/ 32 uint8_t elm_gets(uint8_t *buf, uint8_t len); /*Reads a string*/ 33 uint8_t elm_readdata( uint8_t *buf, uint8_t len ); /*Read a hex string into a byte array*/ 34 void elm_flush( uint8_t ch ); /*Flush till character*/ 35 36 /*Inline functions*/ 37 inline void elm_set_protocol( uint8_t protocol ); /*Set the protocol with auto search*/ 38 39 /*Globals*/ 40 uint8_t get_pid_error; /*Error flag. Indicates that there was atleast 1 PID read that had the incorrect value returned*/ 41 42 /*This function disables echo, sets the protocol, checks to see if it can connect to the bus (and returns 0 if it cannot). It also sets the PID supported values*/ 43 uint8_t 44 elm_init( elm_dev *dev, uint8_t prot ) 45 { 46 uint8_t buf[25]; 47 get_pid_error = 0; /*Initialize the error flag to 0*/ 48 49 elm_setecho(0); /*Turn off echo*/ 50 elm_set_protocol( prot ); /*Set the protocol*/ 51 52 snprintf( buf, sizeof(buf), "01 %2.2x 1\r", PID_SUPPORTED ); /*Perform the PID supported check*/ 53 usart_put( DEV_ELM, buf, sizeof(buf) ); 54 55 elm_gets( buf, sizeof(buf) ); /*Gets the next line from the buffer.. "SEARCHING"*/
145
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
56 elm_gets( buf, sizeof(buf) ); /*Gets the next line from the buffer*/ 57 58 #ifdef WINDOWS 59 printf("Read %s\n", buf); 60 #endif 61 62 if( strcmp( buf, "UNABLE TO CONNECT" ) == 0 ) /*Check to see if we got the could not connect message*/ 63 { 64 status("Unable to connect detected! Bye!\n"); 65 return 0; 66 } 67 68 elm_flush('>'); /*Find the prompt*/ 69 70 elm_getpid( PID_SUPPORTED , buf ,sizeof(buf) ); /*Get the PID again, since last time we just did some error checking.*/ 71 72 dev->caps[0] = BA4_TO_UINT32(buf); /*Set the first set of device capabilities.*/ 73 74 dev->caps[1] = 0x0; /*Set the other lists to 0 before checking if they are supported*/ 75 dev->caps[2] = 0x0; 76 77 if( PID_IS_SUPPORTED( PID_SUPPORTED, PID_SUPPORTED2, dev->caps[0] ) ) /*Check if the second set of PIDs are supported*/ 78 { 79 status("PID SET 2 SUPPORTED. REQUESTING..\n"); 80 elm_getpid( PID_SUPPORTED2 , buf, sizeof(buf) ); /*Get the PID again, since last time we just did some error checking.*/ 81 dev->caps[1] = BA4_TO_UINT32(buf); /*Set the first set of device capabilities.*/ 82 83 if( PID_IS_SUPPORTED( PID_SUPPORTED2, PID_SUPPORTED3, dev->caps[1] ) ) /*Chek if the third set of PIDs are supported*/ 84 { 85 status("PID SET 3 SUPPORTED. REQUESTING..\n"); 86 elm_getpid( PID_SUPPORTED3 , buf, sizeof(buf) ); /*Get the PID again, since last time we just did some error checking.*/ 87 dev->caps[2] = BA4_TO_UINT32(buf); /*Set the first set of device capabilities.*/ 88 } 89 } 90 91 /*Return 1 on success*/ 92 return 1; 93 } 94 95 /*This function is used to get the value for a PID as a byte array and store it into the buffer. Returns the number of bytes read.*/ 96 /*This function checks that the correct response is returned. If it is not returned, dummy data is returned with the expected number of bytes.*/ 97 uint8_t 98 elm_getpid( uint16_t pid, uint8_t *buf, uint8_t len ) 99 { 100 uint8_t tmp[10]; /*The minimum number of characters needed*/ 101 uint8_t read; /*Number of bytes read.*/ 102 uint8_t expected; /*Number of bytes that should be read*/ 103 104 snprintf( tmp, sizeof(tmp), "01 %2.2x 1\r", pid ); /*Generate MODE01 request string.*/ 105 usart_put( DEV_ELM, tmp, sizeof(tmp) ); /*Send the MODE01 request for the PID.*/
146
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
106 107 elm_readdata( buf, 2 ); /*Read the first two bytes (the tag/PID).*/ 108 109 expected = PID_LENGTH( pid ); /*Get the expected return length from the PID lookup table.*/ 110 111 if( (buf[0]<<8 | buf[1]) == ((0x41 << 8)|pid) ) { /*If a correct response header was read.*/ 112 read = elm_readdata( buf, len ); 113 114 if( read != expected ) { /*Check that the expected number of bytes were read, if not, return the number of bytes that should have been read.*/ 115 status("Incorrent number of bytes returned. Returning dummy data."); 116 read = expected; 117 get_pid_error = 1; /*Set error flag*/ 118 memset( (void*)buf,0x00, read*sizeof(uint8_t) ); /*Set buffer to 0x00*/ 119 } 120 } 121 else /*Incorrect header response*/ 122 { 123 status("Incorrent PID response. Returning dummy data."); 124 read = expected; 125 get_pid_error = 1; /*Set error flag*/ 126 memset( (void*)buf,0x00, read*sizeof(uint8_t) ); /*Set buffer to 0x00*/ 127 } 128 129 printf("Get_pid waiting for prmpt\n"); 130 elm_flush('>'); /*Find the prompt*/ 131 132 return read; 133 134 } 135 136 /*This reads a line of hex from the ELM chip, and puts it into a byte array*/ 137 uint8_t 138 elm_readdata(uint8_t *buf, uint8_t len) 139 { 140 uint8_t c; 141 uint8_t index; 142 uint8_t state; /*Used to detect if we are storing the upper or lower nibble*/ 143 144 index = 0; 145 state = 0; 146 147 while( (c=usart_getc(DEV_ELM)) != '\n' ) { 148 149 if( index == len )/*Incase the buffer gets full.*/ 150 break; 151 152 if( isspace(c) ) 153 continue; 154 155 if( c == '\0' ) 156 continue; 157 158 if( c<='F' && c>='A' ) /*Check if we get a character or not*/ 159 c = c-'A'+10; 160 else 161 c = c-'0'; 162
147
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
224 usart_put( DEV_ELM, buf, sizeof(buf) ); 225 elm_flush('>'); /*Wait for prompt*/ 226 } 227 228 /*This is used to flush until a character, typically the command prompt carrot ">"*/ 229 void elm_flush(uint8_t ch) 230 { 231 uint8_t c; 232 status("Flushing...\n"); 233 234 while( (c=usart_getc(DEV_ELM)) != ch ) 235 ; 236 237 usart_getc(DEV_ELM); /*Used in windows to get the uneeded newline character from the buffer*/ 238 239 } 240 241 void elm_reset() 242 { 243 uint8_t buf[] = "AT Z\r"; 244 usart_put( DEV_ELM, buf, sizeof(buf) ); 245 elm_flush('>'); /*Wait for prompt*/ 246 }
<<<<< END software/elm_test/elm.c <<<<<
>>>>> BEG software/elm_test/elm_dtc.c >>>>> 1 /* 2 * Copyright (c) 2009 Nathaniel Houghton <[email protected]> 3 * Robert Kirchgessner <[email protected]> 4 * 5 * Permission to use, copy, modify, and distribute this software for 6 * any purpose with or without fee is hereby granted, provided that 7 * the above copyright notice and this permission notice appear in all 8 * copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA 15 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 * 19 */ 20 #include <stdio.h> 21 #include <stdint.h> 22 #include <stdlib.h> 23 24 #include "elm.h" 25 #include "obd_pid.h" 26 #include "elm_dtc.h" 27 28 static struct dtc_head head; /*The first DTC code*/ 29 static struct dtc *curr; /*Current DTC pointer*/ 30 31 uint16_t elm_dtc_get( uint8_t reset ); /*Get the actual DTCs*/ 32 33 uint8_t 34 elm_dtc_peek() /*Gets the number of trouble codes detected, and sets check_engine to 1
149
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
if the check enchne light is on*/ 35 { 36 uint8_t buf[4]; 37 38 printf("Give me the MILDTC (4 bytes)\n"); 39 elm_getpid( STATUS_MILDTC, buf, sizeof(buf) ); /*Get the MIL/DTC code*/ 40 41 return buf[0]; /*Return the first byte. MSB is the check engine light flag. The rest is the #DTC codes.*/ 42 } 43 44 void 45 elm_dtc_update( uint8_t peek_resp ) /*Get new DTC list from the car*/ 46 { 47 char buf[] = "03\r"; /*Mode 3 gets the trouble codes*/ 48 49 uint8_t i; 50 uint8_t num_dtc; /*Current number of DTC*/ 51 int8_t create_cnt; /*Number of DTC nodes we need to delete/create.*/ 52 53 struct dtc *tmp; 54 55 num_dtc = DTC_COUNT_MSK & peek_resp; /*Get the current number of DTC*/ 56 57 if( head.last == NULL) 58 head.last = (struct dtc*)&head; 59 60 if(head.next == NULL) 61 head.next = (struct dtc*)&head; 62 63 if(head.size == 0) 64 head.size = 1; 65 66 if(num_dtc == 0) 67 { 68 printf("Deleting all nodes...\n"); 69 tmp = head.last->prev; 70 71 for(i=0; i<head.size; ++i) 72 { 73 free(tmp->next); 74 tmp = tmp->prev; 75 } 76 77 head.size = 1; 78 79 return; 80 } 81 82 elm_dtc_get(1); /*Reset the counter, doesnt grab any data.*/ 83 84 create_cnt = num_dtc - head.size; /*Find the number of DTC needed to be created/deleted.*/ 85 86 printf("Create counter: %d\n", create_cnt); 87 while( create_cnt > 0 ) /*While we need to create more nodes*/ 88 { 89 printf("Making a new node\n"); 90 tmp = malloc( sizeof( struct dtc ) ); /*Allocate a new DTC node*/ 91 92 if( head.last != NULL ) /*If there is a last node*/ 93 {
150
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
94 printf("Node detected already\n"); 95 tmp->prev = head.last; /*Set the previous node to the old last*/ 96 head.last->next = tmp; /*Create the new last node*/ 97 } 98 else 99 { 100 printf("No previous node, setting previous equal to head node.\n"); 101 tmp->prev = (struct dtc*)&head; /*Set the previous as the head*/ 102 head.next = tmp; /*Set the next node*/ 103 } 104 105 head.last = tmp; /*Set the new last node*/ 106 head.last->next = (struct dtc*)&head; /*Set the last nodes next to the head node.*/ 107 108 create_cnt--; 109 } 110 111 while( create_cnt < 0 ) /*While we need to delete nodes.*/ 112 { 113 printf("Deleting some nodes\n"); 114 115 tmp = head.last->prev; /*Get the previous node.*/ 116 117 if( head.last != NULL && head.last != (struct dtc*)&head ) /*Dont delete the head*/ 118 free( head.last ); /*Delete the old node*/ 119 else 120 break; /*If we hit the head node, might as well stop*/ 121 122 head.last = tmp; 123 head.last->next = (struct dtc*)&head; 124 125 create_cnt++; 126 } 127 128 printf("Give me the mode 3 DTC lines 6 bytes per line\n"); 129 130 tmp = (struct dtc*)&head; /*Set the starting node*/ 131 usart_put( DEV_ELM, buf, sizeof(buf) ); /*Send the MODE03 request to get the DTC*/ 132 133 printf("num_dtc %d calling elm_dtc_get\n", num_dtc); 134 135 for( i=0; i<num_dtc; ++i ) 136 { 137 tmp->code = elm_dtc_get(0); /*Get the next DTC*/ 138 tmp = tmp->next; 139 } 140 141 head.size = num_dtc; 142 curr = (struct dtc*) &head; /*Update the current node to the head.*/ 143 144 elm_flush('>'); 145 146 return; 147 } 148 149 uint16_t 150 elm_dtc_next() /*Get the next dtc*/ 151 { 152 uint16_t code;
151
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
153 code = curr->code; 154 curr = curr->next; 155 156 return code; 157 } 158 159 uint16_t 160 elm_dtc_prev() /*Get previous dtc*/ 161 { 162 uint16_t code; 163 code = curr->code; 164 curr = curr->prev; 165 166 return code; 167 } 168 169 void 170 elm_dtc_clear() /*Clear the trouble codes*/ 171 { 172 uint8_t buf[] = "04\r"; 173 174 /*Need to do some cleanup for the DRC structure here */ 175 176 /*Clearing the trouble codes is as easy as sending a MODE04 request*/ 177 usart_put( DEV_ELM, buf, sizeof(buf) ); /*Send the MODE04 request to clear the DTC*/ 178 179 elm_flush('>'); /*Find the prompt*/ 180 181 } 182 183 uint16_t 184 elm_dtc_get( uint8_t reset ) /*Get the actual DTCs*/ 185 { 186 uint8_t buf[2]; 187 static uint8_t cnt; /*Keep track of how many DTC we read, so we know when to expect the next tag.*/ 188 189 if( reset == 1 ) 190 { 191 cnt = 0; 192 return 0; 193 } 194 195 printf("elm_dtc_get getting data..\n"); 196 197 if(cnt == 0 ) 198 elm_readdata( buf, 1 ); /*Read the DTC header byte */ 199 200 elm_readdata( buf, 2 ); /*Read the DTC in 2 byte pairs until we get the one we want */ 201 cnt++; /*Increment the DTC byte pair counter*/ 202 203 if(cnt == 3) /*End of the current DTC line*/ 204 { 205 cnt = 0; 206 } 207 printf("Returning %x\n", buf[0]<<8| buf[1] ); 208 return buf[0]<<8| buf[1]; 209 210 }
152
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
<<<<< END software/elm_test/elm_dtc.c <<<<<
>>>>> BEG software/elm_test/elm_test.c >>>>> 1 /* 2 * Copyright (c) 2009 Nathaniel Houghton <[email protected]> 3 * Robert Kirchgessner <[email protected]> 4 * 5 * Permission to use, copy, modify, and distribute this software for 6 * any purpose with or without fee is hereby granted, provided that 7 * the above copyright notice and this permission notice appear in all 8 * copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA 15 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 * 19 */ 20 #include <stdio.h> 21 #include <ctype.h> 22 #include <stdint.h> 23 24 #define WINDOWS 25 #include "elm.h" 26 #include "obd_pid.h" 27 #include "elm_dtc.h" 28 29 uint8_t 30 btoh(uint8_t *bytes, char *buf, uint8_t len) 31 { 32 uint8_t state; 33 uint8_t i; 34 uint8_t index; 35 uint8_t c; 36 37 state=0; 38 index=0; 39 i=0; 40 41 while((c=buf[i++]) != '\0') { 42 43 if( index == len )/*Incase the buffer gets full.*/ 44 break; 45 46 if( isspace(c) ) 47 continue; 48 49 if( c<='F' && c>='A' ) /*Check if we get a character or not*/ 50 c = c-'A'+10; 51 else 52 c = c-'0'; 53 54 if(state == 0) { /*Upper Nisbble*/ 55 bytes[index] = c<<4; 56 state=1; 57 } 58 else { /*Lower Nibble*/ 59 bytes[index++] += c;
153
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
>>>>> BEG software/elm_test/obd_pid.c >>>>> 1 /* 2 * Copyright (c) 2009 Nathaniel Houghton <[email protected]> 3 * Robert Kirchgessner <[email protected]> 4 * 5 * Permission to use, copy, modify, and distribute this software for 6 * any purpose with or without fee is hereby granted, provided that 7 * the above copyright notice and this permission notice appear in all 8 * copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA 15 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
154
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
<<<<< END software/ui_test/menu_res.c <<<<<
>>>>> BEG software/ui_test/ui.c >>>>> 1 /* 2 * Copyright (c) 2009 Nathaniel Houghton <[email protected]> 3 * Robert Kirchgessner <[email protected]> 4 * 5 * Permission to use, copy, modify, and distribute this software for 6 * any purpose with or without fee is hereby granted, provided that 7 * the above copyright notice and this permission notice appear in all 8 * copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA 15 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 * 19 */ 20 21 #include <stdio.h> 22 #include <string.h> 23 #include <windows.h> 24 25 #include "ui.h" 26 #include "menu_res.h" 27 28 #define DEBOUNCE_DELAY 20 29 30 #define SW_MSK 0x1E 31 #define LCD_MSK 0x01 32 33 #define _delay_us(A) Sleep(A) 34 35 static struct menu ui_menu; 36 static volatile uint8_t buttons; 37 38 static uint8_t PORTA; 39 static uint8_t PORTD; 40 static uint8_t PCMSK0; 41 static uint8_t PCMSK3; 42 static uint8_t PCICR; 43 static uint8_t PINA; 44 static uint8_t PIND; 45 46 void menu_set_current( uint8_t ); 47 void menu_add_static( const struct static_menu_entry * ); 48 void menu_add_dynamic( const struct dynamic_menu_entry * ); 49 struct menu_entry *menu_get_entry( uint8_t id ); 50 51 #define BUTTON_LEFT BTN_3 52 #define BUTTON_RIGHT BTN_1 53 #define BUTTON_ENTER BTN_2 54 55 void 56 menu(void) 57 { 58 char c; 59 struct menu_entry * current;
157
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
>>>>> BEG software/ui_test/ui_test.c >>>>> 1 /* 2 * Copyright (c) 2009 Nathaniel Houghton <[email protected]> 3 * Robert Kirchgessner <[email protected]> 4 * 5 * Permission to use, copy, modify, and distribute this software for 6 * any purpose with or without fee is hereby granted, provided that 7 * the above copyright notice and this permission notice appear in all 8 * copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA 15 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 * 19 */ 20 21 #include <stdio.h> 22 #include "ui.h" 23
160
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
51 return 0; 52 }
<<<<< END software/usart_test/usart_test.c <<<<<
>>>>> BEG software/wems/wems.c >>>>> 1 /* 2 * Copyright (c) 2009 Nathaniel Houghton <[email protected]> 3 * Robert Kirchgessner <[email protected]> 4 * 5 * Permission to use, copy, modify, and distribute this software for 6 * any purpose with or without fee is hereby granted, provided that 7 * the above copyright notice and this permission notice appear in all 8 * copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA 15 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 * 19 */ 20 21 #include <string.h> 22 #include <stdio.h> 23 #include <stdlib.h> 24 #include <ctype.h> 25 26 #include <avr/eeprom.h> 27 #include <avr/interrupt.h> 28 #include <util/delay.h> 29 #include <util/crc16.h> 30 31 #include "../usart.h" 32 #include "../lcd.h" 33 #include "../util.h" 34 #include "../spi.h" 35 #include "../nvm.h" 36 #include "../config.h" 37 #include "../event.h" 38 #include "../ui.h" 39 40 #include "../mode.h" 41 #include "../elm.h" 42 43 /* global variables */ 44 struct config cfg; 45 struct lcd lcd; 46 47 int 48 main(int argc, char **argv) 49 { 50 char buf[32]; 51 struct mode mode; 52 53 lcd_init(); 54 usart_init(); 55 spi_init(); 56 nvm_unlock();
162
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
57 init_ui(); 58 59 if (read_config(&cfg) == -1) { /*Reads the config, and writes default if not valid*/ 60 memset(&cfg, 0, sizeof(cfg)); 61 write_config(&cfg); 62 } 63 64 event_init(&cfg.event[0]); 65 66 memset(&mode, 0, sizeof(mode)); 67 mode.mode = MODE_CONFIG; 68 mode.state = STATE_0_INIT; 69 70 sei(); 71 72 /* Wait for ELM to start up. */ 73 elm_pwrwait(); 74 75 if (btn_down(BTN_LCDPRESENT)) 76 lcd.initd = 1; 77 78 for (;;) { 79 mode_seq(&mode); 80 if (mode.mode == MODE_ELM) 81 elm_mode(&mode); 82 else if (mode.mode == MODE_CONFIG) 83 config_mode(&mode); 84 else if (mode.mode == MODE_LOG) 85 log_mode(&mode); 86 87 if (btn_down(BTN_LCDPRESENT)) { 88 if (!lcd.initd) { 89 lcd_init(); 90 lcd.initd = 1; 91 snprintf(buf, sizeof(buf), "Mode: %s", 92 mode_str[mode.mode]); 93 lcd_update(&lcd, 0, buf); 94 } 95 } else 96 lcd.initd = 0; 97 } 98 99 return 0; 100 }
>>>>> BEG software/elm_test/elm.h >>>>> 1 /* 2 * Copyright (c) 2009 Nathaniel Houghton <[email protected]> 3 * Robert Kirchgessner <[email protected]> 4 * 5 * Permission to use, copy, modify, and distribute this software for 6 * any purpose with or without fee is hereby granted, provided that 7 * the above copyright notice and this permission notice appear in all 8 * copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
164
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA 15 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 * 19 */ 20 21 #ifndef ELM_H 22 #define ELM_H 23 24 #include <stdint.h> 25 #include <stdio.h> 26 27 #include "elm_prot.h" //Contains the differnet protocols supported by the ELM chip. 28 29 #define WINDOWS 30 31 #ifdef WINDOWS 32 #define status(A) printf("%s", A) 33 #define usart_getc(A) getc(A) 34 #define usart_peek(A) peek(A) 35 #define usart_put(A,B,C) printf("%s\n",B) 36 #else 37 #include "usart.h" 38 #define status(A) 39 #endif 40 41 #define DEV_ELM stdin 42 43 typedef struct { 44 uint32_t caps[3]; /*Which pids are supported. There are 3 sets of PIDs.*/ 45 uint8_t prot; /*The current protocol set*/ 46 } elm_dev; 47 48 49 /*Macro functions*/ 50 /*PIDSUPPORTED is the PID used for the request, PID is the PID being checked, VALUE is what was returned from the ELM for that check*/ 51 #define PID_IS_SUPPORTED(PIDSUPPORTED, PID, VALUE) (((VALUE - PIDSUPPORTED) & (1<<(PID - PIDSUPPORTED -1 )))!=0) /*Checks if the PID is in the PID_SUPPORTED flag*/ 52 #define BA4_TO_UINT32(A) A[3]<<24 | A[2] << 16 | A[1] << 8 | A[0]; //*Generates a UINT32 from a length 4 byte array*/ 53 54 uint8_t elm_init( elm_dev *dev, uint8_t prot ); /*Initialize the ELM device. Returns 1 if success.*/ 55 uint8_t elm_getpid( uint16_t pid, uint8_t *buf, uint8_t len ); /*Gets the requested PID. Current version does not verify that the PID is supported (up to host software*/ 56 void elm_reset(); 57 void elm_flush(uint8_t ch); 58 uint8_t elm_readdata(uint8_t *buf, uint8_t len); 59 inline void elm_setecho( uint8_t val ); /*Set echo on/off*/ 60 61 #endif
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
4 * 5 * Permission to use, copy, modify, and distribute this software for 6 * any purpose with or without fee is hereby granted, provided that 7 * the above copyright notice and this permission notice appear in all 8 * copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA 15 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 * 19 */ 20 21 #ifndef ELM_DTC_H 22 #define ELM_DTC_H 23 24 #include <stdint.h> 25 26 struct dtc { 27 uint16_t code; 28 struct dtc *next; 29 struct dtc *prev; 30 }; 31 32 struct dtc_head { 33 uint16_t code; 34 struct dtc *next; 35 struct dtc *last; 36 37 uint8_t size; 38 }; 39 40 #define CHECK_ENGINE_MSK 0x80 41 #define DTC_COUNT_MSK 0x7F 42 43 /*elm_dtc_peek returns 1 byte. The MSB is the check engine flag. The least sig. 7 bits are the # dtc.*/ 44 uint8_t elm_dtc_peek(); /*Gets the number of trouble codes detected, and sets check_engine to 1 if the check enchne light is on*/ 45 void elm_dtc_update( uint8_t peek_resp ); 46 uint16_t elm_dtc_next(); /*Get the next dtc*/ 47 uint16_t elm_dtc_prev(); /*Get previous dtc*/ 48 void elm_dtc_clear(); /*Clear the trouble codes*/ 49 50 #endif
<<<<< END software/elm_test/elm_dtc.h <<<<<
>>>>> BEG software/elm_test/elm_prot.h >>>>> 1 /* 2 * Copyright (c) 2009 Nathaniel Houghton <[email protected]> 3 * Robert Kirchgessner <[email protected]> 4 * 5 * Permission to use, copy, modify, and distribute this software for 6 * any purpose with or without fee is hereby granted, provided that 7 * the above copyright notice and this permission notice appear in all 8 * copies. 9 *
166
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA 15 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 * 19 */ 20 21 #ifndef ELM_PROT_H 22 #define ELM_PROT_H 23 24 #define PROT_AUTO 0x0 25 #define PROT_SAEJ1850PWM 0x1 26 #define PROT_SAEJ1850VPW 0x2 27 #define PROT_ISO9141 0x3 28 #define PROT_ISO14230KWP0 0x4 29 #define PROT_ISO14230KWP1 0x5 30 #define PROT_ISOCAN11B500K 0x6 31 #define PROT_ISOCAN29B500K 0x7 32 #define PROT_ISOCAN11B250K 0x8 33 #define PROT_ISOCAN29B250K 0x9 34 #define PROT_SAECAN29B250K 0xA 35 #define PROT_USERCAN1 0xB 36 #define PROT_USERCAN2 0xC 37 38 #endif
<<<<< END software/elm_test/elm_prot.h <<<<<
>>>>> BEG software/elm_test/obd_pid.h >>>>> 1 /* 2 * Copyright (c) 2009 Nathaniel Houghton <[email protected]> 3 * Robert Kirchgessner <[email protected]> 4 * 5 * Permission to use, copy, modify, and distribute this software for 6 * any purpose with or without fee is hereby granted, provided that 7 * the above copyright notice and this permission notice appear in all 8 * copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA 15 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 * 19 */ 20 21 #ifndef OBD_PID_H 22 #define OBD_PID_H 23 24 /*To use the PID length lookup table.*/ 25 extern const char pid_length[]; 26 27 #define MAX_PID 0x52 28 29 /*The following statement returns the length of PID A fromthe PID lookup table.*/
167
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
30 #define PID_LENGTH(A) ( (MAX_PID >= A)?pid_length[A]:0 ) 31 32 /*OBD-II PIDs*/ 33 34 //*Mode 1 PIDs*/ 35 #define PID_SUPPORTED 0x00 //Which PIDs are supported. Bit encoded. PIDs 0x01-0x20 36 #define STATUS_MILDTC 0x01 //Monitor status for MIL and DTCs. Bit encoded. 37 #define FREEZE_DTC 0x02 //Diagnostic Trouble codes 38 #define STATUS_FUELSYS 0x03 //Current fuel system status. Bit encoded. 39 #define ENGINE_LOAD 0x04 //Engine load value. 40 #define ENGINE_TEMP 0x05 //The engine coolant temperature. 41 42 #define SHORT_TERM_FUEL_TRIM_B1 0x06 //Short term fuel % trim, Bank 1. 43 #define LONG_TERM_FUEL_TRIM_B1 0x07 //Long term fuel % trim ,Bank 1. 44 #define SHORT_TERM_FUEL_TRIM_B2 0x08 //Short term fuel % trim, Bank 2. 45 #define LONG_TERM_FUEL_TRIM_B2 0x09 //Long term fuel % trim ,Bank 2. 46 47 #define PRESSURE_FUEL 0x0A //Fuel pressure. 48 #define PRESSURE_MANIFOLD 0x0B //Intake manifold pressure. 49 50 #define ENGINE_RPM 0x0C //Engine RPMs. 51 #define VEHICLE_SPEED 0x0D //Speed of the vehicle. 52 #define TIMING_ADVANCE 0x0E //Engine timing, relative to cylinder 1. 53 #define INTAKE_TEMP 0x0F //Intake air temperature. 54 #define MAF_AIRFLOW 0x10 //MAF Air flow rate. 55 #define THROTTLE_POS 0x11 //Throttle position. 56 #define STATUS_CSA 0x12 //Secondary commanded air status (bit encoded.) 57 58 #define OXY_PRESENT 0x13 //If oxygen sensors are present. 59 #define OXY_B1_S1 0x14 //Bank1, Sensor 1 60 #define OXY_B1_S2 0x15 //Bank1, Sensor 2 61 #define OXY_B1_S3 0x16 //Bank1, Sensor 3 62 #define OXY_B1_S4 0x17 //Bank1, Sensor 4 63 #define OXY_B2_S1 0x18 //Bank2, Sensor 1 64 #define OXY_B2_S2 0x19 //Bank2, Sensor 2 65 #define OXY_B2_S3 0x1A //Bank3, Sensor 3 66 #define OXY_B2_S4 0x1B //Bank3, Sensor 4 67 68 #define OBD_STANDARD0x1C //Which OBD standard this vehicle supports. 69 #define OXY_PRESENT2 0x1D //Similar to PID 0x13, but bit order changed. 70 71 #define STATUS_AUX 0x1E //Auxilary input status. 72 #define ENGINE_RUNTIME 0x1F //Run time since engine start. 73 74 #define PID_SUPPORTED2 0x20 //Which PIDs are supported. Bit encoded. PIDs 0x21-0x40 75 #define DISTANCE_WMIL 0x21 //Distance travelled with MIL on. 76 #define PRESS_FUELRAIL 0x22 //Fuel rail pressure relative to manifold vacuum. 77 #define PRESS_FUELDIESL 0x23 //Fuel rail pressure (Diesel) 78 79 #define OS2S1_WR_LAMBDA 0x24 //Equivalence ratio voltage 80 #define OS2S2_WR_LAMBDA 0x25 //Equivalence ratio voltage 81 #define OS2S3_WR_LAMBDA 0x26 //Equivalence ratio voltage 82 #define OS2S4_WR_LAMBDA 0x27 //Equivalence ratio voltage 83 #define OS2S5_WR_LAMBDA 0x28 //Equivalence ratio voltage 84 #define OS2S6_WR_LAMBDA 0x29 //Equivalence ratio voltage 85 #define OS2S7_WR_LAMBDA 0x2A //Equivalence ratio voltage 86 #define OS2S8_WR_LAMBDA 0x2B //Equivalence ratio voltage 87 88 #define COMMANDED_EGR 0x2C //Commanded EGR. 89 #define ERROR_EGR 0x2D //EGR Error.
168
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
90 91 #define PID_SUPPORTED3 0x40 //PIDs supported 21-60. 92 #define FUEL_TYPE 0x51 //Type of fuel. 01-Gasoline, 02-Methanol, 03-Ethanol, 04-Diesel (10+Gasoline, etc. is electric hybrid. I.e. 11 is gasoline hybrid.) 93 #define FUEL_ETH_PER0x52 //Ethanol fuel %. 94 95 /*Mode 2 PIDs*/ 96 97 #define FREEZE_FRAME_DTC 0x02 98 99 /*Mode 3 PIDs 100 No PIDs required. Mode 3 returns BCD encoded DTC values. 101 Returns 6 byte packets. 102 Each DTC takes 2 bytes. The number of packets is the number of 103 codes divided by 3. 104 105 Each DTC is composed of 5 characters 106 First character - 2 bit encoded Byte0[7..6] (P,C,B,U) 107 Second character - 2 bit encoded Byte0[5..4] (0,1,2,3) 108 Third character - 4 bit encoded Byte0[3..0] (1,2,3,4,5,6,7,8,9) 109 Fourth character - 4 bit encoded Byte1[7..4] (1,2,3,4,5,6,7,8,9) 110 Fifth character - 4 bit encoded Byte1[3..0] (1,2,3,4,5,6,7,8,9); 111 */ 112 113 /* [A] is expected to be in memory as [Byte1][Byte0] . BUF is a buffer that has atleast 6 characters (5 character + null character.)*/ 114 #define DTC_CODE(BUF,A) sprintf(BUF, "%c%d%d%d%d", ( (((A>>6)&0x3)==0)?'P':( (((A>>6)&0x3)==1)?'C':( (((A>>6)&0x3)==2)?'B':( (((A>>6)&0x3)==3)?'U':'?') ) ) ), \ 115
>>>>> BEG software/ui_test/menu_res.h >>>>> 1 /* 2 * Copyright (c) 2009 Nathaniel Houghton <[email protected]> 3 * Robert Kirchgessner <[email protected]> 4 * 5 * Permission to use, copy, modify, and distribute this software for 6 * any purpose with or without fee is hereby granted, provided that 7 * the above copyright notice and this permission notice appear in all 8 * copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA 15 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 * 19 */ 20 21 #ifndef MENU_RES_H 22 #define MENU_RES_H 23 24 #include "ui.h" 25 26 #define MENU_RES_NAME(ID) _RES_##ID
171
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
>>>>> BEG software/ui_test/ui.h >>>>> 1 /* 2 * Copyright (c) 2009 Nathaniel Houghton <[email protected]> 3 * Robert Kirchgessner <[email protected]> 4 * 5 * Permission to use, copy, modify, and distribute this software for 6 * any purpose with or without fee is hereby granted, provided that 7 * the above copyright notice and this permission notice appear in all 8 * copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA 15 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 * 19 */ 20 21 #ifndef UI_H 22 #define UI_H 23 24 #include <stdint.h> 25 #include <stdio.h> 26 #include "menu_res.h" 27 28 #define IGNITION_MSK 0x10 29 30 #define BTN_1 0x10 31 #define BTN_2 0x08 32 #define BTN_3 0x04 33 #define BTN_IGNITION 0x02 34 #define BTN_LCDPRESENT 0x01 35 36 #define LCD_LINE_LENGTH 20 37 38 void init_ui(void); 39 40 uint8_t btn_down(uint8_t); 41 #define lcd_update(A,B,C) printf("%s\n", C ) 42 extern struct lcd lcd;
172
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS
43 44 struct lcd { 45 uint8_t initd; 46 char line0[LCD_LINE_LENGTH+1]; 47 char line1[LCD_LINE_LENGTH+1]; 48 }; 49 50 struct menu_entry { 51 uint8_t type; /*Type of menu entry*/ 52 uint8_t id; 53 uint8_t left; /*Integer identifier to the next menu to display when the left button is pressed*/ 54 uint8_t right; /*Integer identifier to the next menu to display when the right button is pressed*/ 55 uint8_t enter; /*Integer identifier to next menu to display*/ 56 void (*fp)(void); /*Pointer to the function to be run if enter pressed*/ 57 }; 58 59 struct static_menu_entry { 60 uint8_t type; /*Type of menu entry*/ 61 uint8_t id; 62 uint8_t left; /*Integer identifier to the next menu to display when the left button is pressed*/ 63 uint8_t right; /*Integer identifier to the next menu to display when the right button is pressed*/ 64 uint8_t enter; /*Integer identifier to next menu to display*/ 65 66 void (*fp)(void); /*Pointer to the function to be run if enter pressed*/ 67 68 char disp[LCD_LINE_LENGTH+1]; /*Pointer to string to display*/ 69 }; 70 71 struct dynamic_menu_entry { 72 uint8_t type; /*Type of menu entry*/ 73 uint8_t id; 74 uint8_t left; /*Integer identifier to the next menu to display when the left button is pressed*/ 75 uint8_t right; /*Integer identifier to the next menu to display when the right button is pressed*/ 76 uint8_t enter; /*Integer identifier to next menu to display*/ 77 78 void (*fp)(void); /*Pointer to the function to be run if enter pressed*/ 79 80 void (*disp)(void); /*Pointer to the function that will generate the message*/ 81 }; 82 83 struct menu { 84 uint8_t current; /*current menu being displayed*/ 85 uint8_t length; 86 87 struct menu_entry *menu_entries[MENU_ENTRY_COUNT]; /*All the menu entries in the menu*/ 88 }; 89 90 #define MENU_ENTRY_STATIC 0x0 91 #define MENU_ENTRY_DYNAMIC 0x1 92 93 94 95 void init_menu( uint8_t ); 96 void menu(void); 97
173
University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS