Anders Rundgren
/
SerialDriverTest
main.cpp@0:1de208404352, 2009-12-29 (annotated)
- Committer:
- andersrundgren
- Date:
- Tue Dec 29 21:21:56 2009 +0000
- Revision:
- 0:1de208404352
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andersrundgren | 0:1de208404352 | 1 | #include "mbed.h" |
andersrundgren | 0:1de208404352 | 2 | |
andersrundgren | 0:1de208404352 | 3 | Serial pc(USBTX, USBRX); // tx, rx |
andersrundgren | 0:1de208404352 | 4 | |
andersrundgren | 0:1de208404352 | 5 | DigitalOut myled_4(LED4); |
andersrundgren | 0:1de208404352 | 6 | DigitalOut myled_3(LED3); |
andersrundgren | 0:1de208404352 | 7 | DigitalOut myled_2(LED2); |
andersrundgren | 0:1de208404352 | 8 | DigitalOut myled_1(LED1); |
andersrundgren | 0:1de208404352 | 9 | |
andersrundgren | 0:1de208404352 | 10 | Timer serial_timeout; |
andersrundgren | 0:1de208404352 | 11 | |
andersrundgren | 0:1de208404352 | 12 | const int MAX_DELAY_BETWEEN_CHARCTERS_IN_MS = 2000; |
andersrundgren | 0:1de208404352 | 13 | |
andersrundgren | 0:1de208404352 | 14 | const int BAUD_RATE = 115200; |
andersrundgren | 0:1de208404352 | 15 | |
andersrundgren | 0:1de208404352 | 16 | const int CAPACITY_COMMAND = 1; |
andersrundgren | 0:1de208404352 | 17 | const int MISSING_DATA_COMMAND = 2; |
andersrundgren | 0:1de208404352 | 18 | const int DEVICE_TIMEOUT_COMMEND = 3; |
andersrundgren | 0:1de208404352 | 19 | |
andersrundgren | 0:1de208404352 | 20 | static int counter; |
andersrundgren | 0:1de208404352 | 21 | |
andersrundgren | 0:1de208404352 | 22 | static int length; |
andersrundgren | 0:1de208404352 | 23 | static int curr_pos; |
andersrundgren | 0:1de208404352 | 24 | |
andersrundgren | 0:1de208404352 | 25 | static bool buffer_full; |
andersrundgren | 0:1de208404352 | 26 | |
andersrundgren | 0:1de208404352 | 27 | static int readCharTimed () |
andersrundgren | 0:1de208404352 | 28 | { |
andersrundgren | 0:1de208404352 | 29 | serial_timeout.reset (); |
andersrundgren | 0:1de208404352 | 30 | while (!pc.readable ()) |
andersrundgren | 0:1de208404352 | 31 | { |
andersrundgren | 0:1de208404352 | 32 | if (serial_timeout.read_ms () > MAX_DELAY_BETWEEN_CHARCTERS_IN_MS) |
andersrundgren | 0:1de208404352 | 33 | { |
andersrundgren | 0:1de208404352 | 34 | myled_1 = 1; |
andersrundgren | 0:1de208404352 | 35 | return -1; |
andersrundgren | 0:1de208404352 | 36 | } |
andersrundgren | 0:1de208404352 | 37 | } |
andersrundgren | 0:1de208404352 | 38 | return pc.getc (); |
andersrundgren | 0:1de208404352 | 39 | } |
andersrundgren | 0:1de208404352 | 40 | |
andersrundgren | 0:1de208404352 | 41 | int getCommand (int first_char) |
andersrundgren | 0:1de208404352 | 42 | { |
andersrundgren | 0:1de208404352 | 43 | buffer_full = false; |
andersrundgren | 0:1de208404352 | 44 | serial_timeout.start (); |
andersrundgren | 0:1de208404352 | 45 | int next_char = readCharTimed (); |
andersrundgren | 0:1de208404352 | 46 | if (next_char < 0) |
andersrundgren | 0:1de208404352 | 47 | { |
andersrundgren | 0:1de208404352 | 48 | myled_2 = 1; |
andersrundgren | 0:1de208404352 | 49 | return -1; |
andersrundgren | 0:1de208404352 | 50 | } |
andersrundgren | 0:1de208404352 | 51 | length = ((first_char << 8) + next_char) & 0xFFFF; |
andersrundgren | 0:1de208404352 | 52 | curr_pos = 1; |
andersrundgren | 0:1de208404352 | 53 | return readCharTimed (); // This is the command (pos 0 in expected buffer) |
andersrundgren | 0:1de208404352 | 54 | } |
andersrundgren | 0:1de208404352 | 55 | |
andersrundgren | 0:1de208404352 | 56 | #define OUTPUT_TIMED |
andersrundgren | 0:1de208404352 | 57 | |
andersrundgren | 0:1de208404352 | 58 | void putByte (int c) |
andersrundgren | 0:1de208404352 | 59 | { |
andersrundgren | 0:1de208404352 | 60 | #ifdef OUTPUT_TIMED |
andersrundgren | 0:1de208404352 | 61 | if (!buffer_full) |
andersrundgren | 0:1de208404352 | 62 | { |
andersrundgren | 0:1de208404352 | 63 | serial_timeout.start (); |
andersrundgren | 0:1de208404352 | 64 | while (true) |
andersrundgren | 0:1de208404352 | 65 | { |
andersrundgren | 0:1de208404352 | 66 | if (pc.writeable ()) |
andersrundgren | 0:1de208404352 | 67 | { |
andersrundgren | 0:1de208404352 | 68 | #endif |
andersrundgren | 0:1de208404352 | 69 | pc.putc (c); |
andersrundgren | 0:1de208404352 | 70 | #ifdef OUTPUT_TIMED |
andersrundgren | 0:1de208404352 | 71 | break; |
andersrundgren | 0:1de208404352 | 72 | } |
andersrundgren | 0:1de208404352 | 73 | if (serial_timeout.read_ms () > MAX_DELAY_BETWEEN_CHARCTERS_IN_MS) |
andersrundgren | 0:1de208404352 | 74 | { |
andersrundgren | 0:1de208404352 | 75 | myled_1 = 1; |
andersrundgren | 0:1de208404352 | 76 | myled_4 = 1; |
andersrundgren | 0:1de208404352 | 77 | buffer_full = true; |
andersrundgren | 0:1de208404352 | 78 | break; |
andersrundgren | 0:1de208404352 | 79 | } |
andersrundgren | 0:1de208404352 | 80 | } |
andersrundgren | 0:1de208404352 | 81 | } |
andersrundgren | 0:1de208404352 | 82 | #endif |
andersrundgren | 0:1de208404352 | 83 | } |
andersrundgren | 0:1de208404352 | 84 | |
andersrundgren | 0:1de208404352 | 85 | |
andersrundgren | 0:1de208404352 | 86 | void putSuccessStatus () |
andersrundgren | 0:1de208404352 | 87 | { |
andersrundgren | 0:1de208404352 | 88 | if (curr_pos != length) |
andersrundgren | 0:1de208404352 | 89 | { |
andersrundgren | 0:1de208404352 | 90 | myled_1 = 1; |
andersrundgren | 0:1de208404352 | 91 | myled_2 = 1; |
andersrundgren | 0:1de208404352 | 92 | myled_3 = 1; |
andersrundgren | 0:1de208404352 | 93 | } |
andersrundgren | 0:1de208404352 | 94 | putByte (0); |
andersrundgren | 0:1de208404352 | 95 | } |
andersrundgren | 0:1de208404352 | 96 | |
andersrundgren | 0:1de208404352 | 97 | |
andersrundgren | 0:1de208404352 | 98 | void putShort (int v) |
andersrundgren | 0:1de208404352 | 99 | { |
andersrundgren | 0:1de208404352 | 100 | putByte (v >> 8); |
andersrundgren | 0:1de208404352 | 101 | putByte (v & 0xFF); |
andersrundgren | 0:1de208404352 | 102 | } |
andersrundgren | 0:1de208404352 | 103 | |
andersrundgren | 0:1de208404352 | 104 | |
andersrundgren | 0:1de208404352 | 105 | void putString (char *string) |
andersrundgren | 0:1de208404352 | 106 | { |
andersrundgren | 0:1de208404352 | 107 | int len = strlen (string); |
andersrundgren | 0:1de208404352 | 108 | putShort (len); |
andersrundgren | 0:1de208404352 | 109 | for (int i = 0; i < len; i++) |
andersrundgren | 0:1de208404352 | 110 | { |
andersrundgren | 0:1de208404352 | 111 | pc.putc (string[i]); |
andersrundgren | 0:1de208404352 | 112 | } |
andersrundgren | 0:1de208404352 | 113 | } |
andersrundgren | 0:1de208404352 | 114 | |
andersrundgren | 0:1de208404352 | 115 | |
andersrundgren | 0:1de208404352 | 116 | int getChar () |
andersrundgren | 0:1de208404352 | 117 | { |
andersrundgren | 0:1de208404352 | 118 | if (curr_pos++ >= length) |
andersrundgren | 0:1de208404352 | 119 | { |
andersrundgren | 0:1de208404352 | 120 | myled_3 = 1; |
andersrundgren | 0:1de208404352 | 121 | } |
andersrundgren | 0:1de208404352 | 122 | return readCharTimed() & 0xFF; |
andersrundgren | 0:1de208404352 | 123 | } |
andersrundgren | 0:1de208404352 | 124 | |
andersrundgren | 0:1de208404352 | 125 | int getShort () |
andersrundgren | 0:1de208404352 | 126 | { |
andersrundgren | 0:1de208404352 | 127 | int v = getChar () << 8; |
andersrundgren | 0:1de208404352 | 128 | return v + getChar (); |
andersrundgren | 0:1de208404352 | 129 | } |
andersrundgren | 0:1de208404352 | 130 | |
andersrundgren | 0:1de208404352 | 131 | int main() |
andersrundgren | 0:1de208404352 | 132 | { |
andersrundgren | 0:1de208404352 | 133 | pc.baud (BAUD_RATE); |
andersrundgren | 0:1de208404352 | 134 | while (1) |
andersrundgren | 0:1de208404352 | 135 | { |
andersrundgren | 0:1de208404352 | 136 | counter++; |
andersrundgren | 0:1de208404352 | 137 | switch (getCommand (pc.getc ())) |
andersrundgren | 0:1de208404352 | 138 | { |
andersrundgren | 0:1de208404352 | 139 | case CAPACITY_COMMAND: |
andersrundgren | 0:1de208404352 | 140 | int out_buffer_size; |
andersrundgren | 0:1de208404352 | 141 | out_buffer_size = getShort () & 0xFFFF; |
andersrundgren | 0:1de208404352 | 142 | int in_buffer_size; |
andersrundgren | 0:1de208404352 | 143 | in_buffer_size = getShort () & 0xFFFF; |
andersrundgren | 0:1de208404352 | 144 | while (in_buffer_size-- > 0) |
andersrundgren | 0:1de208404352 | 145 | { |
andersrundgren | 0:1de208404352 | 146 | getChar (); |
andersrundgren | 0:1de208404352 | 147 | } |
andersrundgren | 0:1de208404352 | 148 | putSuccessStatus (); |
andersrundgren | 0:1de208404352 | 149 | putShort (out_buffer_size); |
andersrundgren | 0:1de208404352 | 150 | while (out_buffer_size-- > 0) |
andersrundgren | 0:1de208404352 | 151 | { |
andersrundgren | 0:1de208404352 | 152 | putByte (0); |
andersrundgren | 0:1de208404352 | 153 | } |
andersrundgren | 0:1de208404352 | 154 | break; |
andersrundgren | 0:1de208404352 | 155 | |
andersrundgren | 0:1de208404352 | 156 | case MISSING_DATA_COMMAND: |
andersrundgren | 0:1de208404352 | 157 | getShort (); |
andersrundgren | 0:1de208404352 | 158 | putSuccessStatus (); |
andersrundgren | 0:1de208404352 | 159 | break; |
andersrundgren | 0:1de208404352 | 160 | |
andersrundgren | 0:1de208404352 | 161 | case DEVICE_TIMEOUT_COMMEND: |
andersrundgren | 0:1de208404352 | 162 | wait (30); |
andersrundgren | 0:1de208404352 | 163 | putSuccessStatus (); |
andersrundgren | 0:1de208404352 | 164 | break; |
andersrundgren | 0:1de208404352 | 165 | |
andersrundgren | 0:1de208404352 | 166 | default: |
andersrundgren | 0:1de208404352 | 167 | myled_2 = 1; |
andersrundgren | 0:1de208404352 | 168 | pc.putc (7); |
andersrundgren | 0:1de208404352 | 169 | putString ("No such command..."); |
andersrundgren | 0:1de208404352 | 170 | } |
andersrundgren | 0:1de208404352 | 171 | } |
andersrundgren | 0:1de208404352 | 172 | } |
andersrundgren | 0:1de208404352 | 173 |