tetete

Dependencies:   FIFO

Committer:
kanope_benny
Date:
Thu Jul 23 08:45:42 2020 +0000
Revision:
16:96935ea8f8b0
Parent:
15:278f7f125495
tetete

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