Andriy Makukha / Mbed 2 deprecated football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

io/MySerial.h

Committer:
AntonLS
Date:
2015-04-13
Revision:
3:388e441be8df
Parent:
2:fe1566cdb6e7
Child:
4:17b8edf264c3

File content as of revision 3:388e441be8df:

/*
 *
 * Replacement for Serial, so UART flow control is inited properly.    ALS 20150412
 *
 * MySerialBase is a replacement for SerialBase, to prevent it from calling
 *  the faulty-for-nRF51822-uart-init serial_init()
 *  in mbed/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/serial_api.c 
 *
 */

#ifndef MYSERIAL_H
#define MYSERIAL_H

#include "mbed.h"
// #include "Stream.h"
// #include "FunctionPointer.h"
#include "serial_api.h"
#include <cstdarg>

namespace moo
{

/** A base class for serial port implementations
 * Can't be instantiated directly (use MySerial)
 */
class MySerialBase
{
  public:
    /** Set the baud rate of the serial port
     *
     *  @param baudrate The baudrate of the serial port (default = 9600).
     */
    void baud( int baudrate );

    /** Set the transmission format used by the serial port
     *
     *  @param bits The number of bits in a word (5-8; default = 8)
     *  @param parity The parity used (SerialBase::None, SerialBase::Odd, SerialBase::Even, SerialBase::Forced1, SerialBase::Forced0; default = SerialBase::None)
     *  @param stop The number of stop bits (1 or 2; default = 1)
     */
    void format( int bits=8, SerialBase::Parity parity=SerialBase::None, int stop_bits=1 );

    /** Determine if there is a character available to read
     *
     *  @returns
     *    1 if there is a character available to read,
     *    0 otherwise
     */
    int readable();

    /** Determine if there is space available to write a character
     *
     *  @returns
     *    1 if there is space to write a character,
     *    0 otherwise
     */
    int writeable();

    /** Attach a function to call whenever a serial interrupt is generated
     *
     *  @param fptr A pointer to a void function, or 0 to set as none
     *  @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
     */
    void attach( void (*fptr)(void), SerialBase::IrqType type=SerialBase::RxIrq );

    /** Attach a member function to call whenever a serial interrupt is generated
     *
     *  @param tptr pointer to the object to call the member function on
     *  @param mptr pointer to the member function to be called
     *  @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
     */
    template<typename T>
    void attach( T* tptr, void (T::*mptr)(void), SerialBase::IrqType type=SerialBase::RxIrq )
    {
        if( (mptr != NULL) && (tptr != NULL) )
        {
            _my_irq[type].attach( tptr, mptr );
            serial_irq_set( &_my_serial, (SerialIrq)type, 1 );
        }
    }

    /** Generate a break condition on the serial line
     */
    void send_break();

    static void _irq_handler( uint32_t id, SerialIrq irq_type );

  protected:
    MySerialBase( PinName tx, PinName rx, PinName rts=RTS_PIN_NUMBER, PinName cts=CTS_PIN_NUMBER );
    virtual ~MySerialBase()
    {
    }

    void my_serial_init( serial_t *obj, PinName tx, PinName rx, PinName rts, PinName cts, int baudrate );

    int _base_getc();
    int _base_putc( int c );

    serial_t        _my_serial;
    FunctionPointer _my_irq[2];
    int             _my_baud;
};

class MySerial : public MySerialBase, public Stream
{
  public:
    /** Create a Serial port, connected to the specified transmit and receive pins
     *
     *  @param tx Transmit pin
     *  @param rx Receive pin
     *
     *  @note
     *    Either tx or rx may be specified as NC if unused
     */
    MySerial( PinName tx,     PinName rx,    const char *name=NULL,
              PinName rts=NC, PinName cts=NC );  // Defaults to using no flow control.

    /** Write a string to the serial port
     *
     * @param str The string to write
     *
     * @returns 0 if the write succeeds, EOF for error
     */
    int puts( const char *str );

    int printf( const char *format, ... );

    int vprintf( const char *format, va_list arg );

  protected:
    virtual int _getc();
    virtual int _putc( int c );
};

}  // namespace moo

#endif /* MYSERIAL_H */