Skip to content

Commit

Permalink
Add MKS UI (TFT_LITTLE_VGL_UI) (#18071)
Browse files Browse the repository at this point in the history
  • Loading branch information
makerbase-mks authored Jun 16, 2020
1 parent 6d571a5 commit 642112d
Show file tree
Hide file tree
Showing 70 changed files with 16,162 additions and 10 deletions.
10 changes: 10 additions & 0 deletions Marlin/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -2116,10 +2116,20 @@
//
//#define FSMC_GRAPHICAL_TFT

//
// TFT Little VGL UI
//
//#define TFT_LITTLE_VGL_UI

//=============================================================================
//============================ Other Controllers ============================
//=============================================================================

//
// Robin nano v2.0 SPI touch screen
//
//#define SPI_GRAPHICAL_TFT

//
// ADS7843/XPT2046 ADC Touchscreen such as ILI9341 2.8
//
Expand Down
16 changes: 16 additions & 0 deletions Marlin/src/MarlinCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@
#include "gcode/parser.h"
#include "gcode/queue.h"

#if ENABLED(TFT_LITTLE_VGL_UI)
#include "lvgl.h"
#include "lcd/extui/lib/mks_ui/inc/tft_lvgl_configuration.h"
#include "lcd/extui/lib/mks_ui/inc/draw_ui.h"
#endif

#if ENABLED(DIRECT_STEPPING)
#include "feature/direct_stepping.h"
#endif
Expand Down Expand Up @@ -726,6 +732,10 @@ void idle(TERN_(ADVANCED_PAUSE_FEATURE, bool no_stepper_sleep/*=false*/)) {

// Direct Stepping
TERN_(DIRECT_STEPPING, page_manager.write_responses());

#if ENABLED(TFT_LITTLE_VGL_UI)
LV_TASK_HANDLER();
#endif
}

/**
Expand Down Expand Up @@ -1141,6 +1151,10 @@ void setup() {
SETUP_RUN(page_manager.init());
#endif

#if ENABLED(TFT_LITTLE_VGL_UI)
SETUP_RUN(tft_lvgl_init());
#endif

marlin_state = MF_RUNNING;

SETUP_LOG("setup() completed.");
Expand Down Expand Up @@ -1173,5 +1187,7 @@ void loop() {

endstops.event_handler();

TERN_(TFT_LITTLE_VGL_UI, printer_state_polling());

} while (ENABLED(__AVR__)); // Loop forever on slower (AVR) boards
}
2 changes: 2 additions & 0 deletions Marlin/src/inc/Conditionals_LCD.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@
#define DOGLCD
#define IS_ULTIPANEL
#define DELAYED_BACKLIGHT_INIT
#elif ENABLED(SPI_GRAPHICAL_TFT)
#define DELAYED_BACKLIGHT_INIT
#endif

/**
Expand Down
271 changes: 271 additions & 0 deletions Marlin/src/lcd/extui/lib/mks_ui/SPI_TFT.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,271 @@
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

#include "../../../../inc/MarlinConfigPre.h"

#if ENABLED(SPI_GRAPHICAL_TFT)

#include <SPI.h>
#include "../../../../inc/MarlinConfig.h"
#include "SPI_TFT.h"

TFT SPI_TFT;

#ifndef SPI_TFT_MISO_PIN
#define SPI_TFT_MISO_PIN PA6
#endif
#ifndef SPI_TFT_MOSI_PIN
#define SPI_TFT_MOSI_PIN PA7
#endif
#ifndef SPI_TFT_SCK_PIN
#define SPI_TFT_SCK_PIN PA5
#endif
#ifndef SPI_TFT_CS_PIN
#define SPI_TFT_CS_PIN PD11
#endif
#ifndef SPI_TFT_DC_PIN
#define SPI_TFT_DC_PIN PD10
#endif
#ifndef SPI_TFT_RST_PIN
#define SPI_TFT_RST_PIN PC6
#endif

// use SPI1 for the spi tft.
void TFT::spi_init(uint8_t spiRate) {

SPI_TFT_CS_H;

/**
* STM32F1 APB2 = 72MHz, APB1 = 36MHz, max SPI speed of this MCU if 18Mhz
* STM32F1 has 3 SPI ports, SPI1 in APB2, SPI2/SPI3 in APB1
* so the minimum prescale of SPI1 is DIV4, SPI2/SPI3 is DIV2
*/
uint8_t clock;
switch (spiRate) {
case SPI_FULL_SPEED: clock = SPI_CLOCK_DIV4; break;
case SPI_HALF_SPEED: clock = SPI_CLOCK_DIV4; break;
case SPI_QUARTER_SPEED: clock = SPI_CLOCK_DIV8; break;
case SPI_EIGHTH_SPEED: clock = SPI_CLOCK_DIV16; break;
case SPI_SPEED_5: clock = SPI_CLOCK_DIV32; break;
case SPI_SPEED_6: clock = SPI_CLOCK_DIV64; break;
default: clock = SPI_CLOCK_DIV2; // Default from the SPI library
}
SPI.setModule(1);
SPI.begin();
SPI.setClockDivider(clock);
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);
}

