Programme d'utilisation des AX12 avec rajout de l'MX12
Fork of test_carteAToutFaire_PR by
mbed/Serial.h@1:f3f702086a30, 2017-05-11 (annotated)
- Committer:
- ClementBreteau
- Date:
- Thu May 11 11:49:50 2017 +0000
- Revision:
- 1:f3f702086a30
test de la carte ? tout faire du petit robot
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ClementBreteau | 1:f3f702086a30 | 1 | /* mbed Microcontroller Library - Serial |
ClementBreteau | 1:f3f702086a30 | 2 | * Copyright (c) 2007-2011 ARM Limited. All rights reserved. |
ClementBreteau | 1:f3f702086a30 | 3 | */ |
ClementBreteau | 1:f3f702086a30 | 4 | |
ClementBreteau | 1:f3f702086a30 | 5 | #ifndef MBED_SERIAL_H |
ClementBreteau | 1:f3f702086a30 | 6 | #define MBED_SERIAL_H |
ClementBreteau | 1:f3f702086a30 | 7 | |
ClementBreteau | 1:f3f702086a30 | 8 | #include "device.h" |
ClementBreteau | 1:f3f702086a30 | 9 | |
ClementBreteau | 1:f3f702086a30 | 10 | #if DEVICE_SERIAL |
ClementBreteau | 1:f3f702086a30 | 11 | |
ClementBreteau | 1:f3f702086a30 | 12 | #include "platform.h" |
ClementBreteau | 1:f3f702086a30 | 13 | #include "PinNames.h" |
ClementBreteau | 1:f3f702086a30 | 14 | #include "PeripheralNames.h" |
ClementBreteau | 1:f3f702086a30 | 15 | #include "Stream.h" |
ClementBreteau | 1:f3f702086a30 | 16 | #include "FunctionPointer.h" |
ClementBreteau | 1:f3f702086a30 | 17 | |
ClementBreteau | 1:f3f702086a30 | 18 | namespace mbed { |
ClementBreteau | 1:f3f702086a30 | 19 | |
ClementBreteau | 1:f3f702086a30 | 20 | /* Class: Serial |
ClementBreteau | 1:f3f702086a30 | 21 | * A serial port (UART) for communication with other serial devices |
ClementBreteau | 1:f3f702086a30 | 22 | * |
ClementBreteau | 1:f3f702086a30 | 23 | * Can be used for Full Duplex communication, or Simplex by specifying |
ClementBreteau | 1:f3f702086a30 | 24 | * one pin as NC (Not Connected) |
ClementBreteau | 1:f3f702086a30 | 25 | * |
ClementBreteau | 1:f3f702086a30 | 26 | * Example: |
ClementBreteau | 1:f3f702086a30 | 27 | * > // Print "Hello World" to the PC |
ClementBreteau | 1:f3f702086a30 | 28 | * > |
ClementBreteau | 1:f3f702086a30 | 29 | * > #include "mbed.h" |
ClementBreteau | 1:f3f702086a30 | 30 | * > |
ClementBreteau | 1:f3f702086a30 | 31 | * > Serial pc(USBTX, USBRX); |
ClementBreteau | 1:f3f702086a30 | 32 | * > |
ClementBreteau | 1:f3f702086a30 | 33 | * > int main() { |
ClementBreteau | 1:f3f702086a30 | 34 | * > pc.printf("Hello World\n"); |
ClementBreteau | 1:f3f702086a30 | 35 | * > } |
ClementBreteau | 1:f3f702086a30 | 36 | */ |
ClementBreteau | 1:f3f702086a30 | 37 | class Serial : public Stream { |
ClementBreteau | 1:f3f702086a30 | 38 | |
ClementBreteau | 1:f3f702086a30 | 39 | public: |
ClementBreteau | 1:f3f702086a30 | 40 | |
ClementBreteau | 1:f3f702086a30 | 41 | /* Constructor: Serial |
ClementBreteau | 1:f3f702086a30 | 42 | * Create a Serial port, connected to the specified transmit and receive pins |
ClementBreteau | 1:f3f702086a30 | 43 | * |
ClementBreteau | 1:f3f702086a30 | 44 | * Variables: |
ClementBreteau | 1:f3f702086a30 | 45 | * tx - Transmit pin |
ClementBreteau | 1:f3f702086a30 | 46 | * rx - Receive pin |
ClementBreteau | 1:f3f702086a30 | 47 | * |
ClementBreteau | 1:f3f702086a30 | 48 | * Note: Either tx or rx may be specified as NC if unused |
ClementBreteau | 1:f3f702086a30 | 49 | */ |
ClementBreteau | 1:f3f702086a30 | 50 | Serial(PinName tx, PinName rx, const char *name = NULL); |
ClementBreteau | 1:f3f702086a30 | 51 | |
ClementBreteau | 1:f3f702086a30 | 52 | /* Function: baud |
ClementBreteau | 1:f3f702086a30 | 53 | * Set the baud rate of the serial port |
ClementBreteau | 1:f3f702086a30 | 54 | * |
ClementBreteau | 1:f3f702086a30 | 55 | * Variables: |
ClementBreteau | 1:f3f702086a30 | 56 | * baudrate - The baudrate of the serial port (default = 9600). |
ClementBreteau | 1:f3f702086a30 | 57 | */ |
ClementBreteau | 1:f3f702086a30 | 58 | void baud(int baudrate); |
ClementBreteau | 1:f3f702086a30 | 59 | |
ClementBreteau | 1:f3f702086a30 | 60 | enum Parity { |
ClementBreteau | 1:f3f702086a30 | 61 | None = 0 |
ClementBreteau | 1:f3f702086a30 | 62 | , Odd |
ClementBreteau | 1:f3f702086a30 | 63 | , Even |
ClementBreteau | 1:f3f702086a30 | 64 | , Forced1 |
ClementBreteau | 1:f3f702086a30 | 65 | , Forced0 |
ClementBreteau | 1:f3f702086a30 | 66 | }; |
ClementBreteau | 1:f3f702086a30 | 67 | |
ClementBreteau | 1:f3f702086a30 | 68 | enum IrqType { |
ClementBreteau | 1:f3f702086a30 | 69 | RxIrq = 0 |
ClementBreteau | 1:f3f702086a30 | 70 | , TxIrq |
ClementBreteau | 1:f3f702086a30 | 71 | }; |
ClementBreteau | 1:f3f702086a30 | 72 | |
ClementBreteau | 1:f3f702086a30 | 73 | /* Function: format |
ClementBreteau | 1:f3f702086a30 | 74 | * Set the transmission format used by the Serial port |
ClementBreteau | 1:f3f702086a30 | 75 | * |
ClementBreteau | 1:f3f702086a30 | 76 | * Variables: |
ClementBreteau | 1:f3f702086a30 | 77 | * bits - The number of bits in a word (5-8; default = 8) |
ClementBreteau | 1:f3f702086a30 | 78 | * parity - The parity used (Serial::None, Serial::Odd, Serial::Even, Serial::Forced1, Serial::Forced0; default = Serial::None) |
ClementBreteau | 1:f3f702086a30 | 79 | * stop - The number of stop bits (1 or 2; default = 1) |
ClementBreteau | 1:f3f702086a30 | 80 | */ |
ClementBreteau | 1:f3f702086a30 | 81 | void format(int bits = 8, Parity parity = Serial::None, int stop_bits = 1); |
ClementBreteau | 1:f3f702086a30 | 82 | |
ClementBreteau | 1:f3f702086a30 | 83 | #if 0 // Inhereted from Stream, for documentation only |
ClementBreteau | 1:f3f702086a30 | 84 | |
ClementBreteau | 1:f3f702086a30 | 85 | /* Function: putc |
ClementBreteau | 1:f3f702086a30 | 86 | * Write a character |
ClementBreteau | 1:f3f702086a30 | 87 | * |
ClementBreteau | 1:f3f702086a30 | 88 | * Variables: |
ClementBreteau | 1:f3f702086a30 | 89 | * c - The character to write to the serial port |
ClementBreteau | 1:f3f702086a30 | 90 | */ |
ClementBreteau | 1:f3f702086a30 | 91 | int putc(int c); |
ClementBreteau | 1:f3f702086a30 | 92 | |
ClementBreteau | 1:f3f702086a30 | 93 | /* Function: getc |
ClementBreteau | 1:f3f702086a30 | 94 | * Read a character |
ClementBreteau | 1:f3f702086a30 | 95 | * |
ClementBreteau | 1:f3f702086a30 | 96 | * Reads a character from the serial port. This will block until |
ClementBreteau | 1:f3f702086a30 | 97 | * a character is available. To see if a character is available, |
ClementBreteau | 1:f3f702086a30 | 98 | * see <readable> |
ClementBreteau | 1:f3f702086a30 | 99 | * |
ClementBreteau | 1:f3f702086a30 | 100 | * Variables: |
ClementBreteau | 1:f3f702086a30 | 101 | * returns - The character read from the serial port |
ClementBreteau | 1:f3f702086a30 | 102 | */ |
ClementBreteau | 1:f3f702086a30 | 103 | int getc(); |
ClementBreteau | 1:f3f702086a30 | 104 | |
ClementBreteau | 1:f3f702086a30 | 105 | /* Function: printf |
ClementBreteau | 1:f3f702086a30 | 106 | * Write a formated string |
ClementBreteau | 1:f3f702086a30 | 107 | * |
ClementBreteau | 1:f3f702086a30 | 108 | * Variables: |
ClementBreteau | 1:f3f702086a30 | 109 | * format - A printf-style format string, followed by the |
ClementBreteau | 1:f3f702086a30 | 110 | * variables to use in formating the string. |
ClementBreteau | 1:f3f702086a30 | 111 | */ |
ClementBreteau | 1:f3f702086a30 | 112 | int printf(const char* format, ...); |
ClementBreteau | 1:f3f702086a30 | 113 | |
ClementBreteau | 1:f3f702086a30 | 114 | /* Function: scanf |
ClementBreteau | 1:f3f702086a30 | 115 | * Read a formated string |
ClementBreteau | 1:f3f702086a30 | 116 | * |
ClementBreteau | 1:f3f702086a30 | 117 | * Variables: |
ClementBreteau | 1:f3f702086a30 | 118 | * format - A scanf-style format string, |
ClementBreteau | 1:f3f702086a30 | 119 | * followed by the pointers to variables to store the results. |
ClementBreteau | 1:f3f702086a30 | 120 | */ |
ClementBreteau | 1:f3f702086a30 | 121 | int scanf(const char* format, ...); |
ClementBreteau | 1:f3f702086a30 | 122 | |
ClementBreteau | 1:f3f702086a30 | 123 | #endif |
ClementBreteau | 1:f3f702086a30 | 124 | |
ClementBreteau | 1:f3f702086a30 | 125 | /* Function: readable |
ClementBreteau | 1:f3f702086a30 | 126 | * Determine if there is a character available to read |
ClementBreteau | 1:f3f702086a30 | 127 | * |
ClementBreteau | 1:f3f702086a30 | 128 | * Variables: |
ClementBreteau | 1:f3f702086a30 | 129 | * returns - 1 if there is a character available to read, else 0 |
ClementBreteau | 1:f3f702086a30 | 130 | */ |
ClementBreteau | 1:f3f702086a30 | 131 | int readable(); |
ClementBreteau | 1:f3f702086a30 | 132 | |
ClementBreteau | 1:f3f702086a30 | 133 | /* Function: writeable |
ClementBreteau | 1:f3f702086a30 | 134 | * Determine if there is space available to write a character |
ClementBreteau | 1:f3f702086a30 | 135 | * |
ClementBreteau | 1:f3f702086a30 | 136 | * Variables: |
ClementBreteau | 1:f3f702086a30 | 137 | * returns - 1 if there is space to write a character, else 0 |
ClementBreteau | 1:f3f702086a30 | 138 | */ |
ClementBreteau | 1:f3f702086a30 | 139 | int writeable(); |
ClementBreteau | 1:f3f702086a30 | 140 | |
ClementBreteau | 1:f3f702086a30 | 141 | /* Function: attach |
ClementBreteau | 1:f3f702086a30 | 142 | * Attach a function to call whenever a serial interrupt is generated |
ClementBreteau | 1:f3f702086a30 | 143 | * |
ClementBreteau | 1:f3f702086a30 | 144 | * Variables: |
ClementBreteau | 1:f3f702086a30 | 145 | * fptr - A pointer to a void function, or 0 to set as none |
ClementBreteau | 1:f3f702086a30 | 146 | * type - Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) |
ClementBreteau | 1:f3f702086a30 | 147 | */ |
ClementBreteau | 1:f3f702086a30 | 148 | void attach(void (*fptr)(void), IrqType type = RxIrq); |
ClementBreteau | 1:f3f702086a30 | 149 | |
ClementBreteau | 1:f3f702086a30 | 150 | /* Function: attach |
ClementBreteau | 1:f3f702086a30 | 151 | * Attach a member function to call whenever a serial interrupt is generated |
ClementBreteau | 1:f3f702086a30 | 152 | * |
ClementBreteau | 1:f3f702086a30 | 153 | * Variables: |
ClementBreteau | 1:f3f702086a30 | 154 | * tptr - pointer to the object to call the member function on |
ClementBreteau | 1:f3f702086a30 | 155 | * mptr - pointer to the member function to be called |
ClementBreteau | 1:f3f702086a30 | 156 | * type - Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) |
ClementBreteau | 1:f3f702086a30 | 157 | */ |
ClementBreteau | 1:f3f702086a30 | 158 | template<typename T> |
ClementBreteau | 1:f3f702086a30 | 159 | void attach(T* tptr, void (T::*mptr)(void), IrqType type = RxIrq) { |
ClementBreteau | 1:f3f702086a30 | 160 | if((mptr != NULL) && (tptr != NULL)) { |
ClementBreteau | 1:f3f702086a30 | 161 | _irq[type].attach(tptr, mptr); |
ClementBreteau | 1:f3f702086a30 | 162 | setup_interrupt(type); |
ClementBreteau | 1:f3f702086a30 | 163 | } |
ClementBreteau | 1:f3f702086a30 | 164 | } |
ClementBreteau | 1:f3f702086a30 | 165 | |
ClementBreteau | 1:f3f702086a30 | 166 | #ifdef MBED_RPC |
ClementBreteau | 1:f3f702086a30 | 167 | virtual const struct rpc_method *get_rpc_methods(); |
ClementBreteau | 1:f3f702086a30 | 168 | static struct rpc_class *get_rpc_class(); |
ClementBreteau | 1:f3f702086a30 | 169 | #endif |
ClementBreteau | 1:f3f702086a30 | 170 | |
ClementBreteau | 1:f3f702086a30 | 171 | protected: |
ClementBreteau | 1:f3f702086a30 | 172 | |
ClementBreteau | 1:f3f702086a30 | 173 | void setup_interrupt(IrqType type); |
ClementBreteau | 1:f3f702086a30 | 174 | void remove_interrupt(IrqType type); |
ClementBreteau | 1:f3f702086a30 | 175 | |
ClementBreteau | 1:f3f702086a30 | 176 | virtual int _getc(); |
ClementBreteau | 1:f3f702086a30 | 177 | virtual int _putc(int c); |
ClementBreteau | 1:f3f702086a30 | 178 | |
ClementBreteau | 1:f3f702086a30 | 179 | UARTName _uart; |
ClementBreteau | 1:f3f702086a30 | 180 | FunctionPointer _irq[2]; |
ClementBreteau | 1:f3f702086a30 | 181 | int _uidx; |
ClementBreteau | 1:f3f702086a30 | 182 | |
ClementBreteau | 1:f3f702086a30 | 183 | }; |
ClementBreteau | 1:f3f702086a30 | 184 | |
ClementBreteau | 1:f3f702086a30 | 185 | } // namespace mbed |
ClementBreteau | 1:f3f702086a30 | 186 | |
ClementBreteau | 1:f3f702086a30 | 187 | #endif |
ClementBreteau | 1:f3f702086a30 | 188 | |
ClementBreteau | 1:f3f702086a30 | 189 | #endif |