Implementation of 1-Wire with added Alarm Search Functionality
Dependents: Max32630_One_Wire_Interface
Diff: Masters/DS248x/DS248x.cpp
- Revision:
- 74:23be10c32fa3
- Parent:
- 73:2cecc1372acc
- Child:
- 75:8b627804927c
diff -r 2cecc1372acc -r 23be10c32fa3 Masters/DS248x/DS248x.cpp --- a/Masters/DS248x/DS248x.cpp Thu May 12 14:38:16 2016 -0500 +++ b/Masters/DS248x/DS248x.cpp Fri May 13 07:48:35 2016 -0500 @@ -38,7 +38,7 @@ enum DS248X_STATUS_BITS { - STATUS_1WB = 0x01, + STATUS_1WB = 0x01, STATUS_PPD = 0x02, STATUS_SD = 0x04, STATUS_LL = 0x08, @@ -53,47 +53,55 @@ uint8_t DS248x::Config::readByte() const { - uint8_t config = 0; - if (get1WS()) - config |= 0x08; - if (getSPU()) - config |= 0x04; - if (getPDN()) - config |= 0x02; - if (getAPU()) - config |= 0x01; - return config; + uint8_t config = 0; + if (get1WS()) + { + config |= 0x08; + } + if (getSPU()) + { + config |= 0x04; + } + if (getPDN()) + { + config |= 0x02; + } + if (getAPU()) + { + config |= 0x01; + } + return config; } uint8_t DS248x::Config::writeByte() const { - uint8_t config = readByte(); - return ((~config << 4) | config); + uint8_t config = readByte(); + return ((~config << 4) | config); } void DS248x::Config::reset() { - set1WS(false); - setSPU(false); - setPDN(false); - setAPU(true); + set1WS(false); + setSPU(false); + setPDN(false); + setAPU(true); } DS248x::DS248x(mbed::I2C & i2c_bus, uint8_t adrs) -:_p_i2c_bus(&i2c_bus), _adrs(adrs), _i2c_owner(false) + :_p_i2c_bus(&i2c_bus), _adrs(adrs), _i2c_owner(false) { } DS248x::DS248x(PinName sda, PinName scl, uint8_t adrs) -:_p_i2c_bus(new mbed::I2C(sda, scl)), _adrs(adrs), _i2c_owner(true) + : _p_i2c_bus(new mbed::I2C(sda, scl)), _adrs(adrs), _i2c_owner(true) { } DS248x::~DS248x() { - if(_i2c_owner) + if (_i2c_owner) { delete _p_i2c_bus; } @@ -106,7 +114,9 @@ // reset DS2465 result = reset(); if (result != OneWireMaster::Success) - return result; + { + return result; + } // write the default configuration setup Config defaultConfig; @@ -115,55 +125,86 @@ } OneWireMaster::CmdResult DS248x::reset(void) -{ +{ // Device Reset // S AD,0 [A] DRST [A] Sr AD,1 [A] [SS] A\ P // [] indicates from slave // SS status byte to read to verify state - - OneWireMaster::CmdResult result; - uint8_t buf; - - result = sendCommand(CMD_DRST); - - if (result == OneWireMaster::Success) - result = readRegister(StatusReg, buf, true); + + OneWireMaster::CmdResult result; + uint8_t buf; + + result = sendCommand(CMD_DRST); + + if (result == OneWireMaster::Success) + { + result = readRegister(StatusReg, buf, true); + } - if (result == OneWireMaster::Success) - { - if ((buf & 0xF7) != 0x10) - result = OneWireMaster::OperationFailure; - } - - if (result == OneWireMaster::Success) - OWReset(); // do a command to get 1-Wire master reset out of holding state + if (result == OneWireMaster::Success) + { + if ((buf & 0xF7) != 0x10) + { + result = OneWireMaster::OperationFailure; + } + } - return result; + if (result == OneWireMaster::Success) + { + OWReset(); // do a command to get 1-Wire master reset out of holding state + } + + return result; } OneWireMaster::CmdResult DS248x::selectChannel(uint8_t channel) { OneWireMaster::CmdResult result; uint8_t ch, ch_read; - + // Channel Select (Case A) // S AD,0 [A] CHSL [A] CC [A] Sr AD,1 [A] [RR] A\ P // [] indicates from slave // CC channel value // RR channel read back - + switch (channel) { - default: case 0: ch = 0xF0; ch_read = 0xB8; break; - case 1: ch = 0xE1; ch_read = 0xB1; break; - case 2: ch = 0xD2; ch_read = 0xAA; break; - case 3: ch = 0xC3; ch_read = 0xA3; break; - case 4: ch = 0xB4; ch_read = 0x9C; break; - case 5: ch = 0xA5; ch_read = 0x95; break; - case 6: ch = 0x96; ch_read = 0x8E; break; - case 7: ch = 0x87; ch_read = 0x87; break; + default: + case 0: + ch = 0xF0; + ch_read = 0xB8; + break; + case 1: + ch = 0xE1; + ch_read = 0xB1; + break; + case 2: + ch = 0xD2; + ch_read = 0xAA; + break; + case 3: + ch = 0xC3; + ch_read = 0xA3; + break; + case 4: + ch = 0xB4; + ch_read = 0x9C; + break; + case 5: + ch = 0xA5; + ch_read = 0x95; + break; + case 6: + ch = 0x96; + ch_read = 0x8E; + break; + case 7: + ch = 0x87; + ch_read = 0x87; + break; }; - + result = sendCommand(CMD_CHSL, ch); if (result == OneWireMaster::Success) { @@ -177,30 +218,36 @@ } } } - + return result; } OneWireMaster::CmdResult DS248x::adjustOwPort(OwAdjustParam param, uint8_t val) { OneWireMaster::CmdResult result; - + uint8_t read_port_config; uint8_t control_byte; control_byte = (((param & 0x0F) << 4) | (val & 0x0F)); - + result = sendCommand(CMD_A1WP, control_byte); if (result != Success) + { return result; - + } + result = readRegister(PortConfigReg, read_port_config, true); if (result != Success) + { return result; - - if ((control_byte & 0x0F) != read_port_config) + } + + if ((control_byte & 0x0F) != read_port_config) + { result = OneWireMaster::OperationFailure; - + } + return result; } @@ -213,21 +260,21 @@ // [] indicates from slave // SS indicates byte containing search direction bit value in msbit - OneWireMaster::CmdResult result; - result = sendCommand(CMD_1WT, (uint8_t)((search_direction == DIRECTION_WRITE_ONE) ? 0x80 : 0x00)); - if (result == OneWireMaster::Success) - { - uint8_t status; - result = pollBusy(&status); + OneWireMaster::CmdResult result; + result = sendCommand(CMD_1WT, (uint8_t)((search_direction == DIRECTION_WRITE_ONE) ? 0x80 : 0x00)); if (result == OneWireMaster::Success) { - // check bit results in status byte - sbr = ((status & STATUS_SBR) == STATUS_SBR); - tsb = ((status & STATUS_TSB) == STATUS_TSB); - search_direction = ((status & STATUS_DIR) == STATUS_DIR) ? DIRECTION_WRITE_ONE : DIRECTION_WRITE_ZERO; + uint8_t status; + result = pollBusy(&status); + if (result == OneWireMaster::Success) + { + // check bit results in status byte + sbr = ((status & STATUS_SBR) == STATUS_SBR); + tsb = ((status & STATUS_TSB) == STATUS_TSB); + search_direction = ((status & STATUS_DIR) == STATUS_DIR) ? DIRECTION_WRITE_ONE : DIRECTION_WRITE_ZERO; + } } - } - return result; + return result; } OneWireMaster::CmdResult DS248x::OWReset(void) @@ -238,22 +285,26 @@ // Repeat until 1WB bit has changed to 0 // [] indicates from slave - OneWireMaster::CmdResult result; - uint8_t buf; - - result = sendCommand(CMD_1WRS); + OneWireMaster::CmdResult result; + uint8_t buf; + + result = sendCommand(CMD_1WRS); + + if (result == OneWireMaster::Success) + { + result = pollBusy(&buf); + } - if (result == OneWireMaster::Success) - result = pollBusy(&buf); - - if (result == OneWireMaster::Success) - { - // check for presence detect - if ((buf & STATUS_PPD) != STATUS_PPD) - result = OneWireMaster::OperationFailure; - } + if (result == OneWireMaster::Success) + { + // check for presence detect + if ((buf & STATUS_PPD) != STATUS_PPD) + { + result = OneWireMaster::OperationFailure; + } + } - return result; + return result; } OneWireMaster::CmdResult DS248x::OWTouchBitSetLevel(uint8_t & sendrecvbit, OWLevel after_level) @@ -265,23 +316,29 @@ // [] indicates from slave // BB indicates byte containing bit value in msbit - OneWireMaster::CmdResult result; - - result = configureLevel(after_level); - if (result != OneWireMaster::Success) + OneWireMaster::CmdResult result; + + result = configureLevel(after_level); + if (result != OneWireMaster::Success) + { + return result; + } + + uint8_t status; + + result = sendCommand(CMD_1WSB, (uint8_t)(sendrecvbit ? 0x80 : 0x00)); + + if (result == OneWireMaster::Success) + { + result = pollBusy(&status); + } + + if (result == OneWireMaster::Success) + { + sendrecvbit = (status & STATUS_SBR); + } + return result; - - uint8_t status; - - result = sendCommand(CMD_1WSB, (uint8_t)(sendrecvbit ? 0x80 : 0x00)); - - if (result == OneWireMaster::Success) - result = pollBusy(&status); - - if (result == OneWireMaster::Success) - sendrecvbit = (status & STATUS_SBR); - - return result; } OneWireMaster::CmdResult DS248x::OWWriteByteSetLevel(uint8_t sendbyte, OWLevel after_level) @@ -293,17 +350,21 @@ // [] indicates from slave // DD data to write - OneWireMaster::CmdResult result; - - result = configureLevel(after_level); - if (result != OneWireMaster::Success) + OneWireMaster::CmdResult result; + + result = configureLevel(after_level); + if (result != OneWireMaster::Success) + { + return result; + } + + result = sendCommand(CMD_1WWB, sendbyte); + if (result == OneWireMaster::Success) + { + result = pollBusy(); + } + return result; - - result = sendCommand(CMD_1WWB, sendbyte); - if (result == OneWireMaster::Success) - result = pollBusy(); - - return result; } OneWireMaster::CmdResult DS248x::OWReadByteSetLevel(uint8_t & recvbyte, OWLevel after_level) @@ -317,39 +378,49 @@ // [] indicates from slave // DD data read - OneWireMaster::CmdResult result; - uint8_t buf; - - result = configureLevel(after_level); - if (result != OneWireMaster::Success) - return result; - - result = sendCommand(CMD_1WRB); + OneWireMaster::CmdResult result; + uint8_t buf; + + result = configureLevel(after_level); + if (result != OneWireMaster::Success) + { + return result; + } + + result = sendCommand(CMD_1WRB); - if (result == OneWireMaster::Success) - result = pollBusy(); + if (result == OneWireMaster::Success) + { + result = pollBusy(); + } - if (result == OneWireMaster::Success) - result = readRegister(ReadDataReg, buf); - - if (result == OneWireMaster::Success) - recvbyte = buf; - - return result; + if (result == OneWireMaster::Success) + { + result = readRegister(ReadDataReg, buf); + } + + if (result == OneWireMaster::Success) + { + recvbyte = buf; + } + + return result; } OneWireMaster::CmdResult DS248x::OWSetSpeed(OWSpeed new_speed) { - // Requested speed is already set - if (m_curConfig.get1WS() == (new_speed == SPEED_OVERDRIVE)) - return OneWireMaster::Success; - - // set the speed - Config newConfig = m_curConfig; - newConfig.set1WS(new_speed == SPEED_OVERDRIVE); + // Requested speed is already set + if (m_curConfig.get1WS() == (new_speed == SPEED_OVERDRIVE)) + { + return OneWireMaster::Success; + } - // write the new config - return writeConfig(newConfig, true); + // set the speed + Config newConfig = m_curConfig; + newConfig.set1WS(new_speed == SPEED_OVERDRIVE); + + // write the new config + return writeConfig(newConfig, true); } OneWireMaster::CmdResult DS248x::OWSetLevel(OWLevel new_level) @@ -357,35 +428,39 @@ if (new_level == LEVEL_STRONG) { return OneWireMaster::OperationFailure; - } - + } + return configureLevel(new_level); } OneWireMaster::CmdResult DS248x::writeConfig(const Config & config, bool verify) { - uint8_t configBuf; - OneWireMaster::CmdResult result; - - configBuf = config.writeByte(); - result = sendCommand(CMD_WCFG, configBuf); - if (verify) - { + uint8_t configBuf; + OneWireMaster::CmdResult result; + + configBuf = config.writeByte(); + result = sendCommand(CMD_WCFG, configBuf); + if (verify) + { + if (result == OneWireMaster::Success) + { + result = readRegister(ConfigReg, configBuf); + } + if (result == OneWireMaster::Success) + { + if (configBuf != config.readByte()) + { + result = OneWireMaster::OperationFailure; + } + } + } + if (result == OneWireMaster::Success) { - result = readRegister(ConfigReg, configBuf); - } - if (result == OneWireMaster::Success) - { - if (configBuf != config.readByte()) - result = OneWireMaster::OperationFailure; + m_curConfig = config; } - } - - if (result == OneWireMaster::Success) - m_curConfig = config; - return result; + return result; } OneWireMaster::CmdResult DS248x::readRegister(Register reg, uint8_t & buf, bool skipSetPointer) const @@ -397,31 +472,37 @@ if (_p_i2c_bus->read(_adrs, reinterpret_cast<char *>(&buf), 1) != I2C_READ_OK) { result = CommunicationReadError; - } + } } return result; } OneWireMaster::CmdResult DS248x::pollBusy(uint8_t * pStatus) { - const unsigned int pollLimit = 200; + const unsigned int pollLimit = 200; + + OneWireMaster::CmdResult result; + uint8_t status; + unsigned int pollCount = 0; - OneWireMaster::CmdResult result; - uint8_t status; - unsigned int pollCount = 0; - - do - { - result = readRegister(StatusReg, status, true); - if (result != OneWireMaster::Success) - return result; - if (pStatus != NULL) - *pStatus = status; - if (pollCount++ >= pollLimit) - return OneWireMaster::TimeoutError; - } while (status & STATUS_1WB); + do + { + result = readRegister(StatusReg, status, true); + if (result != OneWireMaster::Success) + { + return result; + } + if (pStatus != NULL) + { + *pStatus = status; + } + if (pollCount++ >= pollLimit) + { + return OneWireMaster::TimeoutError; + } + } while (status & STATUS_1WB); - return OneWireMaster::Success; + return OneWireMaster::Success; } OneWireMaster::CmdResult DS248x::configureLevel(OWLevel level) @@ -446,11 +527,11 @@ if (_p_i2c_bus->write(_adrs, reinterpret_cast<const char *>(&cmd), 1) == I2C_WRITE_OK) { result = Success; - } + } else { result = CommunicationWriteError; - } + } return result; } @@ -461,10 +542,10 @@ if (_p_i2c_bus->write(_adrs, buf, 2) == I2C_WRITE_OK) { result = Success; - } + } else { result = CommunicationWriteError; - } + } return result; }