PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
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.

108 lines
3.3KB

  1. /*
  2. TwoWire.h - TWI/I2C library for Arduino & Wiring
  3. Copyright (c) 2006 Nicholas Zambetti. All right reserved.
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. Modified 2012 by Todd Krein (todd@krein.org) to implement repeated starts
  16. */
  17. #ifndef TwoWire_h
  18. #define TwoWire_h
  19. #if defined(__IMXRT1052__) || defined(__IMXRT1062__)
  20. #include "WireIMXRT.h"
  21. #elif defined(__arm__) && defined(TEENSYDUINO)
  22. #include "WireKinetis.h"
  23. #elif defined(__AVR__)
  24. #include <inttypes.h>
  25. #include <Arduino.h>
  26. #define BUFFER_LENGTH 32
  27. #define WIRE_HAS_END 1
  28. class TwoWire : public Stream
  29. {
  30. private:
  31. static uint8_t rxBuffer[];
  32. static uint8_t rxBufferIndex;
  33. static uint8_t rxBufferLength;
  34. static uint8_t txAddress;
  35. static uint8_t txBuffer[];
  36. static uint8_t txBufferIndex;
  37. static uint8_t txBufferLength;
  38. static uint8_t transmitting;
  39. static void onRequestService(void);
  40. static void onReceiveService(uint8_t*, int);
  41. static void (*user_onRequest)(void);
  42. static void (*user_onReceive)(int);
  43. static void sda_rising_isr(void);
  44. public:
  45. TwoWire();
  46. void begin();
  47. void begin(uint8_t);
  48. void begin(int);
  49. void end();
  50. void setClock(uint32_t);
  51. void setSDA(uint8_t);
  52. void setSCL(uint8_t);
  53. void beginTransmission(uint8_t);
  54. void beginTransmission(int);
  55. uint8_t endTransmission(void);
  56. uint8_t endTransmission(uint8_t);
  57. uint8_t requestFrom(uint8_t, uint8_t);
  58. uint8_t requestFrom(uint8_t, uint8_t, uint8_t);
  59. uint8_t requestFrom(int, int);
  60. uint8_t requestFrom(int, int, int);
  61. uint8_t requestFrom(uint8_t, uint8_t, uint32_t, uint8_t, uint8_t);
  62. virtual size_t write(uint8_t);
  63. virtual size_t write(const uint8_t *, size_t);
  64. virtual int available(void);
  65. virtual int read(void);
  66. virtual int peek(void);
  67. virtual void flush(void);
  68. void onReceive( void (*)(int) );
  69. void onRequest( void (*)(void) );
  70. #ifdef CORE_TEENSY
  71. // added by Teensyduino installer, for compatibility
  72. // with pre-1.0 sketches and libraries
  73. void send(uint8_t b) { write(b); }
  74. void send(uint8_t *s, uint8_t n) { write(s, n); }
  75. void send(int n) { write((uint8_t)n); }
  76. void send(char *s) { write(s); }
  77. uint8_t receive(void) {
  78. int c = read();
  79. if (c < 0) return 0;
  80. return c;
  81. }
  82. #endif
  83. inline size_t write(unsigned long n) { return write((uint8_t)n); }
  84. inline size_t write(long n) { return write((uint8_t)n); }
  85. inline size_t write(unsigned int n) { return write((uint8_t)n); }
  86. inline size_t write(int n) { return write((uint8_t)n); }
  87. using Print::write;
  88. };
  89. extern TwoWire Wire;
  90. #endif
  91. #endif