Forked from LibPN532

Dependents:   NFC_Secure_Access NFC_Secure_Access

Fork of LibPN532 by dotnfc Tang

Committer:
udareaniket
Date:
Sun Apr 22 23:29:20 2018 +0000
Revision:
2:9a2ab3fa7862
Parent:
0:db8030e71f55
Initial commit;

Who changed what in which revision?

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