u8g_com_i2c.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. /*
  2. u8g_com_i2c.c
  3. generic i2c interface
  4. Universal 8bit Graphics Library
  5. Copyright (c) 2011, olikraus@gmail.com
  6. All rights reserved.
  7. Redistribution and use in source and binary forms, with or without modification,
  8. are permitted provided that the following conditions are met:
  9. * Redistributions of source code must retain the above copyright notice, this list
  10. of conditions and the following disclaimer.
  11. * Redistributions in binary form must reproduce the above copyright notice, this
  12. list of conditions and the following disclaimer in the documentation and/or other
  13. materials provided with the distribution.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  15. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  16. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  17. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  19. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  24. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  26. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include "u8g.h"
  29. //#define U8G_I2C_WITH_NO_ACK
  30. static uint8_t u8g_i2c_err_code;
  31. static uint8_t u8g_i2c_opt; /* U8G_I2C_OPT_NO_ACK, SAM: U8G_I2C_OPT_DEV_1 */
  32. /*
  33. position values
  34. 1: start condition
  35. 2: sla transfer
  36. */
  37. static uint8_t u8g_i2c_err_pos;
  38. void u8g_i2c_clear_error(void)
  39. {
  40. u8g_i2c_err_code = U8G_I2C_ERR_NONE;
  41. u8g_i2c_err_pos = 0;
  42. }
  43. uint8_t u8g_i2c_get_error(void)
  44. {
  45. return u8g_i2c_err_code;
  46. }
  47. uint8_t u8g_i2c_get_err_pos(void)
  48. {
  49. return u8g_i2c_err_pos;
  50. }
  51. static void u8g_i2c_set_error(uint8_t code, uint8_t pos)
  52. {
  53. if ( u8g_i2c_err_code > 0 )
  54. return;
  55. u8g_i2c_err_code |= code;
  56. u8g_i2c_err_pos = pos;
  57. }
  58. #if defined(__AVR__)
  59. #define U8G_ATMEGA_HW_TWI
  60. /* remove the definition for attiny */
  61. #if __AVR_ARCH__ == 2
  62. #undef U8G_ATMEGA_HW_TWI
  63. #endif
  64. #if __AVR_ARCH__ == 25
  65. #undef U8G_ATMEGA_HW_TWI
  66. #endif
  67. #endif
  68. #if defined(U8G_ATMEGA_HW_TWI)
  69. #include <avr/io.h>
  70. #include <util/twi.h>
  71. void u8g_i2c_init(uint8_t options)
  72. {
  73. /*
  74. TWBR: bit rate register
  75. TWSR: status register (contains preselector bits)
  76. prescalar
  77. 0 1
  78. 1 4
  79. 2 16
  80. 3 64
  81. f = F_CPU/(16+2*TWBR*prescalar)
  82. F_CPU = 16MHz
  83. TWBR = 152;
  84. TWSR = 0;
  85. --> 50KHz
  86. TWBR = 72;
  87. TWSR = 0;
  88. --> 100KHz
  89. TWBR = 12;
  90. TWSR = 0;
  91. --> 400KHz
  92. F_CPU/(2*100000)-8 --> calculate TWBR value for 100KHz
  93. */
  94. u8g_i2c_opt = options;
  95. TWSR = 0;
  96. if ( options & U8G_I2C_OPT_FAST )
  97. {
  98. TWBR = F_CPU/(2*400000)-8;
  99. }
  100. else
  101. {
  102. TWBR = F_CPU/(2*100000)-8;
  103. }
  104. u8g_i2c_clear_error();
  105. }
  106. uint8_t u8g_i2c_wait(uint8_t mask, uint8_t pos)
  107. {
  108. volatile uint16_t cnt = 2000; /* timout value should be > 280 for 50KHz Bus and 16 Mhz CPU, however the start condition might need longer */
  109. while( !(TWCR & mask) )
  110. {
  111. if ( cnt == 0 )
  112. {
  113. if ( u8g_i2c_opt & U8G_I2C_OPT_NO_ACK )
  114. {
  115. return 1; /* all ok */
  116. }
  117. else
  118. {
  119. u8g_i2c_set_error(U8G_I2C_ERR_TIMEOUT, pos);
  120. return 0; /* error */
  121. }
  122. }
  123. cnt--;
  124. }
  125. return 1; /* all ok */
  126. }
  127. /* sla includes all 8 bits (with r/w bit), assums master transmit */
  128. uint8_t u8g_i2c_start(uint8_t sla)
  129. {
  130. register uint8_t status;
  131. /* send start */
  132. TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN);
  133. /* wait */
  134. if ( u8g_i2c_wait(_BV(TWINT), 1) == 0 )
  135. return 0;
  136. status = TW_STATUS;
  137. /* check status after start */
  138. if ( status != TW_START && status != TW_REP_START )
  139. {
  140. u8g_i2c_set_error(U8G_I2C_ERR_BUS, 1);
  141. return 0;
  142. }
  143. /* set slave address */
  144. TWDR = sla;
  145. /* enable sla transfer */
  146. TWCR = _BV(TWINT) | _BV(TWEN);
  147. /* wait */
  148. if ( u8g_i2c_wait(_BV(TWINT), 2) == 0 )
  149. return 0;
  150. if ( u8g_i2c_opt & U8G_I2C_OPT_NO_ACK )
  151. {
  152. /* do not check for ACK */
  153. }
  154. else
  155. {
  156. status = TW_STATUS;
  157. /* check status after sla */
  158. if ( status != TW_MT_SLA_ACK )
  159. {
  160. u8g_i2c_set_error(U8G_I2C_ERR_BUS, 2);
  161. return 0;
  162. }
  163. }
  164. return 1;
  165. }
  166. uint8_t u8g_i2c_send_byte(uint8_t data)
  167. {
  168. register uint8_t status;
  169. TWDR = data;
  170. TWCR = _BV(TWINT) | _BV(TWEN);
  171. if ( u8g_i2c_wait(_BV(TWINT), 3) == 0 )
  172. return 0;
  173. if ( u8g_i2c_opt & U8G_I2C_OPT_NO_ACK )
  174. {
  175. /* do not check for ACK */
  176. }
  177. else
  178. {
  179. status = TW_STATUS;
  180. if ( status != TW_MT_DATA_ACK )
  181. {
  182. u8g_i2c_set_error(U8G_I2C_ERR_BUS, 3);
  183. return 0;
  184. }
  185. }
  186. return 1;
  187. }
  188. void u8g_i2c_stop(void)
  189. {
  190. /* write stop */
  191. TWCR = _BV(TWINT) | _BV(TWEN) | _BV(TWSTO);
  192. /* no error is checked for the stop condition */
  193. u8g_i2c_wait(_BV(TWSTO), 4);
  194. }
  195. /*
  196. void twi_send(uint8_t adr, uint8_t data1, uint8_t data2)
  197. {
  198. u8g_i2c_start(adr<<1);
  199. u8g_i2c_send_byte(data1);
  200. u8g_i2c_send_byte(data2);
  201. u8g_i2c_stop();
  202. }
  203. */
  204. #elif defined(ARDUINO) && defined(__SAM3X8E__)
  205. /* Arduino Due */
  206. #include "Arduino.h"
  207. #include "sam.h"
  208. /*
  209. Controller
  210. TWI0 TWCK0 PA18 A DUE PCB: SCL1
  211. TWI0 TWD0 PA17 A DUE PCB: SDA1
  212. TWI1 TWCK1 PB13 A DUE PCB: SCL 21
  213. TWI1 TWD1 PB12 A DUE PCB: SDA 20
  214. Arduino definitions
  215. #define PIN_WIRE_SDA (20u)
  216. #define PIN_WIRE_SCL (21u)
  217. #define WIRE_INTERFACE TWI1
  218. #define WIRE_INTERFACE_ID ID_TWI1
  219. #define WIRE_ISR_HANDLER TWI1_Handler
  220. #define PIN_WIRE1_SDA (70u)
  221. #define PIN_WIRE1_SCL (71u)
  222. #define WIRE1_INTERFACE TWI0
  223. #define WIRE1_INTERFACE_ID ID_TWI0
  224. #define WIRE1_ISR_HANDLER TWI0_Handler
  225. */
  226. static void i2c_400KHz_delay(void)
  227. {
  228. /* should be at least 4 */
  229. /* should be 5 for 100KHz transfer speed */
  230. /*
  231. Arduino Due
  232. 0x NOP: 470KHz
  233. 4x NOP: 450KHz
  234. 8x NOP: 430KHz
  235. 16x NOP: 400KHz
  236. */
  237. __NOP();
  238. __NOP();
  239. __NOP();
  240. __NOP();
  241. __NOP();
  242. __NOP();
  243. __NOP();
  244. __NOP();
  245. __NOP();
  246. __NOP();
  247. __NOP();
  248. __NOP();
  249. __NOP();
  250. __NOP();
  251. __NOP();
  252. __NOP();
  253. }
  254. static void i2c_100KHz_delay(void)
  255. {
  256. /*
  257. 1x u8g_MicroDelay() ca. 130KHz
  258. 2x u8g_MicroDelay() ca. 80KHz
  259. */
  260. u8g_MicroDelay();
  261. u8g_MicroDelay();
  262. }
  263. uint32_t i2c_started = 0;
  264. uint32_t i2c_scl_pin = 0;
  265. uint32_t i2c_sda_pin = 0;
  266. void (*i2c_delay)(void) = i2c_100KHz_delay;
  267. const PinDescription *i2c_scl_pin_desc;
  268. const PinDescription *i2c_sda_pin_desc;
  269. /* maybe this can be optimized */
  270. static void i2c_init(void)
  271. {
  272. i2c_sda_pin_desc = &(g_APinDescription[i2c_sda_pin]);
  273. i2c_scl_pin_desc = &(g_APinDescription[i2c_scl_pin]);
  274. pinMode(i2c_sda_pin, OUTPUT);
  275. digitalWrite(i2c_sda_pin, HIGH);
  276. pinMode(i2c_scl_pin, OUTPUT);
  277. digitalWrite(i2c_scl_pin, HIGH);
  278. PIO_Configure( i2c_sda_pin_desc->pPort, PIO_OUTPUT_0, i2c_sda_pin_desc->ulPin, PIO_OPENDRAIN );
  279. PIO_Configure( i2c_scl_pin_desc->pPort, PIO_OUTPUT_0, i2c_scl_pin_desc->ulPin, PIO_OPENDRAIN );
  280. PIO_Clear( i2c_sda_pin_desc->pPort, i2c_sda_pin_desc->ulPin) ;
  281. PIO_Clear( i2c_scl_pin_desc->pPort, i2c_scl_pin_desc->ulPin) ;
  282. PIO_Configure( i2c_sda_pin_desc->pPort, PIO_INPUT, i2c_sda_pin_desc->ulPin, PIO_DEFAULT ) ;
  283. PIO_Configure( i2c_scl_pin_desc->pPort, PIO_INPUT, i2c_scl_pin_desc->ulPin, PIO_DEFAULT ) ;
  284. i2c_delay();
  285. }
  286. /* actually, the scl line is not observed, so this procedure does not return a value */
  287. static void i2c_read_scl_and_delay(void)
  288. {
  289. uint32_t dwMask = i2c_scl_pin_desc->ulPin;
  290. //PIO_Configure( i2c_scl_pin_desc->pPort, PIO_INPUT, i2c_scl_pin_desc->ulPin, PIO_DEFAULT ) ;
  291. //PIO_SetInput( i2c_scl_pin_desc->pPort, i2c_scl_pin_desc->ulPin, PIO_DEFAULT ) ;
  292. /* set as input */
  293. i2c_scl_pin_desc->pPort->PIO_ODR = dwMask ;
  294. i2c_scl_pin_desc->pPort->PIO_PER = dwMask ;
  295. i2c_delay();
  296. }
  297. static void i2c_clear_scl(void)
  298. {
  299. uint32_t dwMask = i2c_scl_pin_desc->ulPin;
  300. /* set open collector and drive low */
  301. //PIO_Configure( i2c_scl_pin_desc->pPort, PIO_OUTPUT_0, i2c_scl_pin_desc->ulPin, PIO_OPENDRAIN );
  302. //PIO_SetOutput( i2c_scl_pin_desc->pPort, i2c_scl_pin_desc->ulPin, 0, 1, 0);
  303. /* open drain, zero default output */
  304. i2c_scl_pin_desc->pPort->PIO_MDER = dwMask;
  305. i2c_scl_pin_desc->pPort->PIO_CODR = dwMask;
  306. i2c_scl_pin_desc->pPort->PIO_OER = dwMask;
  307. i2c_scl_pin_desc->pPort->PIO_PER = dwMask;
  308. //PIO_Clear( i2c_scl_pin_desc->pPort, i2c_scl_pin_desc->ulPin) ;
  309. }
  310. static uint8_t i2c_read_sda(void)
  311. {
  312. uint32_t dwMask = i2c_sda_pin_desc->ulPin;
  313. //PIO_Configure( i2c_sda_pin_desc->pPort, PIO_INPUT, i2c_sda_pin_desc->ulPin, PIO_DEFAULT ) ;
  314. //PIO_SetInput( i2c_sda_pin_desc->pPort, i2c_sda_pin_desc->ulPin, PIO_DEFAULT ) ;
  315. /* set as input */
  316. i2c_sda_pin_desc->pPort->PIO_ODR = dwMask ;
  317. i2c_sda_pin_desc->pPort->PIO_PER = dwMask ;
  318. return 1;
  319. }
  320. static void i2c_clear_sda(void)
  321. {
  322. uint32_t dwMask = i2c_sda_pin_desc->ulPin;
  323. /* set open collector and drive low */
  324. //PIO_Configure( i2c_sda_pin_desc->pPort, PIO_OUTPUT_0, i2c_sda_pin_desc->ulPin, PIO_OPENDRAIN );
  325. //PIO_SetOutput( i2c_sda_pin_desc->pPort, i2c_sda_pin_desc->ulPin, 0, 1, 0);
  326. /* open drain, zero default output */
  327. i2c_sda_pin_desc->pPort->PIO_MDER = dwMask ;
  328. i2c_sda_pin_desc->pPort->PIO_CODR = dwMask ;
  329. i2c_sda_pin_desc->pPort->PIO_OER = dwMask ;
  330. i2c_sda_pin_desc->pPort->PIO_PER = dwMask ;
  331. //PIO_Clear( i2c_sda_pin_desc->pPort, i2c_sda_pin_desc->ulPin) ;
  332. }
  333. static void i2c_start(void)
  334. {
  335. if ( i2c_started != 0 )
  336. {
  337. /* if already started: do restart */
  338. i2c_read_sda(); /* SDA = 1 */
  339. i2c_delay();
  340. i2c_read_scl_and_delay();
  341. }
  342. i2c_read_sda();
  343. /*
  344. if (i2c_read_sda() == 0)
  345. {
  346. // do something because arbitration is lost
  347. }
  348. */
  349. /* send the start condition, both lines go from 1 to 0 */
  350. i2c_clear_sda();
  351. i2c_delay();
  352. i2c_clear_scl();
  353. i2c_started = 1;
  354. }
  355. static void i2c_stop(void)
  356. {
  357. /* set SDA to 0 */
  358. i2c_clear_sda();
  359. i2c_delay();
  360. /* now release all lines */
  361. i2c_read_scl_and_delay();
  362. /* set SDA to 1 */
  363. i2c_read_sda();
  364. i2c_delay();
  365. i2c_started = 0;
  366. }
  367. static void i2c_write_bit(uint8_t val)
  368. {
  369. if (val)
  370. i2c_read_sda();
  371. else
  372. i2c_clear_sda();
  373. i2c_delay();
  374. i2c_read_scl_and_delay();
  375. i2c_clear_scl();
  376. }
  377. static uint8_t i2c_read_bit(void)
  378. {
  379. uint8_t val;
  380. /* do not drive SDA */
  381. i2c_read_sda();
  382. i2c_delay();
  383. i2c_read_scl_and_delay();
  384. val = i2c_read_sda();
  385. i2c_delay();
  386. i2c_clear_scl();
  387. return val;
  388. }
  389. static uint8_t i2c_write_byte(uint8_t b)
  390. {
  391. i2c_write_bit(b & 128);
  392. i2c_write_bit(b & 64);
  393. i2c_write_bit(b & 32);
  394. i2c_write_bit(b & 16);
  395. i2c_write_bit(b & 8);
  396. i2c_write_bit(b & 4);
  397. i2c_write_bit(b & 2);
  398. i2c_write_bit(b & 1);
  399. /* read ack from client */
  400. /* 0: ack was given by client */
  401. /* 1: nothing happend during ack cycle */
  402. return i2c_read_bit();
  403. }
  404. void u8g_i2c_init(uint8_t options)
  405. {
  406. u8g_i2c_opt = options;
  407. u8g_i2c_clear_error();
  408. if ( u8g_i2c_opt & U8G_I2C_OPT_FAST )
  409. {
  410. i2c_delay = i2c_400KHz_delay;
  411. }
  412. else
  413. {
  414. i2c_delay = i2c_100KHz_delay;
  415. }
  416. if ( u8g_i2c_opt & U8G_I2C_OPT_DEV_1 )
  417. {
  418. i2c_scl_pin = PIN_WIRE1_SCL;
  419. i2c_sda_pin = PIN_WIRE1_SDA;
  420. //REG_PIOA_PDR = PIO_PB12A_TWD1 | PIO_PB13A_TWCK1;
  421. }
  422. else
  423. {
  424. i2c_scl_pin = PIN_WIRE_SCL;
  425. i2c_sda_pin = PIN_WIRE_SDA;
  426. //REG_PIOA_PDR = PIO_PA17A_TWD0 | PIO_PA18A_TWCK0;
  427. }
  428. i2c_init();
  429. }
  430. /* sla includes also the r/w bit */
  431. uint8_t u8g_i2c_start(uint8_t sla)
  432. {
  433. i2c_start();
  434. i2c_write_byte(sla);
  435. return 1;
  436. }
  437. uint8_t u8g_i2c_send_byte(uint8_t data)
  438. {
  439. return i2c_write_byte(data);
  440. }
  441. void u8g_i2c_stop(void)
  442. {
  443. i2c_stop();
  444. }
  445. #elif defined(U8G_RASPBERRY_PI)
  446. #include <wiringPi.h>
  447. #include <wiringPiI2C.h>
  448. #include <stdio.h>
  449. #include <stdlib.h>
  450. #include <errno.h>
  451. #define I2C_SLA 0x3c
  452. static int fd=-1;
  453. static uint8_t i2cMode = 0;
  454. void u8g_i2c_init(uint8_t options) {
  455. u8g_i2c_clear_error();
  456. u8g_i2c_opt = options;
  457. if (wiringPiSetup() == -1) {
  458. printf("wiringPi-Error\n");
  459. exit(1);
  460. }
  461. fd = wiringPiI2CSetup(I2C_SLA);
  462. if (fd < 0) {
  463. printf ("Unable to open I2C device 0: %s\n", strerror (errno)) ;
  464. exit (1) ;
  465. }
  466. //u8g_SetPIOutput(u8g, U8G_PI_RESET);
  467. //u8g_SetPIOutput(u8g, U8G_PI_A0);
  468. }
  469. uint8_t u8g_i2c_start(uint8_t sla) {
  470. u8g_i2c_send_mode(0);
  471. return 1;
  472. }
  473. void u8g_i2c_stop(void) {
  474. }
  475. uint8_t u8g_i2c_send_mode(uint8_t mode) {
  476. i2cMode = mode;
  477. }
  478. uint8_t u8g_i2c_send_byte(uint8_t data) {
  479. wiringPiI2CWriteReg8(fd, i2cMode, data);
  480. return 1;
  481. }
  482. uint8_t u8g_i2c_wait(uint8_t mask, uint8_t pos)
  483. {
  484. return 1;
  485. }
  486. #else
  487. /* empty interface */
  488. void u8g_i2c_init(uint8_t options)
  489. {
  490. u8g_i2c_clear_error();
  491. }
  492. uint8_t u8g_i2c_wait(uint8_t mask, uint8_t pos)
  493. {
  494. return 1;
  495. }
  496. uint8_t u8g_i2c_start(uint8_t sla)
  497. {
  498. return 1;
  499. }
  500. uint8_t u8g_i2c_send_byte(uint8_t data)
  501. {
  502. return 1;
  503. }
  504. void u8g_i2c_stop(void)
  505. {
  506. }
  507. #endif