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_atmega_st7920_hw_spi.c 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. u8g_com_atmega_st7920_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. A special SPI interface for ST7920 controller with HW SPI Support
  27. Assumes, that
  28. MOSI is at PORTB, Pin 3
  29. and
  30. SCK is at PORTB, Pin 5
  31. */
  32. #include "u8g.h"
  33. #if defined(__AVR__)
  34. #define U8G_ATMEGA_HW_SPI
  35. /* remove the definition for attiny */
  36. #if __AVR_ARCH__ == 2
  37. #undef U8G_ATMEGA_HW_SPI
  38. #endif
  39. #if __AVR_ARCH__ == 25
  40. #undef U8G_ATMEGA_HW_SPI
  41. #endif
  42. #endif
  43. #if defined(U8G_ATMEGA_HW_SPI)
  44. #include <avr/interrupt.h>
  45. #include <avr/io.h>
  46. static uint8_t u8g_atmega_st7920_hw_spi_shift_out(u8g_t *u8g, uint8_t val) U8G_NOINLINE;
  47. static uint8_t u8g_atmega_st7920_hw_spi_shift_out(u8g_t *u8g, uint8_t val)
  48. {
  49. /* send data */
  50. SPDR = val;
  51. /* wait for transmission */
  52. while (!(SPSR & (1<<SPIF)))
  53. ;
  54. /* clear the SPIF flag by reading SPDR */
  55. return SPDR;
  56. }
  57. static void u8g_com_atmega_st7920_write_byte_hw_spi(u8g_t *u8g, uint8_t rs, uint8_t val) U8G_NOINLINE;
  58. static void u8g_com_atmega_st7920_write_byte_hw_spi(u8g_t *u8g, uint8_t rs, uint8_t val)
  59. {
  60. uint8_t i;
  61. if ( rs == 0 )
  62. {
  63. /* command */
  64. u8g_atmega_st7920_hw_spi_shift_out(u8g, 0x0f8);
  65. }
  66. else if ( rs == 1 )
  67. {
  68. /* data */
  69. u8g_atmega_st7920_hw_spi_shift_out(u8g, 0x0fa);
  70. }
  71. u8g_atmega_st7920_hw_spi_shift_out(u8g, val & 0x0f0);
  72. u8g_atmega_st7920_hw_spi_shift_out(u8g, val << 4);
  73. for( i = 0; i < 4; i++ )
  74. u8g_10MicroDelay();
  75. }
  76. uint8_t u8g_com_atmega_st7920_hw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
  77. {
  78. switch(msg)
  79. {
  80. case U8G_COM_MSG_INIT:
  81. u8g_SetPIOutput(u8g, U8G_PI_CS);
  82. //u8g_SetPIOutput(u8g, U8G_PI_A0);
  83. DDRB |= _BV(3); /* D0, MOSI */
  84. DDRB |= _BV(5); /* SCK */
  85. DDRB |= _BV(2); /* slave select */
  86. PORTB &= ~_BV(3); /* D0, MOSI = 0 */
  87. PORTB &= ~_BV(5); /* SCK = 0 */
  88. u8g_SetPILevel(u8g, U8G_PI_CS, 1);
  89. /*
  90. SPR1 SPR0
  91. 0 0 fclk/4
  92. 0 1 fclk/16
  93. 1 0 fclk/64
  94. 1 1 fclk/128
  95. */
  96. SPCR = 0;
  97. /* maybe set CPOL and CPHA to 1 */
  98. /* 20 Dez 2012: did set CPOL and CPHA to 1 in Arduino variant! */
  99. SPCR = (1<<SPE) | (1<<MSTR)|(0<<SPR1)|(0<<SPR0)|(0<<CPOL)|(0<<CPHA);
  100. #ifdef U8G_HW_SPI_2X
  101. SPSR = (1 << SPI2X); /* double speed, issue 89 */
  102. #endif
  103. u8g->pin_list[U8G_PI_A0_STATE] = 0; /* inital RS state: command mode */
  104. break;
  105. case U8G_COM_MSG_STOP:
  106. break;
  107. case U8G_COM_MSG_RESET:
  108. u8g_SetPILevel(u8g, U8G_PI_RESET, arg_val);
  109. break;
  110. case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 0) or data mode (arg_val = 1) */
  111. u8g->pin_list[U8G_PI_A0_STATE] = arg_val;
  112. break;
  113. case U8G_COM_MSG_CHIP_SELECT:
  114. if ( arg_val == 0 )
  115. {
  116. /* disable, note: the st7920 has an active high chip select */
  117. u8g_SetPILevel(u8g, U8G_PI_CS, 0);
  118. }
  119. else
  120. {
  121. /* u8g_SetPILevel(u8g, U8G_PI_SCK, 0 ); */
  122. /* enable */
  123. u8g_SetPILevel(u8g, U8G_PI_CS, 1); /* CS = 1 (high active) */
  124. }
  125. break;
  126. case U8G_COM_MSG_WRITE_BYTE:
  127. u8g_com_atmega_st7920_write_byte_hw_spi(u8g, u8g->pin_list[U8G_PI_A0_STATE], arg_val);
  128. //u8g->pin_list[U8G_PI_A0_STATE] = 2;
  129. break;
  130. case U8G_COM_MSG_WRITE_SEQ:
  131. {
  132. register uint8_t *ptr = arg_ptr;
  133. while( arg_val > 0 )
  134. {
  135. u8g_com_atmega_st7920_write_byte_hw_spi(u8g, u8g->pin_list[U8G_PI_A0_STATE], *ptr++);
  136. //u8g->pin_list[U8G_PI_A0_STATE] = 2;
  137. arg_val--;
  138. }
  139. }
  140. break;
  141. case U8G_COM_MSG_WRITE_SEQ_P:
  142. {
  143. register uint8_t *ptr = arg_ptr;
  144. while( arg_val > 0 )
  145. {
  146. u8g_com_atmega_st7920_write_byte_hw_spi(u8g, u8g->pin_list[U8G_PI_A0_STATE], u8g_pgm_read(ptr));
  147. //u8g->pin_list[U8G_PI_A0_STATE] = 2;
  148. ptr++;
  149. arg_val--;
  150. }
  151. }
  152. break;
  153. }
  154. return 1;
  155. }
  156. #else
  157. uint8_t u8g_com_atmega_st7920_hw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
  158. {
  159. return 1;
  160. }
  161. #endif