fork of library for MAX14661 16:2 mux

Dependents:   ard2pmod

Fork of max14661 by Maxim Integrated

Committer:
j3
Date:
Tue Sep 29 22:26:09 2015 +0000
Revision:
10:ccbe1afdab31
Parent:
8:44257d87fa9e
Child:
11:d3971b4fbdd8
Added second constructor for passing a pointer to an existing I2C object; Removed inheritance of the I2C class

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