Proyecto ABInBev para la tarjeta Guaria 1/2.

Committer:
fmanzano_dtk
Date:
Thu Jul 07 08:56:13 2022 -0600
Revision:
18:522f706f03b1
Parent:
1:9e821e640117
Actualizacion

Who changed what in which revision?

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