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:
2:fe1566cdb6e7
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 #include "MySerial.h"
AntonLS 1:0ba687d4196f 12
AntonLS 1:0ba687d4196f 13 extern "C"
AntonLS 1:0ba687d4196f 14 {
AntonLS 1:0ba687d4196f 15 void pin_mode( PinName, PinMode );
AntonLS 1:0ba687d4196f 16 }
AntonLS 1:0ba687d4196f 17
AntonLS 1:0ba687d4196f 18 extern int stdio_uart_inited;
AntonLS 1:0ba687d4196f 19 extern serial_t stdio_uart;
AntonLS 1:0ba687d4196f 20
AntonLS 1:0ba687d4196f 21
AntonLS 1:0ba687d4196f 22 using namespace moo;
AntonLS 1:0ba687d4196f 23
AntonLS 1:0ba687d4196f 24
AntonLS 1:0ba687d4196f 25 MySerialBase::MySerialBase( PinName tx, PinName rx,
AntonLS 1:0ba687d4196f 26 PinName rts, PinName cts ) : _my_serial(), _my_baud( 9600 )
AntonLS 1:0ba687d4196f 27 {
AntonLS 2:fe1566cdb6e7 28 my_serial_init( &_my_serial, tx, rx, rts, cts, _my_baud );
AntonLS 1:0ba687d4196f 29 serial_irq_handler( &_my_serial, _irq_handler, (uint32_t)this );
AntonLS 1:0ba687d4196f 30 }
AntonLS 1:0ba687d4196f 31 void MySerialBase::baud( int baudrate )
AntonLS 1:0ba687d4196f 32 {
AntonLS 1:0ba687d4196f 33 serial_baud( &_my_serial, baudrate );
AntonLS 1:0ba687d4196f 34 _my_baud = baudrate;
AntonLS 1:0ba687d4196f 35 }
AntonLS 1:0ba687d4196f 36 void MySerialBase::format( int bits, SerialBase::Parity parity, int stop_bits )
AntonLS 1:0ba687d4196f 37 {
AntonLS 1:0ba687d4196f 38 serial_format( &_my_serial, bits, (SerialParity)parity, stop_bits );
AntonLS 1:0ba687d4196f 39 }
AntonLS 1:0ba687d4196f 40 int MySerialBase::readable()
AntonLS 1:0ba687d4196f 41 {
AntonLS 1:0ba687d4196f 42 return serial_readable( &_my_serial );
AntonLS 1:0ba687d4196f 43 }
AntonLS 1:0ba687d4196f 44 int MySerialBase::writeable()
AntonLS 1:0ba687d4196f 45 {
AntonLS 1:0ba687d4196f 46 return serial_writable( &_my_serial );
AntonLS 1:0ba687d4196f 47 }
AntonLS 1:0ba687d4196f 48 void MySerialBase::attach( void (*fptr)(void), SerialBase::IrqType type )
AntonLS 1:0ba687d4196f 49 {
AntonLS 1:0ba687d4196f 50 if( fptr )
AntonLS 1:0ba687d4196f 51 {
AntonLS 1:0ba687d4196f 52 _my_irq[type].attach( fptr );
AntonLS 1:0ba687d4196f 53 serial_irq_set( &_my_serial, (SerialIrq)type, 1 );
AntonLS 1:0ba687d4196f 54 } else
AntonLS 1:0ba687d4196f 55 {
AntonLS 1:0ba687d4196f 56 serial_irq_set( &_my_serial, (SerialIrq)type, 0 );
AntonLS 1:0ba687d4196f 57 }
AntonLS 1:0ba687d4196f 58 }
AntonLS 2:fe1566cdb6e7 59 void MySerialBase::my_serial_init( serial_t *obj, PinName tx, PinName rx, PinName rts, PinName cts, int baudrate )
AntonLS 2:fe1566cdb6e7 60 {
AntonLS 2:fe1566cdb6e7 61 UARTName uart = UART_0;
AntonLS 2:fe1566cdb6e7 62 obj->uart = (NRF_UART_Type *)uart;
AntonLS 2:fe1566cdb6e7 63
AntonLS 2:fe1566cdb6e7 64 // pin configurations --
AntonLS 2:fe1566cdb6e7 65 NRF_GPIO->DIR |= (1 << tx); // TX_PIN_NUMBER
AntonLS 2:fe1566cdb6e7 66 NRF_GPIO->DIR |= ((NC == rts) ? 0 : (1 << rts)); // RTS_PIN_NUMBER
AntonLS 2:fe1566cdb6e7 67
AntonLS 2:fe1566cdb6e7 68 NRF_GPIO->DIR &= ~(1 << rx); // RX_PIN_NUMBER
AntonLS 2:fe1566cdb6e7 69 NRF_GPIO->DIR &= ~((NC == cts) ? 0 : (1 << cts)); // CTS_PIN_NUMBER
AntonLS 2:fe1566cdb6e7 70
AntonLS 2:fe1566cdb6e7 71 obj->uart->PSELRTS = ((NC == rts) ? 0xFFFFFFFF : rts); // RTS_PIN_NUMBER
AntonLS 2:fe1566cdb6e7 72 obj->uart->PSELTXD = tx; // TX_PIN_NUMBER
AntonLS 2:fe1566cdb6e7 73 obj->uart->PSELCTS = ((NC == cts) ? 0xFFFFFFFF : cts); // CTS_PIN_NUMBER
AntonLS 2:fe1566cdb6e7 74 obj->uart->PSELRXD = rx; // RX_PIN_NUMBER
AntonLS 2:fe1566cdb6e7 75
AntonLS 2:fe1566cdb6e7 76 if( (NC != rts) || (NC != cts) )
AntonLS 2:fe1566cdb6e7 77 {
AntonLS 2:fe1566cdb6e7 78 obj->uart->CONFIG |= 0x01; // Enable HWFC
AntonLS 2:fe1566cdb6e7 79
AntonLS 2:fe1566cdb6e7 80 } else
AntonLS 2:fe1566cdb6e7 81 {
AntonLS 2:fe1566cdb6e7 82 obj->uart->CONFIG &= ~0x01; // Disable HWFC;
AntonLS 2:fe1566cdb6e7 83 }
AntonLS 2:fe1566cdb6e7 84
AntonLS 2:fe1566cdb6e7 85 // set default baud rate and format
AntonLS 2:fe1566cdb6e7 86 serial_baud ( obj, baudrate );
AntonLS 2:fe1566cdb6e7 87 serial_format( obj, 8, ParityNone, 1 );
AntonLS 2:fe1566cdb6e7 88
AntonLS 2:fe1566cdb6e7 89 obj->uart->ENABLE = (UART_ENABLE_ENABLE_Enabled << UART_ENABLE_ENABLE_Pos);
AntonLS 2:fe1566cdb6e7 90 obj->uart->TASKS_STARTTX = 1;
AntonLS 2:fe1566cdb6e7 91 obj->uart->TASKS_STARTRX = 1;
AntonLS 2:fe1566cdb6e7 92 obj->uart->EVENTS_RXDRDY = 0;
AntonLS 2:fe1566cdb6e7 93 // dummy write needed or TXDRDY trails write rather than leads write.
AntonLS 2:fe1566cdb6e7 94 // pins are disconnected so nothing is physically transmitted on the wire
AntonLS 2:fe1566cdb6e7 95 obj->uart->TXD = 0;
AntonLS 2:fe1566cdb6e7 96
AntonLS 2:fe1566cdb6e7 97 obj->index = 0;
AntonLS 2:fe1566cdb6e7 98
AntonLS 2:fe1566cdb6e7 99 // set rx/tx pins in PullUp mode
AntonLS 2:fe1566cdb6e7 100 if (tx != NC) {
AntonLS 2:fe1566cdb6e7 101 pin_mode(tx, PullUp);
AntonLS 2:fe1566cdb6e7 102 }
AntonLS 2:fe1566cdb6e7 103 if (rx != NC) {
AntonLS 2:fe1566cdb6e7 104 pin_mode(rx, PullUp);
AntonLS 2:fe1566cdb6e7 105 }
AntonLS 2:fe1566cdb6e7 106
AntonLS 2:fe1566cdb6e7 107 // Set CTS pin to PullDown mode if used.
AntonLS 2:fe1566cdb6e7 108 if( cts != NC )
AntonLS 2:fe1566cdb6e7 109 {
AntonLS 2:fe1566cdb6e7 110 pin_mode( cts, PullDown );
AntonLS 2:fe1566cdb6e7 111 }
AntonLS 2:fe1566cdb6e7 112
AntonLS 2:fe1566cdb6e7 113
AntonLS 2:fe1566cdb6e7 114 if (uart == STDIO_UART) {
AntonLS 2:fe1566cdb6e7 115 stdio_uart_inited = 1;
AntonLS 2:fe1566cdb6e7 116 memcpy(&stdio_uart, obj, sizeof(serial_t));
AntonLS 2:fe1566cdb6e7 117 }
AntonLS 2:fe1566cdb6e7 118 }
AntonLS 1:0ba687d4196f 119 void MySerialBase::_irq_handler( uint32_t id, SerialIrq irq_type )
AntonLS 1:0ba687d4196f 120 {
AntonLS 1:0ba687d4196f 121 MySerialBase *handler = (MySerialBase*)id;
AntonLS 1:0ba687d4196f 122 handler->_my_irq[irq_type].call();
AntonLS 1:0ba687d4196f 123 }
AntonLS 1:0ba687d4196f 124 int MySerialBase::_base_getc()
AntonLS 1:0ba687d4196f 125 {
AntonLS 1:0ba687d4196f 126 return serial_getc( &_my_serial );
AntonLS 1:0ba687d4196f 127 }
AntonLS 1:0ba687d4196f 128 int MySerialBase::_base_putc( int c )
AntonLS 1:0ba687d4196f 129 {
AntonLS 1:0ba687d4196f 130 serial_putc( &_my_serial, c );
AntonLS 1:0ba687d4196f 131 return c;
AntonLS 1:0ba687d4196f 132 }
AntonLS 1:0ba687d4196f 133 void MySerialBase::send_break()
AntonLS 1:0ba687d4196f 134 {
AntonLS 1:0ba687d4196f 135 // Wait for 1.5 frames before clearing the break condition
AntonLS 1:0ba687d4196f 136 // This will have different effects on our platforms, but should
AntonLS 1:0ba687d4196f 137 // ensure that we keep the break active for at least one frame.
AntonLS 1:0ba687d4196f 138 // We consider a full frame (1 start bit + 8 data bits bits +
AntonLS 1:0ba687d4196f 139 // 1 parity bit + 2 stop bits = 12 bits) for computation.
AntonLS 1:0ba687d4196f 140 // One bit time (in us) = 1000000/_my_baud
AntonLS 1:0ba687d4196f 141 // Twelve bits: 12000000/baud delay
AntonLS 1:0ba687d4196f 142 // 1.5 frames: 18000000/baud delay
AntonLS 1:0ba687d4196f 143 serial_break_set( &_my_serial );
AntonLS 1:0ba687d4196f 144 wait_us( 18000000/_my_baud );
AntonLS 1:0ba687d4196f 145 serial_break_clear( &_my_serial );
AntonLS 1:0ba687d4196f 146 }
AntonLS 1:0ba687d4196f 147
AntonLS 1:0ba687d4196f 148
AntonLS 1:0ba687d4196f 149 MySerial::MySerial( PinName tx, PinName rx, const char *name,
AntonLS 1:0ba687d4196f 150 PinName rts, PinName cts ) : MySerialBase( tx, rx, rts, cts ), Stream( name )
AntonLS 1:0ba687d4196f 151 {
AntonLS 1:0ba687d4196f 152 }
AntonLS 1:0ba687d4196f 153 int MySerial::_getc()
AntonLS 1:0ba687d4196f 154 {
AntonLS 1:0ba687d4196f 155 return _base_getc();
AntonLS 1:0ba687d4196f 156 }
AntonLS 1:0ba687d4196f 157 int MySerial::_putc( int c )
AntonLS 1:0ba687d4196f 158 {
AntonLS 1:0ba687d4196f 159 return _base_putc( c );
AntonLS 1:0ba687d4196f 160 }
AntonLS 1:0ba687d4196f 161 int MySerial::puts( const char *str )
AntonLS 1:0ba687d4196f 162 {
AntonLS 1:0ba687d4196f 163 while( *str )
AntonLS 1:0ba687d4196f 164 putc( *str ++ );
AntonLS 1:0ba687d4196f 165
AntonLS 1:0ba687d4196f 166 return 0;
AntonLS 1:0ba687d4196f 167 }
AntonLS 4:17b8edf264c3 168
AntonLS 1:0ba687d4196f 169 int MySerial::printf( const char *format, ... )
AntonLS 1:0ba687d4196f 170 {
AntonLS 1:0ba687d4196f 171 va_list arg;
AntonLS 1:0ba687d4196f 172 va_start( arg, format );
AntonLS 1:0ba687d4196f 173
AntonLS 1:0ba687d4196f 174 int len = MySerial::vprintf( format, arg );
AntonLS 1:0ba687d4196f 175
AntonLS 1:0ba687d4196f 176 va_end( arg );
AntonLS 1:0ba687d4196f 177
AntonLS 1:0ba687d4196f 178 return len;
AntonLS 1:0ba687d4196f 179 }
AntonLS 1:0ba687d4196f 180 int MySerial::vprintf( const char *format, va_list arg )
AntonLS 1:0ba687d4196f 181 {
AntonLS 1:0ba687d4196f 182 int len = vsnprintf( NULL, 0, format, arg );
AntonLS 1:0ba687d4196f 183 if( len < STRING_STACK_LIMIT )
AntonLS 1:0ba687d4196f 184 {
AntonLS 1:0ba687d4196f 185 char temp[STRING_STACK_LIMIT];
AntonLS 1:0ba687d4196f 186 vsprintf( temp, format, arg );
AntonLS 1:0ba687d4196f 187 puts( temp );
AntonLS 1:0ba687d4196f 188
AntonLS 1:0ba687d4196f 189 } else
AntonLS 1:0ba687d4196f 190 {
AntonLS 1:0ba687d4196f 191 char *temp = new char[len + 1];
AntonLS 1:0ba687d4196f 192 vsprintf( temp, format, arg );
AntonLS 1:0ba687d4196f 193 puts( temp );
AntonLS 1:0ba687d4196f 194 delete[] temp;
AntonLS 1:0ba687d4196f 195 }
AntonLS 1:0ba687d4196f 196
AntonLS 1:0ba687d4196f 197 return len;
AntonLS 1:0ba687d4196f 198 }
AntonLS 1:0ba687d4196f 199
AntonLS 1:0ba687d4196f 200 /* EOF */