174
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

Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Name: Houghton, NathanielEmail: [email protected]: 352-216-3394

Page 2: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Concept/Technology................................................................................................................................10Project Architecture..................................................................................................................................12

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

Page 3: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 4: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

“ElmScan 5 Bluetooth” (available here: http://www.scantool.net/elmscan-5-bluetooth.html).

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

Page 5: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 6: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 7: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 8: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 9: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 10: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 11: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 12: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 13: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 14: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 15: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

Bill of Materials

WEMS PC Module

Quantity Description RefDes Package Cost/Unit Cost1 FTDI, FT232R U1 Ultiboard\SSOP28 $4.50 $4.503 RESISTOR, 270Ω R2, R1, R4 IPC-2221A/2222\RES1300-700X250 $0.12 $0.36

3 LED_redLED2, LED1, LED3

Ultiboard\LED9R2_5V $0.09 $0.27

1 FBEAD, FBEAD U2 Generic\0805 $0.09 $0.091 CAPACITOR, 10nF C1 IPC-2221A/2222\CAPPR200-500X1100 $0.12 $0.121 RESISTOR, 10kΩ R3 IPC-2221A/2222\RES1300-700X250 $0.11 $0.111 CAPACITOR, 4.7µF C2 IPC-2221A/2222\CAPPR150-400X500 $0.10 $0.103 CAPACITOR, 100nF C3, C4, C5 IPC-2221A/2222\CAPPR200-500X1100 $0.12 $0.361 Def, IRLML6402 U3 IPC-7351\SOT-23 $0.55 $0.551 RESISTOR, 1kΩ R5 IPC-2221A/2222\RES1300-700X250 $0.12 $0.121 Headers, USBB_HEADER U4 Ultiboard\HEADER2X2 $0.86 $0.861 CAPACITOR, 100nF C6 IPC-2221A/2222\CAPPA1600-1000X450 $0.12 $0.121 CONNECTORS, HDR2X10 J1 Generic\HDR2X10 $0.20 $0.201 LM3940, LM3940IT U5 Generic\TO-220 $2.26 $2.261 CAPACITOR, 33µF C7 IPC-2221A/2222\CAPPA1600-1000X450 $0.12 $0.121 CAPACITOR, 470nF C8 IPC-2221A/2222\CAPPA1600-1000X450 $0.12 $0.121 CONNECTORS, HDR1X2 J2 Generic\HDR1X2 $0.10 $0.10

Total $10.36

WEMS Car Module

Quantity Description RefDes Package Cost/Unit Cost1 TI, TL2575 U7 Generic\TO-263(TS5B) $2.61 $2.611 INDUCTOR, 330µH L1 Ultiboard\HEADER2 $1.27 $1.271 CAPACITOR, 330µF C10 IPC-2221A/2222\CAPPA1600-1000X450 $0.12 $0.122 CAPACITOR, 100µF C11, C9 IPC-2221A/2222\CAPPA1600-1000X450 $0.12 $0.24

15

Page 16: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

2 our_diodes, 1N5819 U8, U10 IPC-2221A/2222\DO-41 $0.42 $0.841 CONNECTORS, DSUB9M J1 Generic\DB9M $1.10 $1.101 CONNECTORS, HDR2X3 J2 Generic\HDR2X3 $0.20 $0.201 Atmega, Atmega644P U1 IPC-2221A/2222\PDIP-40 $7.87 $7.871 Atmel_Flash, AT26DF081A U2 IPC-7351\SOIC-8 $1.30 $1.301 ELM, ELM327 U3 IPC-2221A/2222\PDIP-28 $26.34 $26.341 Microchip, MCP2551 U4 IPC-2221A/2222\PDIP-8 $1.45 $1.452 RESISTOR, 100Ω R2, R3 IPC-2221A/2222\RES1300-700X250 $0.11 $0.222 CAPACITOR, 560pF C2, C1 IPC-2221A/2222\CAPPA1600-1000X450 $0.11 $0.221 BJT_NPN, 2N3904 Q1 Generic\TO-92 $0.11 $0.111 RESISTOR, 2.2kΩ R5 IPC-2221A/2222\RES1300-700X250 $0.10 $0.101 RESISTOR, 47kΩ R6 IPC-2221A/2222\RES1300-700X250 $0.11 $0.111 RESISTOR, 22kΩ R7 IPC-2221A/2222\RES1300-700X250 $0.12 $0.121 XBEE, XBEE-PRO U5 Nathan\XBEE-PRO $32.00 $32.001 RESISTOR, 4.7kΩ R1 IPC-2221A/2222\RES1300-700X250 $0.11 $0.111 RESISTOR, 510Ω R4 IPC-2221A/2222\RES1300-700X250 $0.11 $0.111 RESISTOR, 50kΩ R9 IPC-2221A/2222\RES1300-700X250 $0.11 $0.112 RESISTOR, 100kΩ R10, R8 IPC-2221A/2222\RES1300-700X250 $0.10 $0.201 CRYSTAL, HC-49/US_5MHz X1 Generic\HC-49US $0.63 $0.631 CONNECTORS, HDR2X8 J3 Generic\HDR2X8 $0.30 $0.30

5 CAPACITOR, 100nFC4, C3, C5, C12, C13

IPC-2221A/2222\CAPPA1600-1000X450 $0.13 $0.65

1 National_Semi, LM3940 U6 Generic\TO-263(TS5B) $2.26 $2.261 CAPACITOR, 470nF C8 IPC-2221A/2222\CAPPA1600-1000X450 $0.13 $0.131 LED_red LED1 Ultiboard\LED9R2_5V $0.09 $0.091 RESISTOR, 330Ω R11 IPC-2221A/2222\RES1300-700X250 $0.08 $0.08

4 CAPACITOR, 18pF C7, C6, C14, C15 IPC-2221A/2222\CAPPA1600-1000X450 $0.11 $0.44

Total Cost $81.33

WEMS UI Module

Quantity Description RefDes Package Cost/Unit Cost1 CONNECTORS, HDR1X10 J1 Generic\HDR1X10 $0.30 $0.301 CONNECTORS, HDR2X8 J2 Generic\HDR2X8 $0.20 $0.201 POTENTIOMETER, 50kΩ R1 Generic\LIN_POT $0.60 $0.604 SWITCH, SPST J3, J4, J5, Generic\SPST $0.75 $3.00

16

Page 17: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

J61 CAPACITOR, 33µF C1 IPC-2221A/2222\CAPPA1600-1000X450 $0.12 $0.121 2 Line LCD $7.50 $7.50

Total $11.72

Division of Labor and Timeline

17

Page 18: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

Task Assigned Description

Preliminary Research

Kirchgessner, RobertHoughton, Nathaniel

Finding information on components, required circuitry, circuit layout etc.

Order Parts Kirchgessner, RobertHoughton, Nathaniel

Order the components needed to begin testing and development.

Component Evaluation

Kirchgessner, RobertHoughton, Nathaniel

Testing individual components. Learning how to interface with each component and ensure it will work as planned.

Implementation of PC<->XBee Interface

Houghton, Nathaniel This interface includes the USB to serial to XBee Pro.

Implementation of μP<->XBee Interface

Kirchgessner, Robert This interface utilizes the XBee Pro to a Microprocessor's Serial Communications Port.

Testing of Communication

Kirchgessner, RobertHoughton, Nathaniel

Ensure that the communications interface PC<-> μP works as intended.

Determine ECU Testbed

Kirchgessner, RobertHoughton, Nathaniel

Find an ECU source (junkyard ECU, personal vehicle ECU)

Testing ECU<->ELM<->Serial interface

Kirchgessner, RobertHoughton, Nathaniel

Make sure that the ELM chip and supporting circuitry works properly.

Integration Stage Kirchgessner, RobertHoughton, Nathaniel

This stage involves connecting all components of the WEMS system together (on a bread board).

Software/Firmware Development

Kirchgessner, RobertHoughton, Nathaniel

Develop PC software and μP firmware.

Hardware Finalization (Testing/Debugging)

Kirchgessner, RobertHoughton, Nathaniel

Once all the components are integrated on a bread board, it will be necessary to ensure that the device functions properly.

Design Final PCB Layout

Kirchgessner, Robert Design the final PCB(s) (WEMS and USB interface.)

PCB Milling Kirchgessner, RobertHoughton, Nathaniel

Fabricate and solder components to the PCB(s).

PCB Population Kirchgessner, RobertHoughton, Nathaniel

Populate the PCB with components.

18

Page 19: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 20: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April
Page 21: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

2009. <http://www.autohausaz.com/html/auto_electrical_systems.html .> [2] “On-board diagnostics”, Wikipedia. 26 January 2009. <http://en.wikipedia.org/wiki/On-

Board_Diagnostics>.

21

Page 22: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

Appendix A – Schematics

Schematic 1: WEMS Car Module

Page 23: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

Schematic 2: WEMS Car Power Supply

Page 24: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

Schematic 3: WEMS UI

Page 25: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

Schematic 4: WEMS PC Module

Page 26: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

Appendix B – PCB

PCB 1: WEMS Car Module

Page 27: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

PCB 2: WEMS UI

Page 28: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

PCB 3: WEMS PC Module

Page 29: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

Appendix C – Source Code>>>>> BEG pc/dumpconfig.c >>>>> 1 #include <stdio.h> 2 #include <windows.h> 3 4 #include "config.h" 5 #include "event.h" 6 #include "util.h" 7 8 int 9 main(int argc, char **argv) 10 { 11 FILE *fp; 12 char *in_file; 13 int r; 14 struct config config; 15 16 if (argc != 2) { 17 fprintf(stderr, "usage: %s config_file\n", argv[0]); 18 exit(1); 19 } 20 21 in_file = argv[1]; 22 23 fp = fopen(in_file, "rb"); 24 if (fp == NULL) { 25 fprintf(stderr, "error: could not open %s\n", in_file); 26 exit(1); 27 } 28 29 r = fread(&config, 1, sizeof(config), fp); 30 31 if (!valid_config(&config)) { 32 fprintf(stderr, "error: invalid config read.\n"); 33 exit(1); 34 } 35 36 dump_config(&config); 37 38 fclose(fp); 39 40 return 0; 41 }

<<<<< END pc/dumpconfig.c <<<<<

>>>>> BEG pc/dumplog.c >>>>> 1 #include <math.h> 2 #include <windows.h> 3 #include <stdio.h> 4 5 #include "config.h" 6 #include "event.h" 7 #include "util.h" 8 #include "obd_pid.h" 9 10 int 11 main(int argc, char **argv) 12 {

29

Page 30: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

13 FILE *fpcfg, *fplog; 14 char *cfg_file, *log_file; 15 int r; 16 struct config config; 17 uint32_t next, file_size; 18 int session_num, event_num; 19 int cur_session; 20 double time; 21 int i; 22 23 if (argc != 4) { 24 fprintf(stderr, "usage: %s config_file log_file " 25 "session_num\n", argv[0]); 26 exit(1); 27 } 28 29 cfg_file = argv[1]; 30 log_file = argv[2]; 31 session_num = atoi(argv[3]); 32 event_num = atoi(argv[4]); 33 34 if (event_num >= NUM_LOG_SLOTS || event_num < 0) { 35 fprintf(stderr, "error: invalid event number\n"); 36 exit(1); 37 } 38 39 fpcfg = fopen(cfg_file, "rb"); 40 if (fpcfg == NULL) { 41 fprintf(stderr, "error: could not open %s\n", cfg_file); 42 exit(1); 43 } 44 45 r = fread(&config, 1, sizeof(config), fpcfg); 46 47 if (!valid_config(&config)) { 48 fprintf(stderr, "error: invalid config read.\n"); 49 exit(1); 50 } 51 52 fplog = fopen(log_file, "rb"); 53 if (fplog == NULL) { 54 fprintf(stderr, "error: could not open %s\n", log_file); 55 exit(1); 56 } 57 fseek(fplog, 0, SEEK_END); 58 file_size = ftell(fplog); 59 rewind(fplog); 60 61 cur_session = 0; 62 while (cur_session <= session_num) { 63 r = fread(&next, 1, sizeof(next), fplog); 64 if (r != sizeof(next)) { 65 fprintf(stderr, "error: could not read next" 66 " address (session # too big)\n"); 67 exit(1); 68 } 69 70 if (cur_session == session_num) 71 break; 72 73 if (fseek(fplog, next, SEEK_SET) == -1) { 74 fprintf(stderr, "error: could not seek next"

30

Page 31: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

75 " address (session # too big)\n"); 76 exit(1); 77 } 78 79 ++cur_session; 80 } 81 82 if (ftell(fplog) == file_size) { 83 fprintf(stderr, "error: invalid session number\n"); 84 exit(1); 85 } 86 87 if (config.event[event_num].interval == 0) { 88 fprintf(stderr, "error: invalid event number\n"); 89 exit(1); 90 } 91 92 printf("# Time (s)"); 93 94 for (i = 0; i < NUM_LOG_SLOTS; ++i) { 95 printf("\t"); 96 97 if (config.event[i].interval == 0) 98 continue; 99 100 printf("%s (%s)", pid_name[config.event[i].pid], 101 pid_units(config.event[i].pid)); 102 } 103 104 printf("\n"); 105 106 time = 0; 107 while (ftell(fplog) < next) { 108 uint32_t data; 109 double value; 110 111 #if 0 112 printf("%d:%d:%.2f", (int) floor(time / 3600.0), (int) floor(time / 60.0), ((int)(time * 10.0) % 600) / 10.0); 113 #else 114 printf("%.2f", time); 115 #endif 116 time += 0.5; 117 118 for (i = 0; i < NUM_LOG_SLOTS; ++i) { 119 if (config.event[i].interval == 0) 120 continue; 121 122 config.event[i].count++; 123 124 printf("\t"); 125 126 if (config.event[i].count == config.event[i].interval) { 127 config.event[i].count = 0; 128 129 /* read the data, inc addr */ 130 data = 0; 131 r = fread(&data, 1, PID_LENGTH(config.event[i].pid), fplog); 132 if (r == 0) { 133 fprintf(stderr, "error: read\n"); 134 exit(1); 135 }

31

Page 32: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

136 137 if (PID_LENGTH(config.event[i].pid) == 2) { 138 data = htons(data); 139 } else if (PID_LENGTH(config.event[i].pid) == 4) { 140 data = htonl(data); 141 } 142 143 value = conv_pid_data(config.event[i].pid, data); 144 145 printf("%.02f", value); 146 } else { 147 printf("?0"); 148 } 149 } 150 151 printf("\n"); 152 } 153 154 fclose(fpcfg); 155 fclose(fplog); 156 157 return 0; 158 }

<<<<< END pc/dumplog.c <<<<<

>>>>> BEG pc/getconfig.c >>>>> 1 #include <stdio.h> 2 #include <windows.h> 3 4 #include "config.h" 5 #include "event.h" 6 #include "util.h" 7 #include "serial.h" 8 9 int 10 main(int argc, char **argv) 11 { 12 char *com_file; 13 char *out_file; 14 FILE *fp; 15 struct serial_dev sdev; 16 int r, total; 17 char buf[128]; 18 struct config config; 19 20 if (argc != 3) { 21 fprintf(stderr, "usage: %s comN output_file\n", argv[0]); 22 exit(1); 23 } 24 25 com_file = argv[1]; 26 out_file = argv[2]; 27 28 fp = fopen(out_file, "wb+"); 29 if (fp == NULL) { 30 fprintf(stderr, "error: could not open %s\n", out_file); 31 exit(1); 32 } 33 34 if (init_serial(&sdev, com_file) == -1) 35 exit(1);

32

Page 33: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

