library for MAX14661 16:2 mux

Dependents:   ard2pmod MAX14661_Demo

MAX14661 Component Page

Committer:
j3
Date:
Mon Nov 17 21:22:48 2014 +0000
Revision:
0:c770ad7363c8
Child:
1:c1fdfe4c2354
first release, JJJ

Who changed what in which revision?

UserRevisionLine numberNew contents of line
j3 0:c770ad7363c8 1 /******************************************************************//**
j3 0:c770ad7363c8 2 * @file max14661.cpp
j3 0:c770ad7363c8 3 *
j3 0:c770ad7363c8 4 * @author Justin Jordan
j3 0:c770ad7363c8 5 *
j3 0:c770ad7363c8 6 * @version 0.0
j3 0:c770ad7363c8 7 *
j3 0:c770ad7363c8 8 * Started: 11NOV14
j3 0:c770ad7363c8 9 *
j3 0:c770ad7363c8 10 * Updated:
j3 0:c770ad7363c8 11 *
j3 0:c770ad7363c8 12 * @brief Source file for MAX14661 class
j3 0:c770ad7363c8 13 *
j3 0:c770ad7363c8 14 ***********************************************************************
j3 0:c770ad7363c8 15 *
j3 0:c770ad7363c8 16 * @copyright
j3 0:c770ad7363c8 17 * Copyright (C) 2013 Maxim Integrated Products, Inc., All Rights Reserved.
j3 0:c770ad7363c8 18 *
j3 0:c770ad7363c8 19 * Permission is hereby granted, free of charge, to any person obtaining a
j3 0:c770ad7363c8 20 * copy of this software and associated documentation files (the "Software"),
j3 0:c770ad7363c8 21 * to deal in the Software without restriction, including without limitation
j3 0:c770ad7363c8 22 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
j3 0:c770ad7363c8 23 * and/or sell copies of the Software, and to permit persons to whom the
j3 0:c770ad7363c8 24 * Software is furnished to do so, subject to the following conditions:
j3 0:c770ad7363c8 25 *
j3 0:c770ad7363c8 26 * The above copyright notice and this permission notice shall be included
j3 0:c770ad7363c8 27 * in all copies or substantial portions of the Software.
j3 0:c770ad7363c8 28 *
j3 0:c770ad7363c8 29 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
j3 0:c770ad7363c8 30 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
j3 0:c770ad7363c8 31 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
j3 0:c770ad7363c8 32 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
j3 0:c770ad7363c8 33 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
j3 0:c770ad7363c8 34 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
j3 0:c770ad7363c8 35 * OTHER DEALINGS IN THE SOFTWARE.
j3 0:c770ad7363c8 36 *
j3 0:c770ad7363c8 37 * Except as contained in this notice, the name of Maxim Integrated
j3 0:c770ad7363c8 38 * Products, Inc. shall not be used except as stated in the Maxim Integrated
j3 0:c770ad7363c8 39 * Products, Inc. Branding Policy.
j3 0:c770ad7363c8 40 *
j3 0:c770ad7363c8 41 * The mere transfer of this software does not imply any licenses
j3 0:c770ad7363c8 42 * of trade secrets, proprietary technology, copyrights, patents,
j3 0:c770ad7363c8 43 * trademarks, maskwork rights, or any other form of intellectual
j3 0:c770ad7363c8 44 * property whatsoever. Maxim Integrated Products, Inc. retains all
j3 0:c770ad7363c8 45 * ownership rights.
j3 0:c770ad7363c8 46 **********************************************************************/
j3 0:c770ad7363c8 47
j3 0:c770ad7363c8 48
j3 0:c770ad7363c8 49 #include "max14661.h"
j3 0:c770ad7363c8 50
j3 0:c770ad7363c8 51
j3 0:c770ad7363c8 52 /******************************************************************//**
j3 0:c770ad7363c8 53 * Instantiates object and attaches MAX14661 to I2C buss
j3 0:c770ad7363c8 54 *
j3 0:c770ad7363c8 55 * On Entry:
j3 0:c770ad7363c8 56 * @param[in] p_i2c - pointer to I2C object for bus
j3 0:c770ad7363c8 57 * @param[in] i2c_adrs - 7-bit slave address of MAX14661
j3 0:c770ad7363c8 58 *
j3 0:c770ad7363c8 59 * On Exit:
j3 0:c770ad7363c8 60 * @return none
j3 0:c770ad7363c8 61 *
j3 0:c770ad7363c8 62 * Example:
j3 0:c770ad7363c8 63 * @code
j3 0:c770ad7363c8 64 *
j3 0:c770ad7363c8 65 * I2C i2c_bus(D14, D15); //instantiate I2C bus
j3 0:c770ad7363c8 66 * I2C * p_i2c_bus = &i2c_bus; //create pointer to bus
j3 0:c770ad7363c8 67 *
j3 0:c770ad7363c8 68 * //instantiate mux object
j3 0:c770ad7363c8 69 * Max14661 mux(p_i2c_bus, MAX14661_I2C_ADRS0);
j3 0:c770ad7363c8 70 *
j3 0:c770ad7363c8 71 * @endcode
j3 0:c770ad7363c8 72 **********************************************************************/
j3 0:c770ad7363c8 73 Max14661::Max14661(I2C* ptr_i2c_bus, max14661_i2c_adrs_t i2c_adrs)
j3 0:c770ad7363c8 74 {
j3 0:c770ad7363c8 75 p_i2c = ptr_i2c_bus;
j3 0:c770ad7363c8 76 w_adrs = (i2c_adrs << 1);
j3 0:c770ad7363c8 77 r_adrs = (w_adrs | 0x01);
j3 0:c770ad7363c8 78 }
j3 0:c770ad7363c8 79
j3 0:c770ad7363c8 80
j3 0:c770ad7363c8 81 /******************************************************************//**
j3 0:c770ad7363c8 82 * Writes given commands to CMD_A and CMD_B
j3 0:c770ad7363c8 83 *
j3 0:c770ad7363c8 84 * On Entry:
j3 0:c770ad7363c8 85 * @param[in] cmdA - command for CMD_A
j3 0:c770ad7363c8 86 * @param[in] cmdB - command for CMD_B
j3 0:c770ad7363c8 87 *
j3 0:c770ad7363c8 88 * On Exit:
j3 0:c770ad7363c8 89 * @return return value = 0 on success, non-0 on failure
j3 0:c770ad7363c8 90 *
j3 0:c770ad7363c8 91 * Example:
j3 0:c770ad7363c8 92 * @code
j3 0:c770ad7363c8 93 *
j3 0:c770ad7363c8 94 * I2C i2c_bus(D14, D15); //instantiate I2C bus
j3 0:c770ad7363c8 95 * I2C * p_i2c_bus = &i2c_bus; //create pointer to bus
j3 0:c770ad7363c8 96 *
j3 0:c770ad7363c8 97 * //instantiate mux object
j3 0:c770ad7363c8 98 * Max14661 mux(p_i2c_bus, MAX14661_I2C_ADRS0);
j3 0:c770ad7363c8 99 *
j3 0:c770ad7363c8 100 * uint16_t rtn_val;
j3 0:c770ad7363c8 101 *
j3 0:c770ad7363c8 102 * rtn_val = mux.wrt_cmd_registers(DISABLE_BANK, DISABLE_BANK);
j3 0:c770ad7363c8 103 *
j3 0:c770ad7363c8 104 * @endcode
j3 0:c770ad7363c8 105 **********************************************************************/
j3 0:c770ad7363c8 106 uint16_t Max14661::wrt_cmd_registers(max14661_cmds_t cmdA,
j3 0:c770ad7363c8 107 max14661_cmds_t cmdB)
j3 0:c770ad7363c8 108 {
j3 0:c770ad7363c8 109 uint8_t data[3];
j3 0:c770ad7363c8 110 uint8_t data_length = 0;
j3 0:c770ad7363c8 111 uint16_t rtn_val = 1;
j3 0:c770ad7363c8 112
j3 0:c770ad7363c8 113 //build packet
j3 0:c770ad7363c8 114 data[data_length++] = CMD_A;
j3 0:c770ad7363c8 115 data[data_length++] = cmdA;
j3 0:c770ad7363c8 116 data[data_length++] = cmdB;
j3 0:c770ad7363c8 117
j3 0:c770ad7363c8 118 rtn_val = p_i2c->write(w_adrs,(const char*) data, data_length);
j3 0:c770ad7363c8 119
j3 0:c770ad7363c8 120 return(rtn_val);
j3 0:c770ad7363c8 121 }
j3 0:c770ad7363c8 122
j3 0:c770ad7363c8 123
j3 0:c770ad7363c8 124 /******************************************************************//**
j3 0:c770ad7363c8 125 * Writes data pointed at by 'data' to shadow registers
j3 0:c770ad7363c8 126 *
j3 0:c770ad7363c8 127 * On Entry:
j3 0:c770ad7363c8 128 * @param[in] data - pointer to data
j3 0:c770ad7363c8 129 *
j3 0:c770ad7363c8 130 * On Exit:
j3 0:c770ad7363c8 131 * @return return value = 0 on success, non-0 on failure
j3 0:c770ad7363c8 132 *
j3 0:c770ad7363c8 133 * Example:
j3 0:c770ad7363c8 134 * @code
j3 0:c770ad7363c8 135 *
j3 0:c770ad7363c8 136 * I2C i2c_bus(D14, D15); //instantiate I2C bus
j3 0:c770ad7363c8 137 * I2C * p_i2c_bus = &i2c_bus; //create pointer to bus
j3 0:c770ad7363c8 138 *
j3 0:c770ad7363c8 139 * //instantiate mux object
j3 0:c770ad7363c8 140 * Max14661 mux(p_i2c_bus, MAX14661_I2C_ADRS0);
j3 0:c770ad7363c8 141 *
j3 0:c770ad7363c8 142 * uint8_t data[] = {1, 2, 3, 4};
j3 0:c770ad7363c8 143 * uint16_t rtn_val;
j3 0:c770ad7363c8 144 *
j3 0:c770ad7363c8 145 * //wite shadow registers
j3 0:c770ad7363c8 146 * rtn_val = mux.wrt_shadow_registers(data);
j3 0:c770ad7363c8 147 *
j3 0:c770ad7363c8 148 * @endcode
j3 0:c770ad7363c8 149 **********************************************************************/
j3 0:c770ad7363c8 150 uint16_t Max14661::wrt_shadow_registers(uint8_t* data)
j3 0:c770ad7363c8 151 {
j3 0:c770ad7363c8 152 uint8_t local_data[5];
j3 0:c770ad7363c8 153 uint8_t data_length = 0;
j3 0:c770ad7363c8 154 uint16_t rtn_val = 1;
j3 0:c770ad7363c8 155
j3 0:c770ad7363c8 156 local_data[data_length++] = SHDW0;
j3 0:c770ad7363c8 157
j3 0:c770ad7363c8 158 for(; data_length < 5; data_length++)
j3 0:c770ad7363c8 159 {
j3 0:c770ad7363c8 160 local_data[data_length] = data[data_length-1];
j3 0:c770ad7363c8 161 }
j3 0:c770ad7363c8 162
j3 0:c770ad7363c8 163 rtn_val = p_i2c->write(w_adrs,(const char*) local_data, data_length);
j3 0:c770ad7363c8 164
j3 0:c770ad7363c8 165 return(rtn_val);
j3 0:c770ad7363c8 166 }
j3 0:c770ad7363c8 167
j3 0:c770ad7363c8 168
j3 0:c770ad7363c8 169 /******************************************************************//**
j3 0:c770ad7363c8 170 * Writes data pointed at by 'data' to direct access registers
j3 0:c770ad7363c8 171 *
j3 0:c770ad7363c8 172 * On Entry:
j3 0:c770ad7363c8 173 * @param[in] data - pointer to data to be written
j3 0:c770ad7363c8 174 *
j3 0:c770ad7363c8 175 * On Exit:
j3 0:c770ad7363c8 176 * @return return value = 0 on success, non-0 on failure
j3 0:c770ad7363c8 177 *
j3 0:c770ad7363c8 178 * Example:
j3 0:c770ad7363c8 179 * @code
j3 0:c770ad7363c8 180 *
j3 0:c770ad7363c8 181 * I2C i2c_bus(D14, D15); //instantiate I2C bus
j3 0:c770ad7363c8 182 * I2C * p_i2c_bus = &i2c_bus; //create pointer to bus
j3 0:c770ad7363c8 183 *
j3 0:c770ad7363c8 184 * //instantiate mux object
j3 0:c770ad7363c8 185 * Max14661 mux(p_i2c_bus, MAX14661_I2C_ADRS0);
j3 0:c770ad7363c8 186 *
j3 0:c770ad7363c8 187 * I2C i2c_bus(D14, D15); //instantiate I2C bus
j3 0:c770ad7363c8 188 * I2C * p_i2c_bus = &i2c_bus; //create pointer to bus
j3 0:c770ad7363c8 189 *
j3 0:c770ad7363c8 190 * //instantiate mux object
j3 0:c770ad7363c8 191 * Max14661 mux(p_i2c_bus, MAX14661_I2C_ADRS0);
j3 0:c770ad7363c8 192 *
j3 0:c770ad7363c8 193 * uint8_t data[] = {1, 2, 3, 4};
j3 0:c770ad7363c8 194 * uint16_t rtn_val;
j3 0:c770ad7363c8 195 *
j3 0:c770ad7363c8 196 * //wite shadow registers
j3 0:c770ad7363c8 197 * rtn_val = mux.wrt_shadow_registers(data);
j3 0:c770ad7363c8 198 *
j3 0:c770ad7363c8 199 * @endcode
j3 0:c770ad7363c8 200 **********************************************************************/
j3 0:c770ad7363c8 201 uint16_t Max14661::wrt_dir_registers(uint8_t* data)
j3 0:c770ad7363c8 202 {
j3 0:c770ad7363c8 203 uint8_t local_data[5];
j3 0:c770ad7363c8 204 uint8_t data_length = 0;
j3 0:c770ad7363c8 205 uint16_t rtn_val = 1;
j3 0:c770ad7363c8 206
j3 0:c770ad7363c8 207 local_data[data_length++] = DIR0;
j3 0:c770ad7363c8 208
j3 0:c770ad7363c8 209 for(; data_length < 5; data_length++)
j3 0:c770ad7363c8 210 {
j3 0:c770ad7363c8 211 local_data[data_length] = data[data_length-1];
j3 0:c770ad7363c8 212 }
j3 0:c770ad7363c8 213
j3 0:c770ad7363c8 214 rtn_val = p_i2c->write(w_adrs,(const char*) local_data, data_length);
j3 0:c770ad7363c8 215
j3 0:c770ad7363c8 216 return(rtn_val);
j3 0:c770ad7363c8 217 }
j3 0:c770ad7363c8 218
j3 0:c770ad7363c8 219
j3 0:c770ad7363c8 220 /**********************************************************//**
j3 0:c770ad7363c8 221 * Reads data from direct access registers starting at DIR0 and
j3 0:c770ad7363c8 222 * stores it in byte array pointed at by 'data'
j3 0:c770ad7363c8 223 *
j3 0:c770ad7363c8 224 * On Entry:
j3 0:c770ad7363c8 225 * @param[in] data - pointer to byte array for storing data
j3 0:c770ad7363c8 226 *
j3 0:c770ad7363c8 227 * On Exit:
j3 0:c770ad7363c8 228 * @param[out] data - data buffer now contains data read
j3 0:c770ad7363c8 229 * from dir registers
j3 0:c770ad7363c8 230 * @return return value = 0 on success, non-0 on failure
j3 0:c770ad7363c8 231 *
j3 0:c770ad7363c8 232 * Example:
j3 0:c770ad7363c8 233 * @code
j3 0:c770ad7363c8 234 *
j3 0:c770ad7363c8 235 * I2C i2c_bus(D14, D15); //instantiate I2C bus
j3 0:c770ad7363c8 236 * I2C * p_i2c_bus = &i2c_bus; //create pointer to bus
j3 0:c770ad7363c8 237 *
j3 0:c770ad7363c8 238 * //instantiate mux object
j3 0:c770ad7363c8 239 * Max14661 mux(p_i2c_bus, MAX14661_I2C_ADRS0);
j3 0:c770ad7363c8 240 *
j3 0:c770ad7363c8 241 * uint16_t rtn_val;
j3 0:c770ad7363c8 242 * uint8_t data[4];
j3 0:c770ad7363c8 243 *
j3 0:c770ad7363c8 244 * //read direct access registers
j3 0:c770ad7363c8 245 * rtn_val = mux.rd_dir_registers(data);
j3 0:c770ad7363c8 246 *
j3 0:c770ad7363c8 247 * @endcode
j3 0:c770ad7363c8 248 **********************************************************************/
j3 0:c770ad7363c8 249 uint16_t Max14661::rd_dir_registers(uint8_t* data)
j3 0:c770ad7363c8 250 {
j3 0:c770ad7363c8 251 uint16_t rtn_val = 1;
j3 0:c770ad7363c8 252 uint8_t local_data = DIR0;
j3 0:c770ad7363c8 253
j3 0:c770ad7363c8 254 rtn_val = p_i2c->write(w_adrs,(const char*) local_data, 1);
j3 0:c770ad7363c8 255
j3 0:c770ad7363c8 256 if(!rtn_val)
j3 0:c770ad7363c8 257 {
j3 0:c770ad7363c8 258 rtn_val = p_i2c->read(r_adrs,(char*) data, 4);
j3 0:c770ad7363c8 259 }
j3 0:c770ad7363c8 260
j3 0:c770ad7363c8 261 return(rtn_val);
j3 0:c770ad7363c8 262 }
j3 0:c770ad7363c8 263
j3 0:c770ad7363c8 264
j3 0:c770ad7363c8 265 /**********************************************************//**
j3 0:c770ad7363c8 266 * Reads data from shadow registers starting at SHDW0 and stores
j3 0:c770ad7363c8 267 * it in byte array pointed at by 'data'
j3 0:c770ad7363c8 268 *
j3 0:c770ad7363c8 269 * On Entry:
j3 0:c770ad7363c8 270 * @param[in] data - pointer to byte array for storing data
j3 0:c770ad7363c8 271 *
j3 0:c770ad7363c8 272 * On Exit:
j3 0:c770ad7363c8 273 * @param[out] data - data buffer now contains data read
j3 0:c770ad7363c8 274 * from shadow registers
j3 0:c770ad7363c8 275 * @return return value = 0 on success, non-0 on failure
j3 0:c770ad7363c8 276 *
j3 0:c770ad7363c8 277 * Example:
j3 0:c770ad7363c8 278 * @code
j3 0:c770ad7363c8 279 *
j3 0:c770ad7363c8 280 * I2C i2c_bus(D14, D15); //instantiate I2C bus
j3 0:c770ad7363c8 281 * I2C * p_i2c_bus = &i2c_bus; //create pointer to bus
j3 0:c770ad7363c8 282 *
j3 0:c770ad7363c8 283 * //instantiate mux object
j3 0:c770ad7363c8 284 * Max14661 mux(p_i2c_bus, MAX14661_I2C_ADRS0);
j3 0:c770ad7363c8 285 *
j3 0:c770ad7363c8 286 * uint16_t rtn_val;
j3 0:c770ad7363c8 287 * uint8_t data[4];
j3 0:c770ad7363c8 288 *
j3 0:c770ad7363c8 289 * //read shadow registers
j3 0:c770ad7363c8 290 * rtn_val = mux.rd_shadow_registers(data);
j3 0:c770ad7363c8 291 *
j3 0:c770ad7363c8 292 * @endcode
j3 0:c770ad7363c8 293 **************************************************************/
j3 0:c770ad7363c8 294 uint16_t Max14661::rd_shadow_registers(uint8_t* data)
j3 0:c770ad7363c8 295 {
j3 0:c770ad7363c8 296 uint16_t rtn_val = 1;
j3 0:c770ad7363c8 297 uint8_t local_data = SHDW0;
j3 0:c770ad7363c8 298
j3 0:c770ad7363c8 299 rtn_val = p_i2c->write(w_adrs,(const char*) local_data, 1);
j3 0:c770ad7363c8 300
j3 0:c770ad7363c8 301 if(!rtn_val)
j3 0:c770ad7363c8 302 {
j3 0:c770ad7363c8 303 rtn_val = p_i2c->read(r_adrs,(char*) data, 4);
j3 0:c770ad7363c8 304 }
j3 0:c770ad7363c8 305
j3 0:c770ad7363c8 306 return(rtn_val);
j3 0:c770ad7363c8 307 }