Maxim Integrated / MAX4822

Dependents:   MAXREFDES130_131_Demo MAXREFDES130_Demo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MAX4822.cpp Source File

MAX4822.cpp

00001 /**********************************************************************
00002 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
00003 *
00004 * Permission is hereby granted, free of charge, to any person obtaining a
00005 * copy of this software and associated documentation files (the "Software"),
00006 * to deal in the Software without restriction, including without limitation
00007 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008 * and/or sell copies of the Software, and to permit persons to whom the
00009 * Software is furnished to do so, subject to the following conditions:
00010 *
00011 * The above copyright notice and this permission notice shall be included
00012 * in all copies or substantial portions of the Software.
00013 *
00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00015 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00017 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00018 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00019 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00020 * OTHER DEALINGS IN THE SOFTWARE.
00021 *
00022 * Except as contained in this notice, the name of Maxim Integrated
00023 * Products, Inc. shall not be used except as stated in the Maxim Integrated
00024 * Products, Inc. Branding Policy.
00025 *
00026 * The mere transfer of this software does not imply any licenses
00027 * of trade secrets, proprietary technology, copyrights, patents,
00028 * trademarks, maskwork rights, or any other form of intellectual
00029 * property whatsoever. Maxim Integrated Products, Inc. retains all
00030 * ownership rights.
00031 **********************************************************************/
00032 
00033 #include "MAX4822.h"
00034 
00035 //*********************************************************************
00036 MAX4822::MAX4822(SPI & spi_bus, PinName cs, uint8_t num_devices): 
00037 m_spi(spi_bus), m_cs(cs, 1), m_num_devices(num_devices)
00038 {
00039     if(m_num_devices)
00040     {
00041         for(uint8_t idx = 0; idx < m_num_devices; idx++)
00042         {
00043             m_relay_data[idx] = 0;
00044             m_pwr_save_data[idx] = 0;
00045         }
00046     }
00047     else
00048     {
00049         m_relay_data[0] = 0;
00050         m_pwr_save_data[0] = 0;
00051     }
00052 }
00053 
00054 //*********************************************************************
00055 MAX4822::~MAX4822()
00056 {
00057 }
00058 
00059 //*********************************************************************
00060 MAX4822::CmdResult MAX4822::set_all_relays(DigitalOut & set, uint8_t device)
00061 {
00062     MAX4822::CmdResult result = OpFailure;
00063     
00064     if(device <= m_num_devices)
00065     {
00066         set = 0;
00067         wait_us(1);
00068         set = 1;
00069         
00070         m_relay_data[device] = 0xFF;
00071         result = MAX4822::Success;
00072     }
00073     
00074     return result;
00075 }
00076 
00077 //*********************************************************************
00078 MAX4822::CmdResult MAX4822::reset_all_relays(DigitalOut & reset, uint8_t device)
00079 {
00080     MAX4822::CmdResult result = OpFailure;
00081     
00082     if(device <= m_num_devices)
00083     {
00084         reset = 0;
00085         wait_us(1);
00086         reset = 1;
00087         
00088         m_relay_data[device] = 0;
00089         result = MAX4822::Success;
00090     }
00091     
00092     return result;
00093     
00094 }
00095 
00096 //*********************************************************************
00097 MAX4822::CmdResult MAX4822::set_relay(RelayChannel r, bool send_data, uint8_t n)
00098 {
00099     MAX4822::CmdResult result = OpFailure;
00100     
00101     if(n <= m_num_devices)
00102     {
00103         m_relay_data[n] |= (1 << (r - 1));
00104         
00105         if(send_data)
00106         {
00107             uint16_t num_writes = n + 1;
00108             
00109             m_cs = 0;
00110             while(num_writes--)
00111             {
00112                 m_spi.write(MAX4822::OUTPUT_CNTL_REG);
00113                 m_spi.write(m_relay_data[num_writes]);
00114             }
00115             m_cs = 1;
00116         }
00117         
00118         result = MAX4822::Success;
00119     }
00120     
00121     return result;
00122 }
00123 
00124 //*********************************************************************
00125 MAX4822::CmdResult MAX4822::reset_relay(RelayChannel r, bool send_data, uint8_t n)
00126 {
00127     MAX4822::CmdResult result = OpFailure;
00128     
00129     if(n <= m_num_devices)
00130     {
00131         m_relay_data[n] &= ~(1 << (r - 1));
00132         
00133         if(send_data)
00134         {
00135             uint16_t num_writes = n + 1;
00136             
00137             m_cs = 0;
00138             while(num_writes--)
00139             {
00140                 m_spi.write(MAX4822::OUTPUT_CNTL_REG);
00141                 m_spi.write(m_relay_data[num_writes]);
00142             }
00143             m_cs = 1;
00144         }
00145         
00146         result = MAX4822::Success;
00147     }
00148     
00149     return result;
00150 }
00151 
00152 //*********************************************************************
00153 MAX4822::CmdResult MAX4822::set_pwr_save(PowerSave pwr_save, bool send_data, uint8_t n)
00154 {
00155     MAX4822::CmdResult result = OpFailure;
00156     
00157     if(n <= m_num_devices)
00158     {
00159         m_pwr_save_data[n] = pwr_save;
00160         
00161         if(send_data)
00162         {
00163             uint16_t num_writes = n + 1;
00164             
00165             m_cs = 0;
00166             while(num_writes--)
00167             {
00168                 m_spi.write(MAX4822::POWER_SAVE_REG);
00169                 m_spi.write(m_pwr_save_data[num_writes]);
00170             }
00171             m_cs = 1;
00172         }
00173         
00174         result = MAX4822::Success;
00175     }
00176     
00177     return result;
00178 }