You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SoftwareSerial8e1.h 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //
  2. // Created by robin on 2/24/17.
  3. //
  4. #ifndef ARDUINOSMARTCARD_SOFTWARESERIAL8E1_H
  5. #define ARDUINOSMARTCARD_SOFTWARESERIAL8E1_H
  6. #include <inttypes.h>
  7. #include <Stream.h>
  8. /******************************************************************************
  9. * Definitions
  10. ******************************************************************************/
  11. #ifndef _SS_MAX_RX_BUFF
  12. #define _SS_MAX_RX_BUFF 64 // RX buffer size
  13. #endif
  14. #ifndef GCC_VERSION
  15. #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
  16. #endif
  17. class SoftwareSerial8e1 : public Stream
  18. {
  19. private:
  20. // per object data
  21. uint8_t _receivePin;
  22. uint8_t _receiveBitMask;
  23. volatile uint8_t *_receivePortRegister;
  24. uint8_t _transmitBitMask;
  25. volatile uint8_t *_transmitPortRegister;
  26. volatile uint8_t *_pcint_maskreg;
  27. uint8_t _pcint_maskvalue;
  28. // Expressed as 4-cycle delays (must never be 0!)
  29. uint16_t _rx_delay_centering;
  30. uint16_t _rx_delay_intrabit;
  31. uint16_t _rx_delay_stopbit;
  32. uint16_t _tx_delay;
  33. uint16_t _buffer_overflow:1;
  34. uint16_t _inverse_logic:1;
  35. // static data
  36. static uint8_t _receive_buffer[_SS_MAX_RX_BUFF];
  37. static volatile uint8_t _receive_buffer_tail;
  38. static volatile uint8_t _receive_buffer_head;
  39. static SoftwareSerial8e1 *active_object;
  40. // private methods
  41. inline void recv() __attribute__((__always_inline__));
  42. uint8_t rx_pin_read();
  43. void setTX(uint8_t transmitPin);
  44. void setRX(uint8_t receivePin);
  45. inline void setRxIntMsk(bool enable) __attribute__((__always_inline__));
  46. // Return num - sub, or 1 if the result would be < 1
  47. static uint16_t subtract_cap(uint16_t num, uint16_t sub);
  48. // private static method for timing
  49. static inline void tunedDelay(uint16_t delay);
  50. public:
  51. // public methods
  52. SoftwareSerial8e1(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic = false);
  53. ~SoftwareSerial8e1();
  54. void begin(long speed);
  55. bool listen();
  56. void end();
  57. bool isListening() { return this == active_object; }
  58. bool stopListening();
  59. bool overflow() { bool ret = _buffer_overflow; if (ret) _buffer_overflow = false; return ret; }
  60. int peek();
  61. virtual size_t write(uint8_t byte);
  62. virtual int read();
  63. virtual int available();
  64. virtual void flush();
  65. operator bool() { return true; }
  66. using Print::write;
  67. // public only for easy access by interrupt handlers
  68. static inline void handle_interrupt() __attribute__((__always_inline__));
  69. };
  70. // Arduino 0012 workaround
  71. #undef int
  72. #undef char
  73. #undef long
  74. #undef byte
  75. #undef float
  76. #undef abs
  77. #undef round
  78. #endif //ARDUINOSMARTCARD_SOFTWARESERIAL8E1_H