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_LPC1768_sw_spi.cpp 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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_std_sw_spi.c
  24. *
  25. * Universal 8bit Graphics Library
  26. *
  27. * Copyright (c) 2015, 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 TARGET_LPC1768
  55. #include "../../../inc/MarlinConfigPre.h"
  56. #if HAS_MARLINUI_U8GLIB && !IS_U8GLIB_ST7920
  57. #include <SoftwareSPI.h>
  58. #include "../../shared/HAL_SPI.h"
  59. #ifndef LCD_SPI_SPEED
  60. #define LCD_SPI_SPEED SPI_QUARTER_SPEED // About 2 MHz
  61. #endif
  62. #include <Arduino.h>
  63. #include <algorithm>
  64. #include <LPC17xx.h>
  65. #include <gpio.h>
  66. #include <U8glib-HAL.h>
  67. uint8_t swSpiTransfer_mode_0(uint8_t b, const uint8_t spi_speed, const pin_t sck_pin, const pin_t miso_pin, const pin_t mosi_pin ) {
  68. LOOP_L_N(i, 8) {
  69. if (spi_speed == 0) {
  70. LPC176x::gpio_set(mosi_pin, !!(b & 0x80));
  71. LPC176x::gpio_set(sck_pin, HIGH);
  72. b <<= 1;
  73. if (miso_pin >= 0 && LPC176x::gpio_get(miso_pin)) b |= 1;
  74. LPC176x::gpio_set(sck_pin, LOW);
  75. }
  76. else {
  77. const uint8_t state = (b & 0x80) ? HIGH : LOW;
  78. LOOP_L_N(j, spi_speed)
  79. LPC176x::gpio_set(mosi_pin, state);
  80. LOOP_L_N(j, spi_speed + (miso_pin >= 0 ? 0 : 1))
  81. LPC176x::gpio_set(sck_pin, HIGH);
  82. b <<= 1;
  83. if (miso_pin >= 0 && LPC176x::gpio_get(miso_pin)) b |= 1;
  84. LOOP_L_N(j, spi_speed)
  85. LPC176x::gpio_set(sck_pin, LOW);
  86. }
  87. }
  88. return b;
  89. }
  90. uint8_t swSpiTransfer_mode_3(uint8_t b, const uint8_t spi_speed, const pin_t sck_pin, const pin_t miso_pin, const pin_t mosi_pin ) {
  91. LOOP_L_N(i, 8) {
  92. const uint8_t state = (b & 0x80) ? HIGH : LOW;
  93. if (spi_speed == 0) {
  94. LPC176x::gpio_set(sck_pin, LOW);
  95. LPC176x::gpio_set(mosi_pin, state);
  96. LPC176x::gpio_set(mosi_pin, state); // need some setup time
  97. LPC176x::gpio_set(sck_pin, HIGH);
  98. }
  99. else {
  100. LOOP_L_N(j, spi_speed + (miso_pin >= 0 ? 0 : 1))
  101. LPC176x::gpio_set(sck_pin, LOW);
  102. LOOP_L_N(j, spi_speed)
  103. LPC176x::gpio_set(mosi_pin, state);
  104. LOOP_L_N(j, spi_speed)
  105. LPC176x::gpio_set(sck_pin, HIGH);
  106. }
  107. b <<= 1;
  108. if (miso_pin >= 0 && LPC176x::gpio_get(miso_pin)) b |= 1;
  109. }
  110. return b;
  111. }
  112. static uint8_t SPI_speed = 0;
  113. static void u8g_sw_spi_HAL_LPC1768_shift_out(uint8_t dataPin, uint8_t clockPin, uint8_t val) {
  114. #if EITHER(FYSETC_MINI_12864, MKS_MINI_12864)
  115. swSpiTransfer_mode_3(val, SPI_speed, clockPin, -1, dataPin);
  116. #else
  117. swSpiTransfer_mode_0(val, SPI_speed, clockPin, -1, dataPin);
  118. #endif
  119. }
  120. uint8_t u8g_com_HAL_LPC1768_sw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) {
  121. switch (msg) {
  122. case U8G_COM_MSG_INIT:
  123. u8g_SetPIOutput(u8g, U8G_PI_SCK);
  124. u8g_SetPIOutput(u8g, U8G_PI_MOSI);
  125. u8g_SetPIOutput(u8g, U8G_PI_CS);
  126. u8g_SetPIOutput(u8g, U8G_PI_A0);
  127. if (U8G_PIN_NONE != u8g->pin_list[U8G_PI_RESET]) u8g_SetPIOutput(u8g, U8G_PI_RESET);
  128. SPI_speed = swSpiInit(LCD_SPI_SPEED, u8g->pin_list[U8G_PI_SCK], u8g->pin_list[U8G_PI_MOSI]);
  129. u8g_SetPILevel(u8g, U8G_PI_SCK, 0);
  130. u8g_SetPILevel(u8g, U8G_PI_MOSI, 0);
  131. break;
  132. case U8G_COM_MSG_STOP:
  133. break;
  134. case U8G_COM_MSG_RESET:
  135. if (U8G_PIN_NONE != u8g->pin_list[U8G_PI_RESET]) u8g_SetPILevel(u8g, U8G_PI_RESET, arg_val);
  136. break;
  137. case U8G_COM_MSG_CHIP_SELECT:
  138. #if EITHER(FYSETC_MINI_12864, MKS_MINI_12864) // LCD SPI is running mode 3 while SD card is running mode 0
  139. if (arg_val) { // SCK idle state needs to be set to the proper idle state before
  140. // the next chip select goes active
  141. u8g_SetPILevel(u8g, U8G_PI_SCK, 1); // Set SCK to mode 3 idle state before CS goes active
  142. u8g_SetPILevel(u8g, U8G_PI_CS, LOW);
  143. }
  144. else {
  145. u8g_SetPILevel(u8g, U8G_PI_CS, HIGH);
  146. u8g_SetPILevel(u8g, U8G_PI_SCK, 0); // Set SCK to mode 0 idle state after CS goes inactive
  147. }
  148. #else
  149. u8g_SetPILevel(u8g, U8G_PI_CS, !arg_val);
  150. #endif
  151. break;
  152. case U8G_COM_MSG_WRITE_BYTE:
  153. u8g_sw_spi_HAL_LPC1768_shift_out(u8g->pin_list[U8G_PI_MOSI], u8g->pin_list[U8G_PI_SCK], arg_val);
  154. break;
  155. case U8G_COM_MSG_WRITE_SEQ: {
  156. uint8_t *ptr = (uint8_t *)arg_ptr;
  157. while (arg_val > 0) {
  158. u8g_sw_spi_HAL_LPC1768_shift_out(u8g->pin_list[U8G_PI_MOSI], u8g->pin_list[U8G_PI_SCK], *ptr++);
  159. arg_val--;
  160. }
  161. }
  162. break;
  163. case U8G_COM_MSG_WRITE_SEQ_P: {
  164. uint8_t *ptr = (uint8_t *)arg_ptr;
  165. while (arg_val > 0) {
  166. u8g_sw_spi_HAL_LPC1768_shift_out(u8g->pin_list[U8G_PI_MOSI], u8g->pin_list[U8G_PI_SCK], u8g_pgm_read(ptr));
  167. ptr++;
  168. arg_val--;
  169. }
  170. }
  171. break;
  172. case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 0) or data mode (arg_val = 1) */
  173. u8g_SetPILevel(u8g, U8G_PI_A0, arg_val);
  174. break;
  175. }
  176. return 1;
  177. }
  178. #endif // HAS_MARLINUI_U8GLIB && !IS_U8GLIB_ST7920
  179. #endif // TARGET_LPC1768