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_DUE_st7920_sw_spi.cpp 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. #ifdef ARDUINO_ARCH_SAM
  55. #include "../../../inc/MarlinConfigPre.h"
  56. #if IS_U8GLIB_ST7920
  57. #include "../../../inc/MarlinConfig.h"
  58. #include "../../shared/Delay.h"
  59. #include <U8glib-HAL.h>
  60. #include "u8g_com_HAL_DUE_sw_spi_shared.h"
  61. #define SPISEND_SW_DUE u8g_spiSend_sw_DUE_mode_0
  62. static uint8_t rs_last_state = 255;
  63. static void u8g_com_DUE_st7920_write_byte_sw_spi(uint8_t rs, uint8_t val) {
  64. if (rs != rs_last_state) { // time to send a command/data byte
  65. rs_last_state = rs;
  66. SPISEND_SW_DUE(rs ? 0x0FA : 0x0F8); // Command or Data
  67. DELAY_US(40); // give the controller some time to process the data: 20 is bad, 30 is OK, 40 is safe
  68. }
  69. SPISEND_SW_DUE(val & 0xF0);
  70. SPISEND_SW_DUE(val << 4);
  71. }
  72. uint8_t u8g_com_HAL_DUE_ST7920_sw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) {
  73. switch (msg) {
  74. case U8G_COM_MSG_INIT:
  75. SCK_pPio = g_APinDescription[u8g->pin_list[U8G_PI_SCK]].pPort;
  76. SCK_dwMask = g_APinDescription[u8g->pin_list[U8G_PI_SCK]].ulPin;
  77. MOSI_pPio = g_APinDescription[u8g->pin_list[U8G_PI_MOSI]].pPort;
  78. MOSI_dwMask = g_APinDescription[u8g->pin_list[U8G_PI_MOSI]].ulPin;
  79. u8g_SetPILevel_DUE(u8g, U8G_PI_CS, 0);
  80. u8g_SetPIOutput_DUE(u8g, U8G_PI_CS);
  81. u8g_SetPILevel_DUE(u8g, U8G_PI_SCK, 0);
  82. u8g_SetPIOutput_DUE(u8g, U8G_PI_SCK);
  83. u8g_SetPILevel_DUE(u8g, U8G_PI_MOSI, 0);
  84. u8g_SetPIOutput_DUE(u8g, U8G_PI_MOSI);
  85. SCK_pPio->PIO_CODR = SCK_dwMask; //SCK low - needed at power up but not after reset
  86. MOSI_pPio->PIO_CODR = MOSI_dwMask; //MOSI low - needed at power up but not after reset
  87. u8g_Delay(5);
  88. u8g->pin_list[U8G_PI_A0_STATE] = 0; /* initial RS state: command mode */
  89. break;
  90. case U8G_COM_MSG_STOP:
  91. break;
  92. case U8G_COM_MSG_RESET:
  93. if (U8G_PIN_NONE != u8g->pin_list[U8G_PI_RESET]) u8g_SetPILevel_DUE(u8g, U8G_PI_RESET, arg_val);
  94. break;
  95. case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 0) or data mode (arg_val = 1) */
  96. u8g->pin_list[U8G_PI_A0_STATE] = arg_val;
  97. break;
  98. case U8G_COM_MSG_CHIP_SELECT:
  99. if (U8G_PIN_NONE != u8g->pin_list[U8G_PI_CS])
  100. u8g_SetPILevel_DUE(u8g, U8G_PI_CS, arg_val); //note: the st7920 has an active high chip select
  101. break;
  102. case U8G_COM_MSG_WRITE_BYTE:
  103. u8g_com_DUE_st7920_write_byte_sw_spi(u8g->pin_list[U8G_PI_A0_STATE], arg_val);
  104. break;
  105. case U8G_COM_MSG_WRITE_SEQ: {
  106. uint8_t *ptr = (uint8_t*) arg_ptr;
  107. while (arg_val > 0) {
  108. u8g_com_DUE_st7920_write_byte_sw_spi(u8g->pin_list[U8G_PI_A0_STATE], *ptr++);
  109. arg_val--;
  110. }
  111. }
  112. break;
  113. case U8G_COM_MSG_WRITE_SEQ_P: {
  114. uint8_t *ptr = (uint8_t*) arg_ptr;
  115. while (arg_val > 0) {
  116. u8g_com_DUE_st7920_write_byte_sw_spi(u8g->pin_list[U8G_PI_A0_STATE], *ptr++);
  117. arg_val--;
  118. }
  119. }
  120. break;
  121. }
  122. return 1;
  123. }
  124. #if ENABLED(LIGHTWEIGHT_UI)
  125. #include "../../../lcd/marlinui.h"
  126. #include "../../shared/HAL_ST7920.h"
  127. #define ST7920_CS_PIN LCD_PINS_RS
  128. #if DOGM_SPI_DELAY_US > 0
  129. #define U8G_DELAY() DELAY_US(DOGM_SPI_DELAY_US)
  130. #else
  131. #define U8G_DELAY() DELAY_US(10)
  132. #endif
  133. void ST7920_cs() {
  134. WRITE(ST7920_CS_PIN, HIGH);
  135. U8G_DELAY();
  136. }
  137. void ST7920_ncs() {
  138. WRITE(ST7920_CS_PIN, LOW);
  139. }
  140. void ST7920_set_cmd() {
  141. SPISEND_SW_DUE(0xF8);
  142. DELAY_US(40);
  143. }
  144. void ST7920_set_dat() {
  145. SPISEND_SW_DUE(0xFA);
  146. DELAY_US(40);
  147. }
  148. void ST7920_write_byte(const uint8_t val) {
  149. SPISEND_SW_DUE(val & 0xF0);
  150. SPISEND_SW_DUE(val << 4);
  151. }
  152. #endif // LIGHTWEIGHT_UI
  153. #endif // IS_U8GLIB_ST7920
  154. #endif // ARDUINO_ARCH_SAM