An example Program for the SimpleSerialProtocol Library, This program will receive a packet, then echo it back to the client

Dependencies:   mbed SimpleSerialProtocol MODSERIAL

A simple example program that receives a packet over serial and echos it back.

I include this java program to show an example client application, all this program does is send packets as fast as it can without filling up its output buffer, the mbed will echo these packets back.

This is a good benchmark of the serial connection, and should show about 11KB/s at 115200baud

/media/uploads/p3p/serialecho.zip

example command: java -jar SerialEcho.jar com3 115200

Files at this revision

API Documentation at this revision

Comitter:
p3p
Date:
Sun Jul 29 19:31:31 2012 +0000
Parent:
5:f693f68e9de6
Child:
10:f6862abba2d5
Commit message:
Dma tests removed

Changed in this revision

MODDMA.lib Show diff for this revision Revisions of this file
MODSERIAL.lib Show annotated file Show diff for this revision Revisions of this file
SimpleSerialProtocol.lib Show annotated file Show diff for this revision Revisions of this file
SimpleSerialProtocol/.lib Show diff for this revision Revisions of this file
SimpleSerialProtocol/FPointer.h Show diff for this revision Revisions of this file
SimpleSerialProtocol/Packet.h Show diff for this revision Revisions of this file
SimpleSerialProtocol/Protocol.cpp Show diff for this revision Revisions of this file
SimpleSerialProtocol/Protocol.h Show diff for this revision Revisions of this file
SimpleSerialProtocol/Ringbuffer.h Show diff for this revision Revisions of this file
TestProtocol.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
mbed.lib Show diff for this revision Revisions of this file
--- a/MODDMA.lib	Sat Mar 17 16:42:31 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/p3p/libraries/MODDMA/m3x0ky
\ No newline at end of file
--- a/MODSERIAL.lib	Sat Mar 17 16:42:31 2012 +0000
+++ b/MODSERIAL.lib	Sun Jul 29 19:31:31 2012 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/AjK/libraries/MODSERIAL/lqybn0
\ No newline at end of file
+http://mbed.org/users/AjK/code/MODSERIAL/#5c45c21f36b7
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SimpleSerialProtocol.lib	Sun Jul 29 19:31:31 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/p3p/code/SimpleSerialProtocol/#1639507580d5
--- a/SimpleSerialProtocol/.lib	Sat Mar 17 16:42:31 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/p3p/libraries/SimpleSerialProtocol/m6h1dv
\ No newline at end of file
--- a/SimpleSerialProtocol/FPointer.h	Sat Mar 17 16:42:31 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,97 +0,0 @@
-/*
-    Copyright (c) 2011 Andy Kirkham
-
-    Permission is hereby granted, free of charge, to any person obtaining a copy
-    of this software and associated documentation files (the "Software"), to deal
-    in the Software without restriction, including without limitation the rights
-    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-    copies of the Software, and to permit persons to whom the Software is
-    furnished to do so, subject to the following conditions:
-
-    The above copyright notice and this permission notice shall be included in
-    all copies or substantial portions of the Software.
-
-    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-    THE SOFTWARE.
-*/
-
-#ifndef AJK_FPOINTER_H
-#define AJK_FPOINTER_H
-
-#include "Packet.h"
-
-namespace SimpleSerialProtocol {
-
-class FPointerDummy;
-
-typedef Packet* Packet_ptr;
-
-class FPointer {
-
-protected:
-
-    //! C callback function pointer.
-    void (*c_callback)(Packet_ptr);
-
-    //! C++ callback object/method pointer (the object part).
-    FPointerDummy  *obj_callback;
-
-    //! C++ callback object/method pointer (the method part).
-    void (FPointerDummy::*method_callback)(Packet_ptr);
-
-public:
-
-    /** Constructor
-     */
-    FPointer() {
-        c_callback      = NULL;
-        obj_callback    = NULL;
-        method_callback = NULL;
-    }
-
-
-    void attach(void (*function)(Packet_ptr) = 0) {
-        c_callback = function;
-    }
-
-    template<class T>
-    void attach(T* item, void (T::*method)(Packet_ptr)) {
-        obj_callback    = (FPointerDummy *)item;
-        method_callback = (void (FPointerDummy::*)(Packet_ptr))method;
-    }
-
-    void call(Packet_ptr arg) {
-        if (c_callback != NULL) {
-            return (*c_callback)(arg);
-        } else {
-            if (obj_callback  != NULL && method_callback != NULL) {
-                (obj_callback->*method_callback)(arg);
-                return;
-            }
-        }
-        return;
-    }
-
-    void call(void) {
-        if (c_callback != NULL) {
-            (*c_callback)((Packet_ptr)NULL);
-            return;
-        } else {
-            if (obj_callback  != NULL && method_callback != NULL) {
-                (obj_callback->*method_callback)((Packet_ptr)NULL);
-                return;
-            }
-        }
-        return;
-    }
-};
-
-};
-
-
-#endif
--- a/SimpleSerialProtocol/Packet.h	Sat Mar 17 16:42:31 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,62 +0,0 @@
-#ifndef SIMPLE_SERIAL_PROTOCOL_PACKET
-#define SIMPLE_SERIAL_PROTOCOL_PACKET
-
-namespace SimpleSerialProtocol {
-
-class Packet {
-public:
-    Packet() {
-        _type = 0;
-        _size = 0;
-        _checksum = 0;
-        _valid = false;
-        memset(_data, 0, sizeof(_data));
-    }
-    virtual ~Packet() {}
-
-    void reset() {
-        _type = 0;
-        _size = 0;
-        _checksum = 0;
-        _valid = false;
-    }
-
-    template <class T> void buildData (T* message) {
-        uint8_t* payload_array = reinterpret_cast<uint8_t *>(message);
-        memcpy(_data, payload_array, sizeof(T));
-        _type = _data[0];
-        _size = sizeof(T);
-    }
-
-    template <class T> T* interpretData () {
-        if (_size == sizeof(T)) {
-            return reinterpret_cast<T*>(_data);
-        }
-        return 0;
-    }
-    
-    static void swapEndian(void *pv, size_t n) {
-        uint8_t *p = reinterpret_cast<uint8_t *>(pv);
-        size_t lo, hi;
-        for (lo=0, hi=n-1; hi>lo; lo++, hi--) {
-            uint8_t tmp=p[lo];
-            p[lo] = p[hi];
-            p[hi] = tmp;
-        }
-    }
-    
-    template <class T> static T swapEndian(T pv) {
-        swapEndian(reinterpret_cast<void*>(&pv), sizeof(T));
-        return pv;
-    }
-
-    uint8_t _type;
-    uint8_t _data[512];
-    uint16_t _size;
-    uint16_t _checksum;
-    bool _valid;
-};
-
-};
-
-#endif
\ No newline at end of file
--- a/SimpleSerialProtocol/Protocol.cpp	Sat Mar 17 16:42:31 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,206 +0,0 @@
-#include <stdarg.h>
-
-#include "Protocol.h"
-
-namespace SimpleSerialProtocol {
-
-void Protocol::dmaCallback(MODSERIAL_IRQ_INFO *q) {
-    _transfer_in_progress = false;
-}
-
-Protocol::Protocol(PinName tx, PinName rx, PinName led_pin) : MODSERIAL(tx, rx, 64, 512), _receive_led(led_pin), _send_buffer(512) {
-
-    _corrupt_packets = 0;
-    _invalid_bytes = 0;
-
-    _header[0]= 0;
-    _header[1]= 0;
-    _header_read = 0;
-    _data_read = 0;
-    _footer_read = 0;
-    _state = 0;
-    _last_byte = 0;
-    _transfer_in_progress = false;
-    _dma_port = 0;
-    _receive_timeout.start();
-
-    attach_dmaSendComplete( this, &Protocol::dmaCallback );
-}
-
-Protocol::~Protocol() {
-
-}
-
-void Protocol::blockUntilDmaTxEmpty(){
-    while(_send_buffer.available()){
-        send();
-    }
-    return;
-}
-
-void Protocol::update() {
-    for (uint16_t j = 0; j < _transmit_callback.size(); j++) {
-        if (_transmit_callback[j].trigger()) {
-            _transmit_callback[j]._callback.call(&_packet_transmit);
-            sendPacket(&_packet_transmit);
-            _packet_transmit.reset();
-        }
-    }
-    
-    send();
-    receive();
-    
-    if (packetWaiting()) {
-        _receive_led = 1;
-
-        if (_callback.count(_packet._type)) {
-            for (uint16_t i = 0; i < _callback[_packet._type].size(); i++) {
-                _callback[_packet._type][i].call(&_packet);
-            }
-        }
-        _packet.reset();
-        _receive_led = 0;
-    }
-}
-
-void Protocol::send(uint8_t byte) {
-    _send_buffer.write(byte);
-}
-
-void Protocol::send() {
-    if (_transfer_in_progress) {
-        return;
-    }
-
-    uint16_t buffer_size = _send_buffer.available();
-    uint16_t send_length = 0;
-
-    if (buffer_size < SEND_CHUNK_SIZE) {
-        send_length = buffer_size;
-    } else {
-        send_length =  SEND_CHUNK_SIZE;
-    }
-
-    if ( buffer_size ) {
-        for (int i = 0; i < send_length; i++) {
-            _dma_buffer[i] = _send_buffer.read();
-        }
-        dmaSend( (char*)_dma_buffer, send_length, _dma_port);
-        _transfer_in_progress = true;
-    }
-}
-
-void Protocol::receive() {
-    uint8_t new_byte = 0;
-    uint8_t cs = 0;
-
-    _receive_timeout.reset();
-    while (MODSERIAL::rxBufferGetCount() > 0 &&  _receive_timeout.read_us() < 50) {
-        switch (_state) {
-            case PACKET_START:
-                new_byte = MODSERIAL::getc();
-                if (_last_byte == 255 && new_byte == 127) {
-                    _state = HEADER_RECEIVE;
-                } else {
-                    if (new_byte!=255) {
-                        _invalid_bytes++;
-                    }
-                }
-                _last_byte = new_byte;
-                break;
-
-            case HEADER_RECEIVE:
-                _header[_header_read++] = MODSERIAL::getc();
-                _state = HEADER_DECODE;
-                break;
-
-            case HEADER_DECODE:
-                _packet._size = _header[0];
-                _state = DATA_RECEIVE;
-                break;
-
-            case DATA_RECEIVE:
-                if (_data_read < _packet._size) {
-                    _packet._data[_data_read++] = MODSERIAL::getc();
-                    if (_data_read == _packet._size) {
-                        _state = DATA_VALIDATE;
-                    }
-                } else {
-                    _state = PACKET_RESET;
-                    _corrupt_packets++;
-                }
-                break;
-
-            case DATA_VALIDATE:
-                _packet._type = _packet._data[0];
-                _packet._checksum = MODSERIAL::getc();
-                cs = checksum(_packet._data, _packet._size);
-                if (cs == _packet._checksum) {
-                    _state = PACKET_VALID;
-                    _packet._valid = true;
-                } else {
-                    _state = PACKET_RESET;
-                    _corrupt_packets++;
-                }
-                break;
-
-            case PACKET_VALID:
-                if (!_packet._valid) {
-                    _state = PACKET_RESET;
-                } else {
-                    return;
-                }
-                return;
-
-            default:
-                _state = PACKET_START;
-                _header_read = 0;
-                _data_read = 0;
-                break;
-        }
-    }
-}
-
-uint8_t Protocol::checksum(uint8_t* packet, uint16_t packet_size) {
-    uint8_t tmp_checksum = 16;
-    for (int i = 0; i < packet_size; i++) {
-        tmp_checksum ^= packet[i];
-    }
-    return tmp_checksum;
-}
-
-//SERIAL overides
-int Protocol::printf( const char * format, ...) {
-    char buffer[256];
-    buffer[0] = '\0';
-    int status = 0;
-    
-    va_list args;
-    va_start( args, format);
-    status = vsnprintf (buffer, sizeof(buffer) - 1, format, args);
-    va_end(args);
-        
-    uint16_t i = 0;
-    while(buffer[i] != '\0' && i < sizeof(buffer)){
-        send(buffer[i]);
-        ++i;
-    }
-
-    return status;
-}
-
-int Protocol::puts( const char * str) {
-    uint16_t i = 0;
-    while(str[i] != '\0'){
-        send(str[i]);
-        ++i;
-    }
-    return i;
-}
-
-int Protocol::putc( int character){
-    send(character);
-    return 1;
-}
-
-}
\ No newline at end of file
--- a/SimpleSerialProtocol/Protocol.h	Sat Mar 17 16:42:31 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,127 +0,0 @@
-#ifndef SIMPLE_SERIAL_PROTOCOL
-#define SIMPLE_SERIAL_PROTOCOL
-
-#include <map>
-#include <vector>
-
-#include "mbed.h"
-#include "MODDMA.h"
-#include "MODSERIAL.h"
-#include "Packet.h"
-#include "FPointer.h"
-#include "Ringbuffer.h"
-
-namespace SimpleSerialProtocol {
-
-#define SEND_CHUNK_SIZE 96
-
-class Protocol : public MODSERIAL {
-public:
-    enum {
-        PACKET_START,
-        HEADER_RECEIVE,
-        HEADER_DECODE,
-        DATA_RECEIVE,
-        FOOTER_RECEIVE,
-        DATA_VALIDATE,
-        PACKET_VALID,
-        PACKET_RESET
-    };
-
-    Protocol(PinName tx, PinName rx, PinName led_pin);
-    virtual ~Protocol();
-
-    virtual void initialise() {
-        MODSERIAL::baud(115200);
-    }
-    
-    void update();
-    
-    int printf( const char * format, ...);
-    int puts( const char * str);
-    int putc( int character);
-    void send(uint8_t byte);
-    void blockUntilDmaTxEmpty();
-
-    bool packetWaiting() {
-        return _packet._valid;
-    }
-
-    uint32_t _corrupt_packets;
-    uint32_t _invalid_bytes;
-
-    template<class T> void receiveCallback(uint8_t type, T* item, void (T::*method)(Packet*)) {
-        FPointer callback;
-        callback.attach(item, (void (T::*)(Packet*))method);
-        _callback[type].push_back(callback);
-    }
-
-    template<class T> void transmitCallback(float frequency, T* item, void (T::*method)(Packet*)) {
-        FPointer callback;
-        callback.attach(item, (void (T::*)(Packet*))method);
-        _transmit_callback.push_back(TimerData(frequency, callback));
-    }
-
-protected:
-    virtual void sendPacket(Packet* packet) {
-        if (packet!=0) {
-            send(255);
-            send(127);
-            send(packet->_size);
-            for (int i = 0; i < packet->_size; i++) {
-                send(packet->_data[i]);
-            }
-            send(checksum(packet->_data,  packet->_size));
-        }
-    }
-
-    DigitalOut _receive_led;
-    Packet _packet;
-    Packet _packet_transmit;
-    uint8_t _dma_port;
-    Timer _receive_timeout;
-    Timer _throttle_transfer;
-    
-    void send();
-    virtual void receive();
-    static uint8_t checksum(uint8_t* packet, uint16_t packet_size);
-
-    void dmaCallback(MODSERIAL_IRQ_INFO *q);
-
-    struct TimerData {
-        TimerData(float frequency, FPointer callback) {
-            _timer.start();
-            _frequency = frequency;
-            _callback = callback;
-        }
-        bool trigger() {
-            if (_timer.read_us() > _frequency) {
-                _timer.reset();
-                return true;
-            }
-            return false;
-        }
-        Timer _timer;
-        float _frequency;
-        FPointer _callback;
-    };
-
-    uint8_t _header[2];
-    uint8_t _header_read;
-    uint8_t _data_read;
-    uint8_t _footer[4];
-    uint8_t _footer_read;
-    uint8_t _state;
-    uint8_t _last_byte;
-
-    bool _transfer_in_progress;
-    uint8_t _dma_buffer[SEND_CHUNK_SIZE];
-
-    RingBuffer _send_buffer;
-    std::map<uint8_t, std::vector<FPointer> > _callback;
-    std::vector<TimerData> _transmit_callback;
-};
-
-}
-
-#endif
\ No newline at end of file
--- a/SimpleSerialProtocol/Ringbuffer.h	Sat Mar 17 16:42:31 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,61 +0,0 @@
-#ifndef RINGBUFFER
-#define RINGBUFFER
-namespace SimpleSerialProtocol {
-
-class RingBuffer {
-public:
-    RingBuffer() {
-        _read_index = 0;
-        _write_index = 0;
-        _size = 256;
-        _buffer = new uint8_t[_size];
-    }
-    RingBuffer(uint16_t capacity) {
-        _read_index = 0;
-        _write_index = 0;
-        _size = capacity;
-        _buffer = new uint8_t[_size];
-    }
-    ~RingBuffer() {
-        delete[] _buffer;
-    }
-
-    uint16_t size() {
-        return _size;
-    }
-
-    uint16_t available() {
-        if (_write_index < _read_index) {
-            return (_write_index + size()) - _read_index;
-        } else {
-            return _write_index - _read_index;
-        }
-    }
-
-    bool write(uint8_t byte) {
-        if(available() < (size() -1) ){
-            _buffer[_write_index++] = byte;
-            _write_index %= size();
-            return true;
-        }
-        return false;
-    }
-
-    uint8_t read() {
-        if(available()){
-            uint8_t value = _buffer[_read_index++];
-            _read_index %= size();
-            return value;
-        }
-        return 0;
-    }
-
-private:
-    uint16_t _read_index;
-    uint16_t _write_index;
-    uint8_t* _buffer;
-    uint16_t _size;
-};
-
-}
-#endif
\ No newline at end of file
--- a/TestProtocol.h	Sat Mar 17 16:42:31 2012 +0000
+++ b/TestProtocol.h	Sun Jul 29 19:31:31 2012 +0000
@@ -8,7 +8,6 @@
 class TestProtocol : public SimpleSerialProtocol::Protocol {
 public:
     TestProtocol() : Protocol(USBTX, USBRX, LED1) { //LED1 to 4 for a status led, NC to disable
-        _dma_port = MODDMA::Channel_7; //set the dma port, must be unique per class 0 - 9
         receiveCallback(1, this, &TestProtocol::onEchoPacket);
     }
     virtual ~TestProtocol() {};
--- a/main.cpp	Sat Mar 17 16:42:31 2012 +0000
+++ b/main.cpp	Sun Jul 29 19:31:31 2012 +0000
@@ -17,15 +17,16 @@
 //    }
 //    return tmp_checksum;
 
-Timer timer;
+Serial debug(p28, p27);
+
 TestProtocol testProtocol;
-MODDMA dma;
 
 //the main loop
 int main() {
-    timer.start();
     testProtocol.initialise();
-    testProtocol.MODDMA(&dma);
+    
+    debug.baud(115200);
+    debug.printf("Debug Console:\r\n");
 
     testProtocol.printf("Hello printf\r\n");
     testProtocol.printf("Hello printf %s\r\n", "booyaa");
@@ -35,28 +36,8 @@
     testProtocol.puts("\r\n");
 
     testProtocol.puts("SimpleSerialProtocol Serial io overrides tested\r\n");
-
-    for (int i = 0; i < 20; ++i) {
-        testProtocol.puts("*dma*");
-    }
-    testProtocol.puts("\r\n");
-
-    testProtocol.blockUntilDmaTxEmpty();
-
-
+  
     while (1) {
         testProtocol.update();
-
-        for (int i = 0; i < 120; ++i) {
-            testProtocol.puts("*dma*");
-        }
-        testProtocol.puts("\r\n");
-        Timer timer;
-        timer.start();
-        testProtocol.blockUntilDmaTxEmpty();
-        
-        testProtocol.printf("Serial write time: %d\r\n", timer.read_ms());
-        
-        wait_ms(500);
     }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sun Jul 29 19:31:31 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/10b9abbe79a6
\ No newline at end of file
--- a/mbed.lib	Sat Mar 17 16:42:31 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/projects/libraries/svn/mbed/trunk@43
\ No newline at end of file