Implementation of 1-Wire with added Alarm Search Functionality
Dependents: Max32630_One_Wire_Interface
Diff: Masters/DS2480B/DS2480B.cpp
- Revision:
- 74:23be10c32fa3
- Parent:
- 73:2cecc1372acc
- Child:
- 75:8b627804927c
--- a/Masters/DS2480B/DS2480B.cpp Thu May 12 14:38:16 2016 -0500 +++ b/Masters/DS2480B/DS2480B.cpp Fri May 13 07:48:35 2016 -0500 @@ -188,14 +188,14 @@ //********************************************************************* DS2480B::DS2480B(mbed::Serial &p_serial) -:_p_serial(&p_serial), _serial_owner(false) + :_p_serial(&p_serial), _serial_owner(false) { } //********************************************************************* DS2480B::DS2480B(PinName tx, PinName rx) -:_p_serial(new mbed::Serial(tx, rx)), _serial_owner(true) + :_p_serial(new mbed::Serial(tx, rx)), _serial_owner(true) { } @@ -203,7 +203,7 @@ //********************************************************************* DS2480B::~DS2480B() { - if(_serial_owner) + if (_serial_owner) { delete _p_serial; } @@ -213,13 +213,13 @@ //********************************************************************* void DS2480B::rx_callback(void) { - while(_p_serial->readable()) + while (_p_serial->readable()) { rx_buffer.buff[rx_buffer.w_idx++] = _p_serial->getc(); rx_buffer.rx_bytes_available++; } - - if(rx_buffer.w_idx == rx_buffer.r_idx) + + if (rx_buffer.w_idx == rx_buffer.r_idx) { rx_buffer.wrap_error = true; } @@ -230,17 +230,17 @@ OneWireMaster::CmdResult DS2480B::OWInitMaster(void) { _p_serial->attach(this, &DS2480B::rx_callback, mbed::Serial::RxIrq); - + rx_buffer.w_idx = 0; rx_buffer.r_idx = 0; rx_buffer.rx_bytes_available = 0; rx_buffer.wrap_error = false; - + _ULevel = OneWireMaster::LEVEL_NORMAL; _UBaud = BPS_9600; _UMode = MODSEL_COMMAND; _USpeed = OneWireMaster::SPEED_STANDARD; - + return DS2480B_Detect(); } @@ -250,15 +250,15 @@ { OneWireMaster::CmdResult result; - uint8_t readbuffer[10],sendpacket[10]; - uint8_t sendlen=0; + uint8_t readbuffer[10], sendpacket[10]; + uint8_t sendlen = 0; // make sure normal level result = OWSetLevel(OneWireMaster::LEVEL_NORMAL); - if(result == OneWireMaster::Success) + if (result == OneWireMaster::Success) { // check for correct mode - if(_UMode != MODSEL_COMMAND) + if (_UMode != MODSEL_COMMAND) { _UMode = MODSEL_COMMAND; sendpacket[sendlen++] = MODE_COMMAND; @@ -266,19 +266,19 @@ // construct the command sendpacket[sendlen++] = (uint8_t)(CMD_COMM | FUNCTSEL_RESET | _USpeed); - + FlushCOM(); // send the packet - result = WriteCOM(sendlen,sendpacket); - if(result == OneWireMaster::Success) + result = WriteCOM(sendlen, sendpacket); + if (result == OneWireMaster::Success) { // read back the 1 byte response - result = ReadCOM(1,readbuffer); - if(result == OneWireMaster::Success) + result = ReadCOM(1, readbuffer); + if (result == OneWireMaster::Success) { // make sure this byte looks like a reset byte - if(((readbuffer[0] & RB_RESET_MASK) == RB_PRESENCE) || ((readbuffer[0] & RB_RESET_MASK) == RB_ALARMPRESENCE)) + if (((readbuffer[0] & RB_RESET_MASK) == RB_PRESENCE) || ((readbuffer[0] & RB_RESET_MASK) == RB_ALARMPRESENCE)) { result = OneWireMaster::Success; } @@ -288,8 +288,8 @@ } } } - - if(result != OneWireMaster::Success) + + if (result != OneWireMaster::Success) { // an error occured so re-sync with DS2480B DS2480B_Detect(); @@ -305,14 +305,14 @@ { OneWireMaster::CmdResult result = OneWireMaster::OperationFailure; - uint8_t readbuffer[10],sendpacket[10]; - uint8_t sendlen=0; + uint8_t readbuffer[10], sendpacket[10]; + uint8_t sendlen = 0; // make sure normal level OWSetLevel(OneWireMaster::LEVEL_NORMAL); // check for correct mode - if(_UMode != MODSEL_COMMAND) + if (_UMode != MODSEL_COMMAND) { _UMode = MODSEL_COMMAND; sendpacket[sendlen++] = MODE_COMMAND; @@ -326,12 +326,12 @@ FlushCOM(); // send the packet - result = WriteCOM(sendlen,sendpacket); - if(result == OneWireMaster::Success) + result = WriteCOM(sendlen, sendpacket); + if (result == OneWireMaster::Success) { // read back the response - result = ReadCOM(1,readbuffer); - if(result == OneWireMaster::Success) + result = ReadCOM(1, readbuffer); + if (result == OneWireMaster::Success) { // interpret the response if (((readbuffer[0] & 0xE0) == 0x80) && ((readbuffer[0] & RB_BIT_MASK) == RB_BIT_ONE)) @@ -356,13 +356,13 @@ } // an error occured so re-sync with DS2480B - if(result != OneWireMaster::Success) + if (result != OneWireMaster::Success) { DS2480B_Detect(); } else { - result = OWSetLevel(after_level); + result = OWSetLevel(after_level); } return result; @@ -374,14 +374,14 @@ { OneWireMaster::CmdResult result = OneWireMaster::OperationFailure; - uint8_t readbuffer[10],sendpacket[10]; - uint8_t sendlen=0; + uint8_t readbuffer[10], sendpacket[10]; + uint8_t sendlen = 0; // make sure normal level OWSetLevel(OneWireMaster::LEVEL_NORMAL); // check for correct mode - if (_UMode != MODSEL_DATA) + if (_UMode != MODSEL_DATA) { _UMode = MODSEL_DATA; sendpacket[sendlen++] = MODE_DATA; @@ -391,7 +391,7 @@ sendpacket[sendlen++] = sendbyte; // check for duplication of data that looks like COMMAND mode - if(sendbyte == MODE_COMMAND) + if (sendbyte == MODE_COMMAND) { sendpacket[sendlen++] = sendbyte; } @@ -400,12 +400,12 @@ FlushCOM(); // send the packet - result = WriteCOM(sendlen,sendpacket); - if(result == OneWireMaster::Success) + result = WriteCOM(sendlen, sendpacket); + if (result == OneWireMaster::Success) { // read back the 1 byte response - result = ReadCOM(1,readbuffer); - if((result == OneWireMaster::Success) && (readbuffer[0] == sendbyte)) + result = ReadCOM(1, readbuffer); + if ((result == OneWireMaster::Success) && (readbuffer[0] == sendbyte)) { result = OneWireMaster::Success; } @@ -420,13 +420,13 @@ } // an error occured so re-sync with DS2480B - if(result != OneWireMaster::Success) + if (result != OneWireMaster::Success) { DS2480B_Detect(); } else { - result = OWSetLevel(after_level); + result = OWSetLevel(after_level); } return result; @@ -438,14 +438,14 @@ { OneWireMaster::CmdResult result = OneWireMaster::OperationFailure; - uint8_t readbuffer[10],sendpacket[10]; - uint8_t sendlen=0; + uint8_t readbuffer[10], sendpacket[10]; + uint8_t sendlen = 0; // make sure normal level OWSetLevel(OneWireMaster::LEVEL_NORMAL); // check for correct mode - if (_UMode != MODSEL_DATA) + if (_UMode != MODSEL_DATA) { _UMode = MODSEL_DATA; sendpacket[sendlen++] = MODE_DATA; @@ -458,12 +458,12 @@ FlushCOM(); // send the packet - result = WriteCOM(sendlen,sendpacket); - if(result == OneWireMaster::Success) + result = WriteCOM(sendlen, sendpacket); + if (result == OneWireMaster::Success) { // read back the 1 byte response - result = ReadCOM(1,readbuffer); - if(result == OneWireMaster::Success) + result = ReadCOM(1, readbuffer); + if (result == OneWireMaster::Success) { recvbyte = readbuffer[0]; result = OneWireMaster::Success; @@ -479,13 +479,13 @@ } // an error occured so re-sync with DS2480B - if(result != OneWireMaster::Success) + if (result != OneWireMaster::Success) { DS2480B_Detect(); } else { - result = OWSetLevel(after_level); + result = OWSetLevel(after_level); } return result; @@ -496,9 +496,9 @@ OneWireMaster::CmdResult DS2480B::OWSetSpeed(OWSpeed new_speed) { OneWireMaster::CmdResult result = OneWireMaster::OperationFailure; - + //TODO - + return result; } @@ -508,21 +508,21 @@ { OneWireMaster::CmdResult result = OneWireMaster::Success; - uint8_t sendpacket[10],readbuffer[10]; - uint8_t sendlen=0; + uint8_t sendpacket[10], readbuffer[10]; + uint8_t sendlen = 0; // check if need to change level - if(new_level != _ULevel) + if (new_level != _ULevel) { // check for correct mode - if(_UMode != MODSEL_COMMAND) + if (_UMode != MODSEL_COMMAND) { _UMode = MODSEL_COMMAND; sendpacket[sendlen++] = MODE_COMMAND; } - + // check if just putting back to normal - if(new_level == OneWireMaster::LEVEL_NORMAL) + if (new_level == OneWireMaster::LEVEL_NORMAL) { // stop pulse command sendpacket[sendlen++] = MODE_STOP_PULSE; @@ -532,19 +532,19 @@ // stop pulse command sendpacket[sendlen++] = MODE_STOP_PULSE; - + FlushCOM(); // send the packet - result = WriteCOM(sendlen,sendpacket); - if(result == OneWireMaster::Success) + result = WriteCOM(sendlen, sendpacket); + if (result == OneWireMaster::Success) { // read back the 1 byte response - result = ReadCOM(2,readbuffer); - if(result == OneWireMaster::Success) + result = ReadCOM(2, readbuffer); + if (result == OneWireMaster::Success) { // check response byte - if (((readbuffer[0] & 0xE0) == 0xE0) && ((readbuffer[1] & 0xE0) == 0xE0)) + if (((readbuffer[0] & 0xE0) == 0xE0) && ((readbuffer[1] & 0xE0) == 0xE0)) { _ULevel = OneWireMaster::LEVEL_NORMAL; } @@ -556,25 +556,25 @@ } } // set new level - else + else { // set the SPUD time value sendpacket[sendlen++] = CMD_CONFIG | PARMSEL_5VPULSE | PARMSET_infinite; // add the command to begin the pulse sendpacket[sendlen++] = CMD_COMM | FUNCTSEL_CHMOD | SPEEDSEL_PULSE | BITPOL_5V; - + FlushCOM(); // send the packet - result = WriteCOM(sendlen,sendpacket); - if(result == OneWireMaster::Success) + result = WriteCOM(sendlen, sendpacket); + if (result == OneWireMaster::Success) { // read back the 1 byte response from setting time limit - result = ReadCOM(1,readbuffer); - if(result == OneWireMaster::Success) + result = ReadCOM(1, readbuffer); + if (result == OneWireMaster::Success) { // check response byte - if ((readbuffer[0] & 0x81) == 0) + if ((readbuffer[0] & 0x81) == 0) { _ULevel = new_level; } @@ -587,11 +587,11 @@ } // if lost communication with DS2480B then reset - if(result != OneWireMaster::Success) + if (result != OneWireMaster::Success) { DS2480B_Detect(); } - + } return result; @@ -603,8 +603,8 @@ { OneWireMaster::CmdResult result; - uint8_t sendpacket[10],readbuffer[10]; - uint8_t sendlen=0; + uint8_t sendpacket[10], readbuffer[10]; + uint8_t sendlen = 0; // reset modes _UMode = MODSEL_COMMAND; @@ -619,13 +619,13 @@ // delay to let line settle wait_ms(2); - + FlushCOM(); // send the timing byte sendpacket[0] = 0xC1; - result = WriteCOM(1,sendpacket); - if(result == OneWireMaster::Success) + result = WriteCOM(1, sendpacket); + if (result == OneWireMaster::Success) { // delay to let line settle wait_ms(2); @@ -643,20 +643,20 @@ // also do 1 bit operation (to test 1-Wire block) sendpacket[sendlen++] = CMD_COMM | FUNCTSEL_BIT | _UBaud | BITPOL_ONE; - + FlushCOM(); // send the packet - result = WriteCOM(sendlen,sendpacket); - if(result == OneWireMaster::Success) + result = WriteCOM(sendlen, sendpacket); + if (result == OneWireMaster::Success) { // read back the response - result = ReadCOM(5,readbuffer); - if(result == OneWireMaster::Success) + result = ReadCOM(5, readbuffer); + if (result == OneWireMaster::Success) { // look at the baud rate and bit operation // to see if the response makes sense - if (((readbuffer[3] & 0xF1) == 0x00) && ((readbuffer[3] & 0x0E) == _UBaud) && ((readbuffer[4] & 0xF0) == 0x90) && ((readbuffer[4] & 0x0C) == _UBaud)) + if (((readbuffer[3] & 0xF1) == 0x00) && ((readbuffer[3] & 0x0E) == _UBaud) && ((readbuffer[4] & 0xF0) == 0x90) && ((readbuffer[4] & 0x0C) == _UBaud)) { result = OneWireMaster::Success; } @@ -667,7 +667,7 @@ } } } - + return result; } @@ -677,15 +677,15 @@ { OneWireMaster::CmdResult result = OneWireMaster::Success; - uint8_t readbuffer[5],sendpacket[5],sendpacket2[5]; - uint8_t sendlen=0,sendlen2=0; + uint8_t readbuffer[5], sendpacket[5], sendpacket2[5]; + uint8_t sendlen = 0, sendlen2 = 0; //see if diffenent then current baud rate - if(_UBaud != newbaud) + if (_UBaud != newbaud) { // build the command packet // check for correct mode - if(_UMode != MODSEL_COMMAND) + if (_UMode != MODSEL_COMMAND) { _UMode = MODSEL_COMMAND; sendpacket[sendlen++] = MODE_COMMAND; @@ -697,8 +697,8 @@ FlushCOM(); // send the packet - result = WriteCOM(sendlen,sendpacket); - if(result == OneWireMaster::Success) + result = WriteCOM(sendlen, sendpacket); + if (result == OneWireMaster::Success) { // make sure buffer is flushed wait_ms(5); @@ -717,21 +717,21 @@ FlushCOM(); // send the packet - result = WriteCOM(sendlen2,sendpacket2); - if(result == OneWireMaster::Success) + result = WriteCOM(sendlen2, sendpacket2); + if (result == OneWireMaster::Success) { // read back the 1 byte response - result = ReadCOM(1,readbuffer); - if(result == OneWireMaster::Success) + result = ReadCOM(1, readbuffer); + if (result == OneWireMaster::Success) { // verify correct baud - if((readbuffer[0] & 0x0E) == (sendpacket[sendlen-1] & 0x0E)) + if ((readbuffer[0] & 0x0E) == (sendpacket[sendlen - 1] & 0x0E)) { result = OneWireMaster::Success; } else { - result = OneWireMaster::OperationFailure; + result = OneWireMaster::OperationFailure; } } else @@ -749,13 +749,13 @@ result = OneWireMaster::CommunicationWriteError; } } - + // if lost communication with DS2480B then reset - if(result != OneWireMaster::Success) + if (result != OneWireMaster::Success) { DS2480B_Detect(); } - + return result; } @@ -764,34 +764,34 @@ OneWireMaster::CmdResult DS2480B::WriteCOM(uint32_t outlen, uint8_t *outbuf) { OneWireMaster::CmdResult result = OneWireMaster::OperationFailure; - + mbed::Timer t; uint32_t t_val; uint32_t timeout; uint8_t idx = 0; - + //calculate timeout, the time needed to tx 1 byte //double for 115200 due to timer object inaccuracies - switch(_UBaud) + switch (_UBaud) { - case BPS_115200: - timeout = ((1000000/115200)*20); - break; - - case BPS_57600: - timeout = ((1000000/57600)*10); - break; - - case BPS_19200: - timeout = ((1000000/19200)*10); - break; - - case BPS_9600: - default: - timeout = ((1000000/9600)*10); - break; + case BPS_115200: + timeout = ((1000000 / 115200) * 20); + break; + + case BPS_57600: + timeout = ((1000000 / 57600) * 10); + break; + + case BPS_19200: + timeout = ((1000000 / 19200) * 10); + break; + + case BPS_9600: + default: + timeout = ((1000000 / 9600) * 10); + break; } - + t.start(); do { @@ -799,10 +799,9 @@ do { t_val = t.read_us(); - } - while(!_p_serial->writeable() && (t_val < timeout)); - - if(t_val < timeout) + } while (!_p_serial->writeable() && (t_val < timeout)); + + if (t_val < timeout) { _p_serial->putc(outbuf[idx++]); result = OneWireMaster::Success; @@ -811,9 +810,8 @@ { result = OneWireMaster::TimeoutError; } - } - while((idx < outlen) && (result == OneWireMaster::Success)); - + } while ((idx < outlen) && (result == OneWireMaster::Success)); + return result; } @@ -823,40 +821,40 @@ { OneWireMaster::CmdResult result; mbed::Timer t; - uint32_t num_bytes_read= 0; + uint32_t num_bytes_read = 0; uint32_t timeout; uint32_t micro_seconds; - + //calculate timeout, 10x the time needed to recieve inlen bytes //double for 115200 due to timer object inaccuracies - switch(_UBaud) + switch (_UBaud) { - case BPS_115200: - timeout = ((1000000/115200)*200)*inlen; - break; - - case BPS_57600: - timeout = ((1000000/57600)*100)*inlen; - break; - - case BPS_19200: - timeout = ((1000000/19200)*100)*inlen; - break; - - case BPS_9600: - default: - timeout = ((1000000/9600)*100)*inlen; - break; + case BPS_115200: + timeout = ((1000000 / 115200) * 200) * inlen; + break; + + case BPS_57600: + timeout = ((1000000 / 57600) * 100) * inlen; + break; + + case BPS_19200: + timeout = ((1000000 / 19200) * 100) * inlen; + break; + + case BPS_9600: + default: + timeout = ((1000000 / 9600) * 100) * inlen; + break; } - - if(rx_buffer.wrap_error) + + if (rx_buffer.wrap_error) { //reset rx buffer, error, and return failure rx_buffer.w_idx = 0; rx_buffer.r_idx = 0; rx_buffer.rx_bytes_available = 0; rx_buffer.wrap_error = false; - + result = OneWireMaster::OperationFailure; } else @@ -868,23 +866,21 @@ do { micro_seconds = t.read_us(); - } - while(!rx_buffer.rx_bytes_available && (micro_seconds < timeout)); - - if(rx_buffer.rx_bytes_available) + } while (!rx_buffer.rx_bytes_available && (micro_seconds < timeout)); + + if (rx_buffer.rx_bytes_available) { inbuf[num_bytes_read++] = rx_buffer.buff[rx_buffer.r_idx++]; rx_buffer.rx_bytes_available--; - } - } - while((num_bytes_read < inlen) && (micro_seconds < timeout) && !rx_buffer.wrap_error); + } + } while ((num_bytes_read < inlen) && (micro_seconds < timeout) && !rx_buffer.wrap_error); t.stop(); - - if(num_bytes_read == inlen) + + if (num_bytes_read == inlen) { result = OneWireMaster::Success; } - else if(micro_seconds > timeout) + else if (micro_seconds > timeout) { result = OneWireMaster::TimeoutError; } @@ -895,11 +891,11 @@ rx_buffer.r_idx = 0; rx_buffer.rx_bytes_available = 0; rx_buffer.wrap_error = false; - + result = OneWireMaster::CommunicationReadError; } } - + return result; } @@ -907,7 +903,7 @@ //********************************************************************* void DS2480B::BreakCOM(void) { - while(!_p_serial->writeable()); + while (!_p_serial->writeable()); //for some reason send_break wouldn't work unless first sending char _p_serial->putc(0); _p_serial->send_break(); @@ -920,13 +916,13 @@ rx_buffer.w_idx = 0; rx_buffer.r_idx = 0; rx_buffer.wrap_error = false; - + //make sure hardware rx buffer is empty - while(_p_serial->readable()) + while (_p_serial->readable()) { _p_serial->getc(); } - + /*Not sure how to flush tx buffer without sending data out on the bus. from the example in AN192, the data shouldn't be sent, just aborted and the buffer cleaned out, http://pdfserv.maximintegrated.com/en/an/AN192.pdf @@ -940,24 +936,24 @@ //********************************************************************* void DS2480B::SetBaudCOM(uint8_t new_baud) { - switch(new_baud) + switch (new_baud) { - case BPS_115200: - _p_serial->baud(115200); - break; - - case BPS_57600: - _p_serial->baud(57600); - break; - - case BPS_19200: - _p_serial->baud(19200); - break; - - case BPS_9600: - default: - _p_serial->baud(9600); - break; + case BPS_115200: + _p_serial->baud(115200); + break; + + case BPS_57600: + _p_serial->baud(57600); + break; + + case BPS_19200: + _p_serial->baud(19200); + break; + + case BPS_9600: + default: + _p_serial->baud(9600); + break; } } @@ -965,12 +961,12 @@ //********************************************************************* int32_t DS2480B::bitacc(uint32_t op, uint32_t state, uint32_t loc, uint8_t *buf) { - int nbyt,nbit; + int nbyt, nbit; nbyt = (loc / 8); nbit = loc - (nbyt * 8); - if(op == WRITE_FUNCTION) + if (op == WRITE_FUNCTION) { if (state) { @@ -982,7 +978,7 @@ } return 1; - } + } else { return ((buf[nbyt] >> nbit) & 0x01);