FRDM K64F Metronome

Committer:
ram54288
Date:
Sun May 14 18:37:05 2017 +0000
Revision:
0:dbad57390bd1
Initial commit

Who changed what in which revision?

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