Treehouse Mbed Team / Mbed 2 deprecated APS_DCM1SL

Dependencies:   mbed

Committer:
mfwic
Date:
Thu Mar 14 00:21:35 2019 +0000
Revision:
52:2b3b194144da
Parent:
51:abe8a392d50b
Added ifdef IS_MASTER & IS_SLAVE; Corrected Slave_RXstr; Added call to Slave_RXstr in loop.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mfwic 42:3ae73b61f657 1 //-------------------------------------------------------------------------------
mfwic 42:3ae73b61f657 2 //
mfwic 42:3ae73b61f657 3 // Treehouse Designs Inc.
mfwic 42:3ae73b61f657 4 // Colorado Springs, Colorado
mfwic 42:3ae73b61f657 5 //
mfwic 42:3ae73b61f657 6 // Copyright (c) 2016 by Treehouse Designs Inc.
mfwic 42:3ae73b61f657 7 // Copyright (c) 2018 by Agility Power Systems Inc.
mfwic 42:3ae73b61f657 8 //
mfwic 42:3ae73b61f657 9 // This code is the property of Treehouse Designs, Inc. (Treehouse) and
mfwic 42:3ae73b61f657 10 // Agility Power Systems Inc. (Agility) and may not be redistributed
mfwic 42:3ae73b61f657 11 // in any form without prior written permission from
mfwic 42:3ae73b61f657 12 // both copyright holders, Treehouse and Agility.
mfwic 42:3ae73b61f657 13 //
mfwic 42:3ae73b61f657 14 // The above copyright notice and this permission notice shall be included in
mfwic 42:3ae73b61f657 15 // all copies or substantial portions of the Software.
mfwic 42:3ae73b61f657 16 //
mfwic 42:3ae73b61f657 17 //
mfwic 42:3ae73b61f657 18 //-------------------------------------------------------------------------------
mfwic 42:3ae73b61f657 19 //
mfwic 42:3ae73b61f657 20 // REVISION HISTORY:
mfwic 42:3ae73b61f657 21 //
mfwic 42:3ae73b61f657 22 // $Author: $
mfwic 42:3ae73b61f657 23 // $Rev: $
mfwic 42:3ae73b61f657 24 // $Date: $
mfwic 42:3ae73b61f657 25 // $URL: $
mfwic 42:3ae73b61f657 26 //
mfwic 42:3ae73b61f657 27 //-------------------------------------------------------------------------------
mfwic 42:3ae73b61f657 28
mfwic 42:3ae73b61f657 29 #include "mbed.h"
mfwic 42:3ae73b61f657 30 #include "globals.h"
mfwic 42:3ae73b61f657 31 #include "parameters.h"
mfwic 42:3ae73b61f657 32 #include "boards.h"
mfwic 42:3ae73b61f657 33 #include "lut.h"
mfwic 42:3ae73b61f657 34 #include "all_io.h"
mfwic 42:3ae73b61f657 35 #include "serial.h"
mfwic 42:3ae73b61f657 36 #include "stdio.h"
Slord2142 51:abe8a392d50b 37 #include "th_rtz.h"
mfwic 42:3ae73b61f657 38
mfwic 42:3ae73b61f657 39 unsigned int boardEnableBits;
mfwic 42:3ae73b61f657 40
mfwic 44:741ee27c8a34 41 /*******************************************************************************
mfwic 44:741ee27c8a34 42 initI2C
mfwic 44:741ee27c8a34 43 *******************************************************************************/
mfwic 44:741ee27c8a34 44 void initI2C(void){
mfwic 44:741ee27c8a34 45
mfwic 44:741ee27c8a34 46 }
mfwic 44:741ee27c8a34 47
mfwic 44:741ee27c8a34 48 /*******************************************************************************
mfwic 52:2b3b194144da 49 initI2C
mfwic 52:2b3b194144da 50 *******************************************************************************/
mfwic 52:2b3b194144da 51 void setFan(unsigned int temperatureVal){
mfwic 52:2b3b194144da 52 float fan_duty = temperatureVal/TEMPERATURE_THRESH;
mfwic 52:2b3b194144da 53 fan_on.period(FAN_PERIOD);
mfwic 52:2b3b194144da 54 fan_on.write(fan_duty);
mfwic 52:2b3b194144da 55 }
mfwic 52:2b3b194144da 56
mfwic 52:2b3b194144da 57 /*******************************************************************************
mfwic 44:741ee27c8a34 58 initBoards
mfwic 44:741ee27c8a34 59 *******************************************************************************/
mfwic 42:3ae73b61f657 60 void initBoards(struct adcValues adcVals){
mfwic 42:3ae73b61f657 61 //en_out = 32;
mfwic 46:0de65f1bd714 62 //setBoardEnables(16383);
mfwic 46:0de65f1bd714 63
mfwic 46:0de65f1bd714 64 //Need to enable all chips fast. NEW code here
mfwic 42:3ae73b61f657 65
mfwic 42:3ae73b61f657 66 }
mfwic 42:3ae73b61f657 67
mfwic 42:3ae73b61f657 68 /*******************************************************************************
mfwic 44:741ee27c8a34 69 getMasterCommands
mfwic 44:741ee27c8a34 70 *******************************************************************************/
mfwic 44:741ee27c8a34 71 unsigned short getMasterCommands(void){
mfwic 44:741ee27c8a34 72 unsigned short slave_code = 0;
mfwic 52:2b3b194144da 73 if(pulseReady){
mfwic 52:2b3b194144da 74 slave_code = Slave_RXstr();
mfwic 52:2b3b194144da 75 }
mfwic 44:741ee27c8a34 76 return slave_code;
mfwic 44:741ee27c8a34 77 }
mfwic 44:741ee27c8a34 78
mfwic 44:741ee27c8a34 79 /*******************************************************************************
mfwic 44:741ee27c8a34 80 sendSlaveCommands
mfwic 44:741ee27c8a34 81 *******************************************************************************/
mfwic 44:741ee27c8a34 82 void sendSlaveCommands(unsigned short slave_code){
mfwic 44:741ee27c8a34 83
Slord2142 51:abe8a392d50b 84 char data_write;
mfwic 44:741ee27c8a34 85 int status;
mfwic 44:741ee27c8a34 86
mfwic 44:741ee27c8a34 87 // Write slave1 code
Slord2142 51:abe8a392d50b 88 data_write = slave_code | 0x00FF;
Slord2142 51:abe8a392d50b 89 sendByte(1, data_write);
mfwic 44:741ee27c8a34 90
mfwic 44:741ee27c8a34 91 // Write slave2 code
Slord2142 51:abe8a392d50b 92 data_write = (slave_code | 0xFF00) >> 8;
Slord2142 51:abe8a392d50b 93 sendByte(2, data_write);
mfwic 44:741ee27c8a34 94
mfwic 44:741ee27c8a34 95 }
mfwic 44:741ee27c8a34 96
mfwic 44:741ee27c8a34 97 /*******************************************************************************
mfwic 42:3ae73b61f657 98 delay
mfwic 42:3ae73b61f657 99 *******************************************************************************/
mfwic 42:3ae73b61f657 100 void delay(long ticks)
mfwic 42:3ae73b61f657 101 {
mfwic 42:3ae73b61f657 102 long i;
mfwic 42:3ae73b61f657 103 for(i=0;i<ticks;i++);
mfwic 42:3ae73b61f657 104 }
mfwic 42:3ae73b61f657 105
mfwic 42:3ae73b61f657 106 /*******************************************************************************
mfwic 42:3ae73b61f657 107 setBoardControls
mfwic 42:3ae73b61f657 108 *******************************************************************************/
mfwic 42:3ae73b61f657 109 void setBoardControls(unsigned int tCode)
mfwic 42:3ae73b61f657 110 {
mfwic 43:291bbdba48f3 111 //for(unsigned int b=0; b<max_boards; b++){
mfwic 42:3ae73b61f657 112 //wr_out = 0;
mfwic 42:3ae73b61f657 113 //en_out_code = bCodeRow[b];
mfwic 42:3ae73b61f657 114
mfwic 42:3ae73b61f657 115 //wr_out_code = 1<<b;
mfwic 42:3ae73b61f657 116 //wr_out = ~wr_out_code;
mfwic 42:3ae73b61f657 117 //en_out = en_out_code;
mfwic 42:3ae73b61f657 118
mfwic 43:291bbdba48f3 119 //wait_us(250);
mfwic 43:291bbdba48f3 120 //}
mfwic 43:291bbdba48f3 121 //for(unsigned int b=max_boards; b<13; b++){
mfwic 42:3ae73b61f657 122
mfwic 42:3ae73b61f657 123 //wr_out_code = 1<<b;
mfwic 42:3ae73b61f657 124 //wr_out = ~wr_out_code;
mfwic 42:3ae73b61f657 125 //en_out = 0;
mfwic 42:3ae73b61f657 126
mfwic 43:291bbdba48f3 127 //wait_us(250);
mfwic 43:291bbdba48f3 128 //}
mfwic 42:3ae73b61f657 129 }
mfwic 42:3ae73b61f657 130
mfwic 42:3ae73b61f657 131 /*******************************************************************************
mfwic 42:3ae73b61f657 132 setBoardEnables
mfwic 42:3ae73b61f657 133 *******************************************************************************/
mfwic 42:3ae73b61f657 134 unsigned int setBoardEnables(unsigned int tCode)
mfwic 42:3ae73b61f657 135 {
mfwic 42:3ae73b61f657 136 //wr_out = tCode;
mfwic 42:3ae73b61f657 137 //unsigned int en_out_save = en_out;
mfwic 42:3ae73b61f657 138 //en_out = 0;
mfwic 42:3ae73b61f657 139 //wr_out = ~tCode;
mfwic 42:3ae73b61f657 140 //en_out = en_out_save;
mfwic 42:3ae73b61f657 141 return tCode;
mfwic 42:3ae73b61f657 142 }
mfwic 42:3ae73b61f657 143
mfwic 42:3ae73b61f657 144 /*******************************************************************************
mfwic 42:3ae73b61f657 145 setBoardWeights
mfwic 42:3ae73b61f657 146 *******************************************************************************/
mfwic 42:3ae73b61f657 147 unsigned int setBoardWeights(unsigned int bCode)
mfwic 42:3ae73b61f657 148 {
mfwic 42:3ae73b61f657 149 //en_out = bCode;
mfwic 42:3ae73b61f657 150 toggleLatchSignal();
mfwic 42:3ae73b61f657 151 return bCode;
mfwic 42:3ae73b61f657 152 }
mfwic 42:3ae73b61f657 153
mfwic 42:3ae73b61f657 154 /*******************************************************************************
mfwic 42:3ae73b61f657 155 toggleLatchSignal
mfwic 42:3ae73b61f657 156 *******************************************************************************/
mfwic 42:3ae73b61f657 157 void toggleLatchSignal(void){
mfwic 42:3ae73b61f657 158 //extchlat = OFF;
mfwic 42:3ae73b61f657 159 //extchlat = ON;
mfwic 42:3ae73b61f657 160 //extchlat = OFF;
mfwic 42:3ae73b61f657 161 }
mfwic 42:3ae73b61f657 162
mfwic 42:3ae73b61f657 163 /************************************************************
mfwic 42:3ae73b61f657 164 * Routine: checkRange
mfwic 42:3ae73b61f657 165 * Input: setval
mfwic 42:3ae73b61f657 166 * limlo -- low limit
mfwic 42:3ae73b61f657 167 * limhi -- high limit
mfwic 42:3ae73b61f657 168 * Returns: 1 if entry validates and is written
mfwic 42:3ae73b61f657 169 * 0 if entry fails
mfwic 42:3ae73b61f657 170 * -1 if it slips past
mfwic 42:3ae73b61f657 171 * Description:
mfwic 42:3ae73b61f657 172 * Checks if setvalue is between the given limits.
mfwic 42:3ae73b61f657 173 *
mfwic 42:3ae73b61f657 174 **************************************************************/
mfwic 42:3ae73b61f657 175 int checkRange(int setvalue, int limlo, int limhi)
mfwic 42:3ae73b61f657 176 {
mfwic 42:3ae73b61f657 177 if ((setvalue >= limlo) && (setvalue <= limhi)){
mfwic 42:3ae73b61f657 178 return 1;
mfwic 42:3ae73b61f657 179 }else{
mfwic 42:3ae73b61f657 180 return 0;
mfwic 42:3ae73b61f657 181 }
mfwic 42:3ae73b61f657 182 }
mfwic 42:3ae73b61f657 183
mfwic 42:3ae73b61f657 184 /*******************************************************************************
mfwic 42:3ae73b61f657 185 startConverter
mfwic 42:3ae73b61f657 186 *******************************************************************************/
mfwic 42:3ae73b61f657 187 void startConverter(unsigned int reg)
mfwic 42:3ae73b61f657 188 {
mfwic 42:3ae73b61f657 189 if(!running){
mfwic 42:3ae73b61f657 190 running = TRUE;
mfwic 42:3ae73b61f657 191 // Fire in the hole!
mfwic 42:3ae73b61f657 192 //wr_out_code = setBoardEnables(reg);
mfwic 42:3ae73b61f657 193
mfwic 42:3ae73b61f657 194 sprintf(strbuf, "\r\nConverter started");
mfwic 42:3ae73b61f657 195 sendSerial(strbuf);
mfwic 42:3ae73b61f657 196 }
mfwic 42:3ae73b61f657 197 }
mfwic 42:3ae73b61f657 198
mfwic 42:3ae73b61f657 199 /*******************************************************************************
mfwic 42:3ae73b61f657 200 stopConverter - stop the converter and set outputs to 0
mfwic 42:3ae73b61f657 201 *******************************************************************************/
mfwic 42:3ae73b61f657 202 void stopConverter(void)
mfwic 42:3ae73b61f657 203 {
mfwic 42:3ae73b61f657 204 if(running){
mfwic 42:3ae73b61f657 205 //en_out = 32;
mfwic 42:3ae73b61f657 206 //wr_out_code = setBoardEnables(ALLON);
mfwic 42:3ae73b61f657 207 running = FALSE;
mfwic 42:3ae73b61f657 208 sprintf(strbuf, "\r\nConverter stopped");
mfwic 42:3ae73b61f657 209 sendSerial(strbuf);
mfwic 42:3ae73b61f657 210 }
mfwic 42:3ae73b61f657 211 }
mfwic 42:3ae73b61f657 212
mfwic 42:3ae73b61f657 213 /*******************************************************************************
mfwic 42:3ae73b61f657 214 checkLevels
mfwic 42:3ae73b61f657 215 *******************************************************************************/
mfwic 42:3ae73b61f657 216 struct statusValues checkLevels(struct adcValues adcVals){
mfwic 42:3ae73b61f657 217
mfwic 42:3ae73b61f657 218 struct statusValues statVals;
mfwic 42:3ae73b61f657 219
mfwic 42:3ae73b61f657 220 // Check 48V levels
mfwic 42:3ae73b61f657 221 if(adcVals.v48 > V48_HI){
mfwic 42:3ae73b61f657 222 statVals.V48_IS_HI = TRUE;
mfwic 42:3ae73b61f657 223 statVals.V48_IS_LO = FALSE;
mfwic 42:3ae73b61f657 224 }else if(adcVals.v48 < V48_LO){
mfwic 42:3ae73b61f657 225 statVals.V48_IS_HI = FALSE;
mfwic 42:3ae73b61f657 226 statVals.V48_IS_LO = TRUE;
mfwic 42:3ae73b61f657 227 }else{
mfwic 42:3ae73b61f657 228 statVals.V48_IS_HI = FALSE;
mfwic 42:3ae73b61f657 229 statVals.V48_IS_LO = FALSE;
mfwic 42:3ae73b61f657 230 }
mfwic 42:3ae73b61f657 231
mfwic 42:3ae73b61f657 232 // Check 12V levels
mfwic 42:3ae73b61f657 233 if(adcVals.v12 > V12_HI){
mfwic 42:3ae73b61f657 234 statVals.V12_IS_HI = TRUE;
mfwic 42:3ae73b61f657 235 statVals.V12_IS_LO = FALSE;
mfwic 42:3ae73b61f657 236 }else if(adcVals.v12 < V12_LO){
mfwic 42:3ae73b61f657 237 statVals.V12_IS_HI = FALSE;
mfwic 42:3ae73b61f657 238 statVals.V12_IS_LO = TRUE;
mfwic 42:3ae73b61f657 239 }else{
mfwic 42:3ae73b61f657 240 statVals.V12_IS_HI = FALSE;
mfwic 42:3ae73b61f657 241 statVals.V12_IS_LO = FALSE;
mfwic 42:3ae73b61f657 242 }
mfwic 42:3ae73b61f657 243 return statVals;
mfwic 42:3ae73b61f657 244 }
mfwic 42:3ae73b61f657 245
mfwic 42:3ae73b61f657 246
mfwic 42:3ae73b61f657 247 /*******************************************************************************
mfwic 44:741ee27c8a34 248 updateMasterControls
mfwic 42:3ae73b61f657 249 *******************************************************************************/
mfwic 44:741ee27c8a34 250 unsigned short updateMasterControls(unsigned short ref){
mfwic 42:3ae73b61f657 251
mfwic 42:3ae73b61f657 252 unsigned int tBuf = getLUT_thermCode(ref);
mfwic 43:291bbdba48f3 253 //if(max_boards <= 3){
mfwic 43:291bbdba48f3 254 // getLUT_binCodeArray(ref);
mfwic 43:291bbdba48f3 255 // setBoardControls(tBuf);
mfwic 43:291bbdba48f3 256 //}else{
mfwic 43:291bbdba48f3 257 //wr_out_code = setBoardEnables(tBuf);
mfwic 43:291bbdba48f3 258 //unsigned int bBuf = getLUT_binCode(ref);
mfwic 43:291bbdba48f3 259 //en_out_code = setBoardWeights(bBuf);
mfwic 43:291bbdba48f3 260 //}
mfwic 44:741ee27c8a34 261 unsigned short slave_code = 0;
mfwic 44:741ee27c8a34 262 return slave_code;
mfwic 42:3ae73b61f657 263 }
mfwic 42:3ae73b61f657 264
mfwic 42:3ae73b61f657 265 void XupdateControls(unsigned short ref){
mfwic 42:3ae73b61f657 266
mfwic 42:3ae73b61f657 267 sprintf(strbuf, "refr=%d\r\n", ref);
mfwic 42:3ae73b61f657 268 sendSerial(strbuf);
mfwic 42:3ae73b61f657 269
mfwic 42:3ae73b61f657 270 ref = ref/64;
mfwic 42:3ae73b61f657 271
mfwic 42:3ae73b61f657 272 sprintf(strbuf, "refc=%d\r\n", ref);
mfwic 42:3ae73b61f657 273 sendSerial(strbuf);
mfwic 42:3ae73b61f657 274
mfwic 42:3ae73b61f657 275 sendSerial("enter updateControls\r\n");
mfwic 42:3ae73b61f657 276 unsigned int cBuf = getLUT_thermCode(ref);
mfwic 42:3ae73b61f657 277 sendSerial("cBuf1 updateControls\r\n");
mfwic 43:291bbdba48f3 278 //wr_out_code = setBoardEnables(cBuf);
mfwic 42:3ae73b61f657 279 sendSerial("wr_out_code updateControls\r\n");
mfwic 42:3ae73b61f657 280
mfwic 43:291bbdba48f3 281 //cBuf = getLUT_binCode(ref);
mfwic 42:3ae73b61f657 282 sendSerial("cBuf2 updateControls\r\n");
mfwic 43:291bbdba48f3 283 //en_out_code = setBoardWeights(cBuf);
mfwic 42:3ae73b61f657 284 sendSerial("en_out_code updateControls\r\n");
mfwic 42:3ae73b61f657 285
mfwic 42:3ae73b61f657 286 wait(0.5);
mfwic 44:741ee27c8a34 287 }
mfwic 44:741ee27c8a34 288
mfwic 44:741ee27c8a34 289 /*******************************************************************************
mfwic 44:741ee27c8a34 290 updateSlaveControls
mfwic 44:741ee27c8a34 291 *******************************************************************************/
mfwic 44:741ee27c8a34 292 void updateSlaveControls(unsigned short ref){
mfwic 44:741ee27c8a34 293
mfwic 44:741ee27c8a34 294 unsigned int tBuf = getLUT_thermCode(ref);
mfwic 44:741ee27c8a34 295 //if(max_boards <= 3){
mfwic 44:741ee27c8a34 296 // getLUT_binCodeArray(ref);
mfwic 44:741ee27c8a34 297 // setBoardControls(tBuf);
mfwic 44:741ee27c8a34 298 //}else{
mfwic 44:741ee27c8a34 299 //wr_out_code = setBoardEnables(tBuf);
mfwic 44:741ee27c8a34 300 //unsigned int bBuf = getLUT_binCode(ref);
mfwic 44:741ee27c8a34 301 //en_out_code = setBoardWeights(bBuf);
mfwic 44:741ee27c8a34 302 //}
mfwic 42:3ae73b61f657 303 }