Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

366 lines
10KB

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