uint8_t TFT::spi_Rec() {
uint8_t returnByte = SPI.transfer(ff);
return returnByte;
}

uint8_t TFT::spi_read_write_byte(uint8_t data) {
uint8_t returnByte = SPI.transfer(data);
return returnByte;
}

/**
* @brief Receive a number of bytes from the SPI port to a buffer
*
* @param buf Pointer to starting address of buffer to write to.
* @param nbyte Number of bytes to receive.
* @return Nothing
*
* @details Uses DMA
*/
void TFT::spi_Read(uint8_t* buf, uint16_t nbyte) {SPI.dmaTransfer(0, const_cast<uint8_t*>(buf), nbyte);}

/**
* @brief Send a single byte on SPI port
*
* @param b Byte to send
*
* @details
*/
void TFT::spi_Send(uint8_t b) {SPI.send(b);}

/**
* @brief Write token and then write from 512 byte buffer to SPI (for SD card)
*
* @param buf Pointer with buffer start address
* @return Nothing
*
* @details Use DMA
*/
void TFT::spi_SendBlock(uint8_t token, const uint8_t* buf) {
SPI.send(token);
SPI.dmaSend(const_cast<uint8_t*>(buf), 512);
}

void TFT::LCD_WR_REG(uint8_t cmd) {
SPI_TFT_CS_L;
SPI_TFT_DC_L;
spi_Send(cmd);
SPI_TFT_CS_H;
}
void TFT::LCD_WR_DATA(uint8_t data) {
SPI_TFT_CS_L;
SPI_TFT_DC_H;
spi_Send(data);
SPI_TFT_CS_H;
}
void TFT::LCD_WriteRAM_Prepare() {LCD_WR_REG(0X2C);}
void TFT::SetCursor(uint16_t x, uint16_t y) {
LCD_WR_REG(0x2a);
LCD_WR_DATA(x >> 8);
LCD_WR_DATA(x);
LCD_WR_DATA(x >> 8);
LCD_WR_DATA(x);

LCD_WR_REG(0x2b);
LCD_WR_DATA(y >> 8);
LCD_WR_DATA(y);
LCD_WR_DATA(y >> 8);
LCD_WR_DATA(y);
}
void TFT::SetWindows(uint16_t x, uint16_t y, uint16_t with, uint16_t height) {
LCD_WR_REG(0x2a);
LCD_WR_DATA(x >> 8);
LCD_WR_DATA(x);
LCD_WR_DATA((x + with) >> 8);
LCD_WR_DATA((x + with));

LCD_WR_REG(0x2b);
LCD_WR_DATA(y >> 8);
LCD_WR_DATA(y);
LCD_WR_DATA((y + height) >> 8);
LCD_WR_DATA(y + height);
}
void TFT::LCD_init() {
SPI_TFT_RST_H;
delay(150);
SPI_TFT_RST_L;
delay(150);
SPI_TFT_RST_H;

delay(120);
LCD_WR_REG(0x11);
delay(120);

LCD_WR_REG(0xf0);
LCD_WR_DATA(0xc3);
LCD_WR_REG(0xf0);
LCD_WR_DATA(0x96);

LCD_WR_REG(0x36);
LCD_WR_DATA(0x28);

LCD_WR_REG(0x3A);
LCD_WR_DATA(0x55);

LCD_WR_REG(0xB4);
LCD_WR_DATA(0x01);
LCD_WR_REG(0xB7);
LCD_WR_DATA(0xC6);
LCD_WR_REG(0xe8);
LCD_WR_DATA(0x40);
LCD_WR_DATA(0x8a);
LCD_WR_DATA(0x00);
LCD_WR_DATA(0x00);
LCD_WR_DATA(0x29);
LCD_WR_DATA(0x19);
LCD_WR_DATA(0xa5);
LCD_WR_DATA(0x33);
LCD_WR_REG(0xc1);
LCD_WR_DATA(0x06);
LCD_WR_REG(0xc2);
LCD_WR_DATA(0xa7);
LCD_WR_REG(0xc5);
LCD_WR_DATA(0x18);
LCD_WR_REG(0xe0); // Positive Voltage Gamma Control
LCD_WR_DATA(0xf0);
LCD_WR_DATA(0x09);
LCD_WR_DATA(0x0b);
LCD_WR_DATA(0x06);
LCD_WR_DATA(0x04);
LCD_WR_DATA(0x15);
LCD_WR_DATA(0x2f);
LCD_WR_DATA(0x54);
LCD_WR_DATA(0x42);
LCD_WR_DATA(0x3c);
LCD_WR_DATA(0x17);
LCD_WR_DATA(0x14);
LCD_WR_DATA(0x18);
LCD_WR_DATA(0x1b);
LCD_WR_REG(0xe1); // Negative Voltage Gamma Control
LCD_WR_DATA(0xf0);
LCD_WR_DATA(0x09);
LCD_WR_DATA(0x0b);
LCD_WR_DATA(0x06);
LCD_WR_DATA(0x04);
LCD_WR_DATA(0x03);
LCD_WR_DATA(0x2d);
LCD_WR_DATA(0x43);
LCD_WR_DATA(0x42);
LCD_WR_DATA(0x3b);
LCD_WR_DATA(0x16);
LCD_WR_DATA(0x14);
LCD_WR_DATA(0x17);
LCD_WR_DATA(0x1b);
LCD_WR_REG(0xf0);
LCD_WR_DATA(0x3c);
LCD_WR_REG(0xf0);
LCD_WR_DATA(0x69);
delay(120); // Delay 120ms
LCD_WR_REG(0x29); // Display ON

LCD_clear(0x0000); //
SPI_TFT_BLK_H;

}
void TFT::LCD_clear(uint16_t color) {
unsigned int i, m;
uint32_t count;
uint8_t tbuf[960];

SetCursor(0, 0);
SetWindows(0, 0, 480 - 1, 320 - 1);
LCD_WriteRAM_Prepare();
SPI_TFT_CS_L;
SPI_TFT_DC_H;
for (i = 0; i < 960;) {
tbuf[i] = color >> 8;
tbuf[i + 1] = color;
i += 2;
}
for (i = 0; i < 320; i++) {
// for(m=0;m<480;m++)
// {
// LCD_WR_DATA(color>>8);
// LCD_WR_DATA(color);

SPI.dmaSend(tbuf, 960, true);
// SPI_TFT_CS_H;
// }
}
SPI_TFT_CS_H;
}

#endif // SPI_GRAPHICAL_TFT
Loading

0 comments on commit 642112d

Please sign in to comment.