Main Code

Dependencies:   DRV8833 PidControllerV3 mbed Buffer

Fork of ApexPID by James Batchelar

Committer:
batchee7
Date:
Mon May 07 05:20:37 2018 +0000
Revision:
0:95384d72794f
IntialRelease

Who changed what in which revision?

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