Treehouse Mbed Team / Mbed 2 deprecated APS_DCM1SL

Dependencies:   mbed

Committer:
Slord2142
Date:
Tue Mar 12 22:16:03 2019 +0000
Revision:
49:65c714a5def2
Parent:
48:8e9de9ff1f22
Child:
52:2b3b194144da
ILR properly interrupts, I2C set up to interrupt (currently unused), and pins now reference the numFast table for when they turn on.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mfwic 0:44a3005d4f20 1 //-------------------------------------------------------------------------------
mfwic 0:44a3005d4f20 2 //
mfwic 0:44a3005d4f20 3 // Treehouse Designs Inc.
mfwic 0:44a3005d4f20 4 // Colorado Springs, Colorado
mfwic 0:44a3005d4f20 5 //
Slord2142 41:bd54c88e3334 6 // Copyright (c) 2018 by Treehouse Designs Inc.
mfwic 0:44a3005d4f20 7 // Copyright (c) 2018 by Agility Power Systems Inc.
mfwic 0:44a3005d4f20 8 //
mfwic 0:44a3005d4f20 9 // This code is the property of Treehouse Designs, Inc. (Treehouse) and
mfwic 0:44a3005d4f20 10 // Agility Power Systems Inc. (Agility) and may not be redistributed
mfwic 0:44a3005d4f20 11 // in any form without prior written permission from
mfwic 0:44a3005d4f20 12 // both copyright holders, Treehouse and Agility.
mfwic 0:44a3005d4f20 13 //
mfwic 0:44a3005d4f20 14 // The above copyright notice and this permission notice shall be included in
mfwic 0:44a3005d4f20 15 // all copies or substantial portions of the Software.
mfwic 0:44a3005d4f20 16 //
mfwic 0:44a3005d4f20 17 //
mfwic 0:44a3005d4f20 18 //-------------------------------------------------------------------------------
mfwic 0:44a3005d4f20 19 //
mfwic 0:44a3005d4f20 20 // REVISION HISTORY:
mfwic 0:44a3005d4f20 21 //
mfwic 0:44a3005d4f20 22 // $Author: $
mfwic 0:44a3005d4f20 23 // $Rev: $
mfwic 0:44a3005d4f20 24 // $Date: $
mfwic 0:44a3005d4f20 25 // $URL: $
mfwic 0:44a3005d4f20 26 //
mfwic 0:44a3005d4f20 27 //-------------------------------------------------------------------------------
mfwic 0:44a3005d4f20 28
mfwic 0:44a3005d4f20 29 #include "mbed.h"
mfwic 42:3ae73b61f657 30 #include "math.h"
mfwic 42:3ae73b61f657 31 #include "globals.h"
mfwic 42:3ae73b61f657 32 #include "parameters.h"
mfwic 0:44a3005d4f20 33 #include "all_io.h"
mfwic 42:3ae73b61f657 34 #include "stdio.h"
mfwic 42:3ae73b61f657 35 #include "stdlib.h"
mfwic 42:3ae73b61f657 36 #include "serial.h"
mfwic 42:3ae73b61f657 37 #include "adc.h"
mfwic 42:3ae73b61f657 38 #include "adc_defs.h"
mfwic 42:3ae73b61f657 39 #include "menu.h"
mfwic 42:3ae73b61f657 40 #include "boards.h"
mfwic 42:3ae73b61f657 41 #include "command.h"
mfwic 42:3ae73b61f657 42 #include "clocks.h"
Slord2142 41:bd54c88e3334 43
Slord2142 41:bd54c88e3334 44
Slord2142 41:bd54c88e3334 45 //DigitalOut myled(LED1);
Slord2142 41:bd54c88e3334 46 Ticker slowClk;
Slord2142 41:bd54c88e3334 47 Ticker fastClk;
Slord2142 41:bd54c88e3334 48
mfwic 42:3ae73b61f657 49 unsigned short my12=0;
mfwic 42:3ae73b61f657 50 volatile bool updateReady = FALSE;
mfwic 42:3ae73b61f657 51 unsigned int loopTime = 0;
mfwic 42:3ae73b61f657 52 bool raw = FALSE;
mfwic 42:3ae73b61f657 53 bool running = FALSE;
mfwic 42:3ae73b61f657 54 bool testing = FALSE;
mfwic 42:3ae73b61f657 55 bool buck = TRUE;
mfwic 44:741ee27c8a34 56 bool pcConnected = FALSE; //New for 1U5
mfwic 44:741ee27c8a34 57 bool master = FALSE; //New for 1U5
mfwic 44:741ee27c8a34 58
mfwic 43:291bbdba48f3 59 unsigned int max_rows = 2048;
mfwic 43:291bbdba48f3 60 unsigned int bCodeRow[13];
mfwic 44:741ee27c8a34 61 unsigned short max_boards = 3;
mfwic 43:291bbdba48f3 62 unsigned short row = 0;
mfwic 44:741ee27c8a34 63 short row_test = 0;
mfwic 44:741ee27c8a34 64 unsigned short slave_code = 0;
mfwic 42:3ae73b61f657 65
mfwic 42:3ae73b61f657 66 double CURRENT_48_OFFSET = 33940;
mfwic 42:3ae73b61f657 67 double CURRENT_24_OFFSET = 33580;
mfwic 42:3ae73b61f657 68 double CURRENT_12_OFFSET = 33256;
mfwic 1:9f8583ba2431 69
Slord2142 41:bd54c88e3334 70
mfwic 25:8bcc8bea0e31 71
Slord2142 47:d10bf65cb7dc 72 //void fastFlip(unsigned short numFast){
Slord2142 47:d10bf65cb7dc 73 // if(numFast >= 1){ec0 = ~ec0;}
Slord2142 47:d10bf65cb7dc 74 // if(numFast >= 2){ec1 = ~ec1;}
Slord2142 47:d10bf65cb7dc 75 // ...
Slord2142 47:d10bf65cb7dc 76 // if(numFast >= 36){ec35 = ~ec35;}
Slord2142 47:d10bf65cb7dc 77 //}
mfwic 45:bd8da8d90dbb 78
Slord2142 47:d10bf65cb7dc 79 //void slowFlip(unsigned short numFast){
Slord2142 47:d10bf65cb7dc 80 // numSlow = MAX_CHIPS - numFast;
Slord2142 47:d10bf65cb7dc 81 // if( numSlow >= 36){ec35 = ~ec35;}else{return;}
Slord2142 47:d10bf65cb7dc 82 // if( numSlow >= 35){ec34 = ~ec34;}else{return;}
Slord2142 47:d10bf65cb7dc 83 // ...
Slord2142 47:d10bf65cb7dc 84 // if( numSlow >= 1){ec0 = ~ec0;}
Slord2142 47:d10bf65cb7dc 85 //}
mfwic 45:bd8da8d90dbb 86
mfwic 44:741ee27c8a34 87
mfwic 44:741ee27c8a34 88
mfwic 44:741ee27c8a34 89 int main() {
mfwic 44:741ee27c8a34 90
mfwic 44:741ee27c8a34 91 if(MS){ //New for 1U5
mfwic 44:741ee27c8a34 92 master = TRUE;
mfwic 44:741ee27c8a34 93 }else{
mfwic 44:741ee27c8a34 94 master = FALSE;
mfwic 44:741ee27c8a34 95 }
Slord2142 41:bd54c88e3334 96
Slord2142 41:bd54c88e3334 97 initOut();
Slord2142 41:bd54c88e3334 98
Slord2142 48:8e9de9ff1f22 99 slowClk.attach(&slowFlip, 2.5);
Slord2142 48:8e9de9ff1f22 100 fastClk.attach(&fastFlip, 1.0);
Slord2142 49:65c714a5def2 101 ILR_enable.rise(&ILR);
mfwic 43:291bbdba48f3 102
mfwic 43:291bbdba48f3 103 running = FALSE;
mfwic 43:291bbdba48f3 104
mfwic 43:291bbdba48f3 105 initSerial();
Slord2142 41:bd54c88e3334 106
mfwic 44:741ee27c8a34 107 initI2C();
mfwic 44:741ee27c8a34 108
mfwic 43:291bbdba48f3 109 initDRT(); // Display Refresh Timer
mfwic 43:291bbdba48f3 110
mfwic 43:291bbdba48f3 111 initADC();
mfwic 43:291bbdba48f3 112
mfwic 43:291bbdba48f3 113 struct adcValues adcVals = getADCresults();
mfwic 43:291bbdba48f3 114 struct displayValues dispVals = calcDisplayValues(adcVals);
mfwic 43:291bbdba48f3 115 struct statusValues statVals = checkLevels(adcVals);
mfwic 43:291bbdba48f3 116
mfwic 43:291bbdba48f3 117 row = (unsigned short)(dispVals.i12f*dispVals.v12f*ROW_CORRECTION_FACTOR);
mfwic 43:291bbdba48f3 118 unsigned int old_row = row;
mfwic 43:291bbdba48f3 119
mfwic 43:291bbdba48f3 120 menu_banner();
mfwic 43:291bbdba48f3 121 sprintf(strbuf, "\r\nPress Enter to continue");
mfwic 43:291bbdba48f3 122 sendSerial(strbuf);
mfwic 43:291bbdba48f3 123 //while(waitCommand()){ // Wait for user to press Enter
mfwic 43:291bbdba48f3 124 //}
mfwic 43:291bbdba48f3 125
mfwic 43:291bbdba48f3 126 menuRedraw(WITH_PROMPT);
mfwic 43:291bbdba48f3 127
mfwic 43:291bbdba48f3 128 initBoards(adcVals);
mfwic 43:291bbdba48f3 129
mfwic 43:291bbdba48f3 130 while (1)
mfwic 43:291bbdba48f3 131 {
mfwic 43:291bbdba48f3 132 adcVals = getADCresults();
mfwic 43:291bbdba48f3 133
mfwic 43:291bbdba48f3 134 dispVals = calcDisplayValues(adcVals);
mfwic 43:291bbdba48f3 135
mfwic 43:291bbdba48f3 136 statVals = checkLevels(adcVals);
mfwic 43:291bbdba48f3 137
mfwic 43:291bbdba48f3 138 processCommand();
mfwic 43:291bbdba48f3 139
mfwic 43:291bbdba48f3 140 checkFlip();
mfwic 43:291bbdba48f3 141
mfwic 43:291bbdba48f3 142 // Select RUN from menu to activate running mode.
mfwic 43:291bbdba48f3 143 // BRDS, MULT activate testing mode. MY12 emulates running mode wthout being in running mode.
mfwic 43:291bbdba48f3 144 // CAL and UNCAL do not change state of running or testing.
mfwic 43:291bbdba48f3 145 if(adcVals.i12 < CURRENT_12_OFFSET){
mfwic 43:291bbdba48f3 146 row = (unsigned short)(dispVals.i12f*dispVals.v12f*ROW_CORRECTION_FACTOR);
mfwic 43:291bbdba48f3 147 buck = TRUE;
mfwic 43:291bbdba48f3 148 }else{
mfwic 43:291bbdba48f3 149 row = (unsigned short)(dispVals.i12f*dispVals.v12f*(-ROW_CORRECTION_FACTOR));
mfwic 43:291bbdba48f3 150 buck = FALSE;
mfwic 43:291bbdba48f3 151 }
mfwic 43:291bbdba48f3 152 row_test = abs((int)(row-old_row));
mfwic 44:741ee27c8a34 153 if(running && master){ //New for 1U5
mfwic 43:291bbdba48f3 154 //The current sensors results are single-ended. Results below the midpoint are for buck mode, above is for boost mode.
mfwic 44:741ee27c8a34 155 if(row_test>ROW_HYSTERESIS){
mfwic 44:741ee27c8a34 156 old_row = row;
mfwic 44:741ee27c8a34 157 if(row<=1023){
mfwic 44:741ee27c8a34 158 slave_code = updateMasterControls(row); //New for 1U5
mfwic 44:741ee27c8a34 159 sendSlaveCommands(slave_code); //New for 1U5
mfwic 44:741ee27c8a34 160 }
mfwic 43:291bbdba48f3 161 }
mfwic 44:741ee27c8a34 162 else if(running && !master){ //New for 1U5
mfwic 44:741ee27c8a34 163 slave_code = getMasterCommands(); //New for 1U5
mfwic 44:741ee27c8a34 164 updateSlaveControls(slave_code); //New for 1U5
mfwic 44:741ee27c8a34 165 }
mfwic 43:291bbdba48f3 166 }else if(!testing){
mfwic 43:291bbdba48f3 167 row_test = abs((int)(my12-old_row));
mfwic 43:291bbdba48f3 168 if(row_test>ROW_HYSTERESIS){
mfwic 43:291bbdba48f3 169 old_row = my12;
mfwic 43:291bbdba48f3 170 if(row<=1023){
mfwic 44:741ee27c8a34 171 updateMasterControls(my12);
mfwic 43:291bbdba48f3 172 }
mfwic 43:291bbdba48f3 173 }
mfwic 43:291bbdba48f3 174 }
mfwic 43:291bbdba48f3 175
mfwic 43:291bbdba48f3 176 if(updateReady && pcConnected){
mfwic 43:291bbdba48f3 177 updateReady = FALSE;
mfwic 43:291bbdba48f3 178 updateTerminal(adcVals, statVals); // May want to gate this call when we run a headless system.
mfwic 43:291bbdba48f3 179
mfwic 43:291bbdba48f3 180 }
mfwic 43:291bbdba48f3 181 }//end while(1)
mfwic 43:291bbdba48f3 182 }//end void main(void)