Library for interfacing with the MAX4822 relay driver.
Dependents: MAXREFDES130_131_Demo MAXREFDES130_Demo
MAX4822.cpp@3:90f7cd976f18, 2016-08-02 (annotated)
- Committer:
- j3
- Date:
- Tue Aug 02 23:17:48 2016 +0000
- Revision:
- 3:90f7cd976f18
- Parent:
- 2:7b30a3361e40
Updated set_all/reset_all fxs to update private data array
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
j3 | 0:074983020f27 | 1 | /********************************************************************** |
j3 | 0:074983020f27 | 2 | * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. |
j3 | 0:074983020f27 | 3 | * |
j3 | 0:074983020f27 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
j3 | 0:074983020f27 | 5 | * copy of this software and associated documentation files (the "Software"), |
j3 | 0:074983020f27 | 6 | * to deal in the Software without restriction, including without limitation |
j3 | 0:074983020f27 | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
j3 | 0:074983020f27 | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
j3 | 0:074983020f27 | 9 | * Software is furnished to do so, subject to the following conditions: |
j3 | 0:074983020f27 | 10 | * |
j3 | 0:074983020f27 | 11 | * The above copyright notice and this permission notice shall be included |
j3 | 0:074983020f27 | 12 | * in all copies or substantial portions of the Software. |
j3 | 0:074983020f27 | 13 | * |
j3 | 0:074983020f27 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
j3 | 0:074983020f27 | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
j3 | 0:074983020f27 | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
j3 | 0:074983020f27 | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
j3 | 0:074983020f27 | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
j3 | 0:074983020f27 | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
j3 | 0:074983020f27 | 20 | * OTHER DEALINGS IN THE SOFTWARE. |
j3 | 0:074983020f27 | 21 | * |
j3 | 0:074983020f27 | 22 | * Except as contained in this notice, the name of Maxim Integrated |
j3 | 0:074983020f27 | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
j3 | 0:074983020f27 | 24 | * Products, Inc. Branding Policy. |
j3 | 0:074983020f27 | 25 | * |
j3 | 0:074983020f27 | 26 | * The mere transfer of this software does not imply any licenses |
j3 | 0:074983020f27 | 27 | * of trade secrets, proprietary technology, copyrights, patents, |
j3 | 0:074983020f27 | 28 | * trademarks, maskwork rights, or any other form of intellectual |
j3 | 0:074983020f27 | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
j3 | 0:074983020f27 | 30 | * ownership rights. |
j3 | 0:074983020f27 | 31 | **********************************************************************/ |
j3 | 0:074983020f27 | 32 | |
j3 | 0:074983020f27 | 33 | #include "MAX4822.h" |
j3 | 0:074983020f27 | 34 | |
j3 | 0:074983020f27 | 35 | //********************************************************************* |
j3 | 1:0263f798de82 | 36 | MAX4822::MAX4822(SPI & spi_bus, PinName cs, uint8_t num_devices): |
j3 | 1:0263f798de82 | 37 | m_spi(spi_bus), m_cs(cs, 1), m_num_devices(num_devices) |
j3 | 0:074983020f27 | 38 | { |
j3 | 1:0263f798de82 | 39 | if(m_num_devices) |
j3 | 1:0263f798de82 | 40 | { |
j3 | 1:0263f798de82 | 41 | for(uint8_t idx = 0; idx < m_num_devices; idx++) |
j3 | 1:0263f798de82 | 42 | { |
j3 | 1:0263f798de82 | 43 | m_relay_data[idx] = 0; |
j3 | 1:0263f798de82 | 44 | m_pwr_save_data[idx] = 0; |
j3 | 1:0263f798de82 | 45 | } |
j3 | 1:0263f798de82 | 46 | } |
j3 | 1:0263f798de82 | 47 | else |
j3 | 1:0263f798de82 | 48 | { |
j3 | 1:0263f798de82 | 49 | m_relay_data[0] = 0; |
j3 | 1:0263f798de82 | 50 | m_pwr_save_data[0] = 0; |
j3 | 1:0263f798de82 | 51 | } |
j3 | 0:074983020f27 | 52 | } |
j3 | 0:074983020f27 | 53 | |
j3 | 0:074983020f27 | 54 | //********************************************************************* |
j3 | 0:074983020f27 | 55 | MAX4822::~MAX4822() |
j3 | 0:074983020f27 | 56 | { |
j3 | 0:074983020f27 | 57 | } |
j3 | 0:074983020f27 | 58 | |
j3 | 0:074983020f27 | 59 | //********************************************************************* |
j3 | 3:90f7cd976f18 | 60 | MAX4822::CmdResult MAX4822::set_all_relays(DigitalOut & set, uint8_t device) |
j3 | 1:0263f798de82 | 61 | { |
j3 | 3:90f7cd976f18 | 62 | MAX4822::CmdResult result = OpFailure; |
j3 | 3:90f7cd976f18 | 63 | |
j3 | 3:90f7cd976f18 | 64 | if(device <= m_num_devices) |
j3 | 3:90f7cd976f18 | 65 | { |
j3 | 3:90f7cd976f18 | 66 | set = 0; |
j3 | 3:90f7cd976f18 | 67 | wait_us(1); |
j3 | 3:90f7cd976f18 | 68 | set = 1; |
j3 | 3:90f7cd976f18 | 69 | |
j3 | 3:90f7cd976f18 | 70 | m_relay_data[device] = 0xFF; |
j3 | 3:90f7cd976f18 | 71 | result = MAX4822::Success; |
j3 | 3:90f7cd976f18 | 72 | } |
j3 | 3:90f7cd976f18 | 73 | |
j3 | 3:90f7cd976f18 | 74 | return result; |
j3 | 1:0263f798de82 | 75 | } |
j3 | 1:0263f798de82 | 76 | |
j3 | 1:0263f798de82 | 77 | //********************************************************************* |
j3 | 3:90f7cd976f18 | 78 | MAX4822::CmdResult MAX4822::reset_all_relays(DigitalOut & reset, uint8_t device) |
j3 | 1:0263f798de82 | 79 | { |
j3 | 3:90f7cd976f18 | 80 | MAX4822::CmdResult result = OpFailure; |
j3 | 3:90f7cd976f18 | 81 | |
j3 | 3:90f7cd976f18 | 82 | if(device <= m_num_devices) |
j3 | 3:90f7cd976f18 | 83 | { |
j3 | 3:90f7cd976f18 | 84 | reset = 0; |
j3 | 3:90f7cd976f18 | 85 | wait_us(1); |
j3 | 3:90f7cd976f18 | 86 | reset = 1; |
j3 | 3:90f7cd976f18 | 87 | |
j3 | 3:90f7cd976f18 | 88 | m_relay_data[device] = 0; |
j3 | 3:90f7cd976f18 | 89 | result = MAX4822::Success; |
j3 | 3:90f7cd976f18 | 90 | } |
j3 | 3:90f7cd976f18 | 91 | |
j3 | 3:90f7cd976f18 | 92 | return result; |
j3 | 3:90f7cd976f18 | 93 | |
j3 | 1:0263f798de82 | 94 | } |
j3 | 1:0263f798de82 | 95 | |
j3 | 1:0263f798de82 | 96 | //********************************************************************* |
j3 | 1:0263f798de82 | 97 | MAX4822::CmdResult MAX4822::set_relay(RelayChannel r, bool send_data, uint8_t n) |
j3 | 0:074983020f27 | 98 | { |
j3 | 0:074983020f27 | 99 | MAX4822::CmdResult result = OpFailure; |
j3 | 0:074983020f27 | 100 | |
j3 | 1:0263f798de82 | 101 | if(n <= m_num_devices) |
j3 | 0:074983020f27 | 102 | { |
j3 | 1:0263f798de82 | 103 | m_relay_data[n] |= (1 << (r - 1)); |
j3 | 1:0263f798de82 | 104 | |
j3 | 1:0263f798de82 | 105 | if(send_data) |
j3 | 1:0263f798de82 | 106 | { |
j3 | 1:0263f798de82 | 107 | uint16_t num_writes = n + 1; |
j3 | 1:0263f798de82 | 108 | |
j3 | 1:0263f798de82 | 109 | m_cs = 0; |
j3 | 1:0263f798de82 | 110 | while(num_writes--) |
j3 | 1:0263f798de82 | 111 | { |
j3 | 1:0263f798de82 | 112 | m_spi.write(MAX4822::OUTPUT_CNTL_REG); |
j3 | 1:0263f798de82 | 113 | m_spi.write(m_relay_data[num_writes]); |
j3 | 1:0263f798de82 | 114 | } |
j3 | 1:0263f798de82 | 115 | m_cs = 1; |
j3 | 1:0263f798de82 | 116 | } |
j3 | 1:0263f798de82 | 117 | |
j3 | 0:074983020f27 | 118 | result = MAX4822::Success; |
j3 | 0:074983020f27 | 119 | } |
j3 | 0:074983020f27 | 120 | |
j3 | 0:074983020f27 | 121 | return result; |
j3 | 0:074983020f27 | 122 | } |
j3 | 0:074983020f27 | 123 | |
j3 | 0:074983020f27 | 124 | //********************************************************************* |
j3 | 1:0263f798de82 | 125 | MAX4822::CmdResult MAX4822::reset_relay(RelayChannel r, bool send_data, uint8_t n) |
j3 | 0:074983020f27 | 126 | { |
j3 | 0:074983020f27 | 127 | MAX4822::CmdResult result = OpFailure; |
j3 | 0:074983020f27 | 128 | |
j3 | 1:0263f798de82 | 129 | if(n <= m_num_devices) |
j3 | 1:0263f798de82 | 130 | { |
j3 | 1:0263f798de82 | 131 | m_relay_data[n] &= ~(1 << (r - 1)); |
j3 | 1:0263f798de82 | 132 | |
j3 | 1:0263f798de82 | 133 | if(send_data) |
j3 | 1:0263f798de82 | 134 | { |
j3 | 1:0263f798de82 | 135 | uint16_t num_writes = n + 1; |
j3 | 1:0263f798de82 | 136 | |
j3 | 1:0263f798de82 | 137 | m_cs = 0; |
j3 | 1:0263f798de82 | 138 | while(num_writes--) |
j3 | 1:0263f798de82 | 139 | { |
j3 | 1:0263f798de82 | 140 | m_spi.write(MAX4822::OUTPUT_CNTL_REG); |
j3 | 1:0263f798de82 | 141 | m_spi.write(m_relay_data[num_writes]); |
j3 | 1:0263f798de82 | 142 | } |
j3 | 1:0263f798de82 | 143 | m_cs = 1; |
j3 | 1:0263f798de82 | 144 | } |
j3 | 1:0263f798de82 | 145 | |
j3 | 1:0263f798de82 | 146 | result = MAX4822::Success; |
j3 | 1:0263f798de82 | 147 | } |
j3 | 1:0263f798de82 | 148 | |
j3 | 0:074983020f27 | 149 | return result; |
j3 | 0:074983020f27 | 150 | } |
j3 | 0:074983020f27 | 151 | |
j3 | 0:074983020f27 | 152 | //********************************************************************* |
j3 | 1:0263f798de82 | 153 | MAX4822::CmdResult MAX4822::set_pwr_save(PowerSave pwr_save, bool send_data, uint8_t n) |
j3 | 0:074983020f27 | 154 | { |
j3 | 0:074983020f27 | 155 | MAX4822::CmdResult result = OpFailure; |
j3 | 0:074983020f27 | 156 | |
j3 | 1:0263f798de82 | 157 | if(n <= m_num_devices) |
j3 | 1:0263f798de82 | 158 | { |
j3 | 1:0263f798de82 | 159 | m_pwr_save_data[n] = pwr_save; |
j3 | 1:0263f798de82 | 160 | |
j3 | 1:0263f798de82 | 161 | if(send_data) |
j3 | 1:0263f798de82 | 162 | { |
j3 | 1:0263f798de82 | 163 | uint16_t num_writes = n + 1; |
j3 | 1:0263f798de82 | 164 | |
j3 | 1:0263f798de82 | 165 | m_cs = 0; |
j3 | 1:0263f798de82 | 166 | while(num_writes--) |
j3 | 1:0263f798de82 | 167 | { |
j3 | 1:0263f798de82 | 168 | m_spi.write(MAX4822::POWER_SAVE_REG); |
j3 | 2:7b30a3361e40 | 169 | m_spi.write(m_pwr_save_data[num_writes]); |
j3 | 1:0263f798de82 | 170 | } |
j3 | 1:0263f798de82 | 171 | m_cs = 1; |
j3 | 1:0263f798de82 | 172 | } |
j3 | 1:0263f798de82 | 173 | |
j3 | 1:0263f798de82 | 174 | result = MAX4822::Success; |
j3 | 1:0263f798de82 | 175 | } |
j3 | 1:0263f798de82 | 176 | |
j3 | 0:074983020f27 | 177 | return result; |
j3 | 0:074983020f27 | 178 | } |