tetete

Dependencies:   FIFO

Committer:
babylonica
Date:
Tue Aug 22 07:30:07 2017 +0000
Revision:
10:1baa8e833ac6
Parent:
9:709719dddd3e
Child:
12:bfe3984fb2e5
AsyncSerial is RawSerial 2 Async has Raw.

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