Library for interfacing with the MAX4822 relay driver.
Dependents: MAXREFDES130_131_Demo MAXREFDES130_Demo
MAX4822.cpp@1:0263f798de82, 2016-07-28 (annotated)
- Committer:
- j3
- Date:
- Thu Jul 28 21:41:40 2016 +0000
- Revision:
- 1:0263f798de82
- Parent:
- 0:074983020f27
- Child:
- 2:7b30a3361e40
Added implementation
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 | 1:0263f798de82 | 60 | void MAX4822::set_all_relays(DigitalOut & set) |
j3 | 1:0263f798de82 | 61 | { |
j3 | 1:0263f798de82 | 62 | set = 0; |
j3 | 1:0263f798de82 | 63 | wait_us(1); |
j3 | 1:0263f798de82 | 64 | set = 1; |
j3 | 1:0263f798de82 | 65 | } |
j3 | 1:0263f798de82 | 66 | |
j3 | 1:0263f798de82 | 67 | //********************************************************************* |
j3 | 1:0263f798de82 | 68 | void MAX4822::reset_all_relays(DigitalOut & reset) |
j3 | 1:0263f798de82 | 69 | { |
j3 | 1:0263f798de82 | 70 | reset = 0; |
j3 | 1:0263f798de82 | 71 | wait_us(1); |
j3 | 1:0263f798de82 | 72 | reset = 1; |
j3 | 1:0263f798de82 | 73 | } |
j3 | 1:0263f798de82 | 74 | |
j3 | 1:0263f798de82 | 75 | //********************************************************************* |
j3 | 1:0263f798de82 | 76 | MAX4822::CmdResult MAX4822::set_relay(RelayChannel r, bool send_data, uint8_t n) |
j3 | 0:074983020f27 | 77 | { |
j3 | 0:074983020f27 | 78 | MAX4822::CmdResult result = OpFailure; |
j3 | 0:074983020f27 | 79 | |
j3 | 1:0263f798de82 | 80 | if(n <= m_num_devices) |
j3 | 0:074983020f27 | 81 | { |
j3 | 1:0263f798de82 | 82 | m_relay_data[n] |= (1 << (r - 1)); |
j3 | 1:0263f798de82 | 83 | |
j3 | 1:0263f798de82 | 84 | if(send_data) |
j3 | 1:0263f798de82 | 85 | { |
j3 | 1:0263f798de82 | 86 | uint16_t num_writes = n + 1; |
j3 | 1:0263f798de82 | 87 | |
j3 | 1:0263f798de82 | 88 | m_cs = 0; |
j3 | 1:0263f798de82 | 89 | while(num_writes--) |
j3 | 1:0263f798de82 | 90 | { |
j3 | 1:0263f798de82 | 91 | m_spi.write(MAX4822::OUTPUT_CNTL_REG); |
j3 | 1:0263f798de82 | 92 | m_spi.write(m_relay_data[num_writes]); |
j3 | 1:0263f798de82 | 93 | } |
j3 | 1:0263f798de82 | 94 | m_cs = 1; |
j3 | 1:0263f798de82 | 95 | } |
j3 | 1:0263f798de82 | 96 | |
j3 | 0:074983020f27 | 97 | result = MAX4822::Success; |
j3 | 0:074983020f27 | 98 | } |
j3 | 0:074983020f27 | 99 | |
j3 | 0:074983020f27 | 100 | return result; |
j3 | 0:074983020f27 | 101 | } |
j3 | 0:074983020f27 | 102 | |
j3 | 0:074983020f27 | 103 | //********************************************************************* |
j3 | 1:0263f798de82 | 104 | MAX4822::CmdResult MAX4822::reset_relay(RelayChannel r, bool send_data, uint8_t n) |
j3 | 0:074983020f27 | 105 | { |
j3 | 0:074983020f27 | 106 | MAX4822::CmdResult result = OpFailure; |
j3 | 0:074983020f27 | 107 | |
j3 | 1:0263f798de82 | 108 | if(n <= m_num_devices) |
j3 | 1:0263f798de82 | 109 | { |
j3 | 1:0263f798de82 | 110 | m_relay_data[n] &= ~(1 << (r - 1)); |
j3 | 1:0263f798de82 | 111 | |
j3 | 1:0263f798de82 | 112 | if(send_data) |
j3 | 1:0263f798de82 | 113 | { |
j3 | 1:0263f798de82 | 114 | uint16_t num_writes = n + 1; |
j3 | 1:0263f798de82 | 115 | |
j3 | 1:0263f798de82 | 116 | m_cs = 0; |
j3 | 1:0263f798de82 | 117 | while(num_writes--) |
j3 | 1:0263f798de82 | 118 | { |
j3 | 1:0263f798de82 | 119 | m_spi.write(MAX4822::OUTPUT_CNTL_REG); |
j3 | 1:0263f798de82 | 120 | m_spi.write(m_relay_data[num_writes]); |
j3 | 1:0263f798de82 | 121 | } |
j3 | 1:0263f798de82 | 122 | m_cs = 1; |
j3 | 1:0263f798de82 | 123 | } |
j3 | 1:0263f798de82 | 124 | |
j3 | 1:0263f798de82 | 125 | result = MAX4822::Success; |
j3 | 1:0263f798de82 | 126 | } |
j3 | 1:0263f798de82 | 127 | |
j3 | 0:074983020f27 | 128 | return result; |
j3 | 0:074983020f27 | 129 | } |
j3 | 0:074983020f27 | 130 | |
j3 | 0:074983020f27 | 131 | //********************************************************************* |
j3 | 1:0263f798de82 | 132 | MAX4822::CmdResult MAX4822::set_pwr_save(PowerSave pwr_save, bool send_data, uint8_t n) |
j3 | 0:074983020f27 | 133 | { |
j3 | 0:074983020f27 | 134 | MAX4822::CmdResult result = OpFailure; |
j3 | 0:074983020f27 | 135 | |
j3 | 1:0263f798de82 | 136 | if(n <= m_num_devices) |
j3 | 1:0263f798de82 | 137 | { |
j3 | 1:0263f798de82 | 138 | m_pwr_save_data[n] = pwr_save; |
j3 | 1:0263f798de82 | 139 | |
j3 | 1:0263f798de82 | 140 | if(send_data) |
j3 | 1:0263f798de82 | 141 | { |
j3 | 1:0263f798de82 | 142 | uint16_t num_writes = n + 1; |
j3 | 1:0263f798de82 | 143 | |
j3 | 1:0263f798de82 | 144 | m_cs = 0; |
j3 | 1:0263f798de82 | 145 | while(num_writes--) |
j3 | 1:0263f798de82 | 146 | { |
j3 | 1:0263f798de82 | 147 | m_spi.write(MAX4822::POWER_SAVE_REG); |
j3 | 1:0263f798de82 | 148 | m_spi.write(m_relay_data[num_writes]); |
j3 | 1:0263f798de82 | 149 | } |
j3 | 1:0263f798de82 | 150 | m_cs = 1; |
j3 | 1:0263f798de82 | 151 | } |
j3 | 1:0263f798de82 | 152 | |
j3 | 1:0263f798de82 | 153 | result = MAX4822::Success; |
j3 | 1:0263f798de82 | 154 | } |
j3 | 1:0263f798de82 | 155 | |
j3 | 0:074983020f27 | 156 | return result; |
j3 | 0:074983020f27 | 157 | } |