u8g_com_arduino_st7920_spi.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. u8g_com_arduino_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. Update for ATOMIC operation done (01 Jun 2013)
  28. U8G_ATOMIC_OR(ptr, val)
  29. U8G_ATOMIC_AND(ptr, val)
  30. U8G_ATOMIC_START();
  31. U8G_ATOMIC_END();
  32. */
  33. #include "u8g.h"
  34. #if defined(ARDUINO)
  35. #if ARDUINO < 100
  36. #include <WProgram.h>
  37. #include "wiring_private.h"
  38. #include "pins_arduino.h"
  39. #else
  40. #include <Arduino.h>
  41. #include "wiring_private.h"
  42. #endif
  43. #if defined(__AVR__)
  44. static uint8_t u8g_bitData, u8g_bitNotData;
  45. static uint8_t u8g_bitClock, u8g_bitNotClock;
  46. static volatile uint8_t *u8g_outData;
  47. static volatile uint8_t *u8g_outClock;
  48. static void u8g_com_arduino_init_shift_out(uint8_t dataPin, uint8_t clockPin)
  49. {
  50. u8g_outData = portOutputRegister(digitalPinToPort(dataPin));
  51. u8g_outClock = portOutputRegister(digitalPinToPort(clockPin));
  52. u8g_bitData = digitalPinToBitMask(dataPin);
  53. u8g_bitClock = digitalPinToBitMask(clockPin);
  54. u8g_bitNotClock = u8g_bitClock;
  55. u8g_bitNotClock ^= 0x0ff;
  56. u8g_bitNotData = u8g_bitData;
  57. u8g_bitNotData ^= 0x0ff;
  58. }
  59. static void u8g_com_arduino_do_shift_out_msb_first(uint8_t val) U8G_NOINLINE;
  60. static void u8g_com_arduino_do_shift_out_msb_first(uint8_t val)
  61. {
  62. uint8_t cnt = 8;
  63. uint8_t bitData = u8g_bitData;
  64. uint8_t bitNotData = u8g_bitNotData;
  65. uint8_t bitClock = u8g_bitClock;
  66. uint8_t bitNotClock = u8g_bitNotClock;
  67. volatile uint8_t *outData = u8g_outData;
  68. volatile uint8_t *outClock = u8g_outClock;
  69. U8G_ATOMIC_START();
  70. bitData |= *outData;
  71. bitNotData &= *outData;
  72. do
  73. {
  74. if ( val & 128 )
  75. *outData = bitData;
  76. else
  77. *outData = bitNotData;
  78. /*
  79. *outClock |= bitClock;
  80. val <<= 1;
  81. cnt--;
  82. *outClock &= bitNotClock;
  83. */
  84. val <<= 1;
  85. *outClock &= bitNotClock;
  86. cnt--;
  87. // removed micro delays, because AVRs are too slow and the delay is not required
  88. //u8g_MicroDelay();
  89. *outClock |= bitClock;
  90. //u8g_MicroDelay();
  91. } while( cnt != 0 );
  92. U8G_ATOMIC_END();
  93. }
  94. #elif defined(__18CXX) || defined(__PIC32MX)
  95. uint16_t dog_bitData, dog_bitNotData;
  96. uint16_t dog_bitClock, dog_bitNotClock;
  97. volatile uint32_t *dog_outData;
  98. volatile uint32_t *dog_outClock;
  99. volatile uint32_t dog_pic32_spi_tmp;
  100. static void u8g_com_arduino_init_shift_out(uint8_t dataPin, uint8_t clockPin)
  101. {
  102. dog_outData = portOutputRegister(digitalPinToPort(dataPin));
  103. dog_outClock = portOutputRegister(digitalPinToPort(clockPin));
  104. dog_bitData = digitalPinToBitMask(dataPin);
  105. dog_bitClock = digitalPinToBitMask(clockPin);
  106. dog_bitNotClock = dog_bitClock;
  107. dog_bitNotClock ^= 0x0ffff;
  108. dog_bitNotData = dog_bitData;
  109. dog_bitNotData ^= 0x0ffff;
  110. }
  111. static void u8g_com_arduino_do_shift_out_msb_first(uint8_t val)
  112. {
  113. uint8_t cnt = 8;
  114. U8G_ATOMIC_START();
  115. do
  116. {
  117. if ( val & 128 )
  118. *dog_outData |= dog_bitData;
  119. else
  120. *dog_outData &= dog_bitNotData;
  121. val <<= 1;
  122. //u8g_MicroDelay();
  123. //*dog_outClock |= dog_bitClock;
  124. *dog_outClock &= dog_bitNotClock;
  125. cnt--;
  126. u8g_MicroDelay();
  127. //*dog_outClock &= dog_bitNotClock;
  128. *dog_outClock |= dog_bitClock;
  129. u8g_MicroDelay();
  130. } while( cnt != 0 );
  131. U8G_ATOMIC_END();
  132. }
  133. #else
  134. /* default interface, Arduino DUE (__arm__) */
  135. uint8_t u8g_data_pin;
  136. uint8_t u8g_clock_pin;
  137. static void u8g_com_arduino_init_shift_out(uint8_t dataPin, uint8_t clockPin)
  138. {
  139. u8g_data_pin = dataPin;
  140. u8g_clock_pin = clockPin;
  141. }
  142. static void u8g_com_arduino_do_shift_out_msb_first(uint8_t val)
  143. {
  144. uint8_t cnt = 8;
  145. do
  146. {
  147. if ( val & 128 )
  148. digitalWrite(u8g_data_pin, HIGH);
  149. else
  150. digitalWrite(u8g_data_pin, LOW);
  151. val <<= 1;
  152. //u8g_MicroDelay();
  153. digitalWrite(u8g_clock_pin, LOW);
  154. cnt--;
  155. u8g_MicroDelay();
  156. digitalWrite(u8g_clock_pin, HIGH);
  157. u8g_MicroDelay();
  158. } while( cnt != 0 );
  159. }
  160. #endif
  161. static void u8g_com_arduino_st7920_write_byte_seq(uint8_t rs, uint8_t *ptr, uint8_t len)
  162. {
  163. uint8_t i;
  164. if ( rs == 0 )
  165. {
  166. /* command */
  167. u8g_com_arduino_do_shift_out_msb_first(0x0f8);
  168. }
  169. else if ( rs == 1 )
  170. {
  171. /* data */
  172. u8g_com_arduino_do_shift_out_msb_first(0x0fa);
  173. }
  174. while( len > 0 )
  175. {
  176. u8g_com_arduino_do_shift_out_msb_first(*ptr & 0x0f0);
  177. u8g_com_arduino_do_shift_out_msb_first(*ptr << 4);
  178. ptr++;
  179. len--;
  180. u8g_10MicroDelay();
  181. }
  182. for( i = 0; i < 4; i++ )
  183. u8g_10MicroDelay();
  184. }
  185. static void u8g_com_arduino_st7920_write_byte(uint8_t rs, uint8_t val)
  186. {
  187. uint8_t i;
  188. if ( rs == 0 )
  189. {
  190. /* command */
  191. u8g_com_arduino_do_shift_out_msb_first(0x0f8);
  192. }
  193. else if ( rs == 1 )
  194. {
  195. /* data */
  196. u8g_com_arduino_do_shift_out_msb_first(0x0fa);
  197. }
  198. u8g_com_arduino_do_shift_out_msb_first(val & 0x0f0);
  199. u8g_com_arduino_do_shift_out_msb_first(val << 4);
  200. for( i = 0; i < 4; i++ )
  201. u8g_10MicroDelay();
  202. }
  203. uint8_t u8g_com_arduino_st7920_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
  204. {
  205. switch(msg)
  206. {
  207. case U8G_COM_MSG_INIT:
  208. u8g_com_arduino_assign_pin_output_high(u8g);
  209. u8g_com_arduino_digital_write(u8g, U8G_PI_CS, LOW);
  210. // u8g_com_arduino_digital_write(u8g, U8G_PI_SCK, LOW);
  211. u8g_com_arduino_digital_write(u8g, U8G_PI_SCK, HIGH);
  212. u8g_com_arduino_digital_write(u8g, U8G_PI_MOSI, LOW);
  213. u8g_com_arduino_init_shift_out(u8g->pin_list[U8G_PI_MOSI], u8g->pin_list[U8G_PI_SCK]);
  214. u8g->pin_list[U8G_PI_A0_STATE] = 0; /* inital RS state: command mode */
  215. break;
  216. case U8G_COM_MSG_STOP:
  217. break;
  218. case U8G_COM_MSG_RESET:
  219. if ( u8g->pin_list[U8G_PI_RESET] != U8G_PIN_NONE )
  220. u8g_com_arduino_digital_write(u8g, U8G_PI_RESET, arg_val);
  221. break;
  222. case U8G_COM_MSG_CHIP_SELECT:
  223. if ( arg_val == 0 )
  224. {
  225. /* disable, note: the st7920 has an active high chip select */
  226. u8g_com_arduino_digital_write(u8g, U8G_PI_CS, LOW);
  227. }
  228. else
  229. {
  230. /* enable */
  231. //u8g_com_arduino_digital_write(u8g, U8G_PI_SCK, HIGH);
  232. u8g_com_arduino_digital_write(u8g, U8G_PI_CS, HIGH);
  233. /* 28 Dec 2013 reassign pins, fixes issue with more than one display */
  234. /* issue 227 */
  235. u8g_com_arduino_init_shift_out(u8g->pin_list[U8G_PI_MOSI], u8g->pin_list[U8G_PI_SCK]);
  236. }
  237. break;
  238. case U8G_COM_MSG_WRITE_BYTE:
  239. u8g_com_arduino_st7920_write_byte( u8g->pin_list[U8G_PI_A0_STATE], arg_val);
  240. //u8g->pin_list[U8G_PI_A0_STATE] = 2;
  241. //u8g_arduino_sw_spi_shift_out(u8g->pin_list[U8G_PI_MOSI], u8g->pin_list[U8G_PI_SCK], arg_val);
  242. break;
  243. case U8G_COM_MSG_WRITE_SEQ:
  244. u8g_com_arduino_st7920_write_byte_seq(u8g->pin_list[U8G_PI_A0_STATE], (uint8_t *)arg_ptr, arg_val);
  245. break;
  246. case U8G_COM_MSG_WRITE_SEQ_P:
  247. {
  248. register uint8_t *ptr = arg_ptr;
  249. while( arg_val > 0 )
  250. {
  251. u8g_com_arduino_st7920_write_byte(u8g->pin_list[U8G_PI_A0_STATE], u8g_pgm_read(ptr) );
  252. //u8g->pin_list[U8G_PI_A0_STATE] = 2;
  253. ptr++;
  254. arg_val--;
  255. }
  256. }
  257. break;
  258. case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 0) or data mode (arg_val = 1) */
  259. u8g->pin_list[U8G_PI_A0_STATE] = arg_val;
  260. break;
  261. }
  262. return 1;
  263. }
  264. #else /* ARDUINO */
  265. uint8_t u8g_com_arduino_st7920_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
  266. {
  267. return 1;
  268. }
  269. #endif /* ARDUINO */