u8g_com_arduino_t6963.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. u8g_com_arduino_t6963.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. PIN_D0 8
  27. PIN_D1 9
  28. PIN_D2 10
  29. PIN_D3 11
  30. PIN_D4 4
  31. PIN_D5 5
  32. PIN_D6 6
  33. PIN_D7 7
  34. PIN_CS 14
  35. PIN_A0 15
  36. PIN_RESET 16
  37. PIN_WR 17
  38. PIN_RD 18
  39. u8g_InitRW8Bit(u8g, dev, d0, d1, d2, d3, d4, d5, d6, d7, cs, a0, wr, rd, reset)
  40. u8g_InitRW8Bit(u8g, dev, 8, 9, 10, 11, 4, 5, 6, 7, 14, 15, 17, 18, 16)
  41. Update for ATOMIC operation done (01 Jun 2013)
  42. U8G_ATOMIC_OR(ptr, val)
  43. U8G_ATOMIC_AND(ptr, val)
  44. U8G_ATOMIC_START();
  45. U8G_ATOMIC_END();
  46. */
  47. #include "u8g.h"
  48. #if defined(ARDUINO)
  49. #if ARDUINO < 100
  50. //#include <WProgram.h>
  51. #include <wiring_private.h>
  52. #include <pins_arduino.h>
  53. #else
  54. #include <Arduino.h>
  55. #endif
  56. #if defined(__PIC32MX)
  57. /* CHIPKIT PIC32 */
  58. static volatile uint32_t *u8g_output_data_port[8];
  59. static volatile uint32_t *u8g_input_data_port[8];
  60. static volatile uint32_t *u8g_mode_port[8];
  61. static uint32_t u8g_data_mask[8];
  62. #else
  63. static volatile uint8_t *u8g_output_data_port[8];
  64. static volatile uint8_t *u8g_input_data_port[8];
  65. static volatile uint8_t *u8g_mode_port[8];
  66. static uint8_t u8g_data_mask[8];
  67. #endif
  68. static void u8g_com_arduino_t6963_init(u8g_t *u8g)
  69. {
  70. u8g_output_data_port[0] = portOutputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D0]));
  71. u8g_input_data_port[0] = portInputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D0]));
  72. u8g_mode_port[0] = portModeRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D0]));
  73. u8g_data_mask[0] = digitalPinToBitMask(u8g->pin_list[U8G_PI_D0]);
  74. u8g_output_data_port[1] = portOutputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D1]));
  75. u8g_input_data_port[1] = portInputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D1]));
  76. u8g_mode_port[1] = portModeRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D1]));
  77. u8g_data_mask[1] = digitalPinToBitMask(u8g->pin_list[U8G_PI_D1]);
  78. u8g_output_data_port[2] = portOutputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D2]));
  79. u8g_input_data_port[2] = portInputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D2]));
  80. u8g_mode_port[2] = portModeRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D2]));
  81. u8g_data_mask[2] = digitalPinToBitMask(u8g->pin_list[U8G_PI_D2]);
  82. u8g_output_data_port[3] = portOutputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D3]));
  83. u8g_input_data_port[3] = portInputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D3]));
  84. u8g_mode_port[3] = portModeRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D3]));
  85. u8g_data_mask[3] = digitalPinToBitMask(u8g->pin_list[U8G_PI_D3]);
  86. u8g_output_data_port[4] = portOutputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D4]));
  87. u8g_input_data_port[4] = portInputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D4]));
  88. u8g_mode_port[4] = portModeRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D4]));
  89. u8g_data_mask[4] = digitalPinToBitMask(u8g->pin_list[U8G_PI_D4]);
  90. u8g_output_data_port[5] = portOutputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D5]));
  91. u8g_input_data_port[5] = portInputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D5]));
  92. u8g_mode_port[5] = portModeRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D5]));
  93. u8g_data_mask[5] = digitalPinToBitMask(u8g->pin_list[U8G_PI_D5]);
  94. u8g_output_data_port[6] = portOutputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D6]));
  95. u8g_input_data_port[6] = portInputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D6]));
  96. u8g_mode_port[6] = portModeRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D6]));
  97. u8g_data_mask[6] = digitalPinToBitMask(u8g->pin_list[U8G_PI_D6]);
  98. u8g_output_data_port[7] = portOutputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D7]));
  99. u8g_input_data_port[7] = portInputRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D7]));
  100. u8g_mode_port[7] = portModeRegister(digitalPinToPort(u8g->pin_list[U8G_PI_D7]));
  101. u8g_data_mask[7] = digitalPinToBitMask(u8g->pin_list[U8G_PI_D7]);
  102. }
  103. static void u8g_com_arduino_t6963_write_data_pin(uint8_t pin, uint8_t val)
  104. {
  105. /* no ATOMIC protection required here, this is done by calling procedure */
  106. if ( val != 0 )
  107. *u8g_output_data_port[pin] |= u8g_data_mask[pin];
  108. else
  109. *u8g_output_data_port[pin] &= ~u8g_data_mask[pin];
  110. }
  111. static void u8g_com_arduino_t6963_set_port_output(void)
  112. {
  113. uint8_t i;
  114. U8G_ATOMIC_START();
  115. for( i = 0; i < 8; i++ )
  116. {
  117. #if defined(__PIC32MX)
  118. /* CHIPKIT PIC32 */
  119. *u8g_mode_port[i] |= u8g_data_mask[i];
  120. #elif defined(__AVR__)
  121. *u8g_mode_port[i] |= u8g_data_mask[i];
  122. #else
  123. /* TODO: use generic Arduino API */
  124. *u8g_mode_port[i] |= u8g_data_mask[i];
  125. #endif
  126. }
  127. U8G_ATOMIC_END();
  128. }
  129. static void u8g_com_arduino_t6963_set_port_input(void)
  130. {
  131. uint8_t i;
  132. U8G_ATOMIC_START();
  133. for( i = 0; i < 8; i++ )
  134. {
  135. #if defined(__PIC32MX)
  136. /* CHIPKIT PIC32 */
  137. *u8g_mode_port[i] &= ~u8g_data_mask[i];
  138. #elif defined(__AVR__)
  139. /* avr */
  140. *u8g_mode_port[i] &= ~u8g_data_mask[i];
  141. *u8g_output_data_port[i] &= ~u8g_data_mask[i]; // no pullup
  142. #else
  143. /* TODO: use generic Arduino API */
  144. *u8g_mode_port[i] &= ~u8g_data_mask[i];
  145. *u8g_output_data_port[i] &= ~u8g_data_mask[i]; // no pullup
  146. #endif
  147. }
  148. U8G_ATOMIC_END();
  149. }
  150. static void u8g_com_arduino_t6963_write(u8g_t *u8g, uint8_t val)
  151. {
  152. U8G_ATOMIC_START();
  153. u8g_com_arduino_t6963_write_data_pin( 0, val&1 );
  154. val >>= 1;
  155. u8g_com_arduino_t6963_write_data_pin( 1, val&1 );
  156. val >>= 1;
  157. u8g_com_arduino_t6963_write_data_pin( 2, val&1 );
  158. val >>= 1;
  159. u8g_com_arduino_t6963_write_data_pin( 3, val&1 );
  160. val >>= 1;
  161. u8g_com_arduino_t6963_write_data_pin( 4, val&1 );
  162. val >>= 1;
  163. u8g_com_arduino_t6963_write_data_pin( 5, val&1 );
  164. val >>= 1;
  165. u8g_com_arduino_t6963_write_data_pin( 6, val&1 );
  166. val >>= 1;
  167. u8g_com_arduino_t6963_write_data_pin( 7, val&1 );
  168. val >>= 1;
  169. U8G_ATOMIC_END();
  170. u8g_com_arduino_digital_write(u8g, U8G_PI_WR, 0);
  171. u8g_MicroDelay(); /* 80ns, reference: t6963 datasheet */
  172. u8g_com_arduino_digital_write(u8g, U8G_PI_WR, 1);
  173. u8g_MicroDelay(); /* 10ns, reference: t6963 datasheet */
  174. }
  175. static uint8_t u8g_com_arduino_t6963_read(u8g_t *u8g)
  176. {
  177. uint8_t val = 0;
  178. u8g_com_arduino_digital_write(u8g, U8G_PI_RD, 0);
  179. u8g_MicroDelay(); /* 150ns, reference: t6963 datasheet */
  180. U8G_ATOMIC_START();
  181. /* only read bits 0, 1 and 3 */
  182. if ( (*u8g_input_data_port[3] & u8g_data_mask[3]) != 0 )
  183. val++;
  184. val <<= 1;
  185. val <<= 1;
  186. if ( (*u8g_input_data_port[1] & u8g_data_mask[1]) != 0 )
  187. val++;
  188. val <<= 1;
  189. if ( (*u8g_input_data_port[0] & u8g_data_mask[0]) != 0 )
  190. val++;
  191. U8G_ATOMIC_END();
  192. u8g_com_arduino_digital_write(u8g, U8G_PI_RD, 1);
  193. u8g_MicroDelay(); /* 10ns, reference: t6963 datasheet */
  194. return val;
  195. }
  196. #define U8G_STATUS_TIMEOUT 50
  197. static uint8_t u8g_com_arduino_t6963_until_01_ok(u8g_t *u8g)
  198. {
  199. long x;
  200. u8g_com_arduino_t6963_set_port_input();
  201. x = millis();
  202. x += U8G_STATUS_TIMEOUT;
  203. for(;;)
  204. {
  205. if ( (u8g_com_arduino_t6963_read(u8g) & 3) == 3 )
  206. break;
  207. if ( x < millis() )
  208. return 0;
  209. }
  210. u8g_com_arduino_t6963_set_port_output();
  211. return 1;
  212. }
  213. static uint8_t u8g_com_arduino_t6963_until_3_ok(u8g_t *u8g)
  214. {
  215. long x;
  216. u8g_com_arduino_t6963_set_port_input();
  217. x = millis();
  218. x += U8G_STATUS_TIMEOUT;
  219. for(;;)
  220. {
  221. if ( (u8g_com_arduino_t6963_read(u8g) & 8) == 8 )
  222. break;
  223. if ( x < millis() )
  224. return 0;
  225. }
  226. u8g_com_arduino_t6963_set_port_output();
  227. return 1;
  228. }
  229. static uint8_t u8g_com_arduino_t6963_write_cmd(u8g_t *u8g, uint8_t val)
  230. {
  231. u8g_com_arduino_digital_write(u8g, U8G_PI_A0, 1);
  232. if ( u8g_com_arduino_t6963_until_01_ok(u8g) == 0 )
  233. return 0;
  234. u8g_com_arduino_digital_write(u8g, U8G_PI_A0, 1);
  235. u8g_com_arduino_t6963_write(u8g, val);
  236. return 1;
  237. }
  238. static uint8_t u8g_com_arduino_t6963_write_data(u8g_t *u8g, uint8_t val)
  239. {
  240. u8g_com_arduino_digital_write(u8g, U8G_PI_A0, 1);
  241. if ( u8g_com_arduino_t6963_until_01_ok(u8g) == 0 )
  242. return 0;
  243. u8g_com_arduino_digital_write(u8g, U8G_PI_A0, 0);
  244. u8g_com_arduino_t6963_write(u8g, val);
  245. return 1;
  246. }
  247. static uint8_t u8g_com_arduino_t6963_write_auto_data(u8g_t *u8g, uint8_t val)
  248. {
  249. u8g_com_arduino_digital_write(u8g, U8G_PI_A0, 1);
  250. if ( u8g_com_arduino_t6963_until_3_ok(u8g) == 0 )
  251. return 0;
  252. u8g_com_arduino_digital_write(u8g, U8G_PI_A0, 0);
  253. u8g_com_arduino_t6963_write(u8g, val);
  254. return 1;
  255. }
  256. uint8_t u8g_com_arduino_t6963_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
  257. {
  258. switch(msg)
  259. {
  260. case U8G_COM_MSG_INIT:
  261. u8g->pin_list[U8G_PI_A0_STATE] = 0;
  262. u8g_com_arduino_t6963_init(u8g);
  263. /* setup the RW (equal to WR) pin as output and force it to high */
  264. if ( u8g->pin_list[U8G_PI_WR] != U8G_PIN_NONE )
  265. {
  266. pinMode(u8g->pin_list[U8G_PI_WR], OUTPUT);
  267. u8g_com_arduino_digital_write(u8g, U8G_PI_WR, HIGH);
  268. }
  269. /* set all pins (except WR pin) */
  270. u8g_com_arduino_assign_pin_output_high(u8g);
  271. break;
  272. case U8G_COM_MSG_STOP:
  273. break;
  274. case U8G_COM_MSG_CHIP_SELECT:
  275. if ( arg_val == 0 )
  276. {
  277. /* disable, active low chip select */
  278. u8g_com_arduino_digital_write(u8g, U8G_PI_CS, HIGH);
  279. }
  280. else
  281. {
  282. /* enable */
  283. u8g_com_arduino_digital_write(u8g, U8G_PI_CS, LOW);
  284. }
  285. break;
  286. case U8G_COM_MSG_WRITE_BYTE:
  287. if ( u8g->pin_list[U8G_PI_A0_STATE] == 0 )
  288. {
  289. u8g_com_arduino_t6963_write_data(u8g, arg_val);
  290. }
  291. else
  292. {
  293. u8g_com_arduino_t6963_write_cmd(u8g, arg_val);
  294. }
  295. break;
  296. case U8G_COM_MSG_WRITE_SEQ:
  297. {
  298. register uint8_t *ptr = arg_ptr;
  299. u8g_com_arduino_t6963_write_cmd(u8g, 0x0b0); /* auto write */
  300. while( arg_val > 0 )
  301. {
  302. if ( u8g_com_arduino_t6963_write_auto_data(u8g, *ptr++) == 0 )
  303. break;
  304. arg_val--;
  305. }
  306. u8g_com_arduino_t6963_write_cmd(u8g, 0x0b2); /* auto reset */
  307. }
  308. break;
  309. case U8G_COM_MSG_WRITE_SEQ_P:
  310. {
  311. register uint8_t *ptr = arg_ptr;
  312. u8g_com_arduino_t6963_write_cmd(u8g, 0x0b0); /* auto write */
  313. while( arg_val > 0 )
  314. {
  315. if ( u8g_com_arduino_t6963_write_auto_data(u8g, u8g_pgm_read(ptr)) == 0 )
  316. break;
  317. ptr++;
  318. arg_val--;
  319. }
  320. u8g_com_arduino_t6963_write_cmd(u8g, 0x0b2); /* auto reset */
  321. }
  322. break;
  323. case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 1) or data mode (arg_val = 0) */
  324. u8g->pin_list[U8G_PI_A0_STATE] = arg_val;
  325. //u8g_com_arduino_digital_write(u8g, U8G_PI_DI, arg_val);
  326. break;
  327. case U8G_COM_MSG_RESET:
  328. if ( u8g->pin_list[U8G_PI_RESET] != U8G_PIN_NONE )
  329. u8g_com_arduino_digital_write(u8g, U8G_PI_RESET, arg_val);
  330. break;
  331. }
  332. return 1;
  333. }
  334. #else
  335. uint8_t u8g_com_arduino_t6963_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
  336. {
  337. return 1;
  338. }
  339. #endif /* ARDUINO */