Library for interfacing with the MAX4822 relay driver.
Dependents: MAXREFDES130_131_Demo MAXREFDES130_Demo
Diff: MAX4822.cpp
- Revision:
- 1:0263f798de82
- Parent:
- 0:074983020f27
- Child:
- 2:7b30a3361e40
diff -r 074983020f27 -r 0263f798de82 MAX4822.cpp --- a/MAX4822.cpp Thu Jul 28 18:42:58 2016 +0000 +++ b/MAX4822.cpp Thu Jul 28 21:41:40 2016 +0000 @@ -33,9 +33,22 @@ #include "MAX4822.h" //********************************************************************* -MAX4822::MAX4822(SPI & spi_bus, PinName cs, PinName set, PinName reset): -m_spi(spi_bus), m_cs(cs), m_set(set, 1), m_reset(reset, 0), m_num_devices(1) +MAX4822::MAX4822(SPI & spi_bus, PinName cs, uint8_t num_devices): +m_spi(spi_bus), m_cs(cs, 1), m_num_devices(num_devices) { + if(m_num_devices) + { + for(uint8_t idx = 0; idx < m_num_devices; idx++) + { + m_relay_data[idx] = 0; + m_pwr_save_data[idx] = 0; + } + } + else + { + m_relay_data[0] = 0; + m_pwr_save_data[0] = 0; + } } //********************************************************************* @@ -44,13 +57,43 @@ } //********************************************************************* -MAX4822::CmdResult MAX4822::set_num_devices(uint8_t n) +void MAX4822::set_all_relays(DigitalOut & set) +{ + set = 0; + wait_us(1); + set = 1; +} + +//********************************************************************* +void MAX4822::reset_all_relays(DigitalOut & reset) +{ + reset = 0; + wait_us(1); + reset = 1; +} + +//********************************************************************* +MAX4822::CmdResult MAX4822::set_relay(RelayChannel r, bool send_data, uint8_t n) { MAX4822::CmdResult result = OpFailure; - if(n) + if(n <= m_num_devices) { - m_num_devices = n; + m_relay_data[n] |= (1 << (r - 1)); + + if(send_data) + { + uint16_t num_writes = n + 1; + + m_cs = 0; + while(num_writes--) + { + m_spi.write(MAX4822::OUTPUT_CNTL_REG); + m_spi.write(m_relay_data[num_writes]); + } + m_cs = 1; + } + result = MAX4822::Success; } @@ -58,33 +101,57 @@ } //********************************************************************* -MAX4822::CmdResult MAX4822::set_all_relays(uint8_t n) +MAX4822::CmdResult MAX4822::reset_relay(RelayChannel r, bool send_data, uint8_t n) { MAX4822::CmdResult result = OpFailure; + if(n <= m_num_devices) + { + m_relay_data[n] &= ~(1 << (r - 1)); + + if(send_data) + { + uint16_t num_writes = n + 1; + + m_cs = 0; + while(num_writes--) + { + m_spi.write(MAX4822::OUTPUT_CNTL_REG); + m_spi.write(m_relay_data[num_writes]); + } + m_cs = 1; + } + + result = MAX4822::Success; + } + return result; } //********************************************************************* -MAX4822::CmdResult MAX4822::reset_all_relays(uint8_t n) +MAX4822::CmdResult MAX4822::set_pwr_save(PowerSave pwr_save, bool send_data, uint8_t n) { MAX4822::CmdResult result = OpFailure; + if(n <= m_num_devices) + { + m_pwr_save_data[n] = pwr_save; + + if(send_data) + { + uint16_t num_writes = n + 1; + + m_cs = 0; + while(num_writes--) + { + m_spi.write(MAX4822::POWER_SAVE_REG); + m_spi.write(m_relay_data[num_writes]); + } + m_cs = 1; + } + + result = MAX4822::Success; + } + return result; } - -//********************************************************************* -MAX4822::CmdResult MAX4822::set_relay(RelayChannel r, uint8_t n) -{ - MAX4822::CmdResult result = OpFailure; - - return result; -} - -//********************************************************************* -MAX4822::CmdResult MAX4822::reset_relay(RelayChannel r, uint8_t n) -{ - MAX4822::CmdResult result = OpFailure; - - return result; -}