Implementation of 1-Wire with added Alarm Search Functionality
Dependents: Max32630_One_Wire_Interface
Diff: OneWire_Masters/DS248x/ds248x.cpp
- Revision:
- 5:ce108eeb878d
- Parent:
- 3:644fc630f958
- Child:
- 6:1faafa0b3cd7
--- a/OneWire_Masters/DS248x/ds248x.cpp Tue Feb 02 21:34:27 2016 +0000 +++ b/OneWire_Masters/DS248x/ds248x.cpp Tue Feb 09 03:30:22 2016 +0000 @@ -46,86 +46,34 @@ #include "ds248x.h" -//OW ROM Commands -#define READ_ROM 0x33 -#define MATCH_ROM 0x55 -#define SEARCH_ROM 0xF0 -#define SKIP_ROM 0xCC - -// ds248x commands -#define CMD_DRST 0xF0 -#define CMD_WCFG 0xD2 -#define CMD_A1WP 0xC3 //DS2484 only -#define CMD_CHSL 0xC3 //DS2482-800 only -#define CMD_SRP 0xE1 -#define CMD_1WRS 0xB4 -#define CMD_1WWB 0xA5 -#define CMD_1WRB 0x96 -#define CMD_1WSB 0x87 -#define CMD_1WT 0x78 - -// ds248x config bits -#define CONFIG_APU 0x01 -#define CONFIG_PDN 0x02 -#define CONFIG_SPU 0x04 -#define CONFIG_1WS 0x08 - -// ds248x status bits -#define STATUS_1WB 0x01 -#define STATUS_PPD 0x02 -#define STATUS_SD 0x04 -#define STATUS_LL 0x08 -#define STATUS_RST 0x10 -#define STATUS_SBR 0x20 -#define STATUS_TSB 0x40 -#define STATUS_DIR 0x80 - -// ds2484 adjustable parameters -#define TRSTL 0 -#define TRSTL_OD 1 -#define TMSP 2 -#define TMSP_OD 3 -#define TW0L 4 -#define TW0L_OD 5 -#define TREC0 6 //OD NA -#define RWPU 8 //OD NA - - -// misc constants -#define POLL_LIMIT 200 -#define TRUE 1 -#define FALSE 0 - -// API mode bit flags -#define MODE_STANDARD 0x00 -#define MODE_OVERDRIVE 0x01 -#define MODE_STRONG 0x02 - - //********************************************************************* -Ds248x::Ds248x(I2C *p_i2c_bus, ds248x_i2c_adrs_t adrs) +Ds248x::Ds248x(I2C &i2c_bus, DS248X_I2C_ADRS adrs) +:_p_i2c_bus(&i2c_bus), _w_adrs(adrs << 1), _r_adrs((adrs << 1) | 1), _i2c_owner(false) { - _p_i2c_bus = p_i2c_bus; - _w_adrs = (adrs << 1); - _r_adrs = (_w_adrs | 1); - i2c_owner = false; + //empty body } //********************************************************************* -Ds248x::Ds248x(PinName sda, PinName scl, ds248x_i2c_adrs_t adrs) +Ds248x::Ds248x(PinName sda, PinName scl, DS248X_I2C_ADRS adrs) +:_p_i2c_bus(new I2C(sda, scl)), _w_adrs(adrs << 1), _r_adrs((adrs << 1) | 1), _i2c_owner(true) { - _p_i2c_bus = new I2C(sda, scl); - _w_adrs = (adrs << 1); - _r_adrs = (_w_adrs | 1); - i2c_owner = true; + //empty body +} + + +//********************************************************************* +Ds248x::Ds248x(DS248X_I2C_ADRS adrs) +:_w_adrs(adrs << 1), _r_adrs((adrs << 1) | 1) +{ + //empty body } //********************************************************************* Ds248x::~Ds248x() { - if(i2c_owner) + if(_i2c_owner) { delete _p_i2c_bus; } @@ -145,10 +93,10 @@ else { // default configuration - _c1WS = FALSE; - _cSPU = FALSE; - _cPDN = FALSE; - _cAPU = FALSE; + _c1WS = false; + _cSPU = false; + _cPDN = false; + _cAPU = false; // write the default configuration setup if (!write_config(_c1WS | _cSPU | _cPDN | _cAPU)) @@ -310,7 +258,7 @@ // handle error // ... reset(); - rtn_val = FALSE; + rtn_val = false; } else { @@ -359,11 +307,11 @@ // check for short condition if (status & STATUS_SD) { - _short_detected = TRUE; + _short_detected = true; } else { - _short_detected = FALSE; + _short_detected = false; } // check for presence detect @@ -571,11 +519,35 @@ //********************************************************************* +void Ds248x::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 Ds248x::OWReadBlock(uint8_t *recv_buf, uint8_t recv_len) +{ + uint8_t idx; + + for(idx = 0; idx < recv_len; idx++) + { + recv_buf[idx] = OWReadByte(); + } +} + + +//********************************************************************* bool Ds248x::OWFirst(void) { // reset the search state _last_discrepancy = 0; - _last_device_flag = FALSE; + _last_device_flag = false; _last_family_discrepancy = 0; return OWSearch(); @@ -610,24 +582,24 @@ // set search to find the same device _last_discrepancy = 64; - _last_device_flag = FALSE; + _last_device_flag = false; if (OWSearch()) { // check if same device found - rslt = TRUE; + rslt = true; for (i = 0; i < 8; i++) { if (rom_backup[i] != _rom_number[i]) { - rslt = FALSE; + rslt = false; break; } } } else { - rslt = FALSE; + rslt = false; } // restore the search state @@ -661,7 +633,7 @@ _last_discrepancy = 64; _last_family_discrepancy = 0; - _last_device_flag = FALSE; + _last_device_flag = false; } @@ -677,7 +649,7 @@ // check for end of list if (_last_discrepancy == 0) { - _last_device_flag = TRUE; + _last_device_flag = true; } } @@ -695,7 +667,7 @@ last_zero = 0; rom_byte_number = 0; rom_byte_mask = 1; - search_result = FALSE; + search_result = false; _crc8 = 0; // if the last call was not the last one @@ -706,9 +678,9 @@ { // reset the search _last_discrepancy = 0; - _last_device_flag = FALSE; + _last_device_flag = false; _last_family_discrepancy = 0; - return FALSE; + return false; } // issue the search command @@ -772,7 +744,7 @@ // if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask if (rom_byte_mask == 0) { - OWCalc_crc8(_rom_number[rom_byte_number]); // accumulate the CRC + _crc8 = OWCalc_crc8(_rom_number[rom_byte_number], _crc8); // accumulate the CRC rom_byte_number++; rom_byte_mask = 1; } @@ -788,9 +760,9 @@ // check for last device if (_last_discrepancy == 0) - _last_device_flag = TRUE; + _last_device_flag = true; - search_result = TRUE; + search_result = true; } } @@ -798,9 +770,9 @@ if (!search_result || (_rom_number[0] == 0)) { _last_discrepancy = 0; - _last_device_flag = FALSE; + _last_device_flag = false; _last_family_discrepancy = 0; - search_result = FALSE; + search_result = false; } return search_result; @@ -808,16 +780,182 @@ //********************************************************************* -uint8_t Ds248x::OWSpeed(uint8_t new_speed) +bool Ds248x::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 Ds248x::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 Ds248x::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 Ds248x::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 Ds248x::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 Ds248x::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; +} + + +//********************************************************************* +uint8_t Ds248x::OWSpeed(OW_SPEED new_speed) { // set the speed - if (new_speed == MODE_OVERDRIVE) + if (new_speed == SPEED_OVERDRIVE) { _c1WS = CONFIG_1WS; } else { - _c1WS = FALSE; + _c1WS = false; } // write the new config @@ -828,22 +966,22 @@ //********************************************************************* -uint8_t Ds248x::OWLevel(uint8_t new_level) +uint8_t Ds248x::OWLevel(OW_LEVEL new_level) { uint8_t rtn_val; // function only will turn back to non-strong pull-up - if (new_level != MODE_STANDARD) + if (new_level != LEVEL_NORMAL) { - rtn_val = MODE_STRONG; + rtn_val = LEVEL_STRONG; } else { // clear the strong pull-up bit in the global config state - _cSPU = FALSE; + _cSPU = false; // write the new config write_config(_c1WS | _cSPU | _cPDN | _cAPU); - rtn_val = MODE_STANDARD; + rtn_val = LEVEL_NORMAL; } return(rtn_val); @@ -897,7 +1035,7 @@ // check if response was correct, if not then turn off strong pull-up if (rdbit != applyPowerResponse) { - OWLevel(MODE_STANDARD); + OWLevel(LEVEL_NORMAL); rtn_val = false; } @@ -909,33 +1047,7 @@ //********************************************************************* -uint8_t Ds248x::OWCalc_crc8(uint8_t data) +const uint8_t (&Ds248x::OWgetROMnumber() const)[ROMnumberLen] { - 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; + return _rom_number; } - - -//********************************************************************* -void Ds248x::OWgetROMnumber(uint8_t *p_rom_buff) -{ - for(uint8_t idx = 0; idx < 8; idx++) - { - *(p_rom_buff + idx) = _rom_number[idx]; - } -} \ No newline at end of file