36 37 strcpy(buf, "!!!\recho off\r"); 38 r = write_ser(&sdev, buf, strlen(buf)); 39 if (r != strlen(buf)) { 40 fprintf(stderr, "error: could not write.\n"); 41 exit(1); 42 } 43 44 ser_flush(&sdev); 45 46 strcpy(buf, "read_cfg\r"); 47 r = write_ser(&sdev, buf, strlen(buf)); 48 if (r != strlen(buf)) { 49 fprintf(stderr, "error: could not write.\n"); 50 exit(1); 51 } 52 53 total = 0; 54 while (total < sizeof(config)) { 55 r = sizeof(config) - total; 56 if (r > sizeof(buf)) 57 r = sizeof(buf); 58 59 r = read_ser(&sdev, buf, r); 60 if (r == 0) { 61 fprintf(stderr, "error: could not read.\n"); 62 exit(1); 63 } else { 64 memcpy(&config + total, buf, r); 65 #if 0 66 fwrite(buf, 1, r, fp); 67 printf("wrote %d bytes\n", r); 68 #endif 69 total += r; 70 } 71 } 72 73 if (!valid_config(&config)) { 74 fprintf(stderr, "error: invalid config read.\n"); 75 exit(1); 76 } else { 77 r = fwrite(&config, 1, sizeof(config), fp); 78 printf("Read good config.\n"); 79 } 80 81 close_serial(&sdev); 82 83 fclose(fp); 84 85 return 0; 86 }

<<<<< END pc/getconfig.c <<<<<

>>>>> BEG pc/getlog.c >>>>> 1 #include <stdio.h> 2 #include <windows.h> 3 4 #include "config.h" 5 #include "event.h" 6 #include "util.h" 7 #include "serial.h"

33

Page 34: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

8 #include "progress.h" 9 10 #define CRC_SIZE 2 11 12 int 13 main(int argc, char **argv) 14 { 15 char *com_file; 16 char *out_file; 17 char *conf_file; 18 FILE *fpconf; 19 FILE *fpout; 20 struct serial_dev sdev; 21 int r, total; 22 char buf[128]; 23 struct config config; 24 uint32_t read_addr; 25 uint32_t amt; 26 27 if (argc != 4) { 28 fprintf(stderr, "usage: %s comN config_file " 29 "output_file\n", argv[0]); 30 exit(1); 31 } 32 33 com_file = argv[1]; 34 conf_file = argv[2]; 35 out_file = argv[3]; 36 37 fpout = fopen(out_file, "wb+"); 38 if (fpout == NULL) { 39 fprintf(stderr, "error: could not open %s\n", out_file); 40 exit(1); 41 } 42 fpconf = fopen(conf_file, "rb"); 43 if (fpconf == NULL) { 44 fprintf(stderr, "error: could not open %s\n", 45 conf_file); 46 exit(1); 47 } 48 49 r = fread(&config, 1, sizeof(config), fpconf); 50 51 if (!valid_config(&config)) { 52 fprintf(stderr, "error: invalid config read.\n"); 53 exit(1); 54 } 55 56 if (init_serial(&sdev, com_file) == -1) 57 exit(1); 58 59 strcpy(buf, "!!!\recho off\r"); 60 r = write_ser(&sdev, buf, strlen(buf)); 61 if (r != strlen(buf)) { 62 fprintf(stderr, "error: could not write.\n"); 63 exit(1); 64 } 65 66 ser_flush(&sdev); 67 68 if (config.nvm_start_addr == 0) { 69 fprintf(stderr, "error: nothing logged.\n");

34

Page 35: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

70 exit(1); 71 } 72 73 read_addr = 0; 74 while (read_addr < config.nvm_start_addr) { 75 amt = config.nvm_start_addr - read_addr; 76 if (amt > 126) 77 amt = 126; 78 79 snprintf(buf, sizeof(buf), "read_log %08x %d\r", 80 read_addr, amt); 81 82 #if 0 83 printf("%s\n", buf); 84 #endif 85 86 r = write_ser(&sdev, buf, strlen(buf)); 87 if (r != strlen(buf)) { 88 fprintf(stderr, "error: could send command.\n"); 89 exit(1); 90 } 91 92 total = 0; 93 do { 94 r = read_ser(&sdev, buf + total, 95 amt + CRC_SIZE - total); 96 if (r == 0) { 97 fprintf(stderr, "error: read " 98 "timeout.\n"); 99 exit(1); 100 } 101 total += r; 102 103 } while (total < amt + CRC_SIZE); 104 105 if (!valid_crc(buf, amt + CRC_SIZE)) { 106 fprintf(stderr, "error: invalid CRC at " 107 "%08x.\n", read_addr); 108 exit(1); 109 } 110 111 r = fwrite(buf, 1, amt, fpout); 112 if (r != amt) { 113 fprintf(stderr, "error: could not write to " 114 "local file.\n"); 115 exit(1); 116 } 117 118 read_addr += amt; 119 120 progress_bar(read_addr, config.nvm_start_addr); 121 } 122 123 close_serial(&sdev); 124 125 fclose(fpconf); 126 fclose(fpout); 127 128 return 0; 129 }

<<<<< END pc/getlog.c <<<<<

35

Page 36: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

>>>>> BEG pc/loginfo.c >>>>> 1 #include <stdio.h> 2 #include <windows.h> 3 4 #include "config.h" 5 #include "event.h" 6 #include "util.h" 7 8 int 9 main(int argc, char **argv) 10 { 11 FILE *fpcfg, *fplog; 12 char *cfg_file, *log_file; 13 int r; 14 struct config config; 15 uint32_t addr, next; 16 int sessions; 17 18 if (argc != 3) { 19 fprintf(stderr, "usage: %s config_file log_file\n", 20 argv[0]); 21 exit(1); 22 } 23 24 cfg_file = argv[1]; 25 log_file = argv[2]; 26 27 fpcfg = fopen(cfg_file, "rb"); 28 if (fpcfg == NULL) { 29 fprintf(stderr, "error: could not open %s\n", cfg_file); 30 exit(1); 31 } 32 33 r = fread(&config, 1, sizeof(config), fpcfg); 34 35 if (!valid_config(&config)) { 36 fprintf(stderr, "error: invalid config read.\n"); 37 exit(1); 38 } 39 40 fplog = fopen(log_file, "rb"); 41 if (fplog == NULL) { 42 fprintf(stderr, "error: could not open %s\n", log_file); 43 exit(1); 44 } 45 46 addr = 0; 47 sessions = 0; 48 while (addr < config.nvm_start_addr) { 49 r = fread(&next, 1, sizeof(next), fplog); 50 if (r != sizeof(next)) { 51 fprintf(stderr, "error: could not read next" 52 " address\n"); 53 exit(1); 54 } 55 addr += r; 56 57 if (fseek(fplog, next, SEEK_SET) == -1) { 58 fprintf(stderr, "error: could not seek next" 59 " address\n"); 60 exit(1);

36

Page 37: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

61 } 62 63 printf("Session %d: %d bytes.\n", sessions, next - addr); 64 addr = next; 65 ++sessions; 66 } 67 68 fclose(fpcfg); 69 fclose(fplog); 70 71 return 0; 72 }

<<<<< END pc/loginfo.c <<<<<

>>>>> BEG pc/mkconfig.c >>>>> 1 #include <stdio.h> 2 #include <windows.h> 3 4 #include "config.h" 5 #include "event.h" 6 #include "util.h" 7 8 void update_crc(struct config *conf); 9 10 int 11 main(int argc, char **argv) 12 { 13 FILE *fp; 14 char *out_file; 15 int r; 16 int i; 17 struct config config; 18 19 if (argc < 5 || (argc - 2) % 3) { 20 fprintf(stderr, "usage: %s out_file interval mode pid " 21 "[interval mode pid] [...]\n", argv[0]); 22 exit(1); 23 } 24 25 out_file = argv[1]; 26 27 argc -= 2; 28 argv += 2; 29 30 memset(&config, 0, sizeof(config)); 31 32 for (i = 0; i < NUM_LOG_SLOTS; ++i) { 33 if (argc < 3) 34 break; 35 config.event[i].interval = atoi(argv[0]); 36 config.event[i].mode = atoi(argv[1]); 37 config.event[i].pid = atoi(argv[2]); 38 argc -= 3; 39 argv += 3; 40 #if 0 41 printf("int: %d, mode: %d, pid: %d\n", 42 config.event[i].interval, config.event[i].mode, 43 config.event[i].pid); 44 #endif 45 } 46

37

Page 38: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 39: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 40: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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 };

<<<<< END pc/obd_pid.c <<<<<

>>>>> BEG pc/plotlog.c >>>>> 1 #include <math.h> 2 #include <windows.h> 3 #include <stdio.h> 4 5 #include "config.h" 6 #include "event.h" 7 #include "util.h" 8 #include "obd_pid.h" 9

40

Page 41: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

10 #define TWO_AXIS_DIFF_MIN 1000 11 12 int 13 main(int argc, char **argv) 14 { 15 FILE *fpcfg, *fplog; 16 char *cfg_file, *log_file; 17 int r; 18 struct config config; 19 uint32_t next, file_size; 20 int session_num; 21 int cur_session; 22 double time; 23 int i, j; 24 double min[NUM_LOG_SLOTS], max[NUM_LOG_SLOTS]; 25 int overall_min, overall_max; 26 int firstrun[NUM_LOG_SLOTS]; 27 double xrange; 28 int event_count; 29 int axis_num[NUM_LOG_SLOTS]; 30 char *out_file; 31 char *in_file; 32 int xpixels; 33 34 if (argc != 6) { 35 fprintf(stderr, "usage: %s config_file log_file " 36 "in_file out_file session_num\n", argv[0]); 37 exit(1); 38 } 39 40 cfg_file = argv[1]; 41 log_file = argv[2]; 42 in_file = argv[3]; 43 out_file = argv[4]; 44 session_num = atoi(argv[5]); 45 46 fpcfg = fopen(cfg_file, "rb"); 47 if (fpcfg == NULL) { 48 fprintf(stderr, "error: could not open %s\n", cfg_file); 49 exit(1); 50 } 51 52 r = fread(&config, 1, sizeof(config), fpcfg); 53 54 if (!valid_config(&config)) { 55 fprintf(stderr, "error: invalid config read.\n"); 56 exit(1); 57 } 58 59 fplog = fopen(log_file, "rb"); 60 if (fplog == NULL) { 61 fprintf(stderr, "error: could not open %s\n", log_file); 62 exit(1); 63 } 64 fseek(fplog, 0, SEEK_END); 65 file_size = ftell(fplog); 66 rewind(fplog); 67 68 cur_session = 0; 69 while (cur_session <= session_num) { 70 r = fread(&next, 1, sizeof(next), fplog); 71 if (r != sizeof(next)) {

41

Page 42: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

72 fprintf(stderr, "error: could not read next" 73 " address (session # too big)\n"); 74 exit(1); 75 } 76 77 if (cur_session == session_num) 78 break; 79 80 if (fseek(fplog, next, SEEK_SET) == -1) { 81 fprintf(stderr, "error: could not seek next" 82 " address (session # too big)\n"); 83 exit(1); 84 } 85 86 ++cur_session; 87 } 88 89 if (ftell(fplog) == file_size) { 90 fprintf(stderr, "error: invalid session number\n"); 91 exit(1); 92 } 93 94 for (i = 0; i < NUM_LOG_SLOTS; ++i) 95 firstrun[i] = 1; 96 97 event_count = 0; 98 99 time = 0; 100 while (ftell(fplog) < next) { 101 uint32_t data; 102 double value; 103 104 time += 0.5; 105 106 for (i = 0; i < NUM_LOG_SLOTS; ++i) { 107 if (config.event[i].interval == 0) 108 continue; 109 110 config.event[i].count++; 111 112 if (config.event[i].count == config.event[i].interval) { 113 config.event[i].count = 0; 114 115 /* read the data, inc addr */ 116 data = 0; 117 r = fread(&data, 1, PID_LENGTH(config.event[i].pid), fplog); 118 if (r == 0) { 119 fprintf(stderr, "error: read\n"); 120 exit(1); 121 } 122 123 if (PID_LENGTH(config.event[i].pid) == 2) { 124 data = htons(data); 125 } else if (PID_LENGTH(config.event[i].pid) == 4) { 126 data = htonl(data); 127 } 128 129 value = conv_pid_data(config.event[i].pid, data); 130 if (firstrun[i]) { 131 min[i] = value; 132 max[i] = value; 133 firstrun[i] = 0;

42

Page 43: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

134 ++event_count; 135 } else { 136 if (min[i] > value) 137 min[i] = value; 138 if (max[i] < value) 139 max[i] = value; 140 } 141 } 142 } 143 } 144 145 overall_min = 0; 146 overall_max = 0; 147 148 for (i = 0; i < NUM_LOG_SLOTS; ++i) { 149 if (config.event[i].interval == 0) 150 continue; 151 152 if (min[overall_min] > min[i]) 153 overall_min = i; 154 if (max[overall_max] < max[i]) 155 overall_max = i; 156 } 157 158 xrange = time; 159 #if 0 160 for (i = 0; i < NUM_LOG_SLOTS; ++i) { 161 if (config.event[i].interval == 0) 162 continue; 163 164 printf("min[%d]: %f\n", i, min[i]); 165 printf("max[%d]: %f\n", i, max[i]); 166 } 167 168 printf("overall_min: %f\n", min[overall_min]); 169 printf("overall_max: %f\n", max[overall_max]); 170 printf("y range: %f\n", yrange); 171 printf("xrange: %f\n", xrange); 172 printf("%d events\n", event_count); 173 #endif 174 for (i = 0; i < NUM_LOG_SLOTS; ++i) 175 axis_num[i] = -1; 176 177 axis_num[0] = 0; 178 for (i = 1; i < NUM_LOG_SLOTS; ++i) { 179 if (config.event[i].interval == 0) 180 continue; 181 182 for (j = 0; j < NUM_LOG_SLOTS; ++j) { 183 if (config.event[j].interval == 0) 184 continue; 185 /* 186 * see if we wouldn't fit with a plot 187 * in axis 1. If we dont, go to axis 2 188 */ 189 if (axis_num[j] == 0) { 190 if (abs(min[i] - min[j]) > TWO_AXIS_DIFF_MIN) { 191 axis_num[i] = 1; 192 break; 193 } else if (abs(max[i] - max[j]) > TWO_AXIS_DIFF_MIN) { 194 axis_num[i] = 1; 195 break;

43

Page 44: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

196 } else { 197 axis_num[i] = 0; 198 break; 199 } 200 } 201 } 202 } 203 204 #define DIST_BETWEEN_POINTS 5 205 xpixels = time * DIST_BETWEEN_POINTS; 206 printf("#!/bin/sh\n\n"); 207 printf("echo 'set terminal png size %d; set output \"%s\"; set xtics 0, 20; set xlabel \"Time (s)\"; set grid; set xrange [%f:%f]; set y2tics border; plot", xpixels, out_file, 0.0, xrange); 208 209 for (i = 0; i < NUM_LOG_SLOTS; ++i) { 210 char *axis; 211 if (config.event[i].interval == 0) 212 continue; 213 214 if (axis_num[i] == 0) 215 axis = "x1y1"; 216 else 217 axis = "x1y2"; 218 219 printf(" \"%s\" using 1:%d axis %s title \"%s (%s) %s\" with lines lw 3", in_file, i + 2, axis, pid_name[config.event[i].pid], pid_units(config.event[i].pid), axis); 220 221 --event_count; 222 223 if (event_count > 0) 224 printf(","); 225 } 226 227 printf(";' | gnuplot\n"); 228 229 fclose(fpcfg); 230 fclose(fplog); 231 232 return 0; 233 }

<<<<< END pc/plotlog.c <<<<<

>>>>> BEG pc/progress.c >>>>> 1 #include <stdio.h> 2 #include <string.h> 3 #include <stdint.h> 4 #include <unistd.h> 5 #include <math.h> 6 #include <sys/time.h> 7 8 #ifndef WIN32 9 #include <sys/ioctl.h> 10 #include <termios.h> 11 #endif 12 13 void 14 progress_bar(uint32_t part, uint32_t total) 15 { 16 #define MINWIDTH 80 17 static struct timeval now, lp = { 0 };

44

Page 45: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

18 char *units[] = { "Bytes", "KBytes", "MBytes", "GBytes" }; 19 int unit; 20 char buf[MINWIDTH]; 21 int pct = ((float) part / total) * 100; 22 int cnt; 23 float t, p; 24 int hlen, tlen, plen; 25 int width = MINWIDTH; 26 #ifndef WIN32 27 struct winsize ws; 28 #endif 29 30 gettimeofday(&now, NULL); 31 if (part < total) { 32 if (now.tv_sec - lp.tv_sec == 0 && 33 now.tv_usec - lp.tv_usec < 100000) 34 return; 35 } 36 gettimeofday(&lp, NULL); 37 38 #ifndef WIN32 39 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) != -1) 40 width = ws.ws_col; 41 #endif 42 43 t = total; 44 p = part; 45 for (unit = 0; unit < 3 && t > 1024; ++unit) { 46 t /= 1024; 47 p /= 1024; 48 } 49 tlen = snprintf(buf, sizeof(buf), "] % 3d%% (%.01f/%.01f %s) ", 50 100, t, t, units[unit]); 51 hlen = snprintf(buf, sizeof(buf), "Progress: ["); 52 plen = width - hlen - 35; 53 if (width - hlen - tlen < plen) 54 plen = width - hlen - tlen; 55 else 56 tlen = 35; 57 58 printf("\r"); 59 printf("%s", buf); 60 cnt = floor(plen * pct / 100.0); 61 while (cnt-- > 0) 62 putchar('='); 63 cnt = plen - floor(plen * pct / 100.0); 64 while (cnt-- > 0) 65 putchar(' '); 66 if (t != p) 67 printf("] % 3d%% (%.01f/%.01f %s) ", pct, p, t, 68 units[unit]); 69 else { 70 printf("] Complete. "); 71 cnt = tlen - strlen("] Complete. "); 72 while (cnt-- > 0) 73 putchar(' '); 74 printf("\n"); 75 } 76 77 fflush(stdout); 78 }

45

Page 46: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

<<<<< END pc/progress.c <<<<<

>>>>> BEG pc/putconfig.c >>>>> 1 #include <stdio.h> 2 #include <windows.h> 3 4 #include "config.h" 5 #include "event.h" 6 #include "util.h" 7 #include "serial.h" 8 9 int 10 main(int argc, char **argv) 11 { 12 char *com_file; 13 char *in_file; 14 FILE *fp; 15 struct serial_dev sdev; 16 int r, total; 17 struct config config; 18 uint8_t *p; 19 char buf[128]; 20 21 if (argc != 3) { 22 fprintf(stderr, "usage: %s comN input_file\n", argv[0]); 23 exit(1); 24 } 25 26 com_file = argv[1]; 27 in_file = argv[2]; 28 29 fp = fopen(in_file, "rb"); 30 if (fp == NULL) { 31 fprintf(stderr, "error: could not open %s\n", in_file); 32 exit(1); 33 } 34 35 if (init_serial(&sdev, com_file) == -1) 36 exit(1); 37 38 r = fread(&config, 1, sizeof(config), fp); 39 if (r != sizeof(config)) { 40 fprintf(stderr, "error: could not read config.\n"); 41 exit(1); 42 } 43 44 if (!valid_config(&config)) { 45 fprintf(stderr, "error: invalid config read.\n"); 46 exit(1); 47 } 48 49 strcpy(buf, "!!!\recho off\r"); 50 r = write_ser(&sdev, buf, strlen(buf)); 51 if (r != strlen(buf)) { 52 fprintf(stderr, "error: could not write.\n"); 53 exit(1); 54 } 55 56 ser_flush(&sdev); 57 58 strcpy(buf, "write_cfg\r"); 59 r = write_ser(&sdev, buf, strlen(buf));

46

Page 47: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

60 if (r != strlen(buf)) { 61 fprintf(stderr, "error: could not write.\n"); 62 exit(1); 63 } 64 65 total = 0; 66 p = (uint8_t *) &config; 67 while (total < sizeof(config)) { 68 r = sizeof(config) - total; 69 70 r = write_ser(&sdev, p, r); 71 if (r == 0) { 72 fprintf(stderr, "error: could not write.\n"); 73 exit(1); 74 } else { 75 total += r; 76 p += r; 77 } 78 } 79 80 r = read_ser(&sdev, buf, sizeof(buf)); 81 buf[r] = '\0'; 82 if (r == 0) { 83 fprintf(stderr, "error: getting response.\n"); 84 exit(1); 85 } 86 87 if (!strncmp(buf, "OK", 2)) { 88 printf("Device Accepted Configuration.\n"); 89 } else { 90 fprintf(stderr, "Device Rejected Configuration.\n"); 91 exit(1); 92 } 93 94 close_serial(&sdev); 95 96 fclose(fp); 97 98 return 0; 99 }

<<<<< END pc/putconfig.c <<<<<

>>>>> BEG pc/serial.c >>>>> 1 #include <stdio.h> 2 #include <windows.h> 3 4 #include "serial.h" 5 6 int 7 init_serial(struct serial_dev *sdev, char *port_name) 8 { 9 DCB dcbSerialParams; 10 COMMTIMEOUTS timeouts; 11 12 memset(&dcbSerialParams, 0, sizeof(dcbSerialParams)); 13 memset(&timeouts, 0, sizeof(timeouts)); 14 15 sdev->hSerial = CreateFile(port_name, GENERIC_READ | GENERIC_WRITE, 16 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); 17 18 if (sdev->hSerial == INVALID_HANDLE_VALUE) {

47

Page 48: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

