fork of library for MAX14661 16:2 mux

Dependents:   ard2pmod

Fork of max14661 by Maxim Integrated

Committer:
j3
Date:
Tue Sep 29 23:06:01 2015 +0000
Revision:
11:d3971b4fbdd8
Parent:
10:ccbe1afdab31
Child:
12:f14ce75c0661
fixed some comments

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 7:1d4e59ec0fba 6 * @version 1.0
j3 0:c770ad7363c8 7 *
j3 0:c770ad7363c8 8 * Started: 11NOV14
j3 0:c770ad7363c8 9 *
j3 0:c770ad7363c8 10 * Updated:
j3 10:ccbe1afdab31 11 * 29SEP15 - added second constructor that uses pointer to I2C bus
j3 11:d3971b4fbdd8 12 * - added destructor
j3 10:ccbe1afdab31 13 * - removed redundant comments, see 'DRY' methodology
j3 0:c770ad7363c8 14 *
j3 0:c770ad7363c8 15 * @brief Source file for MAX14661 class
j3 0:c770ad7363c8 16 *
j3 0:c770ad7363c8 17 ***********************************************************************
j3 0:c770ad7363c8 18 *
j3 0:c770ad7363c8 19 * @copyright
j3 8:44257d87fa9e 20 * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved.
j3 0:c770ad7363c8 21 *
j3 0:c770ad7363c8 22 * Permission is hereby granted, free of charge, to any person obtaining a
j3 0:c770ad7363c8 23 * copy of this software and associated documentation files (the "Software"),
j3 0:c770ad7363c8 24 * to deal in the Software without restriction, including without limitation
j3 0:c770ad7363c8 25 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
j3 0:c770ad7363c8 26 * and/or sell copies of the Software, and to permit persons to whom the
j3 0:c770ad7363c8 27 * Software is furnished to do so, subject to the following conditions:
j3 0:c770ad7363c8 28 *
j3 0:c770ad7363c8 29 * The above copyright notice and this permission notice shall be included
j3 0:c770ad7363c8 30 * in all copies or substantial portions of the Software.
j3 0:c770ad7363c8 31 *
j3 0:c770ad7363c8 32 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
j3 0:c770ad7363c8 33 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
j3 0:c770ad7363c8 34 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
j3 0:c770ad7363c8 35 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
j3 0:c770ad7363c8 36 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
j3 0:c770ad7363c8 37 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
j3 0:c770ad7363c8 38 * OTHER DEALINGS IN THE SOFTWARE.
j3 0:c770ad7363c8 39 *
j3 0:c770ad7363c8 40 * Except as contained in this notice, the name of Maxim Integrated
j3 0:c770ad7363c8 41 * Products, Inc. shall not be used except as stated in the Maxim Integrated
j3 0:c770ad7363c8 42 * Products, Inc. Branding Policy.
j3 0:c770ad7363c8 43 *
j3 0:c770ad7363c8 44 * The mere transfer of this software does not imply any licenses
j3 0:c770ad7363c8 45 * of trade secrets, proprietary technology, copyrights, patents,
j3 0:c770ad7363c8 46 * trademarks, maskwork rights, or any other form of intellectual
j3 0:c770ad7363c8 47 * property whatsoever. Maxim Integrated Products, Inc. retains all
j3 0:c770ad7363c8 48 * ownership rights.
j3 0:c770ad7363c8 49 **********************************************************************/
j3 0:c770ad7363c8 50
j3 0:c770ad7363c8 51
j3 0:c770ad7363c8 52 #include "max14661.h"
j3 0:c770ad7363c8 53
j3 0:c770ad7363c8 54
j3 10:ccbe1afdab31 55 //*********************************************************************
j3 10:ccbe1afdab31 56 Max14661::Max14661(I2C *i2c_bus, max14661_i2c_adrs_t i2c_adrs): _p_i2c(i2c_bus)
j3 0:c770ad7363c8 57 {
j3 10:ccbe1afdab31 58 _i2c_owner = false;
j3 10:ccbe1afdab31 59
j3 10:ccbe1afdab31 60 _r_adrs = ((i2c_adrs << 1) | 1);
j3 10:ccbe1afdab31 61 _w_adrs = (i2c_adrs << 1);
j3 0:c770ad7363c8 62 }
j3 0:c770ad7363c8 63
j3 0:c770ad7363c8 64
j3 10:ccbe1afdab31 65 //*********************************************************************
j3 10:ccbe1afdab31 66 Max14661::Max14661(PinName sda, PinName scl, max14661_i2c_adrs_t i2c_adrs)
j3 10:ccbe1afdab31 67 {
j3 10:ccbe1afdab31 68 _p_i2c = new I2C(sda, scl);
j3 10:ccbe1afdab31 69 _i2c_owner = true;
j3 10:ccbe1afdab31 70
j3 10:ccbe1afdab31 71 _r_adrs = ((i2c_adrs << 1) | 1);
j3 10:ccbe1afdab31 72 _w_adrs = (i2c_adrs << 1);
j3 10:ccbe1afdab31 73 }
j3 10:ccbe1afdab31 74
j3 10:ccbe1afdab31 75
j3 10:ccbe1afdab31 76 //*********************************************************************
j3 10:ccbe1afdab31 77 Max14661::~Max14661()
j3 10:ccbe1afdab31 78 {
j3 10:ccbe1afdab31 79 if(_i2c_owner)
j3 10:ccbe1afdab31 80 {
j3 10:ccbe1afdab31 81 delete _p_i2c;
j3 10:ccbe1afdab31 82 }
j3 10:ccbe1afdab31 83 }
j3 10:ccbe1afdab31 84
j3 10:ccbe1afdab31 85
j3 10:ccbe1afdab31 86 /*********************************************************************/
j3 0:c770ad7363c8 87 uint16_t Max14661::wrt_cmd_registers(max14661_cmds_t cmdA,
j3 0:c770ad7363c8 88 max14661_cmds_t cmdB)
j3 0:c770ad7363c8 89 {
j3 0:c770ad7363c8 90 uint8_t data[3];
j3 0:c770ad7363c8 91 uint8_t data_length = 0;
j3 0:c770ad7363c8 92 uint16_t rtn_val = 1;
j3 0:c770ad7363c8 93
j3 0:c770ad7363c8 94 //build packet
j3 0:c770ad7363c8 95 data[data_length++] = CMD_A;
j3 0:c770ad7363c8 96 data[data_length++] = cmdA;
j3 0:c770ad7363c8 97 data[data_length++] = cmdB;
j3 0:c770ad7363c8 98
j3 10:ccbe1afdab31 99 rtn_val = _p_i2c->write(_w_adrs,(const char*) data, data_length);
j3 0:c770ad7363c8 100
j3 0:c770ad7363c8 101 return(rtn_val);
j3 0:c770ad7363c8 102 }
j3 0:c770ad7363c8 103
j3 0:c770ad7363c8 104
j3 10:ccbe1afdab31 105 /*********************************************************************/
j3 4:45fa0192f66d 106 uint16_t Max14661::wrt_shadow_registers(uint16_t bankA, uint16_t bankB)
j3 0:c770ad7363c8 107 {
j3 4:45fa0192f66d 108 uint8_t data[5];
j3 0:c770ad7363c8 109 uint8_t data_length = 0;
j3 0:c770ad7363c8 110 uint16_t rtn_val = 1;
j3 0:c770ad7363c8 111
j3 4:45fa0192f66d 112 data[data_length++] = SHDW0;
j3 4:45fa0192f66d 113 data[data_length++] = (bankA & 0x00FF);
j3 4:45fa0192f66d 114 data[data_length++] = ((bankA >> 8) & 0x00FF);
j3 4:45fa0192f66d 115 data[data_length++] = (bankB & 0x00FF);
j3 4:45fa0192f66d 116 data[data_length++] = ((bankB >> 8) & 0x00FF);
j3 0:c770ad7363c8 117
j3 10:ccbe1afdab31 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 10:ccbe1afdab31 124 /*********************************************************************/
j3 4:45fa0192f66d 125 uint16_t Max14661::wrt_dir_registers(uint16_t bankA, uint16_t bankB)
j3 0:c770ad7363c8 126 {
j3 4:45fa0192f66d 127 uint8_t data[5];
j3 0:c770ad7363c8 128 uint8_t data_length = 0;
j3 0:c770ad7363c8 129 uint16_t rtn_val = 1;
j3 0:c770ad7363c8 130
j3 4:45fa0192f66d 131 data[data_length++] = DIR0;
j3 4:45fa0192f66d 132 data[data_length++] = (bankA & 0x00FF);
j3 4:45fa0192f66d 133 data[data_length++] = ((bankA >> 8) & 0x00FF);
j3 4:45fa0192f66d 134 data[data_length++] = (bankB & 0x00FF);
j3 4:45fa0192f66d 135 data[data_length++] = ((bankB >> 8) & 0x00FF);
j3 4:45fa0192f66d 136
j3 10:ccbe1afdab31 137 rtn_val = _p_i2c->write(_w_adrs,(const char*) data, data_length);
j3 0:c770ad7363c8 138
j3 4:45fa0192f66d 139 return(rtn_val);
j3 4:45fa0192f66d 140 }
j3 4:45fa0192f66d 141
j3 4:45fa0192f66d 142
j3 10:ccbe1afdab31 143 /*********************************************************************/
j3 4:45fa0192f66d 144 uint16_t Max14661::set_switches(uint16_t bankA, uint16_t bankB)
j3 4:45fa0192f66d 145 {
j3 4:45fa0192f66d 146 uint8_t data[7];
j3 4:45fa0192f66d 147 uint8_t data_length = 0;
j3 4:45fa0192f66d 148 uint16_t rtn_val = 1;
j3 0:c770ad7363c8 149
j3 4:45fa0192f66d 150 data[data_length++] = SHDW0;
j3 4:45fa0192f66d 151 data[data_length++] = (bankA & 0x00FF);
j3 4:45fa0192f66d 152 data[data_length++] = ((bankA >> 8) & 0x00FF);
j3 4:45fa0192f66d 153 data[data_length++] = (bankB & 0x00FF);
j3 4:45fa0192f66d 154 data[data_length++] = ((bankB >> 8) & 0x00FF);
j3 4:45fa0192f66d 155 data[data_length++] = COPY_SHADOW;
j3 4:45fa0192f66d 156 data[data_length++] = COPY_SHADOW;
j3 4:45fa0192f66d 157
j3 10:ccbe1afdab31 158 rtn_val = _p_i2c->write(_w_adrs,(const char*) data, data_length);
j3 0:c770ad7363c8 159
j3 0:c770ad7363c8 160 return(rtn_val);
j3 0:c770ad7363c8 161 }
j3 0:c770ad7363c8 162
j3 0:c770ad7363c8 163
j3 10:ccbe1afdab31 164 /*********************************************************************/
j3 0:c770ad7363c8 165 uint16_t Max14661::rd_dir_registers(uint8_t* data)
j3 0:c770ad7363c8 166 {
j3 0:c770ad7363c8 167 uint16_t rtn_val = 1;
j3 0:c770ad7363c8 168
j3 6:be4f2d7fc054 169 data[0] = DIR0;
j3 6:be4f2d7fc054 170
j3 10:ccbe1afdab31 171 rtn_val = _p_i2c->write(_w_adrs,(const char*) data, 1);
j3 0:c770ad7363c8 172
j3 0:c770ad7363c8 173 if(!rtn_val)
j3 0:c770ad7363c8 174 {
j3 10:ccbe1afdab31 175 rtn_val = _p_i2c->read(_r_adrs,(char*) data, 4);
j3 0:c770ad7363c8 176 }
j3 0:c770ad7363c8 177
j3 0:c770ad7363c8 178 return(rtn_val);
j3 0:c770ad7363c8 179 }
j3 0:c770ad7363c8 180
j3 0:c770ad7363c8 181
j3 10:ccbe1afdab31 182 /*********************************************************************/
j3 0:c770ad7363c8 183 uint16_t Max14661::rd_shadow_registers(uint8_t* data)
j3 0:c770ad7363c8 184 {
j3 0:c770ad7363c8 185 uint16_t rtn_val = 1;
j3 0:c770ad7363c8 186
j3 6:be4f2d7fc054 187 data[0] = SHDW0;
j3 6:be4f2d7fc054 188
j3 10:ccbe1afdab31 189 rtn_val = _p_i2c->write(_w_adrs,(const char*) data, 1);
j3 0:c770ad7363c8 190
j3 0:c770ad7363c8 191 if(!rtn_val)
j3 0:c770ad7363c8 192 {
j3 10:ccbe1afdab31 193 rtn_val = _p_i2c->read(_r_adrs,(char*) data, 4);
j3 0:c770ad7363c8 194 }
j3 0:c770ad7363c8 195
j3 0:c770ad7363c8 196 return(rtn_val);
j3 0:c770ad7363c8 197 }