test

Dependencies:   mbed Watchdog stm32-sensor-base2

Committer:
ruslanbredun
Date:
Fri Nov 05 14:39:08 2021 +0000
Revision:
19:7935ca386e13
Parent:
11:32eeb052cda5
LastFirmWare05/11/21;

Who changed what in which revision?

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