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: Slaves/Memory/DS2431/DS2431.cpp
- Revision:
- 116:8058bb54e959
- Parent:
- 115:a1ca2f3bf46d
- Child:
- 121:4bee07064d0d
diff -r a1ca2f3bf46d -r 8058bb54e959 Slaves/Memory/DS2431/DS2431.cpp --- a/Slaves/Memory/DS2431/DS2431.cpp Mon Aug 22 05:38:20 2016 +0000 +++ b/Slaves/Memory/DS2431/DS2431.cpp Mon Aug 22 21:35:47 2016 +0000 @@ -50,7 +50,124 @@ } //********************************************************************* -OneWireSlave::CmdResult DS2431::writeScratchPad(uint16_t targetAddress, uint8_t *data) +OneWireSlave::CmdResult DS2431::writeMemory(uint16_t targetAddress, const uint8_t *data, uint8_t numBytes) +{ + OneWireSlave::CmdResult result = OneWireSlave::OperationFailure; + + if((targetAddress + numBytes) <= 0x88) + { + result = OneWireSlave::Success; + + uint8_t startOffset = (targetAddress & 0x0007); + uint16_t startRowAddress = (targetAddress & 0xFFF8); + uint8_t endOffset = ((targetAddress + numBytes) & 0x0007); + uint16_t endRowAddress = ((targetAddress + numBytes) & 0xFFF8); + const uint8_t *dataIdx = data; + + DS2431::Scratchpad scratchpadData; + uint8_t esByte; + + if(startOffset != 0) + { + result = this->readMemory(startRowAddress, scratchpadData, 8); + if(result == OneWireSlave::Success) + { + std::memcpy((scratchpadData + startOffset), data, (8 - startOffset)); + result = this->writeScratchpad(startRowAddress, scratchpadData); + if(result == OneWireSlave::Success) + { + result = this->readScratchpad(scratchpadData , esByte); + if(result == OneWireSlave::Success) + { + result = this->copyScratchpad(startRowAddress, esByte); + startRowAddress += 8; + dataIdx = (data + (8 - startOffset)); + } + } + } + } + + if(result == OneWireSlave::Success) + { + for(uint16_t row = startRowAddress; row < endRowAddress; row += 8) + { + std::memcpy(scratchpadData, dataIdx, 8); + + result = this->writeScratchpad(row, scratchpadData); + if(result != OneWireSlave::Success) + { + break; + } + + result = this->readScratchpad(scratchpadData, esByte); + if(result != OneWireSlave::Success) + { + break; + } + + result = this->copyScratchpad(row, esByte); + if(result != OneWireSlave::Success) + { + break; + } + + dataIdx += 8; + } + } + + if(result == OneWireSlave::Success) + { + if(endOffset != 0) + { + result = this->readMemory(endRowAddress, scratchpadData, 8); + if(result == OneWireSlave::Success) + { + std::memcpy(scratchpadData, dataIdx, endOffset); + result = this->writeScratchpad(endRowAddress, scratchpadData); + if(result == OneWireSlave::Success) + { + result = this->readScratchpad(scratchpadData, esByte); + if(result == OneWireSlave::Success) + { + result = this->copyScratchpad(endRowAddress, esByte); + } + } + } + } + } + } + + return result; +} + +//********************************************************************* +OneWireSlave::CmdResult DS2431::readMemory(uint16_t targetAddress, uint8_t *data, uint8_t numBytes) +{ + OneWireSlave::CmdResult result = OneWireSlave::OperationFailure; + + if((targetAddress + numBytes) <= 0x88) + { + OneWireMaster::CmdResult owmResult = selectDevice(); + if(owmResult == OneWireMaster::Success) + { + uint8_t sendBlock[] = {READ_MEMORY, (targetAddress & 0xFF), ((targetAddress >> 8) & 0xFF)}; + owmResult = master().OWWriteBlock(sendBlock, 3); + if(owmResult == OneWireMaster::Success) + { + owmResult = master().OWReadBlock(data, numBytes); + if(owmResult == OneWireMaster::Success) + { + result = OneWireSlave::Success; + } + } + } + } + + return result; +} + +//********************************************************************* +OneWireSlave::CmdResult DS2431::writeScratchpad(uint16_t targetAddress, const Scratchpad &data) { OneWireSlave::CmdResult result = OneWireSlave::OperationFailure; @@ -85,7 +202,7 @@ } //********************************************************************* -OneWireSlave::CmdResult DS2431::readScratchPad(uint8_t *data) +OneWireSlave::CmdResult DS2431::readScratchpad(Scratchpad &data, uint8_t &esByte) { OneWireSlave::CmdResult result = OneWireSlave::OperationFailure; @@ -112,7 +229,8 @@ if(invCRC16 == calculatedInvCRC16) { - std::memcpy(data, (recvBlock + 1), 11); + esByte = recvBlock[3]; + std::memcpy(data, (recvBlock + 4), 8); result = OneWireSlave::Success; } } @@ -122,7 +240,7 @@ } //********************************************************************* -OneWireSlave::CmdResult DS2431::copyScratchPad(uint16_t targetAddress, uint8_t esByte) +OneWireSlave::CmdResult DS2431::copyScratchpad(uint16_t targetAddress, uint8_t esByte) { OneWireSlave::CmdResult result = OneWireSlave::OperationFailure; @@ -146,119 +264,3 @@ return result; } - -//********************************************************************* -OneWireSlave::CmdResult DS2431::writeBlock(uint16_t targetAddress, uint8_t *data, uint8_t numBytes) -{ - OneWireSlave::CmdResult result = OneWireSlave::OperationFailure; - - if((targetAddress + numBytes) <= 0x88) - { - result = OneWireSlave::Success; - - uint8_t startOffset = (targetAddress & 0x0007); - uint16_t startRowAddress = (targetAddress & 0xFFF8); - uint8_t endOffset = ((targetAddress + numBytes) & 0x0007); - uint16_t endRowAddress = ((targetAddress + numBytes) & 0xFFF8); - uint8_t *dataIdx = data; - - uint8_t localData[12]; - - if(startOffset != 0) - { - result = this->readBlock(startRowAddress, localData, 8); - if(result == OneWireSlave::Success) - { - std::memcpy((localData + startOffset), data, (8 - startOffset)); - result = this->writeScratchPad(startRowAddress, localData); - if(result == OneWireSlave::Success) - { - result = this->readScratchPad(localData); - if(result == OneWireSlave::Success) - { - result = this->copyScratchPad(startRowAddress, localData[2]); - startRowAddress += 8; - dataIdx = (data + (8 - startOffset)); - } - } - } - } - - if(result == OneWireSlave::Success) - { - for(uint16_t row = startRowAddress; row < endRowAddress; row += 8) - { - std::memcpy(localData, dataIdx, 8); - - result = this->writeScratchPad(row, localData); - if(result != OneWireSlave::Success) - { - break; - } - - result = this->readScratchPad(localData); - if(result != OneWireSlave::Success) - { - break; - } - - result = this->copyScratchPad(row, localData[2]); - if(result != OneWireSlave::Success) - { - break; - } - - dataIdx += 8; - } - } - - if(result == OneWireSlave::Success) - { - if(endOffset != 0) - { - result = this->readBlock(endRowAddress, localData, 8); - if(result == OneWireSlave::Success) - { - std::memcpy(localData, dataIdx, endOffset); - result = this->writeScratchPad(endRowAddress, localData); - if(result == OneWireSlave::Success) - { - result = this->readScratchPad(localData); - if(result == OneWireSlave::Success) - { - result = this->copyScratchPad(endRowAddress, localData[2]); - } - } - } - } - } - } - - return result; -} - -//********************************************************************* -OneWireSlave::CmdResult DS2431::readBlock(uint16_t targetAddress, uint8_t *data, uint8_t numBytes) -{ - OneWireSlave::CmdResult result = OneWireSlave::OperationFailure; - - if((targetAddress + numBytes) <= 0x88) - { - OneWireMaster::CmdResult owmResult = selectDevice(); - if(owmResult == OneWireMaster::Success) - { - uint8_t sendBlock[] = {READ_MEMORY, (targetAddress & 0xFF), ((targetAddress >> 8) & 0xFF)}; - owmResult = master().OWWriteBlock(sendBlock, 3); - if(owmResult == OneWireMaster::Success) - { - owmResult = master().OWReadBlock(data, numBytes); - if(owmResult == OneWireMaster::Success) - { - result = OneWireSlave::Success; - } - } - } - } - - return result; -}