Christopher Haster / Mbed 2 deprecated ESPQuickFlash

Dependencies:   BufferedSerial mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers slip.h Source File

slip.h

00001 #ifndef SLIP_H
00002 #define SLIP_H
00003 
00004 // Packet wrapping using the SLIP protocol
00005 template <typename SERIAL>
00006 class SLIPPacket {
00007 private:
00008     SERIAL _serial;
00009     
00010 public:
00011     SLIPPacket(PinName tx, PinName rx) : _serial(tx, rx) {}
00012     
00013     int checkgetc() {
00014         Timer timer;
00015         timer.start();
00016         
00017         while (!_serial.readable()) {
00018             if (timer.read_ms() > 1500)
00019                 return -1;
00020         }
00021             
00022         int c = _serial.getc();
00023 #ifdef DEBUG
00024         printf("<- 0x%02x\r\n", c);
00025 #endif
00026         return c;
00027     }
00028     
00029     int checkputc(char c) {
00030         wait_us(100);
00031         
00032         while (!_serial.writeable())
00033             ;
00034 
00035 #ifdef DEBUG
00036         printf("-> 0x%02x\r\n", c);
00037 #endif
00038         return _serial.putc(c);
00039     }
00040     
00041     void flush() {
00042         while (_serial.readable())
00043             _serial.getc();
00044     }
00045 
00046 public:
00047     bool req_start() {
00048 #ifdef LED_STATUS
00049         led_blue = 0; // Show progress
00050 #endif
00051         return !(checkputc(0xc0) < 0);
00052     }
00053     
00054     bool req_finish() {
00055 #ifdef LED_STATUS
00056         led_blue = 1; // Show progress
00057 #endif
00058         return !(checkputc(0xc0) < 0);
00059     }
00060     
00061     bool putc(char c) {
00062         switch (c) {
00063             case 0xdb: return checkputc(0xdb) >= 0 && checkputc(0xdd) >= 0;
00064             case 0xc0: return checkputc(0xdb) >= 0 && checkputc(0xdc) >= 0;
00065             default:   return checkputc(c) >= 0;
00066         }
00067     }
00068     
00069     bool send(const void *data, int len) {
00070         for (int i = 0; i < len; i++) {
00071             if (!putc(((char *)data)[i]))
00072                 return false;
00073         }
00074         
00075         return true;
00076     }
00077  
00078 public:
00079     bool resp_start() {
00080         return checkgetc() == 0xc0;
00081     }
00082     
00083     bool resp_finish() {
00084         return checkgetc() == 0xc0;
00085     }
00086     
00087     int getc() {
00088         int c = checkgetc();
00089         
00090         if (c < 0) {
00091             return -1;
00092         } else if (c == 0xdb) {
00093             c = checkgetc();
00094             switch (c) {
00095                 case 0xdd: return 0xdb;
00096                 case 0xdc: return 0xc0;
00097                 default:   return -1;
00098             }
00099         } else {
00100             return c;
00101         }
00102     }
00103     
00104     bool recv(void *resp, int len) {
00105         for (int i = 0; i < len; i++) {
00106             int c = getc();
00107             
00108             if (c < 0)
00109                 return false;
00110             
00111             if (resp)
00112                 ((char *)resp)[i] = c;
00113         }
00114         
00115         return true;
00116     }
00117 };
00118 
00119 #endif