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 { 30public: 31BridgeClass(Stream &_stream); 32voidbegin(); 33 34// Methods to handle key/value datastore 35voidput(const char*key,const char*value); 36voidput(const String &key,const String &value) 37{ 38put(key.c_str(), value.c_str()); 39} 40unsigned intget(const char*key,uint8_t*buff,unsigned int size); 41unsigned intget(const char*key,char*value,unsigned int maxlen) 42{ 43returnget(key,reinterpret_cast<uint8_t*>(value), maxlen); 44} 45 46// Trasnfer a frame (with error correction and response) 47uint16_ttransfer(const uint8_t*buff1,uint16_t len1, 48const uint8_t*buff2,uint16_t len2, 49const uint8_t*buff3,uint16_t len3, 50uint8_t*rxbuff,uint16_t rxlen); 51// multiple inline versions of the same function to allow efficient frame concatenation 52uint16_ttransfer(const uint8_t*buff1,uint16_t len1) 53{ 54returntransfer(buff1, len1, NULL,0); 55} 56uint16_ttransfer(const uint8_t*buff1,uint16_t len1, 57uint8_t*rxbuff,uint16_t rxlen) 58{ 59returntransfer(buff1, len1, NULL,0, rxbuff, rxlen); 60} 61uint16_ttransfer(const uint8_t*buff1,uint16_t len1, 62const uint8_t*buff2,uint16_t len2, 63uint8_t*rxbuff,uint16_t rxlen) 64{ 65returntransfer(buff1, len1, buff2, len2, NULL,0, rxbuff, rxlen); 66} 67 68uint16_tgetBridgeVersion() 69{ 70return bridgeVersion; 71} 72 73static const uint16_t TRANSFER_TIMEOUT =0xFFFF; 74 75private: 76uint8_t index; 77inttimedRead(unsigned int timeout); 78voiddropAll(); 79uint16_t bridgeVersion; 80 81private: 82voidcrcUpdate(uint8_t c); 83voidcrcReset(); 84voidcrcWrite(); 85boolcrcCheck(uint16_t _CRC); 86uint16_t CRC; 87 88private: 89static const char CTRL_C =3; 90 Stream &stream; 91bool started; 92uint8_t max_retries; 93}; 94 95// This subclass uses a serial port Stream 96class SerialBridgeClass :public BridgeClass { 97public: 98SerialBridgeClass(HardwareSerial &_serial) 99:BridgeClass(_serial),serial(_serial) { 100// Empty 101} 102 103voidbegin(unsigned long baudrate = BRIDGE_BAUDRATE) { 104 serial.begin(baudrate); 105BridgeClass::begin(); 106} 107 108private: 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. 120externvoidcheckForRemoteSketchUpdate(uint8_t pin =7); 121 122#endif/* BRIDGE_H_ */ 123 124#include <Console.h> 125#include <Process.h>