Base station firmware

Committer:
Perijah
Date:
Tue May 24 13:16:55 2016 +0000
Revision:
0:ecc3925d3f8c
Base station firmware

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Perijah 0:ecc3925d3f8c 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
Perijah 0:ecc3925d3f8c 2 *
Perijah 0:ecc3925d3f8c 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
Perijah 0:ecc3925d3f8c 4 * and associated documentation files (the "Software"), to deal in the Software without
Perijah 0:ecc3925d3f8c 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
Perijah 0:ecc3925d3f8c 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
Perijah 0:ecc3925d3f8c 7 * Software is furnished to do so, subject to the following conditions:
Perijah 0:ecc3925d3f8c 8 *
Perijah 0:ecc3925d3f8c 9 * The above copyright notice and this permission notice shall be included in all copies or
Perijah 0:ecc3925d3f8c 10 * substantial portions of the Software.
Perijah 0:ecc3925d3f8c 11 *
Perijah 0:ecc3925d3f8c 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
Perijah 0:ecc3925d3f8c 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Perijah 0:ecc3925d3f8c 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
Perijah 0:ecc3925d3f8c 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Perijah 0:ecc3925d3f8c 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Perijah 0:ecc3925d3f8c 17 */
Perijah 0:ecc3925d3f8c 18
Perijah 0:ecc3925d3f8c 19 #ifndef USBSERIAL_H
Perijah 0:ecc3925d3f8c 20 #define USBSERIAL_H
Perijah 0:ecc3925d3f8c 21
Perijah 0:ecc3925d3f8c 22 #include "USBCDC.h"
Perijah 0:ecc3925d3f8c 23 #include "Stream.h"
Perijah 0:ecc3925d3f8c 24 #include "CircBuffer.h"
Perijah 0:ecc3925d3f8c 25
Perijah 0:ecc3925d3f8c 26
Perijah 0:ecc3925d3f8c 27 /**
Perijah 0:ecc3925d3f8c 28 * USBSerial example
Perijah 0:ecc3925d3f8c 29 *
Perijah 0:ecc3925d3f8c 30 * @code
Perijah 0:ecc3925d3f8c 31 * #include "mbed.h"
Perijah 0:ecc3925d3f8c 32 * #include "USBSerial.h"
Perijah 0:ecc3925d3f8c 33 *
Perijah 0:ecc3925d3f8c 34 * //Virtual serial port over USB
Perijah 0:ecc3925d3f8c 35 * USBSerial serial;
Perijah 0:ecc3925d3f8c 36 *
Perijah 0:ecc3925d3f8c 37 * int main(void) {
Perijah 0:ecc3925d3f8c 38 *
Perijah 0:ecc3925d3f8c 39 * while(1)
Perijah 0:ecc3925d3f8c 40 * {
Perijah 0:ecc3925d3f8c 41 * serial.printf("I am a virtual serial port\n");
Perijah 0:ecc3925d3f8c 42 * wait(1);
Perijah 0:ecc3925d3f8c 43 * }
Perijah 0:ecc3925d3f8c 44 * }
Perijah 0:ecc3925d3f8c 45 * @endcode
Perijah 0:ecc3925d3f8c 46 */
Perijah 0:ecc3925d3f8c 47 class USBSerial: public USBCDC, public Stream {
Perijah 0:ecc3925d3f8c 48 public:
Perijah 0:ecc3925d3f8c 49
Perijah 0:ecc3925d3f8c 50 /**
Perijah 0:ecc3925d3f8c 51 * Constructor
Perijah 0:ecc3925d3f8c 52 *
Perijah 0:ecc3925d3f8c 53 * @param vendor_id Your vendor_id (default: 0x1f00)
Perijah 0:ecc3925d3f8c 54 * @param product_id Your product_id (default: 0x2012)
Perijah 0:ecc3925d3f8c 55 * @param product_release Your preoduct_release (default: 0x0001)
Perijah 0:ecc3925d3f8c 56 *
Perijah 0:ecc3925d3f8c 57 */
Perijah 0:ecc3925d3f8c 58 USBSerial(uint16_t vendor_id = 0x1f00, uint16_t product_id = 0x2012, uint16_t product_release = 0x0001): USBCDC(vendor_id, product_id, product_release), buf(128){ };
Perijah 0:ecc3925d3f8c 59
Perijah 0:ecc3925d3f8c 60
Perijah 0:ecc3925d3f8c 61 /**
Perijah 0:ecc3925d3f8c 62 * Send a character. You can use puts, printf.
Perijah 0:ecc3925d3f8c 63 *
Perijah 0:ecc3925d3f8c 64 * @param c character to be sent
Perijah 0:ecc3925d3f8c 65 * @returns true if there is no error, false otherwise
Perijah 0:ecc3925d3f8c 66 */
Perijah 0:ecc3925d3f8c 67 virtual int _putc(int c);
Perijah 0:ecc3925d3f8c 68
Perijah 0:ecc3925d3f8c 69 /**
Perijah 0:ecc3925d3f8c 70 * Read a character: blocking
Perijah 0:ecc3925d3f8c 71 *
Perijah 0:ecc3925d3f8c 72 * @returns character read
Perijah 0:ecc3925d3f8c 73 */
Perijah 0:ecc3925d3f8c 74 virtual int _getc();
Perijah 0:ecc3925d3f8c 75
Perijah 0:ecc3925d3f8c 76 /**
Perijah 0:ecc3925d3f8c 77 * Check the number of bytes available.
Perijah 0:ecc3925d3f8c 78 *
Perijah 0:ecc3925d3f8c 79 * @returns the number of bytes available
Perijah 0:ecc3925d3f8c 80 */
Perijah 0:ecc3925d3f8c 81 uint8_t available();
Perijah 0:ecc3925d3f8c 82
Perijah 0:ecc3925d3f8c 83 /**
Perijah 0:ecc3925d3f8c 84 * Write a block of data.
Perijah 0:ecc3925d3f8c 85 *
Perijah 0:ecc3925d3f8c 86 * For more efficiency, a block of size 64 (maximum size of a bulk endpoint) has to be written.
Perijah 0:ecc3925d3f8c 87 *
Perijah 0:ecc3925d3f8c 88 * @param buf pointer on data which will be written
Perijah 0:ecc3925d3f8c 89 * @param size size of the buffer. The maximum size of a block is limited by the size of the endpoint (64 bytes)
Perijah 0:ecc3925d3f8c 90 *
Perijah 0:ecc3925d3f8c 91 * @returns true if successfull
Perijah 0:ecc3925d3f8c 92 */
Perijah 0:ecc3925d3f8c 93 bool writeBlock(uint8_t * buf, uint16_t size);
Perijah 0:ecc3925d3f8c 94
Perijah 0:ecc3925d3f8c 95 /**
Perijah 0:ecc3925d3f8c 96 * Attach a member function to call when a packet is received.
Perijah 0:ecc3925d3f8c 97 *
Perijah 0:ecc3925d3f8c 98 * @param tptr pointer to the object to call the member function on
Perijah 0:ecc3925d3f8c 99 * @param mptr pointer to the member function to be called
Perijah 0:ecc3925d3f8c 100 */
Perijah 0:ecc3925d3f8c 101 template<typename T>
Perijah 0:ecc3925d3f8c 102 void attach(T* tptr, void (T::*mptr)(void)) {
Perijah 0:ecc3925d3f8c 103 if((mptr != NULL) && (tptr != NULL)) {
Perijah 0:ecc3925d3f8c 104 rx.attach(tptr, mptr);
Perijah 0:ecc3925d3f8c 105 }
Perijah 0:ecc3925d3f8c 106 }
Perijah 0:ecc3925d3f8c 107
Perijah 0:ecc3925d3f8c 108 /**
Perijah 0:ecc3925d3f8c 109 * Attach a callback called when a packet is received
Perijah 0:ecc3925d3f8c 110 *
Perijah 0:ecc3925d3f8c 111 * @param fptr function pointer
Perijah 0:ecc3925d3f8c 112 */
Perijah 0:ecc3925d3f8c 113 void attach(void (*fn)(void)) {
Perijah 0:ecc3925d3f8c 114 if(fn != NULL) {
Perijah 0:ecc3925d3f8c 115 rx.attach(fn);
Perijah 0:ecc3925d3f8c 116 }
Perijah 0:ecc3925d3f8c 117 }
Perijah 0:ecc3925d3f8c 118
Perijah 0:ecc3925d3f8c 119
Perijah 0:ecc3925d3f8c 120 protected:
Perijah 0:ecc3925d3f8c 121 virtual bool EP2_OUT_callback();
Perijah 0:ecc3925d3f8c 122
Perijah 0:ecc3925d3f8c 123 private:
Perijah 0:ecc3925d3f8c 124 FunctionPointer rx;
Perijah 0:ecc3925d3f8c 125 CircBuffer<uint8_t> buf;
Perijah 0:ecc3925d3f8c 126 };
Perijah 0:ecc3925d3f8c 127
Perijah 0:ecc3925d3f8c 128 #endif