Library for MAX7300 GPIO Expander
max7300.cpp
- Committer:
- j3
- Date:
- 2015-07-15
- Revision:
- 0:350a850a7191
- Child:
- 1:e1ee2549a047
File content as of revision 0:350a850a7191:
/******************************************************************//** * @file max7300.h * * @author Justin Jordan * * @version 0.0 * * Started: 14JUL15 * * Updated: * * @brief Source file for Max7300 class *********************************************************************** * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * * Except as contained in this notice, the name of Maxim Integrated * Products, Inc. shall not be used except as stated in the Maxim Integrated * Products, Inc. Branding Policy. * * The mere transfer of this software does not imply any licenses * of trade secrets, proprietary technology, copyrights, patents, * trademarks, maskwork rights, or any other form of intellectual * property whatsoever. Maxim Integrated Products, Inc. retains all * ownership rights. **********************************************************************/ #include "max7300.h" //********************************************************************* Max7300::Max7300(I2C *i2c_bus, max7300_i2c_adrs_t i2c_adrs): _p_i2c(i2c_bus) { _i2c_owner = false; _r_adrs = ((i2c_adrs << 1) | 1); _w_adrs = (i2c_adrs << 1); } //********************************************************************* Max7300::Max7300(PinName sda, PinName scl, max7300_i2c_adrs_t i2c_adrs) { _p_i2c = new I2C(sda, scl); _i2c_owner = true; _r_adrs = ((i2c_adrs << 1) | 1); _w_adrs = (i2c_adrs << 1); } //********************************************************************* Max7300::~Max7300() { if(_i2c_owner) { delete _p_i2c; } } //********************************************************************* int16_t Max7300::enable_ports(void) { int16_t result = -1; char data[] = {MAX7300_CONFIGURATION, 0}; //set internal register pointer to configuration register result = _p_i2c->write(_w_adrs, data, 1, true); if(!result) { //get current configuration register result = _p_i2c->read(_w_adrs, (data +1 ), 1, true); if(!result) { //set 'S' bit data[1] |= 0x01; //write back to device result = result = _p_i2c->write(_w_adrs, data, 2, false); } } return result; } //********************************************************************* int16_t Max7300::disable_ports(void) { int16_t result = -1; char data[] = {MAX7300_CONFIGURATION, 0}; //set internal register pointer to configuration register result = _p_i2c->write(_w_adrs, data, 1, true); if(!result) { //get current configuration register result = _p_i2c->read(_w_adrs, (data +1 ), 1, true); if(!result) { //clear 'S' bit data[1] &= ~0x01; //write back to device result = result = _p_i2c->write(_w_adrs, data, 2, false); } } return result; } //********************************************************************* int16_t Max7300::config_port(max7300_port_number_t port_num, max7300_port_type_t port_type) { int16_t result = -1; char data[2]; //get address of port configuration register data[0] = ((port_num/4) + 8); //get port config bits offset in that register uint8_t offset = (port_num % 4); //set internal register pointer to port configuration register result = _p_i2c->write(_w_adrs, data, 1, true); if(!result) { //get current port configuration register result = _p_i2c->read(_w_adrs, (data +1 ), 1, true); if(!result) { //clear old port configuration data[1] &= ~(0x03 << (offset*2)); //set port configuration bits data[1] |= ((port_type & 0x03) << (offset*2)); //write back to device result = result = _p_i2c->write(_w_adrs, data, 2, false); } } return result; } //********************************************************************* int16_t Max7300::config_ports(max7300_registers_t reg, uint8_t data) { int16_t result = -1; char local_data[] = {reg, data}; //no need for read, modify, write. //Fx is intended to write whole register result = _p_i2c->write(_w_adrs, local_data, 2, false); return result; } //********************************************************************* int16_t Max7300::read_port(max7300_port_number_t port_num) { int16_t result = -1; char data[2]; data[0] = (port_num + 0x20); //set internal register pointer to port data register result = _p_i2c->write(_w_adrs, data, 1, true); if(!result) { //get port data result = _p_i2c->read(_w_adrs, (data +1 ), 1, false); if(!result) { result = data[1]; } else { result = -1; } } return result; } //********************************************************************* int16_t Max7300::write_port(max7300_port_number_t port_num, uint8_t data) { int16_t result = -1; char local_data[] = {(port_num + 0x20), data}; //no need for read, modify, write. //Fx is intended to write whole register result = _p_i2c->write(_w_adrs, local_data, 2, false); return result; } //********************************************************************* int16_t Max7300::read_eight_ports(max7300_registers_t reg) { int16_t result = -1; char data[2]; data[0] = reg; //set internal register pointer to port data register result = _p_i2c->write(_w_adrs, data, 1, true); if(!result) { //get port data result = _p_i2c->read(_w_adrs, (data +1 ), 1, false); if(!result) { result = data[1]; } else { result = -1; } } return result; } //********************************************************************* int16_t Max7300::write_eight_ports(max7300_registers_t reg, uint8_t data) { int16_t result = -1; char local_data[] = {reg, data}; //no need for read, modify, write. //Fx is intended to write whole register result = _p_i2c->write(_w_adrs, local_data, 2, false); return result; }