u8g_com_atmega_st7920_spi.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. u8g_com_atmega_st7920_spi.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. A special SPI interface for ST7920 controller
  27. */
  28. #include "u8g.h"
  29. #if defined(__AVR__)
  30. static void u8g_atmega_st7920_sw_spi_shift_out(u8g_t *u8g, uint8_t val) U8G_NOINLINE;
  31. static void u8g_atmega_st7920_sw_spi_shift_out(u8g_t *u8g, uint8_t val)
  32. {
  33. uint8_t i = 8;
  34. do
  35. {
  36. u8g_SetPILevel(u8g, U8G_PI_MOSI, val & 128 );
  37. val <<= 1;
  38. u8g_SetPILevel(u8g, U8G_PI_SCK, 1 );
  39. u8g_MicroDelay(); /* 15 Aug 2012: added for high speed uC */
  40. u8g_SetPILevel(u8g, U8G_PI_SCK, 0 );
  41. u8g_MicroDelay(); /* 15 Aug 2012: added for high speed uC */
  42. i--;
  43. } while( i != 0 );
  44. }
  45. static void u8g_com_atmega_st7920_write_byte(u8g_t *u8g, uint8_t rs, uint8_t val) U8G_NOINLINE;
  46. static void u8g_com_atmega_st7920_write_byte(u8g_t *u8g, uint8_t rs, uint8_t val)
  47. {
  48. uint8_t i;
  49. if ( rs == 0 )
  50. {
  51. /* command */
  52. u8g_atmega_st7920_sw_spi_shift_out(u8g, 0x0f8);
  53. }
  54. else if ( rs == 1 )
  55. {
  56. /* data */
  57. u8g_atmega_st7920_sw_spi_shift_out(u8g, 0x0fa);
  58. }
  59. u8g_atmega_st7920_sw_spi_shift_out(u8g, val & 0x0f0);
  60. u8g_atmega_st7920_sw_spi_shift_out(u8g, val << 4);
  61. for( i = 0; i < 4; i++ )
  62. u8g_10MicroDelay();
  63. }
  64. uint8_t u8g_com_atmega_st7920_sw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
  65. {
  66. switch(msg)
  67. {
  68. case U8G_COM_MSG_INIT:
  69. u8g_SetPIOutput(u8g, U8G_PI_SCK);
  70. u8g_SetPIOutput(u8g, U8G_PI_MOSI);
  71. /* u8g_SetPIOutput(u8g, U8G_PI_A0); */
  72. u8g_SetPIOutput(u8g, U8G_PI_CS);
  73. u8g_SetPIOutput(u8g, U8G_PI_RESET);
  74. u8g_SetPILevel(u8g, U8G_PI_SCK, 0 );
  75. u8g_SetPILevel(u8g, U8G_PI_MOSI, 0 );
  76. u8g_SetPILevel(u8g, U8G_PI_CS, 0 );
  77. /* u8g_SetPILevel(u8g, U8G_PI_A0, 0); */
  78. u8g->pin_list[U8G_PI_A0_STATE] = 0; /* inital RS state: command mode */
  79. break;
  80. case U8G_COM_MSG_STOP:
  81. break;
  82. case U8G_COM_MSG_RESET:
  83. u8g_SetPILevel(u8g, U8G_PI_RESET, arg_val);
  84. break;
  85. case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 0) or data mode (arg_val = 1) */
  86. u8g->pin_list[U8G_PI_A0_STATE] = arg_val;
  87. break;
  88. case U8G_COM_MSG_CHIP_SELECT:
  89. if ( arg_val == 0 )
  90. {
  91. /* disable, note: the st7920 has an active high chip select */
  92. u8g_SetPILevel(u8g, U8G_PI_CS, 0);
  93. }
  94. else
  95. {
  96. /* u8g_SetPILevel(u8g, U8G_PI_SCK, 0 ); */
  97. /* enable */
  98. u8g_SetPILevel(u8g, U8G_PI_CS, 1); /* CS = 1 (high active) */
  99. }
  100. break;
  101. case U8G_COM_MSG_WRITE_BYTE:
  102. u8g_com_atmega_st7920_write_byte(u8g, u8g->pin_list[U8G_PI_A0_STATE], arg_val);
  103. u8g->pin_list[U8G_PI_A0_STATE] = 2;
  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_atmega_st7920_write_byte(u8g, u8g->pin_list[U8G_PI_A0_STATE], *ptr++);
  111. u8g->pin_list[U8G_PI_A0_STATE] = 2;
  112. arg_val--;
  113. }
  114. }
  115. break;
  116. case U8G_COM_MSG_WRITE_SEQ_P:
  117. {
  118. register uint8_t *ptr = arg_ptr;
  119. while( arg_val > 0 )
  120. {
  121. u8g_com_atmega_st7920_write_byte(u8g, u8g->pin_list[U8G_PI_A0_STATE], u8g_pgm_read(ptr));
  122. u8g->pin_list[U8G_PI_A0_STATE] = 2;
  123. ptr++;
  124. arg_val--;
  125. }
  126. }
  127. break;
  128. }
  129. return 1;
  130. }
  131. #else
  132. uint8_t u8g_com_atmega_st7920_sw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
  133. {
  134. return 1;
  135. }
  136. #endif