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.

1085 lines
26KB

  1. /*
  2. TwoWire.cpp - TWI/I2C library for Wiring & Arduino
  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. #include "Wire.h"
  18. #if defined(__arm__) && defined(CORE_TEENSY)
  19. #include "kinetis.h"
  20. #include <string.h> // for memcpy
  21. #include "core_pins.h"
  22. //#include "HardwareSerial.h"
  23. #include "Wire.h"
  24. uint8_t TwoWire::rxBuffer[BUFFER_LENGTH];
  25. uint8_t TwoWire::rxBufferIndex = 0;
  26. uint8_t TwoWire::rxBufferLength = 0;
  27. uint8_t TwoWire::txBuffer[BUFFER_LENGTH+1];
  28. uint8_t TwoWire::txBufferIndex = 0;
  29. uint8_t TwoWire::txBufferLength = 0;
  30. uint8_t TwoWire::transmitting = 0;
  31. uint8_t TwoWire::sda_pin_num = 18;
  32. uint8_t TwoWire::scl_pin_num = 19;
  33. void (*TwoWire::user_onRequest)(void) = NULL;
  34. void (*TwoWire::user_onReceive)(int) = NULL;
  35. TwoWire::TwoWire()
  36. {
  37. }
  38. static uint8_t slave_mode = 0;
  39. static uint8_t irqcount=0;
  40. void TwoWire::begin(void)
  41. {
  42. //serial_begin(BAUD2DIV(115200));
  43. //serial_print("\nWire Begin\n");
  44. slave_mode = 0;
  45. SIM_SCGC4 |= SIM_SCGC4_I2C0; // TODO: use bitband
  46. I2C0_C1 = 0;
  47. // On Teensy 3.0 external pullup resistors *MUST* be used
  48. // the PORT_PCR_PE bit is ignored when in I2C mode
  49. // I2C will not work at all without pullup resistors
  50. // It might seem like setting PORT_PCR_PE & PORT_PCR_PS
  51. // would enable pullup resistors. However, there seems
  52. // to be a bug in chip while I2C is enabled, where setting
  53. // those causes the port to be driven strongly high.
  54. if (sda_pin_num == 18) {
  55. CORE_PIN18_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  56. } else if (sda_pin_num == 17) {
  57. CORE_PIN17_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  58. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  59. } else if (sda_pin_num == 34) {
  60. CORE_PIN34_CONFIG = PORT_PCR_MUX(5)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  61. } else if (sda_pin_num == 8) {
  62. CORE_PIN8_CONFIG = PORT_PCR_MUX(7)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  63. } else if (sda_pin_num == 48) {
  64. CORE_PIN48_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  65. #endif
  66. }
  67. if (scl_pin_num == 19) {
  68. CORE_PIN19_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  69. } else if (scl_pin_num == 16) {
  70. CORE_PIN16_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  71. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  72. } else if (scl_pin_num == 33) {
  73. CORE_PIN33_CONFIG = PORT_PCR_MUX(5)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  74. } else if (scl_pin_num == 7) {
  75. CORE_PIN7_CONFIG = PORT_PCR_MUX(7)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  76. } else if (scl_pin_num == 47) {
  77. CORE_PIN47_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  78. #endif
  79. }
  80. setClock(100000);
  81. I2C0_C2 = I2C_C2_HDRS;
  82. I2C0_C1 = I2C_C1_IICEN;
  83. //pinMode(3, OUTPUT);
  84. //pinMode(4, OUTPUT);
  85. }
  86. void TwoWire::setClock(uint32_t frequency)
  87. {
  88. if (!(SIM_SCGC4 & SIM_SCGC4_I2C0)) return;
  89. #if F_BUS == 120000000
  90. if (frequency < 400000) {
  91. I2C0_F = I2C_F_DIV1152; // 104 kHz
  92. } else if (frequency < 1000000) {
  93. I2C0_F = I2C_F_DIV288; // 416 kHz
  94. } else {
  95. I2C0_F = I2C_F_DIV128; // 0.94 MHz
  96. }
  97. I2C0_FLT = 4;
  98. #elif F_BUS == 108000000
  99. if (frequency < 400000) {
  100. I2C0_F = I2C_F_DIV1024; // 105 kHz
  101. } else if (frequency < 1000000) {
  102. I2C0_F = I2C_F_DIV256; // 422 kHz
  103. } else {
  104. I2C0_F = I2C_F_DIV112; // 0.96 MHz
  105. }
  106. I2C0_FLT = 4;
  107. #elif F_BUS == 96000000
  108. if (frequency < 400000) {
  109. I2C0_F = I2C_F_DIV960; // 100 kHz
  110. } else if (frequency < 1000000) {
  111. I2C0_F = I2C_F_DIV240; // 400 kHz
  112. } else {
  113. I2C0_F = I2C_F_DIV96; // 1.0 MHz
  114. }
  115. I2C0_FLT = 4;
  116. #elif F_BUS == 90000000
  117. if (frequency < 400000) {
  118. I2C0_F = I2C_F_DIV896; // 100 kHz
  119. } else if (frequency < 1000000) {
  120. I2C0_F = I2C_F_DIV224; // 402 kHz
  121. } else {
  122. I2C0_F = I2C_F_DIV88; // 1.02 MHz
  123. }
  124. I2C0_FLT = 4;
  125. #elif F_BUS == 80000000
  126. if (frequency < 400000) {
  127. I2C0_F = I2C_F_DIV768; // 104 kHz
  128. } else if (frequency < 1000000) {
  129. I2C0_F = I2C_F_DIV192; // 416 kHz
  130. } else {
  131. I2C0_F = I2C_F_DIV80; // 1.0 MHz
  132. }
  133. I2C0_FLT = 4;
  134. #elif F_BUS == 72000000
  135. if (frequency < 400000) {
  136. I2C0_F = I2C_F_DIV640; // 112 kHz
  137. } else if (frequency < 1000000) {
  138. I2C0_F = I2C_F_DIV192; // 375 kHz
  139. } else {
  140. I2C0_F = I2C_F_DIV72; // 1.0 MHz
  141. }
  142. I2C0_FLT = 4;
  143. #elif F_BUS == 64000000
  144. if (frequency < 400000) {
  145. I2C0_F = I2C_F_DIV640; // 100 kHz
  146. } else if (frequency < 1000000) {
  147. I2C0_F = I2C_F_DIV160; // 400 kHz
  148. } else {
  149. I2C0_F = I2C_F_DIV64; // 1.0 MHz
  150. }
  151. I2C0_FLT = 4;
  152. #elif F_BUS == 60000000
  153. if (frequency < 400000) {
  154. I2C0_F = 0x2C; // 104 kHz
  155. } else if (frequency < 1000000) {
  156. I2C0_F = 0x1C; // 416 kHz
  157. } else {
  158. I2C0_F = 0x12; // 938 kHz
  159. }
  160. I2C0_FLT = 4;
  161. #elif F_BUS == 56000000
  162. if (frequency < 400000) {
  163. I2C0_F = 0x2B; // 109 kHz
  164. } else if (frequency < 1000000) {
  165. I2C0_F = 0x1C; // 389 kHz
  166. } else {
  167. I2C0_F = 0x0E; // 1 MHz
  168. }
  169. I2C0_FLT = 4;
  170. #elif F_BUS == 54000000
  171. if (frequency < 400000) {
  172. I2C0_F = I2C_F_DIV512; // 105 kHz
  173. } else if (frequency < 1000000) {
  174. I2C0_F = I2C_F_DIV128; // 422 kHz
  175. } else {
  176. I2C0_F = I2C_F_DIV56; // 0.96 MHz
  177. }
  178. I2C0_FLT = 4;
  179. #elif F_BUS == 48000000
  180. if (frequency < 400000) {
  181. I2C0_F = 0x27; // 100 kHz
  182. } else if (frequency < 1000000) {
  183. I2C0_F = 0x1A; // 400 kHz
  184. } else {
  185. I2C0_F = 0x0D; // 1 MHz
  186. }
  187. I2C0_FLT = 4;
  188. #elif F_BUS == 40000000
  189. if (frequency < 400000) {
  190. I2C0_F = 0x29; // 104 kHz
  191. } else if (frequency < 1000000) {
  192. I2C0_F = 0x19; // 416 kHz
  193. } else {
  194. I2C0_F = 0x0B; // 1 MHz
  195. }
  196. I2C0_FLT = 3;
  197. #elif F_BUS == 36000000
  198. if (frequency < 400000) {
  199. I2C0_F = 0x28; // 113 kHz
  200. } else if (frequency < 1000000) {
  201. I2C0_F = 0x19; // 375 kHz
  202. } else {
  203. I2C0_F = 0x0A; // 1 MHz
  204. }
  205. I2C0_FLT = 3;
  206. #elif F_BUS == 24000000
  207. if (frequency < 400000) {
  208. I2C0_F = 0x1F; // 100 kHz
  209. } else if (frequency < 1000000) {
  210. I2C0_F = 0x12; // 375 kHz
  211. } else {
  212. I2C0_F = 0x02; // 1 MHz
  213. }
  214. I2C0_FLT = 2;
  215. #elif F_BUS == 16000000
  216. if (frequency < 400000) {
  217. I2C0_F = 0x20; // 100 kHz
  218. } else if (frequency < 1000000) {
  219. I2C0_F = 0x07; // 400 kHz
  220. } else {
  221. I2C0_F = 0x00; // 800 MHz
  222. }
  223. I2C0_FLT = 1;
  224. #elif F_BUS == 8000000
  225. if (frequency < 400000) {
  226. I2C0_F = 0x14; // 100 kHz
  227. } else {
  228. I2C0_F = 0x00; // 400 kHz
  229. }
  230. I2C0_FLT = 1;
  231. #elif F_BUS == 4000000
  232. if (frequency < 400000) {
  233. I2C0_F = 0x07; // 100 kHz
  234. } else {
  235. I2C0_F = 0x00; // 200 kHz
  236. }
  237. I2C0_FLT = 1;
  238. #elif F_BUS == 2000000
  239. I2C0_F = 0x00; // 100 kHz
  240. I2C0_FLT = 1;
  241. #else
  242. #error "F_BUS must be 120, 108, 96, 9, 80, 72, 64, 60, 56, 54, 48, 40, 36, 24, 16, 8, 4 or 2 MHz"
  243. #endif
  244. }
  245. void TwoWire::setSDA(uint8_t pin)
  246. {
  247. if (pin == sda_pin_num) return;
  248. if ((SIM_SCGC4 & SIM_SCGC4_I2C0)) {
  249. if (sda_pin_num == 18) {
  250. CORE_PIN18_CONFIG = 0;
  251. } else if (sda_pin_num == 17) {
  252. CORE_PIN17_CONFIG = 0;
  253. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  254. } else if (sda_pin_num == 34) {
  255. CORE_PIN34_CONFIG = 0;
  256. } else if (sda_pin_num == 8) {
  257. CORE_PIN8_CONFIG = 0;
  258. } else if (sda_pin_num == 48) {
  259. CORE_PIN48_CONFIG = 0;
  260. #endif
  261. }
  262. if (pin == 18) {
  263. CORE_PIN18_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  264. } else if (pin == 17) {
  265. CORE_PIN17_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  266. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  267. } else if (pin == 34) {
  268. CORE_PIN34_CONFIG = PORT_PCR_MUX(5)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  269. } else if (pin == 8) {
  270. CORE_PIN8_CONFIG = PORT_PCR_MUX(7)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  271. } else if (pin == 48) {
  272. CORE_PIN48_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  273. #endif
  274. }
  275. }
  276. sda_pin_num = pin;
  277. }
  278. void TwoWire::setSCL(uint8_t pin)
  279. {
  280. if (pin == scl_pin_num) return;
  281. if ((SIM_SCGC4 & SIM_SCGC4_I2C0)) {
  282. if (scl_pin_num == 19) {
  283. CORE_PIN19_CONFIG = 0;
  284. } else if (scl_pin_num == 16) {
  285. CORE_PIN16_CONFIG = 0;
  286. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  287. } else if (scl_pin_num == 33) {
  288. CORE_PIN33_CONFIG = 0;
  289. } else if (scl_pin_num == 7) {
  290. CORE_PIN7_CONFIG = 0;
  291. } else if (scl_pin_num == 47) {
  292. CORE_PIN47_CONFIG = 0;
  293. #endif
  294. }
  295. if (pin == 19) {
  296. CORE_PIN19_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  297. } else if (pin == 16) {
  298. CORE_PIN16_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  299. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  300. } else if (pin == 33) {
  301. CORE_PIN33_CONFIG = PORT_PCR_MUX(5)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  302. } else if (pin == 7) {
  303. CORE_PIN7_CONFIG = PORT_PCR_MUX(7)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  304. } else if (pin == 47) {
  305. CORE_PIN47_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  306. #endif
  307. }
  308. }
  309. scl_pin_num = pin;
  310. }
  311. void TwoWire::begin(uint8_t address)
  312. {
  313. begin();
  314. I2C0_A1 = address << 1;
  315. slave_mode = 1;
  316. I2C0_C1 = I2C_C1_IICEN | I2C_C1_IICIE;
  317. NVIC_ENABLE_IRQ(IRQ_I2C0);
  318. }
  319. void TwoWire::end()
  320. {
  321. if (!(SIM_SCGC4 & SIM_SCGC4_I2C0)) return;
  322. NVIC_DISABLE_IRQ(IRQ_I2C0);
  323. I2C0_C1 = 0;
  324. if (sda_pin_num == 18) {
  325. CORE_PIN18_CONFIG = 0;
  326. } else if (sda_pin_num == 17) {
  327. CORE_PIN17_CONFIG = 0;
  328. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  329. } else if (sda_pin_num == 34) {
  330. CORE_PIN34_CONFIG = 0;
  331. } else if (sda_pin_num == 8) {
  332. CORE_PIN8_CONFIG = 0;
  333. } else if (sda_pin_num == 48) {
  334. CORE_PIN48_CONFIG = 0;
  335. #endif
  336. }
  337. if (scl_pin_num == 19) {
  338. CORE_PIN19_CONFIG = 0;
  339. } else if (scl_pin_num == 16) {
  340. CORE_PIN16_CONFIG = 0;
  341. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  342. } else if (scl_pin_num == 33) {
  343. CORE_PIN33_CONFIG = 0;
  344. } else if (scl_pin_num == 7) {
  345. CORE_PIN7_CONFIG = 0;
  346. } else if (scl_pin_num == 47) {
  347. CORE_PIN47_CONFIG = 0;
  348. #endif
  349. }
  350. SIM_SCGC4 &= ~SIM_SCGC4_I2C0; // TODO: use bitband
  351. }
  352. void i2c0_isr(void)
  353. {
  354. uint8_t status, c1, data;
  355. static uint8_t receiving=0;
  356. status = I2C0_S;
  357. //serial_print(".");
  358. if (status & I2C_S_ARBL) {
  359. // Arbitration Lost
  360. I2C0_S = I2C_S_ARBL;
  361. //serial_print("a");
  362. if (receiving && TwoWire::rxBufferLength > 0) {
  363. // TODO: does this detect the STOP condition in slave receive mode?
  364. }
  365. if (!(status & I2C_S_IAAS)) return;
  366. }
  367. if (status & I2C_S_IAAS) {
  368. //serial_print("\n");
  369. // Addressed As A Slave
  370. if (status & I2C_S_SRW) {
  371. //serial_print("T");
  372. // Begin Slave Transmit
  373. receiving = 0;
  374. TwoWire::txBufferLength = 0;
  375. if (TwoWire::user_onRequest != NULL) {
  376. TwoWire::user_onRequest();
  377. }
  378. if (TwoWire::txBufferLength == 0) {
  379. // is this correct, transmitting a single zero
  380. // when we should send nothing? Arduino's AVR
  381. // implementation does this, but is it ok?
  382. TwoWire::txBufferLength = 1;
  383. TwoWire::txBuffer[0] = 0;
  384. }
  385. I2C0_C1 = I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_TX;
  386. I2C0_D = TwoWire::txBuffer[0];
  387. TwoWire::txBufferIndex = 1;
  388. } else {
  389. // Begin Slave Receive
  390. //serial_print("R");
  391. receiving = 1;
  392. TwoWire::rxBufferLength = 0;
  393. I2C0_C1 = I2C_C1_IICEN | I2C_C1_IICIE;
  394. data = I2C0_D;
  395. }
  396. I2C0_S = I2C_S_IICIF;
  397. return;
  398. }
  399. #if defined(KINETISL)
  400. c1 = I2C0_FLT;
  401. if ((c1 & I2C_FLT_STOPF) && (c1 & I2C_FLT_STOPIE)) {
  402. I2C0_FLT = c1 & ~I2C_FLT_STOPIE;
  403. if (TwoWire::user_onReceive != NULL) {
  404. TwoWire::rxBufferIndex = 0;
  405. TwoWire::user_onReceive(TwoWire::rxBufferLength);
  406. }
  407. }
  408. #endif
  409. c1 = I2C0_C1;
  410. if (c1 & I2C_C1_TX) {
  411. // Continue Slave Transmit
  412. //serial_print("t");
  413. if ((status & I2C_S_RXAK) == 0) {
  414. //serial_print(".");
  415. // Master ACK'd previous byte
  416. if (TwoWire::txBufferIndex < TwoWire::txBufferLength) {
  417. I2C0_D = TwoWire::txBuffer[TwoWire::txBufferIndex++];
  418. } else {
  419. I2C0_D = 0;
  420. }
  421. I2C0_C1 = I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_TX;
  422. } else {
  423. //serial_print("*");
  424. // Master did not ACK previous byte
  425. I2C0_C1 = I2C_C1_IICEN | I2C_C1_IICIE;
  426. data = I2C0_D;
  427. }
  428. } else {
  429. // Continue Slave Receive
  430. irqcount = 0;
  431. #if defined(KINETISK)
  432. attachInterrupt(18, TwoWire::sda_rising_isr, RISING);
  433. #elif defined(KINETISL)
  434. I2C0_FLT |= I2C_FLT_STOPIE;
  435. #endif
  436. //digitalWriteFast(4, HIGH);
  437. data = I2C0_D;
  438. //serial_phex(data);
  439. if (TwoWire::rxBufferLength < BUFFER_LENGTH && receiving) {
  440. TwoWire::rxBuffer[TwoWire::rxBufferLength++] = data;
  441. }
  442. //digitalWriteFast(4, LOW);
  443. }
  444. I2C0_S = I2C_S_IICIF;
  445. }
  446. // Detects the stop condition that terminates a slave receive transfer.
  447. // Sadly, the I2C in Kinetis K series lacks the stop detect interrupt
  448. // This pin change interrupt hack is needed to detect the stop condition
  449. void TwoWire::sda_rising_isr(void)
  450. {
  451. //digitalWrite(3, HIGH);
  452. if (!(I2C0_S & I2C_S_BUSY)) {
  453. detachInterrupt(18);
  454. if (user_onReceive != NULL) {
  455. rxBufferIndex = 0;
  456. user_onReceive(rxBufferLength);
  457. }
  458. //delayMicroseconds(100);
  459. } else {
  460. if (++irqcount >= 2 || !slave_mode) {
  461. detachInterrupt(18);
  462. }
  463. }
  464. //digitalWrite(3, LOW);
  465. }
  466. // Chapter 44: Inter-Integrated Circuit (I2C) - Page 1012
  467. // I2C0_A1 // I2C Address Register 1
  468. // I2C0_F // I2C Frequency Divider register
  469. // I2C0_C1 // I2C Control Register 1
  470. // I2C0_S // I2C Status register
  471. // I2C0_D // I2C Data I/O register
  472. // I2C0_C2 // I2C Control Register 2
  473. // I2C0_FLT // I2C Programmable Input Glitch Filter register
  474. static uint8_t i2c_status(void)
  475. {
  476. static uint32_t p=0xFFFF;
  477. uint32_t s = I2C0_S;
  478. if (s != p) {
  479. //Serial.printf("(%02X)", s);
  480. p = s;
  481. }
  482. return s;
  483. }
  484. static void i2c_wait(void)
  485. {
  486. #if 0
  487. while (!(I2C0_S & I2C_S_IICIF)) ; // wait
  488. I2C0_S = I2C_S_IICIF;
  489. #endif
  490. //Serial.write('^');
  491. while (1) {
  492. if ((i2c_status() & I2C_S_IICIF)) break;
  493. }
  494. I2C0_S = I2C_S_IICIF;
  495. }
  496. void TwoWire::beginTransmission(uint8_t address)
  497. {
  498. txBuffer[0] = (address << 1);
  499. transmitting = 1;
  500. txBufferLength = 1;
  501. }
  502. size_t TwoWire::write(uint8_t data)
  503. {
  504. if (transmitting || slave_mode) {
  505. if (txBufferLength >= BUFFER_LENGTH+1) {
  506. setWriteError();
  507. return 0;
  508. }
  509. txBuffer[txBufferLength++] = data;
  510. return 1;
  511. }
  512. return 0;
  513. }
  514. size_t TwoWire::write(const uint8_t *data, size_t quantity)
  515. {
  516. if (transmitting || slave_mode) {
  517. size_t avail = BUFFER_LENGTH+1 - txBufferLength;
  518. if (quantity > avail) {
  519. quantity = avail;
  520. setWriteError();
  521. }
  522. memcpy(txBuffer + txBufferLength, data, quantity);
  523. txBufferLength += quantity;
  524. return quantity;
  525. }
  526. return 0;
  527. }
  528. void TwoWire::flush(void)
  529. {
  530. }
  531. uint8_t TwoWire::endTransmission(uint8_t sendStop)
  532. {
  533. uint8_t i, status, ret=0;
  534. // clear the status flags
  535. I2C0_S = I2C_S_IICIF | I2C_S_ARBL;
  536. // now take control of the bus...
  537. if (I2C0_C1 & I2C_C1_MST) {
  538. // we are already the bus master, so send a repeated start
  539. //Serial.print("rstart:");
  540. I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_RSTA | I2C_C1_TX;
  541. } else {
  542. // we are not currently the bus master, so wait for bus ready
  543. //Serial.print("busy:");
  544. uint32_t wait_begin = millis();
  545. while (i2c_status() & I2C_S_BUSY) {
  546. //Serial.write('.') ;
  547. if (millis() - wait_begin > 15) {
  548. // bus stuck busy too long
  549. I2C0_C1 = 0;
  550. I2C0_C1 = I2C_C1_IICEN;
  551. //Serial.println("abort");
  552. return 4;
  553. }
  554. }
  555. // become the bus master in transmit mode (send start)
  556. slave_mode = 0;
  557. I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TX;
  558. }
  559. // wait until start condition establishes control of the bus
  560. while (1) {
  561. status = i2c_status();
  562. if ((status & I2C_S_BUSY)) break;
  563. }
  564. // transmit the address and data
  565. for (i=0; i < txBufferLength; i++) {
  566. I2C0_D = txBuffer[i];
  567. //Serial.write('^');
  568. while (1) {
  569. status = i2c_status();
  570. if ((status & I2C_S_IICIF)) break;
  571. if (!(status & I2C_S_BUSY)) break;
  572. }
  573. I2C0_S = I2C_S_IICIF;
  574. //Serial.write('$');
  575. status = i2c_status();
  576. if ((status & I2C_S_ARBL)) {
  577. // we lost bus arbitration to another master
  578. // TODO: what is the proper thing to do here??
  579. //Serial.printf(" c1=%02X ", I2C0_C1);
  580. I2C0_C1 = I2C_C1_IICEN;
  581. ret = 4; // 4:other error
  582. break;
  583. }
  584. if (!(status & I2C_S_BUSY)) {
  585. // suddenly lost control of the bus!
  586. I2C0_C1 = I2C_C1_IICEN;
  587. ret = 4; // 4:other error
  588. break;
  589. }
  590. if (status & I2C_S_RXAK) {
  591. // the slave device did not acknowledge
  592. if (i == 0) {
  593. ret = 2; // 2:received NACK on transmit of address
  594. } else {
  595. ret = 3; // 3:received NACK on transmit of data
  596. }
  597. sendStop = 1;
  598. break;
  599. }
  600. }
  601. if (sendStop) {
  602. // send the stop condition
  603. I2C0_C1 = I2C_C1_IICEN;
  604. // TODO: do we wait for this somehow?
  605. }
  606. transmitting = 0;
  607. //Serial.print(" ret=");
  608. //Serial.println(ret);
  609. return ret;
  610. }
  611. uint8_t TwoWire::requestFrom(uint8_t address, uint8_t length, uint8_t sendStop)
  612. {
  613. uint8_t tmp __attribute__((unused));
  614. uint8_t status, count=0;
  615. rxBufferIndex = 0;
  616. rxBufferLength = 0;
  617. //serial_print("requestFrom\n");
  618. // clear the status flags
  619. I2C0_S = I2C_S_IICIF | I2C_S_ARBL;
  620. // now take control of the bus...
  621. if (I2C0_C1 & I2C_C1_MST) {
  622. // we are already the bus master, so send a repeated start
  623. I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_RSTA | I2C_C1_TX;
  624. } else {
  625. // we are not currently the bus master, so wait for bus ready
  626. while (i2c_status() & I2C_S_BUSY) ;
  627. // become the bus master in transmit mode (send start)
  628. slave_mode = 0;
  629. I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TX;
  630. }
  631. // send the address
  632. I2C0_D = (address << 1) | 1;
  633. i2c_wait();
  634. status = i2c_status();
  635. if ((status & I2C_S_RXAK) || (status & I2C_S_ARBL)) {
  636. // the slave device did not acknowledge
  637. // or we lost bus arbitration to another master
  638. I2C0_C1 = I2C_C1_IICEN;
  639. return 0;
  640. }
  641. if (length == 0) {
  642. // TODO: does anybody really do zero length reads?
  643. // if so, does this code really work?
  644. I2C0_C1 = I2C_C1_IICEN | (sendStop ? 0 : I2C_C1_MST);
  645. return 0;
  646. } else if (length == 1) {
  647. I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TXAK;
  648. } else {
  649. I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST;
  650. }
  651. tmp = I2C0_D; // initiate the first receive
  652. while (length > 1) {
  653. i2c_wait();
  654. length--;
  655. if (length == 1) I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TXAK;
  656. if (count < BUFFER_LENGTH) {
  657. rxBuffer[count++] = I2C0_D;
  658. } else {
  659. tmp = I2C0_D;
  660. }
  661. }
  662. i2c_wait();
  663. I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TX;
  664. if (count < BUFFER_LENGTH) {
  665. rxBuffer[count++] = I2C0_D;
  666. } else {
  667. tmp = I2C0_D;
  668. }
  669. if (sendStop) I2C0_C1 = I2C_C1_IICEN;
  670. rxBufferLength = count;
  671. return count;
  672. }
  673. int TwoWire::available(void)
  674. {
  675. return rxBufferLength - rxBufferIndex;
  676. }
  677. int TwoWire::read(void)
  678. {
  679. if (rxBufferIndex >= rxBufferLength) return -1;
  680. return rxBuffer[rxBufferIndex++];
  681. }
  682. int TwoWire::peek(void)
  683. {
  684. if (rxBufferIndex >= rxBufferLength) return -1;
  685. return rxBuffer[rxBufferIndex];
  686. }
  687. // alternate function prototypes
  688. uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity)
  689. {
  690. return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)true);
  691. }
  692. uint8_t TwoWire::requestFrom(int address, int quantity)
  693. {
  694. return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)true);
  695. }
  696. uint8_t TwoWire::requestFrom(int address, int quantity, int sendStop)
  697. {
  698. return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)sendStop);
  699. }
  700. void TwoWire::beginTransmission(int address)
  701. {
  702. beginTransmission((uint8_t)address);
  703. }
  704. uint8_t TwoWire::endTransmission(void)
  705. {
  706. return endTransmission(true);
  707. }
  708. void TwoWire::begin(int address)
  709. {
  710. begin((uint8_t)address);
  711. }
  712. void TwoWire::onReceive( void (*function)(int) )
  713. {
  714. user_onReceive = function;
  715. }
  716. void TwoWire::onRequest( void (*function)(void) )
  717. {
  718. user_onRequest = function;
  719. }
  720. //TwoWire Wire = TwoWire();
  721. TwoWire Wire;
  722. #endif // __MK20DX128__ || __MK20DX256__
  723. #if defined(__AVR__)
  724. extern "C" {
  725. #include <stdlib.h>
  726. #include <string.h>
  727. #include <inttypes.h>
  728. #include "twi.h"
  729. }
  730. // Initialize Class Variables //////////////////////////////////////////////////
  731. uint8_t TwoWire::rxBuffer[BUFFER_LENGTH];
  732. uint8_t TwoWire::rxBufferIndex = 0;
  733. uint8_t TwoWire::rxBufferLength = 0;
  734. uint8_t TwoWire::txAddress = 0;
  735. uint8_t TwoWire::txBuffer[BUFFER_LENGTH];
  736. uint8_t TwoWire::txBufferIndex = 0;
  737. uint8_t TwoWire::txBufferLength = 0;
  738. uint8_t TwoWire::transmitting = 0;
  739. void (*TwoWire::user_onRequest)(void);
  740. void (*TwoWire::user_onReceive)(int);
  741. // Constructors ////////////////////////////////////////////////////////////////
  742. TwoWire::TwoWire()
  743. {
  744. }
  745. // Public Methods //////////////////////////////////////////////////////////////
  746. void TwoWire::begin(void)
  747. {
  748. rxBufferIndex = 0;
  749. rxBufferLength = 0;
  750. txBufferIndex = 0;
  751. txBufferLength = 0;
  752. twi_init();
  753. }
  754. void TwoWire::begin(uint8_t address)
  755. {
  756. twi_setAddress(address);
  757. twi_attachSlaveTxEvent(onRequestService);
  758. twi_attachSlaveRxEvent(onReceiveService);
  759. begin();
  760. }
  761. void TwoWire::begin(int address)
  762. {
  763. begin((uint8_t)address);
  764. }
  765. void TwoWire::end()
  766. {
  767. TWCR &= ~(_BV(TWEN) | _BV(TWIE) | _BV(TWEA));
  768. digitalWrite(SDA, 0);
  769. digitalWrite(SCL, 0);
  770. }
  771. void TwoWire::setClock(uint32_t frequency)
  772. {
  773. TWBR = ((F_CPU / frequency) - 16) / 2;
  774. }
  775. void TwoWire::setSDA(uint8_t pin)
  776. {
  777. }
  778. void TwoWire::setSCL(uint8_t pin)
  779. {
  780. }
  781. uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop)
  782. {
  783. // clamp to buffer length
  784. if(quantity > BUFFER_LENGTH){
  785. quantity = BUFFER_LENGTH;
  786. }
  787. // perform blocking read into buffer
  788. uint8_t read = twi_readFrom(address, rxBuffer, quantity, sendStop);
  789. // set rx buffer iterator vars
  790. rxBufferIndex = 0;
  791. rxBufferLength = read;
  792. return read;
  793. }
  794. uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity)
  795. {
  796. return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)true);
  797. }
  798. uint8_t TwoWire::requestFrom(int address, int quantity)
  799. {
  800. return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)true);
  801. }
  802. uint8_t TwoWire::requestFrom(int address, int quantity, int sendStop)
  803. {
  804. return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)sendStop);
  805. }
  806. void TwoWire::beginTransmission(uint8_t address)
  807. {
  808. // indicate that we are transmitting
  809. transmitting = 1;
  810. // set address of targeted slave
  811. txAddress = address;
  812. // reset tx buffer iterator vars
  813. txBufferIndex = 0;
  814. txBufferLength = 0;
  815. }
  816. void TwoWire::beginTransmission(int address)
  817. {
  818. beginTransmission((uint8_t)address);
  819. }
  820. //
  821. // Originally, 'endTransmission' was an f(void) function.
  822. // It has been modified to take one parameter indicating
  823. // whether or not a STOP should be performed on the bus.
  824. // Calling endTransmission(false) allows a sketch to
  825. // perform a repeated start.
  826. //
  827. // WARNING: Nothing in the library keeps track of whether
  828. // the bus tenure has been properly ended with a STOP. It
  829. // is very possible to leave the bus in a hung state if
  830. // no call to endTransmission(true) is made. Some I2C
  831. // devices will behave oddly if they do not see a STOP.
  832. //
  833. uint8_t TwoWire::endTransmission(uint8_t sendStop)
  834. {
  835. // transmit buffer (blocking)
  836. int8_t ret = twi_writeTo(txAddress, txBuffer, txBufferLength, 1, sendStop);
  837. // reset tx buffer iterator vars
  838. txBufferIndex = 0;
  839. txBufferLength = 0;
  840. // indicate that we are done transmitting
  841. transmitting = 0;
  842. return ret;
  843. }
  844. // This provides backwards compatibility with the original
  845. // definition, and expected behaviour, of endTransmission
  846. //
  847. uint8_t TwoWire::endTransmission(void)
  848. {
  849. return endTransmission(true);
  850. }
  851. // must be called in:
  852. // slave tx event callback
  853. // or after beginTransmission(address)
  854. size_t TwoWire::write(uint8_t data)
  855. {
  856. if(transmitting){
  857. // in master transmitter mode
  858. // don't bother if buffer is full
  859. if(txBufferLength >= BUFFER_LENGTH){
  860. setWriteError();
  861. return 0;
  862. }
  863. // put byte in tx buffer
  864. txBuffer[txBufferIndex] = data;
  865. ++txBufferIndex;
  866. // update amount in buffer
  867. txBufferLength = txBufferIndex;
  868. }else{
  869. // in slave send mode
  870. // reply to master
  871. twi_transmit(&data, 1);
  872. }
  873. return 1;
  874. }
  875. // must be called in:
  876. // slave tx event callback
  877. // or after beginTransmission(address)
  878. size_t TwoWire::write(const uint8_t *data, size_t quantity)
  879. {
  880. if(transmitting){
  881. // in master transmitter mode
  882. for(size_t i = 0; i < quantity; ++i){
  883. write(data[i]);
  884. }
  885. }else{
  886. // in slave send mode
  887. // reply to master
  888. twi_transmit(data, quantity);
  889. }
  890. return quantity;
  891. }
  892. // must be called in:
  893. // slave rx event callback
  894. // or after requestFrom(address, numBytes)
  895. int TwoWire::available(void)
  896. {
  897. return rxBufferLength - rxBufferIndex;
  898. }
  899. // must be called in:
  900. // slave rx event callback
  901. // or after requestFrom(address, numBytes)
  902. int TwoWire::read(void)
  903. {
  904. int value = -1;
  905. // get each successive byte on each call
  906. if(rxBufferIndex < rxBufferLength){
  907. value = rxBuffer[rxBufferIndex];
  908. ++rxBufferIndex;
  909. }
  910. return value;
  911. }
  912. // must be called in:
  913. // slave rx event callback
  914. // or after requestFrom(address, numBytes)
  915. int TwoWire::peek(void)
  916. {
  917. int value = -1;
  918. if(rxBufferIndex < rxBufferLength){
  919. value = rxBuffer[rxBufferIndex];
  920. }
  921. return value;
  922. }
  923. void TwoWire::flush(void)
  924. {
  925. // XXX: to be implemented.
  926. }
  927. // behind the scenes function that is called when data is received
  928. void TwoWire::onReceiveService(uint8_t* inBytes, int numBytes)
  929. {
  930. // don't bother if user hasn't registered a callback
  931. if(!user_onReceive){
  932. return;
  933. }
  934. // don't bother if rx buffer is in use by a master requestFrom() op
  935. // i know this drops data, but it allows for slight stupidity
  936. // meaning, they may not have read all the master requestFrom() data yet
  937. if(rxBufferIndex < rxBufferLength){
  938. return;
  939. }
  940. // copy twi rx buffer into local read buffer
  941. // this enables new reads to happen in parallel
  942. for(uint8_t i = 0; i < numBytes; ++i){
  943. rxBuffer[i] = inBytes[i];
  944. }
  945. // set rx iterator vars
  946. rxBufferIndex = 0;
  947. rxBufferLength = numBytes;
  948. // alert user program
  949. user_onReceive(numBytes);
  950. }
  951. // behind the scenes function that is called when data is requested
  952. void TwoWire::onRequestService(void)
  953. {
  954. // don't bother if user hasn't registered a callback
  955. if(!user_onRequest){
  956. return;
  957. }
  958. // reset tx buffer iterator vars
  959. // !!! this will kill any pending pre-master sendTo() activity
  960. txBufferIndex = 0;
  961. txBufferLength = 0;
  962. // alert user program
  963. user_onRequest();
  964. }
  965. // sets function called on slave write
  966. void TwoWire::onReceive( void (*function)(int) )
  967. {
  968. user_onReceive = function;
  969. }
  970. // sets function called on slave read
  971. void TwoWire::onRequest( void (*function)(void) )
  972. {
  973. user_onRequest = function;
  974. }
  975. // Preinstantiate Objects //////////////////////////////////////////////////////
  976. TwoWire Wire = TwoWire();
  977. #endif // __AVR__