19 fprintf(stderr, "error: could not open serial port\n"); 20 return -1; 21 } 22 23 dcbSerialParams.DCBlength = sizeof(dcbSerialParams); 24 if (!GetCommState(sdev->hSerial, &dcbSerialParams)) { 25 fprintf(stderr, "error: could not get serial port state\n"); 26 return -1; 27 } 28 29 dcbSerialParams.BaudRate = CBR_57600; 30 dcbSerialParams.ByteSize = 8; 31 dcbSerialParams.StopBits = ONESTOPBIT; 32 dcbSerialParams.Parity = NOPARITY; 33 34 if (!SetCommState(sdev->hSerial, &dcbSerialParams)) { 35 fprintf(stderr, "error: could not set serial port state\n"); 36 return -1; 37 } 38 39 timeouts.ReadIntervalTimeout = 50; 40 timeouts.ReadTotalTimeoutConstant = 200; 41 timeouts.ReadTotalTimeoutMultiplier = 10; 42 timeouts.WriteTotalTimeoutConstant = 50; 43 timeouts.WriteTotalTimeoutMultiplier = 10; 44 45 if (!SetCommTimeouts(sdev->hSerial, &timeouts)) { 46 fprintf(stderr, "error: could not set serial port " 47 "timeouts\n"); 48 return -1; 49 } 50 51 EscapeCommFunction(sdev->hSerial, SETRTS); 52 53 return 0; 54 } 55 56 void 57 close_serial(struct serial_dev *sdev) 58 { 59 CloseHandle(sdev->hSerial); 60 } 61 62 size_t 63 write_ser(struct serial_dev *sdev, void *buf, size_t len) 64 { 65 DWORD written; 66 67 WriteFile(sdev->hSerial, buf, len, &written, NULL); 68 69 return written; 70 } 71 72 size_t 73 read_ser(struct serial_dev *sdev, void *buf, size_t len) 74 { 75 DWORD read; 76 77 ReadFile(sdev->hSerial, buf, len, &read, NULL); 78 79 return read; 80 }

48

Page 49: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

81 82 void 83 ser_flush(struct serial_dev *sdev) 84 { 85 int r; 86 char buf[128]; 87 88 do { 89 r = read_ser(sdev, buf, sizeof(buf)); 90 } while (r > 0); 91 }

<<<<< END pc/serial.c <<<<<

>>>>> BEG pc/statistics.c >>>>> 1 #include <math.h> 2 #include <windows.h> 3 #include <stdio.h> 4 5 #include "config.h" 6 #include "event.h" 7 #include "util.h" 8 #include "obd_pid.h" 9 10 #define TWO_AXIS_DIFF_MIN 1000 11 12 /* what speed to count as a 'stop' */ 13 #define MPH_STOP_SPEED 3 14 #define MPH_START_SPEED 5 15 16 int 17 main(int argc, char **argv) 18 { 19 FILE *fpcfg, *fplog; 20 char *cfg_file, *log_file; 21 int r; 22 struct config config; 23 uint32_t next, file_size; 24 int session_num; 25 int cur_session; 26 double time; 27 int i, j; 28 double min[NUM_LOG_SLOTS], max[NUM_LOG_SLOTS]; 29 int firstrun[NUM_LOG_SLOTS]; 30 char *out_file; 31 char *in_file; 32 int num_stops; 33 int speed_state; 34 int speed_present; 35 double stopped_time; 36 double distance; 37 38 if (argc != 4) { 39 fprintf(stderr, "usage: %s config_file log_file " 40 "session_num\n", argv[0]); 41 exit(1); 42 } 43 44 cfg_file = argv[1]; 45 log_file = argv[2]; 46 session_num = atoi(argv[3]); 47

49

Page 50: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

48 fpcfg = fopen(cfg_file, "rb"); 49 if (fpcfg == NULL) { 50 fprintf(stderr, "error: could not open %s\n", cfg_file); 51 exit(1); 52 } 53 54 r = fread(&config, 1, sizeof(config), fpcfg); 55 56 if (!valid_config(&config)) { 57 fprintf(stderr, "error: invalid config read.\n"); 58 exit(1); 59 } 60 61 fplog = fopen(log_file, "rb"); 62 if (fplog == NULL) { 63 fprintf(stderr, "error: could not open %s\n", log_file); 64 exit(1); 65 } 66 fseek(fplog, 0, SEEK_END); 67 file_size = ftell(fplog); 68 rewind(fplog); 69 70 cur_session = 0; 71 while (cur_session <= session_num) { 72 r = fread(&next, 1, sizeof(next), fplog); 73 if (r != sizeof(next)) { 74 fprintf(stderr, "error: could not read next" 75 " address (session # too big)\n"); 76 exit(1); 77 } 78 79 if (cur_session == session_num) 80 break; 81 82 if (fseek(fplog, next, SEEK_SET) == -1) { 83 fprintf(stderr, "error: could not seek next" 84 " address (session # too big)\n"); 85 exit(1); 86 } 87 88 ++cur_session; 89 } 90 91 if (ftell(fplog) == file_size) { 92 fprintf(stderr, "error: invalid session number\n"); 93 exit(1); 94 } 95 96 for (i = 0; i < NUM_LOG_SLOTS; ++i) 97 firstrun[i] = 1; 98 99 #define MOVING 0 100 #define STOPPED 1 101 102 time = 0; 103 num_stops = 0; 104 speed_state = STOPPED; 105 speed_present = 0; 106 stopped_time = 0; 107 distance = 0; 108 while (ftell(fplog) < next) { 109 uint32_t data;

50

Page 51: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

110 double value; 111 112 time += 0.5; 113 114 for (i = 0; i < NUM_LOG_SLOTS; ++i) { 115 if (config.event[i].interval == 0) 116 continue; 117 118 config.event[i].count++; 119 120 if (config.event[i].count == config.event[i].interval) { 121 config.event[i].count = 0; 122 123 /* read the data, inc addr */ 124 data = 0; 125 r = fread(&data, 1, PID_LENGTH(config.event[i].pid), fplog); 126 if (r == 0) { 127 fprintf(stderr, "error: read\n"); 128 exit(1); 129 } 130 131 if (PID_LENGTH(config.event[i].pid) == 2) { 132 data = htons(data); 133 } else if (PID_LENGTH(config.event[i].pid) == 4) { 134 data = htonl(data); 135 } 136 137 value = conv_pid_data(config.event[i].pid, data); 138 139 if (config.event[i].pid == VEHICLE_SPEED) { 140 static double last_accel = 0; 141 static double last_speed = -1; 142 static double accel; 143 144 speed_present = 1; 145 if (speed_state == STOPPED) { 146 stopped_time += 0.5; 147 if (num_stops == 0) 148 ++num_stops; 149 } 150 151 if (value < MPH_STOP_SPEED && 152 speed_state == MOVING) { 153 speed_state = STOPPED; 154 ++num_stops; 155 #if 0 156 printf("stop at time: %.02f\n", time); 157 #endif 158 } else if (value > MPH_START_SPEED) 159 speed_state = MOVING; 160 161 distance += config.event[i].interval * 0.5 * value; 162 163 if (last_speed == -1) 164 last_speed = value; 165 166 accel = (value - last_speed) / (config.event[i].interval * 0.5); 167 //accel printf("%.02f %.02f\n", time, accel / 3600.0 * 1609.344); 168 169 last_speed = value;

51

Page 52: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

170 } 171 172 if (firstrun[i]) { 173 min[i] = value; 174 max[i] = value; 175 firstrun[i] = 0; 176 } else { 177 if (min[i] > value) 178 min[i] = value; 179 if (max[i] < value) 180 max[i] = value; 181 } 182 } 183 } 184 } 185 186 // exit(0); 187 188 for (i = 0; i < NUM_LOG_SLOTS; ++i) { 189 if (config.event[i].interval == 0) 190 continue; 191 192 printf("%s:\n", pid_name[config.event[i].pid]); 193 194 printf("\tMin: %.02f %s\n", min[i], 195 pid_units(config.event[i].pid)); 196 printf("\tMax: %.02f %s\n", max[i], 197 pid_units(config.event[i].pid)); 198 printf("\n"); 199 } 200 201 if (speed_present) { 202 printf("Number of stops: %d\n", num_stops); 203 printf("Amount of time stopped: %02d:%02d:%02d (%.2f seconds)\n", (int) floor(stopped_time / 3600), (int) floor(stopped_time / 60), (int) stopped_time % 60, stopped_time); 204 printf("Disntance travelled: %.02f miles\n", distance / 3600); 205 } 206 207 fclose(fpcfg); 208 fclose(fplog); 209 210 return 0; 211 }

<<<<< END pc/statistics.c <<<<<

>>>>> BEG pc/util.c >>>>> 1 #include <stdint.h> 2 #include <stdio.h> 3 #include <string.h> 4 5 #include "obd_pid.h" 6 #include "util.h" 7 8 int 9 valid_config(struct config *conf) 10 { 11 uint8_t *p; 12 uint16_t crc, orig_crc; 13 int i; 14

52

Page 53: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 54: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 55: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 56: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 57: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

21 #include <string.h> 22 #include <stdio.h> 23 #include <stdlib.h> 24 #include <ctype.h> 25 26 #include <avr/eeprom.h> 27 #include <util/crc16.h> 28 #include <util/delay.h> 29 30 #include "elm.h" 31 #include "util.h" 32 #include "usart.h" 33 #include "config.h" 34 #include "event.h" 35 #include "nvm.h" 36 #include "cmd.h" 37 #include "obd_pid.h" 38 #include "elm_dtc.h" 39 #include "ui.h" 40 41 #define DTC_INIT_TRIES 2 42 43 void 44 num_dtc_cmd(struct cmd * cmd) 45 { 46 char buf[32]; 47 char dtc_buf[6]; 48 uint8_t i; 49 uint8_t count; 50 51 elm_init_retry( &myelm, PROT_ISO9141 ); /*Init with retries*/ 52 53 if (myelm.initd == 0) { 54 usart_puts(0, "DTC Check Failed. ELM could " 55 "not be initialized.\r\n"); 56 return; 57 } 58 59 usart_puts(0,"Getting DTC codes...\r\n"); 60 elm_dtc_update(); 61 62 if( elm_dtc_mil() ) 63 usart_puts(0,"Your check engine light is on.\r\n"); 64 65 count = elm_dtc_count(); 66 67 if( count == 0 ) { 68 usart_puts(0, "No trouble codes detected.\r\n"); 69 return; 70 } 71 72 snprintf(buf,sizeof(buf),"Detected %d trouble codes\r\n", 73 count ); 74 usart_puts(0,buf); 75 76 for(i=0;i<count;++i) { 77 DTC_CODE( dtc_buf, elm_dtc_current() ); 78 snprintf(buf,sizeof(buf),"%d: %s\r\n", i, dtc_buf); 79 usart_puts(0, buf); 80 elm_dtc_next(); 81 } 82

57

Page 58: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

83 return; 84 } 85 86 void 87 set_livemode_cmd(struct cmd *cmd) 88 { 89 live_mode = 1; 90 usart_puts(0, "OK\r\n"); 91 } 92 93 void 94 read_cfg_cmd(struct cmd *cmd) 95 { 96 uint8_t buf[128]; 97 98 eeprom_read_block(buf, 0, sizeof(cfg)); 99 100 usart_put(0, buf, sizeof(cfg)); 101 } 102 103 void 104 write_cfg_cmd(struct cmd *cmd) 105 { 106 struct config new_cfg; 107 int i; 108 uint8_t *p; 109 110 p = (uint8_t *) &new_cfg; 111 for (i = 0; i < sizeof(new_cfg); ++i) { 112 *p = usart_getc(0); 113 ++p; 114 } 115 116 if (!valid_config(&new_cfg)) { 117 usart_puts(0, "ERROR\r\n"); 118 return; 119 } else 120 usart_puts(0, "OK\r\n"); 121 122 /* update configuration */ 123 memcpy(&cfg, &new_cfg, sizeof(cfg)); 124 write_config(&cfg); 125 126 /* re-init events */ 127 event_init(&cfg.event[0]); 128 129 lcd_update(&lcd, 0, "Erasing flash..."); 130 lcd_update(&lcd, 1, ""); 131 132 /* erase flash */ 133 nvm_erase(); 134 135 usart_puts(0, "DONE\r\n"); 136 137 lcd_update(&lcd, 1, " Done."); 138 139 _delay_ms(300); 140 141 menu_repaint(); 142 } 143 144 void

58

Page 59: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

145 read_cmd(struct cmd *cmd) 146 { 147 uint32_t beg, end, len; 148 uint8_t buf[128]; 149 150 if (cmd->argc != 3) { 151 snprintf((char *) buf, sizeof(buf), 152 "usage: %s start_addr length\r\n", cmd->argv[0]); 153 usart_puts(0, (char *) buf); 154 return; 155 } 156 157 beg = hexstr(cmd->argv[1], strlen(cmd->argv[1])); 158 len = atol(cmd->argv[2]); 159 160 if (len == 0) { 161 usart_puts(0, "invalid length\r\n"); 162 return; 163 } 164 165 end = len + beg - 1; 166 167 while (beg <= end) { 168 len = end - beg + 1; 169 if (len > sizeof(buf)) 170 len = sizeof(buf); 171 172 if (!strcasecmp(cmd->argv[0], "reade")) 173 eeprom_read_block(buf, beg, len); 174 else if (!strcasecmp(cmd->argv[0], "readn")) 175 nvm_read(buf, beg, len); 176 177 hexdump(buf, len); 178 179 beg += len; 180 } 181 182 usart_puts(0, "done.\r\n"); 183 } 184 185 void 186 read_log_cmd(struct cmd *cmd) 187 { 188 uint32_t beg, len; 189 uint8_t buf[128]; 190 int i; 191 uint16_t crc; 192 193 if (cmd->argc != 3) 194 return; 195 196 beg = hexstr(cmd->argv[1], strlen(cmd->argv[1])); 197 len = atol(cmd->argv[2]); 198 if (len < 1 || len > 126) { 199 usart_puts(0, "invalid length\r\n"); 200 return; 201 } 202 203 nvm_read(buf, beg, len); 204 205 buf[len] = 0; 206 buf[len + 1] = 0;

59

Page 60: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

207 crc = 0xffff; 208 for (i = 0; i < len + 2; ++i) 209 crc = _crc16_update(crc, buf[i]); 210 211 memcpy(&buf[len], &crc, sizeof(crc)); 212 213 usart_put(0, buf, len + 2); 214 } 215 216 void 217 erase_nvm_cmd(void) 218 { 219 usart_puts(0, "Erasing flash..."); 220 lcd_update(&lcd, 0, "Erasing flash..."); 221 lcd_update(&lcd, 1, ""); 222 nvm_erase(); 223 usart_puts(0, "\r\nErased.\r\n"); 224 lcd_update(&lcd, 1, " Done."); 225 cfg.nvm_start_addr = 0; 226 write_config(&cfg); 227 menu_repaint(); 228 _delay_ms(300); 229 } 230 231 void 232 echo_cmd(struct cmd *cmd) 233 { 234 uint8_t echo; 235 char buf[64]; 236 237 echo = -1; 238 if (cmd->argc == 2) { 239 echo = atoi(cmd->argv[1]); 240 } 241 242 if (echo < 0 || echo > 1) { 243 snprintf(buf, sizeof(buf), "usage: echo <0 or 1>\r\n"); 244 usart_puts(0, buf); 245 } else { 246 usart_puts(0, "OK\r\n"); 247 usart_echo(0, echo); 248 } 249 } 250 251 void 252 parse_line(struct cmd *v) 253 { 254 int c; 255 int i; 256 static char parse_buf[WC][WORD_LEN]; 257 258 v->argc = -1; 259 260 i = usart_peek(0, '\r'); 261 if (i == 0) 262 return; 263 else if (i == -1) { 264 usart_flush(0); 265 return; 266 } 267 268 v->argc = 0;

60

Page 61: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 62: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

28 #include "config.h" 29 #include "util.h" 30 #include "lcd.h" 31 #include "usart.h" 32 33 int8_t 34 read_config(struct config *conf) 35 { 36 eeprom_read_block(conf, 0, sizeof(*conf)); 37 38 if (valid_config(conf)) 39 return 0; 40 41 return -1; 42 } 43 44 void 45 write_config(struct config *conf) 46 { 47 uint16_t i; 48 uint16_t crc; 49 uint8_t *p; 50 51 p = (uint8_t *) conf; 52 53 crc = 0xffff; 54 conf->crc = 0x0000; 55 56 for (i = 0; i < sizeof(*conf); ++i) { 57 crc = _crc16_update(crc, *p); 58 ++p; 59 } 60 61 conf->crc = crc; 62 63 eeprom_write_block(conf, 0, sizeof(*conf)); 64 } 65 66 uint8_t 67 valid_config(struct config *conf) 68 { 69 uint8_t *p; 70 uint16_t crc, orig_crc; 71 int i; 72 73 p = (uint8_t *) conf; 74 orig_crc = conf->crc; 75 76 conf->crc = 0; 77 crc = 0xffff; 78 for (i = 0; i < sizeof(*conf); ++i) { 79 crc = _crc16_update(crc, *p); 80 ++p; 81 } 82 83 conf->crc = orig_crc; 84 85 if (crc == orig_crc) 86 return 1; 87 88 return 0; 89 }

62

Page 63: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 64: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 65: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 66: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 67: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 68: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 69: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 70: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 71: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 72: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 73: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

240 241 uint8_t buf[32]; 242 243 if(reset == 1) { 244 line_count=0; 245 cnt=0; 246 return 0; 247 }

248 249 if(line_count == 0 && cnt == 0) { 250 elm_readdata(buf,sizeof(buf)); /*This will get the multi-line byte cound (3 hex digts???? WHY did they do that?)*/ 251 #if 0 252 num_lines = buf[0]&0xF<<4)|(buf[1]>>4); 253 num_lines += 1; 254 num_lines = ceil( num_lines / 7.0 ); 255 snprintf(buf,sizeof(buf), "Number of lines: %d", num_lines); 256 usart_puts(0,buf); 257 #endif 258 cnt++; /*Counts as first byte on first line..*/ 259 } 260 261 code = 0; 262 state = 0; 263 264 for(i=0;i<2;++i) 265 { 266 usart_puts(0, "loopdyloop\r\n"); 267 if( (line_count == 0 && cnt == 1) || (cnt==0 && line_count != 0) ) 268 { 269 line = multi_linenum(); 270 271 if(line != line_count%16 ) 272 { 273 lcd_update(&lcd, 1, "Bus Error. Retry." ); 274 _delay_ms(300); 275 menu_set_current( MENU_ID_ROOT ); 276 menu_repaint(); 277 return 0; 278 } 279 } 280 281 if(line_count == 0 && cnt == 1) 282 { 283 cnt+=2; 284 elm_readdata(buf,2); /*Throw away the response bytes*/ 285 } 286 287 elm_readdata(buf,1); 288 cnt++; 289 290 if(state == 0){ 291 code = buf[0]; 292 state = 1; 293 } 294 else { 295 code |= buf[0]<<8; 296 state = 0; 297 } 298 299 if( cnt == MULTI_LINE_PACKET_LENGTH )

73

Page 74: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

300 { 301 elm_flush('\n'); 302 line_count++; 303 cnt = 0; 304 } 305 } 306 307 return code; 308 }

<<<<< END software/elm_dtc.c <<<<<

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

Page 75: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

