ROM Comm / Mbed 2 deprecated espar_mini_control_CAN

Dependencies:   mbed mbed-STM32F103C8T6

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002 
00003 mbed Can-Bus ECU simulator
00004 
00005 v1.0 December 2014
00006 
00007 ********************************************************************************
00008 
00009 WARNING: Use at your own risk, sadly this software comes with no guarantees.
00010 This software is provided 'free' and in good faith, but the author does not
00011 accept liability for any damage arising from its use.
00012 
00013 ********************************************************************************
00014 
00015 */
00016 //#include "MapleMini.h"
00017 #include "stm32f103c8t6.h"
00018 #include "mbed.h"
00019 //#include "ecu_simulator.h"
00020 #include "globals.h"
00021 #include "heatcontrol.h"
00022 #include "spl0601.h"
00023 #include "i2c_base.h"
00024 extern I2C* spl_i2c;
00025 // pinout for Maple Mini
00026 DigitalIn click(PA_1);   // Button inputs
00027 // pinout for lpc1768
00028 //DigitalIn click(p21);   // Joystick inputs
00029 //DigitalIn right(p22);
00030 //DigitalIn down(p23);
00031 //DigitalIn left(p24);
00032 //DigitalIn up(p25);
00033 
00034 volatile unsigned long lifetimer = 0;
00035 volatile int secondFlag = 0;
00036 
00037 Ticker timer;
00038 Ticker quartersecond_timed_event;
00039 Ticker hundredms_timed_event;
00040 Ticker onesecond_timed_event;
00041 
00042 void attime()
00043 {
00044     //second_tick = true;
00045     //myled = !myled;
00046     led1 = 0; // keep it off.  Flicker on with CAN
00047 }
00048 
00049 // Menu defines
00050 
00051 //unsigned char menu_state;
00052 //unsigned char canspeed;
00053 
00054 // external variables
00055 volatile unsigned char idletimer;
00056 extern char otherControllerDetected;
00057 wiced_bool_t hasOnboardAltimeter = WICED_FALSE;
00058 
00059 char printbuff[256];
00060 
00061 void DebugWrite(const char* str)
00062 {
00063     pc.printf(str);
00064 }
00065 void resetTimer(struct sHeatVars *s)
00066 {
00067     s->resettick = 1;
00068     //TODO: This.
00069 }
00070 void resetTimerAsync(struct sHeatVars *s)
00071 {
00072     s->resettick = 1;
00073     //TODO: This.
00074 }
00075 void one_second_tick()
00076 {
00077     int i;
00078     lifetimer++; // used for wifi reset test.
00079     //wiced_rtos_lock_mutex( &xively_data.mutex );
00080 
00081     for (i = 0; i < HEATERSTATECOUNT; i++) {
00082         if (heaterState[i].heattime > 0 && heaterState[i].heattime < 59995) { // count down the heat timer
00083             // tick,tick...
00084             heaterState[i].heattime--;
00085         }
00086         if (heaterState[i].heatresettime > 0) {
00087             heaterState[i].heatresettime--;
00088         }
00089         if (heaterState[i].preheattime > 0) {
00090             heaterState[i].preheattime--;
00091         }
00092         if (heaterState[i].heaterDetected > 0) {
00093             heaterState[i].heaterDetected--;
00094         }
00095     }
00096 
00097     if (idletimer > 0) {
00098         idletimer--;
00099     }
00100     if (otherControllerDetected > 0) {
00101         otherControllerDetected--;
00102     }
00103     secondFlag = 1;
00104 
00105 }
00106 
00107 void quarter_second_tick()
00108 {
00109     int i;
00110     for (i = 0; i < HEATERSTATECOUNT; i++) {
00111         heaterState[i].tickcount += 250;
00112         if (heaterState[i].resettick == 1) {
00113             heaterState[i].tickcount = 0;
00114             heaterState[i].resettick = 0;
00115         }
00116     }
00117 }
00118 void hundredms_tick()
00119 {
00120     led1 = 0;// off
00121 }
00122 
00123 int main()
00124 {
00125     confSysClock();     //Configure system clock (72MHz HSE clock, 48MHz USB clock)
00126     pc.baud(115200);
00127 
00128     //Enable Pullup
00129     click.mode(PullUp);
00130 
00131 
00132     pc.printf("\n\nRIXEN Simulator v1.1 \r\n");
00133     led1 = 1;
00134 
00135     wait(1);
00136     led1 = 0;
00137     spl_i2c = new I2C(SDA, SCL);
00138     //canspeed = CAN500;
00139 //    ecu.dtc = false;
00140 //   ecu.active_fault = false;
00141 
00142 //   sim.canspeed(CANSPEED_500);
00143 
00144     wait(0.2);
00145     led1 = 1;
00146 
00147     //timer.attach(&attime, 1);
00148     quartersecond_timed_event.attach(&quarter_second_tick, 0.250);
00149     hundredms_timed_event.attach(&hundredms_tick, 0.1);
00150     onesecond_timed_event.attach(&one_second_tick, 1.0);
00151 
00152 
00153     initHeaterState(&heaterState[0]);
00154 
00155     heaterState[0].bustype = HEATERTYPECAN;
00156     heaterState[0].heaternum = 1;
00157 
00158 
00159     InitCAN();
00160 
00161     double presComp;
00162     while(1) { // Main CAN loop
00163 
00164         for (int i = 0; i < HEATERSTATECOUNT; i++) {
00165             //            wiced_update_system_monitor(&main_thread_monitor, 10 * 1000);
00166             doHeatTaskCAN(&heaterState[i]);
00167             //            wiced_update_system_monitor(&main_thread_monitor, 10 * 1000);
00168             doHeatLogicTask(&heaterState[i]);
00169             if (i < HEATERSTATECOUNT) {
00170                 wait_ms(50);
00171             } else {
00172                 //wiced_rtos_delay_milliseconds(50);
00173             }
00174         }
00175         if (!click) {
00176             if (heaterState[0].heatOn == HEATCALLOFF) {
00177                 set_heat_con(&heaterState[0], HEATCALLINIT);
00178             }
00179         } else {
00180             if (heaterState[0].heatOn != HEATCALLOFF) {
00181                 set_heat_con(&heaterState[0], HEATCALLOFF);
00182             }
00183         }
00184         
00185         if (secondFlag > 0) {
00186             secondFlag = 0;
00187 
00188             DebugWrite("About to clean I2C\r\n");
00189             cleanI2CLines(spl_i2c, SDA, SCL);
00190 
00191             DebugWrite("Done clean I2C, get pressure\r\n");
00192 
00193             hasOnboardAltimeter = getPressure(&presComp);
00194             if (hasOnboardAltimeter) {
00195                 int hpa = presComp / 100; // convert to HPA
00196                 sprintf(printbuff, "Pressure %0.5lf vs %d\r\n", presComp, hpa);
00197                 DebugWrite(printbuff);
00198 
00199                 //sprintf(printbuff, "Pressure is %.1f (%d)\r\n", presComp, hpa);
00200                 //DebugWrite(printbuff);
00201                 if (hpa < 600) {
00202                     hpa = 680;
00203                 }
00204                 if (hpa > 1105) {
00205                     hpa = 680;
00206                 }
00207                 heaterState[0].rawAltitude = presComp;
00208                 heaterState[0].internalAltitude = hpa;
00209             } else {
00210                 DebugWrite("No altimeter\r\n");
00211             }
00212             if (heaterState[0].initTimer > 0) {
00213                 heaterState[0].initTimer--;
00214             }
00215 //            DebugWrite("Doing re-init of I2c\r\n");
00216 
00217             // check I2C
00218             //          DeInit_SC16IS740();
00219             //          DeInit_SPL0601();
00220             //          cleanI2CLines();
00221             //          Init_SC16IS740(2);
00222             //          Init_SPL0601();
00223         }
00224     }
00225 }