fork of library for MAX14661 16:2 mux

Dependents:   ard2pmod

Fork of max14661 by Maxim Integrated

Committer:
j3
Date:
Tue Mar 29 00:58:54 2016 +0000
Revision:
12:f14ce75c0661
Parent:
11:d3971b4fbdd8
removed inheritance of I2C class and used private member var instead

Who changed what in which revision?

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