Implementation of 1-Wire with added Alarm Search Functionality
Dependents: Max32630_One_Wire_Interface
Diff: Masters/OneWireMaster.cpp
- Revision:
- 75:8b627804927c
- Parent:
- 74:23be10c32fa3
- Child:
- 76:84e6c4994e29
diff -r 23be10c32fa3 -r 8b627804927c Masters/OneWireMaster.cpp --- a/Masters/OneWireMaster.cpp Fri May 13 07:48:35 2016 -0500 +++ b/Masters/OneWireMaster.cpp Fri May 13 14:52:50 2016 -0500 @@ -35,26 +35,24 @@ using OneWire::Masters::OneWireMaster; -enum OW_ROM_CMD +enum OwRomCmd { - READ_ROM = 0x33, - MATCH_ROM = 0x55, - SEARCH_ROM = 0xF0, - SKIP_ROM = 0xCC, - RESUME = 0xA5, - OVERDRIVE_SKIP_ROM = 0x3C, - OVERDRIVE_MATCH_ROM = 0x69 + ReadRomCmd = 0x33, + MatchRomCmd = 0x55, + SearchRomCmd = 0xF0, + SkipRomCmd = 0xCC, + ResumeCmd = 0xA5, + OverdriveSkipRomCmd = 0x3C, + OverdriveMatchRomCmd = 0x69 }; - -//********************************************************************* -OneWireMaster::CmdResult OneWireMaster::OWWriteBlock(const uint8_t *tran_buf, uint8_t tran_len) +OneWireMaster::CmdResult OneWireMaster::OWWriteBlock(const uint8_t *sendBuf, uint8_t sendLen) { CmdResult result; - for (uint8_t idx = 0; idx < tran_len; idx++) + for (uint8_t idx = 0; idx < sendLen; idx++) { - result = OWWriteByte(tran_buf[idx]); + result = OWWriteByte(sendBuf[idx]); if (result != Success) { break; @@ -64,15 +62,13 @@ return result; } - -//********************************************************************* -OneWireMaster::CmdResult OneWireMaster::OWReadBlock(uint8_t *rx_buf, uint8_t rx_len) +OneWireMaster::CmdResult OneWireMaster::OWReadBlock(uint8_t *recvBuf, uint8_t recvLen) { CmdResult result; - for (uint8_t idx = 0; idx < rx_len; idx++) + for (uint8_t idx = 0; idx < recvLen; idx++) { - result = OWReadByte(rx_buf[idx]); + result = OWReadByte(recvBuf[idx]); if (result != Success) { break; @@ -85,22 +81,17 @@ OneWireMaster::CmdResult OneWireMaster::OWFirst(SearchState & searchState) { - // reset the search state + // Reset and begin a new search searchState.reset(); - return OWSearch(searchState); } - -//********************************************************************* OneWireMaster::CmdResult OneWireMaster::OWNext(SearchState & searchState) { - // leave the search state alone + // Continue the previous search return OWSearch(searchState); } - -//********************************************************************* OneWireMaster::CmdResult OneWireMaster::OWVerify(const RomId & romId) { OneWireMaster::CmdResult result; @@ -125,8 +116,6 @@ return result; } - -//********************************************************************* void OneWireMaster::OWTargetSetup(SearchState & searchState) { // set the search state to find SearchFamily type devices @@ -136,8 +125,6 @@ searchState.last_discrepancy = 64; } - -//********************************************************************* void OneWireMaster::OWFamilySkipSetup(SearchState & searchState) { // set the Last discrepancy to last family discrepancy @@ -153,52 +140,53 @@ } } - -//********************************************************************* -OneWireMaster::CmdResult OneWireMaster::OWReadROM(RomId & romId) +OneWireMaster::CmdResult OneWireMaster::OWReadRom(RomId & romId) { OneWireMaster::CmdResult result; - uint8_t buf[2 + RomId::byteLen]; + RomId readId; result = OWReset(); if (result == OneWireMaster::Success) { - result = OWWriteByte(READ_ROM); + result = OWWriteByte(ReadRomCmd); } // read the ROM if (result == OneWireMaster::Success) { - result = OWReadBlock(buf, RomId::byteLen); + result = OWReadBlock(readId, RomId::byteLen); } // verify CRC8 - if ((result == OneWireMaster::Success) && (RomId::calculateCRC8(buf, RomId::byteLen) == 0)) + if (result == OneWireMaster::Success) { - romId = RomId(reinterpret_cast<uint8_t(&)[RomId::byteLen]>(buf[0])); + if (readId.crc8Valid()) + { + romId = readId; + } + else + { + result = OneWireMaster::OperationFailure; + } } return result; } - - -//********************************************************************* -OneWireMaster::CmdResult OneWireMaster::OWSkipROM(void) + +OneWireMaster::CmdResult OneWireMaster::OWSkipRom() { OneWireMaster::CmdResult result; result = OWReset(); if (result == OneWireMaster::Success) { - result = OWWriteByte(SKIP_ROM); + result = OWWriteByte(SkipRomCmd); } return result; } - - -//********************************************************************* -OneWireMaster::CmdResult OneWireMaster::OWMatchROM(const RomId & romId) + +OneWireMaster::CmdResult OneWireMaster::OWMatchRom(const RomId & romId) { OneWireMaster::CmdResult result; @@ -208,7 +196,7 @@ result = OWReset(); if (result == OneWireMaster::Success) { - buf[0] = MATCH_ROM; + buf[0] = MatchRomCmd; std::memcpy(&buf[1], romId, RomId::byteLen); // send command and rom result = OWWriteBlock(buf, 1 + RomId::byteLen); @@ -216,12 +204,10 @@ return result; } - - -//********************************************************************* -OneWireMaster::CmdResult OneWireMaster::OWOverdriveSkipROM(void) + +OneWireMaster::CmdResult OneWireMaster::OWOverdriveSkipRom() { - OneWireMaster::CmdResult result = OWSetSpeed(SPEED_STANDARD); + OneWireMaster::CmdResult result = OWSetSpeed(StandardSpeed); if (result == OneWireMaster::Success) { @@ -230,56 +216,51 @@ if (result == OneWireMaster::Success) { - result = OWWriteByte(OVERDRIVE_SKIP_ROM); + result = OWWriteByte(OverdriveSkipRomCmd); } if (result == OneWireMaster::Success) { - result = OWSetSpeed(SPEED_OVERDRIVE); + result = OWSetSpeed(OverdriveSpeed); } return result; } - - -//********************************************************************* -OneWireMaster::CmdResult OneWireMaster::OWOverdriveMatchROM(const RomId & romId) + +OneWireMaster::CmdResult OneWireMaster::OWOverdriveMatchRom(const RomId & romId) { OneWireMaster::CmdResult result; // use overdrive MatchROM - OWSetSpeed(SPEED_STANDARD); + OWSetSpeed(StandardSpeed); result = OWReset(); if (result == OneWireMaster::Success) { - result = OWWriteByte(OVERDRIVE_MATCH_ROM); + result = OWWriteByte(OverdriveMatchRomCmd); if (result == OneWireMaster::Success) { - OWSetSpeed(SPEED_OVERDRIVE); + OWSetSpeed(OverdriveSpeed); // send ROM result = OWWriteBlock(romId, RomId::byteLen); } } return result; } - - -//********************************************************************* -OneWireMaster::CmdResult OneWireMaster::OWResume(void) + +OneWireMaster::CmdResult OneWireMaster::OWResume() { OneWireMaster::CmdResult result; result = OWReset(); if (result == OneWireMaster::Success) { - result = OWWriteByte(RESUME); + result = OWWriteByte(ResumeCmd); } return result; } - OneWireMaster::CmdResult OneWireMaster::OWSearch(SearchState & searchState) { uint8_t id_bit_number; @@ -310,7 +291,7 @@ } // issue the search command - OneWireMaster::OWWriteByte(SEARCH_ROM); + OneWireMaster::OWWriteByte(SearchRomCmd); // loop to do the search do @@ -321,11 +302,11 @@ { if ((searchState.romId[rom_byte_number] & rom_byte_mask) > 0) { - search_direction = DIRECTION_WRITE_ONE; + search_direction = WriteOne; } else { - search_direction = DIRECTION_WRITE_ZERO; + search_direction = WriteZero; } } else @@ -333,11 +314,11 @@ // if equal to last pick 1, if not then pick 0 if (id_bit_number == searchState.last_discrepancy) { - search_direction = DIRECTION_WRITE_ONE; + search_direction = WriteOne; } else { - search_direction = DIRECTION_WRITE_ZERO; + search_direction = WriteZero; } } @@ -355,7 +336,7 @@ } else { - if ((!id_bit) && (!cmp_id_bit) && (search_direction == DIRECTION_WRITE_ZERO)) + if ((!id_bit) && (!cmp_id_bit) && (search_direction == WriteZero)) { last_zero = id_bit_number; @@ -368,7 +349,7 @@ // set or clear the bit in the ROM byte rom_byte_number // with mask rom_byte_mask - if (search_direction == DIRECTION_WRITE_ONE) + if (search_direction == WriteOne) { searchState.romId[rom_byte_number] |= rom_byte_mask; } @@ -385,7 +366,7 @@ // if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask if (rom_byte_mask == 0) { - crc8 = RomId::calculateCRC8(crc8, searchState.romId[rom_byte_number]); // accumulate the CRC + crc8 = RomId::calculateCrc8(crc8, searchState.romId[rom_byte_number]); // accumulate the CRC rom_byte_number++; rom_byte_mask = 1; } @@ -418,8 +399,7 @@ return (search_result ? OneWireMaster::Success : OneWireMaster::OperationFailure); } - -OneWireMaster::CmdResult OneWireMaster::OWTriplet(SearchDirection & search_direction, uint8_t & sbr, uint8_t & tsb) +OneWireMaster::CmdResult OneWireMaster::OWTriplet(SearchDirection & searchDirection, uint8_t & sbr, uint8_t & tsb) { CmdResult result; result = OWReadBit(sbr); @@ -431,46 +411,44 @@ { if (sbr) { - search_direction = DIRECTION_WRITE_ONE; + searchDirection = WriteOne; } else if (tsb) { - search_direction = DIRECTION_WRITE_ZERO; + searchDirection = WriteZero; } // else: use search_direction parameter - result = OWWriteBit((search_direction == DIRECTION_WRITE_ONE) ? 1 : 0); + result = OWWriteBit((searchDirection == WriteOne) ? 1 : 0); } return result; } - -uint16_t OneWireMaster::calculateCRC16(uint16_t CRC16, uint16_t data) +uint16_t OneWireMaster::calculateCrc16(uint16_t crc16, uint16_t data) { - const uint16_t _oddparity[] = { 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0 }; + const uint16_t oddparity[] = { 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0 }; - data = (data ^ (CRC16 & 0xff)) & 0xff; - CRC16 >>= 8; + data = (data ^ (crc16 & 0xff)) & 0xff; + crc16 >>= 8; - if (_oddparity[data & 0xf] ^ _oddparity[data >> 4]) + if (oddparity[data & 0xf] ^ oddparity[data >> 4]) { - CRC16 ^= 0xc001; + crc16 ^= 0xc001; } data <<= 6; - CRC16 ^= data; + crc16 ^= data; data <<= 1; - CRC16 ^= data; + crc16 ^= data; - return CRC16; + return crc16; } - -uint16_t OneWireMaster::calculateCRC16(const uint8_t * data, size_t data_offset, size_t data_len, uint16_t crc) +uint16_t OneWireMaster::calculateCrc16(const uint8_t * data, size_t dataOffset, size_t dataLen, uint16_t crc) { - for (size_t i = data_offset; i < (data_len + data_offset); i++) + for (size_t i = dataOffset; i < (dataLen + dataOffset); i++) { - crc = calculateCRC16(crc, data[i]); + crc = calculateCrc16(crc, data[i]); } return crc; }