50 51 void 52 timer_start(void) 53 { 54 usart_puts(0, "enabled timer\r\n"); 55 56 timer.ding = 0; 57 timer.overflow = 0; 58 59 TCCR1B = 0x00; 60 61 TCNT1H = 0xFF; 62 TCNT1L = 0xFF; 63 TIFR1 = 0x01; 64 TIMSK1 = 0x01; 65 66 TCCR1B = 0x05; 67 } 68 69 void 70 timer_stop(void) 71 { 72 TIMSK1 = 0x00; 73 usart_puts(0, "disabled timer\r\n"); 74 } 75 76 /* called at startup, and every time the config event's change */ 77 int8_t 78 event_init(struct event *e) 79 { 80 int i; 81 82 events_used = 0; 83 memset(event, 0, sizeof(event)); 84 85 for (i = 0; i < NUM_LOG_SLOTS; ++i) { 86 if (event_add(e) == -1) 87 return -1; 88 ++e; 89 } 90 91 return 0; 92 } 93 94 int8_t 95 event_add(struct event *e) 96 { 97 if (events_used == 0) { 98 event[0].interval = FLASH_INTERVAL; 99 event[0].action = ACTION_FLASH; 100 ++events_used; 101 } 102 103 if (events_used >= MAX_EVENTS) 104 return -1; 105 if (e->action == ACTION_FLASH) 106 return -1; 107 108 memcpy(&event[events_used], e, sizeof(*e)); 109 event[events_used].count = 0; 110 ++events_used; 111

75

Page 76: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

112 return 0; 113 } 114 115 /* called to start / resume logging after event_init() */ 116 void 117 events_start(uint32_t nvm_start_addr) 118 { 119 int i; 120 char buf[32]; 121 double mem_used; 122 123 for (i = 0; i < events_used; ++i) 124 event[i].count = 0; 125 126 /* 127 * Leave 32 bit's of 1s here. Updated when logging is stopped. 128 * (points to start of next logging session). 129 */ 130 cur_nvm_addr = nvm_start_addr; 131 cur_nvm_addr += 4; 132 133 if (live_mode) { 134 snprintf(buf, sizeof(buf), " Live Statistics"); 135 } else { 136 mem_used = (cur_nvm_addr * 100.0 / NVM_PAGE_SIZE) / 137 NVM_NUM_PAGES; 138 snprintf(buf, sizeof(buf), " Mem Used: %.2f%%", 139 mem_used); 140 } 141 lcd_update(&lcd, 1, buf); 142 143 timer_start(); 144 } 145 146 uint8_t 147 events_end(uint32_t *nvm_start_addr) 148 { 149 char buf[64]; 150 uint8_t logged_something; 151 152 logged_something = 0; 153 154 nvm_write(cur_nvm_addr, log_buf, log_buf_used); 155 #if 0 156 snprintf(buf, sizeof(buf), "wrote %d bytes to addr: " 157 "%08lx.\r\n", log_buf_used, cur_nvm_addr); 158 usart_puts(0, buf); 159 #endif 160 cur_nvm_addr += log_buf_used; 161 log_buf_used = 0; 162 163 /* 164 * Write ending address to the beginning of the data we just 165 * logged (if we logged anything). The +4 is the next pointer 166 * address we will write if we logged something. 167 */ 168 if (cur_nvm_addr > *nvm_start_addr + 4) { 169 nvm_write(*nvm_start_addr, &cur_nvm_addr, 170 sizeof(cur_nvm_addr)); 171 snprintf(buf, sizeof(buf), "started at addr: %08lx, ended" 172 " at address: %08lx\r\n", *nvm_start_addr, 173 cur_nvm_addr);

76

Page 77: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

174 usart_puts(0, buf); 175 *nvm_start_addr = cur_nvm_addr; 176 logged_something = 1; 177 } else 178 usart_puts(0, "logged nothing\r\n"); 179 180 timer_stop(); 181 182 lcd_update(&lcd, 1, ""); 183 184 live_mode = 0; 185 186 return logged_something; 187 } 188 189 void 190 events(struct mode *mode) 191 { 192 int i; 193 194 if (timer.overflow) 195 fatal("timer overflowed..."); 196 197 if (!timer.ding) 198 return; 199 200 for (i = 0; i < events_used; ++i) { 201 /* event disabled */ 202 if (event[i].interval == 0) 203 continue; 204 205 ++event[i].count; 206 if (event[i].count == event[i].interval) { 207 /* perform some action */ 208 event[i].count = 0; 209 #if 0 210 char buf[64]; 211 sprintf(buf, "event %d\r\n", i); 212 usart_puts(0, buf); 213 #endif 214 handle_event(&event[i], mode); 215 } 216 } 217 218 timer.ding = 0; 219 } 220 221 void 222 handle_event(struct event *e, struct mode *mode) 223 { 224 char buf[64]; 225 double mem_used; 226 227 switch (e->action) { 228 case ACTION_FLASH: 229 nvm_write(cur_nvm_addr, log_buf, log_buf_used); 230 #if 0 231 snprintf(buf, sizeof(buf), "wrote %d bytes to " 232 "addr: %08lx.\r\n", log_buf_used, cur_nvm_addr); 233 usart_puts(0, buf); 234 #endif 235 cur_nvm_addr += log_buf_used;

77

Page 78: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

236 log_buf_used = 0; 237 238 if (live_mode) { 239 snprintf(buf, sizeof(buf), " Live Statistics"); 240 } else { 241 mem_used = (cur_nvm_addr * 100.0 / 242 NVM_PAGE_SIZE) / NVM_NUM_PAGES; 243 snprintf(buf, sizeof(buf), " Mem Used: " 244 "%.2f%%", mem_used); 245 } 246 lcd_update(&lcd, 1, buf); 247 break; 248 249 case ACTION_ELM: 250 elm_event(e, mode); 251 break; 252 } 253 254 #if 0 255 snprintf(buf, sizeof(buf), "buf used: %d\r\n", log_buf_used); 256 usart_puts(0, buf); 257 #endif 258 } 259 260 void 261 elm_event(struct event *e, struct mode *mode) 262 { 263 uint8_t read; /*Number of bytes read from the ELM*/ 264 char buf[20]; 265 266 get_pid_error = 0; 267 read = elm_getpid( e->pid, &log_buf[log_buf_used], sizeof(log_buf) - log_buf_used ); /*Read into the buffer*/ 268 269 if (get_pid_error) 270 { 271 change_mode(mode, MODE_CONFIG); 272 snprintf(buf,sizeof(buf),"Get PID Error (%02x)", e->pid ); 273 lcd_update(&lcd, 1, buf); 274 _delay_ms(500); 275 } 276 277 // usart_puts(0, "\r\n"); 278 // hexdump(&log_buf[log_buf_used], 2); 279 // usart_puts(0, "\r\n"); 280 281 if (live_mode) { 282 int i; 283 uint32_t val = 0; 284 uint8_t *p; 285 286 p = (uint8_t *) &val; 287 288 /* reverse byte order */ 289 for (i = 0; i < read && i < sizeof(val); ++i) { 290 *p = log_buf[read - 1 - i]; 291 ++p; 292 } 293 294 snprintf(buf, sizeof(buf), "%d=%ld\r\n", e->pid, val); 295 usart_puts(0, buf); 296 } else

78

Page 79: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

297 log_buf_used += read; /*Increment position counter*/ 298 } 299 300 ISR(TIMER1_OVF_vect) 301 { 302 TCNT1H = 0xF0; 303 TCNT1L = 0xBE; 304 305 if (timer.ding == 1) 306 timer.overflow = 1; 307 308 timer.ding = 1; 309 }

<<<<< END software/event.c <<<<<

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

Page 80: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 81: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 82: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 83: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 84: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

48 } 49 50 void 51 change_state(struct mode *mode, uint8_t newstate) 52 { 53 mode->state = newstate; 54 } 55 56 void 57 mode_seq(struct mode *mode) 58 { 59 if (mode->mode_update == 1) { 60 if (mode->state > STATE_0_INIT) { 61 mode->state = STATE_FF_END; 62 mode->mode_update = 2; 63 } else { 64 mode->mode = mode->new_mode; 65 mode->state = STATE_0_INIT; 66 mode->mode_update = 0; 67 } 68 } else if (mode->mode_update == 2) { 69 mode->mode = mode->new_mode; 70 mode->state = STATE_0_INIT; 71 mode->mode_update = 0; 72 } 73 } 74 75 uint8_t 76 ui_mode_change(struct mode *mode) 77 { 78 uint8_t r; 79 uint8_t ignition_down; 80 81 r = 0; 82 83 if (!btn_down(BTN_LCDPRESENT)) 84 return 0; 85 86 ignition_down = btn_down(BTN_IGNITION); 87 88 if (mode->mode == MODE_LOG) { 89 if (!ignition_down) { 90 r = change_mode(mode, MODE_CONFIG); 91 } 92 } else { 93 if (ignition_down) 94 r = change_mode(mode, MODE_LOG); 95 } 96 97 return r; 98 } 99 100 uint8_t 101 ser_mode_change(struct mode *mode, struct cmd *v) 102 { 103 uint8_t r; 104 uint8_t print_ok; 105 106 r = 0; 107 print_ok = 0; 108 109 if (v->argc == 3 && !strcmp(v->argv[0], "wems") &&

84

Page 85: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 86: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 87: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 88: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 89: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

57 elm_reset(); 58 return; 59 } 60 61 snprintf(buf, sizeof(buf), "Mode: %s", 62 mode_str[mode->mode]); 63 lcd_update(&lcd, 0, buf); 64 lcd_update(&lcd, 1, ""); 65 66 events_start(cfg.nvm_start_addr); 67 68 change_state(mode, 1); 69 70 break; 71 72 case 1: 73 events(mode); 74 break; 75 76 case STATE_FF_END: 77 r = events_end(&cfg.nvm_start_addr); 78 79 /* 80 * We actually logged something, update eeprom start 81 * address. 82 */ 83 if (r) { 84 usart_puts(0, "updating config\r\n"); 85 write_config(&cfg); 86 } 87 88 /* If we still arent initialized... */ 89 if (myelm.initd == 0) { 90 usart_puts(0, "Mode change failed. Could " 91 "not initialize ELM.\r\n"); 92 snprintf(buf, sizeof(buf), "Mode: %s", 93 mode_str[mode->mode]); 94 lcd_update(&lcd, 0, buf); 95 } 96 break; 97 } 98 }

<<<<< END software/mode_log.c <<<<<

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

Page 90: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

17 * PERFORMANCE OF THIS SOFTWARE. 18 * 19 */ 20 21 #include <stdint.h> 22 #include <string.h> 23 #include <stdio.h> 24 25 #include <avr/io.h> 26 #include <util/delay.h> 27 28 #include "nvm.h" 29 #include "spi.h" 30 #include "util.h" 31 #include "lcd.h" 32 #include "usart.h" 33 34 #define nvm_busy() (nvm_status(NVM_READ, 0) & NVM_BUSY_BIT) 35 36 #define NVM_SPRL_BIT (1 << 7) 37 #define NVM_SPM_BIT (1 << 6) 38 #define NVM_EPE_BIT (1 << 5) 39 #define NVM_WPP_BIT (1 << 4) 40 #define NVM_SWP_BIT (1 << 3 | 1 << 2) 41 #define NVM_WEL_BIT (1 << 1) 42 #define NVM_BUSY_BIT (1 << 0) 43 44 #define NVM_CMD_READ 0x03 45 46 #define NVM_CMD_WDISABLE 0x04 47 #define NVM_CMD_WENABLE 0x06 48 49 #define NVM_CMD_STATUS_READ 0x05 50 #define NVM_CMD_STATUS_WRITE 0x01 51 52 #define NVM_CMD_BYTE_PROGRAM 0x02 53 54 #define NVM_CMD_ERASE_CHIP 0x60 55 56 void 57 nvm_read(void *vdst, uint32_t src, uint8_t len) 58 { 59 uint8_t out[4]; 60 uint8_t *dst; 61 62 dst = (uint8_t *) vdst; 63 64 out[0] = NVM_CMD_READ; 65 66 src = byte_swap(src); 67 memcpy(&out[1], (uint8_t *) &src + 1, sizeof(src) - 1); 68 69 /* select slave */ 70 SPI_SEL(); 71 72 /* send read array command */ 73 spi_tx(out, sizeof(out)); 74 75 spi_rx(dst, len); 76 77 /* deselect slave (terminate read) */ 78 SPI_DSEL();

90

Page 91: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

79 } 80 81 int 82 nvm_erase(void) 83 { 84 uint8_t op; 85 86 op = NVM_CMD_ERASE_CHIP; 87 88 nvm_wel(NVM_SET); 89 90 SPI_SEL(); 91 92 spi_tx(&op, sizeof(op)); 93 94 SPI_DSEL(); 95 96 while (nvm_busy()); 97 98 if (nvm_status(NVM_READ, 0) & NVM_EPE_BIT) 99 fatal("NVM write failed!"); 100 101 return 0; 102 } 103 104 uint8_t 105 nvm_status(uint8_t op, uint8_t status) 106 { 107 if (op == NVM_WRITE) { 108 nvm_wel(NVM_SET); 109 op = NVM_CMD_STATUS_WRITE; 110 SPI_SEL(); 111 spi_tx(&op, sizeof(op)); 112 spi_tx(&status, sizeof(status)); 113 } else { 114 op = NVM_CMD_STATUS_READ; 115 SPI_SEL(); 116 spi_tx(&op, sizeof(op)); 117 spi_rx(&status, sizeof(status)); 118 } 119 120 SPI_DSEL(); 121 122 return status; 123 } 124 125 /* 126 * Must be called before status register can be written, and also 127 * before a byte or sequential write. 128 */ 129 void 130 nvm_wel(uint8_t op) 131 { 132 while (nvm_busy()); 133 134 if (op == NVM_SET) 135 op = NVM_CMD_WENABLE; 136 else 137 op = NVM_CMD_WDISABLE; 138 139 SPI_SEL(); 140

91

Page 92: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

141 spi_tx(&op, sizeof(op)); 142 143 SPI_DSEL(); 144 } 145 146 void 147 nvm_write(uint32_t dst, void *vsrc, uint16_t len) 148 { 149 uint8_t out[4]; 150 uint32_t be_dst; 151 uint16_t chunk_size; 152 uint8_t page_offset; 153 uint8_t *src; 154 155 src = (uint8_t *) vsrc; 156 157 out[0] = NVM_CMD_BYTE_PROGRAM; 158 159 /* 160 * Calculate page offset. Page offset is 0 for 2nd, 3rd, etc 161 * write. 162 */ 163 page_offset = dst % NVM_PAGE_SIZE; 164 chunk_size = NVM_PAGE_SIZE - page_offset; 165 166 while (len != 0) { 167 be_dst = byte_swap(dst); 168 memcpy(&out[1], (uint8_t *) &be_dst + 1, sizeof(be_dst) - 1); 169 170 if (chunk_size > len) 171 chunk_size = len; 172 173 #if 0 174 char buf[128]; 175 int i = 0; 176 _delay_ms(1000); 177 178 lcd_clrscr(); 179 lcd_home(0); 180 sprintf(buf, "a: %08lX o: %02X", be_dst, page_offset); 181 lcd_print(buf); 182 sprintf(buf, "cs: %d n: %d", chunk_size, ++i); 183 lcd_home(1); 184 lcd_print(buf); 185 186 _delay_ms(1000); 187 #endif 188 189 /* enable writing (disabled after each write command) */ 190 nvm_wel(NVM_SET); 191 #if 1 192 193 SPI_SEL(); 194 195 spi_tx(out, sizeof(out)); 196 spi_tx(src, chunk_size); 197 198 SPI_DSEL(); 199 #endif 200 201 len -= chunk_size; 202 dst += chunk_size;

92

Page 93: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

203 src += chunk_size; 204 chunk_size = NVM_PAGE_SIZE; 205 page_offset = 0; 206 207 /* wait for write to finish */ 208 while (nvm_busy()); 209 210 /* programming failure */ 211 if (nvm_status(NVM_READ, 0) & NVM_EPE_BIT) 212 fatal("NVM write failed!"); 213 } 214 }

<<<<< END software/nvm.c <<<<<

>>>>> 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 };

<<<<< END software/obd_pid.c <<<<<

