u8g_com_arduino_st7920_hw_spi.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. u8g_com_arduino_st7920_hw_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 HW SPI interface for ST7920 controller
  27. */
  28. #include "u8g.h"
  29. #if defined(ARDUINO)
  30. #if ARDUINO < 100
  31. #include <WProgram.h>
  32. #include "wiring_private.h"
  33. #include "pins_arduino.h"
  34. #else
  35. #include <Arduino.h>
  36. #include "wiring_private.h"
  37. #endif
  38. #if defined(__AVR__)
  39. #define U8G_ARDUINO_ATMEGA_HW_SPI
  40. /* remove the definition for attiny */
  41. #if __AVR_ARCH__ == 2
  42. #undef U8G_ARDUINO_ATMEGA_HW_SPI
  43. #endif
  44. #if __AVR_ARCH__ == 25
  45. #undef U8G_ARDUINO_ATMEGA_HW_SPI
  46. #endif
  47. #endif
  48. #if defined(U8G_ARDUINO_ATMEGA_HW_SPI)
  49. #include <avr/interrupt.h>
  50. #include <avr/io.h>
  51. #if ARDUINO < 100
  52. /* fixed pins */
  53. #if defined(__AVR_ATmega644P__) || defined(__AVR_ATmega1284P__) // Sanguino.cc board
  54. #define PIN_SCK 7
  55. #define PIN_MISO 6
  56. #define PIN_MOSI 5
  57. #define PIN_CS 4
  58. #else // Arduino Board
  59. #define PIN_SCK 13
  60. #define PIN_MISO 12
  61. #define PIN_MOSI 11
  62. #define PIN_CS 10
  63. #endif // (__AVR_ATmega644P__) || defined(__AVR_ATmega1284P__)
  64. #else
  65. /* use Arduino pin definitions */
  66. #define PIN_SCK SCK
  67. #define PIN_MISO MISO
  68. #define PIN_MOSI MOSI
  69. #define PIN_CS SS
  70. #endif
  71. static uint8_t u8g_arduino_st7920_hw_spi_shift_out(u8g_t *u8g, uint8_t val) U8G_NOINLINE;
  72. static uint8_t u8g_arduino_st7920_hw_spi_shift_out(u8g_t *u8g, uint8_t val)
  73. {
  74. /* send data */
  75. SPDR = val;
  76. /* wait for transmission */
  77. while (!(SPSR & (1<<SPIF)))
  78. ;
  79. /* clear the SPIF flag by reading SPDR */
  80. return SPDR;
  81. }
  82. static void u8g_com_arduino_st7920_write_byte_hw_spi_seq(u8g_t *u8g, uint8_t rs, uint8_t *ptr, uint8_t len)
  83. {
  84. uint8_t i;
  85. if ( rs == 0 )
  86. {
  87. /* command */
  88. u8g_arduino_st7920_hw_spi_shift_out(u8g, 0x0f8);
  89. }
  90. else if ( rs == 1 )
  91. {
  92. /* data */
  93. u8g_arduino_st7920_hw_spi_shift_out(u8g, 0x0fa);
  94. }
  95. while( len > 0 )
  96. {
  97. u8g_arduino_st7920_hw_spi_shift_out(u8g, *ptr & 0x0f0);
  98. u8g_arduino_st7920_hw_spi_shift_out(u8g, *ptr << 4);
  99. ptr++;
  100. len--;
  101. u8g_10MicroDelay();
  102. }
  103. for( i = 0; i < 4; i++ )
  104. u8g_10MicroDelay();
  105. }
  106. static void u8g_com_arduino_st7920_write_byte_hw_spi(u8g_t *u8g, uint8_t rs, uint8_t val) U8G_NOINLINE;
  107. static void u8g_com_arduino_st7920_write_byte_hw_spi(u8g_t *u8g, uint8_t rs, uint8_t val)
  108. {
  109. uint8_t i;
  110. if ( rs == 0 )
  111. {
  112. /* command */
  113. u8g_arduino_st7920_hw_spi_shift_out(u8g, 0x0f8);
  114. }
  115. else if ( rs == 1 )
  116. {
  117. /* data */
  118. u8g_arduino_st7920_hw_spi_shift_out(u8g, 0x0fa);
  119. }
  120. else
  121. {
  122. /* do nothing, keep same state */
  123. }
  124. u8g_arduino_st7920_hw_spi_shift_out(u8g, val & 0x0f0);
  125. u8g_arduino_st7920_hw_spi_shift_out(u8g, val << 4);
  126. for( i = 0; i < 4; i++ )
  127. u8g_10MicroDelay();
  128. }
  129. uint8_t u8g_com_arduino_st7920_hw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
  130. {
  131. switch(msg)
  132. {
  133. case U8G_COM_MSG_INIT:
  134. u8g_com_arduino_assign_pin_output_high(u8g);
  135. /* code from u8g_com-arduino_hw_spi.c */
  136. pinMode(PIN_SCK, OUTPUT);
  137. digitalWrite(PIN_SCK, LOW);
  138. pinMode(PIN_MOSI, OUTPUT);
  139. digitalWrite(PIN_MOSI, LOW);
  140. /* pinMode(PIN_MISO, INPUT); */
  141. pinMode(PIN_CS, OUTPUT); /* system chip select for the atmega board */
  142. digitalWrite(PIN_CS, HIGH);
  143. //u8g_com_arduino_digital_write(u8g, U8G_PI_CS, LOW);
  144. #ifdef OBSOLETE
  145. DDRB |= _BV(3); /* D0, MOSI */
  146. DDRB |= _BV(5); /* SCK */
  147. DDRB |= _BV(2); /* slave select */
  148. PORTB &= ~_BV(3); /* D0, MOSI = 0 */
  149. PORTB &= ~_BV(5); /* SCK = 0 */
  150. #endif
  151. /*
  152. SPR1 SPR0
  153. 0 0 fclk/4
  154. 0 1 fclk/16
  155. 1 0 fclk/64
  156. 1 1 fclk/128
  157. */
  158. SPCR = 0;
  159. /* 20 Dez 2012: set CPOL and CPHA to 1 !!! */
  160. SPCR = (1<<SPE) | (1<<MSTR)|(0<<SPR1)|(0<<SPR0)|(1<<CPOL)|(1<<CPHA);
  161. #ifdef U8G_HW_SPI_2X
  162. SPSR = (1 << SPI2X); /* double speed, issue 89 */
  163. #endif
  164. u8g->pin_list[U8G_PI_A0_STATE] = 0; /* inital RS state: command mode */
  165. break;
  166. case U8G_COM_MSG_STOP:
  167. break;
  168. case U8G_COM_MSG_RESET:
  169. if ( u8g->pin_list[U8G_PI_RESET] != U8G_PIN_NONE )
  170. u8g_com_arduino_digital_write(u8g, U8G_PI_RESET, arg_val);
  171. break;
  172. case U8G_COM_MSG_CHIP_SELECT:
  173. if ( arg_val == 0 )
  174. {
  175. /* disable, note: the st7920 has an active high chip select */
  176. u8g_com_arduino_digital_write(u8g, U8G_PI_CS, LOW);
  177. }
  178. else
  179. {
  180. /* enable */
  181. //u8g_com_arduino_digital_write(u8g, U8G_PI_SCK, LOW);
  182. u8g_com_arduino_digital_write(u8g, U8G_PI_CS, HIGH);
  183. }
  184. break;
  185. case U8G_COM_MSG_WRITE_BYTE:
  186. u8g_com_arduino_st7920_write_byte_hw_spi(u8g, u8g->pin_list[U8G_PI_A0_STATE], arg_val);
  187. // u8g->pin_list[U8G_PI_A0_STATE] = 2;
  188. //u8g_arduino_sw_spi_shift_out(u8g->pin_list[U8G_PI_MOSI], u8g->pin_list[U8G_PI_SCK], arg_val);
  189. break;
  190. case U8G_COM_MSG_WRITE_SEQ:
  191. u8g_com_arduino_st7920_write_byte_hw_spi_seq(u8g, u8g->pin_list[U8G_PI_A0_STATE], (uint8_t *)arg_ptr, arg_val);
  192. /*
  193. {
  194. register uint8_t *ptr = arg_ptr;
  195. while( arg_val > 0 )
  196. {
  197. u8g_com_arduino_st7920_write_byte_hw_spi(u8g, u8g->pin_list[U8G_PI_A0_STATE], *ptr++);
  198. arg_val--;
  199. }
  200. }
  201. */
  202. break;
  203. case U8G_COM_MSG_WRITE_SEQ_P:
  204. {
  205. register uint8_t *ptr = arg_ptr;
  206. while( arg_val > 0 )
  207. {
  208. u8g_com_arduino_st7920_write_byte_hw_spi(u8g, u8g->pin_list[U8G_PI_A0_STATE], u8g_pgm_read(ptr) );
  209. // u8g->pin_list[U8G_PI_A0_STATE] = 2;
  210. ptr++;
  211. arg_val--;
  212. }
  213. }
  214. break;
  215. case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 0) or data mode (arg_val = 1) */
  216. u8g->pin_list[U8G_PI_A0_STATE] = arg_val;
  217. break;
  218. }
  219. return 1;
  220. }
  221. #else
  222. uint8_t u8g_com_arduino_st7920_hw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
  223. {
  224. return 1;
  225. }
  226. #endif
  227. #else /* ARDUINO */
  228. uint8_t u8g_com_arduino_st7920_hw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
  229. {
  230. return 1;
  231. }
  232. #endif /* ARDUINO */