![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Fork to see if I can get working
Dependencies: BufferedSerial OneWire WinbondSPIFlash libxDot-dev-mbed5-deprecated
Fork of xDotBridge_update_test20180823 by
xDotBridge/src/DS2408.cpp@50:e89647e77fd5, 2017-02-17 (annotated)
- Committer:
- Matt Briggs
- Date:
- Fri Feb 17 11:58:40 2017 -0700
- Revision:
- 50:e89647e77fd5
- Parent:
- 40:2ec4be320961
- Child:
- 56:40b454c952cc
Added retries in ds2408 lib and started updating main to make static wireless bridge
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Matt Briggs | 40:2ec4be320961 | 1 | #include "DS2408.h" |
Matt Briggs | 40:2ec4be320961 | 2 | |
Matt Briggs | 40:2ec4be320961 | 3 | enum DS2408Cmd { |
Matt Briggs | 40:2ec4be320961 | 4 | registerReadCmd = 0xF0, |
Matt Briggs | 40:2ec4be320961 | 5 | channelAccessReadCmd = 0xF5, |
Matt Briggs | 40:2ec4be320961 | 6 | channelAccessWriteCmd = 0x5A |
Matt Briggs | 40:2ec4be320961 | 7 | }; |
Matt Briggs | 40:2ec4be320961 | 8 | |
Matt Briggs | 40:2ec4be320961 | 9 | DS2408::DS2408(OneWire *owMaster, uint8_t romAddr[8]) |
Matt Briggs | 40:2ec4be320961 | 10 | { |
Matt Briggs | 40:2ec4be320961 | 11 | mMaster = owMaster; |
Matt Briggs | 40:2ec4be320961 | 12 | std::memcpy(mRomAddr, romAddr, sizeof(mRomAddr)); |
Matt Briggs | 40:2ec4be320961 | 13 | } |
Matt Briggs | 40:2ec4be320961 | 14 | |
Matt Briggs | 40:2ec4be320961 | 15 | CmdResult DS2408::init() |
Matt Briggs | 40:2ec4be320961 | 16 | { |
Matt Briggs | 40:2ec4be320961 | 17 | mMaster->reset(); |
Matt Briggs | 40:2ec4be320961 | 18 | // TODO implement safety cancel test mode recommendation |
Matt Briggs | 40:2ec4be320961 | 19 | return cmdSuccess; |
Matt Briggs | 40:2ec4be320961 | 20 | } |
Matt Briggs | 40:2ec4be320961 | 21 | |
Matt Briggs | 40:2ec4be320961 | 22 | CmdResult DS2408::registerRead(uint8_t addr, uint8_t &val) |
Matt Briggs | 40:2ec4be320961 | 23 | { |
Matt Briggs | 40:2ec4be320961 | 24 | uint8_t addrArray[] = {0x00, 0x00}; |
Matt Briggs | 40:2ec4be320961 | 25 | addrArray[0] = addr; |
Matt Briggs | 40:2ec4be320961 | 26 | mMaster->reset(); |
Matt Briggs | 40:2ec4be320961 | 27 | mMaster->select(mRomAddr); |
Matt Briggs | 40:2ec4be320961 | 28 | mMaster->write(registerReadCmd); // Read Register Command |
Matt Briggs | 40:2ec4be320961 | 29 | mMaster->write_bytes(addrArray, 2); // Write 2 byte addr |
Matt Briggs | 40:2ec4be320961 | 30 | val = mMaster->read(); |
Matt Briggs | 40:2ec4be320961 | 31 | // logDebug("Reg Value: %02x\n", result); |
Matt Briggs | 40:2ec4be320961 | 32 | return cmdSuccess; |
Matt Briggs | 40:2ec4be320961 | 33 | } |
Matt Briggs | 40:2ec4be320961 | 34 | |
Matt Briggs | 40:2ec4be320961 | 35 | CmdResult DS2408::pioLogicRead(uint8_t &val) |
Matt Briggs | 40:2ec4be320961 | 36 | { |
Matt Briggs | 40:2ec4be320961 | 37 | return registerRead(pioLogicStateReg, val); |
Matt Briggs | 40:2ec4be320961 | 38 | } |
Matt Briggs | 40:2ec4be320961 | 39 | |
Matt Briggs | 50:e89647e77fd5 | 40 | CmdResult DS2408::pioLogicReliableRead(uint8_t &val) |
Matt Briggs | 50:e89647e77fd5 | 41 | { |
Matt Briggs | 50:e89647e77fd5 | 42 | uint8_t result = 0; |
Matt Briggs | 50:e89647e77fd5 | 43 | uint8_t result1 = 0xFF; |
Matt Briggs | 50:e89647e77fd5 | 44 | uint8_t cmdResult; |
Matt Briggs | 50:e89647e77fd5 | 45 | cmdResult = pioLogicRead(result); |
Matt Briggs | 50:e89647e77fd5 | 46 | for (int i=0; i < DS2408_NRETRIES; i++) { |
Matt Briggs | 50:e89647e77fd5 | 47 | cmdResult = pioLogicRead(result1); |
Matt Briggs | 50:e89647e77fd5 | 48 | if (cmdResult != cmdSuccess) { |
Matt Briggs | 50:e89647e77fd5 | 49 | continue; |
Matt Briggs | 50:e89647e77fd5 | 50 | } |
Matt Briggs | 50:e89647e77fd5 | 51 | // Check they match |
Matt Briggs | 50:e89647e77fd5 | 52 | if (result == result1) { |
Matt Briggs | 50:e89647e77fd5 | 53 | val = result; |
Matt Briggs | 50:e89647e77fd5 | 54 | return cmdSuccess; |
Matt Briggs | 50:e89647e77fd5 | 55 | } |
Matt Briggs | 50:e89647e77fd5 | 56 | else { |
Matt Briggs | 50:e89647e77fd5 | 57 | result = result1; |
Matt Briggs | 50:e89647e77fd5 | 58 | } |
Matt Briggs | 50:e89647e77fd5 | 59 | } |
Matt Briggs | 50:e89647e77fd5 | 60 | return cmdTimeout; |
Matt Briggs | 50:e89647e77fd5 | 61 | } |
Matt Briggs | 50:e89647e77fd5 | 62 | |
Matt Briggs | 40:2ec4be320961 | 63 | CmdResult DS2408::pioLogicWrite(uint8_t val) |
Matt Briggs | 40:2ec4be320961 | 64 | { |
Matt Briggs | 40:2ec4be320961 | 65 | mMaster->reset(); |
Matt Briggs | 40:2ec4be320961 | 66 | mMaster->select(mRomAddr); |
Matt Briggs | 40:2ec4be320961 | 67 | mMaster->write(channelAccessWriteCmd); |
Matt Briggs | 40:2ec4be320961 | 68 | mMaster->write(val); |
Matt Briggs | 40:2ec4be320961 | 69 | mMaster->write(~val); // Need to write complement to ensure no bit errors |
Matt Briggs | 40:2ec4be320961 | 70 | uint8_t result = mMaster->read(); |
Matt Briggs | 40:2ec4be320961 | 71 | if (result == 0xAA) { |
Matt Briggs | 40:2ec4be320961 | 72 | return cmdSuccess; |
Matt Briggs | 40:2ec4be320961 | 73 | } |
Matt Briggs | 40:2ec4be320961 | 74 | else { |
Matt Briggs | 40:2ec4be320961 | 75 | return cmdError; |
Matt Briggs | 40:2ec4be320961 | 76 | } |
Matt Briggs | 40:2ec4be320961 | 77 | } |
Matt Briggs | 50:e89647e77fd5 | 78 | |
Matt Briggs | 50:e89647e77fd5 | 79 | CmdResult DS2408::pioLogicReliableWrite(uint8_t val) { |
Matt Briggs | 50:e89647e77fd5 | 80 | uint8_t result; |
Matt Briggs | 50:e89647e77fd5 | 81 | for (int i=0; i < DS2408_NRETRIES; i++) { |
Matt Briggs | 50:e89647e77fd5 | 82 | result = pioLogicWrite(val); |
Matt Briggs | 50:e89647e77fd5 | 83 | if (result == cmdSuccess) { |
Matt Briggs | 50:e89647e77fd5 | 84 | return cmdSuccess; |
Matt Briggs | 50:e89647e77fd5 | 85 | } |
Matt Briggs | 50:e89647e77fd5 | 86 | } |
Matt Briggs | 50:e89647e77fd5 | 87 | return cmdTimeout; |
Matt Briggs | 50:e89647e77fd5 | 88 | } |