u8g_com_atmega_parallel.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. u8g_com_atmega_parallel.c
  3. Universal 8bit Graphics Library
  4. Copyright (c) 2012, olikraus@gmail.com
  5. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without modification,
  7. are permitted provided that the following conditions are met:
  8. * Redistributions of source code must retain the above copyright notice, this list
  9. of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright notice, this
  11. list of conditions and the following disclaimer in the documentation and/or other
  12. materials provided with the distribution.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  14. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  15. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  16. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  18. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  23. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. PIN_D0 8
  27. PIN_D1 9
  28. PIN_D2 10
  29. PIN_D3 11
  30. PIN_D4 4
  31. PIN_D5 5
  32. PIN_D6 6
  33. PIN_D7 7
  34. PIN_CS1 14
  35. PIN_CS2 15
  36. PIN_RW 16
  37. PIN_DI 17
  38. PIN_EN 18
  39. u8g_Init8Bit(u8g, dev, d0, d1, d2, d3, d4, d5, d6, d7, en, cs1, cs2, di, rw, reset)
  40. u8g_Init8Bit(u8g, dev, 8, 9, 10, 11, 4, 5, 6, 7, 18, 14, 15, 17, 16, U8G_PIN_NONE)
  41. */
  42. #include "u8g.h"
  43. #if defined(__AVR__)
  44. static void u8g_com_atmega_parallel_write(u8g_t *u8g, uint8_t val) U8G_NOINLINE;
  45. static void u8g_com_atmega_parallel_write(u8g_t *u8g, uint8_t val)
  46. {
  47. u8g_SetPILevel(u8g, U8G_PI_D0, val&1);
  48. val >>= 1;
  49. u8g_SetPILevel(u8g, U8G_PI_D1, val&1);
  50. val >>= 1;
  51. u8g_SetPILevel(u8g, U8G_PI_D2, val&1);
  52. val >>= 1;
  53. u8g_SetPILevel(u8g, U8G_PI_D3, val&1);
  54. val >>= 1;
  55. u8g_SetPILevel(u8g, U8G_PI_D4, val&1);
  56. val >>= 1;
  57. u8g_SetPILevel(u8g, U8G_PI_D5, val&1);
  58. val >>= 1;
  59. u8g_SetPILevel(u8g, U8G_PI_D6, val&1);
  60. val >>= 1;
  61. u8g_SetPILevel(u8g, U8G_PI_D7, val&1);
  62. /* EN cycle time must be 1 micro second */
  63. u8g_SetPILevel(u8g, U8G_PI_EN, 1);
  64. u8g_MicroDelay(); /* delay by 1000ns, reference: ST7920: 140ns, SBN1661: 100ns */
  65. u8g_SetPILevel(u8g, U8G_PI_EN, 0);
  66. u8g_10MicroDelay(); /* ST7920 commands: 72us */
  67. u8g_10MicroDelay(); /* ST7920 commands: 72us */
  68. }
  69. uint8_t u8g_com_atmega_parallel_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
  70. {
  71. switch(msg)
  72. {
  73. case U8G_COM_MSG_INIT:
  74. /* setup the RW pin as output and force it to low */
  75. u8g_SetPIOutput(u8g, U8G_PI_RW);
  76. u8g_SetPILevel(u8g, U8G_PI_RW, 0);
  77. u8g_SetPIOutput(u8g, U8G_PI_D0);
  78. u8g_SetPIOutput(u8g, U8G_PI_D1);
  79. u8g_SetPIOutput(u8g, U8G_PI_D2);
  80. u8g_SetPIOutput(u8g, U8G_PI_D3);
  81. u8g_SetPIOutput(u8g, U8G_PI_D4);
  82. u8g_SetPIOutput(u8g, U8G_PI_D5);
  83. u8g_SetPIOutput(u8g, U8G_PI_D6);
  84. u8g_SetPIOutput(u8g, U8G_PI_D7);
  85. u8g_SetPIOutput(u8g, U8G_PI_EN);
  86. u8g_SetPIOutput(u8g, U8G_PI_CS1);
  87. u8g_SetPIOutput(u8g, U8G_PI_CS2);
  88. u8g_SetPIOutput(u8g, U8G_PI_DI);
  89. u8g_SetPILevel(u8g, U8G_PI_CS1, 1);
  90. u8g_SetPILevel(u8g, U8G_PI_CS2, 1);
  91. break;
  92. case U8G_COM_MSG_STOP:
  93. break;
  94. case U8G_COM_MSG_CHIP_SELECT:
  95. if ( arg_val == 0 )
  96. {
  97. /* disable */
  98. u8g_SetPILevel(u8g, U8G_PI_CS1, 1);
  99. u8g_SetPILevel(u8g, U8G_PI_CS2, 1);
  100. }
  101. else if ( arg_val == 1 )
  102. {
  103. /* enable */
  104. u8g_SetPILevel(u8g, U8G_PI_CS1, 0);
  105. u8g_SetPILevel(u8g, U8G_PI_CS2, 1);
  106. }
  107. else if ( arg_val == 2 )
  108. {
  109. /* enable */
  110. u8g_SetPILevel(u8g, U8G_PI_CS1, 1);
  111. u8g_SetPILevel(u8g, U8G_PI_CS2, 0);
  112. }
  113. else
  114. {
  115. /* enable */
  116. u8g_SetPILevel(u8g, U8G_PI_CS1, 0);
  117. u8g_SetPILevel(u8g, U8G_PI_CS2, 0);
  118. }
  119. break;
  120. case U8G_COM_MSG_WRITE_BYTE:
  121. u8g_com_atmega_parallel_write(u8g, arg_val);
  122. break;
  123. case U8G_COM_MSG_WRITE_SEQ:
  124. {
  125. register uint8_t *ptr = arg_ptr;
  126. while( arg_val > 0 )
  127. {
  128. u8g_com_atmega_parallel_write(u8g, *ptr++);
  129. arg_val--;
  130. }
  131. }
  132. break;
  133. case U8G_COM_MSG_WRITE_SEQ_P:
  134. {
  135. register uint8_t *ptr = arg_ptr;
  136. while( arg_val > 0 )
  137. {
  138. u8g_com_atmega_parallel_write(u8g, u8g_pgm_read(ptr));
  139. ptr++;
  140. arg_val--;
  141. }
  142. }
  143. break;
  144. case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 0) or data mode (arg_val = 1) */
  145. u8g_SetPILevel(u8g, U8G_PI_DI, arg_val);
  146. break;
  147. case U8G_COM_MSG_RESET:
  148. u8g_SetPILevel(u8g, U8G_PI_RESET, arg_val);
  149. break;
  150. }
  151. return 1;
  152. }
  153. #else
  154. uint8_t u8g_com_atmega_parallel_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
  155. {
  156. return 1;
  157. }
  158. #endif /* ARDUINO */