>>>>> BEG software/spi.c >>>>> 1 /* 2 * Copyright (c) 2009 Nathaniel Houghton <[email protected]> 3 * Robert Kirchgessner <[email protected]> 4 *

93

Page 94: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 95: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 96: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

53 MENU_RES_DYNAMIC(MENU_ID_DTCMIL, menu_dynamic_dtcmil, MENU_ID_BACK, MENU_ID_VIEWDTC, MENU_ID_DTCMIL, NULL ); 54 MENU_RES_STATIC(MENU_ID_VIEWDTC, "\x7F View DTC \x7E", MENU_ID_DTCMIL, MENU_ID_CLEARDTC, MENU_ID_DTC, NULL ); 55 MENU_RES_STATIC(MENU_ID_CLEARDTC, "\x7F Clear DTC \x7E", MENU_ID_VIEWDTC, MENU_ID_BACK, MENU_ID_CLEAREDDTC, elm_dtc_clear ); 56 MENU_RES_STATIC(MENU_ID_CLEAREDDTC, "DTCs Cleared!", MENU_ID_CLEAREDDTC, MENU_ID_CLEAREDDTC, MENU_ID_CLEARDTC, NULL ); 57 MENU_RES_STATIC(MENU_ID_BACK, "\x7F Back \x7E", MENU_ID_CLEARDTC, MENU_ID_DTCMIL, MENU_ID_ROOT, NULL ); 58 59 /*Third Level Menu*/ 60 MENU_RES_DYNAMIC(MENU_ID_DTC, menu_dtc_current, MENU_ID_DTCLEFT, MENU_ID_DTCRIGHT, MENU_ID_VIEWDTC, NULL ); 61 62 /*Fourth Level Menu*/ 63 /*If current menu is DTCLEFT, then menu_dtc_current calls prev(), otherwise calls next()*/ 64 MENU_RES_DYNAMIC(MENU_ID_DTCLEFT, menu_dtc_current, MENU_ID_DTCLEFT, MENU_ID_DTCRIGHT, MENU_ID_VIEWDTC, NULL ); 65 MENU_RES_DYNAMIC(MENU_ID_DTCRIGHT, menu_dtc_current, MENU_ID_DTCLEFT, MENU_ID_DTCRIGHT, MENU_ID_VIEWDTC, NULL ); 66 67 static struct menu ui_menu = {MENU_ID_ROOT, { MENU_ENTRY(MENU_ID_ROOT), MENU_ENTRY(MENU_ID_CHECK_DTC), MENU_ENTRY(MENU_ID_FLASH_STATUS), 68 MENU_ENTRY(MENU_ID_NVM_ERASE), MENU_ENTRY(MENU_ID_DTCMIL), MENU_ENTRY(MENU_ID_VIEWDTC), 69 MENU_ENTRY(MENU_ID_CLEARDTC), MENU_ENTRY(MENU_ID_CLEAREDDTC), MENU_ENTRY(MENU_ID_BACK),MENU_ENTRY(MENU_ID_DTC), 70 MENU_ENTRY(MENU_ID_DTCLEFT), MENU_ENTRY(MENU_ID_DTCRIGHT) }}; 71 72 static volatile uint8_t buttons; 73 static uint8_t update = 1; 74 75 struct menu_entry *menu_get_entry( uint8_t id ); 76 77 void 78 menu(struct mode *mode, uint8_t init) 79 { 80 struct menu_entry * current; 81 struct static_menu_entry * static_current; 82 struct dynamic_menu_entry * dynamic_current; 83 char buf[64]; 84 85 current = menu_get_entry( ui_menu.current ); /*Get the current menu entry*/ 86 87 if( update || init ) 88 { 89 snprintf(buf, sizeof(buf), "Mode: %s", 90 mode_str[mode->mode]); 91 lcd_update(&lcd, 0, buf); 92 lcd_update(&lcd, 1, ""); 93 94 update = 0; 95 96 if( current->type == MENU_ENTRY_STATIC ) /*Static menus have a constant string*/ 97 { 98 static_current = (struct static_menu_entry*)current; 99 lcd_update(&lcd, 1, static_current->disp );

96

Page 97: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

100 } 101 else if( current->type == MENU_ENTRY_DYNAMIC ) /*Static menus have output strings generated by functions*/ 102 { 103 dynamic_current = (struct dynamic_menu_entry*)current; 104 dynamic_current->disp(); 105 } 106 } 107 108 if ( btn_down(BUTTON_LEFT) ) 109 { 110 update = 1; 111 menu_set_current( current->left ); 112 } 113 114 if ( btn_down(BUTTON_ENTER) ) 115 { 116 update = 1; 117 menu_set_current( current->enter ); 118 119 if( current->fp != NULL ) /*If there is a function to be executed on enter button, then run it*/ 120 current->fp(); 121 } 122 123 if ( btn_down(BUTTON_RIGHT) ) 124 { 125 update = 1; 126 menu_set_current( current->right ); 127 } 128 } 129 130 void 131 menu_dynamic_root(void) 132 { 133 lcd_update(&lcd, 1, "Root Menu" ); 134 _delay_ms( 300 ); 135 menu_set_current( MENU_ID_CHECK_DTC ); 136 update = 1; 137 } 138 139 void 140 menu_dynamic_dtcmil(void) 141 { 142 char buf[21]; 143 144 snprintf(buf, sizeof(buf), "%3d DTCs, MIL: %s", elm_dtc_count(), ((elm_dtc_mil())?"ON":"OFF") ); 145 lcd_update(&lcd, 1, buf ); 146 } 147 148 void 149 menu_dtc_update(void) 150 { 151 elm_init_retry( &myelm, PROT_ISO9141 ); 152 153 if( myelm.initd == 0 ) 154 { 155 menu_set_current( MENU_ID_ROOT ); 156 update = 1; 157 return; 158 }

97

Page 98: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

159 160 elm_dtc_update(); /*Update the DTC list*/ 161 } 162 163 void 164 menu_dtc_current(void) 165 { 166 char buf[15]; 167 char dtc_buf[6]; 168 169 if( elm_dtc_count() == 0 ) 170 { 171 lcd_update(&lcd, 1, "No DTCs Reported." ); 172 _delay_ms(300); 173 menu_set_current( MENU_ID_VIEWDTC ); 174 update = 1; 175 return; 176 } 177 178 if( ui_menu.current == MENU_ID_DTCLEFT ) 179 elm_dtc_prev(); 180 181 if( ui_menu.current == MENU_ID_DTCRIGHT ) 182 elm_dtc_next(); 183 184 DTC_CODE( dtc_buf, elm_dtc_current() ); 185 snprintf(buf,sizeof(buf),"Code: %s", dtc_buf); 186 187 lcd_update(&lcd, 1, buf ); 188 } 189 190 void 191 menu_dynamic_flash(void) 192 { 193 double mem_used; 194 char buf[32]; 195 196 mem_used = (cfg.nvm_start_addr * 100.0 / NVM_PAGE_SIZE) / 197 NVM_NUM_PAGES; 198 snprintf(buf, sizeof(buf), "\x7F Mem Used: %.2f%% \x7E", mem_used); 199 200 lcd_update(&lcd, 1, buf); 201 } 202 203 void 204 init_ui(void) 205 { 206 /* enable pull-up resistors */ 207 PORTA |= 0xF0; 208 PORTD |= 0x80; 209 210 _delay_us(30); 211 212 buttons |= (~(PIND & 0x80) >> 7) & LCD_MSK; 213 buttons |= (~(PINA & 0xF0) >> 3) & SW_MSK; 214 215 /* mask off pins of interest for b3..1 */ 216 PCMSK0 = 0xF0; 217 218 /* enable PCINT0 for b3..1 */ 219 PCICR |= 0x01; 220

98

Page 99: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

221 /* mask off pins of interest for lcd_present */ 222 PCMSK3 = 0x80; 223 224 /* enable PCINT3 for lcd_present */ 225 PCICR |= 0x08; 226 227 /*Build the LCD Menu*/ 228 //menu_set_current(MENU_ID_ROOT); //Set as default in the global declaration 229 230 } 231 232 uint8_t 233 btn_down(uint8_t btn) 234 { 235 uint8_t r; 236 237 r = 0; 238 239 _delay_ms(DEBOUNCE_DELAY); 240 if (btn == BTN_LCDPRESENT) { 241 if (PIND & 0x80) 242 buttons &= ~BTN_LCDPRESENT; 243 } else if (PINA & 0xF0) 244 buttons &= ~((PINA >> 3) & SW_MSK); 245 246 if (buttons & btn) 247 r = 1; 248 249 return r; 250 } 251 252 void 253 lcd_update(struct lcd *lcd, uint8_t line, char *str) 254 { 255 int i; 256 char *p; 257 258 if (line == 0) { 259 p = lcd->line0; 260 } else { 261 p = lcd->line1; 262 } 263 264 strlcpy(p, str, sizeof(lcd->line0)); 265 266 for (i = strlen(p); i < sizeof(lcd->line0) - 1; ++i) 267 p[i] = ' '; 268 269 p[i] = '\0'; 270 271 lcd_home(0); 272 lcd_print(lcd->line0); 273 lcd_home(1); 274 lcd_print(lcd->line1); 275 } 276 277 void menu_set_current( uint8_t id ) 278 { 279 ui_menu.current = id; 280 } 281 282 struct menu_entry *

99

Page 100: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

283 menu_get_entry( uint8_t id ) 284 { 285 int i; 286 287 for(i=0;i<MENU_ENTRY_COUNT;++i) 288 { 289 if( ui_menu.menu_entries[i]->id == id ) 290 break; 291 } 292 293 return ui_menu.menu_entries[i]; 294 } 295 296 void 297 menu_repaint(void) 298 { 299 update = 1; 300 } 301 302 ISR(PCINT0_vect) 303 { 304 /* set button bits (cleared in btn_down) */ 305 buttons |= (~(PINA & 0xF0) >> 3) & SW_MSK; 306 } 307 308 ISR(PCINT3_vect) 309 { 310 /* set button bits (cleared in btn_down) */ 311 buttons |= (~(PIND & 0x80) >> 7) & LCD_MSK; 312 }

<<<<< END software/ui.c <<<<<

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

Page 101: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

29 #include "usart.h" 30 #include "util.h" 31 #include "lcd.h" 32 33 /* devices */ 34 #define XBEE 0 35 #define ELM 1 36 37 /* PB0 is cts, PB1 is rts (both low true) */ 38 #define usart0_cts_set(void) (PORTB &= 0xFE) 39 #define usart0_cts_clr(void) (PORTB |= 0x01) 40 #define usart0_rts(void) ((PINB & 0x02) ? 0 : 1) 41 42 static volatile struct usart_rxbuf rx_buf[2]; 43 static volatile struct usart_txbuf tx_buf[2]; 44 45 void 46 usart_init(void) 47 { 48 memset(&rx_buf, 0, sizeof(rx_buf)); 49 memset(&tx_buf, 0, sizeof(tx_buf)); 50 51 rx_buf[XBEE].echo = 1; 52 53 /* USART0 (XBEE) */ 54 /* 57.6 kbps */ 55 UBRR0H = 0x00; 56 UBRR0L = 0x10; 57 UCSR0A = 0x02; 58 /* rx and tx enable, rx interrupt */ 59 UCSR0B = 0x98; 60 /* asynchronous, 8 bit, no parity */ 61 UCSR0C = 0x06; 62 63 /* USART1 (ELM) */ 64 /* 38.4 kbps */ 65 UBRR1H = 0x00; 66 UBRR1L = 0x0C; 67 UCSR1A = 0x00; 68 /* rx and tx enable, rx interrupt */ 69 UCSR1B = 0x98; 70 /* asynchronous, 8 bit, no parity */ 71 UCSR1C = 0x06; 72 73 /* set pin PB0 (cts) to an output */ 74 DDRB |= 0x01; 75 76 /* enable PCINT 9 (PB1) for rts */ 77 PCMSK1 = 0x02; 78 PCICR = 0x02; 79 } 80 81 #define rx_int_disable(dev) if (dev == 0) UCSR0B &= 0x7F; else UCSR1B &= 0x7F; 82 #define rx_int_enable(dev) if (dev == 0) UCSR0B |= 0x80; else UCSR1B |= 0x80; 83 84 uint16_t 85 usart_rx(uint8_t dev, uint8_t *dst, uint16_t size) 86 { 87 uint16_t r; 88 89 r = buf_rem(dev, dst, size); 90

101

Page 102: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

91 return r; 92 } 93 94 #define udre_int_disable(dev) if (dev == 0) UCSR0B &= 0xDF; else UCSR1B &= 0xDF; 95 #define udre_int_enable(dev) if (dev == 0) UCSR0B |= 0x20; else UCSR1B |= 0x20; 96 97 uint16_t 98 usart_tx(uint8_t dev, uint8_t *src, uint16_t len) 99 { 100 uint16_t appended; 101 102 appended = buf_cat(dev, src, len); 103 104 return appended; 105 } 106 107 uint16_t 108 buf_cat(uint8_t dev, uint8_t *src, uint16_t len) 109 { 110 uint16_t appended; 111 uint16_t chunk_size; 112 volatile struct usart_txbuf *txb = &tx_buf[dev]; 113 114 if (userland_wait()) 115 return 0; 116 117 appended = 0; 118 119 txb->inuse = 1; 120 121 /* 122 * Pause TX and RX while modifying buffer. 123 * RX int disabled because RX int can modify the TX buffer 124 * (echo) and also restart udre 125 */ 126 rx_int_disable(dev); 127 udre_int_disable(dev); 128 129 /* adjust len to what we can actually append */ 130 if (len > USART_TXBUFSIZE - txb->used) 131 len = USART_TXBUFSIZE - txb->used; 132 133 while (len != 0) { 134 if (txb->tail >= txb->head) 135 chunk_size = USART_TXBUFSIZE - txb->tail; 136 else 137 chunk_size = txb->head - txb->tail + 1; 138 139 if (chunk_size > len) 140 chunk_size = len; 141 142 memcpy((struct usart_txbuf *) &txb->buf[txb->tail], 143 src, chunk_size); 144 145 src += chunk_size; 146 len -= chunk_size; 147 txb->tail += chunk_size; 148 appended += chunk_size; 149 txb->used += chunk_size; 150 151 if (txb->tail >= USART_TXBUFSIZE) 152 txb->tail = 0;

102

Page 103: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

153 } 154 155 txb->inuse = 0; 156 157 udre_int_enable(dev); 158 rx_int_enable(dev); 159 160 return appended; 161 } 162 163 int 164 usart_getc(uint8_t dev) 165 { 166 uint8_t c; 167 uint16_t r; 168 169 do { 170 r = usart_rx(dev, &c, sizeof(c)); 171 } while (r != sizeof(c)); 172 173 return c; 174 } 175 176 void 177 usart_putc(uint8_t dev, int c) 178 { 179 uint16_t r; 180 uint8_t d; 181 182 d = c; 183 do { 184 r = usart_tx(dev, &d, sizeof(d)); 185 } while (r != sizeof(d)); 186 } 187 188 void 189 usart_put(uint8_t dev, const void *vbuf, uint16_t len) 190 { 191 uint16_t sent; 192 uint16_t r; 193 uint8_t *buf; 194 195 buf = (uint8_t *) vbuf; 196 197 sent = 0; 198 while (sent < len) { 199 r = usart_tx(dev, (uint8_t *) buf, len - sent); 200 buf += r; 201 sent += r; 202 } 203 } 204 205 void 206 usart_puts(uint8_t dev, const char *str) 207 { 208 usart_put(dev, (uint8_t *) str, strlen(str)); 209 } 210 211 uint16_t 212 usart_rxused(uint8_t dev) 213 { 214 volatile struct usart_rxbuf *rxb = &rx_buf[dev];

103

Page 104: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

215 uint16_t used; 216 217 cli(); 218 used = rxb->used; 219 sei(); 220 221 return used; 222 } 223 224 int8_t 225 usart_peek(uint8_t dev, char c) 226 { 227 volatile struct usart_rxbuf *rxb = &rx_buf[dev]; 228 int i, j; 229 int found = 0; 230 231 cli(); 232 233 i = rxb->head; 234 for (j = rxb->used; j > 0; --j) { 235 if (rxb->buf[i] == c) { 236 found = 1; 237 break; 238 } 239 240 ++i; 241 if (i >= USART_RXBUFSIZE) 242 i = 0; 243 } 244 245 if (!found && rxb->used >= USART_CTS_MAX) 246 found = -1; 247 248 sei(); 249 250 return found; 251 } 252 253 void 254 usart_flush(uint8_t dev) 255 { 256 volatile struct usart_rxbuf *rxb = &rx_buf[dev]; 257 258 cli(); 259 260 rxb->used = rxb->head = rxb->tail = 0; 261 if (dev == 0) 262 usart0_cts_set(); 263 264 sei(); 265 } 266 267 uint16_t 268 buf_rem(uint8_t dev, uint8_t *dst, uint16_t len) 269 { 270 uint16_t removed; 271 volatile struct usart_rxbuf *rxb = &rx_buf[dev]; 272 273 if (userland_wait()) 274 return 0; 275 276 removed = 0;

104

Page 105: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

277 278 rx_int_disable(dev); 279 280 if (rxb->used < len) 281 len = rxb->used; 282 283 rx_int_enable(dev); 284 285 removed = len; 286 287 while (len > 0) { 288 *dst = rxb->buf[rxb->head]; 289 290 ++rxb->head; 291 --len; 292 ++dst; 293 294 if (rxb->head >= USART_RXBUFSIZE) 295 rxb->head = 0; 296 } 297 298 rx_int_disable(dev); 299 300 rxb->used -= removed; 301 302 rx_int_enable(dev); 303 304 if (dev == 0) { 305 if (rx_buf[XBEE].used <= USART_CTS_MIN) 306 usart0_cts_set(); 307 } 308 309 return removed; 310 } 311 312 ISR(USART0_RX_vect) 313 { 314 volatile struct usart_rxbuf *rxb = &rx_buf[XBEE]; 315 volatile struct usart_txbuf *txb = &tx_buf[XBEE]; 316 317 if (UCSR0A & 0x08) 318 fatal("rx0 overflow"); 319 320 if (rxb->used == USART_RXBUFSIZE) 321 fatal("RX0 buffer full!"); 322 323 #if 1 324 if (rxb->used >= USART_CTS_MAX) 325 usart0_cts_clr(); 326 #endif 327 328 rxb->buf[rxb->tail] = UDR0; 329 330 #if 1 331 if (rxb->echo) { 332 if (txb->used + 1 < USART_TXBUFSIZE) { 333 uint8_t c; 334 335 c = txb->buf[txb->tail] = rxb->buf[rxb->tail]; 336 ++txb->tail; 337 ++txb->used; 338

105

Page 106: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

339 if (txb->tail >= USART_TXBUFSIZE) 340 txb->tail = 0; 341 342 if (c == '\r') { 343 txb->buf[txb->tail] = '\n'; 344 ++txb->tail; 345 ++txb->used; 346 347 if (txb->tail >= USART_TXBUFSIZE) 348 txb->tail = 0; 349 } 350 } 351 /* this interrupt cannot run if we are in userland tx routines */ 352 udre_int_enable(XBEE); 353 } 354 #endif 355 356 ++rxb->used; 357 ++rxb->tail; 358 359 if (rxb->tail >= USART_RXBUFSIZE) 360 rxb->tail = 0; 361 362 return; 363 } 364 365 /* PB1 / PCINT9 pin change interrupt (for rts) */ 366 ISR(PCINT1_vect) 367 { 368 /* remote buffer ready, and we have something to send */ 369 if (usart0_rts() && tx_buf[XBEE].used > 0) { 370 if (tx_buf[XBEE].inuse == 0) { 371 udre_int_enable(XBEE); 372 } 373 } 374 } 375 376 ISR(USART0_UDRE_vect) 377 { 378 volatile struct usart_txbuf *txb = &tx_buf[XBEE]; 379 380 /* 381 * Remote buffer full or nothing more to send, pause 382 * transmission. 383 */ 384 if (!usart0_rts() || txb->used == 0) { 385 udre_int_disable(XBEE); 386 return; 387 } 388 389 UDR0 = txb->buf[txb->head]; 390 391 --txb->used; 392 ++txb->head; 393 394 if (txb->head >= USART_TXBUFSIZE) 395 txb->head = 0; 396 397 return; 398 } 399 400 ISR(USART1_RX_vect)

106

Page 107: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

401 { 402 volatile struct usart_rxbuf *rxb = &rx_buf[ELM]; 403 404 if (UCSR1A & 0x08) 405 fatal("rx1 overflow"); 406 407 if (rxb->used == USART_RXBUFSIZE) 408 fatal("RX1 buffer full!"); 409 410 rxb->buf[rxb->tail] = UDR1; 411 412 ++rxb->used; 413 ++rxb->tail; 414 415 if (rxb->tail >= USART_RXBUFSIZE) 416 rxb->tail = 0; 417 418 return; 419 } 420 421 ISR(USART1_UDRE_vect) 422 { 423 volatile struct usart_txbuf *txb = &tx_buf[ELM]; 424 425 /* nothing more to send, disable this interrupt */ 426 if (txb->used == 0) { 427 udre_int_disable(ELM); 428 return; 429 } 430 431 UDR1 = txb->buf[txb->head]; 432 433 --txb->used; 434 ++txb->head; 435 436 if (txb->head >= USART_TXBUFSIZE) 437 txb->head = 0; 438 439 return; 440 } 441 442 void 443 usart_echo(uint8_t dev, uint8_t setting) 444 { 445 volatile struct usart_rxbuf *rxb = &rx_buf[dev]; 446 447 rxb->echo = setting; 448 } 449 450 uint8_t 451 userland_wait(void) 452 { 453 return 0; 454 }

<<<<< END software/usart.c <<<<<

