Treehouse Mbed Team / Mbed 2 deprecated APS_DCM1SL

Dependencies:   mbed

Committer:
mfwic
Date:
Sat Mar 09 22:32:40 2019 +0000
Revision:
44:741ee27c8a34
Parent:
43:291bbdba48f3
Child:
46:0de65f1bd714
Added Master Slave controls.; Added I2C subroutines in boards.cpp.

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