| | 389 | } |
| | 390 | |
| | 391 | /** |
| | 392 | Clear out the serial port. |
| | 393 | Ensures that there are no bytes in the incoming buffer. |
| | 394 | |
| | 395 | \b Example |
| | 396 | \code |
| | 397 | Serial_SetActive(1); |
| | 398 | Serial_Flush( ); // after starting up, make sure there's no junk in there |
| | 399 | \endcode |
| | 400 | */ |
| | 401 | void Serial_Flush() |
| | 402 | { |
| | 403 | char c; |
| | 404 | while( Serial_GetReadable() ) |
| | 405 | c = Serial_GetChar( ); |
| | 406 | } |
| | 407 | |
| | 408 | /** |
| | 409 | Reset the error flags in the serial system. |
| | 410 | In the normal course of operation, the serial system may experience |
| | 411 | a variety of different error modes, including buffer overruns, framing |
| | 412 | and parity errors, and more. When in an error state, the serial system |
| | 413 | may behave differently than normal. |
| | 414 | |
| | 415 | Serial_ClearErrors() resets the appropriate status bits to a state of |
| | 416 | normal operation. It will only reset the error states if there are |
| | 417 | currently any errors. |
| | 418 | @see Serial_GetErrors |
| | 419 | |
| | 420 | \b Example |
| | 421 | |
| | 422 | \code |
| | 423 | Serial_ClearErrors(); |
| | 424 | // that's all there is to it. |
| | 425 | \endcode |
| | 426 | */ |
| | 427 | void Serial_ClearErrors( ) |
| | 428 | { |
| | 429 | if( AT91C_BASE_US0->US_CSR & (AT91C_US_OVRE | AT91C_US_FRAME | AT91C_US_PARE) ) |
| | 430 | AT91C_BASE_US0->US_CR = AT91C_US_RSTSTA; // clear all errors |
| | 431 | } |
| | 432 | |
| | 433 | /** |
| | 434 | Read whether there are any errors. |
| | 435 | We can check for three kinds of errors in the serial system: |
| | 436 | - buffer overrun |
| | 437 | - framing error |
| | 438 | - parity error |
| | 439 | |
| | 440 | Each parameter will be set with a true or a false given the current |
| | 441 | error state. If you don't care to check one of the parameters, just |
| | 442 | pass in 0. |
| | 443 | |
| | 444 | @param overrun A bool that will be set with the overrun error state. |
| | 445 | @param frame A bool that will be set with the frame error state. |
| | 446 | @param parity A bool that will be set with the parity error state. |
| | 447 | @return True if there were any errors, false if there were no errors. |
| | 448 | @see Serial_ClearErrors( ) |
| | 449 | |
| | 450 | \b Example |
| | 451 | \code |
| | 452 | bool over, fr, par; |
| | 453 | if( Serial_GetErrors( &over, &fr, &par ) ) |
| | 454 | { |
| | 455 | // if we wanted, we could just clear them all right here with Serial_ClearErrors() |
| | 456 | // but here we'll check to see what kind of errors we got |
| | 457 | if(over) |
| | 458 | { |
| | 459 | // then we have an overrun error |
| | 460 | } |
| | 461 | if(fr) |
| | 462 | { |
| | 463 | // then we have a framing error |
| | 464 | } |
| | 465 | if(par) |
| | 466 | { |
| | 467 | // then we have a parity error |
| | 468 | } |
| | 469 | } |
| | 470 | else |
| | 471 | { |
| | 472 | // there were no errors |
| | 473 | } |
| | 474 | \endcode |
| | 475 | */ |
| | 476 | bool Serial_GetErrors( bool* overrun, bool* frame, bool* parity ) |
| | 477 | { |
| | 478 | bool retval = false; |
| | 479 | |
| | 480 | bool ovre = AT91C_BASE_US0->US_CSR & AT91C_US_OVRE; |
| | 481 | if(ovre) |
| | 482 | retval = true; |
| | 483 | if(overrun) |
| | 484 | *overrun = ovre; |
| | 485 | |
| | 486 | bool fr = AT91C_BASE_US0->US_CSR & AT91C_US_FRAME; |
| | 487 | if(fr) |
| | 488 | retval = true; |
| | 489 | if(frame) |
| | 490 | *frame = fr; |
| | 491 | |
| | 492 | bool par = AT91C_BASE_US0->US_CSR & AT91C_US_PARE; |
| | 493 | if(par) |
| | 494 | retval = true; |
| | 495 | if(parity) |
| | 496 | *parity = par; |
| | 497 | |
| | 498 | return retval; |
| | 499 | } |
| | 500 | |
| | 501 | /** |
| | 502 | Start the transimission of a break. |
| | 503 | This has no effect if a break is already in progress. |
| | 504 | |
| | 505 | \b Example |
| | 506 | |
| | 507 | \code |
| | 508 | Serial_StartBreak(); |
| | 509 | \endcode |
| | 510 | */ |
| | 511 | void Serial_StartBreak( ) |
| | 512 | { |
| | 513 | AT91C_BASE_US0->US_CR = AT91C_US_STTBRK; |
| | 514 | } |
| | 515 | |
| | 516 | /** |
| | 517 | Stop the transimission of a break. |
| | 518 | This has no effect if there's not a break already in progress. |
| | 519 | |
| | 520 | \b Example |
| | 521 | |
| | 522 | \code |
| | 523 | Serial_StopBreak(); |
| | 524 | \endcode |
| | 525 | */ |
| | 526 | void Serial_StopBreak( ) |
| | 527 | { |
| | 528 | AT91C_BASE_US0->US_CR = AT91C_US_STPBRK; |