Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of football_project by
io/MySerial.h@3:388e441be8df, 2015-04-13 (annotated)
- Committer:
- AntonLS
- Date:
- Mon Apr 13 17:41:48 2015 +0000
- Revision:
- 3:388e441be8df
- Parent:
- 2:fe1566cdb6e7
- Child:
- 4:17b8edf264c3
Ready for HW FC test.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| AntonLS | 1:0ba687d4196f | 1 | /* |
| AntonLS | 1:0ba687d4196f | 2 | * |
| AntonLS | 1:0ba687d4196f | 3 | * Replacement for Serial, so UART flow control is inited properly. ALS 20150412 |
| AntonLS | 1:0ba687d4196f | 4 | * |
| AntonLS | 1:0ba687d4196f | 5 | * MySerialBase is a replacement for SerialBase, to prevent it from calling |
| AntonLS | 1:0ba687d4196f | 6 | * the faulty-for-nRF51822-uart-init serial_init() |
| AntonLS | 1:0ba687d4196f | 7 | * in mbed/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/serial_api.c |
| AntonLS | 1:0ba687d4196f | 8 | * |
| AntonLS | 1:0ba687d4196f | 9 | */ |
| AntonLS | 1:0ba687d4196f | 10 | |
| AntonLS | 1:0ba687d4196f | 11 | #ifndef MYSERIAL_H |
| AntonLS | 1:0ba687d4196f | 12 | #define MYSERIAL_H |
| AntonLS | 1:0ba687d4196f | 13 | |
| AntonLS | 1:0ba687d4196f | 14 | #include "mbed.h" |
| AntonLS | 3:388e441be8df | 15 | // #include "Stream.h" |
| AntonLS | 3:388e441be8df | 16 | // #include "FunctionPointer.h" |
| AntonLS | 3:388e441be8df | 17 | #include "serial_api.h" |
| AntonLS | 1:0ba687d4196f | 18 | #include <cstdarg> |
| AntonLS | 1:0ba687d4196f | 19 | |
| AntonLS | 1:0ba687d4196f | 20 | namespace moo |
| AntonLS | 1:0ba687d4196f | 21 | { |
| AntonLS | 1:0ba687d4196f | 22 | |
| AntonLS | 1:0ba687d4196f | 23 | /** A base class for serial port implementations |
| AntonLS | 1:0ba687d4196f | 24 | * Can't be instantiated directly (use MySerial) |
| AntonLS | 1:0ba687d4196f | 25 | */ |
| AntonLS | 1:0ba687d4196f | 26 | class MySerialBase |
| AntonLS | 1:0ba687d4196f | 27 | { |
| AntonLS | 1:0ba687d4196f | 28 | public: |
| AntonLS | 1:0ba687d4196f | 29 | /** Set the baud rate of the serial port |
| AntonLS | 1:0ba687d4196f | 30 | * |
| AntonLS | 1:0ba687d4196f | 31 | * @param baudrate The baudrate of the serial port (default = 9600). |
| AntonLS | 1:0ba687d4196f | 32 | */ |
| AntonLS | 1:0ba687d4196f | 33 | void baud( int baudrate ); |
| AntonLS | 1:0ba687d4196f | 34 | |
| AntonLS | 1:0ba687d4196f | 35 | /** Set the transmission format used by the serial port |
| AntonLS | 1:0ba687d4196f | 36 | * |
| AntonLS | 1:0ba687d4196f | 37 | * @param bits The number of bits in a word (5-8; default = 8) |
| AntonLS | 1:0ba687d4196f | 38 | * @param parity The parity used (SerialBase::None, SerialBase::Odd, SerialBase::Even, SerialBase::Forced1, SerialBase::Forced0; default = SerialBase::None) |
| AntonLS | 1:0ba687d4196f | 39 | * @param stop The number of stop bits (1 or 2; default = 1) |
| AntonLS | 1:0ba687d4196f | 40 | */ |
| AntonLS | 1:0ba687d4196f | 41 | void format( int bits=8, SerialBase::Parity parity=SerialBase::None, int stop_bits=1 ); |
| AntonLS | 1:0ba687d4196f | 42 | |
| AntonLS | 1:0ba687d4196f | 43 | /** Determine if there is a character available to read |
| AntonLS | 1:0ba687d4196f | 44 | * |
| AntonLS | 1:0ba687d4196f | 45 | * @returns |
| AntonLS | 1:0ba687d4196f | 46 | * 1 if there is a character available to read, |
| AntonLS | 1:0ba687d4196f | 47 | * 0 otherwise |
| AntonLS | 1:0ba687d4196f | 48 | */ |
| AntonLS | 1:0ba687d4196f | 49 | int readable(); |
| AntonLS | 1:0ba687d4196f | 50 | |
| AntonLS | 1:0ba687d4196f | 51 | /** Determine if there is space available to write a character |
| AntonLS | 1:0ba687d4196f | 52 | * |
| AntonLS | 1:0ba687d4196f | 53 | * @returns |
| AntonLS | 1:0ba687d4196f | 54 | * 1 if there is space to write a character, |
| AntonLS | 1:0ba687d4196f | 55 | * 0 otherwise |
| AntonLS | 1:0ba687d4196f | 56 | */ |
| AntonLS | 1:0ba687d4196f | 57 | int writeable(); |
| AntonLS | 1:0ba687d4196f | 58 | |
| AntonLS | 1:0ba687d4196f | 59 | /** Attach a function to call whenever a serial interrupt is generated |
| AntonLS | 1:0ba687d4196f | 60 | * |
| AntonLS | 1:0ba687d4196f | 61 | * @param fptr A pointer to a void function, or 0 to set as none |
| AntonLS | 1:0ba687d4196f | 62 | * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) |
| AntonLS | 1:0ba687d4196f | 63 | */ |
| AntonLS | 1:0ba687d4196f | 64 | void attach( void (*fptr)(void), SerialBase::IrqType type=SerialBase::RxIrq ); |
| AntonLS | 1:0ba687d4196f | 65 | |
| AntonLS | 1:0ba687d4196f | 66 | /** Attach a member function to call whenever a serial interrupt is generated |
| AntonLS | 1:0ba687d4196f | 67 | * |
| AntonLS | 1:0ba687d4196f | 68 | * @param tptr pointer to the object to call the member function on |
| AntonLS | 1:0ba687d4196f | 69 | * @param mptr pointer to the member function to be called |
| AntonLS | 1:0ba687d4196f | 70 | * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) |
| AntonLS | 1:0ba687d4196f | 71 | */ |
| AntonLS | 1:0ba687d4196f | 72 | template<typename T> |
| AntonLS | 1:0ba687d4196f | 73 | void attach( T* tptr, void (T::*mptr)(void), SerialBase::IrqType type=SerialBase::RxIrq ) |
| AntonLS | 1:0ba687d4196f | 74 | { |
| AntonLS | 1:0ba687d4196f | 75 | if( (mptr != NULL) && (tptr != NULL) ) |
| AntonLS | 1:0ba687d4196f | 76 | { |
| AntonLS | 1:0ba687d4196f | 77 | _my_irq[type].attach( tptr, mptr ); |
| AntonLS | 1:0ba687d4196f | 78 | serial_irq_set( &_my_serial, (SerialIrq)type, 1 ); |
| AntonLS | 1:0ba687d4196f | 79 | } |
| AntonLS | 1:0ba687d4196f | 80 | } |
| AntonLS | 1:0ba687d4196f | 81 | |
| AntonLS | 1:0ba687d4196f | 82 | /** Generate a break condition on the serial line |
| AntonLS | 1:0ba687d4196f | 83 | */ |
| AntonLS | 1:0ba687d4196f | 84 | void send_break(); |
| AntonLS | 1:0ba687d4196f | 85 | |
| AntonLS | 1:0ba687d4196f | 86 | static void _irq_handler( uint32_t id, SerialIrq irq_type ); |
| AntonLS | 1:0ba687d4196f | 87 | |
| AntonLS | 1:0ba687d4196f | 88 | protected: |
| AntonLS | 2:fe1566cdb6e7 | 89 | MySerialBase( PinName tx, PinName rx, PinName rts=RTS_PIN_NUMBER, PinName cts=CTS_PIN_NUMBER ); |
| AntonLS | 1:0ba687d4196f | 90 | virtual ~MySerialBase() |
| AntonLS | 1:0ba687d4196f | 91 | { |
| AntonLS | 1:0ba687d4196f | 92 | } |
| AntonLS | 1:0ba687d4196f | 93 | |
| AntonLS | 2:fe1566cdb6e7 | 94 | void my_serial_init( serial_t *obj, PinName tx, PinName rx, PinName rts, PinName cts, int baudrate ); |
| AntonLS | 2:fe1566cdb6e7 | 95 | |
| AntonLS | 1:0ba687d4196f | 96 | int _base_getc(); |
| AntonLS | 1:0ba687d4196f | 97 | int _base_putc( int c ); |
| AntonLS | 1:0ba687d4196f | 98 | |
| AntonLS | 1:0ba687d4196f | 99 | serial_t _my_serial; |
| AntonLS | 1:0ba687d4196f | 100 | FunctionPointer _my_irq[2]; |
| AntonLS | 1:0ba687d4196f | 101 | int _my_baud; |
| AntonLS | 1:0ba687d4196f | 102 | }; |
| AntonLS | 1:0ba687d4196f | 103 | |
| AntonLS | 1:0ba687d4196f | 104 | class MySerial : public MySerialBase, public Stream |
| AntonLS | 1:0ba687d4196f | 105 | { |
| AntonLS | 1:0ba687d4196f | 106 | public: |
| AntonLS | 1:0ba687d4196f | 107 | /** Create a Serial port, connected to the specified transmit and receive pins |
| AntonLS | 1:0ba687d4196f | 108 | * |
| AntonLS | 1:0ba687d4196f | 109 | * @param tx Transmit pin |
| AntonLS | 1:0ba687d4196f | 110 | * @param rx Receive pin |
| AntonLS | 1:0ba687d4196f | 111 | * |
| AntonLS | 1:0ba687d4196f | 112 | * @note |
| AntonLS | 1:0ba687d4196f | 113 | * Either tx or rx may be specified as NC if unused |
| AntonLS | 1:0ba687d4196f | 114 | */ |
| AntonLS | 3:388e441be8df | 115 | MySerial( PinName tx, PinName rx, const char *name=NULL, |
| AntonLS | 3:388e441be8df | 116 | PinName rts=NC, PinName cts=NC ); // Defaults to using no flow control. |
| AntonLS | 1:0ba687d4196f | 117 | |
| AntonLS | 1:0ba687d4196f | 118 | /** Write a string to the serial port |
| AntonLS | 1:0ba687d4196f | 119 | * |
| AntonLS | 1:0ba687d4196f | 120 | * @param str The string to write |
| AntonLS | 1:0ba687d4196f | 121 | * |
| AntonLS | 1:0ba687d4196f | 122 | * @returns 0 if the write succeeds, EOF for error |
| AntonLS | 1:0ba687d4196f | 123 | */ |
| AntonLS | 1:0ba687d4196f | 124 | int puts( const char *str ); |
| AntonLS | 1:0ba687d4196f | 125 | |
| AntonLS | 1:0ba687d4196f | 126 | int printf( const char *format, ... ); |
| AntonLS | 1:0ba687d4196f | 127 | |
| AntonLS | 1:0ba687d4196f | 128 | int vprintf( const char *format, va_list arg ); |
| AntonLS | 1:0ba687d4196f | 129 | |
| AntonLS | 1:0ba687d4196f | 130 | protected: |
| AntonLS | 1:0ba687d4196f | 131 | virtual int _getc(); |
| AntonLS | 1:0ba687d4196f | 132 | virtual int _putc( int c ); |
| AntonLS | 1:0ba687d4196f | 133 | }; |
| AntonLS | 1:0ba687d4196f | 134 | |
| AntonLS | 1:0ba687d4196f | 135 | } // namespace moo |
| AntonLS | 1:0ba687d4196f | 136 | |
| AntonLS | 1:0ba687d4196f | 137 | #endif /* MYSERIAL_H */ |
