u8g_com_arduino_sw_spi.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. u8g_arduino_sw_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. Update for ATOMIC operation done (01 Jun 2013)
  27. U8G_ATOMIC_OR(ptr, val)
  28. U8G_ATOMIC_AND(ptr, val)
  29. U8G_ATOMIC_START();
  30. U8G_ATOMIC_END();
  31. */
  32. #include "u8g.h"
  33. #if defined(ARDUINO)
  34. #if ARDUINO < 100
  35. #include <WProgram.h>
  36. #include "wiring_private.h"
  37. #include "pins_arduino.h"
  38. #else
  39. #include <Arduino.h>
  40. #include "wiring_private.h"
  41. #endif
  42. /*=========================================================*/
  43. /* Arduino, AVR */
  44. #if defined(__AVR__)
  45. uint8_t u8g_bitData, u8g_bitNotData;
  46. uint8_t u8g_bitClock, u8g_bitNotClock;
  47. volatile uint8_t *u8g_outData;
  48. volatile uint8_t *u8g_outClock;
  49. static void u8g_com_arduino_init_shift_out(uint8_t dataPin, uint8_t clockPin)
  50. {
  51. u8g_outData = portOutputRegister(digitalPinToPort(dataPin));
  52. u8g_outClock = portOutputRegister(digitalPinToPort(clockPin));
  53. u8g_bitData = digitalPinToBitMask(dataPin);
  54. u8g_bitClock = digitalPinToBitMask(clockPin);
  55. u8g_bitNotClock = u8g_bitClock;
  56. u8g_bitNotClock ^= 0x0ff;
  57. u8g_bitNotData = u8g_bitData;
  58. u8g_bitNotData ^= 0x0ff;
  59. }
  60. static void u8g_com_arduino_do_shift_out_msb_first(uint8_t val) U8G_NOINLINE;
  61. static void u8g_com_arduino_do_shift_out_msb_first(uint8_t val)
  62. {
  63. uint8_t cnt = 8;
  64. uint8_t bitData = u8g_bitData;
  65. uint8_t bitNotData = u8g_bitNotData;
  66. uint8_t bitClock = u8g_bitClock;
  67. uint8_t bitNotClock = u8g_bitNotClock;
  68. volatile uint8_t *outData = u8g_outData;
  69. volatile uint8_t *outClock = u8g_outClock;
  70. U8G_ATOMIC_START();
  71. do
  72. {
  73. if ( val & 128 )
  74. *outData |= bitData;
  75. else
  76. *outData &= bitNotData;
  77. *outClock |= bitClock;
  78. val <<= 1;
  79. cnt--;
  80. *outClock &= bitNotClock;
  81. } while( cnt != 0 );
  82. U8G_ATOMIC_END();
  83. }
  84. /*=========================================================*/
  85. /* Arduino, Chipkit */
  86. #elif defined(__18CXX) || defined(__PIC32MX)
  87. uint16_t dog_bitData, dog_bitNotData;
  88. uint16_t dog_bitClock, dog_bitNotClock;
  89. volatile uint32_t *dog_outData;
  90. volatile uint32_t *dog_outClock;
  91. volatile uint32_t dog_pic32_spi_tmp;
  92. static void u8g_com_arduino_init_shift_out(uint8_t dataPin, uint8_t clockPin)
  93. {
  94. dog_outData = portOutputRegister(digitalPinToPort(dataPin));
  95. dog_outClock = portOutputRegister(digitalPinToPort(clockPin));
  96. dog_bitData = digitalPinToBitMask(dataPin);
  97. dog_bitClock = digitalPinToBitMask(clockPin);
  98. dog_bitNotClock = dog_bitClock;
  99. dog_bitNotClock ^= 0x0ffff;
  100. dog_bitNotData = dog_bitData;
  101. dog_bitNotData ^= 0x0ffff;
  102. }
  103. static void u8g_com_arduino_do_shift_out_msb_first(uint8_t val)
  104. {
  105. uint8_t cnt = 8;
  106. U8G_ATOMIC_START();
  107. do
  108. {
  109. if ( val & 128 )
  110. *dog_outData |= dog_bitData;
  111. else
  112. *dog_outData &= dog_bitNotData;
  113. val <<= 1;
  114. /*
  115. There must be some delay here. However
  116. fetching the adress dog_outClock is enough delay, so
  117. do not place dog_outClock in a local variable. This will
  118. break the procedure
  119. */
  120. *dog_outClock |= dog_bitClock;
  121. cnt--;
  122. *dog_outClock &= dog_bitNotClock;
  123. /*
  124. little additional delay after clk pulse, done by 3x32bit reads
  125. from I/O. Optimized for PIC32 with 80 MHz.
  126. */
  127. dog_pic32_spi_tmp = *dog_outClock;
  128. dog_pic32_spi_tmp = *dog_outClock;
  129. dog_pic32_spi_tmp = *dog_outClock;
  130. } while( cnt != 0 );
  131. U8G_ATOMIC_END();
  132. }
  133. /*=========================================================*/
  134. /* Arduino Due */
  135. #elif defined(__SAM3X8E__)
  136. /* Due */
  137. void u8g_digital_write_sam_high(uint8_t pin)
  138. {
  139. PIO_Set( g_APinDescription[pin].pPort, g_APinDescription[pin].ulPin) ;
  140. }
  141. void u8g_digital_write_sam_low(uint8_t pin)
  142. {
  143. PIO_Clear( g_APinDescription[pin].pPort, g_APinDescription[pin].ulPin) ;
  144. }
  145. static uint8_t u8g_sam_data_pin;
  146. static uint8_t u8g_sam_clock_pin;
  147. static void u8g_com_arduino_init_shift_out(uint8_t dataPin, uint8_t clockPin)
  148. {
  149. u8g_sam_data_pin = dataPin;
  150. u8g_sam_clock_pin = clockPin;
  151. }
  152. static void u8g_com_arduino_do_shift_out_msb_first(uint8_t val)
  153. {
  154. uint8_t i = 8;
  155. do
  156. {
  157. if ( val & 128 )
  158. u8g_digital_write_sam_high(u8g_sam_data_pin);
  159. else
  160. u8g_digital_write_sam_low(u8g_sam_data_pin);
  161. val <<= 1;
  162. //u8g_MicroDelay();
  163. u8g_digital_write_sam_high(u8g_sam_clock_pin);
  164. u8g_MicroDelay();
  165. u8g_digital_write_sam_low(u8g_sam_clock_pin);
  166. u8g_MicroDelay();
  167. i--;
  168. } while( i != 0 );
  169. }
  170. #else
  171. /* empty interface */
  172. static void u8g_com_arduino_init_shift_out(uint8_t dataPin, uint8_t clockPin)
  173. {
  174. }
  175. static void u8g_com_arduino_do_shift_out_msb_first(uint8_t val)
  176. {
  177. }
  178. #endif
  179. uint8_t u8g_com_arduino_sw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
  180. {
  181. switch(msg)
  182. {
  183. case U8G_COM_MSG_INIT:
  184. u8g_com_arduino_assign_pin_output_high(u8g);
  185. u8g_com_arduino_digital_write(u8g, U8G_PI_SCK, LOW);
  186. u8g_com_arduino_digital_write(u8g, U8G_PI_MOSI, LOW);
  187. u8g_com_arduino_init_shift_out(u8g->pin_list[U8G_PI_MOSI], u8g->pin_list[U8G_PI_SCK]);
  188. break;
  189. case U8G_COM_MSG_STOP:
  190. break;
  191. case U8G_COM_MSG_RESET:
  192. if ( u8g->pin_list[U8G_PI_RESET] != U8G_PIN_NONE )
  193. u8g_com_arduino_digital_write(u8g, U8G_PI_RESET, arg_val);
  194. break;
  195. case U8G_COM_MSG_CHIP_SELECT:
  196. if ( arg_val == 0 )
  197. {
  198. /* disable */
  199. u8g_com_arduino_digital_write(u8g, U8G_PI_CS, HIGH);
  200. }
  201. else
  202. {
  203. /* enable */
  204. u8g_com_arduino_digital_write(u8g, U8G_PI_SCK, LOW);
  205. u8g_com_arduino_digital_write(u8g, U8G_PI_CS, LOW);
  206. /* issue 227 */
  207. u8g_com_arduino_init_shift_out(u8g->pin_list[U8G_PI_MOSI], u8g->pin_list[U8G_PI_SCK]);
  208. }
  209. break;
  210. case U8G_COM_MSG_WRITE_BYTE:
  211. u8g_com_arduino_do_shift_out_msb_first( arg_val );
  212. //u8g_arduino_sw_spi_shift_out(u8g->pin_list[U8G_PI_MOSI], u8g->pin_list[U8G_PI_SCK], arg_val);
  213. break;
  214. case U8G_COM_MSG_WRITE_SEQ:
  215. {
  216. register uint8_t *ptr = arg_ptr;
  217. while( arg_val > 0 )
  218. {
  219. u8g_com_arduino_do_shift_out_msb_first(*ptr++);
  220. // u8g_arduino_sw_spi_shift_out(u8g->pin_list[U8G_PI_MOSI], u8g->pin_list[U8G_PI_SCK], *ptr++);
  221. arg_val--;
  222. }
  223. }
  224. break;
  225. case U8G_COM_MSG_WRITE_SEQ_P:
  226. {
  227. register uint8_t *ptr = arg_ptr;
  228. while( arg_val > 0 )
  229. {
  230. u8g_com_arduino_do_shift_out_msb_first( u8g_pgm_read(ptr) );
  231. //u8g_arduino_sw_spi_shift_out(u8g->pin_list[U8G_PI_MOSI], u8g->pin_list[U8G_PI_SCK], u8g_pgm_read(ptr));
  232. ptr++;
  233. arg_val--;
  234. }
  235. }
  236. break;
  237. case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 0) or data mode (arg_val = 1) */
  238. u8g_com_arduino_digital_write(u8g, U8G_PI_A0, arg_val);
  239. break;
  240. }
  241. return 1;
  242. }
  243. #else /* ARDUINO */
  244. uint8_t u8g_com_arduino_sw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
  245. {
  246. return 1;
  247. }
  248. #endif /* ARDUINO */