u8g_com_arduino_port_d_wr.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. u8g_arduino_port_d_wr.c
  3. Universal 8bit Graphics Library
  4. Copyright (c) 2011, 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. Assumes PORTD for 8 bit data transfer.
  27. EN is assumed to be a low active write signal (WR)
  28. ILI9325D_320x240 from iteadstudio.com
  29. RS=19, WR=18, CS=17, RST=16
  30. u8g_Init8Bit(u8g, dev, d0, d1, d2, d3, d4, d5, d6, d7, en, cs1, cs2, di, rw, reset)
  31. u8g_Init8Bit(u8g, dev, 8, 9, 10, 11, 4, 5, 6, 7, 18, 14, 15, 17, 16, U8G_PIN_NONE)
  32. Update for ATOMIC operation done (01 Jun 2013)
  33. U8G_ATOMIC_OR(ptr, val)
  34. U8G_ATOMIC_AND(ptr, val)
  35. U8G_ATOMIC_START();
  36. U8G_ATOMIC_END();
  37. */
  38. #include "u8g.h"
  39. #if defined(ARDUINO) && defined(PORTD)
  40. #if ARDUINO < 100
  41. #include <WProgram.h>
  42. #else
  43. #include <Arduino.h>
  44. #endif
  45. static void u8g_com_arduino_port_d_8bit_wr(u8g_t *u8g, uint8_t val)
  46. {
  47. PORTD = val;
  48. /* WR cycle time must be 1 micro second, digitalWrite is slow enough to do this */
  49. u8g_com_arduino_digital_write(u8g, U8G_PI_EN, LOW);
  50. u8g_com_arduino_digital_write(u8g, U8G_PI_EN, HIGH);
  51. }
  52. uint8_t u8g_com_arduino_port_d_wr_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
  53. {
  54. switch(msg)
  55. {
  56. case U8G_COM_MSG_INIT:
  57. #ifdef UCSR0B
  58. UCSR0B = 0; // disable USART 0
  59. #endif
  60. U8G_ATOMIC_START();
  61. DDRD = 0x0ff;
  62. PORTD = 0x0ff;
  63. U8G_ATOMIC_END();
  64. /* setup the RW pin as output and force it to low */
  65. if ( u8g->pin_list[U8G_PI_RW] != U8G_PIN_NONE )
  66. {
  67. pinMode(u8g->pin_list[U8G_PI_RW], OUTPUT);
  68. u8g_com_arduino_digital_write(u8g, U8G_PI_RW, HIGH);
  69. }
  70. /* set all pins (except RW pin) */
  71. u8g_com_arduino_assign_pin_output_high(u8g);
  72. u8g_com_arduino_digital_write(u8g, U8G_PI_EN, HIGH);
  73. break;
  74. case U8G_COM_MSG_STOP:
  75. break;
  76. case U8G_COM_MSG_CHIP_SELECT:
  77. if ( arg_val == 0 )
  78. {
  79. /* disable */
  80. u8g_com_arduino_digital_write(u8g, U8G_PI_CS1, HIGH);
  81. u8g_com_arduino_digital_write(u8g, U8G_PI_CS2, HIGH);
  82. }
  83. else if ( arg_val == 1 )
  84. {
  85. /* enable */
  86. u8g_com_arduino_digital_write(u8g, U8G_PI_CS1, LOW);
  87. u8g_com_arduino_digital_write(u8g, U8G_PI_CS2, HIGH);
  88. }
  89. else if ( arg_val == 2 )
  90. {
  91. /* enable */
  92. u8g_com_arduino_digital_write(u8g, U8G_PI_CS1, HIGH);
  93. u8g_com_arduino_digital_write(u8g, U8G_PI_CS2, LOW);
  94. }
  95. else
  96. {
  97. /* enable */
  98. u8g_com_arduino_digital_write(u8g, U8G_PI_CS1, LOW);
  99. u8g_com_arduino_digital_write(u8g, U8G_PI_CS2, LOW);
  100. }
  101. break;
  102. case U8G_COM_MSG_WRITE_BYTE:
  103. u8g_com_arduino_port_d_8bit_wr(u8g, arg_val);
  104. break;
  105. case U8G_COM_MSG_WRITE_SEQ:
  106. {
  107. register uint8_t *ptr = arg_ptr;
  108. while( arg_val > 0 )
  109. {
  110. u8g_com_arduino_port_d_8bit_wr(u8g, *ptr++);
  111. arg_val--;
  112. }
  113. }
  114. break;
  115. case U8G_COM_MSG_WRITE_SEQ_P:
  116. {
  117. register uint8_t *ptr = arg_ptr;
  118. while( arg_val > 0 )
  119. {
  120. u8g_com_arduino_port_d_8bit_wr(u8g, u8g_pgm_read(ptr));
  121. ptr++;
  122. arg_val--;
  123. }
  124. }
  125. break;
  126. case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 0) or data mode (arg_val = 1) */
  127. u8g_com_arduino_digital_write(u8g, U8G_PI_DI, arg_val);
  128. break;
  129. case U8G_COM_MSG_RESET:
  130. if ( u8g->pin_list[U8G_PI_RESET] != U8G_PIN_NONE )
  131. u8g_com_arduino_digital_write(u8g, U8G_PI_RESET, arg_val);
  132. break;
  133. }
  134. return 1;
  135. }
  136. #else
  137. uint8_t u8g_com_arduino_port_d_wr_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
  138. {
  139. return 1;
  140. }
  141. #endif /* ARDUINO && PORTD */