Asynchronous (Non-blocking) Serial Communication library with variable length software ring buffer (FIFO). You can use primary method of the RawSerial Library. Operability confirmed on mbed 2.0. (Rev.146)

Dependencies:   FIFO

Dependents:   InvertedPendulum2017 SerialConnect mbed_2018 mbed_2019_rx3 ... more

Committer:
babylonica
Date:
Sat Jun 17 11:44:36 2017 +0000
Revision:
8:7390f9bb28d3
Parent:
5:a50cdaa90d6a
Child:
9:709719dddd3e
FIFO Buffer Update.; Bug Fixed.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
babylonica 5:a50cdaa90d6a 1 // -*- coding: utf-8 -*-
babylonica 5:a50cdaa90d6a 2 /**
babylonica 5:a50cdaa90d6a 3 @file AsyncSerial.hpp
babylonica 5:a50cdaa90d6a 4 @brief Asynchronous (Non-brocking) Serial Communication library with variable length software ring buffer (FIFO). You can use also RawSerial Library's method. You can set the baudrate of the serial communication when instantiating.
babylonica 5:a50cdaa90d6a 5
babylonica 5:a50cdaa90d6a 6 @author T.Kawamura
babylonica 8:7390f9bb28d3 7 @version 1.2
babylonica 5:a50cdaa90d6a 8 @date 2017-03-29 T.Kawamura Written for C++/mbed.
babylonica 5:a50cdaa90d6a 9 @date 2017-03-30 T.Kawamura Bug Fixed: Cannot use format(), baud().
babylonica 8:7390f9bb28d3 10 @date 2017-06-17 T.Kawamura Update: FIFO Buffer Fixed.
babylonica 5:a50cdaa90d6a 11 @see
babylonica 5:a50cdaa90d6a 12 Copyright (C) 2017 T.Kawamura.
babylonica 5:a50cdaa90d6a 13 Released under the MIT license.
babylonica 5:a50cdaa90d6a 14 http://opensource.org/licenses/mit-license.php
babylonica 5:a50cdaa90d6a 15
babylonica 5:a50cdaa90d6a 16 */
babylonica 5:a50cdaa90d6a 17
babylonica 5:a50cdaa90d6a 18 #ifndef ASYNCSERIAL_H
babylonica 5:a50cdaa90d6a 19 #define ASYNCSERIAL_H
babylonica 5:a50cdaa90d6a 20
babylonica 5:a50cdaa90d6a 21 #include "mbed.h"
babylonica 5:a50cdaa90d6a 22 #include "FIFO.hpp"
babylonica 5:a50cdaa90d6a 23 #include <stdarg.h>
babylonica 5:a50cdaa90d6a 24 #include <string.h>
babylonica 5:a50cdaa90d6a 25
babylonica 5:a50cdaa90d6a 26 using namespace std;
babylonica 5:a50cdaa90d6a 27
babylonica 5:a50cdaa90d6a 28 #define PRINTF_STRING_BUFFER_SIZE 256
babylonica 5:a50cdaa90d6a 29
babylonica 5:a50cdaa90d6a 30 /**
babylonica 5:a50cdaa90d6a 31 @class AsyncSerial
babylonica 5:a50cdaa90d6a 32 @brief Asynchronous Serial Communication with FIFO
babylonica 5:a50cdaa90d6a 33 */
babylonica 5:a50cdaa90d6a 34 class AsyncSerial : public RawSerial{
babylonica 5:a50cdaa90d6a 35 private:
babylonica 5:a50cdaa90d6a 36
babylonica 5:a50cdaa90d6a 37 // FIFO is uint8_t(unsigned char) buffer;
babylonica 5:a50cdaa90d6a 38 FIFO<uint8_t> fifo_tx;
babylonica 5:a50cdaa90d6a 39 FIFO<uint8_t> fifo_rx;
babylonica 5:a50cdaa90d6a 40
babylonica 5:a50cdaa90d6a 41 bool Is_Serial_Sending;
babylonica 5:a50cdaa90d6a 42
babylonica 5:a50cdaa90d6a 43 void ISR_TX(void);
babylonica 5:a50cdaa90d6a 44 void ISR_RX(void);
babylonica 5:a50cdaa90d6a 45
babylonica 5:a50cdaa90d6a 46 public:
babylonica 5:a50cdaa90d6a 47
babylonica 5:a50cdaa90d6a 48 /**
babylonica 5:a50cdaa90d6a 49 @brief Create a new AsyncSerial Port.
babylonica 5:a50cdaa90d6a 50 @param txpin Tx pin name (Defined in PinName.h)
babylonica 5:a50cdaa90d6a 51 @param rxpin Rx pin name (Defined in PinName.h)
babylonica 5:a50cdaa90d6a 52 @param baudrate Baudrate (ex: 115200). Default value is 9600.
babylonica 5:a50cdaa90d6a 53 @param buffer_size Buffer size. Default value is 256. (byte)
babylonica 5:a50cdaa90d6a 54 */
babylonica 5:a50cdaa90d6a 55 AsyncSerial(PinName txpin, PinName rxpin, uint32_t baudrate=9600, uint32_t buffer_size=256);
babylonica 5:a50cdaa90d6a 56 /**
babylonica 5:a50cdaa90d6a 57 @brief Disable the AsyncSerial Port.
babylonica 5:a50cdaa90d6a 58 @param No parameters.
babylonica 5:a50cdaa90d6a 59 */
babylonica 5:a50cdaa90d6a 60 virtual ~AsyncSerial(void);
babylonica 5:a50cdaa90d6a 61
babylonica 5:a50cdaa90d6a 62 /**
babylonica 5:a50cdaa90d6a 63 @brief Get how many bytes are in the rx buffer.
babylonica 5:a50cdaa90d6a 64 @param No parameters.
babylonica 5:a50cdaa90d6a 65 @return Size of readable data. (byte)
babylonica 5:a50cdaa90d6a 66 */
babylonica 5:a50cdaa90d6a 67 virtual int readable(void);
babylonica 5:a50cdaa90d6a 68
babylonica 5:a50cdaa90d6a 69 /**
babylonica 5:a50cdaa90d6a 70 @brief Check writeable or not.
babylonica 5:a50cdaa90d6a 71 @param No Parameters.
babylonica 5:a50cdaa90d6a 72 @retval 1 Always return 1 because this library provides asynchronous serial.
babylonica 5:a50cdaa90d6a 73 */
babylonica 5:a50cdaa90d6a 74 virtual int writeable(void);
babylonica 0:907ac3c2fadc 75
babylonica 5:a50cdaa90d6a 76 /**
babylonica 5:a50cdaa90d6a 77 @brief Get 1byte from the AsyncSerial port.
babylonica 5:a50cdaa90d6a 78 @param No parameters.
babylonica 5:a50cdaa90d6a 79 @retval All Got Data
babylonica 5:a50cdaa90d6a 80 @retval 0 Error.
babylonica 5:a50cdaa90d6a 81 */
babylonica 5:a50cdaa90d6a 82 virtual int getc(void);
babylonica 5:a50cdaa90d6a 83
babylonica 5:a50cdaa90d6a 84 /**
babylonica 5:a50cdaa90d6a 85 @brief Peek 1byte from the AsyncSerial port.
babylonica 5:a50cdaa90d6a 86 @param No parameters.
babylonica 5:a50cdaa90d6a 87 @retval ALL Got Data
babylonica 5:a50cdaa90d6a 88 @retval 0 Error.
babylonica 5:a50cdaa90d6a 89 */
babylonica 5:a50cdaa90d6a 90 virtual int peekc(void);
babylonica 5:a50cdaa90d6a 91
babylonica 5:a50cdaa90d6a 92 /**
babylonica 5:a50cdaa90d6a 93 @brief Put 1byte to the AsyncSerial port.
babylonica 5:a50cdaa90d6a 94 @param data A Data for put
babylonica 8:7390f9bb28d3 95 @return Nothing.
babylonica 5:a50cdaa90d6a 96 */
babylonica 8:7390f9bb28d3 97 virtual void putc(int c);
babylonica 5:a50cdaa90d6a 98
babylonica 5:a50cdaa90d6a 99 /**
babylonica 5:a50cdaa90d6a 100 @brief Write a string with new line. The string must be NULL terminated.
babylonica 5:a50cdaa90d6a 101 @param *str A String for write (Must be NULL terminated).
babylonica 8:7390f9bb28d3 102 @return Nothing.
babylonica 5:a50cdaa90d6a 103 */
babylonica 8:7390f9bb28d3 104 virtual void puts(const char *str);
babylonica 5:a50cdaa90d6a 105
babylonica 5:a50cdaa90d6a 106 /**
babylonica 5:a50cdaa90d6a 107 @brief Write a formatted string to the AsyncSerial port.
babylonica 5:a50cdaa90d6a 108 @param *format A Formatted string for write.
babylonica 5:a50cdaa90d6a 109 @retval 0 Error.
babylonica 5:a50cdaa90d6a 110 @retval 1+ Wrote string size (byte).
babylonica 5:a50cdaa90d6a 111 */
babylonica 5:a50cdaa90d6a 112 virtual int printf(const char *format, ...);
babylonica 5:a50cdaa90d6a 113
babylonica 5:a50cdaa90d6a 114 /**
babylonica 5:a50cdaa90d6a 115 @brief Write byte array to the AsyncSerial port.
babylonica 5:a50cdaa90d6a 116 @param *s A pointer to the array for write.
babylonica 5:a50cdaa90d6a 117 @param length Write size (byte).
babylonica 5:a50cdaa90d6a 118 @retval 0 Error.
babylonica 5:a50cdaa90d6a 119 @retval 1 Success.
babylonica 5:a50cdaa90d6a 120 */
babylonica 5:a50cdaa90d6a 121 virtual int write(const uint8_t *buffer, int length);
babylonica 5:a50cdaa90d6a 122
babylonica 5:a50cdaa90d6a 123 /**
babylonica 5:a50cdaa90d6a 124 @brief Clear the rx buffer by compulsion.
babylonica 5:a50cdaa90d6a 125 @param No parameters.
babylonica 5:a50cdaa90d6a 126 */
babylonica 5:a50cdaa90d6a 127 virtual void flush(void);
babylonica 5:a50cdaa90d6a 128
babylonica 5:a50cdaa90d6a 129 /**
babylonica 5:a50cdaa90d6a 130 @brief Wait until finish all sending.
babylonica 5:a50cdaa90d6a 131 @param No parameters.
babylonica 5:a50cdaa90d6a 132 */
babylonica 5:a50cdaa90d6a 133 virtual void wait(void);
babylonica 5:a50cdaa90d6a 134
babylonica 5:a50cdaa90d6a 135 /**
babylonica 5:a50cdaa90d6a 136 @brief Set bits, parity and stop bits.
babylonica 5:a50cdaa90d6a 137 @param bits Bits (5 ~ 8)
babylonica 5:a50cdaa90d6a 138 @param parity Parity
babylonica 5:a50cdaa90d6a 139 @param stop_bits Stop bits (1 or 2)
babylonica 5:a50cdaa90d6a 140 */
babylonica 5:a50cdaa90d6a 141 virtual void format(int bits=8, Parity parity=SerialBase::None, int stop_bits=1);
babylonica 5:a50cdaa90d6a 142
babylonica 5:a50cdaa90d6a 143 /**
babylonica 5:a50cdaa90d6a 144 @brief Set baud rate.
babylonica 5:a50cdaa90d6a 145 @param baudrate baudrate (bps).
babylonica 5:a50cdaa90d6a 146 */
babylonica 5:a50cdaa90d6a 147 virtual void baud(int baudrate);
babylonica 5:a50cdaa90d6a 148
babylonica 5:a50cdaa90d6a 149 };
babylonica 5:a50cdaa90d6a 150
babylonica 5:a50cdaa90d6a 151 #endif
babylonica 5:a50cdaa90d6a 152