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_arduino_hw_spi.c 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. u8g_com_arduino_hw_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. */
  27. #include "u8g.h"
  28. #if defined(ARDUINO)
  29. #if defined(__AVR__)
  30. #include <avr/interrupt.h>
  31. #include <avr/io.h>
  32. #if ARDUINO < 100
  33. #include <WProgram.h>
  34. /* fixed pins */
  35. #if defined(__AVR_ATmega644P__) || defined(__AVR_ATmega1284P__) // Sanguino.cc board
  36. #define PIN_SCK 7
  37. #define PIN_MISO 6
  38. #define PIN_MOSI 5
  39. #define PIN_CS 4
  40. #else // Arduino Board
  41. #define PIN_SCK 13
  42. #define PIN_MISO 12
  43. #define PIN_MOSI 11
  44. #define PIN_CS 10
  45. #endif // (__AVR_ATmega644P__) || defined(__AVR_ATmega1284P__)
  46. #else
  47. #include <Arduino.h>
  48. /* use Arduino pin definitions */
  49. #define PIN_SCK SCK
  50. #define PIN_MISO MISO
  51. #define PIN_MOSI MOSI
  52. #define PIN_CS SS
  53. #endif
  54. //static uint8_t u8g_spi_out(uint8_t data) U8G_NOINLINE;
  55. static uint8_t u8g_spi_out(uint8_t data)
  56. {
  57. /* unsigned char x = 100; */
  58. /* send data */
  59. SPDR = data;
  60. /* wait for transmission */
  61. while (!(SPSR & (1<<SPIF)))
  62. ;
  63. /* clear the SPIF flag by reading SPDR */
  64. return SPDR;
  65. }
  66. uint8_t u8g_com_arduino_hw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
  67. {
  68. switch(msg)
  69. {
  70. case U8G_COM_MSG_STOP:
  71. break;
  72. case U8G_COM_MSG_INIT:
  73. u8g_com_arduino_assign_pin_output_high(u8g);
  74. pinMode(PIN_SCK, OUTPUT);
  75. digitalWrite(PIN_SCK, LOW);
  76. pinMode(PIN_MOSI, OUTPUT);
  77. digitalWrite(PIN_MOSI, LOW);
  78. /* pinMode(PIN_MISO, INPUT); */
  79. pinMode(PIN_CS, OUTPUT); /* system chip select for the atmega board */
  80. digitalWrite(PIN_CS, HIGH);
  81. /*
  82. SPR1 SPR0
  83. 0 0 fclk/4
  84. 0 1 fclk/16
  85. 1 0 fclk/64
  86. 1 1 fclk/128
  87. */
  88. SPCR = 0;
  89. SPCR = (1<<SPE) | (1<<MSTR)|(0<<SPR1)|(1<<SPR0)|(0<<CPOL)|(0<<CPHA);
  90. #ifdef U8G_HW_SPI_2X
  91. SPSR = (1 << SPI2X); /* double speed, issue 89 */
  92. #endif
  93. break;
  94. case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 0) or data mode (arg_val = 1) */
  95. u8g_com_arduino_digital_write(u8g, U8G_PI_A0, arg_val);
  96. break;
  97. case U8G_COM_MSG_CHIP_SELECT:
  98. if ( arg_val == 0 )
  99. {
  100. /* disable */
  101. u8g_com_arduino_digital_write(u8g, U8G_PI_CS, HIGH);
  102. }
  103. else
  104. {
  105. /* enable */
  106. u8g_com_arduino_digital_write(u8g, U8G_PI_SCK, LOW);
  107. u8g_com_arduino_digital_write(u8g, U8G_PI_CS, LOW);
  108. }
  109. break;
  110. case U8G_COM_MSG_RESET:
  111. if ( u8g->pin_list[U8G_PI_RESET] != U8G_PIN_NONE )
  112. u8g_com_arduino_digital_write(u8g, U8G_PI_RESET, arg_val);
  113. break;
  114. case U8G_COM_MSG_WRITE_BYTE:
  115. u8g_spi_out(arg_val);
  116. break;
  117. case U8G_COM_MSG_WRITE_SEQ:
  118. {
  119. register uint8_t *ptr = arg_ptr;
  120. while( arg_val > 0 )
  121. {
  122. u8g_spi_out(*ptr++);
  123. arg_val--;
  124. }
  125. }
  126. break;
  127. case U8G_COM_MSG_WRITE_SEQ_P:
  128. {
  129. register uint8_t *ptr = arg_ptr;
  130. while( arg_val > 0 )
  131. {
  132. u8g_spi_out(u8g_pgm_read(ptr));
  133. ptr++;
  134. arg_val--;
  135. }
  136. }
  137. break;
  138. }
  139. return 1;
  140. }
  141. /* #elif defined(__18CXX) || defined(__PIC32MX) */
  142. #else /* __AVR__ */
  143. #endif /* __AVR__ */
  144. #else /* ARDUINO */
  145. uint8_t u8g_com_arduino_hw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
  146. {
  147. return 1;
  148. }
  149. #endif /* ARDUINO */