SoftSerial.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * This file is free software: you can redistribute it and/or modify it
  3. * under the terms of the GNU General Public License as published by the
  4. * Free Software Foundation, either version 3 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This file is distributed in the hope that it will be useful, but
  8. * WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. * See the GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License along
  13. * with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. /*
  16. soft serial receive implementation, based on pulse width inputs
  17. */
  18. #include "SoftSerial.h"
  19. #include <stdio.h>
  20. SoftSerial::SoftSerial(uint32_t _baudrate, serial_config _config) :
  21. baudrate(_baudrate),
  22. config(_config),
  23. half_bit((1000000U / baudrate)/2)
  24. {
  25. switch (config) {
  26. case SERIAL_CONFIG_8N1:
  27. data_width = 8;
  28. byte_width = 10;
  29. stop_mask = 0x200;
  30. break;
  31. case SERIAL_CONFIG_8E2I:
  32. data_width = 9;
  33. byte_width = 12;
  34. stop_mask = 0xC00;
  35. break;
  36. }
  37. }
  38. /*
  39. process a pulse made up of a width of values at high voltage
  40. followed by a width at low voltage
  41. */
  42. bool SoftSerial::process_pulse(uint32_t width_high, uint32_t width_low, uint8_t &byte)
  43. {
  44. // convert to bit widths, allowing for a half bit error
  45. uint16_t bits_high = ((width_high+half_bit)*baudrate) / 1000000;
  46. uint16_t bits_low = ((width_low+half_bit)*baudrate) / 1000000;
  47. byte_timestamp_us = timestamp_us;
  48. timestamp_us += (width_high + width_low);
  49. if (bits_high == 0 || bits_low == 0) {
  50. // invalid data
  51. goto reset;
  52. }
  53. if (bits_high >= byte_width) {
  54. // if we have a start bit and a stop bit then we can have at
  55. // most 9 bits in high state for data. The rest must be idle
  56. // bits
  57. bits_high = byte_width-1;
  58. }
  59. if (state.bit_ofs == 0) {
  60. // we are in idle state, waiting for first low bit. swallow
  61. // the high bits
  62. bits_high = 0;
  63. }
  64. state.byte |= ((1U<<bits_high)-1) << state.bit_ofs;
  65. state.bit_ofs += bits_high + bits_low;
  66. if (state.bit_ofs >= byte_width) {
  67. // check start bit
  68. if ((state.byte & 1) != 0) {
  69. goto reset;
  70. }
  71. // check stop bits
  72. if ((state.byte & stop_mask) != stop_mask) {
  73. goto reset;
  74. }
  75. if (config == SERIAL_CONFIG_8E2I) {
  76. // check parity
  77. if (__builtin_parity((state.byte>>1)&0xFF) != (state.byte&0x200)>>9) {
  78. goto reset;
  79. }
  80. }
  81. byte = ((state.byte>>1) & 0xFF);
  82. state.byte >>= byte_width;
  83. state.bit_ofs -= byte_width;
  84. if (state.bit_ofs > byte_width) {
  85. state.byte = 0;
  86. state.bit_ofs = bits_low;
  87. }
  88. // swallow idle bits
  89. while (state.bit_ofs > 0 && (state.byte & 1)) {
  90. state.bit_ofs--;
  91. state.byte >>= 1;
  92. }
  93. return true;
  94. }
  95. return false;
  96. reset:
  97. state.byte = 0;
  98. state.bit_ofs = 0;
  99. return false;
  100. }