tetete

Dependencies:   FIFO

Committer:
babylonica
Date:
Thu Mar 30 02:18:24 2017 +0000
Revision:
0:907ac3c2fadc
Child:
1:d3af33dfc87d
First Commit.

Who changed what in which revision?

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