Treehouse Mbed Team / Mbed 2 deprecated APS_DCM1SL

Dependencies:   mbed

Committer:
mfwic
Date:
Sun Mar 10 18:03:46 2019 +0000
Revision:
46:0de65f1bd714
Parent:
44:741ee27c8a34
Child:
48:8e9de9ff1f22
Added slave address statements to globals.h

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"
mfwic 42:3ae73b61f657 37
mfwic 42:3ae73b61f657 38 unsigned int boardEnableBits;
mfwic 42:3ae73b61f657 39
mfwic 44:741ee27c8a34 40 I2C i2c(PB_6, PB_7);
mfwic 44:741ee27c8a34 41
mfwic 44:741ee27c8a34 42 /*******************************************************************************
mfwic 44:741ee27c8a34 43 initI2C
mfwic 44:741ee27c8a34 44 *******************************************************************************/
mfwic 44:741ee27c8a34 45 void initI2C(void){
mfwic 44:741ee27c8a34 46
mfwic 44:741ee27c8a34 47 }
mfwic 44:741ee27c8a34 48
mfwic 44:741ee27c8a34 49 /*******************************************************************************
mfwic 44:741ee27c8a34 50 initBoards
mfwic 44:741ee27c8a34 51 *******************************************************************************/
mfwic 42:3ae73b61f657 52 void initBoards(struct adcValues adcVals){
mfwic 42:3ae73b61f657 53 //en_out = 32;
mfwic 46:0de65f1bd714 54 //setBoardEnables(16383);
mfwic 46:0de65f1bd714 55
mfwic 46:0de65f1bd714 56 //Need to enable all chips fast. NEW code here
mfwic 42:3ae73b61f657 57
mfwic 42:3ae73b61f657 58 }
mfwic 42:3ae73b61f657 59
mfwic 42:3ae73b61f657 60 /*******************************************************************************
mfwic 44:741ee27c8a34 61 getMasterCommands
mfwic 44:741ee27c8a34 62 *******************************************************************************/
mfwic 44:741ee27c8a34 63 unsigned short getMasterCommands(void){
mfwic 44:741ee27c8a34 64 unsigned short slave_code = 0;
mfwic 46:0de65f1bd714 65 //NEW code here
mfwic 44:741ee27c8a34 66 return slave_code;
mfwic 44:741ee27c8a34 67 }
mfwic 44:741ee27c8a34 68
mfwic 44:741ee27c8a34 69 /*******************************************************************************
mfwic 44:741ee27c8a34 70 sendSlaveCommands
mfwic 44:741ee27c8a34 71 *******************************************************************************/
mfwic 44:741ee27c8a34 72 void sendSlaveCommands(unsigned short slave_code){
mfwic 44:741ee27c8a34 73
mfwic 44:741ee27c8a34 74 char data_write[2];
mfwic 44:741ee27c8a34 75 int status;
mfwic 44:741ee27c8a34 76
mfwic 44:741ee27c8a34 77 // Write slave1 code
mfwic 44:741ee27c8a34 78 data_write[1] = 0;
mfwic 44:741ee27c8a34 79 data_write[0] = slave_code | 0x00FF;
mfwic 44:741ee27c8a34 80 status = i2c.write(i2c_addr_slave1, data_write, 2, 0);
mfwic 44:741ee27c8a34 81
mfwic 44:741ee27c8a34 82 // Write slave2 code
mfwic 44:741ee27c8a34 83 data_write[1] = 0;
mfwic 44:741ee27c8a34 84 data_write[0] = slave_code | 0xFF00;
mfwic 44:741ee27c8a34 85 status = i2c.write(i2c_addr_slave2, data_write, 2, 0);
mfwic 44:741ee27c8a34 86
mfwic 44:741ee27c8a34 87 }
mfwic 44:741ee27c8a34 88
mfwic 44:741ee27c8a34 89 /*******************************************************************************
mfwic 42:3ae73b61f657 90 delay
mfwic 42:3ae73b61f657 91 *******************************************************************************/
mfwic 42:3ae73b61f657 92 void delay(long ticks)
mfwic 42:3ae73b61f657 93 {
mfwic 42:3ae73b61f657 94 long i;
mfwic 42:3ae73b61f657 95 for(i=0;i<ticks;i++);
mfwic 42:3ae73b61f657 96 }
mfwic 42:3ae73b61f657 97
mfwic 42:3ae73b61f657 98 /*******************************************************************************
mfwic 42:3ae73b61f657 99 setBoardControls
mfwic 42:3ae73b61f657 100 *******************************************************************************/
mfwic 42:3ae73b61f657 101 void setBoardControls(unsigned int tCode)
mfwic 42:3ae73b61f657 102 {
mfwic 43:291bbdba48f3 103 //for(unsigned int b=0; b<max_boards; b++){
mfwic 42:3ae73b61f657 104 //wr_out = 0;
mfwic 42:3ae73b61f657 105 //en_out_code = bCodeRow[b];
mfwic 42:3ae73b61f657 106
mfwic 42:3ae73b61f657 107 //wr_out_code = 1<<b;
mfwic 42:3ae73b61f657 108 //wr_out = ~wr_out_code;
mfwic 42:3ae73b61f657 109 //en_out = en_out_code;
mfwic 42:3ae73b61f657 110
mfwic 43:291bbdba48f3 111 //wait_us(250);
mfwic 43:291bbdba48f3 112 //}
mfwic 43:291bbdba48f3 113 //for(unsigned int b=max_boards; b<13; 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 = 0;
mfwic 42:3ae73b61f657 118
mfwic 43:291bbdba48f3 119 //wait_us(250);
mfwic 43:291bbdba48f3 120 //}
mfwic 42:3ae73b61f657 121 }
mfwic 42:3ae73b61f657 122
mfwic 42:3ae73b61f657 123 /*******************************************************************************
mfwic 42:3ae73b61f657 124 setBoardEnables
mfwic 42:3ae73b61f657 125 *******************************************************************************/
mfwic 42:3ae73b61f657 126 unsigned int setBoardEnables(unsigned int tCode)
mfwic 42:3ae73b61f657 127 {
mfwic 42:3ae73b61f657 128 //wr_out = tCode;
mfwic 42:3ae73b61f657 129 //unsigned int en_out_save = en_out;
mfwic 42:3ae73b61f657 130 //en_out = 0;
mfwic 42:3ae73b61f657 131 //wr_out = ~tCode;
mfwic 42:3ae73b61f657 132 //en_out = en_out_save;
mfwic 42:3ae73b61f657 133 return tCode;
mfwic 42:3ae73b61f657 134 }
mfwic 42:3ae73b61f657 135
mfwic 42:3ae73b61f657 136 /*******************************************************************************
mfwic 42:3ae73b61f657 137 setBoardWeights
mfwic 42:3ae73b61f657 138 *******************************************************************************/
mfwic 42:3ae73b61f657 139 unsigned int setBoardWeights(unsigned int bCode)
mfwic 42:3ae73b61f657 140 {
mfwic 42:3ae73b61f657 141 //en_out = bCode;
mfwic 42:3ae73b61f657 142 toggleLatchSignal();
mfwic 42:3ae73b61f657 143 return bCode;
mfwic 42:3ae73b61f657 144 }
mfwic 42:3ae73b61f657 145
mfwic 42:3ae73b61f657 146 /*******************************************************************************
mfwic 42:3ae73b61f657 147 toggleLatchSignal
mfwic 42:3ae73b61f657 148 *******************************************************************************/
mfwic 42:3ae73b61f657 149 void toggleLatchSignal(void){
mfwic 42:3ae73b61f657 150 //extchlat = OFF;
mfwic 42:3ae73b61f657 151 //extchlat = ON;
mfwic 42:3ae73b61f657 152 //extchlat = OFF;
mfwic 42:3ae73b61f657 153 }
mfwic 42:3ae73b61f657 154
mfwic 42:3ae73b61f657 155 /************************************************************
mfwic 42:3ae73b61f657 156 * Routine: checkRange
mfwic 42:3ae73b61f657 157 * Input: setval
mfwic 42:3ae73b61f657 158 * limlo -- low limit
mfwic 42:3ae73b61f657 159 * limhi -- high limit
mfwic 42:3ae73b61f657 160 * Returns: 1 if entry validates and is written
mfwic 42:3ae73b61f657 161 * 0 if entry fails
mfwic 42:3ae73b61f657 162 * -1 if it slips past
mfwic 42:3ae73b61f657 163 * Description:
mfwic 42:3ae73b61f657 164 * Checks if setvalue is between the given limits.
mfwic 42:3ae73b61f657 165 *
mfwic 42:3ae73b61f657 166 **************************************************************/
mfwic 42:3ae73b61f657 167 int checkRange(int setvalue, int limlo, int limhi)
mfwic 42:3ae73b61f657 168 {
mfwic 42:3ae73b61f657 169 if ((setvalue >= limlo) && (setvalue <= limhi)){
mfwic 42:3ae73b61f657 170 return 1;
mfwic 42:3ae73b61f657 171 }else{
mfwic 42:3ae73b61f657 172 return 0;
mfwic 42:3ae73b61f657 173 }
mfwic 42:3ae73b61f657 174 }
mfwic 42:3ae73b61f657 175
mfwic 42:3ae73b61f657 176 /*******************************************************************************
mfwic 42:3ae73b61f657 177 startConverter
mfwic 42:3ae73b61f657 178 *******************************************************************************/
mfwic 42:3ae73b61f657 179 void startConverter(unsigned int reg)
mfwic 42:3ae73b61f657 180 {
mfwic 42:3ae73b61f657 181 if(!running){
mfwic 42:3ae73b61f657 182 running = TRUE;
mfwic 42:3ae73b61f657 183 // Fire in the hole!
mfwic 42:3ae73b61f657 184 //wr_out_code = setBoardEnables(reg);
mfwic 42:3ae73b61f657 185
mfwic 42:3ae73b61f657 186 sprintf(strbuf, "\r\nConverter started");
mfwic 42:3ae73b61f657 187 sendSerial(strbuf);
mfwic 42:3ae73b61f657 188 }
mfwic 42:3ae73b61f657 189 }
mfwic 42:3ae73b61f657 190
mfwic 42:3ae73b61f657 191 /*******************************************************************************
mfwic 42:3ae73b61f657 192 stopConverter - stop the converter and set outputs to 0
mfwic 42:3ae73b61f657 193 *******************************************************************************/
mfwic 42:3ae73b61f657 194 void stopConverter(void)
mfwic 42:3ae73b61f657 195 {
mfwic 42:3ae73b61f657 196 if(running){
mfwic 42:3ae73b61f657 197 //en_out = 32;
mfwic 42:3ae73b61f657 198 //wr_out_code = setBoardEnables(ALLON);
mfwic 42:3ae73b61f657 199 running = FALSE;
mfwic 42:3ae73b61f657 200 sprintf(strbuf, "\r\nConverter stopped");
mfwic 42:3ae73b61f657 201 sendSerial(strbuf);
mfwic 42:3ae73b61f657 202 }
mfwic 42:3ae73b61f657 203 }
mfwic 42:3ae73b61f657 204
mfwic 42:3ae73b61f657 205 /*******************************************************************************
mfwic 42:3ae73b61f657 206 checkLevels
mfwic 42:3ae73b61f657 207 *******************************************************************************/
mfwic 42:3ae73b61f657 208 struct statusValues checkLevels(struct adcValues adcVals){
mfwic 42:3ae73b61f657 209
mfwic 42:3ae73b61f657 210 struct statusValues statVals;
mfwic 42:3ae73b61f657 211
mfwic 42:3ae73b61f657 212 // Check 48V levels
mfwic 42:3ae73b61f657 213 if(adcVals.v48 > V48_HI){
mfwic 42:3ae73b61f657 214 statVals.V48_IS_HI = TRUE;
mfwic 42:3ae73b61f657 215 statVals.V48_IS_LO = FALSE;
mfwic 42:3ae73b61f657 216 }else if(adcVals.v48 < V48_LO){
mfwic 42:3ae73b61f657 217 statVals.V48_IS_HI = FALSE;
mfwic 42:3ae73b61f657 218 statVals.V48_IS_LO = TRUE;
mfwic 42:3ae73b61f657 219 }else{
mfwic 42:3ae73b61f657 220 statVals.V48_IS_HI = FALSE;
mfwic 42:3ae73b61f657 221 statVals.V48_IS_LO = FALSE;
mfwic 42:3ae73b61f657 222 }
mfwic 42:3ae73b61f657 223
mfwic 42:3ae73b61f657 224 // Check 12V levels
mfwic 42:3ae73b61f657 225 if(adcVals.v12 > V12_HI){
mfwic 42:3ae73b61f657 226 statVals.V12_IS_HI = TRUE;
mfwic 42:3ae73b61f657 227 statVals.V12_IS_LO = FALSE;
mfwic 42:3ae73b61f657 228 }else if(adcVals.v12 < V12_LO){
mfwic 42:3ae73b61f657 229 statVals.V12_IS_HI = FALSE;
mfwic 42:3ae73b61f657 230 statVals.V12_IS_LO = TRUE;
mfwic 42:3ae73b61f657 231 }else{
mfwic 42:3ae73b61f657 232 statVals.V12_IS_HI = FALSE;
mfwic 42:3ae73b61f657 233 statVals.V12_IS_LO = FALSE;
mfwic 42:3ae73b61f657 234 }
mfwic 42:3ae73b61f657 235 return statVals;
mfwic 42:3ae73b61f657 236 }
mfwic 42:3ae73b61f657 237
mfwic 42:3ae73b61f657 238
mfwic 42:3ae73b61f657 239 /*******************************************************************************
mfwic 44:741ee27c8a34 240 updateMasterControls
mfwic 42:3ae73b61f657 241 *******************************************************************************/
mfwic 44:741ee27c8a34 242 unsigned short updateMasterControls(unsigned short ref){
mfwic 42:3ae73b61f657 243
mfwic 42:3ae73b61f657 244 unsigned int tBuf = getLUT_thermCode(ref);
mfwic 43:291bbdba48f3 245 //if(max_boards <= 3){
mfwic 43:291bbdba48f3 246 // getLUT_binCodeArray(ref);
mfwic 43:291bbdba48f3 247 // setBoardControls(tBuf);
mfwic 43:291bbdba48f3 248 //}else{
mfwic 43:291bbdba48f3 249 //wr_out_code = setBoardEnables(tBuf);
mfwic 43:291bbdba48f3 250 //unsigned int bBuf = getLUT_binCode(ref);
mfwic 43:291bbdba48f3 251 //en_out_code = setBoardWeights(bBuf);
mfwic 43:291bbdba48f3 252 //}
mfwic 44:741ee27c8a34 253 unsigned short slave_code = 0;
mfwic 44:741ee27c8a34 254 return slave_code;
mfwic 42:3ae73b61f657 255 }
mfwic 42:3ae73b61f657 256
mfwic 42:3ae73b61f657 257 void XupdateControls(unsigned short ref){
mfwic 42:3ae73b61f657 258
mfwic 42:3ae73b61f657 259 sprintf(strbuf, "refr=%d\r\n", ref);
mfwic 42:3ae73b61f657 260 sendSerial(strbuf);
mfwic 42:3ae73b61f657 261
mfwic 42:3ae73b61f657 262 ref = ref/64;
mfwic 42:3ae73b61f657 263
mfwic 42:3ae73b61f657 264 sprintf(strbuf, "refc=%d\r\n", ref);
mfwic 42:3ae73b61f657 265 sendSerial(strbuf);
mfwic 42:3ae73b61f657 266
mfwic 42:3ae73b61f657 267 sendSerial("enter updateControls\r\n");
mfwic 42:3ae73b61f657 268 unsigned int cBuf = getLUT_thermCode(ref);
mfwic 42:3ae73b61f657 269 sendSerial("cBuf1 updateControls\r\n");
mfwic 43:291bbdba48f3 270 //wr_out_code = setBoardEnables(cBuf);
mfwic 42:3ae73b61f657 271 sendSerial("wr_out_code updateControls\r\n");
mfwic 42:3ae73b61f657 272
mfwic 43:291bbdba48f3 273 //cBuf = getLUT_binCode(ref);
mfwic 42:3ae73b61f657 274 sendSerial("cBuf2 updateControls\r\n");
mfwic 43:291bbdba48f3 275 //en_out_code = setBoardWeights(cBuf);
mfwic 42:3ae73b61f657 276 sendSerial("en_out_code updateControls\r\n");
mfwic 42:3ae73b61f657 277
mfwic 42:3ae73b61f657 278 wait(0.5);
mfwic 44:741ee27c8a34 279 }
mfwic 44:741ee27c8a34 280
mfwic 44:741ee27c8a34 281 /*******************************************************************************
mfwic 44:741ee27c8a34 282 updateSlaveControls
mfwic 44:741ee27c8a34 283 *******************************************************************************/
mfwic 44:741ee27c8a34 284 void updateSlaveControls(unsigned short ref){
mfwic 44:741ee27c8a34 285
mfwic 44:741ee27c8a34 286 unsigned int tBuf = getLUT_thermCode(ref);
mfwic 44:741ee27c8a34 287 //if(max_boards <= 3){
mfwic 44:741ee27c8a34 288 // getLUT_binCodeArray(ref);
mfwic 44:741ee27c8a34 289 // setBoardControls(tBuf);
mfwic 44:741ee27c8a34 290 //}else{
mfwic 44:741ee27c8a34 291 //wr_out_code = setBoardEnables(tBuf);
mfwic 44:741ee27c8a34 292 //unsigned int bBuf = getLUT_binCode(ref);
mfwic 44:741ee27c8a34 293 //en_out_code = setBoardWeights(bBuf);
mfwic 44:741ee27c8a34 294 //}
mfwic 42:3ae73b61f657 295 }