u8g_com_arduino_uc_i2c.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. u8g_com_arduino_uc_i2c.c
  3. com interface for arduino (AND atmega) and the SSDxxxx chip (SOLOMON) variant
  4. I2C protocol
  5. ToDo: Rename this to u8g_com_avr_ssd_i2c.c
  6. Universal 8bit Graphics Library
  7. Copyright (c) 2012, olikraus@gmail.com
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without modification,
  10. are permitted provided that the following conditions are met:
  11. * Redistributions of source code must retain the above copyright notice, this list
  12. of conditions and the following disclaimer.
  13. * Redistributions in binary form must reproduce the above copyright notice, this
  14. list of conditions and the following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  17. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  18. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  19. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  21. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  26. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  28. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. Special pin usage:
  30. U8G_PI_I2C_OPTION additional options
  31. U8G_PI_A0_STATE used to store the last value of the command/data register selection
  32. U8G_PI_SET_A0 1: Signal request to update I2C device with new A0_STATE, 0: Do nothing, A0_STATE matches I2C device
  33. U8G_PI_SCL clock line (NOT USED)
  34. U8G_PI_SDA data line (NOT USED)
  35. U8G_PI_RESET reset line (currently disabled, see below)
  36. Protocol:
  37. SLA, Cmd/Data Selection, Arguments
  38. The command/data register is selected by a special instruction byte, which is sent after SLA
  39. The continue bit is always 0 so that a (re)start is equired for the change from cmd to/data mode
  40. */
  41. #include "u8g.h"
  42. #if defined(U8G_WITH_PINLIST)
  43. #define DOGM240_SLA_CMD (0x38*2)
  44. #define DOGM240_SLA_DATA (0x39*2)
  45. uint8_t u8g_com_arduino_uc_start_sequence(u8g_t *u8g)
  46. {
  47. /* are we requested to set the a0 state? */
  48. if ( u8g->pin_list[U8G_PI_SET_A0] == 0 )
  49. return 1;
  50. if ( u8g->pin_list[U8G_PI_A0_STATE] == 0 )
  51. {
  52. if ( u8g_i2c_start(DOGM240_SLA_CMD) == 0 )
  53. return 0;
  54. }
  55. else
  56. {
  57. if ( u8g_i2c_start(DOGM240_SLA_DATA) == 0 )
  58. return 0;
  59. }
  60. u8g->pin_list[U8G_PI_SET_A0] = 0;
  61. return 1;
  62. }
  63. uint8_t u8g_com_arduino_uc_i2c_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
  64. {
  65. switch(msg)
  66. {
  67. case U8G_COM_MSG_INIT:
  68. //u8g_com_arduino_digital_write(u8g, U8G_PI_SCL, HIGH);
  69. //u8g_com_arduino_digital_write(u8g, U8G_PI_SDA, HIGH);
  70. //u8g->pin_list[U8G_PI_A0_STATE] = 0; /* inital RS state: unknown mode */
  71. u8g_i2c_init(u8g->pin_list[U8G_PI_I2C_OPTION]);
  72. break;
  73. case U8G_COM_MSG_STOP:
  74. break;
  75. case U8G_COM_MSG_RESET:
  76. /* Currently disabled, but it could be enable. Previous restrictions have been removed */
  77. /* u8g_com_arduino_digital_write(u8g, U8G_PI_RESET, arg_val); */
  78. break;
  79. case U8G_COM_MSG_CHIP_SELECT:
  80. u8g->pin_list[U8G_PI_A0_STATE] = 0;
  81. u8g->pin_list[U8G_PI_SET_A0] = 1; /* force a0 to set again, also forces start condition */
  82. if ( arg_val == 0 )
  83. {
  84. /* disable chip, send stop condition */
  85. u8g_i2c_stop();
  86. }
  87. else
  88. {
  89. /* enable, do nothing: any byte writing will trigger the i2c start */
  90. }
  91. break;
  92. case U8G_COM_MSG_WRITE_BYTE:
  93. //u8g->pin_list[U8G_PI_SET_A0] = 1;
  94. if ( u8g_com_arduino_uc_start_sequence(u8g) == 0 )
  95. return u8g_i2c_stop(), 0;
  96. if ( u8g_i2c_send_byte(arg_val) == 0 )
  97. return u8g_i2c_stop(), 0;
  98. // u8g_i2c_stop();
  99. break;
  100. case U8G_COM_MSG_WRITE_SEQ:
  101. //u8g->pin_list[U8G_PI_SET_A0] = 1;
  102. if ( u8g_com_arduino_uc_start_sequence(u8g) == 0 )
  103. return u8g_i2c_stop(), 0;
  104. {
  105. register uint8_t *ptr = arg_ptr;
  106. while( arg_val > 0 )
  107. {
  108. if ( u8g_i2c_send_byte(*ptr++) == 0 )
  109. return u8g_i2c_stop(), 0;
  110. arg_val--;
  111. }
  112. }
  113. // u8g_i2c_stop();
  114. break;
  115. case U8G_COM_MSG_WRITE_SEQ_P:
  116. //u8g->pin_list[U8G_PI_SET_A0] = 1;
  117. if ( u8g_com_arduino_uc_start_sequence(u8g) == 0 )
  118. return u8g_i2c_stop(), 0;
  119. {
  120. register uint8_t *ptr = arg_ptr;
  121. while( arg_val > 0 )
  122. {
  123. if ( u8g_i2c_send_byte(u8g_pgm_read(ptr)) == 0 )
  124. return 0;
  125. ptr++;
  126. arg_val--;
  127. }
  128. }
  129. // u8g_i2c_stop();
  130. break;
  131. case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 0) or data mode (arg_val = 1) */
  132. u8g->pin_list[U8G_PI_A0_STATE] = arg_val;
  133. u8g->pin_list[U8G_PI_SET_A0] = 1; /* force a0 to set again */
  134. #ifdef OLD_CODE
  135. if ( i2c_state != 0 )
  136. {
  137. u8g_i2c_stop();
  138. i2c_state = 0;
  139. }
  140. if ( u8g_com_arduino_uc_start_sequence(arg_val) == 0 )
  141. return 0;
  142. /* setup bus, might be a repeated start */
  143. /*
  144. if ( u8g_i2c_start(I2C_SLA) == 0 )
  145. return 0;
  146. if ( arg_val == 0 )
  147. {
  148. i2c_state = 1;
  149. if ( u8g_i2c_send_byte(I2C_CMD_MODE) == 0 )
  150. return 0;
  151. }
  152. else
  153. {
  154. i2c_state = 2;
  155. if ( u8g_i2c_send_byte(I2C_DATA_MODE) == 0 )
  156. return 0;
  157. }
  158. */
  159. #endif
  160. break;
  161. }
  162. return 1;
  163. }
  164. #else /* defined(U8G_WITH_PINLIST) */
  165. uint8_t u8g_com_arduino_uc_i2c_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
  166. {
  167. return 1;
  168. }
  169. #endif /* defined(U8G_WITH_PINLIST) */