My Marlin configs for Fabrikator Mini and CTC i3 Pro B
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.

u8g_com_i2c.c 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. static uint8_t u8g_i2c_err_code;
  30. /*
  31. position values
  32. 1: start condition
  33. 2: sla transfer
  34. */
  35. static uint8_t u8g_i2c_err_pos;
  36. void u8g_i2c_clear_error(void)
  37. {
  38. u8g_i2c_err_code = U8G_I2C_ERR_NONE;
  39. u8g_i2c_err_pos = 0;
  40. }
  41. uint8_t u8g_i2c_get_error(void)
  42. {
  43. return u8g_i2c_err_code;
  44. }
  45. uint8_t u8g_i2c_get_err_pos(void)
  46. {
  47. return u8g_i2c_err_pos;
  48. }
  49. static void u8g_i2c_set_error(uint8_t code, uint8_t pos)
  50. {
  51. if ( u8g_i2c_err_code > 0 )
  52. return;
  53. u8g_i2c_err_code |= code;
  54. u8g_i2c_err_pos = pos;
  55. }
  56. #if defined(__AVR__)
  57. #define U8G_ATMEGA_HW_TWI
  58. /* remove the definition for attiny */
  59. #if __AVR_ARCH__ == 2
  60. #undef U8G_ATMEGA_HW_TWI
  61. #endif
  62. #if __AVR_ARCH__ == 25
  63. #undef U8G_ATMEGA_HW_TWI
  64. #endif
  65. #endif
  66. #if defined(U8G_ATMEGA_HW_TWI)
  67. #include <avr/io.h>
  68. #include <util/twi.h>
  69. void u8g_i2c_init(uint8_t options)
  70. {
  71. /*
  72. TWBR: bit rate register
  73. TWSR: status register (contains preselector bits)
  74. prescalar
  75. 0 1
  76. 1 4
  77. 2 16
  78. 3 64
  79. f = F_CPU/(16+2*TWBR*prescalar)
  80. F_CPU = 16MHz
  81. TWBR = 152;
  82. TWSR = 0;
  83. --> 50KHz
  84. TWBR = 72;
  85. TWSR = 0;
  86. --> 100KHz
  87. F_CPU/(2*100000)-8 --> calculate TWBR value for 100KHz
  88. */
  89. TWSR = 0;
  90. TWBR = F_CPU/(2*100000)-8;
  91. u8g_i2c_clear_error();
  92. }
  93. uint8_t u8g_i2c_wait(uint8_t mask, uint8_t pos)
  94. {
  95. volatile uint16_t cnt = 2000; /* timout value should be > 280 for 50KHz Bus and 16 Mhz CPU, however the start condition might need longer */
  96. while( !(TWCR & mask) )
  97. {
  98. if ( cnt == 0 )
  99. {
  100. u8g_i2c_set_error(U8G_I2C_ERR_TIMEOUT, pos);
  101. return 0; /* error */
  102. }
  103. cnt--;
  104. }
  105. return 1; /* all ok */
  106. }
  107. /* sla includes all 8 bits (with r/w bit), assums master transmit */
  108. uint8_t u8g_i2c_start(uint8_t sla)
  109. {
  110. register uint8_t status;
  111. /* send start */
  112. TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN);
  113. /* wait */
  114. if ( u8g_i2c_wait(_BV(TWINT), 1) == 0 )
  115. return 0;
  116. status = TW_STATUS;
  117. /* check status after start */
  118. if ( status != TW_START && status != TW_REP_START )
  119. {
  120. u8g_i2c_set_error(U8G_I2C_ERR_BUS, 1);
  121. return 0;
  122. }
  123. /* set slave address */
  124. TWDR = sla;
  125. /* enable sla transfer */
  126. TWCR = _BV(TWINT) | _BV(TWEN);
  127. /* wait */
  128. if ( u8g_i2c_wait(_BV(TWINT), 2) == 0 )
  129. return 0;
  130. status = TW_STATUS;
  131. /* check status after sla */
  132. if ( status != TW_MT_SLA_ACK )
  133. {
  134. u8g_i2c_set_error(U8G_I2C_ERR_BUS, 2);
  135. return 0;
  136. }
  137. return 1;
  138. }
  139. uint8_t u8g_i2c_send_byte(uint8_t data)
  140. {
  141. register uint8_t status;
  142. TWDR = data;
  143. TWCR = _BV(TWINT) | _BV(TWEN);
  144. if ( u8g_i2c_wait(_BV(TWINT), 3) == 0 )
  145. return 0;
  146. status = TW_STATUS;
  147. if ( status != TW_MT_DATA_ACK )
  148. {
  149. u8g_i2c_set_error(U8G_I2C_ERR_BUS, 3);
  150. return 0;
  151. }
  152. return 1;
  153. }
  154. void u8g_i2c_stop(void)
  155. {
  156. /* write stop */
  157. TWCR = _BV(TWINT) | _BV(TWEN) | _BV(TWSTO);
  158. /* no error is checked for the stop condition */
  159. u8g_i2c_wait(_BV(TWSTO), 4);
  160. }
  161. /*
  162. void twi_send(uint8_t adr, uint8_t data1, uint8_t data2)
  163. {
  164. u8g_i2c_start(adr<<1);
  165. u8g_i2c_send_byte(data1);
  166. u8g_i2c_send_byte(data2);
  167. u8g_i2c_stop();
  168. }
  169. */
  170. #else
  171. /* empty interface */
  172. void u8g_i2c_init(uint8_t options)
  173. {
  174. u8g_i2c_clear_error();
  175. }
  176. uint8_t u8g_i2c_wait(uint8_t mask, uint8_t pos)
  177. {
  178. return 1;
  179. }
  180. uint8_t u8g_i2c_start(uint8_t sla)
  181. {
  182. return 1;
  183. }
  184. uint8_t u8g_i2c_send_byte(uint8_t data)
  185. {
  186. return 1;
  187. }
  188. void u8g_i2c_stop(void)
  189. {
  190. }
  191. #endif