u8g_com_io.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. u8g_com_io.c
  3. abstraction layer for low level i/o
  4. Universal 8bit Graphics Library
  5. Copyright (c) 2012, olikraus@gmail.com
  6. All rights reserved.
  7. Redistribution and use in source and binary forms, with or without modification,
  8. are permitted provided that the following conditions are met:
  9. * Redistributions of source code must retain the above copyright notice, this list
  10. of conditions and the following disclaimer.
  11. * Redistributions in binary form must reproduce the above copyright notice, this
  12. list of conditions and the following disclaimer in the documentation and/or other
  13. materials provided with the distribution.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  15. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  16. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  17. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  19. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  24. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  26. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  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. uint8_t u8g_Pin(uint8_t port, uint8_t bitpos) Convert to internal number: AVR: port*8+bitpos, ARM: port*16+bitpos
  33. void u8g_SetPinOutput(uint8_t internal_pin_number)
  34. void u8g_SetPinInput(uint8_t internal_pin_number)
  35. void u8g_SetPinLevel(uint8_t internal_pin_number, uint8_t level)
  36. uint8_t u8g_GetPinLevel(uint8_t internal_pin_number)
  37. */
  38. #include "u8g.h"
  39. #if defined(__AVR__)
  40. #include <avr/interrupt.h>
  41. #include <avr/io.h>
  42. typedef volatile uint8_t * IO_PTR;
  43. /* create internal pin number */
  44. uint8_t u8g_Pin(uint8_t port, uint8_t bitpos)
  45. {
  46. port <<= 3;
  47. port += bitpos;
  48. return port;
  49. }
  50. const IO_PTR u8g_avr_ddr_P[] PROGMEM = {
  51. #ifdef DDRA
  52. &DDRA,
  53. #else
  54. 0,
  55. #endif
  56. &DDRB,
  57. #ifdef DDRC
  58. &DDRC,
  59. #ifdef DDRD
  60. &DDRD,
  61. #ifdef DDRE
  62. &DDRE,
  63. #ifdef DDRF
  64. &DDRF,
  65. #ifdef DDRG
  66. &DDRG,
  67. #ifdef DDRH
  68. &DDRH,
  69. #endif
  70. #endif
  71. #endif
  72. #endif
  73. #endif
  74. #endif
  75. };
  76. const IO_PTR u8g_avr_port_P[] PROGMEM = {
  77. #ifdef PORTA
  78. &PORTA,
  79. #else
  80. 0,
  81. #endif
  82. &PORTB,
  83. #ifdef PORTC
  84. &PORTC,
  85. #ifdef PORTD
  86. &PORTD,
  87. #ifdef PORTE
  88. &PORTE,
  89. #ifdef PORTF
  90. &PORTF,
  91. #ifdef PORTG
  92. &PORTG,
  93. #ifdef PORTH
  94. &PORTH,
  95. #endif
  96. #endif
  97. #endif
  98. #endif
  99. #endif
  100. #endif
  101. };
  102. const IO_PTR u8g_avr_pin_P[] PROGMEM = {
  103. #ifdef PINA
  104. &PINA,
  105. #else
  106. 0,
  107. #endif
  108. &PINB,
  109. #ifdef PINC
  110. &PINC,
  111. #ifdef PIND
  112. &PIND,
  113. #ifdef PINE
  114. &PINE,
  115. #ifdef PINF
  116. &PINF,
  117. #ifdef PING
  118. &PING,
  119. #ifdef PINH
  120. &PINH,
  121. #endif
  122. #endif
  123. #endif
  124. #endif
  125. #endif
  126. #endif
  127. };
  128. static volatile uint8_t *u8g_get_avr_io_ptr(const IO_PTR *base, uint8_t offset)
  129. {
  130. volatile uint8_t * tmp;
  131. base += offset;
  132. memcpy_P(&tmp, base, sizeof(volatile uint8_t * PROGMEM));
  133. return tmp;
  134. }
  135. /* set direction to output of the specified pin (internal pin number) */
  136. void u8g_SetPinOutput(uint8_t internal_pin_number)
  137. {
  138. *u8g_get_avr_io_ptr(u8g_avr_ddr_P, internal_pin_number>>3) |= _BV(internal_pin_number&7);
  139. }
  140. void u8g_SetPinInput(uint8_t internal_pin_number)
  141. {
  142. *u8g_get_avr_io_ptr(u8g_avr_ddr_P, internal_pin_number>>3) &= ~_BV(internal_pin_number&7);
  143. }
  144. void u8g_SetPinLevel(uint8_t internal_pin_number, uint8_t level)
  145. {
  146. volatile uint8_t * tmp = u8g_get_avr_io_ptr(u8g_avr_port_P, internal_pin_number>>3);
  147. if ( level == 0 )
  148. {
  149. U8G_ATOMIC_AND(tmp, ~_BV(internal_pin_number&7));
  150. // *tmp &= ~_BV(internal_pin_number&7);
  151. }
  152. else
  153. {
  154. U8G_ATOMIC_OR(tmp, _BV(internal_pin_number&7));
  155. //*tmp |= _BV(internal_pin_number&7);
  156. }
  157. }
  158. uint8_t u8g_GetPinLevel(uint8_t internal_pin_number)
  159. {
  160. volatile uint8_t * tmp = u8g_get_avr_io_ptr(u8g_avr_pin_P, internal_pin_number>>3);
  161. if ( ((*tmp) & _BV(internal_pin_number&7)) != 0 )
  162. return 1;
  163. return 0;
  164. }
  165. #elif defined(U8G_RASPBERRY_PI)
  166. #include <wiringPi.h>
  167. //#include "/usr/local/include/wiringPi.h"
  168. void u8g_SetPinOutput(uint8_t internal_pin_number) {
  169. pinMode(internal_pin_number, OUTPUT);
  170. }
  171. void u8g_SetPinInput(uint8_t internal_pin_number) {
  172. pinMode(internal_pin_number, INPUT);
  173. }
  174. void u8g_SetPinLevel(uint8_t internal_pin_number, uint8_t level) {
  175. digitalWrite(internal_pin_number, level);
  176. }
  177. uint8_t u8g_GetPinLevel(uint8_t internal_pin_number) {
  178. return digitalRead(internal_pin_number);
  179. }
  180. #else
  181. /* convert "port" and "bitpos" to internal pin number */
  182. uint8_t u8g_Pin(uint8_t port, uint8_t bitpos)
  183. {
  184. port <<= 3;
  185. port += bitpos;
  186. return port;
  187. }
  188. void u8g_SetPinOutput(uint8_t internal_pin_number)
  189. {
  190. }
  191. void u8g_SetPinInput(uint8_t internal_pin_number)
  192. {
  193. }
  194. void u8g_SetPinLevel(uint8_t internal_pin_number, uint8_t level)
  195. {
  196. }
  197. uint8_t u8g_GetPinLevel(uint8_t internal_pin_number)
  198. {
  199. return 0;
  200. }
  201. #endif
  202. #if defined(U8G_WITH_PINLIST)
  203. void u8g_SetPIOutput(u8g_t *u8g, uint8_t pi)
  204. {
  205. uint8_t pin;
  206. pin = u8g->pin_list[pi];
  207. if ( pin != U8G_PIN_NONE )
  208. u8g_SetPinOutput(pin);
  209. }
  210. void u8g_SetPILevel(u8g_t *u8g, uint8_t pi, uint8_t level)
  211. {
  212. uint8_t pin;
  213. pin = u8g->pin_list[pi];
  214. if ( pin != U8G_PIN_NONE )
  215. u8g_SetPinLevel(pin, level);
  216. }
  217. #else /* defined(U8G_WITH_PINLIST) */
  218. void u8g_SetPIOutput(u8g_t *u8g, uint8_t pi)
  219. {
  220. }
  221. void u8g_SetPILevel(u8g_t *u8g, uint8_t pi, uint8_t level)
  222. {
  223. }
  224. #endif /* defined(U8G_WITH_PINLIST) */