Rev 453 | Go to most recent revision | Details | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 450 | arune | 1 | using System.Threading; |
| 2 | using System; |
||
| 3 | |||
| 4 | public class CanNMT { |
||
| 5 | private const byte CAN_NMT = 0x00; |
||
| 6 | |||
| 7 | private const byte CAN_NMT_RESET = 0x04; |
||
| 8 | private const byte CAN_NMT_BIOS_START = 0x08; |
||
| 9 | private const byte CAN_NMT_PGM_START = 0x0C; |
||
| 10 | private const byte CAN_NMT_PGM_DATA = 0x10; |
||
| 11 | private const byte CAN_NMT_PGM_END = 0x14; |
||
| 12 | private const byte CAN_NMT_PGM_COPY = 0x18; |
||
| 13 | private const byte CAN_NMT_PGM_ACK = 0x1C; |
||
| 14 | private const byte CAN_NMT_PGM_NACK = 0x20; |
||
| 15 | private const byte CAN_NMT_START_APP = 0x24; |
||
| 16 | private const byte CAN_NMT_APP_START = 0x28; |
||
| 17 | private const byte CAN_NMT_HEARTBEAT = 0x2C; |
||
| 18 | |||
| 19 | CanPacket cp; |
||
| 20 | private bool running = false; |
||
| 21 | |||
| 22 | public CanNMT() { |
||
| 23 | cp = new CanPacket(); |
||
| 24 | cp.setExt(1); |
||
| 25 | } |
||
| 26 | |||
| 27 | public void setSender(byte id) { |
||
| 28 | cp.setId( ( ( cp.getId()&0x00FF00FF )|((uint)id<<8) ) ); |
||
| 29 | } |
||
| 30 | public void setReceiver(byte id) { |
||
| 31 | cp.setId( ( ( cp.getId()&0x00FFFF00 )|((uint)id) ) ); |
||
| 32 | } |
||
| 33 | |||
| 34 | public CanPacket getResetPacket() { |
||
| 35 | cp.setId( ( cp.getId()&0x0000FFFF )|( CAN_NMT_RESET<<16 ) ); |
||
| 36 | cp.setDataLength(0); |
||
| 37 | return cp; |
||
| 38 | } |
||
| 39 | |||
| 40 | public CanPacket getStartPacket() { |
||
| 41 | cp.setId( ( cp.getId()&0x0000FFFF )|( CAN_NMT_START_APP<<16 ) ); |
||
| 42 | cp.setDataLength(0); |
||
| 43 | return cp; |
||
| 44 | } |
||
| 45 | |||
| 46 | public bool isBiosStart(CanPacket cp, byte sender) { |
||
| 47 | bool returnval = false; |
||
| 48 | uint id = cp.getId(); |
||
| 49 | if ((id&0x00FF0000) == (CAN_NMT_BIOS_START<<16) && (id&0x0000FF00) == (sender<<8)) { |
||
| 50 | returnval = true; |
||
| 51 | byte[] data = cp.getData(); |
||
| 52 | string appinfo = "Application installed"; |
||
| 53 | if (data[2] == 0) { |
||
| 54 | appinfo = "No application installed"; |
||
| 55 | } |
||
| 56 | Console.WriteLine("Bios started on node 0x" + String.Format("{0:x2}", sender) + |
||
| 57 | ", bios version 0x" + String.Format("{0:x2}", data[1]) + String.Format("{0:x2}", data[0]) + |
||
| 58 | ", " + appinfo |
||
| 59 | ); |
||
| 60 | } |
||
| 61 | return returnval; |
||
| 62 | } |
||
| 63 | |||
| 64 | public bool doStart(DaemonConnection dc, byte receiver) { |
||
| 65 | return doStart(dc, receiver, 0); |
||
| 66 | } |
||
| 67 | |||
| 68 | public bool doStart(DaemonConnection dc, byte receiver, byte sender) { |
||
| 69 | bool success = true; |
||
| 70 | |||
| 71 | setReceiver(receiver); |
||
| 72 | dc.sendCanPacket(getStartPacket()); |
||
| 73 | return success; |
||
| 74 | } |
||
| 75 | |||
| 76 | public bool doReset(DaemonConnection dc, byte receiver) { |
||
| 77 | return doReset(dc, receiver, 0); |
||
| 78 | } |
||
| 79 | |||
| 80 | public bool doReset(DaemonConnection dc, byte receiver, byte sender) { |
||
| 81 | bool success = false; |
||
| 82 | |||
| 83 | setReceiver(receiver); |
||
| 84 | dc.sendCanPacket(getResetPacket()); |
||
| 85 | CanPacket getcp; |
||
| 86 | running = true; |
||
| 87 | dc.flushData(); |
||
| 88 | for (int i = 0; i < 150; i++) { |
||
| 89 | Thread.Sleep(50); |
||
| 90 | if (dc.getData(out getcp)) { |
||
| 91 | if (isBiosStart(getcp, receiver)) { |
||
| 92 | success = true; |
||
| 93 | break; |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | if (!running) { |
||
| 98 | break; |
||
| 99 | } |
||
| 100 | } |
||
| 101 | if (!success) { |
||
| 102 | Console.WriteLine("Timed out while waiting for node to reset"); |
||
| 103 | } |
||
| 104 | return success; |
||
| 105 | } |
||
| 106 | |||
| 107 | public void stop() { |
||
| 108 | running = false; |
||
| 109 | } |
||
| 110 | |||
| 111 | } |