Andriy Makukha / Mbed 2 deprecated football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Committer:
AntonLS
Date:
Mon Apr 13 12:23:44 2015 +0000
Revision:
1:0ba687d4196f
Child:
2:fe1566cdb6e7
Blah

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