Andriy Makukha / Mbed 2 deprecated football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Committer:
AntonLS
Date:
Tue Jun 30 21:45:21 2015 +0000
Revision:
15:b86c4b798aa1
Parent:
9:95dc84e9fb7f
No longer needs mbed-src--Uses function patch feature to accomplish same.

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 6:ef758ac3c928 16
AntonLS 8:d5d055be2bb8 17 void UART0_IRQHandler(); // Make sure UART0_IRQHandler is commented-out in serial_api.c (rebuild mbed lib.)
AntonLS 8:d5d055be2bb8 18 // TODO Maybe move all serial_api.c workarounds into serial_api.c
AntonLS 15:b86c4b798aa1 19 // NOTE: Source of lib no longer needed to comment-out IRQ handler--
AntonLS 15:b86c4b798aa1 20 // Now overridden w/ $Sub$$UART0_IRQHandler()
AntonLS 1:0ba687d4196f 21 }
AntonLS 1:0ba687d4196f 22
AntonLS 1:0ba687d4196f 23 extern int stdio_uart_inited;
AntonLS 1:0ba687d4196f 24 extern serial_t stdio_uart;
AntonLS 1:0ba687d4196f 25
AntonLS 6:ef758ac3c928 26 // Our versions of serial_api.c's...
AntonLS 8:d5d055be2bb8 27 // (serial_free(), etc. in serial_api.c won't work when use our own copy.) TODO Maybe make one in serial_api.c accessible.
AntonLS 6:ef758ac3c928 28 #define UART_NUM 1
AntonLS 6:ef758ac3c928 29 static uint32_t serial_irq_ids[UART_NUM] = {0};
AntonLS 6:ef758ac3c928 30 static uart_irq_handler irq_handler;
AntonLS 6:ef758ac3c928 31
AntonLS 1:0ba687d4196f 32
AntonLS 1:0ba687d4196f 33 using namespace moo;
AntonLS 1:0ba687d4196f 34
AntonLS 1:0ba687d4196f 35
AntonLS 1:0ba687d4196f 36 MySerialBase::MySerialBase( PinName tx, PinName rx,
AntonLS 1:0ba687d4196f 37 PinName rts, PinName cts ) : _my_serial(), _my_baud( 9600 )
AntonLS 1:0ba687d4196f 38 {
AntonLS 2:fe1566cdb6e7 39 my_serial_init( &_my_serial, tx, rx, rts, cts, _my_baud );
AntonLS 8:d5d055be2bb8 40 // serial_irq_handler( &_my_serial, _irq_handler, (uint32_t)this ); // serial_api.c only calls handler for Tx & Rx.
AntonLS 7:205ef63d311a 41
AntonLS 8:d5d055be2bb8 42 // Won't work unless UART0_IRQHandler is commented-out in serial_api.c (rebuild mbed lib.)
AntonLS 8:d5d055be2bb8 43 my_serial_irq_handler( &_my_serial, _irq_handler, (uint32_t)this );
AntonLS 5:1b9734e68327 44
AntonLS 8:d5d055be2bb8 45 // For uart errors -- Won't work unless UART0_IRQHandler is commented-out in serial_api.c (rebuild mbed lib.)
AntonLS 8:d5d055be2bb8 46 attach( this, &MySerialBase::error_handler, (Serial::IrqType)ErrIrq );
AntonLS 8:d5d055be2bb8 47 // For cts -- Won't work unless UART0_IRQHandler is commented-out in serial_api.c (rebuild mbed lib.)
AntonLS 8:d5d055be2bb8 48 attach( this, &MySerialBase::ncts_handler, (Serial::IrqType)NctsIrq );
AntonLS 8:d5d055be2bb8 49 attach( this, &MySerialBase::cts_handler, (Serial::IrqType)CtsIrq );
AntonLS 1:0ba687d4196f 50 }
AntonLS 1:0ba687d4196f 51 void MySerialBase::baud( int baudrate )
AntonLS 1:0ba687d4196f 52 {
AntonLS 1:0ba687d4196f 53 serial_baud( &_my_serial, baudrate );
AntonLS 1:0ba687d4196f 54 _my_baud = baudrate;
AntonLS 1:0ba687d4196f 55 }
AntonLS 1:0ba687d4196f 56 void MySerialBase::format( int bits, SerialBase::Parity parity, int stop_bits )
AntonLS 1:0ba687d4196f 57 {
AntonLS 1:0ba687d4196f 58 serial_format( &_my_serial, bits, (SerialParity)parity, stop_bits );
AntonLS 1:0ba687d4196f 59 }
AntonLS 1:0ba687d4196f 60 int MySerialBase::readable()
AntonLS 1:0ba687d4196f 61 {
AntonLS 1:0ba687d4196f 62 return serial_readable( &_my_serial );
AntonLS 1:0ba687d4196f 63 }
AntonLS 1:0ba687d4196f 64 int MySerialBase::writeable()
AntonLS 1:0ba687d4196f 65 {
AntonLS 1:0ba687d4196f 66 return serial_writable( &_my_serial );
AntonLS 1:0ba687d4196f 67 }
AntonLS 5:1b9734e68327 68 void MySerialBase::attach( void (*fptr)(void), Serial::IrqType type )
AntonLS 1:0ba687d4196f 69 {
AntonLS 1:0ba687d4196f 70 if( fptr )
AntonLS 1:0ba687d4196f 71 {
AntonLS 1:0ba687d4196f 72 _my_irq[type].attach( fptr );
AntonLS 5:1b9734e68327 73 my_serial_irq_set( &_my_serial, (IrqType)type, 1 );
AntonLS 5:1b9734e68327 74
AntonLS 1:0ba687d4196f 75 } else
AntonLS 5:1b9734e68327 76 {
AntonLS 5:1b9734e68327 77 my_serial_irq_set( &_my_serial, (IrqType)type, 0 );
AntonLS 5:1b9734e68327 78 }
AntonLS 1:0ba687d4196f 79 }
AntonLS 2:fe1566cdb6e7 80 void MySerialBase::my_serial_init( serial_t *obj, PinName tx, PinName rx, PinName rts, PinName cts, int baudrate )
AntonLS 2:fe1566cdb6e7 81 {
AntonLS 2:fe1566cdb6e7 82 UARTName uart = UART_0;
AntonLS 2:fe1566cdb6e7 83 obj->uart = (NRF_UART_Type *)uart;
AntonLS 2:fe1566cdb6e7 84
AntonLS 2:fe1566cdb6e7 85 // pin configurations --
AntonLS 2:fe1566cdb6e7 86 NRF_GPIO->DIR |= (1 << tx); // TX_PIN_NUMBER
AntonLS 2:fe1566cdb6e7 87 NRF_GPIO->DIR |= ((NC == rts) ? 0 : (1 << rts)); // RTS_PIN_NUMBER
AntonLS 2:fe1566cdb6e7 88
AntonLS 2:fe1566cdb6e7 89 NRF_GPIO->DIR &= ~(1 << rx); // RX_PIN_NUMBER
AntonLS 2:fe1566cdb6e7 90 NRF_GPIO->DIR &= ~((NC == cts) ? 0 : (1 << cts)); // CTS_PIN_NUMBER
AntonLS 2:fe1566cdb6e7 91
AntonLS 2:fe1566cdb6e7 92 obj->uart->PSELRTS = ((NC == rts) ? 0xFFFFFFFF : rts); // RTS_PIN_NUMBER
AntonLS 2:fe1566cdb6e7 93 obj->uart->PSELTXD = tx; // TX_PIN_NUMBER
AntonLS 2:fe1566cdb6e7 94 obj->uart->PSELCTS = ((NC == cts) ? 0xFFFFFFFF : cts); // CTS_PIN_NUMBER
AntonLS 2:fe1566cdb6e7 95 obj->uart->PSELRXD = rx; // RX_PIN_NUMBER
AntonLS 2:fe1566cdb6e7 96
AntonLS 9:95dc84e9fb7f 97 // set default baud rate and format
AntonLS 9:95dc84e9fb7f 98 serial_baud ( obj, baudrate );
AntonLS 9:95dc84e9fb7f 99 serial_format( obj, 8, ParityNone, 1 );
AntonLS 9:95dc84e9fb7f 100
AntonLS 2:fe1566cdb6e7 101 if( (NC != rts) || (NC != cts) )
AntonLS 2:fe1566cdb6e7 102 {
AntonLS 9:95dc84e9fb7f 103 obj->uart->CONFIG |= UART_CONFIG_HWFC_Msk; // Enable HWFC
AntonLS 2:fe1566cdb6e7 104
AntonLS 2:fe1566cdb6e7 105 } else
AntonLS 2:fe1566cdb6e7 106 {
AntonLS 9:95dc84e9fb7f 107 obj->uart->CONFIG &= ~UART_CONFIG_HWFC_Msk; // Disable HWFC;
AntonLS 2:fe1566cdb6e7 108 }
AntonLS 2:fe1566cdb6e7 109
AntonLS 2:fe1566cdb6e7 110 obj->uart->ENABLE = (UART_ENABLE_ENABLE_Enabled << UART_ENABLE_ENABLE_Pos);
AntonLS 2:fe1566cdb6e7 111 obj->uart->TASKS_STARTTX = 1;
AntonLS 2:fe1566cdb6e7 112 obj->uart->TASKS_STARTRX = 1;
AntonLS 2:fe1566cdb6e7 113 obj->uart->EVENTS_RXDRDY = 0;
AntonLS 2:fe1566cdb6e7 114 // dummy write needed or TXDRDY trails write rather than leads write.
AntonLS 2:fe1566cdb6e7 115 // pins are disconnected so nothing is physically transmitted on the wire
AntonLS 2:fe1566cdb6e7 116 obj->uart->TXD = 0;
AntonLS 2:fe1566cdb6e7 117
AntonLS 2:fe1566cdb6e7 118 obj->index = 0;
AntonLS 9:95dc84e9fb7f 119
AntonLS 2:fe1566cdb6e7 120 // set rx/tx pins in PullUp mode
AntonLS 2:fe1566cdb6e7 121 if (tx != NC) {
AntonLS 2:fe1566cdb6e7 122 pin_mode(tx, PullUp);
AntonLS 2:fe1566cdb6e7 123 }
AntonLS 2:fe1566cdb6e7 124 if (rx != NC) {
AntonLS 2:fe1566cdb6e7 125 pin_mode(rx, PullUp);
AntonLS 2:fe1566cdb6e7 126 }
AntonLS 2:fe1566cdb6e7 127
AntonLS 2:fe1566cdb6e7 128 // Set CTS pin to PullDown mode if used.
AntonLS 2:fe1566cdb6e7 129 if( cts != NC )
AntonLS 2:fe1566cdb6e7 130 {
AntonLS 2:fe1566cdb6e7 131 pin_mode( cts, PullDown );
AntonLS 2:fe1566cdb6e7 132 }
AntonLS 2:fe1566cdb6e7 133
AntonLS 2:fe1566cdb6e7 134
AntonLS 2:fe1566cdb6e7 135 if (uart == STDIO_UART) {
AntonLS 2:fe1566cdb6e7 136 stdio_uart_inited = 1;
AntonLS 2:fe1566cdb6e7 137 memcpy(&stdio_uart, obj, sizeof(serial_t));
AntonLS 2:fe1566cdb6e7 138 }
AntonLS 2:fe1566cdb6e7 139 }
AntonLS 7:205ef63d311a 140
AntonLS 8:d5d055be2bb8 141 // Won't work unless UART0_IRQHandler is commented-out in serial_api.c (rebuild mbed lib.)
AntonLS 6:ef758ac3c928 142 void MySerialBase::my_serial_irq_handler( serial_t *obj, uart_irq_handler handler, uint32_t id )
AntonLS 6:ef758ac3c928 143 {
AntonLS 6:ef758ac3c928 144 irq_handler = handler;
AntonLS 6:ef758ac3c928 145 serial_irq_ids[obj->index] = id;
AntonLS 6:ef758ac3c928 146 }
AntonLS 8:d5d055be2bb8 147 //
AntonLS 6:ef758ac3c928 148
AntonLS 8:d5d055be2bb8 149 // Replacement for serial_irq_set() in serial_api.c so we can grab uart errors.
AntonLS 5:1b9734e68327 150 void MySerialBase::my_serial_irq_set( serial_t *obj, IrqType irq, uint32_t enable )
AntonLS 5:1b9734e68327 151 {
AntonLS 5:1b9734e68327 152 IRQn_Type irq_n = (IRQn_Type)0;
AntonLS 5:1b9734e68327 153
AntonLS 5:1b9734e68327 154 switch( (int)obj->uart )
AntonLS 5:1b9734e68327 155 {
AntonLS 5:1b9734e68327 156 case UART_0:
AntonLS 5:1b9734e68327 157 irq_n = UART0_IRQn;
AntonLS 5:1b9734e68327 158 break;
AntonLS 5:1b9734e68327 159 }
AntonLS 5:1b9734e68327 160
AntonLS 5:1b9734e68327 161 if( enable )
AntonLS 5:1b9734e68327 162 {
AntonLS 5:1b9734e68327 163 switch( irq )
AntonLS 5:1b9734e68327 164 {
AntonLS 5:1b9734e68327 165 case RxIrq:
AntonLS 8:d5d055be2bb8 166 obj->uart->INTENSET = UART_INTENSET_RXDRDY_Msk;
AntonLS 5:1b9734e68327 167 break;
AntonLS 5:1b9734e68327 168 case TxIrq:
AntonLS 8:d5d055be2bb8 169 obj->uart->INTENSET = UART_INTENSET_TXDRDY_Msk;
AntonLS 5:1b9734e68327 170 break;
AntonLS 5:1b9734e68327 171 case ErrIrq:
AntonLS 8:d5d055be2bb8 172 obj->uart->INTENSET = UART_INTENSET_ERROR_Msk;
AntonLS 5:1b9734e68327 173 break;
AntonLS 7:205ef63d311a 174 case CtsIrq:
AntonLS 8:d5d055be2bb8 175 obj->uart->INTENSET = UART_INTENSET_CTS_Msk;
AntonLS 7:205ef63d311a 176 break;
AntonLS 7:205ef63d311a 177 case NctsIrq:
AntonLS 8:d5d055be2bb8 178 obj->uart->INTENSET = UART_INTENSET_NCTS_Msk;
AntonLS 7:205ef63d311a 179 break;
AntonLS 7:205ef63d311a 180 case RxtoIrq:
AntonLS 8:d5d055be2bb8 181 obj->uart->INTENSET = UART_INTENSET_RXTO_Msk;
AntonLS 7:205ef63d311a 182 break;
AntonLS 5:1b9734e68327 183 }
AntonLS 5:1b9734e68327 184 NVIC_SetPriority( irq_n, 3 );
AntonLS 5:1b9734e68327 185 NVIC_EnableIRQ( irq_n );
AntonLS 5:1b9734e68327 186
AntonLS 5:1b9734e68327 187 } else
AntonLS 8:d5d055be2bb8 188 { // Disable
AntonLS 5:1b9734e68327 189 switch( irq )
AntonLS 5:1b9734e68327 190 {
AntonLS 5:1b9734e68327 191 case RxIrq:
AntonLS 8:d5d055be2bb8 192 obj->uart->INTENCLR = UART_INTENCLR_RXDRDY_Msk;
AntonLS 5:1b9734e68327 193 break;
AntonLS 5:1b9734e68327 194 case TxIrq:
AntonLS 8:d5d055be2bb8 195 obj->uart->INTENCLR = UART_INTENCLR_TXDRDY_Msk;
AntonLS 5:1b9734e68327 196 break;
AntonLS 5:1b9734e68327 197 case ErrIrq:
AntonLS 8:d5d055be2bb8 198 obj->uart->INTENCLR = UART_INTENCLR_ERROR_Msk;
AntonLS 5:1b9734e68327 199 break;
AntonLS 7:205ef63d311a 200 case CtsIrq:
AntonLS 8:d5d055be2bb8 201 obj->uart->INTENCLR = UART_INTENCLR_CTS_Msk;
AntonLS 7:205ef63d311a 202 break;
AntonLS 7:205ef63d311a 203 case NctsIrq:
AntonLS 8:d5d055be2bb8 204 obj->uart->INTENCLR = UART_INTENCLR_NCTS_Msk;
AntonLS 7:205ef63d311a 205 break;
AntonLS 7:205ef63d311a 206 case RxtoIrq:
AntonLS 8:d5d055be2bb8 207 obj->uart->INTENCLR = UART_INTENCLR_RXTO_Msk;
AntonLS 7:205ef63d311a 208 break;
AntonLS 5:1b9734e68327 209 }
AntonLS 5:1b9734e68327 210
AntonLS 5:1b9734e68327 211 if( 0 == obj->uart->INTENCLR )
AntonLS 5:1b9734e68327 212 {
AntonLS 5:1b9734e68327 213 NVIC_DisableIRQ( irq_n );
AntonLS 5:1b9734e68327 214 }
AntonLS 5:1b9734e68327 215 }
AntonLS 5:1b9734e68327 216 }
AntonLS 1:0ba687d4196f 217 void MySerialBase::_irq_handler( uint32_t id, SerialIrq irq_type )
AntonLS 1:0ba687d4196f 218 {
AntonLS 1:0ba687d4196f 219 MySerialBase *handler = (MySerialBase*)id;
AntonLS 1:0ba687d4196f 220 handler->_my_irq[irq_type].call();
AntonLS 1:0ba687d4196f 221 }
AntonLS 6:ef758ac3c928 222
AntonLS 8:d5d055be2bb8 223 // Won't work unless UART0_IRQHandler is commented-out in serial_api.c (rebuild mbed lib.)
AntonLS 5:1b9734e68327 224 void MySerialBase::error_handler()
AntonLS 5:1b9734e68327 225 {
AntonLS 8:d5d055be2bb8 226 // Apparently handler will be continuously called until [error] condition is gone even though cleared.
AntonLS 5:1b9734e68327 227
AntonLS 5:1b9734e68327 228 if( (UART_ERRORSRC_OVERRUN_Present << UART_ERRORSRC_OVERRUN_Pos) == (UART_ERRORSRC_OVERRUN_Msk & _my_serial.uart->ERRORSRC) )
AntonLS 5:1b9734e68327 229 {
AntonLS 5:1b9734e68327 230 puts( "\r\n[ERR: OVERRUN]\r\n" );
AntonLS 5:1b9734e68327 231
AntonLS 5:1b9734e68327 232 // Clear the error
AntonLS 5:1b9734e68327 233 _my_serial.uart->ERRORSRC = (UART_ERRORSRC_OVERRUN_Clear << UART_ERRORSRC_OVERRUN_Pos);
AntonLS 8:d5d055be2bb8 234
AntonLS 8:d5d055be2bb8 235 } else if( (UART_ERRORSRC_PARITY_Present << UART_ERRORSRC_PARITY_Pos) == (UART_ERRORSRC_PARITY_Msk & _my_serial.uart->ERRORSRC) )
AntonLS 8:d5d055be2bb8 236 {
AntonLS 8:d5d055be2bb8 237 puts( "\r\n[ERR: PARITY]\r\n" );
AntonLS 8:d5d055be2bb8 238
AntonLS 8:d5d055be2bb8 239 // Clear the error
AntonLS 8:d5d055be2bb8 240 _my_serial.uart->ERRORSRC = (UART_ERRORSRC_PARITY_Clear << UART_ERRORSRC_PARITY_Pos);
AntonLS 8:d5d055be2bb8 241
AntonLS 8:d5d055be2bb8 242 } else if( (UART_ERRORSRC_FRAMING_Present << UART_ERRORSRC_FRAMING_Pos) == (UART_ERRORSRC_FRAMING_Msk & _my_serial.uart->ERRORSRC) )
AntonLS 8:d5d055be2bb8 243 {
AntonLS 8:d5d055be2bb8 244 puts( "\r\n[ERR: FRAMING]\r\n" );
AntonLS 8:d5d055be2bb8 245
AntonLS 8:d5d055be2bb8 246 // Clear the error
AntonLS 8:d5d055be2bb8 247 _my_serial.uart->ERRORSRC = (UART_ERRORSRC_FRAMING_Clear << UART_ERRORSRC_FRAMING_Pos);
AntonLS 8:d5d055be2bb8 248
AntonLS 8:d5d055be2bb8 249 } else if( (UART_ERRORSRC_BREAK_Present << UART_ERRORSRC_BREAK_Pos) == (UART_ERRORSRC_BREAK_Msk & _my_serial.uart->ERRORSRC) )
AntonLS 8:d5d055be2bb8 250 {
AntonLS 8:d5d055be2bb8 251 puts( "\r\n[ERR: BREAK]\r\n" );
AntonLS 8:d5d055be2bb8 252
AntonLS 8:d5d055be2bb8 253 // Clear the error
AntonLS 8:d5d055be2bb8 254 _my_serial.uart->ERRORSRC = (UART_ERRORSRC_BREAK_Clear << UART_ERRORSRC_BREAK_Pos);
AntonLS 8:d5d055be2bb8 255
AntonLS 8:d5d055be2bb8 256 } else
AntonLS 8:d5d055be2bb8 257 {
AntonLS 8:d5d055be2bb8 258 // Clear the error interrupt...
AntonLS 8:d5d055be2bb8 259 _my_serial.uart->EVENTS_ERROR = 0;
AntonLS 8:d5d055be2bb8 260 }
AntonLS 5:1b9734e68327 261 }
AntonLS 6:ef758ac3c928 262
AntonLS 8:d5d055be2bb8 263 // Won't work unless UART0_IRQHandler is commented-out in serial_api.c (rebuild mbed lib.)
AntonLS 6:ef758ac3c928 264 void MySerialBase::cts_handler()
AntonLS 6:ef758ac3c928 265 {
AntonLS 8:d5d055be2bb8 266 // Clear interrupt.
AntonLS 8:d5d055be2bb8 267 _my_serial.uart->EVENTS_CTS = 0;
AntonLS 8:d5d055be2bb8 268
AntonLS 8:d5d055be2bb8 269 // puts( "\r\n[CTS: Clear]\r\n" );
AntonLS 8:d5d055be2bb8 270 }
AntonLS 8:d5d055be2bb8 271
AntonLS 8:d5d055be2bb8 272 // Won't work unless UART0_IRQHandler is commented-out in serial_api.c (rebuild mbed lib.)
AntonLS 8:d5d055be2bb8 273 void MySerialBase::ncts_handler()
AntonLS 8:d5d055be2bb8 274 {
AntonLS 8:d5d055be2bb8 275 // Attempt to clear interrupt.
AntonLS 8:d5d055be2bb8 276 _my_serial.uart->EVENTS_NCTS = 0;
AntonLS 8:d5d055be2bb8 277
AntonLS 6:ef758ac3c928 278 puts( "\r\n[CTS: High]\r\n" );
AntonLS 6:ef758ac3c928 279 }
AntonLS 6:ef758ac3c928 280
AntonLS 1:0ba687d4196f 281 int MySerialBase::_base_getc()
AntonLS 1:0ba687d4196f 282 {
AntonLS 1:0ba687d4196f 283 return serial_getc( &_my_serial );
AntonLS 1:0ba687d4196f 284 }
AntonLS 1:0ba687d4196f 285 int MySerialBase::_base_putc( int c )
AntonLS 1:0ba687d4196f 286 {
AntonLS 1:0ba687d4196f 287 serial_putc( &_my_serial, c );
AntonLS 1:0ba687d4196f 288 return c;
AntonLS 1:0ba687d4196f 289 }
AntonLS 8:d5d055be2bb8 290
AntonLS 1:0ba687d4196f 291 void MySerialBase::send_break()
AntonLS 1:0ba687d4196f 292 {
AntonLS 1:0ba687d4196f 293 // Wait for 1.5 frames before clearing the break condition
AntonLS 1:0ba687d4196f 294 // This will have different effects on our platforms, but should
AntonLS 1:0ba687d4196f 295 // ensure that we keep the break active for at least one frame.
AntonLS 1:0ba687d4196f 296 // We consider a full frame (1 start bit + 8 data bits bits +
AntonLS 1:0ba687d4196f 297 // 1 parity bit + 2 stop bits = 12 bits) for computation.
AntonLS 1:0ba687d4196f 298 // One bit time (in us) = 1000000/_my_baud
AntonLS 1:0ba687d4196f 299 // Twelve bits: 12000000/baud delay
AntonLS 1:0ba687d4196f 300 // 1.5 frames: 18000000/baud delay
AntonLS 1:0ba687d4196f 301 serial_break_set( &_my_serial );
AntonLS 1:0ba687d4196f 302 wait_us( 18000000/_my_baud );
AntonLS 1:0ba687d4196f 303 serial_break_clear( &_my_serial );
AntonLS 1:0ba687d4196f 304 }
AntonLS 1:0ba687d4196f 305
AntonLS 1:0ba687d4196f 306
AntonLS 1:0ba687d4196f 307 MySerial::MySerial( PinName tx, PinName rx, const char *name,
AntonLS 1:0ba687d4196f 308 PinName rts, PinName cts ) : MySerialBase( tx, rx, rts, cts ), Stream( name )
AntonLS 1:0ba687d4196f 309 {
AntonLS 1:0ba687d4196f 310 }
AntonLS 1:0ba687d4196f 311 int MySerial::_getc()
AntonLS 1:0ba687d4196f 312 {
AntonLS 1:0ba687d4196f 313 return _base_getc();
AntonLS 1:0ba687d4196f 314 }
AntonLS 1:0ba687d4196f 315 int MySerial::_putc( int c )
AntonLS 1:0ba687d4196f 316 {
AntonLS 1:0ba687d4196f 317 return _base_putc( c );
AntonLS 1:0ba687d4196f 318 }
AntonLS 1:0ba687d4196f 319 int MySerial::puts( const char *str )
AntonLS 1:0ba687d4196f 320 {
AntonLS 1:0ba687d4196f 321 while( *str )
AntonLS 1:0ba687d4196f 322 putc( *str ++ );
AntonLS 1:0ba687d4196f 323
AntonLS 1:0ba687d4196f 324 return 0;
AntonLS 1:0ba687d4196f 325 }
AntonLS 4:17b8edf264c3 326
AntonLS 1:0ba687d4196f 327 int MySerial::printf( const char *format, ... )
AntonLS 1:0ba687d4196f 328 {
AntonLS 1:0ba687d4196f 329 va_list arg;
AntonLS 1:0ba687d4196f 330 va_start( arg, format );
AntonLS 1:0ba687d4196f 331
AntonLS 1:0ba687d4196f 332 int len = MySerial::vprintf( format, arg );
AntonLS 1:0ba687d4196f 333
AntonLS 1:0ba687d4196f 334 va_end( arg );
AntonLS 1:0ba687d4196f 335
AntonLS 1:0ba687d4196f 336 return len;
AntonLS 1:0ba687d4196f 337 }
AntonLS 1:0ba687d4196f 338 int MySerial::vprintf( const char *format, va_list arg )
AntonLS 1:0ba687d4196f 339 {
AntonLS 1:0ba687d4196f 340 int len = vsnprintf( NULL, 0, format, arg );
AntonLS 1:0ba687d4196f 341 if( len < STRING_STACK_LIMIT )
AntonLS 1:0ba687d4196f 342 {
AntonLS 1:0ba687d4196f 343 char temp[STRING_STACK_LIMIT];
AntonLS 1:0ba687d4196f 344 vsprintf( temp, format, arg );
AntonLS 1:0ba687d4196f 345 puts( temp );
AntonLS 1:0ba687d4196f 346
AntonLS 1:0ba687d4196f 347 } else
AntonLS 1:0ba687d4196f 348 {
AntonLS 1:0ba687d4196f 349 char *temp = new char[len + 1];
AntonLS 1:0ba687d4196f 350 vsprintf( temp, format, arg );
AntonLS 1:0ba687d4196f 351 puts( temp );
AntonLS 1:0ba687d4196f 352 delete[] temp;
AntonLS 1:0ba687d4196f 353 }
AntonLS 1:0ba687d4196f 354
AntonLS 1:0ba687d4196f 355 return len;
AntonLS 1:0ba687d4196f 356 }
AntonLS 1:0ba687d4196f 357
AntonLS 8:d5d055be2bb8 358 // Won't work unless UART0_IRQHandler is commented-out in serial_api.c (rebuild mbed lib.)
AntonLS 6:ef758ac3c928 359 #ifdef __cplusplus
AntonLS 6:ef758ac3c928 360 extern "C"
AntonLS 6:ef758ac3c928 361 {
AntonLS 6:ef758ac3c928 362 #endif
AntonLS 15:b86c4b798aa1 363 void $Sub$$UART0_IRQHandler()
AntonLS 6:ef758ac3c928 364 {
AntonLS 6:ef758ac3c928 365 MySerial::IrqType irq_type;
AntonLS 6:ef758ac3c928 366
AntonLS 6:ef758ac3c928 367 if( (NRF_UART0->INTENSET & UART_INTENSET_TXDRDY_Msk) && NRF_UART0->EVENTS_TXDRDY )
AntonLS 6:ef758ac3c928 368 {
AntonLS 6:ef758ac3c928 369 irq_type = MySerial::TxIrq;
AntonLS 6:ef758ac3c928 370
AntonLS 6:ef758ac3c928 371 } else if( (NRF_UART0->INTENSET & UART_INTENSET_RXDRDY_Msk) && NRF_UART0->EVENTS_RXDRDY )
AntonLS 6:ef758ac3c928 372 {
AntonLS 6:ef758ac3c928 373 irq_type = MySerial::RxIrq;
AntonLS 6:ef758ac3c928 374
AntonLS 6:ef758ac3c928 375 } else if( (NRF_UART0->INTENSET & UART_INTENSET_ERROR_Msk) && NRF_UART0->EVENTS_ERROR )
AntonLS 6:ef758ac3c928 376 {
AntonLS 6:ef758ac3c928 377 irq_type = MySerial::ErrIrq;
AntonLS 6:ef758ac3c928 378
AntonLS 6:ef758ac3c928 379 } else if( (NRF_UART0->INTENSET & UART_INTENSET_CTS_Msk) && NRF_UART0->EVENTS_CTS )
AntonLS 6:ef758ac3c928 380 {
AntonLS 6:ef758ac3c928 381 irq_type = MySerial::CtsIrq;
AntonLS 6:ef758ac3c928 382
AntonLS 6:ef758ac3c928 383 } else if( (NRF_UART0->INTENSET & UART_INTENSET_NCTS_Msk) && NRF_UART0->EVENTS_NCTS )
AntonLS 6:ef758ac3c928 384 {
AntonLS 6:ef758ac3c928 385 irq_type = MySerial::NctsIrq;
AntonLS 6:ef758ac3c928 386
AntonLS 6:ef758ac3c928 387 } else if( (NRF_UART0->INTENSET & UART_INTENSET_RXTO_Msk) && NRF_UART0->EVENTS_RXTO )
AntonLS 6:ef758ac3c928 388 {
AntonLS 6:ef758ac3c928 389 irq_type = MySerial::RxtoIrq;
AntonLS 6:ef758ac3c928 390
AntonLS 6:ef758ac3c928 391 } else
AntonLS 6:ef758ac3c928 392 {
AntonLS 6:ef758ac3c928 393 return;
AntonLS 6:ef758ac3c928 394 }
AntonLS 6:ef758ac3c928 395
AntonLS 6:ef758ac3c928 396 if( serial_irq_ids[0] != 0 )
AntonLS 6:ef758ac3c928 397 {
AntonLS 6:ef758ac3c928 398 irq_handler( serial_irq_ids[0], (SerialIrq)irq_type );
AntonLS 6:ef758ac3c928 399 }
AntonLS 6:ef758ac3c928 400 }
AntonLS 6:ef758ac3c928 401 #ifdef __cplusplus
AntonLS 6:ef758ac3c928 402 }
AntonLS 6:ef758ac3c928 403 #endif
AntonLS 8:d5d055be2bb8 404 //
AntonLS 7:205ef63d311a 405
AntonLS 1:0ba687d4196f 406 /* EOF */