Implementation of 1-Wire with added Alarm Search Functionality
Dependents: Max32630_One_Wire_Interface
OneWire_Masters/OneWireMaster.cpp
- Committer:
- j3
- Date:
- 2016-03-16
- Revision:
- 15:f6cb0d906fb6
- Parent:
- OneWire_Masters/OneWireMastersShared.cpp@ 5:ce108eeb878d
- Child:
- 17:b646b1e3970b
File content as of revision 15:f6cb0d906fb6:
/******************************************************************//** * @file OneWireMaster.cpp * * @author Justin Jordan * * @version 0.0.0 * * Started: 08FEB16 * * Updated: * * @brief Header file for functions shared between masters, that should * be implemented only once. *********************************************************************** * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * * Except as contained in this notice, the name of Maxim Integrated * Products, Inc. shall not be used except as stated in the Maxim Integrated * Products, Inc. Branding Policy. * * The mere transfer of this software does not imply any licenses * of trade secrets, proprietary technology, copyrights, patents, * trademarks, maskwork rights, or any other form of intellectual * property whatsoever. Maxim Integrated Products, Inc. retains all * ownership rights. **********************************************************************/ #include "OneWireMaster.h" //********************************************************************* void OneWireMaster::OWWriteBit(uint8_t sendbit) { OWTouchBit(sendbit); } //********************************************************************* uint8_t OneWireMaster::OWReadBit() { return(OWTouchBit(0x01)); } //********************************************************************* uint8_t OneWireMaster::OWTouchByte(uint8_t sendbyte) { uint8_t rtn_val; if (sendbyte == 0xFF) { rtn_val = OWReadByte(); } else { OWWriteByte(sendbyte); rtn_val = sendbyte; } return(rtn_val); } //********************************************************************* void OneWireMaster::OWBlock(uint8_t *tran_buf, uint8_t tran_len) { uint8_t i; for (i = 0; i < tran_len; i++) { tran_buf[i] = OWTouchByte(tran_buf[i]); } } //********************************************************************* void OneWireMaster::OWWriteBlock(const uint8_t *tran_buf, uint8_t tran_len) { uint8_t idx; for(idx = 0; idx < tran_len; idx++) { OWWriteByte(tran_buf[idx]); } } //********************************************************************* void OneWireMaster::OWReadBlock(uint8_t *recv_buf, uint8_t recv_len) { uint8_t idx; for(idx = 0; idx < recv_len; idx++) { recv_buf[idx] = OWReadByte(); } } //********************************************************************* bool OneWireMaster::OWFirst(void) { // reset the search state _last_discrepancy = 0; _last_device_flag = false; _last_family_discrepancy = 0; return OWSearch(); } //********************************************************************* bool OneWireMaster::OWNext(void) { // leave the search state alone return OWSearch(); } //********************************************************************* bool OneWireMaster::OWVerify(void) { bool rtn_val = false; uint8_t rom_backup[8]; uint8_t i,rslt,ld_backup,ldf_backup,lfd_backup; // keep a backup copy of the current state for (i = 0; i < 8; i++) { rom_backup[i] = _rom_number[i]; } ld_backup = _last_discrepancy; ldf_backup = _last_device_flag; lfd_backup = _last_family_discrepancy; // set search to find the same device _last_discrepancy = 64; _last_device_flag = false; if (OWSearch()) { // check if same device found rslt = true; for (i = 0; i < 8; i++) { if (rom_backup[i] != _rom_number[i]) { rslt = false; break; } } } else { rslt = false; } // restore the search state for (i = 0; i < 8; i++) { _rom_number[i] = rom_backup[i]; } _last_discrepancy = ld_backup; _last_device_flag = ldf_backup; _last_family_discrepancy = lfd_backup; // return the result of the verify rtn_val = rslt; return(rtn_val); } //********************************************************************* void OneWireMaster::OWTargetSetup(uint8_t family_code) { uint8_t i; // set the search state to find SearchFamily type devices _rom_number[0] = family_code; for (i = 1; i < 8; i++) { _rom_number[i] = 0; } _last_discrepancy = 64; _last_family_discrepancy = 0; _last_device_flag = false; } //********************************************************************* void OneWireMaster::OWFamilySkipSetup(void) { // set the Last discrepancy to last family discrepancy _last_discrepancy = _last_family_discrepancy; // clear the last family discrpepancy _last_family_discrepancy = 0; // check for end of list if (_last_discrepancy == 0) { _last_device_flag = true; } } //********************************************************************* bool OneWireMaster::OWReadROM(void) { bool rtn_val = false; if(!OWReset()) { rtn_val = false; } else { if(!OWWriteByte(READ_ROM)) { rtn_val = false; } else { OWReadBlock(_rom_number, ROMnumberLen); rtn_val = true; } } return rtn_val; } //********************************************************************* bool OneWireMaster::OWSkipROM(void) { bool rtn_val = false; if(!OWReset()) { rtn_val = false; } else { if(!OWWriteByte(SKIP_ROM)) { rtn_val = false; } else { rtn_val = true; } } return rtn_val; } //********************************************************************* bool OneWireMaster::OWMatchROM(void) { bool rtn_val = false; uint8_t idx; if(!OWReset()) { rtn_val = false; } else { if(!OWWriteByte(MATCH_ROM)) { rtn_val = false; } else { for(idx = 0; idx < ROMnumberLen; idx++) { OWWriteByte(_rom_number[idx]); } rtn_val = true; } } return rtn_val; } //********************************************************************* bool OneWireMaster::OWOverdriveSkipROM(void) { bool rtn_val = false; if(!OWReset()) { rtn_val = false; } else { if(!OWWriteByte(OVERDRIVE_SKIP)) { rtn_val = false; } else { //change speed for subsequent comands OWSpeed(SPEED_OVERDRIVE); rtn_val = true; } } return rtn_val; } //********************************************************************* bool OneWireMaster::OWOverdriveMatchROM(void) { bool rtn_val = false; uint8_t idx; if(!OWReset()) { rtn_val = false; } else { if(!OWWriteByte(OVERDRIVE_MATCH)) { rtn_val = false; } else { //change speed before sending ROM number OWSpeed(SPEED_OVERDRIVE); for(idx = 0; idx < ROMnumberLen; idx++) { OWWriteByte(_rom_number[idx]); } rtn_val = true; } } return rtn_val; } //********************************************************************* bool OneWireMaster::OWResume(void) { bool rtn_val = false; if(!OWReset()) { rtn_val = false; } else { if(!OWWriteByte(RESUME)) { rtn_val = false; } else { rtn_val = true; } } return rtn_val; } //********************************************************************* const uint8_t (&OneWireMaster::OWgetROMnumber() const)[ROMnumberLen] { return _rom_number; } //********************************************************************* uint8_t OneWireMaster::OWCalc_crc8(uint8_t data, uint8_t crc8) { unsigned char i; // See Application Note 27 crc8 = crc8 ^ data; for (i = 0; i < 8; ++i) { if (crc8 & 1) { crc8 = (crc8 >> 1) ^ 0x8c; } else { crc8 = (crc8 >> 1); } } return crc8; }