Dual CANbus monitor and instrumentation cluster. Presently tuned for the Nissan Leaf EV.

Dependencies:   SPI_TFTx2_ILI9341 TFT_fonts TOUCH_TFTx2_ILI9341 mbed

Fork of CANary_corrupt by Tick Tock

After adding the LPC1768 platform, import as a program and do not select the "update to latest revision" box

User Guide

Eagle Schematic and Board design

/media/uploads/TickTock/canaryr6.zip

/media/uploads/TickTock/canary_sch.jpg

/media/uploads/TickTock/canaryr6brd.jpg

For LCD Rev 1.01:

/media/uploads/TickTock/lcdsch.jpg

For VCD Rev 2.00:

/media/uploads/TickTock/lcdr2.jpg

Parts List

qtyinstancepart #packagesupplierDescription
1BAT3Vhttp://www.ebay.com/itm/10x-CR2032-SMD-Battery-Holder-for-CR2032-Battery-/180938057979?pt=LH_DefaultDomain_0&hash=item2a20bfa8fbLithium 2032 coin battery holder
4C1-C4ECST1DC106R6032Tantalium capacitor 10uF
3FC1-FC3ZF1-20-01-T-WThttp://www.samtec.com/cable-systems/idc-ffc/ffc/zero-insertion.aspx20 conductor 1mm pitch flex cable connector (optional)
1FJ-20-R-08.00-4http://www.samtec.com/cable-systems/idc-ffc/ffc/zero-insertion.aspx8\" 20 conductor 1mm pitch flex connector, end reversed (optional)
2H1-H4(DON'T populate H1-H4 headers - solder mbed directly)
1H5http://www.ebay.com/itm/221186042943?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1497.l26491x12 .1\" pitch header (optional)
1H62x6 .1\" pitch header (optional)
2IC1,IC2VP230LMDSOP8http://www.ebay.com/itm/130488665247?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1497.l2649canbus transciever
1IC3LM1117-5VSOT2235V regulator
5JP*2 pin .1\" jumper header
1mbedLPC1768http://www.ebay.com/itm/200830573509?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1497.l2649mbed uC
2Q1,Q22N2222SOT23General purpose NPN transistor
1R1R393M120639K resistor
1R2R103M120610K resistor
4R4-R6R102M12061K resistor
1R3R500M120650 Ohm resistor
2TR1-TR5ZJYS81R5-2PL51TG01http://www.digikey.com/product-detail/en/ZJYS81R5-2PL51T-G01/445-2223-1-ND/765232CM Choke
1Z11N5340BGC1702-15http://www.ebay.com/itm/150878122425?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1497.l26496V, 5W Zener Diode
1Z1DC-DC conveterhttp://www.ebay.com/itm/251142727849?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1497.l264912V-7V, 3W DC-DC converter
1X1USBhttp://www.ebay.com/itm/New-Vertical-USB-2-0-A-pcb-connector-socket-USB-A-Type-/300553895292?pt=LH_DefaultDomain_0&hash=item45fa687d7cvertical USB connector
2LCD0,LCD1TFThttp://www.mikroe.com/add-on-boards/display/tft-proto/320x240 LCD with touch screen
1E0Enclosurehttp://www.shapeways.com/model/1077799/canary.html?li=user-profile&materialId=63d printed enclosure

Assembly

1) LCD Displays

I found ribbon cable is a nice way to organize the wires to the displays. There are two versions of the display and each must be wired differently. The original project used HW REV. 1.01. For that version, you'll need 12 conductors and I connected them in the following order:

1LED+
2LED-
3RST
4SDI
5WR/SCLK
6CS
7X+
8X-
9Y+
10Y-
11VDD
12GND

If, instead, you have HW REV 2.0, you will need 13 conductors with the following order:

1LED+
2LED-
3RST
4SDI
5RS (SCLK)
6WR (DC)
7CS
8X+
9X-
10Y+
11Y-
12VDD
13GND

First I connected all the GND connections (2 GND & IM0, IM1, IM3 for REV1.01 or 2 GND, RD, & IM0 for REV2.00). Do not connect the bottom GND until you have the ribbon cable connected. After making all the ribbon cable connections (connecting the GND of the ribbon cable to the bottom GND pad), solder the GND bar from the previous step to the back of the bottom GND connection. Finally, make a connection from the back side 3.3V pin to IM2 for REV1.01 or to IM1,IM2,&IM3 for REV2.00. Take a break and repeat for the second display.

Examples of REV1.01 boards:

/media/uploads/TickTock/lcdtop.jpg /media/uploads/TickTock/lcdbot.jpg

Examples of REV2.00:

/media/uploads/TickTock/rev2front.jpg /media/uploads/TickTock/rev2back.jpg

Once the two displays are complete combine all wires except CS0, CS1, X+, X-, Y+, and Y-. Connect X- of the left display to X+ of the right. Similarly connect Y- of the left display to Y+ of the right. Insulate any exposed wires.

2) PCB

Refer to the schematics to place all the components on the board. If you plan to install into the CANary 3D enclosure, DO NOT install the battery holder or the socket for the mbed and, instead, connect two wires to the VB and GND pads nearby. You will have to install the battery holder against the back wall to avoid interfering with the right-hand display and the mbed will have to be directly soldered. I have not found a socket with a low enough profile to fit in the space provided (depth of enclosure is limited by the space behind the center console). Also, I recommend keeping as much lead as possible on the Zener diode (bending it as shown to clear the back wall). Although it is operating well within parameters, the Zener gets quite hot during extended operation and the leads help dissipate the heat and keep it away from the PCB and other components.Update: Several Zeners have failed resulting in damage to some users boards so I recommend using a DC-DC converter instead to bring the 12V down to 7V.

/media/uploads/TickTock/pcbtop.jpg /media/uploads/TickTock/pcbbot.jpg

Once the PCB is populated, solder the LCDs to the PCB. CS0 connects to the right display and CS1 connects to the left. /media/uploads/TickTock/brddis.jpg

Update: The Zener diodes tended to fail after a few months so I am recommending removing them and replacing with a DC-DC converter. This will run cooler and waste less energy, too. To install, remove the left display panel to gain access to the Zener. From there, the Zener can be removed and it's pads used to connect to the DC-DC converter. I recommend setting the output voltage on the bench before installing since the trim pot is tricky to reach once installed. Set it to 7V. The input can be connected to the left pad previously occupied by the zener and the output can connect to the right. GND(-) can be connected to the bottom right pad on the 2x6 header below the flex cable connector. Make sure the GND wire lies flat so it doesn't interfere with the connection of the flex cable. /media/uploads/TickTock/dcdcinst2.jpg

Once soldered in place, the DC-DC converter can easily be mounted to the back wall with double sided tape above the battery holder. /media/uploads/TickTock/dcdcinst3.jpg

3) Testing

1)First step is to buzz out all connections from the LCDs to the pins in the main board
2)Next check the touch screen connections. On the main board, place an Ohm meter across X+ and X-. You should read 700 Ohms. Repeat for Y+ and Y-. Then test the resistance from X+ to Y+. With nothing touching the screens, it should read >100K Ohms and <1K when touching either screen.
3)When all connections are checked, solder in the mbed. Download and install the touch2 program http://mbed.org/users/TickTock/code/touch2/ to test the basic operation of the mbed and touch screens.
tips:
Touch screen is sensitive - excess flux on X+,X-,Y+,Y- connection on mbed can result in flakey operation
If touch is not working, double-check the LCD0_CS and LCD1_CS are not swapped. LCD0_CS must connect to the CS of the LCD that has X- & Y- connected to the mbed. LCD1_CS must connect to the CS of the LCD that has X+ & Y+ connected to the mbed.
4)Once touch2 works, it is time to connect to the OBD connector. I highly recommend double checking all connections from the OBD to the PCB with the cable in place before connecting to the Leaf. Buzz out all the pins in the OBS to make sure none are shorting to each other, Check that the 12V goes to the Zener (and nothing else) and the switched 12V to the resistor divider (and nothing else). Test the ground connection properly connects to ground and nothing else.
5)Once you are confident there are no shorts or wrong connections from the OBD connector, take a deep breath and plug it into your leaf. Touch2 program should come up and function. Unplug and install the latest CANary firmware. If you have the REV2.00 LCD boards, you will need to edit the precompile.h file in the TOUCH_TFTx2_w9341 library and set USE_ILI9341 to 1. Test all features before installing into the enclosure (gids, cellpair, menu system, logging) since installing and removing from the enclosure is a PITA.

/media/uploads/TickTock/pcbdone.jpg /media/uploads/TickTock/functioning.jpg

4) Enclosure

The 3D printer leaves a lot of powder behind - I used a strong spray of water to get it out of all the cracks. The enclosure comes with a rather rough finish. I recommend convincing yourself you like it, then simply lightly sand then paint before assembly. Sanding is very difficult - the nylon is very nicely fused and doesn't want to sand. I tried sandblasting and that didn't work either. I had some limited success with filler and then sanding, but only on the outside - it is too difficult to sand the face. /media/uploads/TickTock/enclosure.jpg

5) Final Assembly

Make sure you are well rested with lots of patience before attempting assembly. It is a puzzle figuring out how to get both displays and the PCB in place. Enclosure was too expensive for me to keep iterating to optimize for assembly. I ended up snipping the thin display posts shorter and using various tools to push the displays into place. Also, some USB connectors are taller than others. If you have one of the taller ones, you will have to deflect the back wall a bit while inserting the PCB (being careful not to bend the housing) to get it to it's opening in the back wall. Do use a screw in the provided post to secure the PCB as USB insertion will otherwise dislodge it.

I added an additional safety line which wraps around the center post to prevent the enclosure from becoming a projectile in the event of an accident. /media/uploads/TickTock/safety.jpg Installed: /media/uploads/TickTock/installed.jpg

