Dependencies:   ConfigFile SDFileSystem mbed

Fork of LAURUS_program by hiroya taura

Committer:
onaka
Date:
Fri Jun 12 04:00:23 2015 +0000
Revision:
6:2b68f85a984a
Child:
28:d993f3bbe302
logger update

Who changed what in which revision?

UserRevisionLine numberNew contents of line
onaka 6:2b68f85a984a 1
onaka 6:2b68f85a984a 2 /**
onaka 6:2b68f85a984a 3 * @file BufferedSerial.h
onaka 6:2b68f85a984a 4 * @brief Software Buffer - Extends mbed Serial functionallity adding irq driven TX and RX
onaka 6:2b68f85a984a 5 * @author sam grove
onaka 6:2b68f85a984a 6 * @version 1.0
onaka 6:2b68f85a984a 7 * @see
onaka 6:2b68f85a984a 8 *
onaka 6:2b68f85a984a 9 * Copyright (c) 2013
onaka 6:2b68f85a984a 10 *
onaka 6:2b68f85a984a 11 * Licensed under the Apache License, Version 2.0 (the "License");
onaka 6:2b68f85a984a 12 * you may not use this file except in compliance with the License.
onaka 6:2b68f85a984a 13 * You may obtain a copy of the License at
onaka 6:2b68f85a984a 14 *
onaka 6:2b68f85a984a 15 * http://www.apache.org/licenses/LICENSE-2.0
onaka 6:2b68f85a984a 16 *
onaka 6:2b68f85a984a 17 * Unless required by applicable law or agreed to in writing, software
onaka 6:2b68f85a984a 18 * distributed under the License is distributed on an "AS IS" BASIS,
onaka 6:2b68f85a984a 19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
onaka 6:2b68f85a984a 20 * See the License for the specific language governing permissions and
onaka 6:2b68f85a984a 21 * limitations under the License.
onaka 6:2b68f85a984a 22 */
onaka 6:2b68f85a984a 23
onaka 6:2b68f85a984a 24 #ifndef BUFFEREDSERIAL_H
onaka 6:2b68f85a984a 25 #define BUFFEREDSERIAL_H
onaka 6:2b68f85a984a 26
onaka 6:2b68f85a984a 27 #include "mbed.h"
onaka 6:2b68f85a984a 28 #include "Buffer.h"
onaka 6:2b68f85a984a 29
onaka 6:2b68f85a984a 30 // Base Class
onaka 6:2b68f85a984a 31 #define SERIAL_BASE RawSerial
onaka 6:2b68f85a984a 32
onaka 6:2b68f85a984a 33 /** A serial port (UART) for communication with other serial devices
onaka 6:2b68f85a984a 34 *
onaka 6:2b68f85a984a 35 * Can be used for Full Duplex communication, or Simplex by specifying
onaka 6:2b68f85a984a 36 * one pin as NC (Not Connected)
onaka 6:2b68f85a984a 37 *
onaka 6:2b68f85a984a 38 * Example:
onaka 6:2b68f85a984a 39 * @code
onaka 6:2b68f85a984a 40 * #include "mbed.h"
onaka 6:2b68f85a984a 41 * #include "BufferedSerial.h"
onaka 6:2b68f85a984a 42 *
onaka 6:2b68f85a984a 43 * BufferedSerial pc(USBTX, USBRX);
onaka 6:2b68f85a984a 44 *
onaka 6:2b68f85a984a 45 * int main()
onaka 6:2b68f85a984a 46 * {
onaka 6:2b68f85a984a 47 * while(1)
onaka 6:2b68f85a984a 48 * {
onaka 6:2b68f85a984a 49 * Timer s;
onaka 6:2b68f85a984a 50 *
onaka 6:2b68f85a984a 51 * s.start();
onaka 6:2b68f85a984a 52 * pc.printf("Hello World - buffered\n");
onaka 6:2b68f85a984a 53 * int buffered_time = s.read_us();
onaka 6:2b68f85a984a 54 * wait(0.1f); // give time for the buffer to empty
onaka 6:2b68f85a984a 55 *
onaka 6:2b68f85a984a 56 * s.reset();
onaka 6:2b68f85a984a 57 * printf("Hello World - blocking\n");
onaka 6:2b68f85a984a 58 * int polled_time = s.read_us();
onaka 6:2b68f85a984a 59 * s.stop();
onaka 6:2b68f85a984a 60 * wait(0.1f); // give time for the buffer to empty
onaka 6:2b68f85a984a 61 *
onaka 6:2b68f85a984a 62 * pc.printf("printf buffered took %d us\n", buffered_time);
onaka 6:2b68f85a984a 63 * pc.printf("printf blocking took %d us\n", polled_time);
onaka 6:2b68f85a984a 64 * wait(0.5f);
onaka 6:2b68f85a984a 65 * }
onaka 6:2b68f85a984a 66 * }
onaka 6:2b68f85a984a 67 * @endcode
onaka 6:2b68f85a984a 68 */
onaka 6:2b68f85a984a 69
onaka 6:2b68f85a984a 70 /**
onaka 6:2b68f85a984a 71 * @class BufferedSerial
onaka 6:2b68f85a984a 72 * @brief Software buffers and interrupt driven tx and rx for Serial
onaka 6:2b68f85a984a 73 */
onaka 6:2b68f85a984a 74 class BufferedSerial : public SERIAL_BASE
onaka 6:2b68f85a984a 75 {
onaka 6:2b68f85a984a 76 private:
onaka 6:2b68f85a984a 77 InterruptIn _cts;
onaka 6:2b68f85a984a 78 Buffer <char> _rxbuf;
onaka 6:2b68f85a984a 79 Buffer <char> _txbuf;
onaka 6:2b68f85a984a 80 uint32_t _buf_size;
onaka 6:2b68f85a984a 81 uint32_t _tx_multiple;
onaka 6:2b68f85a984a 82
onaka 6:2b68f85a984a 83 void rxIrq(void);
onaka 6:2b68f85a984a 84 void txIrq(void);
onaka 6:2b68f85a984a 85 void prime(void);
onaka 6:2b68f85a984a 86
onaka 6:2b68f85a984a 87 void ctsInterrupt(void);
onaka 6:2b68f85a984a 88
onaka 6:2b68f85a984a 89 public:
onaka 6:2b68f85a984a 90 /** Create a BufferedSerial port, connected to the specified transmit and receive pins
onaka 6:2b68f85a984a 91 * @param tx Transmit pin
onaka 6:2b68f85a984a 92 * @param rx Receive pin
onaka 6:2b68f85a984a 93 * @param cts CTS pin
onaka 6:2b68f85a984a 94 * @param buf_size printf() buffer size
onaka 6:2b68f85a984a 95 * @param tx_multiple amount of max printf() present in the internal ring buffer at one time
onaka 6:2b68f85a984a 96 * @param name optional name
onaka 6:2b68f85a984a 97 * @note Either tx or rx may be specified as NC if unused
onaka 6:2b68f85a984a 98 */
onaka 6:2b68f85a984a 99 BufferedSerial(PinName tx, PinName rx, PinName cts, uint32_t buf_size = 256, uint32_t tx_multiple = 4,const char* name=NULL);
onaka 6:2b68f85a984a 100
onaka 6:2b68f85a984a 101 /** Destroy a BufferedSerial port
onaka 6:2b68f85a984a 102 */
onaka 6:2b68f85a984a 103 virtual ~BufferedSerial(void);
onaka 6:2b68f85a984a 104
onaka 6:2b68f85a984a 105 /** Check on how many bytes are in the rx buffer
onaka 6:2b68f85a984a 106 * @return 1 if something exists, 0 otherwise
onaka 6:2b68f85a984a 107 */
onaka 6:2b68f85a984a 108 virtual int readable(void);
onaka 6:2b68f85a984a 109
onaka 6:2b68f85a984a 110 /** Check to see if the tx buffer has room
onaka 6:2b68f85a984a 111 * @return 1 always has room and can overwrite previous content if too small / slow
onaka 6:2b68f85a984a 112 */
onaka 6:2b68f85a984a 113 virtual int writeable(void);
onaka 6:2b68f85a984a 114
onaka 6:2b68f85a984a 115 /** Get a single byte from the BufferedSerial Port.
onaka 6:2b68f85a984a 116 * Should check readable() before calling this.
onaka 6:2b68f85a984a 117 * @return A byte that came in on the Serial Port
onaka 6:2b68f85a984a 118 */
onaka 6:2b68f85a984a 119 virtual int getc(void);
onaka 6:2b68f85a984a 120
onaka 6:2b68f85a984a 121 /** Write a single byte to the BufferedSerial Port.
onaka 6:2b68f85a984a 122 * @param c The byte to write to the Serial Port
onaka 6:2b68f85a984a 123 * @return The byte that was written to the Serial Port Buffer
onaka 6:2b68f85a984a 124 */
onaka 6:2b68f85a984a 125 virtual int putc(int c);
onaka 6:2b68f85a984a 126
onaka 6:2b68f85a984a 127 /** Write a string to the BufferedSerial Port. Must be NULL terminated
onaka 6:2b68f85a984a 128 * @param s The string to write to the Serial Port
onaka 6:2b68f85a984a 129 * @return The number of bytes written to the Serial Port Buffer
onaka 6:2b68f85a984a 130 */
onaka 6:2b68f85a984a 131 virtual int puts(const char *s);
onaka 6:2b68f85a984a 132
onaka 6:2b68f85a984a 133 /** Write a formatted string to the BufferedSerial Port.
onaka 6:2b68f85a984a 134 * @param format The string + format specifiers to write to the Serial Port
onaka 6:2b68f85a984a 135 * @return The number of bytes written to the Serial Port Buffer
onaka 6:2b68f85a984a 136 */
onaka 6:2b68f85a984a 137 virtual int printf(const char* format, ...);
onaka 6:2b68f85a984a 138
onaka 6:2b68f85a984a 139 /** Write data to the Buffered Serial Port
onaka 6:2b68f85a984a 140 * @param s A pointer to data to send
onaka 6:2b68f85a984a 141 * @param length The amount of data being pointed to
onaka 6:2b68f85a984a 142 * @return The number of bytes written to the Serial Port Buffer
onaka 6:2b68f85a984a 143 */
onaka 6:2b68f85a984a 144 virtual ssize_t write(const void *s, std::size_t length);
onaka 6:2b68f85a984a 145 };
onaka 6:2b68f85a984a 146
onaka 6:2b68f85a984a 147 #endif