Andriy Makukha / Mbed 2 deprecated football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Committer:
AntonLS
Date:
Thu Apr 16 17:10:19 2015 +0000
Revision:
4:17b8edf264c3
Parent:
3:388e441be8df
Child:
5:1b9734e68327
printf in MTSSerial now prints to buffered io.

Who changed what in which revision?

UserRevisionLine numberNew 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 4:17b8edf264c3 20 #define STRING_STACK_LIMIT 120
AntonLS 4:17b8edf264c3 21
AntonLS 1:0ba687d4196f 22 namespace moo
AntonLS 1:0ba687d4196f 23 {
AntonLS 1:0ba687d4196f 24
AntonLS 1:0ba687d4196f 25 /** A base class for serial port implementations
AntonLS 1:0ba687d4196f 26 * Can't be instantiated directly (use MySerial)
AntonLS 1:0ba687d4196f 27 */
AntonLS 1:0ba687d4196f 28 class MySerialBase
AntonLS 1:0ba687d4196f 29 {
AntonLS 1:0ba687d4196f 30 public:
AntonLS 1:0ba687d4196f 31 /** Set the baud rate of the serial port
AntonLS 1:0ba687d4196f 32 *
AntonLS 1:0ba687d4196f 33 * @param baudrate The baudrate of the serial port (default = 9600).
AntonLS 1:0ba687d4196f 34 */
AntonLS 1:0ba687d4196f 35 void baud( int baudrate );
AntonLS 1:0ba687d4196f 36
AntonLS 1:0ba687d4196f 37 /** Set the transmission format used by the serial port
AntonLS 1:0ba687d4196f 38 *
AntonLS 1:0ba687d4196f 39 * @param bits The number of bits in a word (5-8; default = 8)
AntonLS 1:0ba687d4196f 40 * @param parity The parity used (SerialBase::None, SerialBase::Odd, SerialBase::Even, SerialBase::Forced1, SerialBase::Forced0; default = SerialBase::None)
AntonLS 1:0ba687d4196f 41 * @param stop The number of stop bits (1 or 2; default = 1)
AntonLS 1:0ba687d4196f 42 */
AntonLS 1:0ba687d4196f 43 void format( int bits=8, SerialBase::Parity parity=SerialBase::None, int stop_bits=1 );
AntonLS 1:0ba687d4196f 44
AntonLS 1:0ba687d4196f 45 /** Determine if there is a character available to read
AntonLS 1:0ba687d4196f 46 *
AntonLS 1:0ba687d4196f 47 * @returns
AntonLS 1:0ba687d4196f 48 * 1 if there is a character available to read,
AntonLS 1:0ba687d4196f 49 * 0 otherwise
AntonLS 1:0ba687d4196f 50 */
AntonLS 1:0ba687d4196f 51 int readable();
AntonLS 1:0ba687d4196f 52
AntonLS 1:0ba687d4196f 53 /** Determine if there is space available to write a character
AntonLS 1:0ba687d4196f 54 *
AntonLS 1:0ba687d4196f 55 * @returns
AntonLS 1:0ba687d4196f 56 * 1 if there is space to write a character,
AntonLS 1:0ba687d4196f 57 * 0 otherwise
AntonLS 1:0ba687d4196f 58 */
AntonLS 1:0ba687d4196f 59 int writeable();
AntonLS 1:0ba687d4196f 60
AntonLS 1:0ba687d4196f 61 /** Attach a function to call whenever a serial interrupt is generated
AntonLS 1:0ba687d4196f 62 *
AntonLS 1:0ba687d4196f 63 * @param fptr A pointer to a void function, or 0 to set as none
AntonLS 1:0ba687d4196f 64 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
AntonLS 1:0ba687d4196f 65 */
AntonLS 1:0ba687d4196f 66 void attach( void (*fptr)(void), SerialBase::IrqType type=SerialBase::RxIrq );
AntonLS 1:0ba687d4196f 67
AntonLS 1:0ba687d4196f 68 /** Attach a member function to call whenever a serial interrupt is generated
AntonLS 1:0ba687d4196f 69 *
AntonLS 1:0ba687d4196f 70 * @param tptr pointer to the object to call the member function on
AntonLS 1:0ba687d4196f 71 * @param mptr pointer to the member function to be called
AntonLS 1:0ba687d4196f 72 * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
AntonLS 1:0ba687d4196f 73 */
AntonLS 1:0ba687d4196f 74 template<typename T>
AntonLS 1:0ba687d4196f 75 void attach( T* tptr, void (T::*mptr)(void), SerialBase::IrqType type=SerialBase::RxIrq )
AntonLS 1:0ba687d4196f 76 {
AntonLS 1:0ba687d4196f 77 if( (mptr != NULL) && (tptr != NULL) )
AntonLS 1:0ba687d4196f 78 {
AntonLS 1:0ba687d4196f 79 _my_irq[type].attach( tptr, mptr );
AntonLS 1:0ba687d4196f 80 serial_irq_set( &_my_serial, (SerialIrq)type, 1 );
AntonLS 1:0ba687d4196f 81 }
AntonLS 1:0ba687d4196f 82 }
AntonLS 1:0ba687d4196f 83
AntonLS 1:0ba687d4196f 84 /** Generate a break condition on the serial line
AntonLS 1:0ba687d4196f 85 */
AntonLS 1:0ba687d4196f 86 void send_break();
AntonLS 1:0ba687d4196f 87
AntonLS 1:0ba687d4196f 88 static void _irq_handler( uint32_t id, SerialIrq irq_type );
AntonLS 1:0ba687d4196f 89
AntonLS 1:0ba687d4196f 90 protected:
AntonLS 2:fe1566cdb6e7 91 MySerialBase( PinName tx, PinName rx, PinName rts=RTS_PIN_NUMBER, PinName cts=CTS_PIN_NUMBER );
AntonLS 1:0ba687d4196f 92 virtual ~MySerialBase()
AntonLS 1:0ba687d4196f 93 {
AntonLS 1:0ba687d4196f 94 }
AntonLS 1:0ba687d4196f 95
AntonLS 2:fe1566cdb6e7 96 void my_serial_init( serial_t *obj, PinName tx, PinName rx, PinName rts, PinName cts, int baudrate );
AntonLS 2:fe1566cdb6e7 97
AntonLS 1:0ba687d4196f 98 int _base_getc();
AntonLS 1:0ba687d4196f 99 int _base_putc( int c );
AntonLS 1:0ba687d4196f 100
AntonLS 1:0ba687d4196f 101 serial_t _my_serial;
AntonLS 1:0ba687d4196f 102 FunctionPointer _my_irq[2];
AntonLS 1:0ba687d4196f 103 int _my_baud;
AntonLS 1:0ba687d4196f 104 };
AntonLS 1:0ba687d4196f 105
AntonLS 1:0ba687d4196f 106 class MySerial : public MySerialBase, public Stream
AntonLS 1:0ba687d4196f 107 {
AntonLS 1:0ba687d4196f 108 public:
AntonLS 1:0ba687d4196f 109 /** Create a Serial port, connected to the specified transmit and receive pins
AntonLS 1:0ba687d4196f 110 *
AntonLS 1:0ba687d4196f 111 * @param tx Transmit pin
AntonLS 1:0ba687d4196f 112 * @param rx Receive pin
AntonLS 1:0ba687d4196f 113 *
AntonLS 1:0ba687d4196f 114 * @note
AntonLS 1:0ba687d4196f 115 * Either tx or rx may be specified as NC if unused
AntonLS 1:0ba687d4196f 116 */
AntonLS 3:388e441be8df 117 MySerial( PinName tx, PinName rx, const char *name=NULL,
AntonLS 3:388e441be8df 118 PinName rts=NC, PinName cts=NC ); // Defaults to using no flow control.
AntonLS 1:0ba687d4196f 119
AntonLS 1:0ba687d4196f 120 /** Write a string to the serial port
AntonLS 1:0ba687d4196f 121 *
AntonLS 1:0ba687d4196f 122 * @param str The string to write
AntonLS 1:0ba687d4196f 123 *
AntonLS 1:0ba687d4196f 124 * @returns 0 if the write succeeds, EOF for error
AntonLS 1:0ba687d4196f 125 */
AntonLS 1:0ba687d4196f 126 int puts( const char *str );
AntonLS 1:0ba687d4196f 127
AntonLS 1:0ba687d4196f 128 int printf( const char *format, ... );
AntonLS 1:0ba687d4196f 129
AntonLS 1:0ba687d4196f 130 int vprintf( const char *format, va_list arg );
AntonLS 1:0ba687d4196f 131
AntonLS 1:0ba687d4196f 132 protected:
AntonLS 1:0ba687d4196f 133 virtual int _getc();
AntonLS 1:0ba687d4196f 134 virtual int _putc( int c );
AntonLS 1:0ba687d4196f 135 };
AntonLS 1:0ba687d4196f 136
AntonLS 1:0ba687d4196f 137 } // namespace moo
AntonLS 1:0ba687d4196f 138
AntonLS 1:0ba687d4196f 139 #endif /* MYSERIAL_H */