Committer:
TickTock
Date:
Wed Jul 17 13:48:51 2013 +0000
Branch:
Metric
Revision:
122:138a40892a4c
Parent:
121:553faf139a20
Child:
123:a8c0042df617
Updated display formatting.  Temporarily dumping all 7bb messages to ascii log for exploration.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
TickTock 13:62e0f7f39ff5 1 // utility.cpp
TickTock 13:62e0f7f39ff5 2 #include "utility.h"
TickTock 13:62e0f7f39ff5 3
TickTock 13:62e0f7f39ff5 4 void mbed_reset();
TickTock 22:a43df3905863 5
TickTock 13:62e0f7f39ff5 6 void RTC_IRQHandler() {
TickTock 13:62e0f7f39ff5 7 timer.reset(); // zero ms at the-seconds-tic
TickTock 20:3bf176d14b14 8 canIdle=(++secsNoMsg>canTimeout)?true:false;
TickTock 20:3bf176d14b14 9 userIdle=(++secsNoTouch>userTimeout)?true:false;
TickTock 13:62e0f7f39ff5 10 LPC_RTC->ILR |= (1<<0); // clear interrupt to prepare for next
TickTock 34:4751a8259b18 11 tick=true;
garygid 69:6bfdfc002036 12 // will use this to generate a logTP() just before the next Message received.
garygid 69:6bfdfc002036 13 if( (time(NULL) % 60) == 0) ZeroSecTick = true; // gg - at 0-second of each minute
TickTock 13:62e0f7f39ff5 14 }
TickTock 13:62e0f7f39ff5 15
TickTock 37:fea2c1d52c5f 16 void RTC_Init (void) {
TickTock 13:62e0f7f39ff5 17 LPC_RTC->ILR=0x00; // set up the RTC interrupts
TickTock 13:62e0f7f39ff5 18 LPC_RTC->CIIR=0x01; // interrupts each second
TickTock 13:62e0f7f39ff5 19 LPC_RTC->CCR = 0x01; // Clock enable
TickTock 92:935adef49ea4 20 //NVIC_SetPriority( RTC_IRQn, 10 );
TickTock 13:62e0f7f39ff5 21 NVIC_EnableIRQ( RTC_IRQn );
TickTock 13:62e0f7f39ff5 22 }
TickTock 13:62e0f7f39ff5 23
TickTock 93:c2402e8cd0e2 24 void printMsg (char *msg) {
TickTock 13:62e0f7f39ff5 25 strcpy(displayLog[displayLoc],msg);
TickTock 13:62e0f7f39ff5 26 displayLoc=displayLoc>17?0:displayLoc+1;
TickTock 13:62e0f7f39ff5 27 }
TickTock 13:62e0f7f39ff5 28
TickTock 13:62e0f7f39ff5 29 void touch_ISR(){
TickTock 37:fea2c1d52c5f 30 //LPC_GPIOINT->IO2IntClr = (LPC_GPIOINT->IO2IntStatR | LPC_GPIOINT->IO2IntStatF); // seems to work without so maybe not necessary (performed inInterruptIn handler?)
TickTock 35:5acbd8a64a89 31 touched=true; // just set flag - touch screen algorythm is long and we don't want to block other interrupts
TickTock 13:62e0f7f39ff5 32 }
TickTock 13:62e0f7f39ff5 33
TickTock 13:62e0f7f39ff5 34 unsigned short getTimeStamp() {
TickTock 13:62e0f7f39ff5 35 unsigned short msec = timer.read_ms() ; // read ms from the timer
TickTock 13:62e0f7f39ff5 36 unsigned long secs = time(NULL); // seconds past 12:00:00 AM 1 Jan 1900
TickTock 13:62e0f7f39ff5 37 unsigned short isecs = secs%60 ; // modulo 60 for 0-59 seconds from RTC
TickTock 13:62e0f7f39ff5 38 return ((isecs<<10)+msec) ; // return the two byte time stamp
TickTock 13:62e0f7f39ff5 39 }
TickTock 13:62e0f7f39ff5 40
TickTock 13:62e0f7f39ff5 41 void logCan (char mType, CANMessage canRXmsg) {
TickTock 85:5c27e88b3fbe 42
TickTock 85:5c27e88b3fbe 43 // re-arranged to put static first
TickTock 85:5c27e88b3fbe 44 static unsigned char ii = 0;
TickTock 85:5c27e88b3fbe 45 static unsigned char lasti = 0; // indexindex
TickTock 85:5c27e88b3fbe 46 static unsigned char bdi=0;
TickTock 85:5c27e88b3fbe 47 static signed short imotorRPM = 0;
TickTock 85:5c27e88b3fbe 48 static unsigned short nLost = 0; // gg - overrun
TickTock 85:5c27e88b3fbe 49
TickTock 83:52b1f330a62d 50 char sTemp[40];
TickTock 92:935adef49ea4 51 unsigned char changed;
TickTock 108:29b5a760adc2 52 unsigned short i,j,k;
TickTock 119:0e2b641e98a2 53 signed short packV_x2;
TickTock 119:0e2b641e98a2 54 signed short packA_x2;
TickTock 40:0e6e71a7323f 55 signed long imWs_x4;
garygid 87:46ac3f2519d6 56 unsigned short ts;
TickTock 85:5c27e88b3fbe 57
TickTock 36:dbd39c315258 58 secsNoMsg=0; // reset deadman switch
garygid 87:46ac3f2519d6 59 if(debugMode||(skin==ggSkin)){
garygid 87:46ac3f2519d6 60 // code to insert actual number of dropped frames for overrun debug - skiped in normal mode to keep logcan short
TickTock 85:5c27e88b3fbe 61 if(logOpen){
TickTock 106:f016912a03db 62 // check to see if buffer is already full (read - write) = 1
TickTock 106:f016912a03db 63 // actually the last buffer location cannot be used because then
TickTock 106:f016912a03db 64 // the buffer would look empty after writePointer++
TickTock 106:f016912a03db 65
TickTock 106:f016912a03db 66 //if (((writePointer+maxBufLen-readPointer)%maxBufLen)>(maxBufLen/16)) // modulo is slow?
TickTock 106:f016912a03db 67
TickTock 106:f016912a03db 68 // pointers are 0 through maxBufLen-1
TickTock 106:f016912a03db 69 if( (readPointer - writePointer) == 1 || (writePointer - readPointer) == (maxBufLen - 1)) {
TickTock 106:f016912a03db 70 // the buffer is "full", so Lose this message
TickTock 85:5c27e88b3fbe 71
TickTock 106:f016912a03db 72 // point to the last-stored message
TickTock 106:f016912a03db 73 int tempWritePointer = writePointer - 1 ;
TickTock 106:f016912a03db 74 if( tempWritePointer < 0 ) tempWritePointer = maxBufLen - 1;
TickTock 106:f016912a03db 75 char strLost[9] ;
TickTock 106:f016912a03db 76
TickTock 106:f016912a03db 77 if( nLost == 0 ) {
TickTock 106:f016912a03db 78 // this is the first message lost
TickTock 106:f016912a03db 79 // and we must overwrite the last message with an FFE comment message
TickTock 106:f016912a03db 80 // So, there will be two messages lost as the comment message is laid in.
TickTock 106:f016912a03db 81 nLost = 2;
TickTock 106:f016912a03db 82 sprintf(strLost,"%s","Lost0002"); // indicate two messages lost
TickTock 85:5c27e88b3fbe 83
TickTock 106:f016912a03db 84 // overlay the last message with a "Lost0002" comment
TickTock 106:f016912a03db 85 writeBuffer[tempWritePointer][0]=0;
TickTock 117:49883c779a74 86 // leave the ts of the overlaid message
TickTock 117:49883c779a74 87 //writeBuffer[tempWritePointer][1]=(ts&0xff00)>>8; // Time Stamp (2 bytes_
TickTock 117:49883c779a74 88 //writeBuffer[tempWritePointer][2]=(ts&0x00ff);
TickTock 106:f016912a03db 89 // force the MsgID to an Event Message
TickTock 106:f016912a03db 90 writeBuffer[tempWritePointer][3]=0xfe; // MsgID, low byte
TickTock 106:f016912a03db 91 writeBuffer[tempWritePointer][4]=0xff; // Len nibble, and MsgID high nibble
TickTock 106:f016912a03db 92 // lay in the "Lost0002" text
TickTock 106:f016912a03db 93 for(i=5;i<13;i++){
TickTock 106:f016912a03db 94 writeBuffer[tempWritePointer][i]= strLost[i-5];
TickTock 85:5c27e88b3fbe 95 }
TickTock 85:5c27e88b3fbe 96 } else {
TickTock 106:f016912a03db 97 // at least one message was previously lost
TickTock 106:f016912a03db 98 // increment the loat counter
TickTock 106:f016912a03db 99 nLost += 1;
garygid 87:46ac3f2519d6 100
TickTock 106:f016912a03db 101 // lay the new count into the comment
TickTock 106:f016912a03db 102 sprintf(strLost,"%04d",nLost);
TickTock 106:f016912a03db 103 for(i=9;i<13;i++){
TickTock 106:f016912a03db 104 writeBuffer[tempWritePointer][i]= strLost[i-9];
TickTock 85:5c27e88b3fbe 105 }
TickTock 106:f016912a03db 106 }
TickTock 106:f016912a03db 107 } else {
TickTock 106:f016912a03db 108 // there is room to insert the message
TickTock 106:f016912a03db 109 // get it inserted quickly
TickTock 106:f016912a03db 110 ts=getTimeStamp();
TickTock 106:f016912a03db 111 writeBuffer[writePointer][0]=mType;
TickTock 106:f016912a03db 112 writeBuffer[writePointer][1]=(ts&0xff00)>>8; // Time Stamp (2 bytes_
TickTock 106:f016912a03db 113 writeBuffer[writePointer][2]=(ts&0x00ff);
TickTock 106:f016912a03db 114 writeBuffer[writePointer][3]=canRXmsg.id&0xff; // MsgID, low byte
TickTock 106:f016912a03db 115 char sLen = canRXmsg.len ;
TickTock 106:f016912a03db 116 writeBuffer[writePointer][4]=(canRXmsg.id>>8)+(sLen<<4); // Len nibble, and MsgID high nibble
TickTock 106:f016912a03db 117 for(i=0;i<8;i++){ // Is there a better way to do this? (writeBuffer[writePointer][i]=canRXmsg.data?)
TickTock 106:f016912a03db 118 if(i<sLen)
TickTock 106:f016912a03db 119 writeBuffer[writePointer][i+5]=canRXmsg.data[i];
TickTock 106:f016912a03db 120 else // i>=sLen
TickTock 106:f016912a03db 121 // force unused data bytes to FF for CAN-Do compatibility
TickTock 106:f016912a03db 122 writeBuffer[writePointer][i+5]=0xFF;
TickTock 85:5c27e88b3fbe 123 }
TickTock 106:f016912a03db 124
TickTock 106:f016912a03db 125 //--------------
TickTock 106:f016912a03db 126 // Note, this is not protected from the interrupt.
TickTock 106:f016912a03db 127 // Due to the nLost code above, this no longer
TickTock 106:f016912a03db 128 // overflows to writePointer = readPointer
TickTock 106:f016912a03db 129 // which would make the buffer look empty
TickTock 106:f016912a03db 130 if (++writePointer >= maxBufLen) {
TickTock 106:f016912a03db 131 writePointer = 0;
TickTock 106:f016912a03db 132 led3 = !led3;
TickTock 106:f016912a03db 133 }
TickTock 106:f016912a03db 134 //--------------
TickTock 106:f016912a03db 135 // log a local message if we had lost messages. gg - logcan
TickTock 106:f016912a03db 136 if( nLost > 0 ) {
TickTock 106:f016912a03db 137 // We previously lost messages that did not get into the buffer
TickTock 106:f016912a03db 138 sprintf(sTemp,"-- Lost %d Messages.\n", nLost);
TickTock 106:f016912a03db 139 printMsg(sTemp); // write buffer overrun
TickTock 106:f016912a03db 140 //spkr.beep(500,0.25);
TickTock 106:f016912a03db 141
TickTock 106:f016912a03db 142 nLost = 0 ;
TickTock 106:f016912a03db 143 }
TickTock 106:f016912a03db 144 //--------------
TickTock 83:52b1f330a62d 145 }
TickTock 85:5c27e88b3fbe 146 }
TickTock 92:935adef49ea4 147 }else{ // not debugMode - keep code short
TickTock 85:5c27e88b3fbe 148 if(logOpen){
TickTock 106:f016912a03db 149 NVIC_DisableIRQ(CAN_IRQn); // Block interrupts until write pointer assigned
TickTock 106:f016912a03db 150 int localWritePointer = writePointer++; // create local copy to make logCan reentrant
TickTock 106:f016912a03db 151 // note that the static variables do not prevent safe reentry
TickTock 106:f016912a03db 152 // since they are only used for msgId<0x800 which will never interrupt
TickTock 106:f016912a03db 153 // another msgId<0x800 (both CANbusses are same priority)
TickTock 106:f016912a03db 154 if (writePointer >= maxBufLen) {
TickTock 106:f016912a03db 155 writePointer = 0;
TickTock 106:f016912a03db 156 led3 = !led3;
TickTock 106:f016912a03db 157 }
TickTock 106:f016912a03db 158 NVIC_EnableIRQ(CAN_IRQn); // Unblock interrupts once local pointer set and global pointer incremented
TickTock 106:f016912a03db 159 ts=getTimeStamp();
TickTock 106:f016912a03db 160 writeBuffer[localWritePointer][0]=mType;
TickTock 106:f016912a03db 161 writeBuffer[localWritePointer][1]=(ts&0xff00)>>8;
TickTock 106:f016912a03db 162 writeBuffer[localWritePointer][2]=(ts&0x00ff);
TickTock 106:f016912a03db 163 writeBuffer[localWritePointer][3]=canRXmsg.id&0xff;
TickTock 106:f016912a03db 164 writeBuffer[localWritePointer][4]=(canRXmsg.id>>8)+(canRXmsg.len<<4);
TickTock 106:f016912a03db 165 for(i=5;i<13;i++){ // Is there a better way to do this? (writeBuffer[localWritePointer][5]=canRXmsg.data?)
TickTock 106:f016912a03db 166 writeBuffer[localWritePointer][i]=canRXmsg.data[i-5];
TickTock 106:f016912a03db 167 }
TickTock 117:49883c779a74 168 if (writePointer==readPointer) {
TickTock 117:49883c779a74 169 // Just caught up to read pointer
TickTock 117:49883c779a74 170 printMsg("Write buffer overrun.\n"); // write buffer overrun
TickTock 117:49883c779a74 171 spkr.beep(500,0.25);
TickTock 117:49883c779a74 172 }
TickTock 13:62e0f7f39ff5 173 }
TickTock 37:fea2c1d52c5f 174 }
TickTock 40:0e6e71a7323f 175
TickTock 83:52b1f330a62d 176 if(canRXmsg.id<0x800){ // Block FFE and FFF messages
TickTock 83:52b1f330a62d 177 if(indexLastMsg[canRXmsg.id]==0) { //Check if no entry
TickTock 92:935adef49ea4 178 //ii=ii<99?ii+1:0; // Should never wrap - less than 100 different messages ever used
garygid 87:46ac3f2519d6 179 if(ii<99) {
TickTock 92:935adef49ea4 180 //indexLastMsg[canRXmsg.id]=ii; //Create entry if first message
garygid 87:46ac3f2519d6 181 indexLastMsg[canRXmsg.id]=++ii; //Create entry for first MsgID occurance
garygid 87:46ac3f2519d6 182 // ii max is 99 here
garygid 87:46ac3f2519d6 183 } else {
garygid 87:46ac3f2519d6 184 // the ii array is full, more than 100 MsgIDs found
garygid 87:46ac3f2519d6 185 if(ii==99) {
TickTock 92:935adef49ea4 186 ii++; // step to 100 to log only one error
TickTock 116:5cd72bae7c12 187 printMsg("MsgID buffer overrun.\n"); // write buffer overrun
garygid 87:46ac3f2519d6 188 }
garygid 87:46ac3f2519d6 189 }
TickTock 83:52b1f330a62d 190 }
garygid 87:46ac3f2519d6 191
garygid 87:46ac3f2519d6 192 //----------------
TickTock 85:5c27e88b3fbe 193 if(dMode[0]==changedScreen||dMode[1]==changedScreen){// Skip if not using (for execution speed)
TickTock 83:52b1f330a62d 194 changed=msgChanged[indexLastMsg[canRXmsg.id]];
TickTock 85:5c27e88b3fbe 195 // This is cleared in the main loop when reset button is touched
TickTock 92:935adef49ea4 196 for(i=0;i<8;i++){
TickTock 83:52b1f330a62d 197 if(lastMsg[indexLastMsg[canRXmsg.id]].data[i]!=canRXmsg.data[i]){
TickTock 83:52b1f330a62d 198 changed |= 1<<i;
garygid 80:24f1793171e7 199 }
TickTock 13:62e0f7f39ff5 200 }
TickTock 83:52b1f330a62d 201 msgChanged[indexLastMsg[canRXmsg.id]]=changed;
TickTock 13:62e0f7f39ff5 202 }
garygid 80:24f1793171e7 203
TickTock 83:52b1f330a62d 204 lastMsg[indexLastMsg[canRXmsg.id]]=canRXmsg; //Store in table
garygid 87:46ac3f2519d6 205
garygid 87:46ac3f2519d6 206 //-------------------
TickTock 83:52b1f330a62d 207 //Miscellaneous on-recieve operations below
TickTock 93:c2402e8cd0e2 208 if((mType==1)&&(canRXmsg.id==0x7bb)){ // is battery data? Need to store all responses
TickTock 83:52b1f330a62d 209 if(canRXmsg.data[0]<0x20){
TickTock 115:549410af477d 210 if(canRXmsg.data[3]==1){//Group 1 data
TickTock 121:553faf139a20 211 bdi=BatDataBaseG1; // index offset for Group 1 data
TickTock 115:549410af477d 212 if(debugMode){
TickTock 116:5cd72bae7c12 213 printMsg(" Getting Group 1 data\n");
TickTock 115:549410af477d 214 }
TickTock 115:549410af477d 215
TickTock 115:549410af477d 216 }else if(canRXmsg.data[3]==2){//Group 2 = cellpair data
TickTock 121:553faf139a20 217 bdi=BatDataBaseG2; // index offset for CP data
TickTock 97:a25940fd7b5b 218 if(debugMode){
TickTock 116:5cd72bae7c12 219 printMsg(" Getting cell pair data\n");
TickTock 97:a25940fd7b5b 220 }
garygid 87:46ac3f2519d6 221
TickTock 115:549410af477d 222 }else if(canRXmsg.data[3]==3){//Group 3 data
TickTock 121:553faf139a20 223 bdi=BatDataBaseG3; // index offset for Group 3 data
TickTock 115:549410af477d 224 if(debugMode){
TickTock 116:5cd72bae7c12 225 printMsg(" Getting Group 3 data\n");
TickTock 115:549410af477d 226 }
TickTock 115:549410af477d 227
garygid 87:46ac3f2519d6 228 }else if(canRXmsg.data[3]==4){//Group 4 = temperature data
TickTock 121:553faf139a20 229 bdi=BatDataBaseG4; // index offset for Temperature data
TickTock 97:a25940fd7b5b 230 if(debugMode){
TickTock 116:5cd72bae7c12 231 printMsg(" Getting temperature data\n");
TickTock 97:a25940fd7b5b 232 }
garygid 87:46ac3f2519d6 233
garygid 87:46ac3f2519d6 234 }else if(canRXmsg.data[3]==5){//Group 5 data
TickTock 121:553faf139a20 235 bdi=BatDataBaseG5; // index offset for Group 5 data
TickTock 97:a25940fd7b5b 236 if(debugMode){
TickTock 116:5cd72bae7c12 237 printMsg(" Getting Group 5 data\n");
TickTock 97:a25940fd7b5b 238 }
garygid 87:46ac3f2519d6 239
TickTock 121:553faf139a20 240 }else if(canRXmsg.data[3]==6){//Group 6 data = shunt data
TickTock 121:553faf139a20 241 bdi=BatDataBaseG6; // index offset for Group 6 data
TickTock 121:553faf139a20 242 if(debugMode){
TickTock 121:553faf139a20 243 printMsg(" Getting Group 6 data\n");
TickTock 121:553faf139a20 244 }
TickTock 121:553faf139a20 245
TickTock 83:52b1f330a62d 246 }else bdi=0xff; // ignore other messages (for now)
TickTock 83:52b1f330a62d 247 lasti=0;
TickTock 83:52b1f330a62d 248 }
garygid 87:46ac3f2519d6 249
TickTock 83:52b1f330a62d 250 if(bdi<0xff){
TickTock 83:52b1f330a62d 251 i=canRXmsg.data[0]&0x0f; //lower nibble of D0 is index
TickTock 83:52b1f330a62d 252 if(lasti>i){ //detect rollover and offset index appropriately
TickTock 115:549410af477d 253 bdi += 0x10; // for CP data
TickTock 83:52b1f330a62d 254 }
TickTock 83:52b1f330a62d 255 lasti=i; //remember the msb to detect rollover next time around
TickTock 83:52b1f330a62d 256 i+=bdi;
garygid 87:46ac3f2519d6 257 //-------
TickTock 121:553faf139a20 258 //-------
TickTock 121:553faf139a20 259 i*=7;
TickTock 121:553faf139a20 260 if(i+6 < BatDataBufMax) {
TickTock 121:553faf139a20 261 battData[i+0]=canRXmsg.data[1];
TickTock 121:553faf139a20 262 battData[i+1]=canRXmsg.data[2];
TickTock 121:553faf139a20 263 battData[i+2]=canRXmsg.data[3];
TickTock 121:553faf139a20 264 battData[i+3]=canRXmsg.data[4];
TickTock 121:553faf139a20 265 battData[i+4]=canRXmsg.data[5];
TickTock 121:553faf139a20 266 battData[i+5]=canRXmsg.data[6];
TickTock 121:553faf139a20 267 battData[i+6]=canRXmsg.data[7];
TickTock 121:553faf139a20 268 }
TickTock 121:553faf139a20 269 if(i==(BatDataBaseG6+3)*7){ // All data loaded
TickTock 83:52b1f330a62d 270 logCP=yesBattLog; // Only log if logging enabled
TickTock 83:52b1f330a62d 271 showCP=true; // Always show
TickTock 121:553faf139a20 272
TickTock 121:553faf139a20 273 // Find hottest temperature by finding smallest ADC value
TickTock 102:fd19f777a0b4 274 // 2013 models only have three sensors
TickTock 108:29b5a760adc2 275 k=battData[(BatDataBaseG4*7)+3]*0x100+battData[(BatDataBaseG4*7)+4];
TickTock 107:e9be732c1ad4 276 j=battData[(BatDataBaseG4*7)+6]*0x100+battData[(BatDataBaseG4*7)+7];
TickTock 115:549410af477d 277 if(j<k)k=j;
TickTock 107:e9be732c1ad4 278 j=battData[(BatDataBaseG4*7)+9]*0x100+battData[(BatDataBaseG4*7)+10];
TickTock 115:549410af477d 279 if(j<k)k=j;
TickTock 107:e9be732c1ad4 280 j=battData[(BatDataBaseG4*7)+12]*0x100+battData[(BatDataBaseG4*7)+13];
TickTock 115:549410af477d 281 if(j<k)k=j;
TickTock 108:29b5a760adc2 282 //interpolate from lookup table
TickTock 115:549410af477d 283 unsigned short temp_adc[8] = {1000,589,487,401,365,340,309,000};
TickTock 115:549410af477d 284 float temp_C[8] = { -27, 13, 23, 32, 36, 39, 43, 76};
TickTock 108:29b5a760adc2 285 char ii=0;
TickTock 108:29b5a760adc2 286 while(k<=temp_adc[++ii]) { } // Find section in table
TickTock 108:29b5a760adc2 287 maxTemp=(float)(k-temp_adc[ii]);
TickTock 108:29b5a760adc2 288 maxTemp/=(float)(temp_adc[ii-1]-temp_adc[ii]);
TickTock 108:29b5a760adc2 289 maxTemp*=(temp_C[ii-1]-temp_C[ii]);
TickTock 108:29b5a760adc2 290 maxTemp+=temp_C[ii];
TickTock 108:29b5a760adc2 291
TickTock 121:553faf139a20 292 // Get state of health
TickTock 102:fd19f777a0b4 293 SOH_x100=battData[(BatDataBaseG1*7)+29]*0x100+battData[(BatDataBaseG1*7)+30];
TickTock 102:fd19f777a0b4 294 Ah_x10000=battData[(BatDataBaseG1*7)+36]*0x10000+battData[(BatDataBaseG1*7)+37]*0x100+battData[(BatDataBaseG1*7)+38];
TickTock 102:fd19f777a0b4 295 SOC_x10000=battData[(BatDataBaseG1*7)+32]*0x10000+battData[(BatDataBaseG1*7)+33]*0x100+battData[(BatDataBaseG1*7)+34];
TickTock 121:553faf139a20 296
TickTock 121:553faf139a20 297 // Save shunt data
TickTock 121:553faf139a20 298 for(j=0; j<24; j++){
TickTock 121:553faf139a20 299 shunt[j*4+0]=battData[BatDataBaseG6*7+j+3]&0x08;
TickTock 121:553faf139a20 300 shunt[j*4+1]=battData[BatDataBaseG6*7+j+3]&0x04;
TickTock 121:553faf139a20 301 shunt[j*4+2]=battData[BatDataBaseG6*7+j+3]&0x02;
TickTock 121:553faf139a20 302 shunt[j*4+3]=battData[BatDataBaseG6*7+j+3]&0x01;
TickTock 121:553faf139a20 303 }
TickTock 83:52b1f330a62d 304 }
TickTock 83:52b1f330a62d 305 }
TickTock 83:52b1f330a62d 306 }else if((mType==1)&&(canRXmsg.id==0x1db)){ //Battery Volts and Amps
TickTock 119:0e2b641e98a2 307 packV_x2=((canRXmsg.data[2]<<2)|(canRXmsg.data[3]>>6)); // 1 LSB = 0.5V
TickTock 119:0e2b641e98a2 308 packA_x2=((canRXmsg.data[0]<<3)|(canRXmsg.data[1]>>5)); // 1 LSB = 0.5A
TickTock 119:0e2b641e98a2 309 if(packA_x2>0x03ff){
TickTock 119:0e2b641e98a2 310 packA_x2|=0xf800;//extend sign;
TickTock 83:52b1f330a62d 311 }
TickTock 119:0e2b641e98a2 312 packA_x2 -= 1; //Slight correction to value required (unique to my Leaf?)
TickTock 121:553faf139a20 313 if (-packA_x2<Imin){
TickTock 121:553faf139a20 314 Imin=-packA_x2;
TickTock 121:553faf139a20 315 } else if (-packA_x2>Imax){
TickTock 121:553faf139a20 316 Imax=-packA_x2;
TickTock 121:553faf139a20 317 }
TickTock 119:0e2b641e98a2 318 imWs_x4 = packV_x2; // Volts*milliSeconds*2
TickTock 119:0e2b641e98a2 319 imWs_x4 *= -packA_x2; // milliWattseconds*4
TickTock 118:02010b18efdc 320 mWs_x4 += imWs_x4; // total mWs_x4
TickTock 121:553faf139a20 321 float temp;
TickTock 121:553faf139a20 322 temp = Resr;
TickTock 121:553faf139a20 323 temp *= (float) -packA_x2;
TickTock 121:553faf139a20 324 temp += (float) packV_x2;
TickTock 120:041edeec08f5 325 if(temp>curRmax){
TickTock 120:041edeec08f5 326 curRmax=temp;
TickTock 120:041edeec08f5 327 } else if(temp<curRmin){
TickTock 120:041edeec08f5 328 curRmin=temp;
TickTock 120:041edeec08f5 329 }
TickTock 121:553faf139a20 330 temp = Resr-0.001;
TickTock 121:553faf139a20 331 temp *= (float) -packA_x2;
TickTock 121:553faf139a20 332 temp += (float) packV_x2;
TickTock 120:041edeec08f5 333 if(temp>redRmax){
TickTock 120:041edeec08f5 334 redRmax=temp;
TickTock 120:041edeec08f5 335 } else if(temp<redRmin){
TickTock 120:041edeec08f5 336 redRmin=temp;
TickTock 120:041edeec08f5 337 }
TickTock 121:553faf139a20 338 temp = Resr+0.001;
TickTock 121:553faf139a20 339 temp *= (float) -packA_x2;
TickTock 121:553faf139a20 340 temp += (float) packV_x2;
TickTock 120:041edeec08f5 341 if(temp>incRmax){
TickTock 120:041edeec08f5 342 incRmax=temp;
TickTock 120:041edeec08f5 343 } else if(temp<incRmin){
TickTock 120:041edeec08f5 344 incRmin=temp;
TickTock 120:041edeec08f5 345 }
TickTock 118:02010b18efdc 346 numWsamples++;
TickTock 83:52b1f330a62d 347 }else if((mType==1)&&(canRXmsg.id==0x1da)){ //Motor Speed
TickTock 83:52b1f330a62d 348 imotorRPM=((canRXmsg.data[4]<<8)|(canRXmsg.data[5]));
TickTock 83:52b1f330a62d 349 if(imotorRPM<0){ // take absolute value
TickTock 83:52b1f330a62d 350 imotorRPM=-imotorRPM;
TickTock 83:52b1f330a62d 351 }
TickTock 83:52b1f330a62d 352 motorRPM+=imotorRPM;
TickTock 92:935adef49ea4 353 numSsamples++;
TickTock 37:fea2c1d52c5f 354 }
TickTock 37:fea2c1d52c5f 355 }
TickTock 13:62e0f7f39ff5 356 }
TickTock 13:62e0f7f39ff5 357
garygid 87:46ac3f2519d6 358 //-----------------------------
TickTock 13:62e0f7f39ff5 359 void logTS () {
TickTock 13:62e0f7f39ff5 360 CANMessage tsMsg;
TickTock 13:62e0f7f39ff5 361 unsigned long secs = time(NULL); // seconds past 12:00:00 AM 1 Jan 1900
garygid 69:6bfdfc002036 362 // NOTE: In Mbed, I believe that this is seconds past start of 1970, not 1900
garygid 69:6bfdfc002036 363 // but this is good, since seconds past 1970 is what CAN-Do expects. GG - Date Time
TickTock 13:62e0f7f39ff5 364 tsMsg.id=0xfff;
TickTock 13:62e0f7f39ff5 365 tsMsg.len=0xf;
garygid 69:6bfdfc002036 366 tsMsg.data[0]=secs&0xff;
TickTock 13:62e0f7f39ff5 367 tsMsg.data[1]=(secs>>8)&0xff;
TickTock 13:62e0f7f39ff5 368 tsMsg.data[2]=(secs>>16)&0xff;
garygid 69:6bfdfc002036 369 tsMsg.data[3]=(secs>>24)&0xff;
garygid 69:6bfdfc002036 370 tsMsg.data[4]=0; // 0xff; gg - Date Time
garygid 69:6bfdfc002036 371 tsMsg.data[5]=0; // 0xff; for CAN-Do
garygid 69:6bfdfc002036 372 tsMsg.data[6]=0; // 0xff;
TickTock 13:62e0f7f39ff5 373 tsMsg.data[7]=0xff;
garygid 69:6bfdfc002036 374 logCan(0,tsMsg); // Date-Time
garygid 69:6bfdfc002036 375 }
garygid 69:6bfdfc002036 376
TickTock 93:c2402e8cd0e2 377 void logEvent (char * errMsg) {
garygid 69:6bfdfc002036 378 // log CAN-Do 8-character Pseudo Message
garygid 69:6bfdfc002036 379 CANMessage tsMsg;
garygid 69:6bfdfc002036 380 tsMsg.id=0xffe; // pseudo Message to CAN-Do log
garygid 69:6bfdfc002036 381 tsMsg.len=0xf;
TickTock 92:935adef49ea4 382 int iMsgLen = strlen(errMsg);
garygid 69:6bfdfc002036 383 // 8 character message compatible with CAN-Do
TickTock 92:935adef49ea4 384 for(int i=0; i<8; i++){
garygid 69:6bfdfc002036 385 tsMsg.data[i]=' ';
TickTock 92:935adef49ea4 386 if( i < iMsgLen ) tsMsg.data[i]=errMsg[i];
garygid 69:6bfdfc002036 387 }
garygid 69:6bfdfc002036 388 logCan(0,tsMsg); // FFE Comment Message
TickTock 13:62e0f7f39ff5 389 }
TickTock 13:62e0f7f39ff5 390
TickTock 78:a383971fe02f 391 void sendReq() {
TickTock 78:a383971fe02f 392 static char data[8] = {0x02, 0x21, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff};
TickTock 78:a383971fe02f 393 if(reqMsgCnt<99){
TickTock 92:935adef49ea4 394 switch (reqMsgCnt){
TickTock 102:fd19f777a0b4 395 case BatDataBaseG1:
TickTock 78:a383971fe02f 396 can1.monitor(false); // set to active mode
TickTock 78:a383971fe02f 397 can1SleepMode = 0; // enable TX
TickTock 83:52b1f330a62d 398 data[0]=0x02; //change to request group 1
TickTock 78:a383971fe02f 399 data[1]=0x21;
TickTock 78:a383971fe02f 400 data[2]=0x01;
TickTock 78:a383971fe02f 401 break;
TickTock 102:fd19f777a0b4 402 case BatDataBaseG2: // group 1 has 6 frames
TickTock 83:52b1f330a62d 403 data[0]=0x02; //change to request group 2 (cp data)
TickTock 78:a383971fe02f 404 data[1]=0x21;
TickTock 78:a383971fe02f 405 data[2]=0x02;
TickTock 78:a383971fe02f 406 break;
TickTock 102:fd19f777a0b4 407 case BatDataBaseG3: // group 2 has 29 frames
TickTock 83:52b1f330a62d 408 data[0]=0x02; //change to request group 3
TickTock 78:a383971fe02f 409 data[1]=0x21;
TickTock 78:a383971fe02f 410 data[2]=0x03;
TickTock 78:a383971fe02f 411 break;
TickTock 102:fd19f777a0b4 412 case BatDataBaseG4: // group 3 has 5 frames
TickTock 83:52b1f330a62d 413 data[0]=0x02; //change to request group 4 (temperature)
TickTock 78:a383971fe02f 414 data[1]=0x21;
TickTock 78:a383971fe02f 415 data[2]=0x04;
TickTock 78:a383971fe02f 416 break;
TickTock 102:fd19f777a0b4 417 case BatDataBaseG5: // group 4 has 3 frames
TickTock 83:52b1f330a62d 418 data[0]=0x02; //change to request group 5
TickTock 78:a383971fe02f 419 data[1]=0x21;
TickTock 78:a383971fe02f 420 data[2]=0x05;
TickTock 78:a383971fe02f 421 break;
TickTock 121:553faf139a20 422 case BatDataBaseG6: // group 4 has 3 frames
TickTock 121:553faf139a20 423 data[0]=0x02; //change to request group 5
TickTock 121:553faf139a20 424 data[1]=0x21;
TickTock 121:553faf139a20 425 data[2]=0x06;
TickTock 121:553faf139a20 426 break;
TickTock 121:553faf139a20 427 case BatDataBaseG7: // group 5 has 11 frames
TickTock 78:a383971fe02f 428 reqMsgCnt = 99;
TickTock 120:041edeec08f5 429 can1SleepMode = VP230Sleep; // disable TX
TickTock 78:a383971fe02f 430 can1.monitor(true); // set to snoop mode
TickTock 83:52b1f330a62d 431 msgReq.detach(); // stop ticker
TickTock 78:a383971fe02f 432 default:
TickTock 78:a383971fe02f 433 data[0]=0x30; //change to request next line message
TickTock 78:a383971fe02f 434 data[1]=0x01;
TickTock 78:a383971fe02f 435 data[2]=0x00;
TickTock 78:a383971fe02f 436 }
TickTock 78:a383971fe02f 437 can1.write(CANMessage(0x79b, data, 8));
TickTock 92:935adef49ea4 438 reqMsgCnt++;
TickTock 78:a383971fe02f 439 }
TickTock 78:a383971fe02f 440 }
TickTock 78:a383971fe02f 441
TickTock 78:a383971fe02f 442 void autoPollISR(){
TickTock 117:49883c779a74 443 //char sTemp[40]; // just for debug
TickTock 117:49883c779a74 444 //sprintf(sTemp,"Requesting cp data\n"); // just for debug
TickTock 117:49883c779a74 445 //printMsg(sTemp); // just for debug
TickTock 78:a383971fe02f 446 reqMsgCnt = 0; //reset message counter
TickTock 94:c3a14b3975d6 447 msgReq.attach(&sendReq,0.015);
TickTock 78:a383971fe02f 448 }
garygid 87:46ac3f2519d6 449
TickTock 36:dbd39c315258 450 void playbackISR() { //Used for autoplayback
TickTock 36:dbd39c315258 451 step=true;
TickTock 36:dbd39c315258 452 }
TickTock 36:dbd39c315258 453
TickTock 36:dbd39c315258 454 void doNothing(){ //CAN deattach work-around
TickTock 36:dbd39c315258 455 }
TickTock 36:dbd39c315258 456
TickTock 13:62e0f7f39ff5 457 void recieve1() {
TickTock 13:62e0f7f39ff5 458 CANMessage msg1;
TickTock 13:62e0f7f39ff5 459 can1.read(msg1);
garygid 69:6bfdfc002036 460
garygid 69:6bfdfc002036 461 if( ZeroSecTick ) { ZeroSecTick = false; logTS(); } // gg - 0-second EV bus
TickTock 106:f016912a03db 462 if(msg1.id>0) {
TickTock 106:f016912a03db 463 logCan(1, msg1); // EVcan Message Received
TickTock 106:f016912a03db 464 led1 = !led1;
TickTock 106:f016912a03db 465 }
TickTock 13:62e0f7f39ff5 466 }
TickTock 13:62e0f7f39ff5 467
TickTock 13:62e0f7f39ff5 468 void recieve2() {
TickTock 13:62e0f7f39ff5 469 CANMessage msg2;
TickTock 13:62e0f7f39ff5 470 can2.read(msg2);
garygid 69:6bfdfc002036 471
garygid 69:6bfdfc002036 472 if( ZeroSecTick ) { ZeroSecTick = false; logTS(); } // gg - 0-second EV bus
garygid 69:6bfdfc002036 473
TickTock 106:f016912a03db 474 if(msg2.id>0) {
TickTock 106:f016912a03db 475 logCan(2, msg2); // CARcan Message Received
TickTock 106:f016912a03db 476 led2 = !led2;
TickTock 106:f016912a03db 477 }
TickTock 13:62e0f7f39ff5 478 }
TickTock 13:62e0f7f39ff5 479
TickTock 22:a43df3905863 480 unsigned char buttonX(unsigned short X, unsigned char columns) {
TickTock 22:a43df3905863 481 unsigned char val = X*columns/320;
TickTock 22:a43df3905863 482 return val;
TickTock 22:a43df3905863 483 }
TickTock 22:a43df3905863 484
TickTock 22:a43df3905863 485 unsigned char buttonY(unsigned short Y, unsigned char rows) {
TickTock 22:a43df3905863 486 unsigned short val = Y*rows/240;
TickTock 22:a43df3905863 487 return val;
TickTock 22:a43df3905863 488 }
TickTock 23:cd03f9c3395e 489
TickTock 23:cd03f9c3395e 490 void saveConfig(){
TickTock 23:cd03f9c3395e 491 FILE *cfile;
TickTock 99:c05abf8e1cdc 492 cfile = fopen("/local/config.txt", "w");
leafman 98:9f8bab96edff 493 fprintf(cfile,"format 5\r\n");
TickTock 23:cd03f9c3395e 494 fprintf(cfile,"x0_off %d\r\n",tt.x0_off);
TickTock 23:cd03f9c3395e 495 fprintf(cfile,"y0_off %d\r\n",tt.y0_off);
TickTock 23:cd03f9c3395e 496 fprintf(cfile,"x0_pp %d\r\n",tt.x0_pp);
TickTock 23:cd03f9c3395e 497 fprintf(cfile,"y0_pp %d\r\n",tt.y0_pp);
TickTock 23:cd03f9c3395e 498 fprintf(cfile,"x1_off %d\r\n",tt.x1_off);
TickTock 23:cd03f9c3395e 499 fprintf(cfile,"y1_off %d\r\n",tt.y1_off);
TickTock 23:cd03f9c3395e 500 fprintf(cfile,"x1_pp %d\r\n",tt.x1_pp);
TickTock 23:cd03f9c3395e 501 fprintf(cfile,"y1_pp %d\r\n",tt.y1_pp);
TickTock 23:cd03f9c3395e 502 fprintf(cfile,"x_mid %d\r\n",tt.x_mid);
leafman 98:9f8bab96edff 503 if (dMode[0]==configScreen)
TickTock 41:8d4609ea7259 504 fprintf(cfile,"dMode0 %d\r\n",mainScreen);
TickTock 26:462ccb580472 505 else
TickTock 26:462ccb580472 506 fprintf(cfile,"dMode0 %d\r\n",dMode[0]);
leafman 98:9f8bab96edff 507 if (dMode[1]==configScreen)
TickTock 41:8d4609ea7259 508 fprintf(cfile,"dMode1 %d\r\n",mainScreen);
TickTock 26:462ccb580472 509 else
TickTock 26:462ccb580472 510 fprintf(cfile,"dMode1 %d\r\n",dMode[1]);
TickTock 35:5acbd8a64a89 511 fprintf(cfile,"ledHi %4.3f\r\n",ledHi);
TickTock 35:5acbd8a64a89 512 fprintf(cfile,"ledLo %4.3f\r\n",ledLo);
TickTock 35:5acbd8a64a89 513 fprintf(cfile,"pollInt %d\r\n",pollInt);
TickTock 35:5acbd8a64a89 514 fprintf(cfile,"scale12V %4.2f\r\n",scale12V);
TickTock 48:d1ce92104a1f 515 fprintf(cfile,"skin %d\r\n",skin);
TickTock 50:83d5864c64a0 516 fprintf(cfile,"dtePeriod %d\r\n",dtePeriod);
garygid 87:46ac3f2519d6 517 fprintf(cfile,"DebugMode %d\r\n",(debugMode?1:0));
leafman 98:9f8bab96edff 518 fprintf(cfile,"metric %d\r\n",(metric?1:0));
leafman 98:9f8bab96edff 519 fprintf(cfile, "firmware %d\r\n", fwCount );
TickTock 23:cd03f9c3395e 520 fclose(cfile);
TickTock 23:cd03f9c3395e 521 }
TickTock 23:cd03f9c3395e 522
TickTock 111:d1559bb25c43 523 void readConfig(){
TickTock 23:cd03f9c3395e 524 FILE *cfile;
TickTock 23:cd03f9c3395e 525 int ff;
TickTock 23:cd03f9c3395e 526 cfile = fopen("/local/config.txt", "r");
TickTock 23:cd03f9c3395e 527 if (cfile==NULL){ // if doesn't exist --> create
TickTock 116:5cd72bae7c12 528 printMsg("No config file found.\n"); // no config file
TickTock 116:5cd72bae7c12 529 printMsg("Calibrating touch screen.\n"); // calibrating
TickTock 23:cd03f9c3395e 530 //tt.setcal(5570, 34030, 80, 108, 33700, 5780, 82, 108, 32500);// bypass calibration using my values
TickTock 23:cd03f9c3395e 531 tt.calibrate(); // run touchscreen calibration routine
garygid 58:4d06288d75a2 532 // NOTE: calibrates screen 1 first, then screen 0.
TickTock 23:cd03f9c3395e 533 saveConfig();
TickTock 23:cd03f9c3395e 534 } else {
TickTock 102:fd19f777a0b4 535 ledHi = 0.8;
TickTock 48:d1ce92104a1f 536 ledLo = 0.1;
TickTock 102:fd19f777a0b4 537 pollInt = 60;
TickTock 48:d1ce92104a1f 538 scale12V = 16.2;
TickTock 48:d1ce92104a1f 539 skin = ttSkin;
leafman 98:9f8bab96edff 540 fscanf(cfile, "format %d\r\n", &ff );
leafman 98:9f8bab96edff 541 fscanf(cfile, "x0_off %d\r\n", &tt.x0_off );
leafman 98:9f8bab96edff 542 fscanf(cfile, "y0_off %d\r\n", &tt.y0_off );
leafman 98:9f8bab96edff 543 fscanf(cfile, "x0_pp %d\r\n", &tt.x0_pp );
leafman 98:9f8bab96edff 544 fscanf(cfile, "y0_pp %d\r\n", &tt.y0_pp );
leafman 98:9f8bab96edff 545 fscanf(cfile, "x1_off %d\r\n", &tt.x1_off );
leafman 98:9f8bab96edff 546 fscanf(cfile, "y1_off %d\r\n", &tt.y1_off );
leafman 98:9f8bab96edff 547 fscanf(cfile, "x1_pp %d\r\n", &tt.x1_pp );
leafman 98:9f8bab96edff 548 fscanf(cfile, "y1_pp %d\r\n", &tt.y1_pp );
leafman 98:9f8bab96edff 549 fscanf(cfile, "x_mid %d\r\n", &tt.x_mid );
leafman 98:9f8bab96edff 550 fscanf(cfile, "dMode0 %d\r\n", &dMode[0] );
leafman 98:9f8bab96edff 551 fscanf(cfile, "dMode1 %d\r\n", &dMode[1] );
TickTock 35:5acbd8a64a89 552 if(ff>1){
leafman 98:9f8bab96edff 553 fscanf(cfile, "ledHi %f\r\n", &ledHi );
leafman 98:9f8bab96edff 554 fscanf(cfile, "ledLo %f\r\n", &ledLo );
leafman 98:9f8bab96edff 555 fscanf(cfile, "pollInt %d\r\n", &pollInt );
leafman 98:9f8bab96edff 556 fscanf(cfile, "scale12V %f\r\n", &scale12V );
TickTock 48:d1ce92104a1f 557 }
TickTock 48:d1ce92104a1f 558 if(ff>2){
leafman 98:9f8bab96edff 559 fscanf(cfile, "skin %d\r\n", &skin );
leafman 98:9f8bab96edff 560 fscanf(cfile, "dtePeriod %d\r\n", &dtePeriod );
TickTock 35:5acbd8a64a89 561 }
garygid 87:46ac3f2519d6 562 if(ff>3){
leafman 98:9f8bab96edff 563 int iDebug;
leafman 98:9f8bab96edff 564 fscanf(cfile, "DebugMode %d\r\n", &iDebug );
leafman 98:9f8bab96edff 565 debugMode = (bool)iDebug;
leafman 98:9f8bab96edff 566 }
leafman 98:9f8bab96edff 567 if(ff>4) {
leafman 98:9f8bab96edff 568 int iMetric;
leafman 98:9f8bab96edff 569 fscanf(cfile, "metric %d\r\n", &iMetric );
leafman 98:9f8bab96edff 570 metric = (bool)iMetric;
leafman 98:9f8bab96edff 571 fscanf(cfile, "firmware %d\r\n", &iMetric );
leafman 98:9f8bab96edff 572 fwCount = iMetric;
garygid 87:46ac3f2519d6 573 }
TickTock 23:cd03f9c3395e 574 fclose(cfile);
garygid 87:46ac3f2519d6 575 if(ff<4){//If not latest format, save as latest format
TickTock 35:5acbd8a64a89 576 saveConfig();
TickTock 116:5cd72bae7c12 577 printMsg("Config file format updated.\n"); // config forat updates
TickTock 48:d1ce92104a1f 578 }
TickTock 116:5cd72bae7c12 579 printMsg("Config file loaded.\n"); // config file loaded
TickTock 23:cd03f9c3395e 580 }
TickTock 23:cd03f9c3395e 581 }
TickTock 25:ddf0ec209f03 582
TickTock 25:ddf0ec209f03 583 void upDate(unsigned char field, bool upDownBar){
TickTock 25:ddf0ec209f03 584 struct tm t; // pointer to a static tm structure
TickTock 25:ddf0ec209f03 585 time_t seconds ;
TickTock 25:ddf0ec209f03 586 seconds = time(NULL);
TickTock 25:ddf0ec209f03 587 t = *localtime(&seconds) ;
TickTock 25:ddf0ec209f03 588 switch(field){
TickTock 25:ddf0ec209f03 589 case 0: // year
TickTock 25:ddf0ec209f03 590 if (upDownBar) {
TickTock 25:ddf0ec209f03 591 t.tm_year = t.tm_year+1;
TickTock 25:ddf0ec209f03 592 } else {
TickTock 25:ddf0ec209f03 593 t.tm_year = t.tm_year-1;
TickTock 25:ddf0ec209f03 594 }
TickTock 25:ddf0ec209f03 595 break;
TickTock 25:ddf0ec209f03 596 case 1: // month
TickTock 25:ddf0ec209f03 597 if (upDownBar) {
TickTock 25:ddf0ec209f03 598 t.tm_mon = (t.tm_mon<12)?t.tm_mon+1:1;
TickTock 25:ddf0ec209f03 599 } else {
TickTock 25:ddf0ec209f03 600 t.tm_mon = (t.tm_mon>2)?t.tm_mon-1:12;
TickTock 25:ddf0ec209f03 601 }
TickTock 25:ddf0ec209f03 602 break;
TickTock 25:ddf0ec209f03 603 case 2: // day
TickTock 25:ddf0ec209f03 604 if (upDownBar) {
TickTock 25:ddf0ec209f03 605 t.tm_mday = (t.tm_mday<31)?t.tm_mday+1:1;
TickTock 25:ddf0ec209f03 606 } else {
TickTock 25:ddf0ec209f03 607 t.tm_mday = (t.tm_mday>2)?t.tm_mday-1:31;
TickTock 25:ddf0ec209f03 608 }
TickTock 25:ddf0ec209f03 609 break;
TickTock 25:ddf0ec209f03 610 case 3: // hour
TickTock 25:ddf0ec209f03 611 if (upDownBar) {
TickTock 25:ddf0ec209f03 612 t.tm_hour = (t.tm_hour<23)?t.tm_hour+1:0;
TickTock 25:ddf0ec209f03 613 } else {
TickTock 25:ddf0ec209f03 614 t.tm_hour = (t.tm_hour>1)?t.tm_hour-1:23;
TickTock 25:ddf0ec209f03 615 }
TickTock 25:ddf0ec209f03 616 break;
TickTock 25:ddf0ec209f03 617 case 4: // minute
TickTock 25:ddf0ec209f03 618 if (upDownBar) {
TickTock 25:ddf0ec209f03 619 t.tm_min = (t.tm_min<59)?t.tm_min+1:0;
TickTock 25:ddf0ec209f03 620 } else {
TickTock 25:ddf0ec209f03 621 t.tm_min = (t.tm_min>1)?t.tm_min-1:59;
TickTock 25:ddf0ec209f03 622 }
TickTock 25:ddf0ec209f03 623 break;
TickTock 25:ddf0ec209f03 624 case 5: // second
TickTock 25:ddf0ec209f03 625 if (upDownBar) {
TickTock 25:ddf0ec209f03 626 t.tm_sec = (t.tm_sec<59)?t.tm_sec+1:0;
TickTock 25:ddf0ec209f03 627 } else {
TickTock 25:ddf0ec209f03 628 t.tm_sec = (t.tm_sec>1)?t.tm_sec-1:59;
TickTock 25:ddf0ec209f03 629 }
TickTock 25:ddf0ec209f03 630 break;
TickTock 25:ddf0ec209f03 631 default:
TickTock 25:ddf0ec209f03 632 break;
TickTock 25:ddf0ec209f03 633 }
TickTock 25:ddf0ec209f03 634 set_time(mktime(&t));
TickTock 25:ddf0ec209f03 635 }
TickTock 25:ddf0ec209f03 636
TickTock 39:eef8beac7411 637 void logPackVoltages() { // Turbo3 - routine to dump CP values to text file
TickTock 110:ffddff3ad2f2 638 char sTemp[40];
TickTock 39:eef8beac7411 639 struct tm t; // pointer to a static tm structure
TickTock 39:eef8beac7411 640 short unsigned max, min, jv, i, bd;
TickTock 39:eef8beac7411 641 unsigned avg;
TickTock 119:0e2b641e98a2 642 unsigned short gids, SOC, packV_x2;
TickTock 119:0e2b641e98a2 643 signed short packA_x2;
TickTock 39:eef8beac7411 644 time_t seconds ;
TickTock 39:eef8beac7411 645
TickTock 39:eef8beac7411 646 CANMessage msg;
TickTock 39:eef8beac7411 647
TickTock 39:eef8beac7411 648 seconds = time(NULL); // Turbo3
TickTock 39:eef8beac7411 649 t = *localtime(&seconds) ; // Turbo3
TickTock 39:eef8beac7411 650
TickTock 39:eef8beac7411 651 msg = lastMsg[indexLastMsg[0x5bc]]; //Get gids
TickTock 39:eef8beac7411 652 gids = (msg.data[0]<<2)+(msg.data[1]>>6);
TickTock 39:eef8beac7411 653 msg = lastMsg[indexLastMsg[0x55b]]; //Get SOC
TickTock 39:eef8beac7411 654 SOC = (msg.data[0]<<2)+(msg.data[1]>>6);
TickTock 39:eef8beac7411 655 msg = lastMsg[indexLastMsg[0x1db]]; //Get pack volts
TickTock 119:0e2b641e98a2 656 packV_x2 = (msg.data[2]<<2)+(msg.data[3]>>6);
TickTock 119:0e2b641e98a2 657 packA_x2 = (msg.data[0]<<3)+(msg.data[1]>>5);
TickTock 119:0e2b641e98a2 658 if (packA_x2 & 0x400) packA_x2 |= 0xf800;
TickTock 39:eef8beac7411 659
TickTock 39:eef8beac7411 660 max=0;
TickTock 39:eef8beac7411 661 min=9999;
TickTock 39:eef8beac7411 662 avg=0;
TickTock 92:935adef49ea4 663 for(i=0; i<96; i++) {
TickTock 114:3f8c59a8a2b9 664 bd=(battData[BatDataBaseG2*7+i*2+3]<<8)+battData[BatDataBaseG2*7+i*2+4];
TickTock 39:eef8beac7411 665 avg+=bd;
TickTock 39:eef8beac7411 666 if(bd>max) max=bd;
TickTock 39:eef8beac7411 667 if(bd<min) min=bd;
TickTock 39:eef8beac7411 668 }
TickTock 39:eef8beac7411 669 avg /= 96;
TickTock 39:eef8beac7411 670 if(min<3713) {
TickTock 39:eef8beac7411 671 jv=avg-(max-avg)*1.5;
TickTock 39:eef8beac7411 672 } else { // Only compute judgement value if min cellpair meets <= 3712mV requirement
TickTock 39:eef8beac7411 673 jv=0;
TickTock 39:eef8beac7411 674 }
TickTock 39:eef8beac7411 675
TickTock 110:ffddff3ad2f2 676 FIL bfile;
TickTock 110:ffddff3ad2f2 677 FRESULT bfr;
TickTock 110:ffddff3ad2f2 678 bfr = f_open(&bfile,"batvolt.txt",FA_WRITE|FA_OPEN_ALWAYS);
TickTock 110:ffddff3ad2f2 679 if(bfr==FR_OK) {
TickTock 110:ffddff3ad2f2 680 f_lseek(&bfile,0xffffffff); // go to end of file to append
TickTock 39:eef8beac7411 681 strftime(sTemp, 40, "%a %m/%d/%Y %X", &t);
TickTock 110:ffddff3ad2f2 682 f_printf(&bfile,"%s,",sTemp);
TickTock 119:0e2b641e98a2 683 sprintf(sTemp,"%d,%5.1f%%,%5.1f,%5.1f,%d,%d,%d,%d,%d",gids,(float)SOC/10,(float)packV_x2/2,(float)packA_x2/2,max,min,avg,max-min,jv);
TickTock 110:ffddff3ad2f2 684 f_printf(&bfile,"%s,",sTemp);
TickTock 110:ffddff3ad2f2 685 f_printf(&bfile,"%d,%d,%d,%d,",(battData[(BatDataBaseG4*7)+ 3]<<8)+battData[(BatDataBaseG4*7)+ 4],battData[(BatDataBaseG4*7)+ 5],(battData[(BatDataBaseG4*7)+ 6]<<8)+battData[(BatDataBaseG4*7)+ 7],battData[(BatDataBaseG4*7)+ 8]);
TickTock 110:ffddff3ad2f2 686 f_printf(&bfile,"%d,%d,%d,%d", (battData[(BatDataBaseG4*7)+ 9]<<8)+battData[(BatDataBaseG4*7)+10],battData[(BatDataBaseG4*7)+11],(battData[(BatDataBaseG4*7)+12]<<8)+battData[(BatDataBaseG4*7)+13],battData[(BatDataBaseG4*7)+14]);
TickTock 108:29b5a760adc2 687 for(i=0; i<96; i++) {
TickTock 114:3f8c59a8a2b9 688 bd=(battData[BatDataBaseG2*7+i*2+3]<<8)+battData[BatDataBaseG2*7+i*2+4];
TickTock 110:ffddff3ad2f2 689 f_printf(&bfile,",%d",bd);
TickTock 108:29b5a760adc2 690 }
TickTock 110:ffddff3ad2f2 691 f_printf(&bfile,"\r\n");
TickTock 110:ffddff3ad2f2 692 f_close(&bfile);
TickTock 110:ffddff3ad2f2 693 }
TickTock 108:29b5a760adc2 694 logCP=false;
TickTock 108:29b5a760adc2 695 showCP=true;
TickTock 108:29b5a760adc2 696 }
TickTock 108:29b5a760adc2 697
TickTock 108:29b5a760adc2 698 void tripLog() { // Daily log
TickTock 110:ffddff3ad2f2 699 char sTemp[40];
TickTock 108:29b5a760adc2 700 struct tm t; // pointer to a static tm structure
TickTock 108:29b5a760adc2 701 short unsigned max, min, jv, i, bd;
TickTock 108:29b5a760adc2 702 unsigned avg;
TickTock 119:0e2b641e98a2 703 unsigned short gids, SOC, packV_x2;
TickTock 119:0e2b641e98a2 704 signed short packA_x2;
TickTock 108:29b5a760adc2 705 time_t seconds ;
TickTock 108:29b5a760adc2 706
TickTock 108:29b5a760adc2 707 CANMessage msg;
TickTock 108:29b5a760adc2 708
TickTock 108:29b5a760adc2 709 seconds = time(NULL); // Turbo3
TickTock 108:29b5a760adc2 710 t = *localtime(&seconds) ; // Turbo3
TickTock 108:29b5a760adc2 711
TickTock 108:29b5a760adc2 712 msg = lastMsg[indexLastMsg[0x5bc]]; //Get gids
TickTock 108:29b5a760adc2 713 gids = (msg.data[0]<<2)+(msg.data[1]>>6);
TickTock 108:29b5a760adc2 714 msg = lastMsg[indexLastMsg[0x55b]]; //Get SOC
TickTock 108:29b5a760adc2 715 SOC = (msg.data[0]<<2)+(msg.data[1]>>6);
TickTock 108:29b5a760adc2 716 msg = lastMsg[indexLastMsg[0x1db]]; //Get pack volts
TickTock 119:0e2b641e98a2 717 packV_x2 = (msg.data[2]<<2)+(msg.data[3]>>6);
TickTock 119:0e2b641e98a2 718 packA_x2 = (msg.data[0]<<3)+(msg.data[1]>>5);
TickTock 119:0e2b641e98a2 719 if (packA_x2 & 0x400) packA_x2 |= 0xf800;
TickTock 108:29b5a760adc2 720
TickTock 108:29b5a760adc2 721 max=0;
TickTock 108:29b5a760adc2 722 min=9999;
TickTock 108:29b5a760adc2 723 avg=0;
TickTock 108:29b5a760adc2 724 for(i=0; i<96; i++) {
TickTock 114:3f8c59a8a2b9 725 bd=(battData[BatDataBaseG2*7+i*2+3]<<8)+battData[BatDataBaseG2*7+i*2+4];
TickTock 108:29b5a760adc2 726 avg+=bd;
TickTock 108:29b5a760adc2 727 if(bd>max) max=bd;
TickTock 108:29b5a760adc2 728 if(bd<min) min=bd;
TickTock 108:29b5a760adc2 729 }
TickTock 108:29b5a760adc2 730 avg /= 96;
TickTock 108:29b5a760adc2 731 if(min<3713) {
TickTock 108:29b5a760adc2 732 jv=avg-(max-avg)*1.5;
TickTock 108:29b5a760adc2 733 } else { // Only compute judgement value if min cellpair meets <= 3712mV requirement
TickTock 108:29b5a760adc2 734 jv=0;
TickTock 108:29b5a760adc2 735 }
TickTock 108:29b5a760adc2 736
TickTock 110:ffddff3ad2f2 737 FIL bfile;
TickTock 110:ffddff3ad2f2 738 FRESULT bfr;
TickTock 111:d1559bb25c43 739 bfr = f_open(&bfile,"triplog.txt",FA_WRITE|FA_OPEN_ALWAYS);
TickTock 110:ffddff3ad2f2 740 if(bfr==FR_OK) {
TickTock 110:ffddff3ad2f2 741 f_lseek(&bfile,0xffffffff); // go to end of file to append
TickTock 108:29b5a760adc2 742 strftime(sTemp, 40, "%a %m/%d/%Y %X", &t);
TickTock 110:ffddff3ad2f2 743 f_printf(&bfile,"%s,",sTemp);
TickTock 122:138a40892a4c 744 sprintf(sTemp,"%3.1f,%d,%5.1f%%,%5.1f%%, %4.2f, %5.1f,%4.1f,%d,%d,%d,%d,%d",accV,gids,(float)SOC/10, (float)SOH_x100/100,(float)Ah_x10000/10000,(float)packV_x2/2,(float)packA_x2/2,max,min,avg,max-min,jv);
TickTock 110:ffddff3ad2f2 745 f_printf(&bfile,"%s,",sTemp);
TickTock 122:138a40892a4c 746 /*f_printf(&bfile,"%d,%d,%d,%d,",(battData[(BatDataBaseG4*7)+ 3]<<8)+battData[(BatDataBaseG4*7)+ 4],battData[(BatDataBaseG4*7)+ 5],(battData[(BatDataBaseG4*7)+ 6]<<8)+battData[(BatDataBaseG4*7)+ 7],battData[(BatDataBaseG4*7)+ 8]);
TickTock 110:ffddff3ad2f2 747 f_printf(&bfile,"%d,%d,%d,%d", (battData[(BatDataBaseG4*7)+ 9]<<8)+battData[(BatDataBaseG4*7)+10],battData[(BatDataBaseG4*7)+11],(battData[(BatDataBaseG4*7)+12]<<8)+battData[(BatDataBaseG4*7)+13],battData[(BatDataBaseG4*7)+14]);
TickTock 92:935adef49ea4 748 for(i=0; i<96; i++) {
TickTock 114:3f8c59a8a2b9 749 bd=(battData[BatDataBaseG2*7+i*2+3]<<8)+battData[BatDataBaseG2*7+i*2+4];
TickTock 110:ffddff3ad2f2 750 f_printf(&bfile,",%d",bd);
TickTock 122:138a40892a4c 751 }*/
TickTock 122:138a40892a4c 752 // temporariliy dump everything
TickTock 122:138a40892a4c 753 for(i=0; i<BatDataBufMax; i++) {
TickTock 122:138a40892a4c 754 f_printf(&bfile,",%02x",battData[i]);
TickTock 39:eef8beac7411 755 }
TickTock 122:138a40892a4c 756
TickTock 110:ffddff3ad2f2 757 f_printf(&bfile,"\r\n");
TickTock 110:ffddff3ad2f2 758 f_close(&bfile);
TickTock 110:ffddff3ad2f2 759 }
TickTock 39:eef8beac7411 760 }
TickTock 25:ddf0ec209f03 761
leafman 98:9f8bab96edff 762 //LM - updates firmware off a usb key, eliminating the need to plug
leafman 98:9f8bab96edff 763 //the CANary into a computer for updates.
leafman 98:9f8bab96edff 764 void updateFirmware()
leafman 98:9f8bab96edff 765 {
TickTock 111:d1559bb25c43 766 FIL sfile; // external usb file
TickTock 111:d1559bb25c43 767 FRESULT sfr; // external file access flags
TickTock 111:d1559bb25c43 768 unsigned int bytesRW;
TickTock 111:d1559bb25c43 769 char sTemp[40];
TickTock 111:d1559bb25c43 770 sfr = f_open(&sfile,"firmware.bin",FA_READ|FA_OPEN_EXISTING);
TickTock 111:d1559bb25c43 771 if(sfr != FR_OK)
TickTock 99:c05abf8e1cdc 772 {
TickTock 99:c05abf8e1cdc 773 sprintf(sTemp,"Couldn't find firmware.bin\n");
TickTock 99:c05abf8e1cdc 774 printf(sTemp);
TickTock 108:29b5a760adc2 775 wait(3);
TickTock 100:63a7456fc972 776 lastDMode[whichTouched]=99;//force refresh
TickTock 99:c05abf8e1cdc 777 return;
TickTock 99:c05abf8e1cdc 778 }
leafman 98:9f8bab96edff 779 fwCount ++;
leafman 98:9f8bab96edff 780 saveConfig();
leafman 98:9f8bab96edff 781 tt.cls();
leafman 98:9f8bab96edff 782 printf("Saved Configuration\n");
leafman 98:9f8bab96edff 783 //delete all bin files in /local
leafman 98:9f8bab96edff 784 DIR *dir;
leafman 98:9f8bab96edff 785 struct dirent *ent;
leafman 98:9f8bab96edff 786 printf("Starting update\n");
leafman 98:9f8bab96edff 787 printf("deleting old firmware files\n");
leafman 98:9f8bab96edff 788 if ((dir = opendir ("/local/")) != NULL) {
TickTock 109:3e6f0e8fca0d 789 // print all the files and directories within directory
leafman 98:9f8bab96edff 790 while ((ent = readdir (dir)) != NULL) {
leafman 98:9f8bab96edff 791 //printf("FILE: %s\n",ent->d_name);
leafman 98:9f8bab96edff 792 char dest[4] = "";
leafman 98:9f8bab96edff 793 strncat(dest, &ent->d_name[strlen(ent->d_name)-3],3);
leafman 98:9f8bab96edff 794 dest[0] = tolower(dest[0]);
leafman 98:9f8bab96edff 795 dest[1] = tolower(dest[1]);
leafman 98:9f8bab96edff 796 dest[2] = tolower(dest[2]);
leafman 98:9f8bab96edff 797 if(strcmp(dest,"bin")==0)
leafman 98:9f8bab96edff 798 {
leafman 98:9f8bab96edff 799 sprintf(sTemp,"/local/%s",ent->d_name);
leafman 98:9f8bab96edff 800 int result = remove(sTemp);
leafman 98:9f8bab96edff 801 printf("REMOVED: %s",ent->d_name);
leafman 98:9f8bab96edff 802 }
leafman 98:9f8bab96edff 803 }
leafman 98:9f8bab96edff 804 closedir (dir);
leafman 98:9f8bab96edff 805 } else {
TickTock 109:3e6f0e8fca0d 806 //could not open directory
leafman 98:9f8bab96edff 807 printf("Couldnt open folder");
TickTock 108:29b5a760adc2 808 wait(3);
leafman 98:9f8bab96edff 809 return;
leafman 98:9f8bab96edff 810 }
leafman 98:9f8bab96edff 811 printf("copying new firmware\n");
leafman 98:9f8bab96edff 812 tt.cls();
leafman 98:9f8bab96edff 813 //Copy the new firmware from usb->local
leafman 98:9f8bab96edff 814 //The newest bin file is the one that is used by the mbed
leafman 98:9f8bab96edff 815 const int bufSize = 2048;
leafman 98:9f8bab96edff 816 FILE *destFile;
leafman 98:9f8bab96edff 817 sprintf(sTemp,"/local/fw%d.bin",fwCount);
leafman 98:9f8bab96edff 818 printf("Writing %s\n",sTemp);
leafman 98:9f8bab96edff 819 wait(2);
leafman 98:9f8bab96edff 820 destFile = fopen(sTemp, "wb");
leafman 98:9f8bab96edff 821 if(destFile == NULL)
leafman 98:9f8bab96edff 822 {
leafman 98:9f8bab96edff 823 sprintf(sTemp,"Couldn't Open Destination\n");
leafman 98:9f8bab96edff 824 printf(sTemp);
TickTock 108:29b5a760adc2 825 wait(3);
leafman 98:9f8bab96edff 826 return;
leafman 98:9f8bab96edff 827 }
leafman 98:9f8bab96edff 828 char buffer[bufSize];
TickTock 97:a25940fd7b5b 829
TickTock 111:d1559bb25c43 830 while (!f_eof(&sfile))
leafman 98:9f8bab96edff 831 {
TickTock 111:d1559bb25c43 832 sfr=f_read(&sfile,&buffer,bufSize,&bytesRW);
TickTock 111:d1559bb25c43 833 fwrite(buffer, 1, bytesRW, destFile);
leafman 98:9f8bab96edff 834 }
leafman 98:9f8bab96edff 835
leafman 98:9f8bab96edff 836 fflush(destFile);
leafman 98:9f8bab96edff 837 fclose(destFile);
TickTock 111:d1559bb25c43 838 f_close(&sfile);
leafman 98:9f8bab96edff 839 tt.cls();
leafman 98:9f8bab96edff 840 printf("Succesful\n\n");
TickTock 108:29b5a760adc2 841 printf("Rebooting in 3 seconds\n");
TickTock 108:29b5a760adc2 842 wait(3);
leafman 98:9f8bab96edff 843 //Now run new firmware
TickTock 111:d1559bb25c43 844 mbed_reset();
TickTock 99:c05abf8e1cdc 845 }
TickTock 108:29b5a760adc2 846
TickTock 108:29b5a760adc2 847 bool detectUSB(void){
TickTock 111:d1559bb25c43 848 FIL tfile; // external usb file
TickTock 111:d1559bb25c43 849 bool usbEn = (f_open(&tfile,"usb.det",FA_WRITE|FA_OPEN_ALWAYS)==FR_OK);
TickTock 108:29b5a760adc2 850 if(usbEn){
TickTock 111:d1559bb25c43 851 f_close(&tfile);
TickTock 111:d1559bb25c43 852 f_unlink("usb.det");
TickTock 111:d1559bb25c43 853 }
TickTock 108:29b5a760adc2 854 return(usbEn);
TickTock 108:29b5a760adc2 855 }