d

Dependencies:   AX12_final MX106_not_working comunication_1 mbed-dev

Fork of MX106-finaltest by Team DIANA

Committer:
clynamen
Date:
Thu Nov 24 15:29:50 2016 +0000
Revision:
12:6ac3cac5677c
Parent:
1:946c65496383
dynamixel

Who changed what in which revision?

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