libraries / Bridge / src / Bridge.hon commit Added link to project report (97a3ba0)
   1/*
   2  Copyright (c) 2013 Arduino LLC. All right reserved.
   3
   4  This library is free software; you can redistribute it and/or
   5  modify it under the terms of the GNU Lesser General Public
   6  License as published by the Free Software Foundation; either
   7  version 2.1 of the License, or (at your option) any later version.
   8
   9  This library is distributed in the hope that it will be useful,
  10  but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12  Lesser General Public License for more details.
  13
  14  You should have received a copy of the GNU Lesser General Public
  15  License along with this library; if not, write to the Free Software
  16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17*/
  18
  19#ifndef BRIDGE_H_
  20#define BRIDGE_H_
  21
  22#ifndef BRIDGE_BAUDRATE
  23#define BRIDGE_BAUDRATE 250000
  24#endif
  25
  26#include <Arduino.h>
  27#include <Stream.h>
  28
  29class BridgeClass {
  30  public:
  31    BridgeClass(Stream &_stream);
  32    void begin();
  33
  34    // Methods to handle key/value datastore
  35    void put(const char *key, const char *value);
  36    void put(const String &key, const String &value)
  37    {
  38      put(key.c_str(), value.c_str());
  39    }
  40    unsigned int get(const char *key, uint8_t *buff, unsigned int size);
  41    unsigned int get(const char *key, char *value, unsigned int maxlen)
  42    {
  43      return get(key, reinterpret_cast<uint8_t *>(value), maxlen);
  44    }
  45
  46    // Trasnfer a frame (with error correction and response)
  47    uint16_t transfer(const uint8_t *buff1, uint16_t len1,
  48                      const uint8_t *buff2, uint16_t len2,
  49                      const uint8_t *buff3, uint16_t len3,
  50                      uint8_t *rxbuff, uint16_t rxlen);
  51    // multiple inline versions of the same function to allow efficient frame concatenation
  52    uint16_t transfer(const uint8_t *buff1, uint16_t len1)
  53    {
  54      return transfer(buff1, len1, NULL, 0);
  55    }
  56    uint16_t transfer(const uint8_t *buff1, uint16_t len1,
  57                      uint8_t *rxbuff, uint16_t rxlen)
  58    {
  59      return transfer(buff1, len1, NULL, 0, rxbuff, rxlen);
  60    }
  61    uint16_t transfer(const uint8_t *buff1, uint16_t len1,
  62                      const uint8_t *buff2, uint16_t len2,
  63                      uint8_t *rxbuff, uint16_t rxlen)
  64    {
  65      return transfer(buff1, len1, buff2, len2, NULL, 0, rxbuff, rxlen);
  66    }
  67
  68    uint16_t getBridgeVersion()
  69    {
  70      return bridgeVersion;
  71    }
  72
  73    static const uint16_t TRANSFER_TIMEOUT = 0xFFFF;
  74
  75  private:
  76    uint8_t index;
  77    int timedRead(unsigned int timeout);
  78    void dropAll();
  79    uint16_t bridgeVersion;
  80
  81  private:
  82    void crcUpdate(uint8_t c);
  83    void crcReset();
  84    void crcWrite();
  85    bool crcCheck(uint16_t _CRC);
  86    uint16_t CRC;
  87
  88  private:
  89    static const char CTRL_C = 3;
  90    Stream &stream;
  91    bool started;
  92    uint8_t max_retries;
  93};
  94
  95// This subclass uses a serial port Stream
  96class SerialBridgeClass : public BridgeClass {
  97  public:
  98    SerialBridgeClass(HardwareSerial &_serial)
  99      : BridgeClass(_serial), serial(_serial) {
 100      // Empty
 101    }
 102
 103    void begin(unsigned long baudrate = BRIDGE_BAUDRATE) {
 104      serial.begin(baudrate);
 105      BridgeClass::begin();
 106    }
 107
 108  private:
 109    HardwareSerial &serial;
 110};
 111
 112extern SerialBridgeClass Bridge;
 113
 114// Some microcrontrollers don't start the bootloader after a reset.
 115// This function is intended to let the microcontroller erase its
 116// flash after checking a specific signal coming from the external
 117// device without the need to press the erase button on the board.
 118// The purpose is to enable a software update that does not require
 119// a manual interaction with the board.
 120extern void checkForRemoteSketchUpdate(uint8_t pin = 7);
 121
 122#endif /* BRIDGE_H_ */
 123
 124#include <Console.h>
 125#include <Process.h>