1-Wire® library for mbed. Complete 1-Wire library that supports our silicon masters along with a bit-bang master on the MAX32600MBED platform with one common interface for mbed. Slave support has also been included and more slaves will be added as time permits.
Dependents: MAXREFDES131_Qt_Demo MAX32630FTHR_iButton_uSD_Logger MAX32630FTHR_DS18B20_uSD_Logger MAXREFDES130_131_Demo ... more
Superseded by MaximInterface.
Diff: OneWire_Masters/DS248x/ds248x.cpp
- Revision:
- 23:e8e403d61359
- Parent:
- 21:00c94aeb533e
- Child:
- 26:a361e3f42ba5
--- a/OneWire_Masters/DS248x/ds248x.cpp Mon Mar 21 21:41:36 2016 +0000 +++ b/OneWire_Masters/DS248x/ds248x.cpp Mon Mar 21 23:18:45 2016 +0000 @@ -61,20 +61,20 @@ //********************************************************************* -OneWireInterface::CmdResult Ds248x::OWInitMaster(void) +OneWireMaster::CmdResult Ds248x::OWInitMaster(void) { return(detect()); } //********************************************************************* -OneWireInterface::CmdResult Ds248x::detect(void) +OneWireMaster::CmdResult Ds248x::detect(void) { - OneWireInterface::CmdResult result; + OneWireMaster::CmdResult result; // reset the ds2484 ON selected address result = reset(); - if(result == OneWireInterface::Success) + if(result == OneWireMaster::Success) { // default configuration _c1WS = 0; @@ -90,9 +90,9 @@ //********************************************************************* -OneWireInterface::CmdResult Ds248x::reset(void) +OneWireMaster::CmdResult Ds248x::reset(void) { - OneWireInterface::CmdResult result; + OneWireMaster::CmdResult result; char status; char packet[] = {CMD_DRST}; @@ -104,23 +104,23 @@ if(_p_i2c_bus->write(_w_adrs, packet, 1) != I2C_WRITE_OK) { - result = OneWireInterface::CommunicationWriteError; + result = OneWireMaster::CommunicationWriteError; } else { if(_p_i2c_bus->read(_r_adrs, &status, 1) != I2C_READ_OK) { - result = OneWireInterface::CommunicationReadError; + result = OneWireMaster::CommunicationReadError; } else { if((status & 0xF7) == 0x10) { - result = OneWireInterface::Success; + result = OneWireMaster::Success; } else { - result = OneWireInterface::OperationFailure; + result = OneWireMaster::OperationFailure; } } } @@ -130,22 +130,22 @@ //********************************************************************* -OneWireInterface::CmdResult Ds248x::write_config(uint8_t config) +OneWireMaster::CmdResult Ds248x::write_config(uint8_t config) { - OneWireInterface::CmdResult result; + OneWireMaster::CmdResult result; char read_config; char packet [] = {CMD_WCFG, (config | (~config << 4))}; if(_p_i2c_bus->write(_w_adrs, packet, 2) != I2C_WRITE_OK) { - result = OneWireInterface::CommunicationWriteError; + result = OneWireMaster::CommunicationWriteError; } else { if(_p_i2c_bus->read(_r_adrs, &read_config, 1) != I2C_READ_OK) { - result = OneWireInterface::CommunicationReadError; + result = OneWireMaster::CommunicationReadError; } else { @@ -153,11 +153,11 @@ if (config != read_config) { reset(); - result = OneWireInterface::OperationFailure; + result = OneWireMaster::OperationFailure; } else { - result = OneWireInterface::Success; + result = OneWireMaster::Success; } } } @@ -167,9 +167,9 @@ //********************************************************************* -OneWireInterface::CmdResult Ds248x::channel_select(uint8_t channel) +OneWireMaster::CmdResult Ds248x::channel_select(uint8_t channel) { - OneWireInterface::CmdResult result; + OneWireMaster::CmdResult result; char ch, ch_read, check; char packet [2]; @@ -198,24 +198,24 @@ if(_p_i2c_bus->write(_w_adrs, packet, 2) != I2C_WRITE_OK) { - result = OneWireInterface::CommunicationWriteError; + result = OneWireMaster::CommunicationWriteError; } else { if(_p_i2c_bus->read(_r_adrs, &check, 1) != I2C_READ_OK) { - result = OneWireInterface::CommunicationReadError; + result = OneWireMaster::CommunicationReadError; } else { // check for failure due to incorrect read back of channel if (check == ch_read) { - result = OneWireInterface::Success; + result = OneWireMaster::Success; } else { - result = OneWireInterface::OperationFailure; + result = OneWireMaster::OperationFailure; } } } @@ -225,9 +225,9 @@ //********************************************************************* -OneWireInterface::CmdResult Ds248x::adjust_timing(uint8_t param, uint8_t val) +OneWireMaster::CmdResult Ds248x::adjust_timing(uint8_t param, uint8_t val) { - OneWireInterface::CmdResult result; + OneWireMaster::CmdResult result; char read_port_config; char control_byte; @@ -238,25 +238,25 @@ if(_p_i2c_bus->write(_w_adrs, packet, 2) != I2C_WRITE_OK) { - result = OneWireInterface::CommunicationWriteError; + result = OneWireMaster::CommunicationWriteError; } else { if(_p_i2c_bus->read(_r_adrs, &read_port_config, 1) != I2C_READ_OK) { - result = OneWireInterface::CommunicationReadError; + result = OneWireMaster::CommunicationReadError; } else { // check for failure due to incorrect read back if ((control_byte & 0x0F) != read_port_config) { - result = OneWireInterface::OperationFailure; + result = OneWireMaster::OperationFailure; reset(); } else { - result = OneWireInterface::Success; + result = OneWireMaster::Success; } } } @@ -266,9 +266,9 @@ //********************************************************************* -OneWireInterface::CmdResult Ds248x::search_triplet(uint8_t search_direction, uint8_t & status) +OneWireMaster::CmdResult Ds248x::search_triplet(uint8_t search_direction, uint8_t & status) { - OneWireInterface::CmdResult result; + OneWireMaster::CmdResult result; uint8_t poll_count = 0; char packet [] = {CMD_1WT, search_direction ? 0x80 : 0x00}; @@ -283,7 +283,7 @@ if(_p_i2c_bus->write(_w_adrs, packet, 2) != I2C_WRITE_OK) { - result = OneWireInterface::CommunicationWriteError; + result = OneWireMaster::CommunicationWriteError; } else { @@ -291,17 +291,17 @@ // abort if poll limit reached //dummy write for loop - result = OneWireInterface::Success; //so far + result = OneWireMaster::Success; //so far do { if(_p_i2c_bus->read(_r_adrs, &read_data, 1) != I2C_READ_OK) { - result = OneWireInterface::CommunicationReadError; + result = OneWireMaster::CommunicationReadError; } } - while ((read_data & STATUS_1WB) && (poll_count++ < POLL_LIMIT) && (result != OneWireInterface::CommunicationReadError)); + while ((read_data & STATUS_1WB) && (poll_count++ < POLL_LIMIT) && (result != OneWireMaster::CommunicationReadError)); - if((result == OneWireInterface::CommunicationReadError) || (poll_count >= POLL_LIMIT)) + if((result == OneWireMaster::CommunicationReadError) || (poll_count >= POLL_LIMIT)) { // check for failure due to poll limit reached if(poll_count >= POLL_LIMIT) @@ -309,13 +309,13 @@ // handle error // ... reset(); - result = OneWireInterface::TimeoutError; + result = OneWireMaster::TimeoutError; } } else { status = read_data; - result = OneWireInterface::Success; + result = OneWireMaster::Success; } } @@ -324,9 +324,9 @@ //********************************************************************* -OneWireInterface::CmdResult Ds248x::OWReset(void) +OneWireMaster::CmdResult Ds248x::OWReset(void) { - OneWireInterface::CmdResult result; + OneWireMaster::CmdResult result; uint8_t poll_count = 0; char status; @@ -340,24 +340,24 @@ if(_p_i2c_bus->write(_w_adrs, packet, 1) != I2C_WRITE_OK) { - result = OneWireInterface::CommunicationWriteError; + result = OneWireMaster::CommunicationWriteError; } else { // loop checking 1WB bit for completion of 1-Wire operation // abort if poll limit reached //dummy write for loop - result = OneWireInterface::Success; //so far + result = OneWireMaster::Success; //so far do { if(_p_i2c_bus->read(_r_adrs, &status, 1) != I2C_READ_OK) { - result = OneWireInterface::CommunicationReadError; + result = OneWireMaster::CommunicationReadError; } } - while ((status & STATUS_1WB) && (poll_count++ < POLL_LIMIT) && (result != OneWireInterface::CommunicationReadError)); + while ((status & STATUS_1WB) && (poll_count++ < POLL_LIMIT) && (result != OneWireMaster::CommunicationReadError)); - if((result == OneWireInterface::CommunicationReadError) || (poll_count >= POLL_LIMIT)) + if((result == OneWireMaster::CommunicationReadError) || (poll_count >= POLL_LIMIT)) { // check for failure due to poll limit reached if(poll_count >= POLL_LIMIT) @@ -365,7 +365,7 @@ // handle error // ... reset(); - result = OneWireInterface::TimeoutError; + result = OneWireMaster::TimeoutError; } } else @@ -383,11 +383,11 @@ // check for presence detect if (status & STATUS_PPD) { - result = OneWireInterface::Success; + result = OneWireMaster::Success; } else { - result = OneWireInterface::OperationFailure; + result = OneWireMaster::OperationFailure; } } } @@ -397,9 +397,9 @@ //********************************************************************* -OneWireInterface::CmdResult Ds248x::OWTouchBit(uint8_t & sendrecvbit) +OneWireMaster::CmdResult Ds248x::OWTouchBit(uint8_t & sendrecvbit) { - OneWireInterface::CmdResult result; + OneWireMaster::CmdResult result; uint8_t poll_count = 0; char status; @@ -414,24 +414,24 @@ if(_p_i2c_bus->write(_w_adrs, packet, 2) != I2C_WRITE_OK) { - result = OneWireInterface::CommunicationWriteError; + result = OneWireMaster::CommunicationWriteError; } else { // loop checking 1WB bit for completion of 1-Wire operation // abort if poll limit reached //dummy write for loop - result = OneWireInterface::Success; //so far + result = OneWireMaster::Success; //so far do { if(_p_i2c_bus->read(_r_adrs, &status, 1) != I2C_READ_OK) { - result = OneWireInterface::CommunicationReadError; + result = OneWireMaster::CommunicationReadError; } } - while ((status & STATUS_1WB) && (poll_count++ < POLL_LIMIT) && (result != OneWireInterface::CommunicationReadError)); + while ((status & STATUS_1WB) && (poll_count++ < POLL_LIMIT) && (result != OneWireMaster::CommunicationReadError)); - if((result == OneWireInterface::CommunicationReadError) || (poll_count >= POLL_LIMIT)) + if((result == OneWireMaster::CommunicationReadError) || (poll_count >= POLL_LIMIT)) { // check for failure due to poll limit reached if (poll_count >= POLL_LIMIT) @@ -439,7 +439,7 @@ // handle error // ... reset(); - result = OneWireInterface::TimeoutError; + result = OneWireMaster::TimeoutError; } } else @@ -454,7 +454,7 @@ { sendrecvbit = 0; } - result = OneWireInterface::Success; + result = OneWireMaster::Success; } } @@ -463,9 +463,9 @@ //********************************************************************* -OneWireInterface::CmdResult Ds248x::OWWriteByte(uint8_t sendbyte) +OneWireMaster::CmdResult Ds248x::OWWriteByte(uint8_t sendbyte) { - OneWireInterface::CmdResult result; + OneWireMaster::CmdResult result; uint8_t poll_count = 0; char status; @@ -480,24 +480,24 @@ if(_p_i2c_bus->write(_w_adrs, packet, 2) != I2C_WRITE_OK) { - result = OneWireInterface::CommunicationWriteError; + result = OneWireMaster::CommunicationWriteError; } else { // loop checking 1WB bit for completion of 1-Wire operation // abort if poll limit reached //dummy write for loop - result = OneWireInterface::Success; //so far + result = OneWireMaster::Success; //so far do { if(_p_i2c_bus->read(_r_adrs, &status, 1) != I2C_READ_OK) { - result = OneWireInterface::CommunicationReadError; + result = OneWireMaster::CommunicationReadError; } } - while ((status & STATUS_1WB) && (poll_count++ < POLL_LIMIT) && (result != OneWireInterface::CommunicationReadError)); + while ((status & STATUS_1WB) && (poll_count++ < POLL_LIMIT) && (result != OneWireMaster::CommunicationReadError)); - if((result == OneWireInterface::CommunicationReadError) || (poll_count >= POLL_LIMIT)) + if((result == OneWireMaster::CommunicationReadError) || (poll_count >= POLL_LIMIT)) { // check for failure due to poll limit reached if (poll_count >= POLL_LIMIT) @@ -505,12 +505,12 @@ // handle error // ... reset(); - result = OneWireInterface::TimeoutError; + result = OneWireMaster::TimeoutError; } } else { - result = OneWireInterface::Success; + result = OneWireMaster::Success; } } @@ -519,9 +519,9 @@ //********************************************************************* -OneWireInterface::CmdResult Ds248x::OWReadByte(uint8_t & recvbyte) +OneWireMaster::CmdResult Ds248x::OWReadByte(uint8_t & recvbyte) { - OneWireInterface::CmdResult result; + OneWireMaster::CmdResult result; uint8_t poll_count = 0; char data, status; @@ -538,24 +538,24 @@ if(_p_i2c_bus->write(_w_adrs, packet, 1) != I2C_WRITE_OK) { - result = OneWireInterface::CommunicationWriteError; + result = OneWireMaster::CommunicationWriteError; } else { // loop checking 1WB bit for completion of 1-Wire operation // abort if poll limit reached //dummy write for loop - result = OneWireInterface::Success; //so far + result = OneWireMaster::Success; //so far do { if(_p_i2c_bus->read(_r_adrs, &status, 1) != I2C_READ_OK) { - result = OneWireInterface::CommunicationReadError; + result = OneWireMaster::CommunicationReadError; } } - while ((status & STATUS_1WB) && (poll_count++ < POLL_LIMIT) && (result != OneWireInterface::CommunicationReadError)); + while ((status & STATUS_1WB) && (poll_count++ < POLL_LIMIT) && (result != OneWireMaster::CommunicationReadError)); - if((result == OneWireInterface::CommunicationReadError) || (poll_count >= POLL_LIMIT)) + if((result == OneWireMaster::CommunicationReadError) || (poll_count >= POLL_LIMIT)) { // check for failure due to poll limit reached if (poll_count >= POLL_LIMIT) @@ -563,7 +563,7 @@ // handle error // ... reset(); - result = OneWireInterface::TimeoutError; + result = OneWireMaster::TimeoutError; } } else @@ -573,18 +573,18 @@ if(_p_i2c_bus->write(_w_adrs, packet, 2) != I2C_WRITE_OK) { - result = OneWireInterface::CommunicationWriteError; + result = OneWireMaster::CommunicationWriteError; } else { if(_p_i2c_bus->read(_r_adrs, &data, 1) != I2C_READ_OK) { - result = OneWireInterface::CommunicationReadError; + result = OneWireMaster::CommunicationReadError; } else { recvbyte = data; - result = OneWireInterface::Success; + result = OneWireMaster::Success; } } } @@ -595,14 +595,14 @@ //********************************************************************* -OneWireInterface::CmdResult Ds248x::OWWriteBlock(const uint8_t *tran_buf, uint8_t tran_len) +OneWireMaster::CmdResult Ds248x::OWWriteBlock(const uint8_t *tran_buf, uint8_t tran_len) { - OneWireInterface::CmdResult result; + OneWireMaster::CmdResult result; for(uint8_t idx = 0; idx < tran_len; idx++) { result = OWWriteByte(tran_buf[idx]); - if(result != OneWireInterface::Success) + if(result != OneWireMaster::Success) { break; } @@ -613,15 +613,15 @@ //********************************************************************* -OneWireInterface::CmdResult Ds248x::OWReadBlock(uint8_t *rx_buf, uint8_t rx_len) +OneWireMaster::CmdResult Ds248x::OWReadBlock(uint8_t *rx_buf, uint8_t rx_len) { - OneWireInterface::CmdResult result; + OneWireMaster::CmdResult result; for(uint8_t idx = 0; idx < rx_len; idx++) { //OwReadByte() uses pass by reference result = OWReadByte(rx_buf[idx]); - if(result != OneWireInterface::Success) + if(result != OneWireMaster::Success) { break; } @@ -632,7 +632,7 @@ //********************************************************************* -OneWireInterface::CmdResult Ds248x::OWSearch(RomId & romId) +OneWireMaster::CmdResult Ds248x::OWSearch(RomId & romId) { int id_bit_number; int last_zero, rom_byte_number; @@ -653,8 +653,8 @@ if (!_last_device_flag) { // 1-Wire reset - OneWireInterface::CmdResult result = OWReset(); - if (result != OneWireInterface::Success) + OneWireMaster::CmdResult result = OWReset(); + if (result != OneWireMaster::Success) { // reset the search _last_discrepancy = 0; @@ -755,12 +755,12 @@ search_result = false; } - return search_result ? OneWireInterface::Success : OneWireInterface::OperationFailure; + return search_result ? OneWireMaster::Success : OneWireMaster::OperationFailure; } //********************************************************************* -OneWireInterface::CmdResult Ds248x::OWSpeed(OW_SPEED new_speed) +OneWireMaster::CmdResult Ds248x::OWSpeed(OW_SPEED new_speed) { // set the speed if (new_speed == SPEED_OVERDRIVE) @@ -778,14 +778,14 @@ //********************************************************************* -OneWireInterface::CmdResult Ds248x::OWLevel(OW_LEVEL new_level) +OneWireMaster::CmdResult Ds248x::OWLevel(OW_LEVEL new_level) { - OneWireInterface::CmdResult result; + OneWireMaster::CmdResult result; // function only will turn back to non-strong pull-up if (new_level != LEVEL_NORMAL) { - result = OneWireInterface::OperationFailure; + result = OneWireMaster::OperationFailure; } else { @@ -800,16 +800,16 @@ //********************************************************************* -OneWireInterface::CmdResult Ds248x::OWWriteBytePower(uint8_t sendbyte) +OneWireMaster::CmdResult Ds248x::OWWriteBytePower(uint8_t sendbyte) { - OneWireInterface::CmdResult result; + OneWireMaster::CmdResult result; // set strong pull-up enable _cSPU = CONFIG_SPU; // write the new config result = write_config(_c1WS | _cSPU | _cPDN | _cAPU); - if (result == OneWireInterface::Success) + if (result == OneWireMaster::Success) { // perform write byte result = OWWriteByte(sendbyte); @@ -820,9 +820,9 @@ //********************************************************************* -OneWireInterface::CmdResult Ds248x::OWReadBitPower(uint8_t applyPowerResponse) +OneWireMaster::CmdResult Ds248x::OWReadBitPower(uint8_t applyPowerResponse) { - OneWireInterface::CmdResult result; + OneWireMaster::CmdResult result; uint8_t rdbit; // set strong pull-up enable @@ -830,17 +830,17 @@ // write the new config result = write_config(_c1WS | _cSPU | _cPDN | _cAPU); - if (result == OneWireInterface::Success) + if (result == OneWireMaster::Success) { // perform read bit result = OWReadBit(rdbit); - if(result == OneWireInterface::Success) + if(result == OneWireMaster::Success) { // check if response was correct, if not then turn off strong pull-up if (rdbit != applyPowerResponse) { OWLevel(LEVEL_NORMAL); - result = OneWireInterface::OperationFailure; + result = OneWireMaster::OperationFailure; } } } @@ -849,9 +849,9 @@ } -OneWireInterface::CmdResult Ds248x::OWReadBytePower(uint8_t & recvbyte) +OneWireMaster::CmdResult Ds248x::OWReadBytePower(uint8_t & recvbyte) { - OneWireInterface::CmdResult result = Success; + OneWireMaster::CmdResult result = Success; uint8_t recvbit; recvbyte = 0;