Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
src/main.cpp@53:004a9674e922, 2019-03-14 (annotated)
- Committer:
- mfwic
- Date:
- Thu Mar 14 03:22:23 2019 +0000
- Revision:
- 53:004a9674e922
- Parent:
- 52:2b3b194144da
Changed slowClk.attach to slowClk.attach_us, also fastClk.
Who changed what in which revision?
User | Revision | Line number | New 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" |
mfwic | 52:2b3b194144da | 43 | #include "th_rtz.h" |
Slord2142 | 41:bd54c88e3334 | 44 | |
mfwic | 52:2b3b194144da | 45 | #define IS_MASTER |
mfwic | 52:2b3b194144da | 46 | //#define IS_SLAVE |
Slord2142 | 41:bd54c88e3334 | 47 | |
Slord2142 | 41:bd54c88e3334 | 48 | //DigitalOut myled(LED1); |
Slord2142 | 41:bd54c88e3334 | 49 | Ticker slowClk; |
Slord2142 | 41:bd54c88e3334 | 50 | Ticker fastClk; |
Slord2142 | 41:bd54c88e3334 | 51 | |
mfwic | 42:3ae73b61f657 | 52 | unsigned short my12=0; |
mfwic | 42:3ae73b61f657 | 53 | volatile bool updateReady = FALSE; |
mfwic | 52:2b3b194144da | 54 | volatile bool pulseReady = FALSE; |
mfwic | 42:3ae73b61f657 | 55 | unsigned int loopTime = 0; |
mfwic | 42:3ae73b61f657 | 56 | bool raw = FALSE; |
mfwic | 42:3ae73b61f657 | 57 | bool running = FALSE; |
mfwic | 42:3ae73b61f657 | 58 | bool testing = FALSE; |
mfwic | 42:3ae73b61f657 | 59 | bool buck = TRUE; |
mfwic | 44:741ee27c8a34 | 60 | bool pcConnected = FALSE; //New for 1U5 |
mfwic | 44:741ee27c8a34 | 61 | bool master = FALSE; //New for 1U5 |
mfwic | 52:2b3b194144da | 62 | bool slave1 = FALSE; //New for 1U5 |
mfwic | 52:2b3b194144da | 63 | bool slave2 = FALSE; //New for 1U5 |
mfwic | 44:741ee27c8a34 | 64 | |
mfwic | 43:291bbdba48f3 | 65 | unsigned int max_rows = 2048; |
mfwic | 43:291bbdba48f3 | 66 | unsigned int bCodeRow[13]; |
mfwic | 44:741ee27c8a34 | 67 | unsigned short max_boards = 3; |
mfwic | 43:291bbdba48f3 | 68 | unsigned short row = 0; |
mfwic | 44:741ee27c8a34 | 69 | short row_test = 0; |
mfwic | 44:741ee27c8a34 | 70 | unsigned short slave_code = 0; |
mfwic | 52:2b3b194144da | 71 | unsigned short new_slave_code = 0; |
mfwic | 42:3ae73b61f657 | 72 | |
mfwic | 42:3ae73b61f657 | 73 | double CURRENT_48_OFFSET = 33940; |
mfwic | 42:3ae73b61f657 | 74 | double CURRENT_24_OFFSET = 33580; |
mfwic | 42:3ae73b61f657 | 75 | double CURRENT_12_OFFSET = 33256; |
mfwic | 1:9f8583ba2431 | 76 | |
mfwic | 52:2b3b194144da | 77 | unsigned int temperatureVal; |
mfwic | 25:8bcc8bea0e31 | 78 | |
Slord2142 | 47:d10bf65cb7dc | 79 | //void fastFlip(unsigned short numFast){ |
Slord2142 | 47:d10bf65cb7dc | 80 | // if(numFast >= 1){ec0 = ~ec0;} |
Slord2142 | 47:d10bf65cb7dc | 81 | // if(numFast >= 2){ec1 = ~ec1;} |
Slord2142 | 47:d10bf65cb7dc | 82 | // ... |
Slord2142 | 47:d10bf65cb7dc | 83 | // if(numFast >= 36){ec35 = ~ec35;} |
Slord2142 | 47:d10bf65cb7dc | 84 | //} |
mfwic | 45:bd8da8d90dbb | 85 | |
Slord2142 | 47:d10bf65cb7dc | 86 | //void slowFlip(unsigned short numFast){ |
Slord2142 | 47:d10bf65cb7dc | 87 | // numSlow = MAX_CHIPS - numFast; |
Slord2142 | 47:d10bf65cb7dc | 88 | // if( numSlow >= 36){ec35 = ~ec35;}else{return;} |
Slord2142 | 47:d10bf65cb7dc | 89 | // if( numSlow >= 35){ec34 = ~ec34;}else{return;} |
Slord2142 | 47:d10bf65cb7dc | 90 | // ... |
Slord2142 | 47:d10bf65cb7dc | 91 | // if( numSlow >= 1){ec0 = ~ec0;} |
Slord2142 | 47:d10bf65cb7dc | 92 | //} |
mfwic | 45:bd8da8d90dbb | 93 | |
mfwic | 44:741ee27c8a34 | 94 | |
mfwic | 44:741ee27c8a34 | 95 | |
mfwic | 44:741ee27c8a34 | 96 | int main() { |
mfwic | 44:741ee27c8a34 | 97 | |
mfwic | 44:741ee27c8a34 | 98 | if(MS){ //New for 1U5 |
mfwic | 52:2b3b194144da | 99 | #ifdef IS_MASTER |
mfwic | 52:2b3b194144da | 100 | master = TRUE; |
mfwic | 52:2b3b194144da | 101 | #endif |
mfwic | 52:2b3b194144da | 102 | #ifdef IS_SLAVE |
mfwic | 52:2b3b194144da | 103 | slave1 = TRUE; |
mfwic | 52:2b3b194144da | 104 | slave2 = FALSE; |
mfwic | 52:2b3b194144da | 105 | #endif |
mfwic | 44:741ee27c8a34 | 106 | }else{ |
mfwic | 52:2b3b194144da | 107 | #ifdef IS_SLAVE |
mfwic | 52:2b3b194144da | 108 | slave1 = FALSE; |
mfwic | 52:2b3b194144da | 109 | slave2 = TRUE; |
mfwic | 52:2b3b194144da | 110 | #endif |
mfwic | 44:741ee27c8a34 | 111 | } |
Slord2142 | 41:bd54c88e3334 | 112 | |
mfwic | 52:2b3b194144da | 113 | Slave_Init(); |
mfwic | 52:2b3b194144da | 114 | |
Slord2142 | 41:bd54c88e3334 | 115 | initOut(); |
Slord2142 | 41:bd54c88e3334 | 116 | |
mfwic | 53:004a9674e922 | 117 | slowClk.attach_us(&slowFlip, CLK_SLOW); |
mfwic | 53:004a9674e922 | 118 | fastClk.attach_us(&fastFlip, CLK_FAST); |
Slord2142 | 49:65c714a5def2 | 119 | ILR_enable.rise(&ILR); |
mfwic | 43:291bbdba48f3 | 120 | |
mfwic | 43:291bbdba48f3 | 121 | running = FALSE; |
mfwic | 43:291bbdba48f3 | 122 | |
mfwic | 43:291bbdba48f3 | 123 | initSerial(); |
Slord2142 | 41:bd54c88e3334 | 124 | |
mfwic | 44:741ee27c8a34 | 125 | initI2C(); |
mfwic | 44:741ee27c8a34 | 126 | |
mfwic | 43:291bbdba48f3 | 127 | initDRT(); // Display Refresh Timer |
mfwic | 43:291bbdba48f3 | 128 | |
mfwic | 43:291bbdba48f3 | 129 | initADC(); |
mfwic | 43:291bbdba48f3 | 130 | |
mfwic | 43:291bbdba48f3 | 131 | struct adcValues adcVals = getADCresults(); |
mfwic | 43:291bbdba48f3 | 132 | struct displayValues dispVals = calcDisplayValues(adcVals); |
mfwic | 43:291bbdba48f3 | 133 | struct statusValues statVals = checkLevels(adcVals); |
mfwic | 43:291bbdba48f3 | 134 | |
mfwic | 43:291bbdba48f3 | 135 | row = (unsigned short)(dispVals.i12f*dispVals.v12f*ROW_CORRECTION_FACTOR); |
mfwic | 43:291bbdba48f3 | 136 | unsigned int old_row = row; |
mfwic | 43:291bbdba48f3 | 137 | |
mfwic | 43:291bbdba48f3 | 138 | menu_banner(); |
mfwic | 43:291bbdba48f3 | 139 | sprintf(strbuf, "\r\nPress Enter to continue"); |
mfwic | 43:291bbdba48f3 | 140 | sendSerial(strbuf); |
mfwic | 43:291bbdba48f3 | 141 | //while(waitCommand()){ // Wait for user to press Enter |
mfwic | 43:291bbdba48f3 | 142 | //} |
mfwic | 43:291bbdba48f3 | 143 | |
mfwic | 43:291bbdba48f3 | 144 | menuRedraw(WITH_PROMPT); |
mfwic | 43:291bbdba48f3 | 145 | |
mfwic | 43:291bbdba48f3 | 146 | initBoards(adcVals); |
mfwic | 43:291bbdba48f3 | 147 | |
mfwic | 43:291bbdba48f3 | 148 | while (1) |
mfwic | 43:291bbdba48f3 | 149 | { |
mfwic | 52:2b3b194144da | 150 | #ifdef IS_MASTER |
mfwic | 52:2b3b194144da | 151 | |
mfwic | 43:291bbdba48f3 | 152 | adcVals = getADCresults(); |
mfwic | 52:2b3b194144da | 153 | |
mfwic | 52:2b3b194144da | 154 | temperatureVal = getTEMPERATUREresults(); |
mfwic | 52:2b3b194144da | 155 | setFan(temperatureVal); |
mfwic | 43:291bbdba48f3 | 156 | |
mfwic | 43:291bbdba48f3 | 157 | dispVals = calcDisplayValues(adcVals); |
mfwic | 43:291bbdba48f3 | 158 | |
mfwic | 43:291bbdba48f3 | 159 | statVals = checkLevels(adcVals); |
mfwic | 43:291bbdba48f3 | 160 | |
mfwic | 43:291bbdba48f3 | 161 | processCommand(); |
mfwic | 43:291bbdba48f3 | 162 | |
mfwic | 43:291bbdba48f3 | 163 | // Select RUN from menu to activate running mode. |
mfwic | 43:291bbdba48f3 | 164 | // BRDS, MULT activate testing mode. MY12 emulates running mode wthout being in running mode. |
mfwic | 43:291bbdba48f3 | 165 | // CAL and UNCAL do not change state of running or testing. |
mfwic | 43:291bbdba48f3 | 166 | if(adcVals.i12 < CURRENT_12_OFFSET){ |
mfwic | 43:291bbdba48f3 | 167 | row = (unsigned short)(dispVals.i12f*dispVals.v12f*ROW_CORRECTION_FACTOR); |
mfwic | 43:291bbdba48f3 | 168 | buck = TRUE; |
mfwic | 43:291bbdba48f3 | 169 | }else{ |
mfwic | 43:291bbdba48f3 | 170 | row = (unsigned short)(dispVals.i12f*dispVals.v12f*(-ROW_CORRECTION_FACTOR)); |
mfwic | 43:291bbdba48f3 | 171 | buck = FALSE; |
mfwic | 43:291bbdba48f3 | 172 | } |
mfwic | 43:291bbdba48f3 | 173 | row_test = abs((int)(row-old_row)); |
mfwic | 52:2b3b194144da | 174 | |
mfwic | 52:2b3b194144da | 175 | if(updateReady && pcConnected){ |
mfwic | 52:2b3b194144da | 176 | updateReady = FALSE; |
mfwic | 52:2b3b194144da | 177 | updateTerminal(adcVals, statVals); // May want to gate this call when we run a headless system. |
mfwic | 52:2b3b194144da | 178 | |
mfwic | 52:2b3b194144da | 179 | } |
mfwic | 52:2b3b194144da | 180 | #endif |
mfwic | 52:2b3b194144da | 181 | |
mfwic | 52:2b3b194144da | 182 | checkFlip(); |
mfwic | 44:741ee27c8a34 | 183 | if(running && master){ //New for 1U5 |
mfwic | 43:291bbdba48f3 | 184 | //The current sensors results are single-ended. Results below the midpoint are for buck mode, above is for boost mode. |
mfwic | 44:741ee27c8a34 | 185 | if(row_test>ROW_HYSTERESIS){ |
mfwic | 44:741ee27c8a34 | 186 | old_row = row; |
mfwic | 44:741ee27c8a34 | 187 | if(row<=1023){ |
mfwic | 44:741ee27c8a34 | 188 | slave_code = updateMasterControls(row); //New for 1U5 |
mfwic | 44:741ee27c8a34 | 189 | sendSlaveCommands(slave_code); //New for 1U5 |
mfwic | 44:741ee27c8a34 | 190 | } |
mfwic | 43:291bbdba48f3 | 191 | } |
mfwic | 44:741ee27c8a34 | 192 | else if(running && !master){ //New for 1U5 |
mfwic | 52:2b3b194144da | 193 | new_slave_code = getMasterCommands(); //New for 1U5 |
mfwic | 52:2b3b194144da | 194 | if(new_slave_code != slave_code){ |
mfwic | 52:2b3b194144da | 195 | slave_code = new_slave_code; |
mfwic | 52:2b3b194144da | 196 | updateSlaveControls(slave_code); //New for 1U5 |
mfwic | 52:2b3b194144da | 197 | } |
mfwic | 44:741ee27c8a34 | 198 | } |
mfwic | 43:291bbdba48f3 | 199 | }else if(!testing){ |
mfwic | 43:291bbdba48f3 | 200 | row_test = abs((int)(my12-old_row)); |
mfwic | 43:291bbdba48f3 | 201 | if(row_test>ROW_HYSTERESIS){ |
mfwic | 43:291bbdba48f3 | 202 | old_row = my12; |
mfwic | 43:291bbdba48f3 | 203 | if(row<=1023){ |
mfwic | 44:741ee27c8a34 | 204 | updateMasterControls(my12); |
mfwic | 43:291bbdba48f3 | 205 | } |
mfwic | 43:291bbdba48f3 | 206 | } |
mfwic | 53:004a9674e922 | 207 | numFast = 36; |
mfwic | 43:291bbdba48f3 | 208 | } |
mfwic | 43:291bbdba48f3 | 209 | }//end while(1) |
mfwic | 43:291bbdba48f3 | 210 | }//end void main(void) |