Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

WireKinetis.cpp 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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(TEENSYDUINO)
  19. #include "kinetis.h"
  20. #include <string.h> // for memcpy
  21. #include "core_pins.h"
  22. #include "Wire.h"
  23. // undefine these, so we can't accidentally access the hardware directly.
  24. #undef I2C0_A1
  25. #undef I2C0_F
  26. #undef I2C0_C1
  27. #undef I2C0_S
  28. #undef I2C0_D
  29. #undef I2C0_C2
  30. #undef I2C0_FLT
  31. #undef I2C0_RA
  32. #undef I2C0_SMB
  33. #undef I2C0_A2
  34. #undef I2C0_SLTH
  35. #undef I2C0_SLTL
  36. void sda_rising_isr(void);
  37. TwoWire::TwoWire(KINETIS_I2C_t &myport) : port(myport)
  38. {
  39. rxBufferIndex = 0;
  40. rxBufferLength = 0;
  41. txBufferIndex = 0;
  42. txBufferLength = 0;
  43. transmitting = 0;
  44. sda_pin_num = 18;
  45. scl_pin_num = 19;
  46. user_onRequest = NULL;
  47. user_onReceive = NULL;
  48. }
  49. void TwoWire::begin(void)
  50. {
  51. //serial_begin(BAUD2DIV(115200));
  52. //serial_print("\nWire Begin\n");
  53. slave_mode = 0;
  54. SIM_SCGC4 |= SIM_SCGC4_I2C0; // TODO: use bitband
  55. port.C1 = 0;
  56. // On Teensy 3.0 external pullup resistors *MUST* be used
  57. // the PORT_PCR_PE bit is ignored when in I2C mode
  58. // I2C will not work at all without pullup resistors
  59. // It might seem like setting PORT_PCR_PE & PORT_PCR_PS
  60. // would enable pullup resistors. However, there seems
  61. // to be a bug in chip while I2C is enabled, where setting
  62. // those causes the port to be driven strongly high.
  63. if (sda_pin_num == 18) {
  64. CORE_PIN18_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  65. } else if (sda_pin_num == 17) {
  66. CORE_PIN17_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  67. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  68. } else if (sda_pin_num == 34) {
  69. CORE_PIN34_CONFIG = PORT_PCR_MUX(5)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  70. } else if (sda_pin_num == 8) {
  71. CORE_PIN8_CONFIG = PORT_PCR_MUX(7)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  72. } else if (sda_pin_num == 48) {
  73. CORE_PIN48_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  74. #endif
  75. }
  76. if (scl_pin_num == 19) {
  77. CORE_PIN19_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  78. } else if (scl_pin_num == 16) {
  79. CORE_PIN16_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  80. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  81. } else if (scl_pin_num == 33) {
  82. CORE_PIN33_CONFIG = PORT_PCR_MUX(5)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  83. } else if (scl_pin_num == 7) {
  84. CORE_PIN7_CONFIG = PORT_PCR_MUX(7)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  85. } else if (scl_pin_num == 47) {
  86. CORE_PIN47_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  87. #endif
  88. }
  89. setClock(100000);
  90. port.C2 = I2C_C2_HDRS;
  91. port.C1 = I2C_C1_IICEN;
  92. //pinMode(3, OUTPUT);
  93. //pinMode(4, OUTPUT);
  94. }
  95. void TwoWire::setClock(uint32_t frequency)
  96. {
  97. if (!(SIM_SCGC4 & SIM_SCGC4_I2C0)) return;
  98. #if F_BUS == 120000000
  99. if (frequency < 400000) {
  100. port.F = I2C_F_DIV1152; // 104 kHz
  101. } else if (frequency < 1000000) {
  102. port.F = I2C_F_DIV288; // 416 kHz
  103. } else {
  104. port.F = I2C_F_DIV128; // 0.94 MHz
  105. }
  106. port.FLT = 4;
  107. #elif F_BUS == 108000000
  108. if (frequency < 400000) {
  109. port.F = I2C_F_DIV1024; // 105 kHz
  110. } else if (frequency < 1000000) {
  111. port.F = I2C_F_DIV256; // 422 kHz
  112. } else {
  113. port.F = I2C_F_DIV112; // 0.96 MHz
  114. }
  115. port.FLT = 4;
  116. #elif F_BUS == 96000000
  117. if (frequency < 400000) {
  118. port.F = I2C_F_DIV960; // 100 kHz
  119. } else if (frequency < 1000000) {
  120. port.F = I2C_F_DIV240; // 400 kHz
  121. } else {
  122. port.F = I2C_F_DIV96; // 1.0 MHz
  123. }
  124. port.FLT = 4;
  125. #elif F_BUS == 90000000
  126. if (frequency < 400000) {
  127. port.F = I2C_F_DIV896; // 100 kHz
  128. } else if (frequency < 1000000) {
  129. port.F = I2C_F_DIV224; // 402 kHz
  130. } else {
  131. port.F = I2C_F_DIV88; // 1.02 MHz
  132. }
  133. port.FLT = 4;
  134. #elif F_BUS == 80000000
  135. if (frequency < 400000) {
  136. port.F = I2C_F_DIV768; // 104 kHz
  137. } else if (frequency < 1000000) {
  138. port.F = I2C_F_DIV192; // 416 kHz
  139. } else {
  140. port.F = I2C_F_DIV80; // 1.0 MHz
  141. }
  142. port.FLT = 4;
  143. #elif F_BUS == 72000000
  144. if (frequency < 400000) {
  145. port.F = I2C_F_DIV640; // 112 kHz
  146. } else if (frequency < 1000000) {
  147. port.F = I2C_F_DIV192; // 375 kHz
  148. } else {
  149. port.F = I2C_F_DIV72; // 1.0 MHz
  150. }
  151. port.FLT = 4;
  152. #elif F_BUS == 64000000
  153. if (frequency < 400000) {
  154. port.F = I2C_F_DIV640; // 100 kHz
  155. } else if (frequency < 1000000) {
  156. port.F = I2C_F_DIV160; // 400 kHz
  157. } else {
  158. port.F = I2C_F_DIV64; // 1.0 MHz
  159. }
  160. port.FLT = 4;
  161. #elif F_BUS == 60000000
  162. if (frequency < 400000) {
  163. port.F = 0x2C; // 104 kHz
  164. } else if (frequency < 1000000) {
  165. port.F = 0x1C; // 416 kHz
  166. } else {
  167. port.F = 0x12; // 938 kHz
  168. }
  169. port.FLT = 4;
  170. #elif F_BUS == 56000000
  171. if (frequency < 400000) {
  172. port.F = 0x2B; // 109 kHz
  173. } else if (frequency < 1000000) {
  174. port.F = 0x1C; // 389 kHz
  175. } else {
  176. port.F = 0x0E; // 1 MHz
  177. }
  178. port.FLT = 4;
  179. #elif F_BUS == 54000000
  180. if (frequency < 400000) {
  181. port.F = I2C_F_DIV512; // 105 kHz
  182. } else if (frequency < 1000000) {
  183. port.F = I2C_F_DIV128; // 422 kHz
  184. } else {
  185. port.F = I2C_F_DIV56; // 0.96 MHz
  186. }
  187. port.FLT = 4;
  188. #elif F_BUS == 48000000
  189. if (frequency < 400000) {
  190. port.F = 0x27; // 100 kHz
  191. } else if (frequency < 1000000) {
  192. port.F = 0x1A; // 400 kHz
  193. } else {
  194. port.F = 0x0D; // 1 MHz
  195. }
  196. port.FLT = 4;
  197. #elif F_BUS == 40000000
  198. if (frequency < 400000) {
  199. port.F = 0x29; // 104 kHz
  200. } else if (frequency < 1000000) {
  201. port.F = 0x19; // 416 kHz
  202. } else {
  203. port.F = 0x0B; // 1 MHz
  204. }
  205. port.FLT = 3;
  206. #elif F_BUS == 36000000
  207. if (frequency < 400000) {
  208. port.F = 0x28; // 113 kHz
  209. } else if (frequency < 1000000) {
  210. port.F = 0x19; // 375 kHz
  211. } else {
  212. port.F = 0x0A; // 1 MHz
  213. }
  214. port.FLT = 3;
  215. #elif F_BUS == 24000000
  216. if (frequency < 400000) {
  217. port.F = 0x1F; // 100 kHz
  218. } else if (frequency < 1000000) {
  219. port.F = 0x12; // 375 kHz
  220. } else {
  221. port.F = 0x02; // 1 MHz
  222. }
  223. port.FLT = 2;
  224. #elif F_BUS == 16000000
  225. if (frequency < 400000) {
  226. port.F = 0x20; // 100 kHz
  227. } else if (frequency < 1000000) {
  228. port.F = 0x07; // 400 kHz
  229. } else {
  230. port.F = 0x00; // 800 MHz
  231. }
  232. port.FLT = 1;
  233. #elif F_BUS == 8000000
  234. if (frequency < 400000) {
  235. port.F = 0x14; // 100 kHz
  236. } else {
  237. port.F = 0x00; // 400 kHz
  238. }
  239. port.FLT = 1;
  240. #elif F_BUS == 4000000
  241. if (frequency < 400000) {
  242. port.F = 0x07; // 100 kHz
  243. } else {
  244. port.F = 0x00; // 200 kHz
  245. }
  246. port.FLT = 1;
  247. #elif F_BUS == 2000000
  248. port.F = 0x00; // 100 kHz
  249. port.FLT = 1;
  250. #else
  251. #error "F_BUS must be 120, 108, 96, 90, 80, 72, 64, 60, 56, 54, 48, 40, 36, 24, 16, 8, 4 or 2 MHz"
  252. #endif
  253. }
  254. void TwoWire::setSDA(uint8_t pin)
  255. {
  256. if (pin == sda_pin_num) return;
  257. if ((SIM_SCGC4 & SIM_SCGC4_I2C0)) {
  258. if (sda_pin_num == 18) {
  259. CORE_PIN18_CONFIG = 0;
  260. } else if (sda_pin_num == 17) {
  261. CORE_PIN17_CONFIG = 0;
  262. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  263. } else if (sda_pin_num == 34) {
  264. CORE_PIN34_CONFIG = 0;
  265. } else if (sda_pin_num == 8) {
  266. CORE_PIN8_CONFIG = 0;
  267. } else if (sda_pin_num == 48) {
  268. CORE_PIN48_CONFIG = 0;
  269. #endif
  270. }
  271. if (pin == 18) {
  272. CORE_PIN18_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  273. } else if (pin == 17) {
  274. CORE_PIN17_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  275. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  276. } else if (pin == 34) {
  277. CORE_PIN34_CONFIG = PORT_PCR_MUX(5)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  278. } else if (pin == 8) {
  279. CORE_PIN8_CONFIG = PORT_PCR_MUX(7)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  280. } else if (pin == 48) {
  281. CORE_PIN48_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  282. #endif
  283. }
  284. }
  285. sda_pin_num = pin;
  286. }
  287. void TwoWire::setSCL(uint8_t pin)
  288. {
  289. if (pin == scl_pin_num) return;
  290. if ((SIM_SCGC4 & SIM_SCGC4_I2C0)) {
  291. if (scl_pin_num == 19) {
  292. CORE_PIN19_CONFIG = 0;
  293. } else if (scl_pin_num == 16) {
  294. CORE_PIN16_CONFIG = 0;
  295. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  296. } else if (scl_pin_num == 33) {
  297. CORE_PIN33_CONFIG = 0;
  298. } else if (scl_pin_num == 7) {
  299. CORE_PIN7_CONFIG = 0;
  300. } else if (scl_pin_num == 47) {
  301. CORE_PIN47_CONFIG = 0;
  302. #endif
  303. }
  304. if (pin == 19) {
  305. CORE_PIN19_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  306. } else if (pin == 16) {
  307. CORE_PIN16_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  308. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  309. } else if (pin == 33) {
  310. CORE_PIN33_CONFIG = PORT_PCR_MUX(5)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  311. } else if (pin == 7) {
  312. CORE_PIN7_CONFIG = PORT_PCR_MUX(7)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  313. } else if (pin == 47) {
  314. CORE_PIN47_CONFIG = PORT_PCR_MUX(2)|PORT_PCR_ODE|PORT_PCR_SRE|PORT_PCR_DSE;
  315. #endif
  316. }
  317. }
  318. scl_pin_num = pin;
  319. }
  320. void TwoWire::begin(uint8_t address)
  321. {
  322. begin();
  323. port.A1 = address << 1;
  324. slave_mode = 1;
  325. port.C1 = I2C_C1_IICEN | I2C_C1_IICIE;
  326. NVIC_ENABLE_IRQ(IRQ_I2C0);
  327. }
  328. void TwoWire::end()
  329. {
  330. if (!(SIM_SCGC4 & SIM_SCGC4_I2C0)) return;
  331. NVIC_DISABLE_IRQ(IRQ_I2C0);
  332. port.C1 = 0;
  333. if (sda_pin_num == 18) {
  334. CORE_PIN18_CONFIG = 0;
  335. } else if (sda_pin_num == 17) {
  336. CORE_PIN17_CONFIG = 0;
  337. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  338. } else if (sda_pin_num == 34) {
  339. CORE_PIN34_CONFIG = 0;
  340. } else if (sda_pin_num == 8) {
  341. CORE_PIN8_CONFIG = 0;
  342. } else if (sda_pin_num == 48) {
  343. CORE_PIN48_CONFIG = 0;
  344. #endif
  345. }
  346. if (scl_pin_num == 19) {
  347. CORE_PIN19_CONFIG = 0;
  348. } else if (scl_pin_num == 16) {
  349. CORE_PIN16_CONFIG = 0;
  350. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  351. } else if (scl_pin_num == 33) {
  352. CORE_PIN33_CONFIG = 0;
  353. } else if (scl_pin_num == 7) {
  354. CORE_PIN7_CONFIG = 0;
  355. } else if (scl_pin_num == 47) {
  356. CORE_PIN47_CONFIG = 0;
  357. #endif
  358. }
  359. SIM_SCGC4 &= ~SIM_SCGC4_I2C0; // TODO: use bitband
  360. }
  361. void i2c0_isr(void)
  362. {
  363. uint8_t status, c1, data;
  364. static uint8_t receiving=0;
  365. status = Wire.port.S;
  366. //serial_print(".");
  367. if (status & I2C_S_ARBL) {
  368. // Arbitration Lost
  369. Wire.port.S = I2C_S_ARBL;
  370. //serial_print("a");
  371. if (receiving && Wire.rxBufferLength > 0) {
  372. // TODO: does this detect the STOP condition in slave receive mode?
  373. }
  374. if (!(status & I2C_S_IAAS)) return;
  375. }
  376. if (status & I2C_S_IAAS) {
  377. //serial_print("\n");
  378. // Addressed As A Slave
  379. if (status & I2C_S_SRW) {
  380. //serial_print("T");
  381. // Begin Slave Transmit
  382. receiving = 0;
  383. Wire.txBufferLength = 0;
  384. if (Wire.user_onRequest != NULL) {
  385. Wire.user_onRequest();
  386. }
  387. if (Wire.txBufferLength == 0) {
  388. // is this correct, transmitting a single zero
  389. // when we should send nothing? Arduino's AVR
  390. // implementation does this, but is it ok?
  391. Wire.txBufferLength = 1;
  392. Wire.txBuffer[0] = 0;
  393. }
  394. Wire.port.C1 = I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_TX;
  395. Wire.port.D = Wire.txBuffer[0];
  396. Wire.txBufferIndex = 1;
  397. } else {
  398. // Begin Slave Receive
  399. //serial_print("R");
  400. receiving = 1;
  401. Wire.rxBufferLength = 0;
  402. Wire.port.C1 = I2C_C1_IICEN | I2C_C1_IICIE;
  403. data = Wire.port.D;
  404. }
  405. Wire.port.S = I2C_S_IICIF;
  406. return;
  407. }
  408. #if defined(KINETISL)
  409. c1 = Wire.port.FLT;
  410. if ((c1 & I2C_FLT_STOPF) && (c1 & I2C_FLT_STOPIE)) {
  411. Wire.port.FLT = c1 & ~I2C_FLT_STOPIE;
  412. if (Wire.user_onReceive != NULL) {
  413. Wire.rxBufferIndex = 0;
  414. Wire.user_onReceive(Wire.rxBufferLength);
  415. }
  416. }
  417. #endif
  418. c1 = Wire.port.C1;
  419. if (c1 & I2C_C1_TX) {
  420. // Continue Slave Transmit
  421. //serial_print("t");
  422. if ((status & I2C_S_RXAK) == 0) {
  423. //serial_print(".");
  424. // Master ACK'd previous byte
  425. if (Wire.txBufferIndex < Wire.txBufferLength) {
  426. Wire.port.D = Wire.txBuffer[Wire.txBufferIndex++];
  427. } else {
  428. Wire.port.D = 0;
  429. }
  430. Wire.port.C1 = I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_TX;
  431. } else {
  432. //serial_print("*");
  433. // Master did not ACK previous byte
  434. Wire.port.C1 = I2C_C1_IICEN | I2C_C1_IICIE;
  435. data = Wire.port.D;
  436. }
  437. } else {
  438. // Continue Slave Receive
  439. Wire.irqcount = 0;
  440. #if defined(KINETISK)
  441. attachInterrupt(18, sda_rising_isr, RISING);
  442. #elif defined(KINETISL)
  443. Wire.port.FLT |= I2C_FLT_STOPIE;
  444. #endif
  445. //digitalWriteFast(4, HIGH);
  446. data = Wire.port.D;
  447. //serial_phex(data);
  448. if (Wire.rxBufferLength < BUFFER_LENGTH && receiving) {
  449. Wire.rxBuffer[Wire.rxBufferLength++] = data;
  450. }
  451. //digitalWriteFast(4, LOW);
  452. }
  453. Wire.port.S = I2C_S_IICIF;
  454. }
  455. // Detects the stop condition that terminates a slave receive transfer.
  456. // Sadly, the I2C in Kinetis K series lacks the stop detect interrupt
  457. // This pin change interrupt hack is needed to detect the stop condition
  458. void sda_rising_isr(void)
  459. {
  460. //digitalWrite(3, HIGH);
  461. if (!(Wire.port.S & I2C_S_BUSY)) {
  462. detachInterrupt(18);
  463. if (Wire.user_onReceive != NULL) {
  464. Wire.rxBufferIndex = 0;
  465. Wire.user_onReceive(Wire.rxBufferLength);
  466. }
  467. //delayMicroseconds(100);
  468. } else {
  469. if (++Wire.irqcount >= 2 || !Wire.slave_mode) {
  470. detachInterrupt(18);
  471. }
  472. }
  473. //digitalWrite(3, LOW);
  474. }
  475. // Chapter 44: Inter-Integrated Circuit (I2C) - Page 1012
  476. // I2C0_A1 // I2C Address Register 1
  477. // I2C0_F // I2C Frequency Divider register
  478. // I2C0_C1 // I2C Control Register 1
  479. // I2C0_S // I2C Status register
  480. // I2C0_D // I2C Data I/O register
  481. // I2C0_C2 // I2C Control Register 2
  482. // I2C0_FLT // I2C Programmable Input Glitch Filter register
  483. size_t TwoWire::write(uint8_t data)
  484. {
  485. if (transmitting || slave_mode) {
  486. if (txBufferLength >= BUFFER_LENGTH+1) {
  487. setWriteError();
  488. return 0;
  489. }
  490. txBuffer[txBufferLength++] = data;
  491. return 1;
  492. }
  493. return 0;
  494. }
  495. size_t TwoWire::write(const uint8_t *data, size_t quantity)
  496. {
  497. if (transmitting || slave_mode) {
  498. size_t avail = BUFFER_LENGTH+1 - txBufferLength;
  499. if (quantity > avail) {
  500. quantity = avail;
  501. setWriteError();
  502. }
  503. memcpy(txBuffer + txBufferLength, data, quantity);
  504. txBufferLength += quantity;
  505. return quantity;
  506. }
  507. return 0;
  508. }
  509. uint8_t TwoWire::endTransmission(uint8_t sendStop)
  510. {
  511. uint8_t i, status, ret=0;
  512. // clear the status flags
  513. port.S = I2C_S_IICIF | I2C_S_ARBL;
  514. // now take control of the bus...
  515. if (port.C1 & I2C_C1_MST) {
  516. // we are already the bus master, so send a repeated start
  517. //Serial.print("rstart:");
  518. port.C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_RSTA | I2C_C1_TX;
  519. } else {
  520. // we are not currently the bus master, so wait for bus ready
  521. //Serial.print("busy:");
  522. uint32_t wait_begin = millis();
  523. while (i2c_status() & I2C_S_BUSY) {
  524. //Serial.write('.') ;
  525. if (millis() - wait_begin > 15) {
  526. // bus stuck busy too long
  527. port.C1 = 0;
  528. port.C1 = I2C_C1_IICEN;
  529. //Serial.println("abort");
  530. return 4;
  531. }
  532. }
  533. // become the bus master in transmit mode (send start)
  534. slave_mode = 0;
  535. port.C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TX;
  536. }
  537. // wait until start condition establishes control of the bus
  538. while (1) {
  539. status = i2c_status();
  540. if ((status & I2C_S_BUSY)) break;
  541. }
  542. // transmit the address and data
  543. for (i=0; i < txBufferLength; i++) {
  544. port.D = txBuffer[i];
  545. //Serial.write('^');
  546. while (1) {
  547. status = i2c_status();
  548. if ((status & I2C_S_IICIF)) break;
  549. if (!(status & I2C_S_BUSY)) break;
  550. }
  551. port.S = I2C_S_IICIF;
  552. //Serial.write('$');
  553. status = i2c_status();
  554. if ((status & I2C_S_ARBL)) {
  555. // we lost bus arbitration to another master
  556. // TODO: what is the proper thing to do here??
  557. //Serial.printf(" c1=%02X ", port.C1);
  558. port.C1 = I2C_C1_IICEN;
  559. ret = 4; // 4:other error
  560. break;
  561. }
  562. if (!(status & I2C_S_BUSY)) {
  563. // suddenly lost control of the bus!
  564. port.C1 = I2C_C1_IICEN;
  565. ret = 4; // 4:other error
  566. break;
  567. }
  568. if (status & I2C_S_RXAK) {
  569. // the slave device did not acknowledge
  570. if (i == 0) {
  571. ret = 2; // 2:received NACK on transmit of address
  572. } else {
  573. ret = 3; // 3:received NACK on transmit of data
  574. }
  575. sendStop = 1;
  576. break;
  577. }
  578. }
  579. if (sendStop) {
  580. // send the stop condition
  581. port.C1 = I2C_C1_IICEN;
  582. // TODO: do we wait for this somehow?
  583. }
  584. transmitting = 0;
  585. //Serial.print(" ret=");
  586. //Serial.println(ret);
  587. return ret;
  588. }
  589. uint8_t TwoWire::requestFrom(uint8_t address, uint8_t length, uint8_t sendStop)
  590. {
  591. uint8_t tmp __attribute__((unused));
  592. uint8_t status, count=0;
  593. rxBufferIndex = 0;
  594. rxBufferLength = 0;
  595. //serial_print("requestFrom\n");
  596. // clear the status flags
  597. port.S = I2C_S_IICIF | I2C_S_ARBL;
  598. // now take control of the bus...
  599. if (port.C1 & I2C_C1_MST) {
  600. // we are already the bus master, so send a repeated start
  601. port.C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_RSTA | I2C_C1_TX;
  602. } else {
  603. // we are not currently the bus master, so wait for bus ready
  604. while (i2c_status() & I2C_S_BUSY) ;
  605. // become the bus master in transmit mode (send start)
  606. slave_mode = 0;
  607. port.C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TX;
  608. }
  609. // send the address
  610. port.D = (address << 1) | 1;
  611. i2c_wait();
  612. status = i2c_status();
  613. if ((status & I2C_S_RXAK) || (status & I2C_S_ARBL)) {
  614. // the slave device did not acknowledge
  615. // or we lost bus arbitration to another master
  616. port.C1 = I2C_C1_IICEN;
  617. return 0;
  618. }
  619. if (length == 0) {
  620. // TODO: does anybody really do zero length reads?
  621. // if so, does this code really work?
  622. port.C1 = I2C_C1_IICEN | (sendStop ? 0 : I2C_C1_MST);
  623. return 0;
  624. } else if (length == 1) {
  625. port.C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TXAK;
  626. } else {
  627. port.C1 = I2C_C1_IICEN | I2C_C1_MST;
  628. }
  629. tmp = port.D; // initiate the first receive
  630. while (length > 1) {
  631. i2c_wait();
  632. length--;
  633. if (length == 1) port.C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TXAK;
  634. if (count < BUFFER_LENGTH) {
  635. rxBuffer[count++] = port.D;
  636. } else {
  637. tmp = port.D;
  638. }
  639. }
  640. i2c_wait();
  641. port.C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TX;
  642. if (count < BUFFER_LENGTH) {
  643. rxBuffer[count++] = port.D;
  644. } else {
  645. tmp = port.D;
  646. }
  647. if (sendStop) port.C1 = I2C_C1_IICEN;
  648. rxBufferLength = count;
  649. return count;
  650. }
  651. TwoWire Wire(KINETIS_I2C0);
  652. #endif // __arm__ && TEENSYDUINO