Test code for interfacing to megasquirt ECU.

Dependencies:   jtlcd mbed

Committer:
jont
Date:
Mon Feb 16 08:34:14 2015 +0000
Revision:
4:50203a03ccd2
Parent:
3:fa1485b77707
Changed greeting message

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jont 2:57a6c415e495 1 /*
jont 2:57a6c415e495 2 jon trinder
jont 2:57a6c415e495 3 jont@ninelocks.com 2015
jont 2:57a6c415e495 4
jont 2:57a6c415e495 5 Megasquirt test and debugging code for GU
jont 2:57a6c415e495 6
jont 2:57a6c415e495 7
jont 2:57a6c415e495 8 */
jont 0:cf99a7b873b3 9 #include "mbed.h"
jont 0:cf99a7b873b3 10 #include "jtlcd.h"
jont 0:cf99a7b873b3 11 #include "Counter.h"
jont 0:cf99a7b873b3 12
jont 0:cf99a7b873b3 13
jont 0:cf99a7b873b3 14 float coolant;
jont 0:cf99a7b873b3 15 int int_coolant = 0;
jont 0:cf99a7b873b3 16 void callback_reed(void);
jont 0:cf99a7b873b3 17 void siphon_data();
jont 0:cf99a7b873b3 18
jont 0:cf99a7b873b3 19 DigitalOut myled(LED1);
jont 0:cf99a7b873b3 20 DigitalOut myled2(LED2);
jont 0:cf99a7b873b3 21 DigitalOut myled3(LED3);
jont 0:cf99a7b873b3 22 DigitalOut myled4(LED4);
jont 0:cf99a7b873b3 23
jont 0:cf99a7b873b3 24
jont 0:cf99a7b873b3 25 DigitalIn reed(p9); //would default to in but we wanna play with its mode
jont 0:cf99a7b873b3 26 Counter counter_reed(p9, callback_reed ); //setup the counter to use pin 9
jont 0:cf99a7b873b3 27
jont 0:cf99a7b873b3 28 void sioRXHandler(void);
jont 0:cf99a7b873b3 29 #define combuffsize 256
jont 3:fa1485b77707 30 volatile char comBuff[combuffsize]; /* space for commands yeh its small make bigger if you need to */
jont 3:fa1485b77707 31 volatile int comBuffIndex; /* buffer index */
jont 0:cf99a7b873b3 32
jont 0:cf99a7b873b3 33 char lineBuffer[32]; //for saving data for the lcd display
jont 0:cf99a7b873b3 34
jont 0:cf99a7b873b3 35 Serial pc(USBTX, USBRX); // tx, rx
jont 0:cf99a7b873b3 36
jont 0:cf99a7b873b3 37 /*=====================================================================================*/
jont 0:cf99a7b873b3 38 //
jont 0:cf99a7b873b3 39 // callback funtions mostly for the counters
jont 0:cf99a7b873b3 40 //
jont 0:cf99a7b873b3 41 /*=====================================================================================*/
jont 0:cf99a7b873b3 42 /*=====================================================================================*/
jont 0:cf99a7b873b3 43 // callback for the reed switch input
jont 0:cf99a7b873b3 44 // puts logentry into the ringbuffer
jont 0:cf99a7b873b3 45 /*=====================================================================================*/
jont 0:cf99a7b873b3 46 void callback_reed()
jont 0:cf99a7b873b3 47 {
jont 0:cf99a7b873b3 48 time_t seconds = time(NULL);
jont 0:cf99a7b873b3 49 //LPC_RTC->GPREG3 = counter_reed.read();
jont 0:cf99a7b873b3 50 __disable_irq(); // Disable Interrupts
jont 0:cf99a7b873b3 51 comBuffIndex = 0;
jont 0:cf99a7b873b3 52 __enable_irq();
jont 0:cf99a7b873b3 53 pc.putc('A');
jont 0:cf99a7b873b3 54
jont 0:cf99a7b873b3 55 }
jont 0:cf99a7b873b3 56
jont 0:cf99a7b873b3 57 void serialInit(void)
jont 0:cf99a7b873b3 58 {
jont 0:cf99a7b873b3 59 pc.attach(&sioRXHandler);
jont 0:cf99a7b873b3 60 }
jont 0:cf99a7b873b3 61
jont 0:cf99a7b873b3 62
jont 0:cf99a7b873b3 63 /*======================================================================*/
jont 0:cf99a7b873b3 64 /* sioRXHandler */
jont 0:cf99a7b873b3 65 /*======================================================================*/
jont 0:cf99a7b873b3 66 /*
jont 0:cf99a7b873b3 67 This is inefficient and long handed but makes explaining it to someone
jont 0:cf99a7b873b3 68 easier.
jont 0:cf99a7b873b3 69
jont 0:cf99a7b873b3 70 Which is the point, to be understandable at this stage :-)
jont 0:cf99a7b873b3 71
jont 0:cf99a7b873b3 72
jont 0:cf99a7b873b3 73 If more data received than buffer length we throw it away!
jont 0:cf99a7b873b3 74 */
jont 0:cf99a7b873b3 75 void sioRXHandler(void)
jont 0:cf99a7b873b3 76 {
jont 0:cf99a7b873b3 77 char theChar = 0x00;
jont 0:cf99a7b873b3 78 myled2 = 1;
jont 0:cf99a7b873b3 79 myled3 = 1;
jont 0:cf99a7b873b3 80 while (pc.readable()){
jont 0:cf99a7b873b3 81 theChar = pc.getc();
jont 0:cf99a7b873b3 82 pc.putc(theChar); //echo it back
jont 0:cf99a7b873b3 83 if (comBuffIndex >= combuffsize - 1)
jont 0:cf99a7b873b3 84 {
jont 0:cf99a7b873b3 85 comBuffIndex = combuffsize - 1;
jont 0:cf99a7b873b3 86 }
jont 0:cf99a7b873b3 87 comBuff[comBuffIndex] = theChar;
jont 0:cf99a7b873b3 88 comBuffIndex++;
jont 0:cf99a7b873b3 89 }
jont 0:cf99a7b873b3 90 myled2 = 0;
jont 0:cf99a7b873b3 91 }
jont 0:cf99a7b873b3 92
jont 0:cf99a7b873b3 93 /*======================================================================*/
jont 0:cf99a7b873b3 94 /* Main */
jont 0:cf99a7b873b3 95 /*======================================================================*/
jont 0:cf99a7b873b3 96 int main() {
jont 0:cf99a7b873b3 97 setbuf(stdout, NULL);
jont 0:cf99a7b873b3 98 // no buffering for this filehandle, this stop the weird
jont 0:cf99a7b873b3 99 //behaviour with things not being dfiplaye untill a character was received.
jont 0:cf99a7b873b3 100 serialInit();
jont 0:cf99a7b873b3 101 reed.mode(PullUp);
jont 0:cf99a7b873b3 102
jont 0:cf99a7b873b3 103 LcdInit();
jont 0:cf99a7b873b3 104 LcdHomeTop();
jont 4:50203a03ccd2 105 LcdWriteTextLine("Squirt Tester");
jont 0:cf99a7b873b3 106 LcdHomeBottom();
jont 0:cf99a7b873b3 107 LcdWriteTextLine("Hello World");
jont 0:cf99a7b873b3 108 while(1) {
jont 0:cf99a7b873b3 109 siphon_data();
jont 0:cf99a7b873b3 110 myled = 1;
jont 0:cf99a7b873b3 111 wait(0.2);
jont 0:cf99a7b873b3 112 myled = 0;
jont 0:cf99a7b873b3 113 wait(0.2);
jont 0:cf99a7b873b3 114 LcdHomeBottom();
jont 0:cf99a7b873b3 115 sprintf(lineBuffer,"%.3f %04X %3d", coolant, int_coolant, counter_reed.read());
jont 0:cf99a7b873b3 116 LcdWriteTextLine(lineBuffer);
jont 0:cf99a7b873b3 117 }
jont 0:cf99a7b873b3 118 }
jont 0:cf99a7b873b3 119
jont 0:cf99a7b873b3 120 void siphon_data(){
jont 0:cf99a7b873b3 121 __disable_irq(); // Disable Interrupts
jont 0:cf99a7b873b3 122
jont 0:cf99a7b873b3 123 int_coolant = 0;
jont 0:cf99a7b873b3 124 if(comBuffIndex > 24){
jont 0:cf99a7b873b3 125 int_coolant = comBuff[23];
jont 0:cf99a7b873b3 126 int_coolant <<= 8;
jont 0:cf99a7b873b3 127 int_coolant |= comBuff[22];
jont 0:cf99a7b873b3 128 coolant = (int_coolant-320) * 0.05555;
jont 0:cf99a7b873b3 129 }
jont 0:cf99a7b873b3 130 __enable_irq();
jont 0:cf99a7b873b3 131
jont 0:cf99a7b873b3 132 }