u8g_com_arduino_st7920_custom.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. u8g_com_arduino_st7920_custom.c
  3. Additional COM device, initially introduced for 3D Printer community
  4. Implements a fast SW SPI com subsystem
  5. Universal 8bit Graphics Library
  6. Copyright (c) 2011, olikraus@gmail.com
  7. All rights reserved.
  8. Redistribution and use in source and binary forms, with or without modification,
  9. are permitted provided that the following conditions are met:
  10. * Redistributions of source code must retain the above copyright notice, this list
  11. of conditions and the following disclaimer.
  12. * Redistributions in binary form must reproduce the above copyright notice, this
  13. list of conditions and the following disclaimer in the documentation and/or other
  14. materials provided with the distribution.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  16. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  17. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  20. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  25. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  27. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. A special SPI interface for ST7920 controller
  29. Update for ATOMIC operation done (01 Jun 2013)
  30. U8G_ATOMIC_OR(ptr, val)
  31. U8G_ATOMIC_AND(ptr, val)
  32. U8G_ATOMIC_START();
  33. U8G_ATOMIC_END();
  34. */
  35. #include "u8g.h"
  36. #if defined(ARDUINO)
  37. #if ARDUINO < 100
  38. #include <WProgram.h>
  39. #include "wiring_private.h"
  40. #include "pins_arduino.h"
  41. #else
  42. #include <Arduino.h>
  43. #include "wiring_private.h"
  44. #endif
  45. #if defined(__AVR__)
  46. static uint8_t u8g_bitData, u8g_bitNotData;
  47. static uint8_t u8g_bitClock, u8g_bitNotClock;
  48. static volatile uint8_t *u8g_outData;
  49. static volatile uint8_t *u8g_outClock;
  50. static void u8g_com_arduino_init_shift_out(uint8_t dataPin, uint8_t clockPin)
  51. {
  52. u8g_outData = portOutputRegister(digitalPinToPort(dataPin));
  53. u8g_outClock = portOutputRegister(digitalPinToPort(clockPin));
  54. u8g_bitData = digitalPinToBitMask(dataPin);
  55. u8g_bitClock = digitalPinToBitMask(clockPin);
  56. u8g_bitNotClock = u8g_bitClock;
  57. u8g_bitNotClock ^= 0x0ff;
  58. u8g_bitNotData = u8g_bitData;
  59. u8g_bitNotData ^= 0x0ff;
  60. }
  61. static void u8g_com_arduino_do_shift_out_msb_first(uint8_t val) U8G_NOINLINE;
  62. static void u8g_com_arduino_do_shift_out_msb_first(uint8_t val)
  63. {
  64. uint8_t cnt = 8;
  65. uint8_t bitData = u8g_bitData;
  66. uint8_t bitNotData = u8g_bitNotData;
  67. uint8_t bitClock = u8g_bitClock;
  68. uint8_t bitNotClock = u8g_bitNotClock;
  69. volatile uint8_t *outData = u8g_outData;
  70. volatile uint8_t *outClock = u8g_outClock;
  71. U8G_ATOMIC_START();
  72. bitData |= *outData;
  73. bitNotData &= *outData;
  74. do
  75. {
  76. if ( val & 128 )
  77. *outData = bitData;
  78. else
  79. *outData = bitNotData;
  80. /*
  81. *outClock |= bitClock;
  82. val <<= 1;
  83. cnt--;
  84. *outClock &= bitNotClock;
  85. */
  86. val <<= 1;
  87. *outClock &= bitNotClock;
  88. cnt--;
  89. // removed micro delays, because AVRs are too slow and the delay is not required
  90. //u8g_MicroDelay();
  91. *outClock |= bitClock;
  92. //u8g_MicroDelay();
  93. } while( cnt != 0 );
  94. U8G_ATOMIC_END();
  95. }
  96. #elif defined(__18CXX) || defined(__PIC32MX)
  97. uint16_t dog_bitData, dog_bitNotData;
  98. uint16_t dog_bitClock, dog_bitNotClock;
  99. volatile uint32_t *dog_outData;
  100. volatile uint32_t *dog_outClock;
  101. volatile uint32_t dog_pic32_spi_tmp;
  102. static void u8g_com_arduino_init_shift_out(uint8_t dataPin, uint8_t clockPin)
  103. {
  104. dog_outData = portOutputRegister(digitalPinToPort(dataPin));
  105. dog_outClock = portOutputRegister(digitalPinToPort(clockPin));
  106. dog_bitData = digitalPinToBitMask(dataPin);
  107. dog_bitClock = digitalPinToBitMask(clockPin);
  108. dog_bitNotClock = dog_bitClock;
  109. dog_bitNotClock ^= 0x0ffff;
  110. dog_bitNotData = dog_bitData;
  111. dog_bitNotData ^= 0x0ffff;
  112. }
  113. static void u8g_com_arduino_do_shift_out_msb_first(uint8_t val)
  114. {
  115. uint8_t cnt = 8;
  116. U8G_ATOMIC_START();
  117. do
  118. {
  119. if ( val & 128 )
  120. *dog_outData |= dog_bitData;
  121. else
  122. *dog_outData &= dog_bitNotData;
  123. val <<= 1;
  124. //u8g_MicroDelay();
  125. //*dog_outClock |= dog_bitClock;
  126. *dog_outClock &= dog_bitNotClock;
  127. cnt--;
  128. u8g_MicroDelay();
  129. //*dog_outClock &= dog_bitNotClock;
  130. *dog_outClock |= dog_bitClock;
  131. u8g_MicroDelay();
  132. } while( cnt != 0 );
  133. U8G_ATOMIC_END();
  134. }
  135. #else
  136. /* default interface, Arduino DUE (__arm__) */
  137. uint8_t u8g_data_custom_pin;
  138. uint8_t u8g_clock_custom_pin;
  139. static void u8g_com_arduino_init_shift_out(uint8_t dataPin, uint8_t clockPin)
  140. {
  141. u8g_data_custom_pin = dataPin;
  142. u8g_clock_custom_pin = clockPin;
  143. }
  144. static void u8g_com_arduino_do_shift_out_msb_first(uint8_t val)
  145. {
  146. uint8_t cnt = 8;
  147. do
  148. {
  149. if ( val & 128 )
  150. digitalWrite(u8g_data_custom_pin, HIGH);
  151. else
  152. digitalWrite(u8g_data_custom_pin, LOW);
  153. val <<= 1;
  154. //u8g_MicroDelay();
  155. digitalWrite(u8g_clock_custom_pin, LOW);
  156. cnt--;
  157. u8g_MicroDelay();
  158. digitalWrite(u8g_clock_custom_pin, HIGH);
  159. u8g_MicroDelay();
  160. } while( cnt != 0 );
  161. }
  162. #endif
  163. static void u8g_com_arduino_st7920_write_byte_seq(uint8_t rs, uint8_t *ptr, uint8_t len)
  164. {
  165. uint8_t i;
  166. if ( rs == 0 )
  167. {
  168. /* command */
  169. u8g_com_arduino_do_shift_out_msb_first(0x0f8);
  170. }
  171. else if ( rs == 1 )
  172. {
  173. /* data */
  174. u8g_com_arduino_do_shift_out_msb_first(0x0fa);
  175. }
  176. while( len > 0 )
  177. {
  178. u8g_com_arduino_do_shift_out_msb_first(*ptr & 0x0f0);
  179. u8g_com_arduino_do_shift_out_msb_first(*ptr << 4);
  180. ptr++;
  181. len--;
  182. u8g_10MicroDelay();
  183. }
  184. for( i = 0; i < 4; i++ )
  185. u8g_10MicroDelay();
  186. }
  187. static void u8g_com_arduino_st7920_write_byte(uint8_t rs, uint8_t val)
  188. {
  189. uint8_t i;
  190. if ( rs == 0 )
  191. {
  192. /* command */
  193. u8g_com_arduino_do_shift_out_msb_first(0x0f8);
  194. }
  195. else if ( rs == 1 )
  196. {
  197. /* data */
  198. u8g_com_arduino_do_shift_out_msb_first(0x0fa);
  199. }
  200. u8g_com_arduino_do_shift_out_msb_first(val & 0x0f0);
  201. u8g_com_arduino_do_shift_out_msb_first(val << 4);
  202. for( i = 0; i < 4; i++ )
  203. u8g_10MicroDelay();
  204. }
  205. uint8_t u8g_com_arduino_st7920_custom_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
  206. {
  207. switch(msg)
  208. {
  209. case U8G_COM_MSG_INIT:
  210. u8g_com_arduino_assign_pin_output_high(u8g);
  211. u8g_com_arduino_digital_write(u8g, U8G_PI_CS, LOW);
  212. // u8g_com_arduino_digital_write(u8g, U8G_PI_SCK, LOW);
  213. u8g_com_arduino_digital_write(u8g, U8G_PI_SCK, HIGH);
  214. u8g_com_arduino_digital_write(u8g, U8G_PI_MOSI, LOW);
  215. u8g_com_arduino_init_shift_out(u8g->pin_list[U8G_PI_MOSI], u8g->pin_list[U8G_PI_SCK]);
  216. u8g->pin_list[U8G_PI_A0_STATE] = 0; /* inital RS state: command mode */
  217. break;
  218. case U8G_COM_MSG_STOP:
  219. break;
  220. case U8G_COM_MSG_RESET:
  221. if ( u8g->pin_list[U8G_PI_RESET] != U8G_PIN_NONE )
  222. u8g_com_arduino_digital_write(u8g, U8G_PI_RESET, arg_val);
  223. break;
  224. case U8G_COM_MSG_CHIP_SELECT:
  225. if ( arg_val == 0 )
  226. {
  227. /* disable, note: the st7920 has an active high chip select */
  228. u8g_com_arduino_digital_write(u8g, U8G_PI_CS, LOW);
  229. }
  230. else
  231. {
  232. /* enable */
  233. //u8g_com_arduino_digital_write(u8g, U8G_PI_SCK, HIGH);
  234. u8g_com_arduino_digital_write(u8g, U8G_PI_CS, HIGH);
  235. }
  236. break;
  237. case U8G_COM_MSG_WRITE_BYTE:
  238. u8g_com_arduino_st7920_write_byte( u8g->pin_list[U8G_PI_A0_STATE], arg_val);
  239. //u8g->pin_list[U8G_PI_A0_STATE] = 2;
  240. //u8g_arduino_sw_spi_shift_out(u8g->pin_list[U8G_PI_MOSI], u8g->pin_list[U8G_PI_SCK], arg_val);
  241. break;
  242. case U8G_COM_MSG_WRITE_SEQ:
  243. u8g_com_arduino_st7920_write_byte_seq(u8g->pin_list[U8G_PI_A0_STATE], (uint8_t *)arg_ptr, arg_val);
  244. break;
  245. case U8G_COM_MSG_WRITE_SEQ_P:
  246. {
  247. register uint8_t *ptr = arg_ptr;
  248. while( arg_val > 0 )
  249. {
  250. u8g_com_arduino_st7920_write_byte(u8g->pin_list[U8G_PI_A0_STATE], u8g_pgm_read(ptr) );
  251. //u8g->pin_list[U8G_PI_A0_STATE] = 2;
  252. ptr++;
  253. arg_val--;
  254. }
  255. }
  256. break;
  257. case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 0) or data mode (arg_val = 1) */
  258. u8g->pin_list[U8G_PI_A0_STATE] = arg_val;
  259. break;
  260. }
  261. return 1;
  262. }
  263. #else /* ARDUINO */
  264. uint8_t u8g_com_arduino_st7920_custom_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
  265. {
  266. return 1;
  267. }
  268. #endif /* ARDUINO */