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_HAL_AVR_sw_spi.cpp 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * Based on u8g_com_st7920_hw_spi.c
  24. *
  25. * Universal 8bit Graphics Library
  26. *
  27. * Copyright (c) 2011, olikraus@gmail.com
  28. * All rights reserved.
  29. *
  30. * Redistribution and use in source and binary forms, with or without modification,
  31. * are permitted provided that the following conditions are met:
  32. *
  33. * * Redistributions of source code must retain the above copyright notice, this list
  34. * of conditions and the following disclaimer.
  35. *
  36. * * Redistributions in binary form must reproduce the above copyright notice, this
  37. * list of conditions and the following disclaimer in the documentation and/or other
  38. * materials provided with the distribution.
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  41. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  42. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  43. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  44. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  45. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  46. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  47. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  48. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  49. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  50. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  51. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  52. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  53. */
  54. #if defined(ARDUINO) && !defined(ARDUINO_ARCH_STM32) && !defined(ARDUINO_ARCH_SAM)
  55. #include "../../inc/MarlinConfigPre.h"
  56. #if HAS_GRAPHICAL_LCD
  57. #include "../shared/Marduino.h"
  58. #include "../shared/Delay.h"
  59. #include <U8glib.h>
  60. uint8_t u8g_bitData, u8g_bitNotData, u8g_bitClock, u8g_bitNotClock;
  61. volatile uint8_t *u8g_outData, *u8g_outClock;
  62. static void u8g_com_arduino_init_shift_out(uint8_t dataPin, uint8_t clockPin) {
  63. u8g_outData = portOutputRegister(digitalPinToPort(dataPin));
  64. u8g_outClock = portOutputRegister(digitalPinToPort(clockPin));
  65. u8g_bitData = digitalPinToBitMask(dataPin);
  66. u8g_bitClock = digitalPinToBitMask(clockPin);
  67. u8g_bitNotClock = u8g_bitClock;
  68. u8g_bitNotClock ^= 0xFF;
  69. u8g_bitNotData = u8g_bitData;
  70. u8g_bitNotData ^= 0xFF;
  71. }
  72. void u8g_spiSend_sw_AVR_mode_0(uint8_t val) {
  73. uint8_t bitData = u8g_bitData,
  74. bitNotData = u8g_bitNotData,
  75. bitClock = u8g_bitClock,
  76. bitNotClock = u8g_bitNotClock;
  77. volatile uint8_t *outData = u8g_outData,
  78. *outClock = u8g_outClock;
  79. U8G_ATOMIC_START();
  80. LOOP_L_N(i, 8) {
  81. if (val & 0x80)
  82. *outData |= bitData;
  83. else
  84. *outData &= bitNotData;
  85. *outClock |= bitClock;
  86. val <<= 1;
  87. *outClock &= bitNotClock;
  88. }
  89. U8G_ATOMIC_END();
  90. }
  91. void u8g_spiSend_sw_AVR_mode_3(uint8_t val) {
  92. uint8_t bitData = u8g_bitData,
  93. bitNotData = u8g_bitNotData,
  94. bitClock = u8g_bitClock,
  95. bitNotClock = u8g_bitNotClock;
  96. volatile uint8_t *outData = u8g_outData,
  97. *outClock = u8g_outClock;
  98. U8G_ATOMIC_START();
  99. LOOP_L_N(i, 8) {
  100. *outClock &= bitNotClock;
  101. if (val & 0x80)
  102. *outData |= bitData;
  103. else
  104. *outData &= bitNotData;
  105. *outClock |= bitClock;
  106. val <<= 1;
  107. }
  108. U8G_ATOMIC_END();
  109. }
  110. #if ENABLED(FYSETC_MINI_12864)
  111. #define SPISEND_SW_AVR u8g_spiSend_sw_AVR_mode_3
  112. #else
  113. #define SPISEND_SW_AVR u8g_spiSend_sw_AVR_mode_0
  114. #endif
  115. uint8_t u8g_com_HAL_AVR_sw_sp_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) {
  116. switch (msg) {
  117. case U8G_COM_MSG_INIT:
  118. u8g_com_arduino_init_shift_out(u8g->pin_list[U8G_PI_MOSI], u8g->pin_list[U8G_PI_SCK]);
  119. u8g_com_arduino_assign_pin_output_high(u8g);
  120. u8g_com_arduino_digital_write(u8g, U8G_PI_SCK, 0);
  121. u8g_com_arduino_digital_write(u8g, U8G_PI_MOSI, 0);
  122. break;
  123. case U8G_COM_MSG_STOP:
  124. break;
  125. case U8G_COM_MSG_RESET:
  126. if (U8G_PIN_NONE != u8g->pin_list[U8G_PI_RESET]) u8g_com_arduino_digital_write(u8g, U8G_PI_RESET, arg_val);
  127. break;
  128. case U8G_COM_MSG_CHIP_SELECT:
  129. #if ENABLED(FYSETC_MINI_12864) // LCD SPI is running mode 3 while SD card is running mode 0
  130. if (arg_val) { // SCK idle state needs to be set to the proper idle state before
  131. // the next chip select goes active
  132. u8g_com_arduino_digital_write(u8g, U8G_PI_SCK, 1); // Set SCK to mode 3 idle state before CS goes active
  133. u8g_com_arduino_digital_write(u8g, U8G_PI_CS, LOW);
  134. }
  135. else {
  136. u8g_com_arduino_digital_write(u8g, U8G_PI_CS, HIGH);
  137. u8g_com_arduino_digital_write(u8g, U8G_PI_SCK, 0); // Set SCK to mode 0 idle state after CS goes inactive
  138. }
  139. #else
  140. u8g_com_arduino_digital_write(u8g, U8G_PI_CS, !arg_val);
  141. #endif
  142. break;
  143. case U8G_COM_MSG_WRITE_BYTE:
  144. SPISEND_SW_AVR(arg_val);
  145. break;
  146. case U8G_COM_MSG_WRITE_SEQ: {
  147. uint8_t *ptr = (uint8_t *)arg_ptr;
  148. while (arg_val > 0) {
  149. SPISEND_SW_AVR(*ptr++);
  150. arg_val--;
  151. }
  152. }
  153. break;
  154. case U8G_COM_MSG_WRITE_SEQ_P: {
  155. uint8_t *ptr = (uint8_t *)arg_ptr;
  156. while (arg_val > 0) {
  157. SPISEND_SW_AVR(u8g_pgm_read(ptr));
  158. ptr++;
  159. arg_val--;
  160. }
  161. }
  162. break;
  163. case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 0) or data mode (arg_val = 1) */
  164. u8g_com_arduino_digital_write(u8g, U8G_PI_A0, arg_val);
  165. break;
  166. }
  167. return 1;
  168. }
  169. #endif // HAS_GRAPHICAL_LCD
  170. #endif // ARDUINO_ARCH_SAM