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.

330 lines
9.9KB

  1. /* Wire Library for Teensy LC & 3.X
  2. * Copyright (c) 2014-2017, Paul Stoffregen, paul@pjrc.com
  3. *
  4. * Development of this I2C library was funded by PJRC.COM, LLC by sales of
  5. * Teensy and related products. Please support PJRC's efforts to develop
  6. * open source software by purchasing Teensy or other PJRC products.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice, development funding notice, and this permission
  16. * notice shall be included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #ifndef TwoWireIMXRT_h
  27. #define TwoWireIMXRT_h
  28. #if defined(__IMXRT1052__) || defined(__IMXRT1062__)
  29. #include <Arduino.h>
  30. #include <stdint.h>
  31. #define BUFFER_LENGTH 32
  32. //#define WIRE_HAS_END 1
  33. #define WIRE_IMPLEMENT_WIRE
  34. #define WIRE_IMPLEMENT_WIRE1
  35. #define WIRE_IMPLEMENT_WIRE2
  36. class TwoWire : public Stream
  37. {
  38. public:
  39. // Hardware description struct
  40. static const uint8_t cnt_sda_pins = 2;
  41. static const uint8_t cnt_scl_pins = 2;
  42. typedef struct {
  43. const uint8_t pin; // The pin number
  44. const uint32_t mux_val; // Value to set for mux;
  45. volatile uint32_t *select_input_register; // Which register controls the selection
  46. const uint32_t select_val; // Value for that selection
  47. } pin_info_t;
  48. typedef struct {
  49. volatile uint32_t &clock_gate_register;
  50. uint32_t clock_gate_mask;
  51. pin_info_t sda_pins[cnt_sda_pins];
  52. pin_info_t scl_pins[cnt_scl_pins];
  53. IRQ_NUMBER_t irq;
  54. } I2C_Hardware_t;
  55. static const I2C_Hardware_t i2c1_hardware;
  56. static const I2C_Hardware_t i2c2_hardware;
  57. static const I2C_Hardware_t i2c3_hardware;
  58. static const I2C_Hardware_t i2c4_hardware;
  59. public:
  60. constexpr TwoWire(IMXRT_LPI2C_t *myport, const I2C_Hardware_t &myhardware)
  61. : port(myport), hardware(myhardware) {
  62. }
  63. void begin();
  64. void begin(uint8_t address);
  65. void begin(int address) {
  66. begin((uint8_t)address);
  67. }
  68. void end();
  69. void setClock(uint32_t frequency);
  70. void setSDA(uint8_t pin);
  71. void setSCL(uint8_t pin);
  72. void beginTransmission(uint8_t address) {
  73. txBuffer[0] = (address << 1);
  74. transmitting = 1;
  75. txBufferLength = 1;
  76. }
  77. void beginTransmission(int address) {
  78. beginTransmission((uint8_t)address);
  79. }
  80. uint8_t endTransmission(uint8_t sendStop);
  81. uint8_t endTransmission(void) {
  82. return endTransmission(1);
  83. }
  84. uint8_t requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop);
  85. uint8_t requestFrom(uint8_t address, uint8_t quantity) {
  86. return requestFrom(address, quantity, (uint8_t)1);
  87. }
  88. uint8_t requestFrom(int address, int quantity, int sendStop) {
  89. return requestFrom((uint8_t)address, (uint8_t)quantity,
  90. (uint8_t)(sendStop ? 1 : 0));
  91. }
  92. uint8_t requestFrom(int address, int quantity) {
  93. return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)1);
  94. }
  95. uint8_t requestFrom(uint8_t addr, uint8_t qty, uint32_t iaddr, uint8_t n, uint8_t stop);
  96. virtual size_t write(uint8_t data);
  97. virtual size_t write(const uint8_t *data, size_t quantity);
  98. virtual int available(void) {
  99. return rxBufferLength - rxBufferIndex;
  100. }
  101. virtual int read(void) {
  102. if (rxBufferIndex >= rxBufferLength) return -1;
  103. return rxBuffer[rxBufferIndex++];
  104. }
  105. virtual int peek(void) {
  106. if (rxBufferIndex >= rxBufferLength) return -1;
  107. return rxBuffer[rxBufferIndex];
  108. }
  109. virtual void flush(void) {
  110. }
  111. void onReceive(void (*function)(int numBytes)) {
  112. user_onReceive = function;
  113. }
  114. void onRequest(void (*function)(void)) {
  115. user_onRequest = function;
  116. }
  117. // send() for compatibility with very old sketches and libraries
  118. void send(uint8_t b) {
  119. write(b);
  120. }
  121. void send(uint8_t *s, uint8_t n) {
  122. write(s, n);
  123. }
  124. void send(int n) {
  125. write((uint8_t)n);
  126. }
  127. void send(char *s) {
  128. write(s);
  129. }
  130. uint8_t receive(void) {
  131. int c = read();
  132. if (c < 0) return 0;
  133. return c;
  134. }
  135. size_t write(unsigned long n) {
  136. return write((uint8_t)n);
  137. }
  138. size_t write(long n) {
  139. return write((uint8_t)n);
  140. }
  141. size_t write(unsigned int n) {
  142. return write((uint8_t)n);
  143. }
  144. size_t write(int n) {
  145. return write((uint8_t)n);
  146. }
  147. using Print::write;
  148. private:
  149. //void isr(void);
  150. //bool wait_idle(void);
  151. bool wait_idle();
  152. bool force_clock();
  153. IMXRT_LPI2C_t * const port;
  154. const I2C_Hardware_t &hardware;
  155. uint8_t sda_pin_index_ = 0x0; // default is always first item
  156. uint8_t scl_pin_index_ = 0x0;
  157. uint8_t rxBuffer[BUFFER_LENGTH] = {};
  158. uint8_t rxBufferIndex = 0;
  159. uint8_t rxBufferLength = 0;
  160. uint8_t txAddress = 0;
  161. uint8_t txBuffer[BUFFER_LENGTH+1] = {};
  162. uint8_t txBufferIndex = 0;
  163. uint8_t txBufferLength = 0;
  164. uint8_t transmitting = 0;
  165. uint8_t slave_mode = 0;
  166. uint8_t irqcount = 0;
  167. uint8_t sda_pin_index = 0;
  168. uint8_t scl_pin_index = 0;
  169. void onRequestService(void);
  170. void onReceiveService(uint8_t*, int);
  171. void (*user_onRequest)(void) = nullptr;
  172. void (*user_onReceive)(int) = nullptr;
  173. void sda_rising_isr(void);
  174. friend void i2c0_isr(void);
  175. friend void i2c1_isr(void);
  176. friend void i2c2_isr(void);
  177. friend void i2c3_isr(void);
  178. friend void sda_rising_isr0(void);
  179. friend void sda_rising_isr1(void);
  180. };
  181. extern TwoWire Wire;
  182. extern TwoWire Wire1;
  183. extern TwoWire Wire2;
  184. extern TwoWire Wire3;
  185. class TWBRemulation
  186. {
  187. public:
  188. inline TWBRemulation & operator = (int val) __attribute__((always_inline)) {
  189. /*if (val == 12 || val == ((F_CPU / 400000) - 16) / 2) { // 22, 52, 112
  190. I2C0_C1 = 0;
  191. #if F_BUS == 128000000
  192. I2C0_F = I2C_F_DIV320; // 400 kHz
  193. #elif F_BUS == 120000000
  194. I2C0_F = I2C_F_DIV288; // 416 kHz
  195. #elif F_BUS == 108000000
  196. I2C0_F = I2C_F_DIV256; // 422 kHz
  197. #elif F_BUS == 96000000
  198. I2C0_F = I2C_F_DIV240; // 400 kHz
  199. #elif F_BUS == 90000000
  200. I2C0_F = I2C_F_DIV224; // 402 kHz
  201. #elif F_BUS == 80000000
  202. I2C0_F = I2C_F_DIV192; // 416 kHz
  203. #elif F_BUS == 72000000
  204. I2C0_F = I2C_F_DIV192; // 375 kHz
  205. #elif F_BUS == 64000000
  206. I2C0_F = I2C_F_DIV160; // 400 kHz
  207. #elif F_BUS == 60000000
  208. I2C0_F = I2C_F_DIV144; // 416 kHz
  209. #elif F_BUS == 56000000
  210. I2C0_F = I2C_F_DIV144; // 389 kHz
  211. #elif F_BUS == 54000000
  212. I2C0_F = I2C_F_DIV128; // 422 kHz
  213. #elif F_BUS == 48000000
  214. I2C0_F = I2C_F_DIV112; // 400 kHz
  215. #elif F_BUS == 40000000
  216. I2C0_F = I2C_F_DIV96; // 416 kHz
  217. #elif F_BUS == 36000000
  218. I2C0_F = I2C_F_DIV96; // 375 kHz
  219. #elif F_BUS == 24000000
  220. I2C0_F = I2C_F_DIV64; // 375 kHz
  221. #elif F_BUS == 16000000
  222. I2C0_F = I2C_F_DIV40; // 400 kHz
  223. #elif F_BUS == 8000000
  224. I2C0_F = I2C_F_DIV20; // 400 kHz
  225. #elif F_BUS == 4000000
  226. I2C0_F = I2C_F_DIV20; // 200 kHz
  227. #elif F_BUS == 2000000
  228. I2C0_F = I2C_F_DIV20; // 100 kHz
  229. #endif
  230. I2C0_C1 = I2C_C1_IICEN;
  231. } else if (val == 72 || val == ((F_CPU / 100000) - 16) / 2) { // 112, 232, 472
  232. I2C0_C1 = 0;
  233. #if F_BUS == 128000000
  234. I2C0_F = I2C_F_DIV1280; // 100 kHz
  235. #elif F_BUS == 120000000
  236. I2C0_F = I2C_F_DIV1152; // 104 kHz
  237. #elif F_BUS == 108000000
  238. I2C0_F = I2C_F_DIV1024; // 105 kHz
  239. #elif F_BUS == 96000000
  240. I2C0_F = I2C_F_DIV960; // 100 kHz
  241. #elif F_BUS == 90000000
  242. I2C0_F = I2C_F_DIV896; // 100 kHz
  243. #elif F_BUS == 80000000
  244. I2C0_F = I2C_F_DIV768; // 104 kHz
  245. #elif F_BUS == 72000000
  246. I2C0_F = I2C_F_DIV640; // 112 kHz
  247. #elif F_BUS == 64000000
  248. I2C0_F = I2C_F_DIV640; // 100 kHz
  249. #elif F_BUS == 60000000
  250. I2C0_F = I2C_F_DIV576; // 104 kHz
  251. #elif F_BUS == 56000000
  252. I2C0_F = I2C_F_DIV512; // 109 kHz
  253. #elif F_BUS == 54000000
  254. I2C0_F = I2C_F_DIV512; // 105 kHz
  255. #elif F_BUS == 48000000
  256. I2C0_F = I2C_F_DIV480; // 100 kHz
  257. #elif F_BUS == 40000000
  258. I2C0_F = I2C_F_DIV384; // 104 kHz
  259. #elif F_BUS == 36000000
  260. I2C0_F = I2C_F_DIV320; // 113 kHz
  261. #elif F_BUS == 24000000
  262. I2C0_F = I2C_F_DIV240; // 100 kHz
  263. #elif F_BUS == 16000000
  264. I2C0_F = I2C_F_DIV160; // 100 kHz
  265. #elif F_BUS == 8000000
  266. I2C0_F = I2C_F_DIV80; // 100 kHz
  267. #elif F_BUS == 4000000
  268. I2C0_F = I2C_F_DIV40; // 100 kHz
  269. #elif F_BUS == 2000000
  270. I2C0_F = I2C_F_DIV20; // 100 kHz
  271. #endif
  272. I2C0_C1 = I2C_C1_IICEN;
  273. } */
  274. return *this;
  275. }
  276. inline operator int () const __attribute__((always_inline)) {
  277. /* #if F_BUS == 128000000
  278. if (I2C0_F == I2C_F_DIV320) return 12;
  279. #elif F_BUS == 120000000
  280. if (I2C0_F == I2C_F_DIV288) return 12;
  281. #elif F_BUS == 108000000
  282. if (I2C0_F == I2C_F_DIV256) return 12;
  283. #elif F_BUS == 96000000
  284. if (I2C0_F == I2C_F_DIV240) return 12;
  285. #elif F_BUS == 90000000
  286. if (I2C0_F == I2C_F_DIV224) return 12;
  287. #elif F_BUS == 80000000
  288. if (I2C0_F == I2C_F_DIV192) return 12;
  289. #elif F_BUS == 72000000
  290. if (I2C0_F == I2C_F_DIV192) return 12;
  291. #elif F_BUS == 64000000
  292. if (I2C0_F == I2C_F_DIV160) return 12;
  293. #elif F_BUS == 60000000
  294. if (I2C0_F == I2C_F_DIV144) return 12;
  295. #elif F_BUS == 56000000
  296. if (I2C0_F == I2C_F_DIV144) return 12;
  297. #elif F_BUS == 54000000
  298. if (I2C0_F == I2C_F_DIV128) return 12;
  299. #elif F_BUS == 48000000
  300. if (I2C0_F == I2C_F_DIV112) return 12;
  301. #elif F_BUS == 40000000
  302. if (I2C0_F == I2C_F_DIV96) return 12;
  303. #elif F_BUS == 36000000
  304. if (I2C0_F == I2C_F_DIV96) return 12;
  305. #elif F_BUS == 24000000
  306. if (I2C0_F == I2C_F_DIV64) return 12;
  307. #elif F_BUS == 16000000
  308. if (I2C0_F == I2C_F_DIV40) return 12;
  309. #elif F_BUS == 8000000
  310. if (I2C0_F == I2C_F_DIV20) return 12;
  311. #elif F_BUS == 4000000
  312. if (I2C0_F == I2C_F_DIV20) return 12;
  313. #endif */
  314. return 72;
  315. }
  316. };
  317. extern TWBRemulation TWBR;
  318. #endif
  319. #endif