>>>>> BEG software/util.c >>>>> 1 /* 2 * Copyright (c) 2009 Nathaniel Houghton <[email protected]> 3 * Robert Kirchgessner <[email protected]> 4 *

107

Page 108: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 109: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 110: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 111: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 112: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 113: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 114: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

117((A>>4)&0x3), \

118((A>>0)&0xF), \

119((A>>12)&0xF),\

120((A>>8) &0xF) )

121 122 /*Mode 5 PIDs 123 Oxygen sensor data. */ 124 125 /*Mode 9 PIDs*/ 126 #define VIN 0x02 //Vehicle VIN number. Returns 5 lines. 127 128 /*Defines for conversion.*/ 129 #define CONV_ENGINE_LOAD(A)(A*100.0/255.0) //Converted engine load value (%) 130 #define CONV_ENGINE_TEMP(A)(A-40.0) //Converted engine coolant temp. (deg C) 131 #define CONV_SHORT_TERM_FUEL_TRIM_B1(A) (A-128.0)*(100.0/128.0) 132 #define CONV_LONG_TERM_FUEL_TRIM_B1(A) (A-128.0)*(100.0/128.0) 133 #define CONV_SHORT_TERM_FUEL_TRIM_B2(A) (A-128.0)*(100.0/128.0) 134 #define CONV_LONG_TERM_FUEL_TRIM_B2(A) (A-128.0)*(100.0/128.0) 135 #define CONV_PRESSURE_FUEL(A) (A*3.0) //Fuel pressure (kPa -- gauged.) 136 #define CONV_PRESSURE_MANIFOLD(A) (A) //Intake manifold pressure (kPa -- absolute.) 137 #define CONV_ENGINE_RPM(A) (A/4.0) //Engine RPMs. 2 Bytes of data. A = B0<<8 + B1 138 #define CONV_VEHICLE_SPEED(A) (A) //Vehicle speed (absolute.) 139 #define CONV_TIMING_ADVANCE(A) (A/2.0) - 64.0 140 #define CONV_INTAKE_TEMP(A)(A-40.0) //Intake air temperature (deg C) 141 #define CONV_MAF_AIRFLOW(A)(A/100.0) //MAF Air flow rate. Gallons/second. 142 #define CONV_THROTTLE_POS(A) (A*(100.0/255.0)) //Throttle position (%) 143 #define CONV_ENGINE_RUNTIME(A) (A) //Two bytes of data. B0<<8 + B1. Seconds. 144 #define CONV_DISTANCE_WMIL(A) (A) //Distance travelled with MIL (km) 145 #define CONV_PRESS_FUELRAIL(A) (A*0.079) //Fuel rail pressure in kPa 146 #define CONV_PRESS_FUELDIESEL(A) (A*10) //Diesel fuel rail pressure. Two bytes of data. kPa (gauge) 147 #define CONV_FUEL_LEVEL(A) (A*(100.0/255.0)) //Fuel level input. % 148 #define CONV_FUEL_ETH_PER(A) (A*(100.0/255.0)) //Ethanol fuel % 149 150 /*PID Lengths*/ 151 #define LEN_M0100 4 152 #define LEN_M0101 4 153 #define LEN_M0102 8 154 #define LEN_M0103 2 155 #define LEN_M0104 1 156 #define LEN_M0105 1 157 #define LEN_M0106 1 158 #define LEN_M0107 1 159 #define LEN_M0108 1 160 #define LEN_M0109 1 161 #define LEN_M010A 1 162 #define LEN_M010B 1 163 #define LEN_M010C 2 164 #define LEN_M010D 1 165 #define LEN_M010E 1 166 #define LEN_M010F 1 167 #define LEN_M0110 2 168 #define LEN_M0111 1 169 #define LEN_M0112 1 170 #define LEN_M0113 1 171 #define LEN_M0114 2 172 #define LEN_M0115 2

114

Page 115: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

173 #define LEN_M0116 2 174 #define LEN_M0117 2 175 #define LEN_M0118 2 176 #define LEN_M0119 2 177 #define LEN_M011A 2 178 #define LEN_M011B 2 179 #define LEN_M011C 1 180 #define LEN_M011D 1 181 #define LEN_M011E 1 182 #define LEN_M011F 2 183 #define LEN_M0120 4 184 #define LEN_M0121 2 185 #define LEN_M0122 2 186 #define LEN_M0123 2 187 #define LEN_M0124 4 188 #define LEN_M0125 4 189 #define LEN_M0126 4 190 #define LEN_M0127 4 191 #define LEN_M0128 4 192 #define LEN_M0129 4 193 #define LEN_M012A 4 194 #define LEN_M012B 4 195 #define LEN_M012C 1 196 #define LEN_M012D 1 197 #define LEN_M012E 1 198 #define LEN_M012F 1 199 #define LEN_M0130 1 200 #define LEN_M0131 2 201 #define LEN_M0132 2 202 #define LEN_M0133 1 203 #define LEN_M0134 4 204 #define LEN_M0135 4 205 #define LEN_M0136 4 206 #define LEN_M0137 4 207 #define LEN_M0138 4 208 #define LEN_M0139 4 209 #define LEN_M013A 4 210 #define LEN_M013B 4 211 #define LEN_M013C 2 212 #define LEN_M013D 2 213 #define LEN_M013E 2 214 #define LEN_M013F 2 215 #define LEN_M0140 4 216 #define LEN_M0141 4 217 #define LEN_M0142 2 218 #define LEN_M0143 2 219 #define LEN_M0144 2 220 #define LEN_M0145 1 221 #define LEN_M0146 1 222 #define LEN_M0147 1 223 #define LEN_M0148 1 224 #define LEN_M0149 1 225 #define LEN_M014A 1 226 #define LEN_M014B 1 227 #define LEN_M014C 1 228 #define LEN_M014D 2 229 #define LEN_M014E 2 230 #define LEN_M014F 0 231 #define LEN_M0150 0 232 #define LEN_M0151 1 233 #define LEN_M0152 1

115

Page 116: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

234 235 #endif

<<<<< END pc/obd_pid.h <<<<<

>>>>> BEG pc/progress.h >>>>> 1 #ifndef _PROGRESS_H_ 2 #define _PROGRESS_H_ 3 4 void progress_bar(uint32_t, uint32_t); 5 6 #endif

<<<<< END pc/progress.h <<<<<

>>>>> BEG pc/serial.h >>>>> 1 #ifndef SERIAL_HEADER 2 #define SERIAL_HEADER 3 4 struct serial_dev { 5 HANDLE hSerial; 6 }; 7 8 int init_serial(struct serial_dev *, char *); 9 void close_serial(struct serial_dev *); 10 size_t write_ser(struct serial_dev *, void *, size_t); 11 size_t read_ser(struct serial_dev *, void *, size_t); 12 void ser_flush(struct serial_dev *); 13 14 #endif

<<<<< END pc/serial.h <<<<<

>>>>> BEG pc/util.h >>>>> 1 #ifndef UTIL_HEADER 2 #define UTIL_HEADER 3 4 #include <stdint.h> 5 6 #include "config.h" 7 8 int valid_config(struct config *); 9 uint16_t crc16_update(uint16_t, uint8_t); 10 void dump_config(struct config *); 11 int valid_crc(void *, int); 12 char *pid_units(uint8_t); 13 double conv_pid_data(uint8_t, uint32_t); 14 15 #endif

<<<<< END pc/util.h <<<<<

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

Page 117: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 118: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 119: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 120: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 121: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

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 #define PROTCAN(A) (A>PROT_ISO14230KWP1) 39 40 #endif

<<<<< END software/elm_prot.h <<<<<

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

Page 122: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

40 uint8_t pid; 41 } __attribute__((__packed__)); 42 43 struct timer { 44 uint8_t ding; 45 uint8_t overflow; 46 }; 47 48 void timer_start(void); 49 void timer_stop(void); 50 int8_t event_init(struct event *); 51 int8_t event_add(struct event *); 52 void events_start(uint32_t); 53 uint8_t events_end(uint32_t *); 54 void events(struct mode *); 55 void elm_event(struct event *, struct mode *); 56 void handle_event(struct event *, struct mode *); 57 58 #endif

<<<<< END software/event.h <<<<<

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

Page 123: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

40 void lcd_on(uint8_t, uint8_t); 41 void lcd_home(uint8_t); 42 43 void lcd_print(char *); 44 void lcd_clrscr(void); 45 46 #endif

<<<<< END software/lcd.h <<<<<

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

Page 124: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

52 53 void elm_mode(struct mode *); 54 void config_mode(struct mode *); 55 void log_mode(struct mode *); 56 57 #endif

<<<<< END software/mode.h <<<<<

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

<<<<< END software/nvm.h <<<<<

>>>>> BEG software/obd_pid.h >>>>> 1 /* 2 * Copyright (c) 2009 Nathaniel Houghton <[email protected]> 3 * Robert Kirchgessner <[email protected]> 4 *

124

Page 125: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 126: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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",

( (((A>>6)&0x3)==0)?'P':( (((A>>6)&0x3)==1)?'C':( (((A>>6)&0x3)==2)?'B':( (((A>>6)&0x3)==3)?'U':'?') ) ) ), \ 115

((A>>4)&0x3), \ 116

((A>>0)&0xF), \ 117

((A>>12)&0xF),\ 118

((A>>8) &0xF) )

126

Page 127: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

119 120 /*Mode 5 PIDs 121 Oxygen sensor data. */ 122 123 /*Mode 9 PIDs*/ 124 #define VIN 0x02 //Vehicle VIN number. Returns 5 lines. 125 126 /*Defines for conversion.*/ 127 #define CONV_PIDM0104(A) (A*100.0/255.0) //Converted engine load value (%) 128 #define CONV_PIDM0105(A) (A-40.0) //Converted engine coolant temp. (deg C) 129 #define CONV_PIDM0106(A) (A-128.0)*(100.0/128.0) 130 #define CONV_PIDM0107(A) (A-128.0)*(100.0/128.0) 131 #define CONV_PIDM0108(A) (A-128.0)*(100.0/128.0) 132 #define CONV_PIDM0109(A) (A-128.0)*(100.0/128.0) 133 #define CONV_PIDM010A(A) (A*3.0) //Fuel pressure (kPa -- gauged.) 134 #define CONV_PIDM010B(A) (A) //Intake manifold pressure (kPa -- absolute.) 135 #define CONV_PIDM010C(A) (A/4.0) //Engine RPMs. 2 Bytes of data. A = B0<<8 + B1 136 #define CONV_PIDM010D(A) (A) //Vehicle speed (absolute.) 137 #define CONV_PIDM010E(A) (A/2.0) - 64.0 138 #define CONV_PIDM010F(A) (A-40.0) //Intake air temperature (deg C) 139 #define CONV_PIDM0110(A) (A/100.0) //MAF Air flow rate. Gallons/second. 140 #define CONV_PIDM0111(A) (A*(100.0/255.0)) //Throttle position (%) 141 #define CONV_PIDM011F(A) (A) //Two bytes of data. B0<<8 + B1. Seconds. 142 #define CONV_PIDM0121(A) (A) //Distance travelled with MIL (km) 143 #define CONV_PIDM0122(A) (A*0.079) //Fuel rail pressure in kPa 144 #define CONV_PIDM0123(A) (A*10) //Diesel fuel rail pressure. Two bytes of data. kPa (gauge) 145 #define CONV_PIDM012F(A) (A*(100.0/255.0)) //Fuel level input. % 146 #define CONV_PIDM0130(A) (A) //Number of warm-ups since DTC cleared. 147 #define CONV_PIDM0131(A) (A) //Distance travelled with codes cleared. (km) 148 #define CONV_PIDM0151(A) (A) //Table lookup value. 149 #define CONV_PIDM0152(A) (A*(100.0/255.0)) //Ethanol fuel % 150 151 /*PID Lengths*/ 152 #define LEN_M0100 4 153 #define LEN_M0101 4 154 #define LEN_M0102 8 155 #define LEN_M0103 2 156 #define LEN_M0104 1 157 #define LEN_M0105 1 158 #define LEN_M0106 1 159 #define LEN_M0107 1 160 #define LEN_M0108 1 161 #define LEN_M0109 1 162 #define LEN_M010A 1 163 #define LEN_M010B 1 164 #define LEN_M010C 2 165 #define LEN_M010D 1 166 #define LEN_M010E 1 167 #define LEN_M010F 1 168 #define LEN_M0110 2 169 #define LEN_M0111 1 170 #define LEN_M0112 1 171 #define LEN_M0113 1 172 #define LEN_M0114 2 173 #define LEN_M0115 2 174 #define LEN_M0116 2 175 #define LEN_M0117 2 176 #define LEN_M0118 2 177 #define LEN_M0119 2 178 #define LEN_M011A 2 179 #define LEN_M011B 2

127

Page 128: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

180 #define LEN_M011C 1 181 #define LEN_M011D 1 182 #define LEN_M011E 1 183 #define LEN_M011F 2 184 #define LEN_M0120 4 185 #define LEN_M0121 2 186 #define LEN_M0122 2 187 #define LEN_M0123 2 188 #define LEN_M0124 4 189 #define LEN_M0125 4 190 #define LEN_M0126 4 191 #define LEN_M0127 4 192 #define LEN_M0128 4 193 #define LEN_M0129 4 194 #define LEN_M012A 4 195 #define LEN_M012B 4 196 #define LEN_M012C 1 197 #define LEN_M012D 1 198 #define LEN_M012E 1 199 #define LEN_M012F 1 200 #define LEN_M0130 1 201 #define LEN_M0131 2 202 #define LEN_M0132 2 203 #define LEN_M0133 1 204 #define LEN_M0134 4 205 #define LEN_M0135 4 206 #define LEN_M0136 4 207 #define LEN_M0137 4 208 #define LEN_M0138 4 209 #define LEN_M0139 4 210 #define LEN_M013A 4 211 #define LEN_M013B 4 212 #define LEN_M013C 2 213 #define LEN_M013D 2 214 #define LEN_M013E 2 215 #define LEN_M013F 2 216 #define LEN_M0140 4 217 #define LEN_M0141 4 218 #define LEN_M0142 2 219 #define LEN_M0143 2 220 #define LEN_M0144 2 221 #define LEN_M0145 1 222 #define LEN_M0146 1 223 #define LEN_M0147 1 224 #define LEN_M0148 1 225 #define LEN_M0149 1 226 #define LEN_M014A 1 227 #define LEN_M014B 1 228 #define LEN_M014C 1 229 #define LEN_M014D 2 230 #define LEN_M014E 2 231 #define LEN_M014F 0 232 #define LEN_M0150 0 233 #define LEN_M0151 1 234 #define LEN_M0152 1

235 236 #endif

<<<<< END software/obd_pid.h <<<<<

>>>>> BEG software/spi.h >>>>>

128

Page 129: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 130: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 131: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 132: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 133: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 134: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

35 OleInitialize(NULL); 36 37 hDlg = CreateDialogParam(hInstance, 38 MAKEINTRESOURCE(IDD_DIALOG1), NULL, callback, (LPARAM) 39 hInstance); 40 if (hDlg <= 0) { 41 MessageBox(NULL, "Error creating dialog box.", "Error", 42 MB_OK | MB_ICONERROR); 43 exit(1); 44 } 45 ShowWindow(hDlg, SW_SHOWNORMAL); 46 UpdateWindow(hDlg); 47 48 while (GetMessage(&msg, NULL, 0, 0) > 0) { 49 TranslateMessage(&msg); 50 DispatchMessage(&msg); 51 } 52 53 OleUninitialize(); 54 55 return msg.wParam; 56 } 57 58 BOOL CALLBACK 59 callback(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 60 { 61 char buf[64]; 62 int com_num; 63 static struct serial_dev sdev; 64 static struct disp_info di; 65 int i; 66 67 switch(uMsg) { 68 case WM_INITDIALOG: 69 // lParam is hInstance 70 { 71 HICON hicon; 72 hicon = LoadIcon((HINSTANCE) lParam, 73 MAKEINTRESOURCE(IDI_ICON1)); 74 if (hicon == NULL) { 75 MessageBox(hwnd, "Could not set icon.", 76 "Error", MB_OK | MB_ICONERROR); 77 DestroyWindow(hwnd); 78 } 79 SendMessage(hwnd, WM_SETICON, ICON_SMALL, 80 (LPARAM) hicon); 81 SendMessage(hwnd, WM_SETICON, ICON_BIG, 82 (LPARAM) hicon); 83 } 84 status_msg(hwnd, START_MSG); 85 for (i = 0; i < NUM_LOG_SLOTS; ++i) 86 di.pid[i] = -1; 87 di.num_pids = 0; 88 break; 89 90 case WM_COMMAND: 91 if (HIWORD(wParam) == BN_CLICKED) { 92 switch(LOWORD(wParam)) { 93 case IDC_SETLIVE: 94 SendDlgItemMessage(hwnd, IDC_EDIT_COM, 95 WM_GETTEXT, sizeof(buf), (LPARAM) 96 buf);

134

Page 135: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

97 com_num = atoi(buf); 98 snprintf(buf, sizeof(buf), "Connecting" 99 " to COM%d...", com_num); 100 status_msg(hwnd, buf); 101 snprintf(buf, sizeof(buf), "\\\\.\\COM%d", 102 com_num); 103 if (init_serial(&sdev, buf) == -1) { 104 status_msg(hwnd, "Error " 105 "initializing " 106 "serial port."); 107 Sleep(1000); 108 status_msg(hwnd, START_MSG); 109 return TRUE; 110 } 111 112 if (set_livemode(hwnd, &sdev) == -1) { 113 Sleep(1000); 114 status_msg(hwnd, START_MSG); 115 return TRUE; 116 } 117 118 SetTimer(hwnd, IDT_TIMER1, DELAY_MS, 0); 119 break; 120 } 121 } 122 break; 123 124 case WM_CLOSE: 125 DestroyWindow(hwnd); 126 break; 127 128 case WM_DESTROY: 129 PostQuitMessage(0); 130 break; 131 132 case WM_TIMER: 133 switch (wParam) { 134 case IDT_TIMER1: 135 update_display(hwnd, &sdev, &di); 136 break; 137 } 138 break; 139 140 default: 141 return FALSE; 142 } 143 144 return TRUE; 145 } 146 147 int 148 update_display(HWND hwnd, struct serial_dev *sdev, struct disp_info *di) 149 { 150 static char rxbuf[256]; 151 static int buf_used = 0; 152 char buf[64]; 153 int r; 154 char *p, *q; 155 int pid, val; 156 157 printf("buf used: %d\n", buf_used); 158

135

Page 136: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

159 if (buf_used < sizeof(rxbuf) - 1) { 160 r = read_ser(sdev, &rxbuf[buf_used], sizeof(rxbuf) - buf_used - 1); 161 buf_used += r; 162 rxbuf[buf_used] = '\0'; 163 } 164 165 for (;;) { 166 p = strchr(rxbuf, '\n'); 167 if (p == NULL) 168 return 0; 169 170 buf_used -= (p - rxbuf + 1); 171 172 *p = '\0'; 173 174 q = strchr(rxbuf, '='); 175 if (q != NULL) { 176 int i; 177 int found = 0; 178 int desc_edit_id; 179 int val_edit_id; 180 int radio_id; 181 182 pid = atoi(rxbuf); 183 val = atoi(q + 1); 184 printf("val is: %d, pid %d\n", val, pid); 185 for (i = 0; i < di->num_pids; ++i) { 186 if (di->pid[i] == pid) { 187 found = 1; 188 break; 189 } 190 } 191 192 if (!found) { 193 di->pid[i] = pid; 194 ++di->num_pids; 195 } 196 197 switch (i) { 198 case 0: 199 desc_edit_id = IDC_EDIT_DESC1; 200 val_edit_id = IDC_EDIT_VAL1; 201 radio_id = IDC_RADIO1; 202 break; 203 case 1: 204 desc_edit_id = IDC_EDIT_DESC2; 205 val_edit_id = IDC_EDIT_VAL2; 206 radio_id = IDC_RADIO2; 207 break; 208 case 2: 209 desc_edit_id = IDC_EDIT_DESC3; 210 val_edit_id = IDC_EDIT_VAL3; 211 radio_id = IDC_RADIO3; 212 break; 213 case 3: 214 desc_edit_id = IDC_EDIT_DESC4; 215 val_edit_id = IDC_EDIT_VAL4; 216 radio_id = IDC_RADIO4; 217 break; 218 case 4: 219 desc_edit_id = IDC_EDIT_DESC5; 220 val_edit_id = IDC_EDIT_VAL5;

136

Page 137: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

221 radio_id = IDC_RADIO5; 222 break; 223 case 5: 224 desc_edit_id = IDC_EDIT_DESC6; 225 val_edit_id = IDC_EDIT_VAL6; 226 radio_id = IDC_RADIO6; 227 break; 228 case 6: 229 desc_edit_id = IDC_EDIT_DESC7; 230 val_edit_id = IDC_EDIT_VAL7; 231 radio_id = IDC_RADIO7; 232 break; 233 case 7: 234 desc_edit_id = IDC_EDIT_DESC8; 235 val_edit_id = IDC_EDIT_VAL8; 236 radio_id = IDC_RADIO8; 237 break; 238 case 8: 239 desc_edit_id = IDC_EDIT_DESC9; 240 val_edit_id = IDC_EDIT_VAL9; 241 radio_id = IDC_RADIO9; 242 break; 243 } 244 245 snprintf(buf, sizeof(buf), "%s " 246 "(0x%02x)", pid_name[di->pid[i]], 247 di->pid[i]); 248 SendDlgItemMessage(hwnd, desc_edit_id, 249 WM_SETTEXT, 0, (LPARAM) buf); 250 printf("pid: %d, val: %d\n", di->pid[i], val); 251 snprintf(buf, sizeof(buf), "%.2f %s", 252 conv_pid_data(di->pid[i], val), 253 pid_units(di->pid[i])); 254 SendDlgItemMessage(hwnd, val_edit_id, 255 WM_SETTEXT, 0, (LPARAM) buf); 256 257 r = SendDlgItemMessage(hwnd, radio_id, BM_GETSTATE, 0, 0); 258 if (r & BST_CHECKED) 259 r = BST_UNCHECKED; 260 else 261 r = BST_CHECKED; 262 263 SendDlgItemMessage(hwnd, radio_id, BM_SETCHECK, r, 0); 264 } 265 266 memcpy(rxbuf, p + 1, buf_used); 267 } 268 } 269 270 void 271 status_msg(HWND hwnd, char *msg) 272 { 273 SendDlgItemMessage(hwnd, IDC_STATUS, WM_SETTEXT, 0, (LPARAM) 274 msg); 275 } 276 277 int 278 set_livemode(HWND hwnd, struct serial_dev *sdev) 279 { 280 char buf[128]; 281 int r; 282

137

Page 138: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

283 strcpy(buf, "!!!\recho off\r"); 284 r = write_ser(sdev, buf, strlen(buf)); 285 if (r != strlen(buf)) { 286 status_msg(hwnd, "Error writing to serial."); 287 return -1; 288 } 289 290 ser_flush(sdev); 291 292 strcpy(buf, "set_livemode\r"); 293 r = write_ser(sdev, buf, strlen(buf)); 294 if (r != strlen(buf)) { 295 status_msg(hwnd, "Error writing to serial."); 296 return -1; 297 } 298 299 r = read_ser(sdev, buf, sizeof(buf)); 300 buf[r] = '\0'; 301 if (r == 0) { 302 status_msg(hwnd, "Error reading from serial."); 303 return -1; 304 } 305 306 if (!strncmp(buf, "OK", 2)) { 307 status_msg(hwnd, "Set live mode, waiting for data..."); 308 } else { 309 status_msg(hwnd, "Could not set live mode."); 310 return -1; 311 } 312 313 return 0; 314 }

