An experimental timer interrupt driven software based 1-Wire master interface that was created by Robert David Brinzer for a University of Glasgow Level 4 Electronic and Software Engineering Final Year project. This implementation requires mbed RTOS and does not use NOP waits. Perfect for programs that require multi-tasking. Includes the Read and Search network commands and can be easily extended to support new network and transport commands.
Instructions/Transport.cpp@0:7c6dd6bc20e4, 2013-04-15 (annotated)
- Committer:
- sroy
- Date:
- Mon Apr 15 07:02:49 2013 +0000
- Revision:
- 0:7c6dd6bc20e4
An experimental 1-Wire Master interface driven by timer interrupts rather than NOP waits created as part of a University of Glasgow Level 4 Electronic and Software Engineering final year project. Requires the use of mbed RTOS.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sroy | 0:7c6dd6bc20e4 | 1 | #include "Transport.h" |
sroy | 0:7c6dd6bc20e4 | 2 | |
sroy | 0:7c6dd6bc20e4 | 3 | /* This table helps with the compution of the CRC8 used in OneWire ROM |
sroy | 0:7c6dd6bc20e4 | 4 | * |
sroy | 0:7c6dd6bc20e4 | 5 | * The theory is that it precomputes all possible states that an eight bit shift register |
sroy | 0:7c6dd6bc20e4 | 6 | * can go to from a certain state when given a certain byte. Computationally and array |
sroy | 0:7c6dd6bc20e4 | 7 | * lookup should be considerably faster than simulating the shift register a bit at a time. |
sroy | 0:7c6dd6bc20e4 | 8 | * |
sroy | 0:7c6dd6bc20e4 | 9 | * Table was sourced from maxim document AN937 that describes the standard. |
sroy | 0:7c6dd6bc20e4 | 10 | */ |
sroy | 0:7c6dd6bc20e4 | 11 | const unsigned char crc8table[256] = { |
sroy | 0:7c6dd6bc20e4 | 12 | 0, 94, 188, 226, 97, 63, 221, 131, 194, 156, 126, 32, 163, 253, 31, 65, |
sroy | 0:7c6dd6bc20e4 | 13 | 157, 195, 33, 127, 252, 162, 64, 30, 95, 1, 227, 189, 62, 96, 130, 220, |
sroy | 0:7c6dd6bc20e4 | 14 | 35, 125, 159, 193, 66, 28, 254, 160, 225, 191, 93, 3, 128, 222, 60, 98, |
sroy | 0:7c6dd6bc20e4 | 15 | 190, 224, 2, 92, 223, 129, 99, 61, 124, 34, 192, 158, 29, 67, 161, 255, |
sroy | 0:7c6dd6bc20e4 | 16 | 70, 24, 250, 164, 39, 121, 155, 197, 132, 218, 56, 102, 229, 187, 89, 7, |
sroy | 0:7c6dd6bc20e4 | 17 | 219, 133, 103, 57, 186, 228, 6, 88, 25, 71, 165, 251, 120, 38, 196, 154, |
sroy | 0:7c6dd6bc20e4 | 18 | 101, 59, 217, 135, 4, 90, 184, 230, 167, 249, 27, 69, 198, 152, 122, 36, |
sroy | 0:7c6dd6bc20e4 | 19 | 248, 166, 68, 26, 153, 199, 37, 123, 58, 100, 134, 216, 91, 5, 231, 185, |
sroy | 0:7c6dd6bc20e4 | 20 | 140, 210, 48, 110, 237, 179, 81, 15, 78, 16, 242, 172, 47, 113, 147, 205, |
sroy | 0:7c6dd6bc20e4 | 21 | 17, 79, 173, 243, 112, 46, 204, 146, 211, 141, 111, 49, 178, 236, 14, 80, |
sroy | 0:7c6dd6bc20e4 | 22 | 175, 241, 19, 77, 206, 144, 114, 44, 109, 51, 209, 143, 12, 82, 176, 238, |
sroy | 0:7c6dd6bc20e4 | 23 | 50, 108, 142, 208, 83, 13, 239, 177, 240, 174, 76, 18, 145, 207, 45, 115, |
sroy | 0:7c6dd6bc20e4 | 24 | 202, 148, 118, 40, 171, 245, 23, 73, 8, 86, 180, 234, 105, 55, 213, 139, |
sroy | 0:7c6dd6bc20e4 | 25 | 87, 9, 235, 181, 54, 104, 138, 212, 149, 203, 41, 119, 244, 170, 72, 22, |
sroy | 0:7c6dd6bc20e4 | 26 | 233, 183, 85, 11, 136, 214, 52, 106, 43, 117, 151, 201, 74, 20, 246, 168, |
sroy | 0:7c6dd6bc20e4 | 27 | 116, 42, 200, 150, 21, 75, 169, 247, 182, 232, 10, 84, 215, 137, 107, 53}; |
sroy | 0:7c6dd6bc20e4 | 28 | |
sroy | 0:7c6dd6bc20e4 | 29 | // read command |
sroy | 0:7c6dd6bc20e4 | 30 | |
sroy | 0:7c6dd6bc20e4 | 31 | void OWTransport::read(OneWire_Instruction * inst, OneWire_ROM * out){ |
sroy | 0:7c6dd6bc20e4 | 32 | inst->network.code = READ_ROM; |
sroy | 0:7c6dd6bc20e4 | 33 | inst->network.inst = &OWTransport::_inst_readsetup; |
sroy | 0:7c6dd6bc20e4 | 34 | inst->network.args = out; |
sroy | 0:7c6dd6bc20e4 | 35 | *out = 0; // clear rom to a valid value (0 does pass CRC8 but is impossible to get as a device) |
sroy | 0:7c6dd6bc20e4 | 36 | } |
sroy | 0:7c6dd6bc20e4 | 37 | |
sroy | 0:7c6dd6bc20e4 | 38 | void OWTransport::_inst_readsetup(OneWire * which){ |
sroy | 0:7c6dd6bc20e4 | 39 | // setup register |
sroy | 0:7c6dd6bc20e4 | 40 | ((TransportRead *)(which->registers))->read = 0; |
sroy | 0:7c6dd6bc20e4 | 41 | ((TransportRead *)(which->registers))->crc8 = 0; |
sroy | 0:7c6dd6bc20e4 | 42 | |
sroy | 0:7c6dd6bc20e4 | 43 | // setup command |
sroy | 0:7c6dd6bc20e4 | 44 | which->readhandle = &OWTransport::_inst_readhandler; |
sroy | 0:7c6dd6bc20e4 | 45 | which->execute.inst = &OWTransport::_inst_read; |
sroy | 0:7c6dd6bc20e4 | 46 | |
sroy | 0:7c6dd6bc20e4 | 47 | // execute command |
sroy | 0:7c6dd6bc20e4 | 48 | OWTransport::_inst_read(which); |
sroy | 0:7c6dd6bc20e4 | 49 | } |
sroy | 0:7c6dd6bc20e4 | 50 | |
sroy | 0:7c6dd6bc20e4 | 51 | void OWTransport::_inst_read(OneWire * which){ |
sroy | 0:7c6dd6bc20e4 | 52 | if(((TransportRead *)(which->registers))->read < 64){ |
sroy | 0:7c6dd6bc20e4 | 53 | which->op_read(); |
sroy | 0:7c6dd6bc20e4 | 54 | }else{ |
sroy | 0:7c6dd6bc20e4 | 55 | // check if crc8 check passed (remainder 0), failure is not recoverable |
sroy | 0:7c6dd6bc20e4 | 56 | if(((TransportRead *)(which->registers))->crc8) which->abort(CRC_ERROR); |
sroy | 0:7c6dd6bc20e4 | 57 | else which->endInstruction(); |
sroy | 0:7c6dd6bc20e4 | 58 | } |
sroy | 0:7c6dd6bc20e4 | 59 | } |
sroy | 0:7c6dd6bc20e4 | 60 | |
sroy | 0:7c6dd6bc20e4 | 61 | void OWTransport::_inst_readhandler(OneWire * which, char bit){ |
sroy | 0:7c6dd6bc20e4 | 62 | // store bit |
sroy | 0:7c6dd6bc20e4 | 63 | *((unsigned char *)(which->execute.args))|= bit<<(((TransportRead *)(which->registers))->read++%8); |
sroy | 0:7c6dd6bc20e4 | 64 | |
sroy | 0:7c6dd6bc20e4 | 65 | // every 8 bits (byte) compute CRC8 and move to next byte |
sroy | 0:7c6dd6bc20e4 | 66 | if(!(((TransportRead *)(which->registers))->read % 8)){ |
sroy | 0:7c6dd6bc20e4 | 67 | ((TransportRead *)(which->registers))->crc8 = crc8table[((TransportRead *)(which->registers))->crc8 ^ (*((unsigned char *)(which->execute.args)))]; |
sroy | 0:7c6dd6bc20e4 | 68 | which->execute.args = (void *)(((unsigned char *)(which->execute.args))+1); |
sroy | 0:7c6dd6bc20e4 | 69 | } |
sroy | 0:7c6dd6bc20e4 | 70 | } |
sroy | 0:7c6dd6bc20e4 | 71 | |
sroy | 0:7c6dd6bc20e4 | 72 | // search command |
sroy | 0:7c6dd6bc20e4 | 73 | |
sroy | 0:7c6dd6bc20e4 | 74 | void TransportSearchPersist::clear(){ |
sroy | 0:7c6dd6bc20e4 | 75 | rom = 0; |
sroy | 0:7c6dd6bc20e4 | 76 | last = -1; |
sroy | 0:7c6dd6bc20e4 | 77 | done = false; |
sroy | 0:7c6dd6bc20e4 | 78 | } |
sroy | 0:7c6dd6bc20e4 | 79 | |
sroy | 0:7c6dd6bc20e4 | 80 | void OWTransport::search(OneWire_Instruction * inst, TransportSearchPersist * search){ |
sroy | 0:7c6dd6bc20e4 | 81 | inst->network.code = SEARCH_ROM; |
sroy | 0:7c6dd6bc20e4 | 82 | inst->network.inst = &OWTransport::_inst_searchsetup; |
sroy | 0:7c6dd6bc20e4 | 83 | inst->network.args = search; |
sroy | 0:7c6dd6bc20e4 | 84 | } |
sroy | 0:7c6dd6bc20e4 | 85 | |
sroy | 0:7c6dd6bc20e4 | 86 | void OWTransport::_inst_searchsetup(OneWire * which){ |
sroy | 0:7c6dd6bc20e4 | 87 | if(((TransportSearchPersist *)(which->execute.args))->done){ |
sroy | 0:7c6dd6bc20e4 | 88 | // the search had completed, there is nothing more to do |
sroy | 0:7c6dd6bc20e4 | 89 | which->abort(SUCCESS); |
sroy | 0:7c6dd6bc20e4 | 90 | return; |
sroy | 0:7c6dd6bc20e4 | 91 | } |
sroy | 0:7c6dd6bc20e4 | 92 | |
sroy | 0:7c6dd6bc20e4 | 93 | // setup register |
sroy | 0:7c6dd6bc20e4 | 94 | ((TransportSearch *)(which->registers))->read = 0; |
sroy | 0:7c6dd6bc20e4 | 95 | ((TransportSearch *)(which->registers))->crc8 = 0; |
sroy | 0:7c6dd6bc20e4 | 96 | ((TransportSearch *)(which->registers))->marker = -1; |
sroy | 0:7c6dd6bc20e4 | 97 | ((TransportSearch *)(which->registers))->bit = 0; |
sroy | 0:7c6dd6bc20e4 | 98 | |
sroy | 0:7c6dd6bc20e4 | 99 | // setup command |
sroy | 0:7c6dd6bc20e4 | 100 | which->readhandle = &OWTransport::_inst_searchhandler; |
sroy | 0:7c6dd6bc20e4 | 101 | which->execute.inst = &OWTransport::_inst_search; |
sroy | 0:7c6dd6bc20e4 | 102 | |
sroy | 0:7c6dd6bc20e4 | 103 | // execute command |
sroy | 0:7c6dd6bc20e4 | 104 | OWTransport::_inst_search(which); |
sroy | 0:7c6dd6bc20e4 | 105 | } |
sroy | 0:7c6dd6bc20e4 | 106 | |
sroy | 0:7c6dd6bc20e4 | 107 | void OWTransport::_inst_search(OneWire * which){ |
sroy | 0:7c6dd6bc20e4 | 108 | if(((TransportSearch *)(which->registers))->read < 64){ |
sroy | 0:7c6dd6bc20e4 | 109 | // state machine, read bit 1 -> read bit 2 and make choice -> send choice -> repeat up to 64 times |
sroy | 0:7c6dd6bc20e4 | 110 | if(((TransportSearch *)(which->registers))->bit>>3 & 0x01){ |
sroy | 0:7c6dd6bc20e4 | 111 | // send choosen bit and advance |
sroy | 0:7c6dd6bc20e4 | 112 | if(((unsigned char *)&(((TransportSearchPersist *)(which->execute.args))->rom))[((TransportSearch *)(which->registers))->read/8]>>(((TransportSearch *)(which->registers))->read%8) &0x01){ |
sroy | 0:7c6dd6bc20e4 | 113 | which->op_send1(); |
sroy | 0:7c6dd6bc20e4 | 114 | }else{ |
sroy | 0:7c6dd6bc20e4 | 115 | which->op_send0(); |
sroy | 0:7c6dd6bc20e4 | 116 | } |
sroy | 0:7c6dd6bc20e4 | 117 | // compute crc8 |
sroy | 0:7c6dd6bc20e4 | 118 | if(((TransportSearch *)(which->registers))->read % 8 == 7){ |
sroy | 0:7c6dd6bc20e4 | 119 | ((TransportSearch *)(which->registers))->crc8 = crc8table[((TransportSearch *)(which->registers))->crc8 ^ ((unsigned char *)&(((TransportSearchPersist *)(which->execute.args))->rom))[((TransportSearch *)(which->registers))->read/8]]; |
sroy | 0:7c6dd6bc20e4 | 120 | } |
sroy | 0:7c6dd6bc20e4 | 121 | |
sroy | 0:7c6dd6bc20e4 | 122 | // prepare for next cycle (reset state machine) |
sroy | 0:7c6dd6bc20e4 | 123 | ((TransportSearch *)(which->registers))->read+= 1; |
sroy | 0:7c6dd6bc20e4 | 124 | ((TransportSearch *)(which->registers))->bit = 0; |
sroy | 0:7c6dd6bc20e4 | 125 | }else{ |
sroy | 0:7c6dd6bc20e4 | 126 | // read bit 1 and 2 (read function advances state machine) |
sroy | 0:7c6dd6bc20e4 | 127 | which->op_read(); |
sroy | 0:7c6dd6bc20e4 | 128 | } |
sroy | 0:7c6dd6bc20e4 | 129 | }else{ |
sroy | 0:7c6dd6bc20e4 | 130 | ((TransportSearchPersist *)(which->execute.args))->last = ((TransportSearch *)(which->registers))->marker; |
sroy | 0:7c6dd6bc20e4 | 131 | if(((TransportSearchPersist *)(which->execute.args))->last == -1) ((TransportSearchPersist *)(which->execute.args))->done = true; |
sroy | 0:7c6dd6bc20e4 | 132 | |
sroy | 0:7c6dd6bc20e4 | 133 | // check if crc8 check passed (remainder 0), failure is not recoverable |
sroy | 0:7c6dd6bc20e4 | 134 | if(((TransportSearch *)(which->registers))->crc8) which->abort(CRC_ERROR); |
sroy | 0:7c6dd6bc20e4 | 135 | else which->endInstruction(); |
sroy | 0:7c6dd6bc20e4 | 136 | } |
sroy | 0:7c6dd6bc20e4 | 137 | } |
sroy | 0:7c6dd6bc20e4 | 138 | |
sroy | 0:7c6dd6bc20e4 | 139 | void OWTransport::_inst_searchhandler(OneWire * which, char bit){ |
sroy | 0:7c6dd6bc20e4 | 140 | // store bit |
sroy | 0:7c6dd6bc20e4 | 141 | ((TransportSearch *)(which->registers))->bit|= bit<<(((TransportSearch *)(which->registers))->bit>>2 & 0x01); |
sroy | 0:7c6dd6bc20e4 | 142 | |
sroy | 0:7c6dd6bc20e4 | 143 | // advance state |
sroy | 0:7c6dd6bc20e4 | 144 | if(((TransportSearch *)(which->registers))->bit>>2 & 0x01){ |
sroy | 0:7c6dd6bc20e4 | 145 | if((((TransportSearch *)(which->registers))->bit & 0x03) == 3){ |
sroy | 0:7c6dd6bc20e4 | 146 | // all slaves were removed or broke? |
sroy | 0:7c6dd6bc20e4 | 147 | ((TransportSearchPersist *)(which->execute.args))->done = true; // any search results that were obtained could be invalid so end search |
sroy | 0:7c6dd6bc20e4 | 148 | which->abort(NO_PRESENCE); |
sroy | 0:7c6dd6bc20e4 | 149 | return; |
sroy | 0:7c6dd6bc20e4 | 150 | }else if((((TransportSearch *)(which->registers))->bit & 0x03) == 0){ |
sroy | 0:7c6dd6bc20e4 | 151 | // a conflict bit |
sroy | 0:7c6dd6bc20e4 | 152 | if(((TransportSearch *)(which->registers))->read == ((TransportSearchPersist *)(which->execute.args))->last){ |
sroy | 0:7c6dd6bc20e4 | 153 | // already searched 0 bit path, this time send a 1 |
sroy | 0:7c6dd6bc20e4 | 154 | ((unsigned char *)&(((TransportSearchPersist *)(which->execute.args))->rom))[((TransportSearch *)(which->registers))->read/8]|= 0x01<<(((TransportSearch *)(which->registers))->read%8); |
sroy | 0:7c6dd6bc20e4 | 155 | }else if(((TransportSearch *)(which->registers))->read > ((TransportSearchPersist *)(which->execute.args))->last){ |
sroy | 0:7c6dd6bc20e4 | 156 | // a new branch to search |
sroy | 0:7c6dd6bc20e4 | 157 | ((unsigned char *)&(((TransportSearchPersist *)(which->execute.args))->rom))[((TransportSearch *)(which->registers))->read/8]&= ~(0x01<<(((TransportSearch *)(which->registers))->read%8)); |
sroy | 0:7c6dd6bc20e4 | 158 | ((TransportSearch *)(which->registers))->marker = ((TransportSearch *)(which->registers))->read; |
sroy | 0:7c6dd6bc20e4 | 159 | }else if( !(((unsigned char *)&(((TransportSearchPersist *)(which->execute.args))->rom))[((TransportSearch *)(which->registers))->read/8]>>(((TransportSearch *)(which->registers))->read%8) &0x01) ){ |
sroy | 0:7c6dd6bc20e4 | 160 | // a previous branch decision |
sroy | 0:7c6dd6bc20e4 | 161 | ((TransportSearch *)(which->registers))->marker = ((TransportSearch *)(which->registers))->read; |
sroy | 0:7c6dd6bc20e4 | 162 | } |
sroy | 0:7c6dd6bc20e4 | 163 | }else{ |
sroy | 0:7c6dd6bc20e4 | 164 | // no conflict so write the bit |
sroy | 0:7c6dd6bc20e4 | 165 | if(((TransportSearch *)(which->registers))->bit & 0x01){ |
sroy | 0:7c6dd6bc20e4 | 166 | ((unsigned char *)&(((TransportSearchPersist *)(which->execute.args))->rom))[((TransportSearch *)(which->registers))->read/8]|= 0x01<<(((TransportSearch *)(which->registers))->read%8); |
sroy | 0:7c6dd6bc20e4 | 167 | }else{ |
sroy | 0:7c6dd6bc20e4 | 168 | ((unsigned char *)&(((TransportSearchPersist *)(which->execute.args))->rom))[((TransportSearch *)(which->registers))->read/8]&= ~(0x01<<(((TransportSearch *)(which->registers))->read%8)); |
sroy | 0:7c6dd6bc20e4 | 169 | } |
sroy | 0:7c6dd6bc20e4 | 170 | } |
sroy | 0:7c6dd6bc20e4 | 171 | |
sroy | 0:7c6dd6bc20e4 | 172 | // tell next call to send the choice |
sroy | 0:7c6dd6bc20e4 | 173 | ((TransportSearch *)(which->registers))->bit|= 0x08; |
sroy | 0:7c6dd6bc20e4 | 174 | }else{ |
sroy | 0:7c6dd6bc20e4 | 175 | // we need to read another bit |
sroy | 0:7c6dd6bc20e4 | 176 | ((TransportSearch *)(which->registers))->bit|= 0x04; |
sroy | 0:7c6dd6bc20e4 | 177 | } |
sroy | 0:7c6dd6bc20e4 | 178 | } |