ser2neo: a serial port to NeoPixel ring bridge.

serial.cc 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Half-duplex interrupt based serial port.
  2. //
  3. // Copyright 2015 Google Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. #include "serial.h"
  18. #include <avr/io.h>
  19. #include <avr/interrupt.h>
  20. #include <avr/sleep.h>
  21. Serial* Serial::instance_;
  22. #define _clear(port, pin) port &= ~_BV(pin)
  23. #define _set(port, pin) port |= _BV(pin)
  24. /// Initialise the hardware.
  25. void Serial::init() {
  26. state_ = Idle;
  27. rx_full_ = false;
  28. instance_ = this;
  29. // Set up the GPIOs.
  30. _set(DDRB, TxPin);
  31. _set(PORTB, TxPin);
  32. _clear(DDRB, RxPin);
  33. // Set up the timer.
  34. static_assert(BitTime > 50, "BitTime is too short");
  35. static_assert(BitTime < 255*2/3, "BitTime may overflow");
  36. OCR1C = BitTime;
  37. TCCR1 = _BV(CTC1) | _BV(PWM1A);
  38. static_assert(Prescale == 2, "Mismatched prescaler");
  39. TCCR1 |= _BV(CS11);
  40. // Set up the pin change interrupt.
  41. PCMSK = _BV(RxPin);
  42. _set(GIMSK, PCIE);
  43. }
  44. /// Handle the pin change interrupt by starting receive.
  45. void Serial::pcint0() {
  46. _clear(PCMSK, RxPin);
  47. _clear(TIMSK, TOV1); // PENDING
  48. OCR1C = (BitTime / 2 * 3) - (PCIntDelay / Prescale);
  49. TCNT1 = 0;
  50. _set(TIFR, TOV1);
  51. state_ = Receive;
  52. bits_ = 8;
  53. _set(TIMSK, TOV1);
  54. }
  55. /// Handle the timer overflow interrupt by doing the next bit.
  56. inline void Serial::timer1ovf() {
  57. switch (state_) {
  58. case Receive: {
  59. auto got = PINB;
  60. OCR1C = BitTime;
  61. if (bits_ > 0) {
  62. rxing_ >>= 1;
  63. if (got & _BV(RxPin)) {
  64. rxing_ |= 0x80;
  65. }
  66. bits_--;
  67. } else {
  68. state_ = Idle;
  69. rxed_ = rxing_;
  70. rx_full_ = true;
  71. }
  72. break;
  73. }
  74. case Transmit:
  75. if (txing_ & 1) {
  76. _set(PORTB, TxPin);
  77. } else {
  78. _clear(PORTB, TxPin);
  79. }
  80. txing_ >>= 1;
  81. if (--bits_ == 0) {
  82. state_ = Idle;
  83. }
  84. break;
  85. default:
  86. break;
  87. }
  88. if (state_ == Idle) {
  89. _clear(TIMSK, TOV1);
  90. _set(PCMSK, RxPin);
  91. }
  92. }
  93. /// Wait until the hardware is free then start the transmit.
  94. void Serial::putch(uint8_t ch) {
  95. while (state_ != Idle) {
  96. sleep_cpu();
  97. }
  98. state_ = Transmit;
  99. txing_ = (ch << 1) | 0xFE00;
  100. bits_ = 10;
  101. TCNT1 = 0;
  102. _set(TIMSK, TOV1);
  103. }
  104. /// Send a string.
  105. void Serial::putstr(const char* str) {
  106. for (; *str != '\0'; str++) {
  107. putch(*str);
  108. }
  109. }
  110. /// Wait until a character is received, then return it.
  111. uint8_t Serial::getch() {
  112. while (!rx_full_) {
  113. sleep_cpu();
  114. }
  115. uint8_t ch = rxed_;
  116. rx_full_ = false;
  117. return ch;
  118. }
  119. inline void Serial::pcint0_bounce() { instance_->pcint0(); }
  120. inline void Serial::timer1ovf_bounce() { instance_->timer1ovf(); }
  121. ISR(TIMER1_OVF_vect) { Serial::timer1ovf_bounce(); }
  122. ISR(PCINT0_vect) { Serial::pcint0_bounce(); }