Andriy Makukha / Mbed 2 deprecated football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Committer:
AntonLS
Date:
Mon Apr 13 14:02:48 2015 +0000
Revision:
2:fe1566cdb6e7
Parent:
1:0ba687d4196f
Child:
4:17b8edf264c3
Working

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