u8g_com_raspberrypi_ssd_i2c.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. Special pin usage:
  3. U8G_PI_I2C_OPTION additional options
  4. U8G_PI_A0_STATE used to store the last value of the command/data register selection
  5. U8G_PI_SET_A0 1: Signal request to update I2C device with new A0_STATE, 0: Do nothing, A0_STATE matches I2C device
  6. U8G_PI_SCL clock line (NOT USED)
  7. U8G_PI_SDA data line (NOT USED)
  8. U8G_PI_RESET reset line (currently disabled, see below)
  9. Protocol:
  10. SLA, Cmd/Data Selection, Arguments
  11. The command/data register is selected by a special instruction byte, which is sent after SLA
  12. The continue bit is always 0 so that a (re)start is equired for the change from cmd to/data mode
  13. */
  14. #include "u8g.h"
  15. #if defined(U8G_RASPBERRY_PI)
  16. #include <wiringPi.h>
  17. #include <wiringPiI2C.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <errno.h>
  21. #define I2C_SLA 0x3c
  22. #define I2C_CMD_MODE 0x000
  23. #define I2C_DATA_MODE 0x040
  24. #if defined(U8G_WITH_PINLIST)
  25. uint8_t u8g_com_raspberrypi_ssd_start_sequence(u8g_t *u8g)
  26. {
  27. /* are we requested to set the a0 state? */
  28. if ( u8g->pin_list[U8G_PI_SET_A0] == 0 )
  29. return 1;
  30. /* setup bus, might be a repeated start */
  31. if ( u8g_i2c_start(I2C_SLA) == 0 )
  32. return 0;
  33. if ( u8g->pin_list[U8G_PI_A0_STATE] == 0 )
  34. {
  35. if ( u8g_i2c_send_mode(I2C_CMD_MODE) == 0 )
  36. return 0;
  37. }
  38. else
  39. {
  40. if ( u8g_i2c_send_mode(I2C_DATA_MODE) == 0 )
  41. return 0;
  42. }
  43. u8g->pin_list[U8G_PI_SET_A0] = 0;
  44. return 1;
  45. }
  46. uint8_t u8g_com_raspberrypi_ssd_i2c_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
  47. {
  48. switch(msg)
  49. {
  50. case U8G_COM_MSG_INIT:
  51. u8g_i2c_init(u8g->pin_list[U8G_PI_I2C_OPTION]);
  52. u8g_SetPIOutput(u8g, U8G_PI_RESET);
  53. u8g_SetPIOutput(u8g, U8G_PI_A0);
  54. break;
  55. case U8G_COM_MSG_STOP:
  56. break;
  57. case U8G_COM_MSG_RESET:
  58. break;
  59. case U8G_COM_MSG_CHIP_SELECT:
  60. u8g->pin_list[U8G_PI_A0_STATE] = 0;
  61. u8g->pin_list[U8G_PI_SET_A0] = 1; /* force a0 to set again, also forces start condition */
  62. if ( arg_val == 0 )
  63. {
  64. /* disable chip, send stop condition */
  65. u8g_i2c_stop();
  66. }
  67. else
  68. {
  69. /* enable, do nothing: any byte writing will trigger the i2c start */
  70. }
  71. break;
  72. case U8G_COM_MSG_WRITE_BYTE:
  73. //u8g->pin_list[U8G_PI_SET_A0] = 1;
  74. if ( u8g_com_raspberrypi_ssd_start_sequence(u8g) == 0 )
  75. return u8g_i2c_stop(), 0;
  76. if ( u8g_i2c_send_byte(arg_val) == 0 )
  77. return u8g_i2c_stop(), 0;
  78. // u8g_i2c_stop();
  79. break;
  80. case U8G_COM_MSG_WRITE_SEQ:
  81. //u8g->pin_list[U8G_PI_SET_A0] = 1;
  82. if ( u8g_com_raspberrypi_ssd_start_sequence(u8g) == 0 )
  83. return u8g_i2c_stop(), 0;
  84. {
  85. register uint8_t *ptr = (uint8_t *)arg_ptr;
  86. while( arg_val > 0 )
  87. {
  88. if ( u8g_i2c_send_byte(*ptr++) == 0 )
  89. return u8g_i2c_stop(), 0;
  90. arg_val--;
  91. }
  92. }
  93. // u8g_i2c_stop();
  94. break;
  95. case U8G_COM_MSG_WRITE_SEQ_P:
  96. //u8g->pin_list[U8G_PI_SET_A0] = 1;
  97. if ( u8g_com_raspberrypi_ssd_start_sequence(u8g) == 0 )
  98. return u8g_i2c_stop(), 0;
  99. {
  100. register uint8_t *ptr = (uint8_t *)arg_ptr;
  101. while( arg_val > 0 )
  102. {
  103. if ( u8g_i2c_send_byte(u8g_pgm_read(ptr)) == 0 )
  104. return 0;
  105. ptr++;
  106. arg_val--;
  107. }
  108. }
  109. // u8g_i2c_stop();
  110. break;
  111. case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 0) or data mode (arg_val = 1) */
  112. u8g->pin_list[U8G_PI_A0_STATE] = arg_val;
  113. u8g->pin_list[U8G_PI_SET_A0] = 1; /* force a0 to set again */
  114. #ifdef OLD_CODE
  115. if ( i2c_state != 0 )
  116. {
  117. u8g_i2c_stop();
  118. i2c_state = 0;
  119. }
  120. if ( u8g_com_raspberrypi_ssd_start_sequence(arg_val) == 0 )
  121. return 0;
  122. /* setup bus, might be a repeated start */
  123. /*
  124. if ( u8g_i2c_start(I2C_SLA) == 0 )
  125. return 0;
  126. if ( arg_val == 0 )
  127. {
  128. i2c_state = 1;
  129. if ( u8g_i2c_send_byte(I2C_CMD_MODE) == 0 )
  130. return 0;
  131. }
  132. else
  133. {
  134. i2c_state = 2;
  135. if ( u8g_i2c_send_byte(I2C_DATA_MODE) == 0 )
  136. return 0;
  137. }
  138. */
  139. #endif
  140. break;
  141. }
  142. return 1;
  143. }
  144. #else /* defined(U8G_WITH_PINLIST) */
  145. uint8_t u8g_com_raspberrypi_ssd_i2c_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) {
  146. return 1;
  147. }
  148. #endif /* defined(U8G_WITH_PINLIST) */
  149. #endif