Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

327 lines
9.8KB

  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. class TwoWire : public Stream
  34. {
  35. public:
  36. // Hardware description struct
  37. static const uint8_t cnt_sda_pins = 2;
  38. static const uint8_t cnt_scl_pins = 2;
  39. typedef struct {
  40. const uint8_t pin; // The pin number
  41. const uint32_t mux_val; // Value to set for mux;
  42. volatile uint32_t *select_input_register; // Which register controls the selection
  43. const uint32_t select_val; // Value for that selection
  44. } pin_info_t;
  45. typedef struct {
  46. volatile uint32_t &clock_gate_register;
  47. uint32_t clock_gate_mask;
  48. pin_info_t sda_pins[cnt_sda_pins];
  49. pin_info_t scl_pins[cnt_scl_pins];
  50. IRQ_NUMBER_t irq;
  51. } I2C_Hardware_t;
  52. static const I2C_Hardware_t i2c1_hardware;
  53. static const I2C_Hardware_t i2c2_hardware;
  54. static const I2C_Hardware_t i2c3_hardware;
  55. static const I2C_Hardware_t i2c4_hardware;
  56. public:
  57. constexpr TwoWire(IMXRT_LPI2C_t *myport, const I2C_Hardware_t &myhardware)
  58. : port(myport), hardware(myhardware) {
  59. }
  60. void begin();
  61. void begin(uint8_t address);
  62. void begin(int address) {
  63. begin((uint8_t)address);
  64. }
  65. void end();
  66. void setClock(uint32_t frequency);
  67. void setSDA(uint8_t pin);
  68. void setSCL(uint8_t pin);
  69. void beginTransmission(uint8_t address) {
  70. txBuffer[0] = (address << 1);
  71. transmitting = 1;
  72. txBufferLength = 1;
  73. }
  74. void beginTransmission(int address) {
  75. beginTransmission((uint8_t)address);
  76. }
  77. uint8_t endTransmission(uint8_t sendStop);
  78. uint8_t endTransmission(void) {
  79. return endTransmission(1);
  80. }
  81. uint8_t requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop);
  82. uint8_t requestFrom(uint8_t address, uint8_t quantity) {
  83. return requestFrom(address, quantity, (uint8_t)1);
  84. }
  85. uint8_t requestFrom(int address, int quantity, int sendStop) {
  86. return requestFrom((uint8_t)address, (uint8_t)quantity,
  87. (uint8_t)(sendStop ? 1 : 0));
  88. }
  89. uint8_t requestFrom(int address, int quantity) {
  90. return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)1);
  91. }
  92. uint8_t requestFrom(uint8_t addr, uint8_t qty, uint32_t iaddr, uint8_t n, uint8_t stop);
  93. virtual size_t write(uint8_t data);
  94. virtual size_t write(const uint8_t *data, size_t quantity);
  95. virtual int available(void) {
  96. return rxBufferLength - rxBufferIndex;
  97. }
  98. virtual int read(void) {
  99. if (rxBufferIndex >= rxBufferLength) return -1;
  100. return rxBuffer[rxBufferIndex++];
  101. }
  102. virtual int peek(void) {
  103. if (rxBufferIndex >= rxBufferLength) return -1;
  104. return rxBuffer[rxBufferIndex];
  105. }
  106. virtual void flush(void) {
  107. }
  108. void onReceive(void (*function)(int numBytes)) {
  109. user_onReceive = function;
  110. }
  111. void onRequest(void (*function)(void)) {
  112. user_onRequest = function;
  113. }
  114. // send() for compatibility with very old sketches and libraries
  115. void send(uint8_t b) {
  116. write(b);
  117. }
  118. void send(uint8_t *s, uint8_t n) {
  119. write(s, n);
  120. }
  121. void send(int n) {
  122. write((uint8_t)n);
  123. }
  124. void send(char *s) {
  125. write(s);
  126. }
  127. uint8_t receive(void) {
  128. int c = read();
  129. if (c < 0) return 0;
  130. return c;
  131. }
  132. size_t write(unsigned long n) {
  133. return write((uint8_t)n);
  134. }
  135. size_t write(long n) {
  136. return write((uint8_t)n);
  137. }
  138. size_t write(unsigned int n) {
  139. return write((uint8_t)n);
  140. }
  141. size_t write(int n) {
  142. return write((uint8_t)n);
  143. }
  144. using Print::write;
  145. private:
  146. //void isr(void);
  147. //bool wait_idle(void);
  148. bool wait_idle();
  149. bool force_clock();
  150. IMXRT_LPI2C_t * const port;
  151. const I2C_Hardware_t &hardware;
  152. uint8_t sda_pin_index_ = 0x0; // default is always first item
  153. uint8_t scl_pin_index_ = 0x0;
  154. uint8_t rxBuffer[BUFFER_LENGTH] = {};
  155. uint8_t rxBufferIndex = 0;
  156. uint8_t rxBufferLength = 0;
  157. uint8_t txAddress = 0;
  158. uint8_t txBuffer[BUFFER_LENGTH+1] = {};
  159. uint8_t txBufferIndex = 0;
  160. uint8_t txBufferLength = 0;
  161. uint8_t transmitting = 0;
  162. uint8_t slave_mode = 0;
  163. uint8_t irqcount = 0;
  164. uint8_t sda_pin_index = 0;
  165. uint8_t scl_pin_index = 0;
  166. void onRequestService(void);
  167. void onReceiveService(uint8_t*, int);
  168. void (*user_onRequest)(void) = nullptr;
  169. void (*user_onReceive)(int) = nullptr;
  170. void sda_rising_isr(void);
  171. friend void i2c0_isr(void);
  172. friend void i2c1_isr(void);
  173. friend void i2c2_isr(void);
  174. friend void i2c3_isr(void);
  175. friend void sda_rising_isr0(void);
  176. friend void sda_rising_isr1(void);
  177. };
  178. extern TwoWire Wire;
  179. extern TwoWire Wire1;
  180. extern TwoWire Wire2;
  181. extern TwoWire Wire3;
  182. class TWBRemulation
  183. {
  184. public:
  185. inline TWBRemulation & operator = (int val) __attribute__((always_inline)) {
  186. /*if (val == 12 || val == ((F_CPU / 400000) - 16) / 2) { // 22, 52, 112
  187. I2C0_C1 = 0;
  188. #if F_BUS == 128000000
  189. I2C0_F = I2C_F_DIV320; // 400 kHz
  190. #elif F_BUS == 120000000
  191. I2C0_F = I2C_F_DIV288; // 416 kHz
  192. #elif F_BUS == 108000000
  193. I2C0_F = I2C_F_DIV256; // 422 kHz
  194. #elif F_BUS == 96000000
  195. I2C0_F = I2C_F_DIV240; // 400 kHz
  196. #elif F_BUS == 90000000
  197. I2C0_F = I2C_F_DIV224; // 402 kHz
  198. #elif F_BUS == 80000000
  199. I2C0_F = I2C_F_DIV192; // 416 kHz
  200. #elif F_BUS == 72000000
  201. I2C0_F = I2C_F_DIV192; // 375 kHz
  202. #elif F_BUS == 64000000
  203. I2C0_F = I2C_F_DIV160; // 400 kHz
  204. #elif F_BUS == 60000000
  205. I2C0_F = I2C_F_DIV144; // 416 kHz
  206. #elif F_BUS == 56000000
  207. I2C0_F = I2C_F_DIV144; // 389 kHz
  208. #elif F_BUS == 54000000
  209. I2C0_F = I2C_F_DIV128; // 422 kHz
  210. #elif F_BUS == 48000000
  211. I2C0_F = I2C_F_DIV112; // 400 kHz
  212. #elif F_BUS == 40000000
  213. I2C0_F = I2C_F_DIV96; // 416 kHz
  214. #elif F_BUS == 36000000
  215. I2C0_F = I2C_F_DIV96; // 375 kHz
  216. #elif F_BUS == 24000000
  217. I2C0_F = I2C_F_DIV64; // 375 kHz
  218. #elif F_BUS == 16000000
  219. I2C0_F = I2C_F_DIV40; // 400 kHz
  220. #elif F_BUS == 8000000
  221. I2C0_F = I2C_F_DIV20; // 400 kHz
  222. #elif F_BUS == 4000000
  223. I2C0_F = I2C_F_DIV20; // 200 kHz
  224. #elif F_BUS == 2000000
  225. I2C0_F = I2C_F_DIV20; // 100 kHz
  226. #endif
  227. I2C0_C1 = I2C_C1_IICEN;
  228. } else if (val == 72 || val == ((F_CPU / 100000) - 16) / 2) { // 112, 232, 472
  229. I2C0_C1 = 0;
  230. #if F_BUS == 128000000
  231. I2C0_F = I2C_F_DIV1280; // 100 kHz
  232. #elif F_BUS == 120000000
  233. I2C0_F = I2C_F_DIV1152; // 104 kHz
  234. #elif F_BUS == 108000000
  235. I2C0_F = I2C_F_DIV1024; // 105 kHz
  236. #elif F_BUS == 96000000
  237. I2C0_F = I2C_F_DIV960; // 100 kHz
  238. #elif F_BUS == 90000000
  239. I2C0_F = I2C_F_DIV896; // 100 kHz
  240. #elif F_BUS == 80000000
  241. I2C0_F = I2C_F_DIV768; // 104 kHz
  242. #elif F_BUS == 72000000
  243. I2C0_F = I2C_F_DIV640; // 112 kHz
  244. #elif F_BUS == 64000000
  245. I2C0_F = I2C_F_DIV640; // 100 kHz
  246. #elif F_BUS == 60000000
  247. I2C0_F = I2C_F_DIV576; // 104 kHz
  248. #elif F_BUS == 56000000
  249. I2C0_F = I2C_F_DIV512; // 109 kHz
  250. #elif F_BUS == 54000000
  251. I2C0_F = I2C_F_DIV512; // 105 kHz
  252. #elif F_BUS == 48000000
  253. I2C0_F = I2C_F_DIV480; // 100 kHz
  254. #elif F_BUS == 40000000
  255. I2C0_F = I2C_F_DIV384; // 104 kHz
  256. #elif F_BUS == 36000000
  257. I2C0_F = I2C_F_DIV320; // 113 kHz
  258. #elif F_BUS == 24000000
  259. I2C0_F = I2C_F_DIV240; // 100 kHz
  260. #elif F_BUS == 16000000
  261. I2C0_F = I2C_F_DIV160; // 100 kHz
  262. #elif F_BUS == 8000000
  263. I2C0_F = I2C_F_DIV80; // 100 kHz
  264. #elif F_BUS == 4000000
  265. I2C0_F = I2C_F_DIV40; // 100 kHz
  266. #elif F_BUS == 2000000
  267. I2C0_F = I2C_F_DIV20; // 100 kHz
  268. #endif
  269. I2C0_C1 = I2C_C1_IICEN;
  270. } */
  271. return *this;
  272. }
  273. inline operator int () const __attribute__((always_inline)) {
  274. /* #if F_BUS == 128000000
  275. if (I2C0_F == I2C_F_DIV320) return 12;
  276. #elif F_BUS == 120000000
  277. if (I2C0_F == I2C_F_DIV288) return 12;
  278. #elif F_BUS == 108000000
  279. if (I2C0_F == I2C_F_DIV256) return 12;
  280. #elif F_BUS == 96000000
  281. if (I2C0_F == I2C_F_DIV240) return 12;
  282. #elif F_BUS == 90000000
  283. if (I2C0_F == I2C_F_DIV224) return 12;
  284. #elif F_BUS == 80000000
  285. if (I2C0_F == I2C_F_DIV192) return 12;
  286. #elif F_BUS == 72000000
  287. if (I2C0_F == I2C_F_DIV192) return 12;
  288. #elif F_BUS == 64000000
  289. if (I2C0_F == I2C_F_DIV160) return 12;
  290. #elif F_BUS == 60000000
  291. if (I2C0_F == I2C_F_DIV144) return 12;
  292. #elif F_BUS == 56000000
  293. if (I2C0_F == I2C_F_DIV144) return 12;
  294. #elif F_BUS == 54000000
  295. if (I2C0_F == I2C_F_DIV128) return 12;
  296. #elif F_BUS == 48000000
  297. if (I2C0_F == I2C_F_DIV112) return 12;
  298. #elif F_BUS == 40000000
  299. if (I2C0_F == I2C_F_DIV96) return 12;
  300. #elif F_BUS == 36000000
  301. if (I2C0_F == I2C_F_DIV96) return 12;
  302. #elif F_BUS == 24000000
  303. if (I2C0_F == I2C_F_DIV64) return 12;
  304. #elif F_BUS == 16000000
  305. if (I2C0_F == I2C_F_DIV40) return 12;
  306. #elif F_BUS == 8000000
  307. if (I2C0_F == I2C_F_DIV20) return 12;
  308. #elif F_BUS == 4000000
  309. if (I2C0_F == I2C_F_DIV20) return 12;
  310. #endif */
  311. return 72;
  312. }
  313. };
  314. extern TWBRemulation TWBR;
  315. #endif
  316. #endif