<<<<< END pc/livestats/wemslivestats.c <<<<<

>>>>> BEG pc/wemsconfiggui/wemsconfiggui.c >>>>> 1 #include <stdint.h> 2 #include <string.h> 3 #include <stdio.h> 4 #include <windows.h> 5 #include <commctrl.h> 6 #include <math.h> 7 8 #include "../util.h" 9 #include "../obd_pid.h" 10 #include "../config.h" 11 #include "../event.h" 12 #include "../util.h" 13 #include "resource.h" 14 15 static struct config config; 16 static int event_cnt; 17 18 struct configfile { 19 FILE *fp; 20 char *filename; 21 struct config *config; 22 uint32_t length; 23 }; 24 25 static struct configfile configfile; 26

138

Page 139: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

27 BOOL CALLBACK callback(HWND, UINT, WPARAM, LPARAM); 28 29 void init_gui(HWND); 30 31 int add_event( HWND hWnd ); 32 void delete_event( HWND ); /*Event number to delete*/ 33 int save_config(HWND, struct configfile *); 34 int load_config(HWND, struct configfile *); 35 int open_cfg(HWND, struct configfile*, char*); 36 void update_crc(struct config *conf); 37 38 int WINAPI 39 WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, 40 int iCmdShow) 41 { 42 MSG msg; 43 HWND hDlg; 44 45 OleInitialize(NULL); 46 47 hDlg = CreateDialogParam(hInstance, 48 MAKEINTRESOURCE(IDD_DIALOG1), NULL, callback, (LPARAM) 49 hInstance); 50 if (hDlg <= 0) { 51 MessageBox(NULL, "Error creating dialog box.", "Error", 52 MB_OK | MB_ICONERROR); 53 exit(1); 54 } 55 ShowWindow(hDlg, SW_SHOWNORMAL); 56 UpdateWindow(hDlg); 57 58 while (GetMessage(&msg, NULL, 0, 0) > 0) { 59 TranslateMessage(&msg); 60 DispatchMessage(&msg); 61 } 62 63 OleUninitialize(); 64 65 return msg.wParam; 66 } 67 68 BOOL CALLBACK 69 callback(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 70 { 71 switch(uMsg) { 72 case WM_INITDIALOG: 73 // lParam is hInstance 74 init_gui( hwnd ); 75 #if 0 76 memset(&pibfile, 0, sizeof(pibfile)); 77 #endif 78 { 79 HICON hicon; 80 hicon = LoadIcon((HINSTANCE) lParam, 81 MAKEINTRESOURCE(IDI_ICON1)); 82 if (hicon == NULL) { 83 MessageBox(hwnd, "Could not set icon.", 84 "Error", MB_OK | MB_ICONERROR); 85 DestroyWindow(hwnd); 86 } 87 SendMessage(hwnd, WM_SETICON, ICON_SMALL, 88 (LPARAM) hicon);

139

Page 140: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

89 SendMessage(hwnd, WM_SETICON, ICON_BIG, 90 (LPARAM) hicon); 91 } 92 break; 93 94 case WM_COMMAND: 95 if (HIWORD(wParam) == BN_CLICKED) { 96 switch(LOWORD(wParam)) { 97 case IDC_BTNPIDADD: 98 add_event(hwnd); 99 break; 100 case IDC_PIDDEL: 101 delete_event(hwnd); 102 break; 103 case IDC_BTNOPEN: 104 if(open_cfg(hwnd, &configfile, "rb") == -1) 105 break; 106 if(load_config(hwnd, &configfile) == -1) 107 break; 108 break; 109 case IDC_BTNSAVE: 110 configfile.config = &config; 111 112 if( open_cfg(hwnd,&configfile,"wb") == -1 ) 113 break; 114 115 if( save_config(hwnd, &configfile) == -1 ) 116 break; 117 break; 118 119 case IDC_CLEARALL: 120 memset(&config,0,sizeof( struct config ) ); 121 event_cnt = 0; 122 SendDlgItemMessage(hwnd, IDC_PIDLIST, LB_RESETCONTENT, 0 , 0); 123 break; 124 } 125 } 126 break; 127 128 case WM_CLOSE: 129 DestroyWindow(hwnd); 130 break; 131 132 case WM_DESTROY: 133 PostQuitMessage(0); 134 break; 135 136 default: 137 return FALSE; 138 } 139 140 return TRUE; 141 } 142 143 void 144 init_gui(HWND hWnd) 145 { 146 int i; 147 char buf[64]; 148 149 i = 0;

140

Page 141: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

150 while( i<=0x2F ) 151 { 152 snprintf(buf, sizeof(buf),"(%02X) %s", i, pid_name[i] ); 153 SendDlgItemMessage( hWnd, IDC_PIDADD, CB_INSERTSTRING, i, (LPARAM) buf ); 154 i++; 155 } 156 157 } 158 159 int 160 add_event( HWND hWnd ) /*Adds an event to the dconfig and the list box*/ 161 { 162 char buf[128]; 163 char subbuf[40]; 164 int interval; 165 double time; 166 int pid; 167 168 if( event_cnt == NUM_LOG_SLOTS ) 169 return -1; 170 171 GetDlgItemText( hWnd, IDC_PIDINTERVAL, buf, sizeof(buf) ); 172 time = atof( buf ); 173 174 interval = rint( time/0.5 ); 175 176 if(interval == 0 ) 177 interval = 1; 178 179 pid = SendDlgItemMessage( hWnd, IDC_PIDADD, CB_GETCURSEL, 0, 0 ); 180 181 if( pid < 0 || interval < 0 ) 182 return -1; 183 184 if( strlen(pid_name[pid]) > 30 ) 185 { 186 strncpy( subbuf, pid_name[pid], 30 ); 187 strncat( subbuf, "...", sizeof(subbuf) ); 188 } 189 else { 190 strncpy( subbuf, pid_name[pid], sizeof(subbuf)); 191 } 192 193 snprintf(buf,sizeof(buf),"(PID %02x) %s / %0.1f s", pid, subbuf, ((double)interval)*0.5 ); 194 SendDlgItemMessage(hWnd, IDC_PIDLIST, LB_INSERTSTRING, event_cnt, (LPARAM) buf ); 195 196 config.event[event_cnt].interval = interval; 197 config.event[event_cnt].mode = 1; 198 config.event[event_cnt].pid = pid; 199 200 event_cnt++; 201 202 return 0; 203 } 204 205 void 206 delete_event( HWND hWnd ) /*Event number to delete*/ 207 { 208 int sel; 209 int i; 210

141

Page 142: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

211 sel = SendDlgItemMessage(hWnd, IDC_PIDLIST, LB_GETCURSEL, 0 , 0); 212 213 if( sel < 0 ) 214 return; 215 216 SendDlgItemMessage(hWnd, IDC_PIDLIST, LB_DELETESTRING, (WPARAM) sel, 0 ); 217 event_cnt--; 218 219 for(i=sel; i < event_cnt; ++i) 220 { 221 config.event[i].interval = config.event[i+1].interval; 222 config.event[i].mode = config.event[i+1].mode; 223 config.event[i].pid = config.event[i+1].pid; 224 } 225 226 for(i=event_cnt; i < NUM_LOG_SLOTS; ++i) 227 { 228 config.event[i].interval = 0; 229 config.event[i].mode = 0; 230 config.event[i].pid = 0; 231 } 232 } 233 234 int 235 open_cfg(HWND hwnd, struct configfile *cfgf, char *perm) 236 { 237 char buf[MAX_PATH]; 238 OPENFILENAME ofn; 239 int ret; 240 241 memset(&ofn, 0, sizeof(ofn)); 242 ofn.lStructSize = sizeof(OPENFILENAME); 243 ofn.hwndOwner = hwnd; 244 ofn.lpstrFile = buf; 245 ofn.nMaxFile = sizeof(buf); 246 ofn.lpstrFile[0] = '\0'; 247 ofn.lpstrFilter = "CFG Files (*.cfg)\0*.cfg\0All Files (*.*)\0*.*\0"; 248 ofn.lpstrCustomFilter = NULL; 249 ofn.nFilterIndex = 0; 250 ofn.lpstrFileTitle = NULL; 251 ofn.lpstrInitialDir = NULL; 252 ofn.lpstrTitle = NULL; 253 ofn.Flags = OFN_FILEMUSTEXIST; 254 ofn.lpstrDefExt = "cfg"; 255 256 cfgf->length = sizeof( struct config ); 257 258 if (strchr(perm, 'r') != NULL) 259 ret = GetOpenFileName(&ofn); 260 else { 261 if (cfgf->config == NULL) { 262 MessageBox(hwnd, "Nothing to save.", "Error", 263 MB_OK | MB_ICONERROR); 264 return -1; 265 } 266 ret = GetSaveFileName(&ofn); 267 } 268 269 if (ret == TRUE) { 270 cfgf->fp = fopen(buf, perm); 271 if (cfgf->fp == NULL) { 272 MessageBox(hwnd, "Could not open CFG file.",

142

Page 143: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

273 "Error", MB_OK | MB_ICONERROR); 274 return -1; 275 } 276 if (cfgf->filename != NULL) 277 free(cfgf->filename); 278 cfgf->filename = strdup(buf); 279 280 return 0; 281 } 282 283 return -1; 284 } 285 286 int 287 save_config(HWND hWnd, struct configfile * file) 288 { 289 int r; 290 291 struct config *config; 292 config = file->config; 293 294 update_crc(config); 295 296 if (!valid_config(config)) { 297 MessageBox(hWnd, "Configuration File Invalid.", "Error", 298 MB_OK | MB_ICONERROR); 299 fclose(file->fp); 300 return -1; 301 } 302 303 r = fwrite(config, 1, sizeof(struct config), file->fp); 304 305 if (r != sizeof(struct config)) { 306 MessageBox(hWnd, "Failed to write configuration file!", "Error", 307 MB_OK | MB_ICONERROR); 308 fclose(file->fp); 309 return -1; 310 } 311 312 fclose(file->fp); 313 314 return r; 315 316 } 317 318 int 319 load_config(HWND hWnd, struct configfile * file) 320 { 321 char buf[128]; 322 char subbuf[40]; 323 int r; 324 int i; 325 struct config tmpconfig; 326 int pid; 327 int interval; 328 329 r = fread(&tmpconfig, 1, sizeof(struct config),file->fp); 330 331 if (!valid_config(&tmpconfig)) { 332 MessageBox(hWnd, "Invalid configuration file!", "Error", 333 MB_OK | MB_ICONERROR); 334 fclose(file->fp);

143

Page 144: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

335 return -1; 336 } 337 338 SendDlgItemMessage(hWnd, IDC_PIDLIST, LB_RESETCONTENT, 0 , 0); 339 340 memset(&config,0,sizeof(config)); 341 event_cnt = 0; 342 343 for(i=0; i< NUM_LOG_SLOTS; ++i) 344 { 345 pid = tmpconfig.event[i].pid; 346 interval = tmpconfig.event[i].interval; 347 348 if( strlen(pid_name[pid]) > 30 ) 349 { 350 strncpy( subbuf, pid_name[pid], 30 ); 351 strncat( subbuf, "...", sizeof(subbuf) ); 352 } 353 else { 354 strncpy( subbuf, pid_name[pid], sizeof(subbuf)); 355 } 356 357 if( interval > 0 ) { 358 snprintf(buf,sizeof(buf),"(PID %02x) %s / %0.1f s", pid, subbuf, ((double)interval)*0.5 ); 359 SendDlgItemMessage(hWnd, IDC_PIDLIST, LB_INSERTSTRING, event_cnt, (LPARAM) buf ); 360 361 config.event[event_cnt].interval = interval; 362 config.event[event_cnt].mode = 1; 363 config.event[event_cnt].pid = pid; 364 365 event_cnt++; 366 } 367 368 } 369 370 fclose(file->fp); 371 return 0; 372 373 } 374 375 void 376 update_crc(struct config *conf) 377 { 378 uint8_t *p; 379 uint16_t crc; 380 int i; 381 382 p = (uint8_t *) conf; 383 384 conf->crc = 0; 385 crc = 0xffff; 386 for (i = 0; i < sizeof(*conf); ++i) { 387 crc = crc16_update(crc, *p); 388 ++p; 389 } 390 391 conf->crc = crc; 392 }

<<<<< END pc/wemsconfiggui/wemsconfiggui.c <<<<<

144

Page 145: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 146: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 147: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 148: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

163 if(state == 0) { /*Upper Nisbble*/ 164 buf[index] = c<<4; 165 state=1; 166 } 167 else { /*Lower Nibble*/ 168 buf[index++] += c; 169 state=0; 170 } 171 } 172 173 return index; 174 } 175 176 /*This reads a string from the ELM chip*/ 177 uint8_t 178 elm_gets(uint8_t *buf, uint8_t len) 179 { 180 int c; 181 int index; 182 183 index=0; 184 185 while( (c=usart_getc(DEV_ELM)) != '\n') 186 { 187 if( c == '\0' ) 188 continue; 189 190 buf[ index++ ] = c; 191 192 if( index == len - 1) 193 break; 194 } 195 196 buf[ index ] = '\0'; 197 198 return index; 199 } 200 201 /*This function toggles echo. 0 = ECHO OFF, 1 = ECHO ON.*/ 202 inline void 203 elm_setecho( uint8_t val ) 204 { 205 uint8_t buf[7]; 206 207 if(val) 208 snprintf( buf, sizeof(buf), "AT E1\r" ); /*Turn echo on*/ 209 else 210 snprintf( buf, sizeof(buf), "AT E0\r" ); /*Turn echo off*/ 211 212 usart_put( DEV_ELM, buf, sizeof(buf) ); 213 214 elm_flush('>'); /*Wait for prompt*/ 215 216 } 217 218 /*This function sets which protocol to use.*/ 219 inline void 220 elm_set_protocol( uint8_t protocol ) 221 { 222 uint8_t buf[10]; 223 snprintf( buf, sizeof(buf), "AT SP A%1.1x\r", protocol ); /*This searches for the specified prot. If not found, auto searches*/

148

Page 149: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 150: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 151: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 152: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 153: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 154: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

60 state=0; 61 } 62 } 63 64 return 0; 65 } 66 67 int main(int argc, char* argv[]) 68 { 69 char buf[] = "0014"; 70 uint8_t this[12]; 71 72 btoh(this, buf, sizeof(this)); 73 74 printf("Little Indian* %04x\n", this[0]<<8 | this[1]); 75 printf("Big Indian* %04x\n", this[1]<<8 | this[0]); 76 /*

77 printf("Test\n"); 78 79 uint8_t resp; 80 int i; 81 82 while(1) { 83 resp = elm_dtc_peek(); 84 85 elm_dtc_update( resp ); 86 87 for(i=0; i< (resp&0x7F); ++i) 88 printf("%x\n", elm_dtc_next() ); 89 90 for(i=0; i< (resp&0x7F); ++i) 91 printf("%x\n", elm_dtc_prev() ); 92 93 } 94 */ 95 return 0; 96 } 97 98 99 100

