| 473 | } |
| 474 | |
| 475 | size_t SoftwareSerial::write(uint8_t b) |
| 476 | { |
| 477 | if (_tx_delay == 0) { |
| 478 | setWriteError(); |
| 479 | return 0; |
| 480 | } |
| 481 | |
| 482 | // By declaring these as local variables, the compiler will put them |
| 483 | // in registers _before_ disabling interrupts and entering the |
| 484 | // critical timing sections below, which makes it a lot easier to |
| 485 | // verify the cycle timings |
| 486 | volatile uint8_t *reg = _transmitPortRegister; |
| 487 | uint8_t reg_mask = _transmitBitMask; |
| 488 | uint8_t inv_mask = ~_transmitBitMask; |
| 489 | uint8_t oldSREG = SREG; |
| 490 | bool inv = _inverse_logic; |
| 491 | uint16_t delay = _tx_delay; |
| 492 | |
| 493 | if (inv) |
| 494 | b = ~b; |
| 495 | |
| 496 | cli(); // turn off interrupts for a clean txmit |
| 497 | |
| 498 | // Write the start bit |
| 499 | if (inv) |
| 500 | *reg |= reg_mask; |
| 501 | else |
| 502 | *reg &= inv_mask; |
| 503 | |
| 504 | tunedDelay(delay); |
| 505 | |
| 506 | // Write each of the 8 bits |
| 507 | for (uint8_t i = 8; i > 0; --i) |
| 508 | { |
| 509 | if (b & 1) // choose bit |
| 510 | *reg |= reg_mask; // send 1 |
| 511 | else |
| 512 | *reg &= inv_mask; // send 0 |
| 513 | |
| 514 | tunedDelay(delay); |
| 515 | b >>= 1; |
| 516 | } |
| 517 | |
| 518 | // restore pin to natural state |
| 519 | if (inv) |
| 520 | *reg &= inv_mask; |
| 521 | else |
| 522 | *reg |= reg_mask; |
| 523 | |
| 524 | SREG = oldSREG; // turn interrupts back on |
| 525 | tunedDelay(_tx_delay); |
| 526 | |
| 527 | return 1; |
| 528 | } |
| 529 | |
| 530 | void SoftwareSerial::flush() |
| 531 | { |
nothing calls this directly
no outgoing calls
no test coverage detected