My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

u8g_com_io.c 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. */
  28. #include "u8g.h"
  29. #if defined(__AVR__)
  30. #include <avr/interrupt.h>
  31. #include <avr/io.h>
  32. typedef volatile uint8_t * IO_PTR;
  33. /* create internal pin number */
  34. uint8_t u8g_Pin(uint8_t port, uint8_t bitpos)
  35. {
  36. port <<= 3;
  37. port += bitpos;
  38. return port;
  39. }
  40. const IO_PTR u8g_avr_ddr_P[] PROGMEM = {
  41. #ifdef DDRA
  42. &DDRA,
  43. #else
  44. 0,
  45. #endif
  46. &DDRB,
  47. #ifdef DDRC
  48. &DDRC,
  49. #ifdef DDRD
  50. &DDRD,
  51. #ifdef DDRE
  52. &DDRE,
  53. #ifdef DDRF
  54. &DDRF,
  55. #ifdef DDRG
  56. &DDRG,
  57. #ifdef DDRH
  58. &DDRH,
  59. #endif
  60. #endif
  61. #endif
  62. #endif
  63. #endif
  64. #endif
  65. };
  66. const IO_PTR u8g_avr_port_P[] PROGMEM = {
  67. #ifdef PORTA
  68. &PORTA,
  69. #else
  70. 0,
  71. #endif
  72. &PORTB,
  73. #ifdef PORTC
  74. &PORTC,
  75. #ifdef PORTD
  76. &PORTD,
  77. #ifdef PORTE
  78. &PORTE,
  79. #ifdef PORTF
  80. &PORTF,
  81. #ifdef PORTG
  82. &PORTG,
  83. #ifdef PORTH
  84. &PORTH,
  85. #endif
  86. #endif
  87. #endif
  88. #endif
  89. #endif
  90. #endif
  91. };
  92. const IO_PTR u8g_avr_pin_P[] PROGMEM = {
  93. #ifdef PINA
  94. &PINA,
  95. #else
  96. 0,
  97. #endif
  98. &PINB,
  99. #ifdef PINC
  100. &PINC,
  101. #ifdef PIND
  102. &PIND,
  103. #ifdef PINE
  104. &PINE,
  105. #ifdef PINF
  106. &PINF,
  107. #ifdef PING
  108. &PING,
  109. #ifdef PINH
  110. &PINH,
  111. #endif
  112. #endif
  113. #endif
  114. #endif
  115. #endif
  116. #endif
  117. };
  118. static volatile uint8_t *u8g_get_avr_io_ptr(const IO_PTR *base, uint8_t offset)
  119. {
  120. volatile uint8_t * tmp;
  121. base += offset;
  122. memcpy_P(&tmp, base, sizeof(volatile uint8_t * PROGMEM));
  123. return tmp;
  124. }
  125. /* set direction to output of the specified pin (internal pin number) */
  126. void u8g_SetPinOutput(uint8_t internal_pin_number)
  127. {
  128. *u8g_get_avr_io_ptr(u8g_avr_ddr_P, internal_pin_number>>3) |= _BV(internal_pin_number&7);
  129. }
  130. void u8g_SetPinInput(uint8_t internal_pin_number)
  131. {
  132. *u8g_get_avr_io_ptr(u8g_avr_ddr_P, internal_pin_number>>3) &= ~_BV(internal_pin_number&7);
  133. }
  134. void u8g_SetPinLevel(uint8_t internal_pin_number, uint8_t level)
  135. {
  136. volatile uint8_t * tmp = u8g_get_avr_io_ptr(u8g_avr_port_P, internal_pin_number>>3);
  137. if ( level == 0 )
  138. *tmp &= ~_BV(internal_pin_number&7);
  139. else
  140. *tmp |= _BV(internal_pin_number&7);
  141. }
  142. uint8_t u8g_GetPinLevel(uint8_t internal_pin_number)
  143. {
  144. volatile uint8_t * tmp = u8g_get_avr_io_ptr(u8g_avr_pin_P, internal_pin_number>>3);
  145. if ( ((*tmp) & _BV(internal_pin_number&7)) != 0 )
  146. return 1;
  147. return 0;
  148. }
  149. #else
  150. uint8_t u8g_Pin(uint8_t port, uint8_t bitpos)
  151. {
  152. port <<= 3;
  153. port += bitpos;
  154. return port;
  155. }
  156. void u8g_SetPinOutput(uint8_t internal_pin_number)
  157. {
  158. }
  159. void u8g_SetPinInput(uint8_t internal_pin_number)
  160. {
  161. }
  162. void u8g_SetPinLevel(uint8_t internal_pin_number, uint8_t level)
  163. {
  164. }
  165. uint8_t u8g_GetPinLevel(uint8_t internal_pin_number)
  166. {
  167. return 0;
  168. }
  169. #endif
  170. void u8g_SetPIOutput(u8g_t *u8g, uint8_t pi)
  171. {
  172. uint8_t pin;
  173. pin = u8g->pin_list[pi];
  174. if ( pin != U8G_PIN_NONE )
  175. u8g_SetPinOutput(pin);
  176. }
  177. void u8g_SetPILevel(u8g_t *u8g, uint8_t pi, uint8_t level)
  178. {
  179. uint8_t pin;
  180. pin = u8g->pin_list[pi];
  181. if ( pin != U8G_PIN_NONE )
  182. u8g_SetPinLevel(pin, level);
  183. }