<<<<< END software/elm_test/elm_test.c <<<<<

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

Page 155: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

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 };

<<<<< END software/elm_test/obd_pid.c <<<<<

>>>>> BEG software/flash_test/flash_test.c >>>>> 1 #include <stdio.h> 2 #include <string.h> 3 4 #include <util/delay.h> 5 6 #include "../nvm.h" 7 #include "../lcd.h" 8 #include "../spi.h" 9 #include "../util.h" 10 11 int 12 main(void) 13 { 14 uint8_t buf[128]; 15 int i; 16 char str[128]; 17 18 lcd_init(); 19 spi_init(); 20 nvm_unlock(); 21 22 #if 0 23 nvm_erase(); 24 fatal("done"); 25 #endif 26 27 #if 0 28 memset(buf, 0, sizeof(buf)); 29 nvm_read(buf, 0x00000005, sizeof(buf)); 30 for (i = 0; i < 128; ++i) { 31 lcd_clrscr(); 32 lcd_home(0); 33 sprintf(str, "%d: %02X", i, buf[i]); 34 lcd_print(str); 35 _delay_ms(250); 36 }

155

Page 156: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

37 #else 38 for (i = 0; i < 128; ++i) 39 buf[i] = i; 40 nvm_write(0x00000000, buf, sizeof(buf)); 41 42 _delay_ms(1000); 43 lcd_clrscr(); 44 lcd_home(0); 45 lcd_print("all done."); 46 #endif 47 48 #if 0 49 status = nvm_status(NVM_READ, 0); 50 51 sprintf(str, "0x%02X", status); 52 lcd_print(str); 53 _delay_ms(500); 54 55 nvm_wel(NVM_SET); 56 _delay_ms(500); 57 58 lcd_clrscr(); 59 lcd_home(0); 60 nvm_read(&b, 0x00000000, 1); 61 sprintf(str, "0x%02X", b); 62 lcd_print(str); 63 _delay_ms(1000); 64 #endif 65 66 fatal("end of program"); 67 68 return 0; 69 }

<<<<< END software/flash_test/flash_test.c <<<<<

>>>>> BEG software/lcd_test/lcd_test.c >>>>> 1 #include "../lcd.h" 2 3 int 4 main(void) 5 { 6 init_lcd(); 7 lcd_print("hello, world"); 8 }

<<<<< END software/lcd_test/lcd_test.c <<<<<

>>>>> BEG software/ui_test/menu_res.c >>>>> 1 #include <stdio.h> 2 #include "ui.h" 3 #include "menu_res.h" 4 5 void test() 6 { 7 printf("This is a test function.\n"); 8 } 9 10 MENU_RES_STATIC(MENU_ID_ROOT, "test1", MENU_ID_CHECK_DTC, MENU_ID_CHECK_DTC, MENU_ID_ROOT, NULL); 11 MENU_RES_STATIC(MENU_ID_CHECK_DTC, "test2", MENU_ID_ROOT, MENU_ID_ROOT, MENU_ID_ROOT, test);

156

Page 157: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 158: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

60 struct static_menu_entry * static_current; 61 struct dynamic_menu_entry * dynamic_current; 62 63 static uint8_t update = 1; 64 65 current = menu_get_entry( ui_menu.current ); /*Get the current menu entry*/ 66 67 if( update ) 68 { 69 if( current->type == MENU_ENTRY_STATIC ) /*Static menus have a constant string*/ 70 { 71 static_current = (struct static_menu_entry*)current; 72 lcd_update(&lcd, 1, static_current->disp ); 73 } 74 else if( current->type == MENU_ENTRY_DYNAMIC ) /*Static menus have output strings generated by functions*/ 75 { 76 dynamic_current = (struct dynamic_menu_entry*)current; 77 dynamic_current->disp(); 78 } 79 update = 0; 80 } 81 82 while( (c=getc(stdin)) != '\n' ) 83 { 84 if( c== 'l' ) 85 buttons |= 0x1; 86 87 if( c=='r' ) 88 buttons |= 0x2; 89 90 if( c== 'x' ) 91 buttons |= 0x4; 92 } 93 94 if ( btn_down(BUTTON_LEFT) ) 95 { 96 if(current->left != ui_menu.current ) 97 { 98 update = 1; 99 menu_set_current( current->left ); 100 } 101 102 } 103 104 if ( btn_down(BUTTON_ENTER) ) 105 { 106 if(current->enter != ui_menu.current ) 107 { 108 update = 1; 109 menu_set_current( current->enter ); 110 } 111 112 if( current->fp != NULL ) /*If there is a function to be executed on enter button, then run it*/ 113 current->fp(); 114 } 115 116 if ( btn_down(BUTTON_RIGHT) ) 117 { 118 if(current->right != ui_menu.current )

158

Page 159: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

119 { 120 update = 1; 121 menu_set_current( current->right ); 122 } 123 } 124 125 126 } 127 128 void 129 init_ui(void) 130 { 131 /* enable pull-up resistors */ 132 PORTA |= 0xF0; 133 PORTD |= 0x80; 134 135 _delay_us(30); 136 137 /* mask off pins of interest for b3..1 */ 138 PCMSK0 = 0xF0; 139 140 /* enable PCINT0 for b3..1 */ 141 PCICR |= 0x01; 142 143 /* mask off pins of interest for lcd_present */ 144 PCMSK3 = 0x80; 145 146 /* enable PCINT3 for lcd_present */ 147 PCICR |= 0x08; 148 149 150 menu_add_static( &MENU_RES_NAME(MENU_ID_ROOT) ); 151 menu_add_static( &MENU_RES_NAME(MENU_ID_CHECK_DTC) ); 152 menu_set_current( MENU_ID_ROOT ); 153 } 154 155 156 157 uint8_t 158 btn_down(uint8_t btn) 159 { 160 161 if((buttons & 0x1 ) && btn == BTN_3) 162 { 163 buttons &= ~0x1; 164 return 1; 165 } 166 167 if((buttons & 0x2 )&& btn == BTN_1) 168 { 169 buttons &= ~0x2; 170 return 1; 171 } 172 173 if((buttons & 0x4 )&& btn == BTN_2) 174 { 175 buttons &= ~0x4; 176 return 1; 177 } 178 179 180 return 0;

159

Page 160: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

181 } 182 183 void menu_set_current( uint8_t id ) 184 { 185 ui_menu.current = id; 186 } 187 188 void 189 menu_add_static( const struct static_menu_entry * entry ) 190 { 191 ui_menu.menu_entries[ ui_menu.length ] = (struct menu_entry*)entry; 192 ui_menu.length++; 193 } 194 195 void 196 menu_add_dynamic( const struct dynamic_menu_entry * entry) 197 { 198 ui_menu.menu_entries[ ui_menu.length ] = (struct menu_entry*)entry; 199 ui_menu.length++; 200 } 201 202 struct menu_entry * 203 menu_get_entry( uint8_t id ) 204 { 205 int i; 206 207 for(i=0;i<MENU_ENTRY_COUNT;++i) 208 { 209 if( ui_menu.menu_entries[i]->id == id ) 210 break; 211 } 212 213 return ui_menu.menu_entries[i]; 214 } 215

<<<<< END software/ui_test/ui.c <<<<<

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

Page 161: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

24 int main() 25 { 26 init_ui(); 27 while(1) 28 { 29 menu(); 30 } 31 return 0; 32 }<<<<< END software/ui_test/ui_test.c <<<<<

>>>>> BEG software/usart_test/usart_test.c >>>>> 1 #include <stdio.h> 2 #include <stdint.h> 3 #include <string.h> 4 5 #include <util/delay.h> 6 #include <avr/interrupt.h> 7 #include <avr/io.h> 8 9 #include "../usart.h" 10 #include "../lcd.h" 11 #include "../util.h" 12 13 #define usart_tx_str(dev, s) (usart_tx((dev), (uint8_t *) (s), strlen((s)))) 14 15 int 16 main(void) 17 { 18 char buf[128]; 19 int len; 20 int i; 21 22 lcd_init(); 23 usart_init(); 24 sei(); 25 26 #if 0 27 usart_tx_str(0, "start tx test:\r\n"); 28 for (i = 0; i < 512; ++i) { 29 sprintf(buf, "hello world %d\r\n", i); 30 j = usart_tx_str(0, buf); 31 if (j != strlen(buf)) { 32 sprintf(buf, "tx: %d, i: %d", j, i); 33 fatal(buf); 34 } 35 _delay_ms(1); 36 } 37 _delay_ms(1000); 38 usart_tx_str(0, "start rx test:\r\n"); 39 #endif 40 41 for (;;) { 42 len = usart_rx(0, (uint8_t *) buf, sizeof(buf) - 1); 43 buf[len] = '\0'; 44 usart_puts(1, buf); 45 46 len = usart_rx(1, (uint8_t *) buf, sizeof(buf) - 1); 47 buf[len] = '\0'; 48 usart_puts(0, buf); 49 } 50

161

Page 162: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 163: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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 }

<<<<< END software/wems/wems.c <<<<<

>>>>> BEG pc/livestats/resource.h >>>>> 1 #ifndef IDC_STATIC 2 #define IDC_STATIC (-1) 3 #endif 4 5 #define IDD_DIALOG1 100 6 #define IDI_ICON1 101 7 #define IDC_RADIO1 1000 8 #define IDC_RADIO2 1001 9 #define IDC_RADIO6 1005 10 #define IDC_RADIO7 1006 11 #define IDC_RADIO9 1008 12 #define IDC_SETLIVE 1009 13 #define IDC_EDIT_COM 1047

163

Page 164: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

14 #define IDC_EDIT_DESC1 1048 15 #define IDC_EDIT_DESC2 1049 16 #define IDC_EDIT_DESC3 1050 17 #define IDC_EDIT_DESC4 1051 18 #define IDC_EDIT_DESC5 1052 19 #define IDC_EDIT_DESC6 1053 20 #define IDC_EDIT_DESC7 1054 21 #define IDC_EDIT_DESC8 1055 22 #define IDC_EDIT_DESC9 1056 23 #define IDC_EDIT_VAL1 1057 24 #define IDC_EDIT_VAL2 1058 25 #define IDC_EDIT_VAL3 1059 26 #define IDC_EDIT_VAL4 1060 27 #define IDC_EDIT_VAL5 1061 28 #define IDC_EDIT_VAL6 1062 29 #define IDC_EDIT_VAL7 1063 30 #define IDC_EDIT_VAL8 1064 31 #define IDC_EDIT_VAL9 1065 32 #define IDC_STATUS 1066 33 #define IDC_RADIO3 1069 34 #define IDC_RADIO4 1070 35 #define IDC_RADIO5 1071 36 #define IDC_RADIO8 1072

<<<<< END pc/livestats/resource.h <<<<<

>>>>> BEG pc/wemsconfiggui/resource.h >>>>> 1 #ifndef IDC_STATIC 2 #define IDC_STATIC (-1) 3 #endif 4 5 #define IDD_DIALOG1 100 6 #define IDI_ICON1 101 7 #define IDC_COMSEL 1001 8 #define IDC_CLEARALL 1002 9 #define IDC_PIDADD 40012 10 #define IDC_PIDINTERVAL 40013 11 #define IDC_BTNPIDADD 40014 12 #define IDC_PIDLIST 40015 13 #define IDC_PIDDEL 40016 14 #define IDC_BTNDOWNLOAD 40018 15 #define IDC_BTNUPLOAD 40019 16 #define IDC_BAUD 40023 17 #define IDC_STATUS 40024 18 #define IDC_BTNOPEN 40025 19 #define IDC_BTNSAVE 40026

<<<<< END pc/wemsconfiggui/resource.h <<<<<

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

Page 165: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

<<<<< END software/elm_test/elm.h <<<<<

>>>>> BEG software/elm_test/elm_dtc.h >>>>> 1 /* 2 * Copyright (c) 2009 Nathaniel Houghton <[email protected]> 3 * Robert Kirchgessner <[email protected]>

165

Page 166: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 167: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 168: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 169: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

((A>>4)&0x3), \ 116

((A>>0)&0xF), \ 117

((A>>12)&0xF),\ 118

((A>>8) &0xF) ) 119 120 /*Mode 5 PIDs 121 Oxygen sensor data. */ 122 123 /*Mode 9 PIDs*/ 124 #define VIN 0x02 //Vehicle VIN number. Returns 5 lines. 125 126 /*Defines for conversion.*/ 127 #define CONV_PIDM0104(A) (A*100.0/255.0) //Converted engine load value (%) 128 #define CONV_PIDM0105(A) (A-40.0) //Converted engine coolant temp. (deg C) 129 #define CONV_PIDM0106(A) (A-128.0)*(100.0/128.0) 130 #define CONV_PIDM0107(A) (A-128.0)*(100.0/128.0) 131 #define CONV_PIDM0108(A) (A-128.0)*(100.0/128.0) 132 #define CONV_PIDM0109(A) (A-128.0)*(100.0/128.0) 133 #define CONV_PIDM010A(A) (A*3.0) //Fuel pressure (kPa -- gauged.) 134 #define CONV_PIDM010B(A) (A) //Intake manifold pressure (kPa -- absolute.) 135 #define CONV_PIDM010C(A) (A/4.0) //Engine RPMs. 2 Bytes of data. A = B0<<8 + B1 136 #define CONV_PIDM010D(A) (A) //Vehicle speed (absolute.) 137 #define CONV_PIDM010E(A) (A/2.0) - 64.0 138 #define CONV_PIDM010F(A) (A-40.0) //Intake air temperature (deg C) 139 #define CONV_PIDM0110(A) (A/100.0) //MAF Air flow rate. Gallons/second. 140 #define CONV_PIDM0111(A) (A*(100.0/255.0)) //Throttle position (%) 141 #define CONV_PIDM011F(A) (A) //Two bytes of data. B0<<8 + B1. Seconds. 142 #define CONV_PIDM0121(A) (A) //Distance travelled with MIL (km) 143 #define CONV_PIDM0122(A) (A*0.079) //Fuel rail pressure in kPa 144 #define CONV_PIDM0123(A) (A*10) //Diesel fuel rail pressure. Two bytes of data. kPa

169

Page 170: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

(gauge) 145 #define CONV_PIDM012F(A) (A*(100.0/255.0)) //Fuel level input. % 146 #define CONV_PIDM0130(A) (A) //Number of warm-ups since DTC cleared. 147 #define CONV_PIDM0131(A) (A) //Distance travelled with codes cleared. (km) 148 #define CONV_PIDM0151(A) (A) //Table lookup value. 149 #define CONV_PIDM0152(A) (A*(100.0/255.0)) //Ethanol fuel % 150 151 /*PID Lengths*/ 152 #define LEN_M0100 4 153 #define LEN_M0101 4 154 #define LEN_M0102 8 155 #define LEN_M0103 2 156 #define LEN_M0104 1 157 #define LEN_M0105 1 158 #define LEN_M0106 1 159 #define LEN_M0107 1 160 #define LEN_M0108 1 161 #define LEN_M0109 1 162 #define LEN_M010A 1 163 #define LEN_M010B 1 164 #define LEN_M010C 1 165 #define LEN_M010D 1 166 #define LEN_M010E 1 167 #define LEN_M010F 1 168 #define LEN_M0110 2 169 #define LEN_M0111 1 170 #define LEN_M0112 1 171 #define LEN_M0113 1 172 #define LEN_M0114 2 173 #define LEN_M0115 2 174 #define LEN_M0116 2 175 #define LEN_M0117 2 176 #define LEN_M0118 2 177 #define LEN_M0119 2 178 #define LEN_M011A 2 179 #define LEN_M011B 2 180 #define LEN_M011C 1 181 #define LEN_M011D 1 182 #define LEN_M011E 1 183 #define LEN_M011F 2 184 #define LEN_M0120 4 185 #define LEN_M0121 2 186 #define LEN_M0122 2 187 #define LEN_M0123 2 188 #define LEN_M0124 4 189 #define LEN_M0125 4 190 #define LEN_M0126 4 191 #define LEN_M0127 4 192 #define LEN_M0128 4 193 #define LEN_M0129 4 194 #define LEN_M012A 4 195 #define LEN_M012B 4 196 #define LEN_M012C 1 197 #define LEN_M012D 1 198 #define LEN_M012E 1 199 #define LEN_M012F 1 200 #define LEN_M0130 1 201 #define LEN_M0131 2 202 #define LEN_M0132 2 203 #define LEN_M0133 1 204 #define LEN_M0134 4 205 #define LEN_M0135 4

170

Page 171: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

206 #define LEN_M0136 4 207 #define LEN_M0137 4 208 #define LEN_M0138 4 209 #define LEN_M0139 4 210 #define LEN_M013A 4 211 #define LEN_M013B 4 212 #define LEN_M013C 2 213 #define LEN_M013D 2 214 #define LEN_M013E 2 215 #define LEN_M013F 2 216 #define LEN_M0140 4 217 #define LEN_M0141 4 218 #define LEN_M0142 2 219 #define LEN_M0143 2 220 #define LEN_M0144 2 221 #define LEN_M0145 1 222 #define LEN_M0146 1 223 #define LEN_M0147 1 224 #define LEN_M0148 1 225 #define LEN_M0149 1 226 #define LEN_M014A 1 227 #define LEN_M014B 1 228 #define LEN_M014C 1 229 #define LEN_M014D 2 230 #define LEN_M014E 2 231 #define LEN_M014F 0 232 #define LEN_M0150 0 233 #define LEN_M0151 1 234 #define LEN_M0152 1

235 236 #endif

<<<<< END software/elm_test/obd_pid.h <<<<<

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

Page 172: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

27 #define MENU_DEC_STATIC(ID) extern const struct static_menu_entry _RES_##ID 28 #define MENU_DEC_DYNAMIC(ID) extern const struct dynamic_menu_entry _RES_##ID 29 #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 } 30 #define MENU_RES_DYNAMIC(ID, DISP, LEFT, RIGHT, ENTER, FP) const struct static_menu_entry _RES_##ID = { MENU_ENTRY_DYNAMIC, ID, LEFT, RIGHT, ENTER, FP, DISP } 31 32 #define MENU_ID_ROOT 0x0 33 #define MENU_ID_CHECK_DTC 0x1 34 #define MENU_ENTRY_COUNT 2 35 36 MENU_DEC_STATIC( MENU_ID_ROOT ); 37 MENU_DEC_STATIC( MENU_ID_CHECK_DTC ); 38 39 40 #endif

<<<<< END software/ui_test/menu_res.h <<<<<

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

Page 173: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

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

Page 174: Final Report A Wireless ECU Monitoring System Team WEMS · EEL 4924 Electrical Engineering Design (Senior Design) Final Report A Wireless ECU Monitoring System Team WEMS 21 April

University of Florida A Wireless ECU Monitoring System 04/21/09Team WEMS

98 99 #endif

<<<<< END software/ui_test/ui.h <<<<<

174