Library for Bluetooth Low Energy Module ble 4.0 HM-11

Committer:
igbt6
Date:
Sat Dec 26 19:40:40 2015 +0000
Revision:
5:9a00e7bb0275
first commit of the new lib

Who changed what in which revision?

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