This is my quadcopter prototype software, still in development!

Committer:
Anaesthetix
Date:
Tue Jul 23 14:01:42 2013 +0000
Revision:
1:ac68f0368a77
Parent:
0:978110f7f027
Other accelerometer added

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Anaesthetix 0:978110f7f027 1 /* mbed Microcontroller Library - SerialHalfDuplex
Anaesthetix 0:978110f7f027 2 * Copyright (c) 2010-2011 ARM Limited. All rights reserved.
Anaesthetix 0:978110f7f027 3 */
Anaesthetix 0:978110f7f027 4
Anaesthetix 0:978110f7f027 5 #ifndef MBED_SERIALHALFDUPLEX_H
Anaesthetix 0:978110f7f027 6 #define MBED_SERIALHALFDUPLEX_H
Anaesthetix 0:978110f7f027 7
Anaesthetix 0:978110f7f027 8 #include "device.h"
Anaesthetix 0:978110f7f027 9
Anaesthetix 0:978110f7f027 10 #if DEVICE_SERIAL
Anaesthetix 0:978110f7f027 11
Anaesthetix 0:978110f7f027 12 #include "Serial.h"
Anaesthetix 0:978110f7f027 13 #include "PinNames.h"
Anaesthetix 0:978110f7f027 14 #include "PeripheralNames.h"
Anaesthetix 0:978110f7f027 15
Anaesthetix 0:978110f7f027 16 namespace mbed {
Anaesthetix 0:978110f7f027 17
Anaesthetix 0:978110f7f027 18 /* Class: SerialHalfDuplex
Anaesthetix 0:978110f7f027 19 * A serial port (UART) for communication with other devices using
Anaesthetix 0:978110f7f027 20 * Half-Duplex, allowing transmit and receive on a single
Anaesthetix 0:978110f7f027 21 * shared transmit and receive line. Only one end should be transmitting
Anaesthetix 0:978110f7f027 22 * at a time.
Anaesthetix 0:978110f7f027 23 *
Anaesthetix 0:978110f7f027 24 * Both the tx and rx pin should be defined, and wired together.
Anaesthetix 0:978110f7f027 25 * This is in addition to them being wired to the other serial
Anaesthetix 0:978110f7f027 26 * device to allow both read and write functions to operate.
Anaesthetix 0:978110f7f027 27 *
Anaesthetix 0:978110f7f027 28 * Example:
Anaesthetix 0:978110f7f027 29 * > // Send a byte to a second HalfDuplex device, and read the response
Anaesthetix 0:978110f7f027 30 * >
Anaesthetix 0:978110f7f027 31 * > #include "mbed.h"
Anaesthetix 0:978110f7f027 32 * >
Anaesthetix 0:978110f7f027 33 * > // p9 and p10 should be wired together to form "a"
Anaesthetix 0:978110f7f027 34 * > // p28 and p27 should be wired together to form "b"
Anaesthetix 0:978110f7f027 35 * > // p9/p10 should be wired to p28/p27 as the Half Duplex connection
Anaesthetix 0:978110f7f027 36 * >
Anaesthetix 0:978110f7f027 37 * > SerialHalfDuplex a(p9, p10);
Anaesthetix 0:978110f7f027 38 * > SerialHalfDuplex b(p28, p27);
Anaesthetix 0:978110f7f027 39 * >
Anaesthetix 0:978110f7f027 40 * > void b_rx() { // second device response
Anaesthetix 0:978110f7f027 41 * > b.putc(b.getc() + 4);
Anaesthetix 0:978110f7f027 42 * > }
Anaesthetix 0:978110f7f027 43 * >
Anaesthetix 0:978110f7f027 44 * > int main() {
Anaesthetix 0:978110f7f027 45 * > b.attach(&b_rx);
Anaesthetix 0:978110f7f027 46 * > for(int c = 'A'; c < 'Z'; c++) {
Anaesthetix 0:978110f7f027 47 * > a.putc(c);
Anaesthetix 0:978110f7f027 48 * > printf("sent [%c]\n", c);
Anaesthetix 0:978110f7f027 49 * > wait(0.5); // b should respond
Anaesthetix 0:978110f7f027 50 * > if(a.readable()) {
Anaesthetix 0:978110f7f027 51 * > printf("received [%c]\n", a.getc());
Anaesthetix 0:978110f7f027 52 * > }
Anaesthetix 0:978110f7f027 53 * > }
Anaesthetix 0:978110f7f027 54 * > }
Anaesthetix 0:978110f7f027 55 *
Anaesthetix 0:978110f7f027 56 * For Simplex and Full-Duplex Serial communication, see <Serial>
Anaesthetix 0:978110f7f027 57 */
Anaesthetix 0:978110f7f027 58 class SerialHalfDuplex : public Serial {
Anaesthetix 0:978110f7f027 59
Anaesthetix 0:978110f7f027 60 public:
Anaesthetix 0:978110f7f027 61 /* Constructor: SerialHalfDuplex
Anaesthetix 0:978110f7f027 62 * Create a half-duplex serial port, connected to the specified transmit
Anaesthetix 0:978110f7f027 63 * and receive pins.
Anaesthetix 0:978110f7f027 64 *
Anaesthetix 0:978110f7f027 65 * These pins should be wired together, as well as to the target device
Anaesthetix 0:978110f7f027 66 *
Anaesthetix 0:978110f7f027 67 * Variables:
Anaesthetix 0:978110f7f027 68 * tx - Transmit pin
Anaesthetix 0:978110f7f027 69 * rx - Receive pin
Anaesthetix 0:978110f7f027 70 */
Anaesthetix 0:978110f7f027 71 SerialHalfDuplex(PinName tx, PinName rx, const char *name = NULL);
Anaesthetix 0:978110f7f027 72
Anaesthetix 0:978110f7f027 73 #if 0 // Inherited from Serial class, for documentation
Anaesthetix 0:978110f7f027 74 /* Function: baud
Anaesthetix 0:978110f7f027 75 * Set the baud rate of the serial port
Anaesthetix 0:978110f7f027 76 *
Anaesthetix 0:978110f7f027 77 * Variables:
Anaesthetix 0:978110f7f027 78 * baudrate - The baudrate of the serial port (default = 9600).
Anaesthetix 0:978110f7f027 79 */
Anaesthetix 0:978110f7f027 80 void baud(int baudrate);
Anaesthetix 0:978110f7f027 81
Anaesthetix 0:978110f7f027 82 enum Parity {
Anaesthetix 0:978110f7f027 83 None = 0
Anaesthetix 0:978110f7f027 84 , Odd
Anaesthetix 0:978110f7f027 85 , Even
Anaesthetix 0:978110f7f027 86 , Forced1
Anaesthetix 0:978110f7f027 87 , Forced0
Anaesthetix 0:978110f7f027 88 };
Anaesthetix 0:978110f7f027 89
Anaesthetix 0:978110f7f027 90 /* Function: format
Anaesthetix 0:978110f7f027 91 * Set the transmission format used by the Serial port
Anaesthetix 0:978110f7f027 92 *
Anaesthetix 0:978110f7f027 93 * Variables:
Anaesthetix 0:978110f7f027 94 * bits - The number of bits in a word (5-8; default = 8)
Anaesthetix 0:978110f7f027 95 * parity - The parity used (Serial::None, Serial::Odd,
Anaesthetix 0:978110f7f027 96 Serial::Even, Serial::Forced1, Serial::Forced0; default = Serial::None)
Anaesthetix 0:978110f7f027 97 * stop - The number of stop bits (1 or 2; default = 1)
Anaesthetix 0:978110f7f027 98 */
Anaesthetix 0:978110f7f027 99 void format(int bits = 8, Parity parity = Serial::None, int stop_bits
Anaesthetix 0:978110f7f027 100 = 1);
Anaesthetix 0:978110f7f027 101
Anaesthetix 0:978110f7f027 102 /* Function: putc
Anaesthetix 0:978110f7f027 103 * Write a character
Anaesthetix 0:978110f7f027 104 *
Anaesthetix 0:978110f7f027 105 * Variables:
Anaesthetix 0:978110f7f027 106 * c - The character to write to the serial port
Anaesthetix 0:978110f7f027 107 */
Anaesthetix 0:978110f7f027 108 int putc(int c);
Anaesthetix 0:978110f7f027 109
Anaesthetix 0:978110f7f027 110 /* Function: getc
Anaesthetix 0:978110f7f027 111 * Read a character
Anaesthetix 0:978110f7f027 112 *
Anaesthetix 0:978110f7f027 113 * Read a character from the serial port. This call will block
Anaesthetix 0:978110f7f027 114 * until a character is available. For testing if a character is
Anaesthetix 0:978110f7f027 115 * available for reading, see <readable>.
Anaesthetix 0:978110f7f027 116 *
Anaesthetix 0:978110f7f027 117 * Variables:
Anaesthetix 0:978110f7f027 118 * returns - The character read from the serial port
Anaesthetix 0:978110f7f027 119 */
Anaesthetix 0:978110f7f027 120 int getc();
Anaesthetix 0:978110f7f027 121
Anaesthetix 0:978110f7f027 122 /* Function: printf
Anaesthetix 0:978110f7f027 123 * Write a formated string
Anaesthetix 0:978110f7f027 124 *
Anaesthetix 0:978110f7f027 125 * Variables:
Anaesthetix 0:978110f7f027 126 * format - A printf-style format string, followed by the
Anaesthetix 0:978110f7f027 127 * variables to use in formating the string.
Anaesthetix 0:978110f7f027 128 */
Anaesthetix 0:978110f7f027 129 int printf(const char* format, ...);
Anaesthetix 0:978110f7f027 130
Anaesthetix 0:978110f7f027 131 /* Function: scanf
Anaesthetix 0:978110f7f027 132 * Read a formated string
Anaesthetix 0:978110f7f027 133 *
Anaesthetix 0:978110f7f027 134 * Variables:
Anaesthetix 0:978110f7f027 135 * format - A scanf-style format string,
Anaesthetix 0:978110f7f027 136 * followed by the pointers to variables to store the results.
Anaesthetix 0:978110f7f027 137 */
Anaesthetix 0:978110f7f027 138 int scanf(const char* format, ...);
Anaesthetix 0:978110f7f027 139
Anaesthetix 0:978110f7f027 140 /* Function: readable
Anaesthetix 0:978110f7f027 141 * Determine if there is a character available to read
Anaesthetix 0:978110f7f027 142 *
Anaesthetix 0:978110f7f027 143 * Variables:
Anaesthetix 0:978110f7f027 144 * returns - 1 if there is a character available to read, else 0
Anaesthetix 0:978110f7f027 145 */
Anaesthetix 0:978110f7f027 146 int readable();
Anaesthetix 0:978110f7f027 147
Anaesthetix 0:978110f7f027 148 /* Function: writeable
Anaesthetix 0:978110f7f027 149 * Determine if there is space available to write a character
Anaesthetix 0:978110f7f027 150 *
Anaesthetix 0:978110f7f027 151 * Variables:
Anaesthetix 0:978110f7f027 152 * returns - 1 if there is space to write a character, else 0
Anaesthetix 0:978110f7f027 153 */
Anaesthetix 0:978110f7f027 154 int writeable();
Anaesthetix 0:978110f7f027 155
Anaesthetix 0:978110f7f027 156 /* Function: attach
Anaesthetix 0:978110f7f027 157 * Attach a function to call whenever a serial interrupt is generated
Anaesthetix 0:978110f7f027 158 *
Anaesthetix 0:978110f7f027 159 * Variables:
Anaesthetix 0:978110f7f027 160 * fptr - A pointer to a void function, or 0 to set as none
Anaesthetix 0:978110f7f027 161 */
Anaesthetix 0:978110f7f027 162 void attach(void (*fptr)(void));
Anaesthetix 0:978110f7f027 163
Anaesthetix 0:978110f7f027 164 /* Function: attach
Anaesthetix 0:978110f7f027 165 * Attach a member function to call whenever a serial interrupt is generated
Anaesthetix 0:978110f7f027 166 *
Anaesthetix 0:978110f7f027 167 * Variables:
Anaesthetix 0:978110f7f027 168 * tptr - pointer to the object to call the member function on
Anaesthetix 0:978110f7f027 169 * mptr - pointer to the member function to be called
Anaesthetix 0:978110f7f027 170 */
Anaesthetix 0:978110f7f027 171 template<typename T>
Anaesthetix 0:978110f7f027 172 void attach(T* tptr, void (T::*mptr)(void));
Anaesthetix 0:978110f7f027 173
Anaesthetix 0:978110f7f027 174 #endif
Anaesthetix 0:978110f7f027 175
Anaesthetix 0:978110f7f027 176 protected:
Anaesthetix 0:978110f7f027 177 PinName _txpin;
Anaesthetix 0:978110f7f027 178
Anaesthetix 0:978110f7f027 179 virtual int _putc(int c);
Anaesthetix 0:978110f7f027 180 virtual int _getc(void);
Anaesthetix 0:978110f7f027 181
Anaesthetix 0:978110f7f027 182 }; // End class SerialHalfDuplex
Anaesthetix 0:978110f7f027 183
Anaesthetix 0:978110f7f027 184 } // End namespace
Anaesthetix 0:978110f7f027 185
Anaesthetix 0:978110f7f027 186 #endif
Anaesthetix 0:978110f7f027 187
Anaesthetix 0:978110f7f027 188 #endif