Library for the MAX11300

Committer:
elelthvd
Date:
Thu Aug 06 08:22:37 2020 +0000
Revision:
20:d4ef18312354
Parent:
19:2d747d4b424b
wait() is deprecated.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
j3 7:8669a53acd0d 1 /**********************************************************************
j3 7:8669a53acd0d 2 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
j3 7:8669a53acd0d 3 *
j3 7:8669a53acd0d 4 * Permission is hereby granted, free of charge, to any person obtaining a
j3 7:8669a53acd0d 5 * copy of this software and associated documentation files (the "Software"),
j3 7:8669a53acd0d 6 * to deal in the Software without restriction, including without limitation
j3 7:8669a53acd0d 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
j3 7:8669a53acd0d 8 * and/or sell copies of the Software, and to permit persons to whom the
j3 7:8669a53acd0d 9 * Software is furnished to do so, subject to the following conditions:
j3 7:8669a53acd0d 10 *
j3 7:8669a53acd0d 11 * The above copyright notice and this permission notice shall be included
j3 7:8669a53acd0d 12 * in all copies or substantial portions of the Software.
j3 7:8669a53acd0d 13 *
j3 7:8669a53acd0d 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
j3 7:8669a53acd0d 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
j3 7:8669a53acd0d 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
j3 7:8669a53acd0d 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
j3 7:8669a53acd0d 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
j3 7:8669a53acd0d 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
j3 7:8669a53acd0d 20 * OTHER DEALINGS IN THE SOFTWARE.
j3 7:8669a53acd0d 21 *
j3 7:8669a53acd0d 22 * Except as contained in this notice, the name of Maxim Integrated
j3 7:8669a53acd0d 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
j3 7:8669a53acd0d 24 * Products, Inc. Branding Policy.
j3 7:8669a53acd0d 25 *
j3 7:8669a53acd0d 26 * The mere transfer of this software does not imply any licenses
j3 7:8669a53acd0d 27 * of trade secrets, proprietary technology, copyrights, patents,
j3 7:8669a53acd0d 28 * trademarks, maskwork rights, or any other form of intellectual
j3 7:8669a53acd0d 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
j3 7:8669a53acd0d 30 * ownership rights.
j3 7:8669a53acd0d 31 **********************************************************************/
j3 7:8669a53acd0d 32
j3 9:094df3de3616 33
j3 7:8669a53acd0d 34 #include "MAX113XX_Pixi.h"
j3 7:8669a53acd0d 35
j3 9:094df3de3616 36
j3 9:094df3de3616 37 //20 port devices
j3 9:094df3de3616 38 #if defined(_MAX11300_DESIGNVALUE_H_) || defined(_MAX11301_DESIGNVALUE_H_)
j3 12:8054ee101bad 39
j3 12:8054ee101bad 40 static const uint16_t configDesignVals[18] = {
j3 12:8054ee101bad 41 gpo_data_15_to_0_DESIGNVALUE,
j3 12:8054ee101bad 42 gpo_data_19_to_16_DESIGNVALUE,
j3 12:8054ee101bad 43 0, //reserved
j3 12:8054ee101bad 44 device_control_DESIGNVALUE,
j3 12:8054ee101bad 45 interrupt_mask_DESIGNVALUE,
j3 12:8054ee101bad 46 gpi_irqmode_7_to_0_DESIGNVALUE,
j3 12:8054ee101bad 47 gpi_irqmode_15_to_8_DESIGNVALUE,
j3 12:8054ee101bad 48 gpi_irqmode_19_to_16_DESIGNVALUE,
j3 12:8054ee101bad 49 0, //reserved
j3 12:8054ee101bad 50 dac_preset_data_1_DESIGNVALUE,
j3 12:8054ee101bad 51 dac_preset_data_2_DESIGNVALUE,
j3 12:8054ee101bad 52 tmp_mon_cfg_DESIGNVALUE,
j3 12:8054ee101bad 53 tmp_mon_int_hi_thresh_DESIGNVALUE,
j3 12:8054ee101bad 54 tmp_mon_int_lo_thresh_DESIGNVALUE,
j3 12:8054ee101bad 55 tmp_mon_ext1_hi_thresh_DESIGNVALUE,
j3 12:8054ee101bad 56 tmp_mon_ext1_lo_thresh_DESIGNVALUE,
j3 12:8054ee101bad 57 tmp_mon_ext2_hi_thresh_DESIGNVALUE,
j3 12:8054ee101bad 58 tmp_mon_ext2_lo_thresh_DESIGNVALUE};
j3 12:8054ee101bad 59
j3 9:094df3de3616 60 static const uint16_t portConfigDesignVals[20] = {
j3 7:8669a53acd0d 61 port_cfg_00_DESIGNVALUE,
j3 7:8669a53acd0d 62 port_cfg_01_DESIGNVALUE,
j3 7:8669a53acd0d 63 port_cfg_02_DESIGNVALUE,
j3 7:8669a53acd0d 64 port_cfg_03_DESIGNVALUE,
j3 7:8669a53acd0d 65 port_cfg_04_DESIGNVALUE,
j3 7:8669a53acd0d 66 port_cfg_05_DESIGNVALUE,
j3 7:8669a53acd0d 67 port_cfg_06_DESIGNVALUE,
j3 7:8669a53acd0d 68 port_cfg_07_DESIGNVALUE,
j3 7:8669a53acd0d 69 port_cfg_08_DESIGNVALUE,
j3 7:8669a53acd0d 70 port_cfg_09_DESIGNVALUE,
j3 7:8669a53acd0d 71 port_cfg_10_DESIGNVALUE,
j3 7:8669a53acd0d 72 port_cfg_11_DESIGNVALUE,
j3 7:8669a53acd0d 73 port_cfg_12_DESIGNVALUE,
j3 7:8669a53acd0d 74 port_cfg_13_DESIGNVALUE,
j3 7:8669a53acd0d 75 port_cfg_14_DESIGNVALUE,
j3 7:8669a53acd0d 76 port_cfg_15_DESIGNVALUE,
j3 7:8669a53acd0d 77 port_cfg_16_DESIGNVALUE,
j3 7:8669a53acd0d 78 port_cfg_17_DESIGNVALUE,
j3 7:8669a53acd0d 79 port_cfg_18_DESIGNVALUE,
j3 7:8669a53acd0d 80 port_cfg_19_DESIGNVALUE};
j3 12:8054ee101bad 81
j3 12:8054ee101bad 82 static const uint16_t dacDesignVals[20]= {
j3 12:8054ee101bad 83 dac_data_port_00_DESIGNVALUE,
j3 12:8054ee101bad 84 dac_data_port_01_DESIGNVALUE,
j3 12:8054ee101bad 85 dac_data_port_02_DESIGNVALUE,
j3 12:8054ee101bad 86 dac_data_port_03_DESIGNVALUE,
j3 12:8054ee101bad 87 dac_data_port_04_DESIGNVALUE,
j3 12:8054ee101bad 88 dac_data_port_05_DESIGNVALUE,
j3 12:8054ee101bad 89 dac_data_port_06_DESIGNVALUE,
j3 12:8054ee101bad 90 dac_data_port_07_DESIGNVALUE,
j3 12:8054ee101bad 91 dac_data_port_08_DESIGNVALUE,
j3 12:8054ee101bad 92 dac_data_port_09_DESIGNVALUE,
j3 12:8054ee101bad 93 dac_data_port_10_DESIGNVALUE,
j3 12:8054ee101bad 94 dac_data_port_11_DESIGNVALUE,
j3 12:8054ee101bad 95 dac_data_port_12_DESIGNVALUE,
j3 12:8054ee101bad 96 dac_data_port_13_DESIGNVALUE,
j3 12:8054ee101bad 97 dac_data_port_14_DESIGNVALUE,
j3 12:8054ee101bad 98 dac_data_port_15_DESIGNVALUE,
j3 12:8054ee101bad 99 dac_data_port_16_DESIGNVALUE,
j3 12:8054ee101bad 100 dac_data_port_17_DESIGNVALUE,
j3 12:8054ee101bad 101 dac_data_port_18_DESIGNVALUE,
j3 12:8054ee101bad 102 dac_data_port_19_DESIGNVALUE};
j3 12:8054ee101bad 103
j3 9:094df3de3616 104 #endif
j3 9:094df3de3616 105
j3 9:094df3de3616 106 //12 port devices...
j3 9:094df3de3616 107 #if defined(_MAX11311_DESIGNVALUE_H_) || defined(_MAX11312_DESIGNVALUE_H_)
j3 9:094df3de3616 108 static const uint16_t portConfigDesignVals[12] = {
j3 9:094df3de3616 109 port_cfg_p0_DESIGNVALUE,
j3 9:094df3de3616 110 port_cfg_p1_DESIGNVALUE,
j3 9:094df3de3616 111 port_cfg_p2_DESIGNVALUE,
j3 9:094df3de3616 112 port_cfg_p3_DESIGNVALUE,
j3 9:094df3de3616 113 port_cfg_p4_DESIGNVALUE,
j3 9:094df3de3616 114 port_cfg_p5_DESIGNVALUE,
j3 9:094df3de3616 115 port_cfg_p6_DESIGNVALUE,
j3 9:094df3de3616 116 port_cfg_p7_DESIGNVALUE,
j3 9:094df3de3616 117 port_cfg_p8_DESIGNVALUE,
j3 9:094df3de3616 118 port_cfg_p9_DESIGNVALUE,
j3 9:094df3de3616 119 port_cfg_p10_DESIGNVALUE,
j3 9:094df3de3616 120 port_cfg_p11_DESIGNVALUE};
j3 9:094df3de3616 121 #endif
j3 9:094df3de3616 122
j3 7:8669a53acd0d 123
j3 7:8669a53acd0d 124 //************************** Base Class member fxs *****************************
j3 9:094df3de3616 125 MAX113XX_Pixi::MAX113XX_Pixi(Device_e device, PinName cnvt):
j3 9:094df3de3616 126 m_device(device), m_cnvt(cnvt, 1)
j3 7:8669a53acd0d 127 {
j3 11:31e7ca030b8f 128 }
j3 11:31e7ca030b8f 129
j3 12:8054ee101bad 130 //*********************************************************************
j3 12:8054ee101bad 131 MAX113XX_Pixi::CmdResult_e MAX113XX_Pixi::singleEndedADCRead(Ports_e port,
j3 12:8054ee101bad 132 uint16_t &data)
j3 12:8054ee101bad 133 {
j3 12:8054ee101bad 134 MAX113XX_Pixi::CmdResult_e result = MAX113XX_Pixi::OpFailure;
j3 12:8054ee101bad 135
j3 15:fb674d741844 136 //check for type of device
j3 12:8054ee101bad 137 if(m_device == MAX11300 || m_device == MAX11301) //20 port device
j3 12:8054ee101bad 138 {
j3 15:fb674d741844 139 //is this port configured for this operation?
j3 12:8054ee101bad 140 if(((portConfigDesignVals[port] & 0xF000) >> 12) == MAX113XX_Pixi::MODE_7)
j3 12:8054ee101bad 141 {
j3 15:fb674d741844 142 //Is device configured for continuous sweep?
j3 15:fb674d741844 143 if((device_control_DESIGNVALUE & device_control_ADCCTL) != MAX113XX_Pixi::ContinuousSweep)
j3 12:8054ee101bad 144 {
j3 15:fb674d741844 145 //No, generate sample requests for averaging
j3 15:fb674d741844 146 uint8_t num_samples = ((portConfigDesignVals[port] & port_cfg_00_funcprm_nsamples) >> 5);
j3 15:fb674d741844 147 num_samples = (1 << num_samples);
j3 15:fb674d741844 148
j3 15:fb674d741844 149 while(num_samples--)
j3 15:fb674d741844 150 {
j3 15:fb674d741844 151 m_cnvt = 0;
j3 15:fb674d741844 152 wait_us(1);
j3 15:fb674d741844 153 m_cnvt = 1;
j3 15:fb674d741844 154 wait_us(100);
j3 15:fb674d741844 155 }
j3 12:8054ee101bad 156 }
j3 15:fb674d741844 157
j3 12:8054ee101bad 158 data = readRegister((adc_data_port_00 + port));
j3 12:8054ee101bad 159
j3 12:8054ee101bad 160 result = MAX113XX_Pixi::Success;
j3 12:8054ee101bad 161 }
j3 12:8054ee101bad 162 }
j3 12:8054ee101bad 163 else //12 port device
j3 12:8054ee101bad 164 {
j3 12:8054ee101bad 165 }
j3 12:8054ee101bad 166
j3 12:8054ee101bad 167 return result;
j3 12:8054ee101bad 168 }
j3 12:8054ee101bad 169
j3 12:8054ee101bad 170 //*********************************************************************
j3 12:8054ee101bad 171 MAX113XX_Pixi::CmdResult_e MAX113XX_Pixi::differentialADCRead(Ports_e posPort,
j3 13:546dd29b1c7a 172 int16_t &data)
j3 12:8054ee101bad 173 {
j3 12:8054ee101bad 174 MAX113XX_Pixi::CmdResult_e result = MAX113XX_Pixi::OpFailure;
j3 12:8054ee101bad 175
j3 15:fb674d741844 176 //check for type of device
j3 12:8054ee101bad 177 if(m_device == MAX11300 || m_device == MAX11301) //20 port device
j3 12:8054ee101bad 178 {
j3 15:fb674d741844 179 //is this port configured for this operation?
j3 13:546dd29b1c7a 180 if(((portConfigDesignVals[posPort] & 0xF000) >> 12) == MAX113XX_Pixi::MODE_8)
j3 13:546dd29b1c7a 181 {
j3 15:fb674d741844 182 //Is device configured for continuous sweep?
j3 15:fb674d741844 183 if((device_control_DESIGNVALUE & device_control_ADCCTL) != MAX113XX_Pixi::ContinuousSweep)
j3 13:546dd29b1c7a 184 {
j3 15:fb674d741844 185 //No, generate sample requests for averaging
j3 16:01d793a25548 186 uint8_t num_samples = ((portConfigDesignVals[posPort] & port_cfg_00_funcprm_nsamples) >> 5);
j3 15:fb674d741844 187 num_samples = (1 << num_samples);
j3 15:fb674d741844 188
j3 15:fb674d741844 189 while(num_samples--)
j3 15:fb674d741844 190 {
j3 15:fb674d741844 191 m_cnvt = 0;
j3 15:fb674d741844 192 wait_us(1);
j3 15:fb674d741844 193 m_cnvt = 1;
j3 15:fb674d741844 194 wait_us(100);
j3 15:fb674d741844 195 }
j3 13:546dd29b1c7a 196 }
j3 15:fb674d741844 197
j3 13:546dd29b1c7a 198 data = readRegister((adc_data_port_00 + posPort));
j3 13:546dd29b1c7a 199 if(data & 0x0800)
j3 13:546dd29b1c7a 200 {
j3 13:546dd29b1c7a 201 data |= 0xF000;
j3 13:546dd29b1c7a 202 }
j3 13:546dd29b1c7a 203
j3 13:546dd29b1c7a 204 result = MAX113XX_Pixi::Success;
j3 13:546dd29b1c7a 205 }
j3 12:8054ee101bad 206 }
j3 12:8054ee101bad 207 else //12 port device
j3 12:8054ee101bad 208 {
j3 12:8054ee101bad 209 }
j3 12:8054ee101bad 210
j3 12:8054ee101bad 211 return result;
j3 12:8054ee101bad 212 }
j3 12:8054ee101bad 213
j3 12:8054ee101bad 214 //*********************************************************************
j3 12:8054ee101bad 215 MAX113XX_Pixi::CmdResult_e MAX113XX_Pixi::dacWrite(Ports_e port,
j3 12:8054ee101bad 216 const uint16_t data)
j3 12:8054ee101bad 217 {
j3 12:8054ee101bad 218 MAX113XX_Pixi::CmdResult_e result = MAX113XX_Pixi::OpFailure;
j3 12:8054ee101bad 219
j3 12:8054ee101bad 220 if(m_device == MAX11300 || m_device == MAX11301) //20 port device
j3 12:8054ee101bad 221 {
j3 12:8054ee101bad 222 if(((portConfigDesignVals[port] & 0xF000) >> 12) == MAX113XX_Pixi::MODE_5)
j3 12:8054ee101bad 223 {
j3 12:8054ee101bad 224 writeRegister((dac_data_port_00 + port) , data);
j3 12:8054ee101bad 225 result = MAX113XX_Pixi::Success;
j3 12:8054ee101bad 226 }
j3 12:8054ee101bad 227 }
j3 12:8054ee101bad 228 else //12 port device
j3 12:8054ee101bad 229 {
j3 12:8054ee101bad 230 }
j3 12:8054ee101bad 231
j3 12:8054ee101bad 232 return result;
j3 12:8054ee101bad 233 }
j3 12:8054ee101bad 234
j3 12:8054ee101bad 235 //*********************************************************************
j3 13:546dd29b1c7a 236 MAX113XX_Pixi::CmdResult_e MAX113XX_Pixi::dacWriteADCMonitor(Ports_e port,
j3 13:546dd29b1c7a 237 const uint16_t data,
j3 13:546dd29b1c7a 238 uint16_t &adcData)
j3 13:546dd29b1c7a 239 {
j3 13:546dd29b1c7a 240 MAX113XX_Pixi::CmdResult_e result = MAX113XX_Pixi::OpFailure;
j3 13:546dd29b1c7a 241
j3 13:546dd29b1c7a 242 if(m_device == MAX11300 || m_device == MAX11301) //20 port device
j3 13:546dd29b1c7a 243 {
j3 13:546dd29b1c7a 244 if(((portConfigDesignVals[port] & 0xF000) >> 12) == MAX113XX_Pixi::MODE_6)
j3 13:546dd29b1c7a 245 {
j3 13:546dd29b1c7a 246 writeRegister((dac_data_port_00 + port) , data);
j3 13:546dd29b1c7a 247
j3 13:546dd29b1c7a 248 m_cnvt = 0;
j3 13:546dd29b1c7a 249 wait_us(1);
j3 13:546dd29b1c7a 250 m_cnvt = 1;
j3 13:546dd29b1c7a 251 wait_us(100);
j3 13:546dd29b1c7a 252
j3 13:546dd29b1c7a 253 adcData = readRegister((adc_data_port_00 + port));
j3 13:546dd29b1c7a 254
j3 13:546dd29b1c7a 255 result = MAX113XX_Pixi::Success;
j3 13:546dd29b1c7a 256 }
j3 13:546dd29b1c7a 257 }
j3 13:546dd29b1c7a 258 else //12 port device
j3 13:546dd29b1c7a 259 {
j3 13:546dd29b1c7a 260 }
j3 13:546dd29b1c7a 261
j3 13:546dd29b1c7a 262 return result;
j3 13:546dd29b1c7a 263 }
j3 13:546dd29b1c7a 264
j3 13:546dd29b1c7a 265 //*********************************************************************
j3 12:8054ee101bad 266 MAX113XX_Pixi::CmdResult_e MAX113XX_Pixi::gpioRead(Ports_e port, uint8_t &state)
j3 12:8054ee101bad 267 {
j3 12:8054ee101bad 268 MAX113XX_Pixi::CmdResult_e result = MAX113XX_Pixi::OpFailure;
j3 12:8054ee101bad 269
j3 12:8054ee101bad 270 if(m_device == MAX11300 || m_device == MAX11301) //20 port device
j3 12:8054ee101bad 271 {
j3 12:8054ee101bad 272 if(((portConfigDesignVals[port] & 0xF000) >> 12) == MAX113XX_Pixi::MODE_1)
j3 12:8054ee101bad 273 {
j3 12:8054ee101bad 274 if(port < MAX113XX_Pixi::PORT16)
j3 12:8054ee101bad 275 {
j3 12:8054ee101bad 276 state = (readRegister(gpi_data_15_to_0) >> port);
j3 12:8054ee101bad 277 }
j3 12:8054ee101bad 278 else
j3 12:8054ee101bad 279 {
j3 12:8054ee101bad 280 state = (readRegister(gpi_data_19_to_16) >> (port - MAX113XX_Pixi::PORT16));
j3 12:8054ee101bad 281 }
j3 12:8054ee101bad 282
j3 12:8054ee101bad 283 result = MAX113XX_Pixi::Success;
j3 12:8054ee101bad 284 }
j3 12:8054ee101bad 285 }
j3 12:8054ee101bad 286 else //12 port device
j3 12:8054ee101bad 287 {
j3 12:8054ee101bad 288 }
j3 12:8054ee101bad 289
j3 12:8054ee101bad 290 return result;
j3 12:8054ee101bad 291 }
j3 7:8669a53acd0d 292
j3 7:8669a53acd0d 293 //*********************************************************************
j3 9:094df3de3616 294 MAX113XX_Pixi::CmdResult_e MAX113XX_Pixi::gpioWrite(Ports_e port,
j3 9:094df3de3616 295 const uint8_t state)
j3 7:8669a53acd0d 296 {
j3 10:6efe114ef882 297 uint16_t temp;
j3 10:6efe114ef882 298 uint16_t port_mask;
j3 10:6efe114ef882 299
j3 7:8669a53acd0d 300 MAX113XX_Pixi::CmdResult_e result = MAX113XX_Pixi::OpFailure;
j3 9:094df3de3616 301
j3 10:6efe114ef882 302 if(m_device == MAX11300 || m_device == MAX11301) //20 port device
j3 9:094df3de3616 303 {
j3 10:6efe114ef882 304 if(((portConfigDesignVals[port] & 0xF000) >> 12) == MAX113XX_Pixi::MODE_3)
j3 10:6efe114ef882 305 {
j3 10:6efe114ef882 306 if(port < MAX113XX_Pixi::PORT16)
j3 10:6efe114ef882 307 {
j3 10:6efe114ef882 308 port_mask = (1 << port);
j3 10:6efe114ef882 309 temp = readRegister(gpo_data_15_to_0);
j3 10:6efe114ef882 310 if(state & 0x01)
j3 10:6efe114ef882 311 {
j3 10:6efe114ef882 312 temp |= port_mask;
j3 10:6efe114ef882 313 }
j3 10:6efe114ef882 314 else
j3 10:6efe114ef882 315 {
j3 10:6efe114ef882 316 temp &= ~port_mask;
j3 10:6efe114ef882 317 }
j3 10:6efe114ef882 318 writeRegister(gpo_data_15_to_0, temp);
j3 10:6efe114ef882 319 }
j3 10:6efe114ef882 320 else
j3 10:6efe114ef882 321 {
j3 10:6efe114ef882 322 port_mask = (1 << (port - MAX113XX_Pixi::PORT16));
j3 10:6efe114ef882 323 temp = readRegister(gpo_data_19_to_16);
j3 10:6efe114ef882 324 if(state & 0x01)
j3 10:6efe114ef882 325 {
j3 10:6efe114ef882 326 temp |= port_mask;
j3 10:6efe114ef882 327 }
j3 10:6efe114ef882 328 else
j3 10:6efe114ef882 329 {
j3 10:6efe114ef882 330 temp &= ~port_mask;
j3 10:6efe114ef882 331 }
j3 10:6efe114ef882 332 writeRegister(gpo_data_19_to_16, temp);
j3 10:6efe114ef882 333 }
j3 10:6efe114ef882 334
j3 10:6efe114ef882 335 result = MAX113XX_Pixi::Success;
j3 10:6efe114ef882 336 }
j3 9:094df3de3616 337 }
j3 9:094df3de3616 338 else //12 port device
j3 9:094df3de3616 339 {
j3 9:094df3de3616 340 }
j3 7:8669a53acd0d 341
j3 7:8669a53acd0d 342 return result;
j3 7:8669a53acd0d 343 }
j3 7:8669a53acd0d 344
j3 7:8669a53acd0d 345 //*********************************************************************
j3 13:546dd29b1c7a 346 MAX113XX_Pixi::CmdResult_e MAX113XX_Pixi::setAnalogSwitchState(Ports_e lowPort,
j3 12:8054ee101bad 347 bool state)
j3 7:8669a53acd0d 348 {
j3 7:8669a53acd0d 349 MAX113XX_Pixi::CmdResult_e result = MAX113XX_Pixi::OpFailure;
j3 7:8669a53acd0d 350
j3 10:6efe114ef882 351 if(m_device == MAX11300 || m_device == MAX11301) //20 port device
j3 9:094df3de3616 352 {
j3 13:546dd29b1c7a 353 if(((portConfigDesignVals[lowPort] & 0xF000) >> 12) == MAX113XX_Pixi::MODE_12)
j3 13:546dd29b1c7a 354 {
j3 13:546dd29b1c7a 355 uint16_t data = readRegister(port_cfg_00 + lowPort);
j3 13:546dd29b1c7a 356 if(state)
j3 13:546dd29b1c7a 357 {
j3 13:546dd29b1c7a 358 data = ((data & 0x0FFF) | (MAX113XX_Pixi::MODE_12 << 12));
j3 13:546dd29b1c7a 359 }
j3 13:546dd29b1c7a 360 else
j3 13:546dd29b1c7a 361 {
j3 13:546dd29b1c7a 362 data = ((data & 0x0FFF) | (MAX113XX_Pixi::MODE_0 << 12));
j3 13:546dd29b1c7a 363 }
j3 13:546dd29b1c7a 364 writeRegister((port_cfg_00 + lowPort), data);
j3 13:546dd29b1c7a 365
j3 13:546dd29b1c7a 366 result = MAX113XX_Pixi::Success;
j3 13:546dd29b1c7a 367 }
j3 13:546dd29b1c7a 368 }
j3 13:546dd29b1c7a 369 else //12 port device
j3 13:546dd29b1c7a 370 {
j3 13:546dd29b1c7a 371 }
j3 13:546dd29b1c7a 372
j3 13:546dd29b1c7a 373 return result;
j3 13:546dd29b1c7a 374 }
j3 13:546dd29b1c7a 375
j3 13:546dd29b1c7a 376 //*********************************************************************
j3 13:546dd29b1c7a 377 MAX113XX_Pixi::CmdResult_e MAX113XX_Pixi::readTempSensor(TempSensor_e sensor,
j3 13:546dd29b1c7a 378 float &data)
j3 13:546dd29b1c7a 379 {
j3 13:546dd29b1c7a 380 MAX113XX_Pixi::CmdResult_e result = MAX113XX_Pixi::OpFailure;
j3 18:8ee1928ffe6c 381 int16_t temp = 0;
j3 13:546dd29b1c7a 382
j3 13:546dd29b1c7a 383 if(m_device == MAX11300 || m_device == MAX11301) //20 port device
j3 13:546dd29b1c7a 384 {
j3 13:546dd29b1c7a 385 switch(sensor)
j3 13:546dd29b1c7a 386 {
j3 13:546dd29b1c7a 387 case Internal:
j3 13:546dd29b1c7a 388 if(device_control_DESIGNVALUE & device_control_TMPCTLINT)
j3 13:546dd29b1c7a 389 {
j3 13:546dd29b1c7a 390 temp = readRegister(tmp_int_data);
j3 13:546dd29b1c7a 391 result = MAX113XX_Pixi::Success;
j3 13:546dd29b1c7a 392 }
j3 13:546dd29b1c7a 393 break;
j3 13:546dd29b1c7a 394
j3 13:546dd29b1c7a 395 case External1:
j3 13:546dd29b1c7a 396 if(device_control_DESIGNVALUE & device_control_TMPCTLEXT0)
j3 13:546dd29b1c7a 397 {
j3 13:546dd29b1c7a 398 temp = readRegister(tmp_ext1_data);
j3 13:546dd29b1c7a 399 result = MAX113XX_Pixi::Success;
j3 13:546dd29b1c7a 400 }
j3 13:546dd29b1c7a 401 break;
j3 13:546dd29b1c7a 402
j3 13:546dd29b1c7a 403 case External2:
j3 13:546dd29b1c7a 404 if(device_control_DESIGNVALUE & device_control_TMPCTLEXT1)
j3 13:546dd29b1c7a 405 {
j3 13:546dd29b1c7a 406 temp = readRegister(tmp_ext2_data);
j3 13:546dd29b1c7a 407 result = MAX113XX_Pixi::Success;
j3 13:546dd29b1c7a 408 }
j3 13:546dd29b1c7a 409 break;
j3 13:546dd29b1c7a 410 };
j3 13:546dd29b1c7a 411
j3 13:546dd29b1c7a 412 if(result == MAX113XX_Pixi::Success)
j3 13:546dd29b1c7a 413 {
j3 13:546dd29b1c7a 414 if(temp & 0x0800)
j3 13:546dd29b1c7a 415 {
j3 13:546dd29b1c7a 416 temp |= 0xF000;
j3 13:546dd29b1c7a 417 }
j3 13:546dd29b1c7a 418 data = (temp/8.0F);
j3 13:546dd29b1c7a 419 }
j3 9:094df3de3616 420 }
j3 9:094df3de3616 421 else //12 port device
j3 9:094df3de3616 422 {
j3 9:094df3de3616 423 }
j3 9:094df3de3616 424
j3 7:8669a53acd0d 425 return result;
j3 7:8669a53acd0d 426 }
j3 7:8669a53acd0d 427
j3 7:8669a53acd0d 428 //*********************************************************************
elelthvd 19:2d747d4b424b 429 void MAX113XX_Pixi::dumpMemory(BufferedSerial &ser)
j3 7:8669a53acd0d 430 {
j3 10:6efe114ef882 431 uint16_t mem[0x74];
elelthvd 19:2d747d4b424b 432 char line[64]="";
elelthvd 19:2d747d4b424b 433
j3 12:8054ee101bad 434 blockRead(dev_id, mem, 0x74);
j3 7:8669a53acd0d 435 for(uint8_t idx = 0; idx < 0x74; idx++)
j3 7:8669a53acd0d 436 {
elelthvd 19:2d747d4b424b 437 sprintf(line, "Register 0x%02x = 0x%04x\r\n", idx, mem[idx]);
elelthvd 19:2d747d4b424b 438 ser.write(line, sizeof(line));
j3 7:8669a53acd0d 439 }
elelthvd 19:2d747d4b424b 440 sprintf(line, "\r\n");
elelthvd 19:2d747d4b424b 441 ser.write(line, sizeof(line));
j3 7:8669a53acd0d 442 }
j3 7:8669a53acd0d 443
j3 7:8669a53acd0d 444
j3 9:094df3de3616 445 /// SPI first byte when writing MAX11300/11
j3 9:094df3de3616 446 //(7-bit address in bits 0x7E; LSB=0 for write)
j3 9:094df3de3616 447 #define MAX113XXAddr_SPI_Write(RegAddr) ( (RegAddr << 1) )
j3 9:094df3de3616 448
j3 9:094df3de3616 449 /// SPI first byte when reading MAX11300/11
j3 9:094df3de3616 450 //(7-bit address in bits 0x7E; LSB=1 for read)
j3 9:094df3de3616 451 #define MAX113XXAddr_SPI_Read(RegAddr) ( (RegAddr << 1) | 1 )
j3 9:094df3de3616 452
j3 7:8669a53acd0d 453 //*************************** SPI Implementation ******************************
j3 9:094df3de3616 454 MAX113XX_SPI::MAX113XX_SPI(SPI & spiBus, PinName cs,
j3 9:094df3de3616 455 MAX113XX_Pixi::Device_e device, PinName cnvt):
j3 9:094df3de3616 456 MAX113XX_Pixi(device, cnvt), m_spiBus(spiBus), m_cs(cs, 1)
j3 7:8669a53acd0d 457 {
j3 11:31e7ca030b8f 458 if((m_device == MAX11300) || (m_device == MAX11301)) //20 port device
j3 11:31e7ca030b8f 459 {
j3 12:8054ee101bad 460 blockWrite(gpo_data_15_to_0, configDesignVals, 18);
j3 11:31e7ca030b8f 461 blockWrite(port_cfg_00, portConfigDesignVals, 20);
j3 12:8054ee101bad 462 blockWrite(dac_data_port_00, dacDesignVals, 20);
elelthvd 20:d4ef18312354 463 wait_us(0.1*1000000);
j3 11:31e7ca030b8f 464 }
j3 11:31e7ca030b8f 465 else //12 port device
j3 11:31e7ca030b8f 466 {
j3 11:31e7ca030b8f 467 }
j3 7:8669a53acd0d 468 }
j3 7:8669a53acd0d 469
j3 7:8669a53acd0d 470 //*********************************************************************
j3 7:8669a53acd0d 471 MAX113XX_SPI::~MAX113XX_SPI()
j3 7:8669a53acd0d 472 {
j3 7:8669a53acd0d 473 //empty block
j3 7:8669a53acd0d 474 }
j3 7:8669a53acd0d 475
j3 7:8669a53acd0d 476 //*********************************************************************
j3 9:094df3de3616 477 void MAX113XX_SPI::writeRegister(uint8_t reg, const uint16_t data)
j3 7:8669a53acd0d 478 {
j3 7:8669a53acd0d 479 m_cs = 0;
j3 9:094df3de3616 480 m_spiBus.write(MAX113XXAddr_SPI_Write(reg));
j3 7:8669a53acd0d 481 m_spiBus.write(((0xFF00 & data) >> 8));
j3 7:8669a53acd0d 482 m_spiBus.write((0x00FF & data));
j3 7:8669a53acd0d 483 m_cs = 1;
j3 7:8669a53acd0d 484 }
j3 7:8669a53acd0d 485
j3 7:8669a53acd0d 486 //*********************************************************************
j3 9:094df3de3616 487 uint16_t MAX113XX_SPI::readRegister(uint8_t reg)
j3 7:8669a53acd0d 488 {
j3 7:8669a53acd0d 489 uint16_t rtn_val = 0;
j3 7:8669a53acd0d 490
j3 7:8669a53acd0d 491 m_cs = 0;
j3 9:094df3de3616 492 m_spiBus.write(MAX113XXAddr_SPI_Read(reg));
j3 7:8669a53acd0d 493 rtn_val |= (m_spiBus.write(0xFF) << 8);
j3 7:8669a53acd0d 494 rtn_val |= m_spiBus.write(0xFF);
j3 7:8669a53acd0d 495 m_cs = 1;
j3 7:8669a53acd0d 496
j3 7:8669a53acd0d 497 return rtn_val;
j3 7:8669a53acd0d 498 }
j3 7:8669a53acd0d 499
j3 7:8669a53acd0d 500 //*********************************************************************
j3 9:094df3de3616 501 void MAX113XX_SPI::blockWrite(uint8_t reg, const uint16_t *data,
j3 7:8669a53acd0d 502 const uint8_t num_reg)
j3 7:8669a53acd0d 503 {
j3 11:31e7ca030b8f 504 m_cs = 0;
j3 11:31e7ca030b8f 505 m_spiBus.write(MAX113XXAddr_SPI_Write(reg));
j3 7:8669a53acd0d 506 for(uint8_t idx = 0; idx < num_reg; idx++)
j3 7:8669a53acd0d 507 {
j3 11:31e7ca030b8f 508 m_spiBus.write(((0xFF00 & data[idx]) >> 8));
j3 11:31e7ca030b8f 509 m_spiBus.write((0x00FF & data[idx]));
j3 7:8669a53acd0d 510 }
j3 11:31e7ca030b8f 511 m_cs = 1;
j3 7:8669a53acd0d 512 }
j3 7:8669a53acd0d 513
j3 7:8669a53acd0d 514 //*********************************************************************
j3 9:094df3de3616 515 void MAX113XX_SPI::blockRead(uint8_t reg, uint16_t *data, const uint8_t num_reg)
j3 7:8669a53acd0d 516 {
j3 11:31e7ca030b8f 517 uint16_t temp;
j3 11:31e7ca030b8f 518 m_cs = 0;
j3 11:31e7ca030b8f 519 m_spiBus.write(MAX113XXAddr_SPI_Read(reg));
j3 11:31e7ca030b8f 520 for(uint8_t idx = 0; idx < num_reg; idx ++)
j3 7:8669a53acd0d 521 {
j3 11:31e7ca030b8f 522 temp = 0;
j3 11:31e7ca030b8f 523 temp |= (m_spiBus.write(0xFF) << 8);
j3 11:31e7ca030b8f 524 temp |= m_spiBus.write(0xFF);
j3 11:31e7ca030b8f 525 data[idx] = temp;
j3 7:8669a53acd0d 526 }
j3 11:31e7ca030b8f 527 m_cs = 1;
j3 7:8669a53acd0d 528 }
j3 7:8669a53acd0d 529
j3 7:8669a53acd0d 530
j3 7:8669a53acd0d 531 //*************************** I2C Implementation ******************************
j3 17:658202c79f33 532 MAX113XX_I2C::MAX113XX_I2C(I2C &i2cBus, MAX113XX_Pixi::Device_e device,
j3 17:658202c79f33 533 uint8_t i2cAdrs, PinName cnvt):
j3 17:658202c79f33 534 MAX113XX_Pixi(device, cnvt), m_i2cBus(i2cBus), m_w_adrs(i2cAdrs << 1),
j3 17:658202c79f33 535 m_r_adrs((i2cAdrs << 1) | 1)
j3 7:8669a53acd0d 536 {
j3 11:31e7ca030b8f 537 if((m_device == MAX11300) || (m_device == MAX11301)) //20 port device
j3 11:31e7ca030b8f 538 {
j3 12:8054ee101bad 539 blockWrite(gpo_data_15_to_0, configDesignVals, 18);
j3 11:31e7ca030b8f 540 blockWrite(port_cfg_00, portConfigDesignVals, 20);
j3 12:8054ee101bad 541 blockWrite(dac_data_port_00, dacDesignVals, 20);
elelthvd 20:d4ef18312354 542 wait_us(0.1*1000000);
j3 11:31e7ca030b8f 543 }
j3 11:31e7ca030b8f 544 else //12 port device
j3 11:31e7ca030b8f 545 {
j3 11:31e7ca030b8f 546 }
j3 7:8669a53acd0d 547 }
j3 7:8669a53acd0d 548
j3 7:8669a53acd0d 549 //*********************************************************************
j3 7:8669a53acd0d 550 MAX113XX_I2C::~MAX113XX_I2C()
j3 7:8669a53acd0d 551 {
j3 7:8669a53acd0d 552 //empty block
j3 7:8669a53acd0d 553 }
j3 7:8669a53acd0d 554
j3 7:8669a53acd0d 555 //*********************************************************************
j3 9:094df3de3616 556 void MAX113XX_I2C::writeRegister(uint8_t reg, const uint16_t data)
j3 7:8669a53acd0d 557 {
j3 17:658202c79f33 558 char localData[3];
j3 17:658202c79f33 559 uint8_t idx = 0;
j3 7:8669a53acd0d 560
j3 17:658202c79f33 561 localData[idx++] = reg;
j3 17:658202c79f33 562 localData[idx++] = ((data & 0xFF00) >> 8);
j3 17:658202c79f33 563 localData[idx++] = (data & 0x00FF);
j3 17:658202c79f33 564
j3 17:658202c79f33 565 m_i2cBus.write(m_w_adrs, localData, idx);
j3 7:8669a53acd0d 566 }
j3 7:8669a53acd0d 567
j3 7:8669a53acd0d 568 //*********************************************************************
j3 9:094df3de3616 569 uint16_t MAX113XX_I2C::readRegister(uint8_t reg)
j3 7:8669a53acd0d 570 {
j3 7:8669a53acd0d 571 uint16_t rtn_val = 0;
j3 7:8669a53acd0d 572
j3 17:658202c79f33 573 char localData[2];
j3 17:658202c79f33 574 localData[0] = reg;
j3 17:658202c79f33 575
j3 17:658202c79f33 576 if(m_i2cBus.write(m_w_adrs, localData, 1, true) == 0)
j3 17:658202c79f33 577 {
j3 17:658202c79f33 578 if(m_i2cBus.read(m_r_adrs, localData, 2) == 0)
j3 17:658202c79f33 579 {
j3 17:658202c79f33 580 rtn_val |= (localData[0] << 8);
j3 17:658202c79f33 581 rtn_val |= localData[1];
j3 17:658202c79f33 582 }
j3 17:658202c79f33 583 }
j3 17:658202c79f33 584
j3 7:8669a53acd0d 585 return rtn_val;
j3 7:8669a53acd0d 586 }
j3 7:8669a53acd0d 587
j3 7:8669a53acd0d 588 //*********************************************************************
j3 9:094df3de3616 589 void MAX113XX_I2C::blockWrite(uint8_t reg, const uint16_t *data,
j3 7:8669a53acd0d 590 const uint8_t num_reg)
j3 7:8669a53acd0d 591 {
j3 17:658202c79f33 592 uint16_t numBytes = ((num_reg * 2) + 1);
j3 18:8ee1928ffe6c 593 //No variable length array
j3 18:8ee1928ffe6c 594 char localData[MAX113XX_Pixi::MAX_REGISTER_ADRS];
j3 17:658202c79f33 595 uint8_t idx(0), dataIdx(0);
j3 17:658202c79f33 596
j3 17:658202c79f33 597 localData[idx++] = reg;
j3 17:658202c79f33 598 do
j3 17:658202c79f33 599 {
j3 17:658202c79f33 600 localData[idx++] = ((data[dataIdx] & 0xFF00) >> 8);
j3 17:658202c79f33 601 localData[idx++] = (data[dataIdx++] & 0x00FF);
j3 17:658202c79f33 602 }
j3 17:658202c79f33 603 while(idx < numBytes);
j3 17:658202c79f33 604
j3 18:8ee1928ffe6c 605 m_i2cBus.write(m_w_adrs, localData, numBytes);
j3 7:8669a53acd0d 606 }
j3 7:8669a53acd0d 607
j3 7:8669a53acd0d 608 //*********************************************************************
j3 9:094df3de3616 609 void MAX113XX_I2C::blockRead(uint8_t reg, uint16_t *data, const uint8_t num_reg)
j3 7:8669a53acd0d 610 {
j3 17:658202c79f33 611 uint16_t numBytes = (num_reg * 2);
j3 18:8ee1928ffe6c 612 //No variable length array
j3 18:8ee1928ffe6c 613 char localData[MAX113XX_Pixi::MAX_REGISTER_ADRS];
j3 17:658202c79f33 614 localData[0] = reg;
j3 17:658202c79f33 615
j3 17:658202c79f33 616 if(m_i2cBus.write(m_w_adrs, localData, 1, true) == 0)
j3 17:658202c79f33 617 {
j3 17:658202c79f33 618 if(m_i2cBus.read(m_r_adrs, localData, numBytes) == 0)
j3 17:658202c79f33 619 {
j3 17:658202c79f33 620 uint8_t dataIdx = 0;
j3 17:658202c79f33 621 for(uint8_t idx = 0; idx < numBytes; idx += 2)
j3 17:658202c79f33 622 {
j3 17:658202c79f33 623 data[dataIdx] = 0;
j3 17:658202c79f33 624 data[dataIdx] |= (localData[idx] << 8);
j3 17:658202c79f33 625 data[dataIdx++] |= localData[idx + 1];
j3 17:658202c79f33 626 }
j3 17:658202c79f33 627 }
j3 17:658202c79f33 628 }
j3 7:8669a53acd0d 629 }