Victor Oliveira
4 years ago
committed by
GitHub
28 changed files with 594 additions and 1376 deletions
@ -1,331 +0,0 @@ |
|||||
/**
|
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
|
||||
* |
|
||||
*/ |
|
||||
|
|
||||
/**
|
|
||||
* u8g_com_stm32duino_fsmc.cpp |
|
||||
* |
|
||||
* Communication interface for FSMC |
|
||||
*/ |
|
||||
|
|
||||
#include "../../../inc/MarlinConfig.h" |
|
||||
|
|
||||
#if defined(ARDUINO_ARCH_STM32F1) && PIN_EXISTS(FSMC_CS) // FSMC on 100/144 pins SoCs
|
|
||||
|
|
||||
#if HAS_GRAPHICAL_LCD |
|
||||
|
|
||||
#include <U8glib.h> |
|
||||
#include <libmaple/fsmc.h> |
|
||||
#include <libmaple/gpio.h> |
|
||||
#include <libmaple/dma.h> |
|
||||
#include <boards.h> |
|
||||
|
|
||||
#ifndef LCD_READ_ID |
|
||||
#define LCD_READ_ID 0x04 // Read display identification information (0xD3 on ILI9341)
|
|
||||
#endif |
|
||||
|
|
||||
/* Timing configuration */ |
|
||||
#define FSMC_ADDRESS_SETUP_TIME 15 // AddressSetupTime
|
|
||||
#define FSMC_DATA_SETUP_TIME 15 // DataSetupTime
|
|
||||
|
|
||||
void LCD_IO_Init(uint8_t cs, uint8_t rs); |
|
||||
void LCD_IO_WriteData(uint16_t RegValue); |
|
||||
void LCD_IO_WriteReg(uint16_t Reg); |
|
||||
uint16_t LCD_IO_ReadData(uint16_t RegValue); |
|
||||
uint32_t LCD_IO_ReadData(uint16_t RegValue, uint8_t ReadSize); |
|
||||
#ifdef LCD_USE_DMA_FSMC |
|
||||
void LCD_IO_WriteMultiple(uint16_t data, uint32_t count); |
|
||||
void LCD_IO_WriteSequence(uint16_t *data, uint16_t length); |
|
||||
#endif |
|
||||
|
|
||||
static uint8_t msgInitCount = 2; // Ignore all messages until 2nd U8G_COM_MSG_INIT
|
|
||||
|
|
||||
uint8_t u8g_com_stm32duino_fsmc_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) { |
|
||||
if (msgInitCount) { |
|
||||
if (msg == U8G_COM_MSG_INIT) msgInitCount--; |
|
||||
if (msgInitCount) return -1; |
|
||||
} |
|
||||
|
|
||||
static uint8_t isCommand; |
|
||||
|
|
||||
switch (msg) { |
|
||||
case U8G_COM_MSG_STOP: break; |
|
||||
case U8G_COM_MSG_INIT: |
|
||||
u8g_SetPIOutput(u8g, U8G_PI_RESET); |
|
||||
|
|
||||
#ifdef LCD_USE_DMA_FSMC |
|
||||
dma_init(FSMC_DMA_DEV); |
|
||||
dma_disable(FSMC_DMA_DEV, FSMC_DMA_CHANNEL); |
|
||||
dma_set_priority(FSMC_DMA_DEV, FSMC_DMA_CHANNEL, DMA_PRIORITY_MEDIUM); |
|
||||
#endif |
|
||||
|
|
||||
LCD_IO_Init(u8g->pin_list[U8G_PI_CS], u8g->pin_list[U8G_PI_A0]); |
|
||||
u8g_Delay(50); |
|
||||
|
|
||||
if (arg_ptr) { |
|
||||
*((uint32_t *)arg_ptr) = LCD_IO_ReadData(0x0000); |
|
||||
if (*((uint32_t *)arg_ptr) == 0) |
|
||||
*((uint32_t *)arg_ptr) = (LCD_READ_ID << 24) | LCD_IO_ReadData(LCD_READ_ID, 3); |
|
||||
} |
|
||||
isCommand = 0; |
|
||||
break; |
|
||||
|
|
||||
case U8G_COM_MSG_ADDRESS: // define cmd (arg_val = 0) or data mode (arg_val = 1)
|
|
||||
isCommand = arg_val == 0 ? 1 : 0; |
|
||||
break; |
|
||||
|
|
||||
case U8G_COM_MSG_RESET: |
|
||||
u8g_SetPILevel(u8g, U8G_PI_RESET, arg_val); |
|
||||
break; |
|
||||
|
|
||||
case U8G_COM_MSG_WRITE_BYTE: |
|
||||
if (isCommand) |
|
||||
LCD_IO_WriteReg(arg_val); |
|
||||
else |
|
||||
LCD_IO_WriteData((uint16_t)arg_val); |
|
||||
break; |
|
||||
|
|
||||
case U8G_COM_MSG_WRITE_SEQ: |
|
||||
for (uint8_t i = 0; i < arg_val; i += 2) |
|
||||
LCD_IO_WriteData(*(uint16_t *)(((uint32_t)arg_ptr) + i)); |
|
||||
break; |
|
||||
} |
|
||||
return 1; |
|
||||
} |
|
||||
|
|
||||
/**
|
|
||||
* FSMC LCD IO |
|
||||
*/ |
|
||||
#define __ASM __asm |
|
||||
#define __STATIC_INLINE static inline |
|
||||
|
|
||||
__attribute__((always_inline)) __STATIC_INLINE void __DSB() { |
|
||||
__ASM volatile ("dsb 0xF":::"memory"); |
|
||||
} |
|
||||
|
|
||||
#define FSMC_CS_NE1 PD7 |
|
||||
|
|
||||
#if ENABLED(STM32_XL_DENSITY) |
|
||||
#define FSMC_CS_NE2 PG9 |
|
||||
#define FSMC_CS_NE3 PG10 |
|
||||
#define FSMC_CS_NE4 PG12 |
|
||||
|
|
||||
#define FSMC_RS_A0 PF0 |
|
||||
#define FSMC_RS_A1 PF1 |
|
||||
#define FSMC_RS_A2 PF2 |
|
||||
#define FSMC_RS_A3 PF3 |
|
||||
#define FSMC_RS_A4 PF4 |
|
||||
#define FSMC_RS_A5 PF5 |
|
||||
#define FSMC_RS_A6 PF12 |
|
||||
#define FSMC_RS_A7 PF13 |
|
||||
#define FSMC_RS_A8 PF14 |
|
||||
#define FSMC_RS_A9 PF15 |
|
||||
#define FSMC_RS_A10 PG0 |
|
||||
#define FSMC_RS_A11 PG1 |
|
||||
#define FSMC_RS_A12 PG2 |
|
||||
#define FSMC_RS_A13 PG3 |
|
||||
#define FSMC_RS_A14 PG4 |
|
||||
#define FSMC_RS_A15 PG5 |
|
||||
#endif |
|
||||
|
|
||||
#define FSMC_RS_A16 PD11 |
|
||||
#define FSMC_RS_A17 PD12 |
|
||||
#define FSMC_RS_A18 PD13 |
|
||||
#define FSMC_RS_A19 PE3 |
|
||||
#define FSMC_RS_A20 PE4 |
|
||||
#define FSMC_RS_A21 PE5 |
|
||||
#define FSMC_RS_A22 PE6 |
|
||||
#define FSMC_RS_A23 PE2 |
|
||||
|
|
||||
#if ENABLED(STM32_XL_DENSITY) |
|
||||
#define FSMC_RS_A24 PG13 |
|
||||
#define FSMC_RS_A25 PG14 |
|
||||
#endif |
|
||||
|
|
||||
static uint8_t fsmcInit = 0; |
|
||||
|
|
||||
typedef struct { |
|
||||
__IO uint16_t REG; |
|
||||
__IO uint16_t RAM; |
|
||||
} LCD_CONTROLLER_TypeDef; |
|
||||
|
|
||||
LCD_CONTROLLER_TypeDef *LCD; |
|
||||
|
|
||||
void LCD_IO_Init(uint8_t cs, uint8_t rs) { |
|
||||
uint32_t controllerAddress; |
|
||||
struct fsmc_nor_psram_reg_map* fsmcPsramRegion; |
|
||||
|
|
||||
if (fsmcInit) return; |
|
||||
fsmcInit = 1; |
|
||||
|
|
||||
switch (cs) { |
|
||||
case FSMC_CS_NE1: controllerAddress = (uint32_t)FSMC_NOR_PSRAM_REGION1; fsmcPsramRegion = FSMC_NOR_PSRAM1_BASE; break; |
|
||||
#if ENABLED(STM32_XL_DENSITY) |
|
||||
case FSMC_CS_NE2: controllerAddress = (uint32_t)FSMC_NOR_PSRAM_REGION2; fsmcPsramRegion = FSMC_NOR_PSRAM2_BASE; break; |
|
||||
case FSMC_CS_NE3: controllerAddress = (uint32_t)FSMC_NOR_PSRAM_REGION3; fsmcPsramRegion = FSMC_NOR_PSRAM3_BASE; break; |
|
||||
case FSMC_CS_NE4: controllerAddress = (uint32_t)FSMC_NOR_PSRAM_REGION4; fsmcPsramRegion = FSMC_NOR_PSRAM4_BASE; break; |
|
||||
#endif |
|
||||
default: return; |
|
||||
} |
|
||||
|
|
||||
#define _ORADDR(N) controllerAddress |= (_BV32(N) - 2) |
|
||||
|
|
||||
switch (rs) { |
|
||||
#if ENABLED(STM32_XL_DENSITY) |
|
||||
case FSMC_RS_A0: _ORADDR( 1); break; |
|
||||
case FSMC_RS_A1: _ORADDR( 2); break; |
|
||||
case FSMC_RS_A2: _ORADDR( 3); break; |
|
||||
case FSMC_RS_A3: _ORADDR( 4); break; |
|
||||
case FSMC_RS_A4: _ORADDR( 5); break; |
|
||||
case FSMC_RS_A5: _ORADDR( 6); break; |
|
||||
case FSMC_RS_A6: _ORADDR( 7); break; |
|
||||
case FSMC_RS_A7: _ORADDR( 8); break; |
|
||||
case FSMC_RS_A8: _ORADDR( 9); break; |
|
||||
case FSMC_RS_A9: _ORADDR(10); break; |
|
||||
case FSMC_RS_A10: _ORADDR(11); break; |
|
||||
case FSMC_RS_A11: _ORADDR(12); break; |
|
||||
case FSMC_RS_A12: _ORADDR(13); break; |
|
||||
case FSMC_RS_A13: _ORADDR(14); break; |
|
||||
case FSMC_RS_A14: _ORADDR(15); break; |
|
||||
case FSMC_RS_A15: _ORADDR(16); break; |
|
||||
#endif |
|
||||
case FSMC_RS_A16: _ORADDR(17); break; |
|
||||
case FSMC_RS_A17: _ORADDR(18); break; |
|
||||
case FSMC_RS_A18: _ORADDR(19); break; |
|
||||
case FSMC_RS_A19: _ORADDR(20); break; |
|
||||
case FSMC_RS_A20: _ORADDR(21); break; |
|
||||
case FSMC_RS_A21: _ORADDR(22); break; |
|
||||
case FSMC_RS_A22: _ORADDR(23); break; |
|
||||
case FSMC_RS_A23: _ORADDR(24); break; |
|
||||
#if ENABLED(STM32_XL_DENSITY) |
|
||||
case FSMC_RS_A24: _ORADDR(25); break; |
|
||||
case FSMC_RS_A25: _ORADDR(26); break; |
|
||||
#endif |
|
||||
default: return; |
|
||||
} |
|
||||
|
|
||||
rcc_clk_enable(RCC_FSMC); |
|
||||
|
|
||||
gpio_set_mode(GPIOD, 14, GPIO_AF_OUTPUT_PP); // FSMC_D00
|
|
||||
gpio_set_mode(GPIOD, 15, GPIO_AF_OUTPUT_PP); // FSMC_D01
|
|
||||
gpio_set_mode(GPIOD, 0, GPIO_AF_OUTPUT_PP); // FSMC_D02
|
|
||||
gpio_set_mode(GPIOD, 1, GPIO_AF_OUTPUT_PP); // FSMC_D03
|
|
||||
gpio_set_mode(GPIOE, 7, GPIO_AF_OUTPUT_PP); // FSMC_D04
|
|
||||
gpio_set_mode(GPIOE, 8, GPIO_AF_OUTPUT_PP); // FSMC_D05
|
|
||||
gpio_set_mode(GPIOE, 9, GPIO_AF_OUTPUT_PP); // FSMC_D06
|
|
||||
gpio_set_mode(GPIOE, 10, GPIO_AF_OUTPUT_PP); // FSMC_D07
|
|
||||
gpio_set_mode(GPIOE, 11, GPIO_AF_OUTPUT_PP); // FSMC_D08
|
|
||||
gpio_set_mode(GPIOE, 12, GPIO_AF_OUTPUT_PP); // FSMC_D09
|
|
||||
gpio_set_mode(GPIOE, 13, GPIO_AF_OUTPUT_PP); // FSMC_D10
|
|
||||
gpio_set_mode(GPIOE, 14, GPIO_AF_OUTPUT_PP); // FSMC_D11
|
|
||||
gpio_set_mode(GPIOE, 15, GPIO_AF_OUTPUT_PP); // FSMC_D12
|
|
||||
gpio_set_mode(GPIOD, 8, GPIO_AF_OUTPUT_PP); // FSMC_D13
|
|
||||
gpio_set_mode(GPIOD, 9, GPIO_AF_OUTPUT_PP); // FSMC_D14
|
|
||||
gpio_set_mode(GPIOD, 10, GPIO_AF_OUTPUT_PP); // FSMC_D15
|
|
||||
|
|
||||
gpio_set_mode(GPIOD, 4, GPIO_AF_OUTPUT_PP); // FSMC_NOE
|
|
||||
gpio_set_mode(GPIOD, 5, GPIO_AF_OUTPUT_PP); // FSMC_NWE
|
|
||||
|
|
||||
gpio_set_mode(PIN_MAP[cs].gpio_device, PIN_MAP[cs].gpio_bit, GPIO_AF_OUTPUT_PP); //FSMC_CS_NEx
|
|
||||
gpio_set_mode(PIN_MAP[rs].gpio_device, PIN_MAP[rs].gpio_bit, GPIO_AF_OUTPUT_PP); //FSMC_RS_Ax
|
|
||||
|
|
||||
fsmcPsramRegion->BCR = FSMC_BCR_WREN | FSMC_BCR_MTYP_SRAM | FSMC_BCR_MWID_16BITS | FSMC_BCR_MBKEN; |
|
||||
fsmcPsramRegion->BTR = (FSMC_DATA_SETUP_TIME << 8) | FSMC_ADDRESS_SETUP_TIME; |
|
||||
|
|
||||
afio_remap(AFIO_REMAP_FSMC_NADV); |
|
||||
|
|
||||
LCD = (LCD_CONTROLLER_TypeDef*)controllerAddress; |
|
||||
} |
|
||||
|
|
||||
void LCD_IO_WriteData(uint16_t RegValue) { |
|
||||
LCD->RAM = RegValue; |
|
||||
__DSB(); |
|
||||
} |
|
||||
|
|
||||
void LCD_IO_WriteReg(uint16_t Reg) { |
|
||||
LCD->REG = Reg; |
|
||||
__DSB(); |
|
||||
} |
|
||||
|
|
||||
uint16_t LCD_IO_ReadData(uint16_t RegValue) { |
|
||||
LCD->REG = RegValue; |
|
||||
__DSB(); |
|
||||
|
|
||||
return LCD->RAM; |
|
||||
} |
|
||||
|
|
||||
uint32_t LCD_IO_ReadData(uint16_t RegValue, uint8_t ReadSize) { |
|
||||
volatile uint32_t data; |
|
||||
LCD->REG = RegValue; |
|
||||
__DSB(); |
|
||||
|
|
||||
data = LCD->RAM; // dummy read
|
|
||||
data = LCD->RAM & 0x00FF; |
|
||||
|
|
||||
while (--ReadSize) { |
|
||||
data <<= 8; |
|
||||
data |= (LCD->RAM & 0x00FF); |
|
||||
} |
|
||||
return uint32_t(data); |
|
||||
} |
|
||||
|
|
||||
#ifdef LCD_USE_DMA_FSMC |
|
||||
|
|
||||
void LCD_IO_WriteMultiple(uint16_t color, uint32_t count) { |
|
||||
while (count > 0) { |
|
||||
dma_setup_transfer(FSMC_DMA_DEV, FSMC_DMA_CHANNEL, &color, DMA_SIZE_16BITS, &LCD->RAM, DMA_SIZE_16BITS, DMA_MEM_2_MEM); |
|
||||
dma_set_num_transfers(FSMC_DMA_DEV, FSMC_DMA_CHANNEL, count > 65535 ? 65535 : count); |
|
||||
dma_clear_isr_bits(FSMC_DMA_DEV, FSMC_DMA_CHANNEL); |
|
||||
dma_enable(FSMC_DMA_DEV, FSMC_DMA_CHANNEL); |
|
||||
|
|
||||
while ((dma_get_isr_bits(FSMC_DMA_DEV, FSMC_DMA_CHANNEL) & 0x0A) == 0) {}; |
|
||||
dma_disable(FSMC_DMA_DEV, FSMC_DMA_CHANNEL); |
|
||||
|
|
||||
count = count > 65535 ? count - 65535 : 0; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
void LCD_IO_WriteSequence(uint16_t *data, uint16_t length) { |
|
||||
dma_setup_transfer(FSMC_DMA_DEV, FSMC_DMA_CHANNEL, data, DMA_SIZE_16BITS, &LCD->RAM, DMA_SIZE_16BITS, DMA_MEM_2_MEM | DMA_PINC_MODE); |
|
||||
dma_set_num_transfers(FSMC_DMA_DEV, FSMC_DMA_CHANNEL, length); |
|
||||
dma_clear_isr_bits(FSMC_DMA_DEV, FSMC_DMA_CHANNEL); |
|
||||
dma_enable(FSMC_DMA_DEV, FSMC_DMA_CHANNEL); |
|
||||
|
|
||||
while ((dma_get_isr_bits(FSMC_DMA_DEV, FSMC_DMA_CHANNEL) & 0x0A) == 0) {}; |
|
||||
dma_disable(FSMC_DMA_DEV, FSMC_DMA_CHANNEL); |
|
||||
} |
|
||||
|
|
||||
void LCD_IO_WriteSequence_Async(uint16_t *data, uint16_t length) { |
|
||||
dma_setup_transfer(FSMC_DMA_DEV, FSMC_DMA_CHANNEL, data, DMA_SIZE_16BITS, &LCD->RAM, DMA_SIZE_16BITS, DMA_MEM_2_MEM | DMA_PINC_MODE); |
|
||||
dma_set_num_transfers(FSMC_DMA_DEV, FSMC_DMA_CHANNEL, length); |
|
||||
dma_clear_isr_bits(FSMC_DMA_DEV, FSMC_DMA_CHANNEL); |
|
||||
dma_enable(FSMC_DMA_DEV, FSMC_DMA_CHANNEL); |
|
||||
} |
|
||||
|
|
||||
void LCD_IO_WaitSequence_Async() { |
|
||||
while ((dma_get_isr_bits(FSMC_DMA_DEV, FSMC_DMA_CHANNEL) & 0x0A) == 0) {}; |
|
||||
dma_disable(FSMC_DMA_DEV, FSMC_DMA_CHANNEL); |
|
||||
} |
|
||||
|
|
||||
#endif // LCD_USE_DMA_FSMC
|
|
||||
|
|
||||
#endif // HAS_GRAPHICAL_LCD
|
|
||||
#endif // ARDUINO_ARCH_STM32F1 && FSMC_CS_PIN
|
|
@ -1,236 +0,0 @@ |
|||||
/**
|
|
||||
* Marlin 3D Printer Firmware |
|
||||
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
|
||||
* |
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
|
||||
* |
|
||||
*/ |
|
||||
#ifdef __STM32F1__ |
|
||||
|
|
||||
#include "../../../inc/MarlinConfig.h" |
|
||||
|
|
||||
#if ENABLED(SPI_GRAPHICAL_TFT) && DISABLED(FORCE_SOFT_SPI) |
|
||||
|
|
||||
#include "../HAL.h" |
|
||||
#include <U8glib.h> |
|
||||
#include <SPI.h> |
|
||||
|
|
||||
#define SPI_TFT_CS_H OUT_WRITE(SPI_TFT_CS_PIN, HIGH) |
|
||||
#define SPI_TFT_CS_L OUT_WRITE(SPI_TFT_CS_PIN, LOW) |
|
||||
|
|
||||
#define SPI_TFT_DC_H OUT_WRITE(SPI_TFT_DC_PIN, HIGH) |
|
||||
#define SPI_TFT_DC_L OUT_WRITE(SPI_TFT_DC_PIN, LOW) |
|
||||
|
|
||||
#define SPI_TFT_RST_H OUT_WRITE(SPI_TFT_RST_PIN, HIGH) |
|
||||
#define SPI_TFT_RST_L OUT_WRITE(SPI_TFT_RST_PIN, LOW) |
|
||||
|
|
||||
#define SPI_TFT_BLK_H OUT_WRITE(LCD_BACKLIGHT_PIN, HIGH) |
|
||||
#define SPI_TFT_BLK_L OUT_WRITE(LCD_BACKLIGHT_PIN, LOW) |
|
||||
|
|
||||
void LCD_IO_Init(uint8_t cs, uint8_t rs); |
|
||||
void LCD_IO_WriteData(uint16_t RegValue); |
|
||||
void LCD_IO_WriteReg(uint16_t Reg); |
|
||||
uint16_t LCD_IO_ReadData(uint16_t RegValue); |
|
||||
uint32_t LCD_IO_ReadData(uint16_t RegValue, uint8_t ReadSize); |
|
||||
#ifdef LCD_USE_DMA_SPI |
|
||||
void LCD_IO_WriteMultiple(uint16_t data, uint32_t count); |
|
||||
void LCD_IO_WriteSequence(uint16_t *data, uint16_t length); |
|
||||
#endif |
|
||||
|
|
||||
void LCD_WR_REG(uint8_t cmd) { |
|
||||
SPI_TFT_CS_L; |
|
||||
SPI_TFT_DC_L; |
|
||||
SPI.send(cmd); |
|
||||
SPI_TFT_CS_H; |
|
||||
} |
|
||||
void LCD_WR_DATA(uint8_t data) { |
|
||||
SPI_TFT_CS_L; |
|
||||
SPI_TFT_DC_H; |
|
||||
SPI.send(data); |
|
||||
SPI_TFT_CS_H; |
|
||||
} |
|
||||
|
|
||||
void spi1Init(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); |
|
||||
} |
|
||||
|
|
||||
void LCD_IO_Init(uint8_t cs, uint8_t rs) { |
|
||||
spi1Init(SPI_FULL_SPEED); |
|
||||
} |
|
||||
|
|
||||
void LCD_IO_WriteData(uint16_t RegValue) { |
|
||||
LCD_WR_DATA(RegValue); |
|
||||
} |
|
||||
|
|
||||
void LCD_IO_WriteReg(uint16_t Reg) { |
|
||||
LCD_WR_REG(Reg); |
|
||||
} |
|
||||
|
|
||||
uint16_t LCD_IO_ReadData(uint16_t RegValue) { |
|
||||
uint16_t d = 0; |
|
||||
SPI_TFT_CS_L; |
|
||||
|
|
||||
SPI_TFT_DC_L; |
|
||||
SPI.send(RegValue); |
|
||||
SPI_TFT_DC_H; |
|
||||
|
|
||||
SPI.read((uint8_t*)&d, 1); //dummy read
|
|
||||
SPI.read((uint8_t*)&d, 1); |
|
||||
|
|
||||
SPI_TFT_CS_H; |
|
||||
return d >> 7; |
|
||||
} |
|
||||
|
|
||||
uint32_t LCD_IO_ReadData(uint16_t RegValue, uint8_t ReadSize) { |
|
||||
uint32_t data = 0; |
|
||||
uint8_t d = 0; |
|
||||
SPI_TFT_CS_L; |
|
||||
|
|
||||
SPI_TFT_DC_L; |
|
||||
SPI.send(RegValue); |
|
||||
SPI_TFT_DC_H; |
|
||||
|
|
||||
SPI.read((uint8_t*)&d, 1); //dummy read
|
|
||||
SPI.read((uint8_t*)&d, 1); |
|
||||
data = d; |
|
||||
while (--ReadSize) { |
|
||||
data <<= 8; |
|
||||
SPI.read((uint8_t*)&d, 1); |
|
||||
data |= (d & 0xFF); |
|
||||
} |
|
||||
|
|
||||
SPI_TFT_CS_H; |
|
||||
return uint32_t(data >> 7); |
|
||||
} |
|
||||
|
|
||||
#ifdef LCD_USE_DMA_SPI |
|
||||
void LCD_IO_WriteMultiple(uint16_t data, uint32_t count) { |
|
||||
if (SPI.getDataSize() == DATA_SIZE_8BIT) { |
|
||||
count *= 2; |
|
||||
} |
|
||||
while (count > 0) { |
|
||||
SPI_TFT_CS_L; |
|
||||
SPI_TFT_DC_H; |
|
||||
SPI.dmaSend(&data, 1, true); |
|
||||
SPI_TFT_CS_H; |
|
||||
count--; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
void LCD_IO_WriteSequence(uint16_t *data, uint16_t length) { |
|
||||
if (SPI.getDataSize() == DATA_SIZE_8BIT) { |
|
||||
length *= 2; |
|
||||
} |
|
||||
SPI_TFT_CS_L; |
|
||||
SPI_TFT_DC_H; |
|
||||
SPI.dmaSend(data, length, true); |
|
||||
SPI_TFT_CS_H; |
|
||||
} |
|
||||
|
|
||||
void LCD_IO_WriteSequence_Async(uint16_t *data, uint16_t length) { |
|
||||
if (SPI.getDataSize() == DATA_SIZE_8BIT) { |
|
||||
length *= 2; |
|
||||
} |
|
||||
SPI_TFT_CS_L; |
|
||||
SPI_TFT_DC_H; |
|
||||
SPI.dmaSendAsync(data, length, true); |
|
||||
SPI_TFT_CS_H; |
|
||||
} |
|
||||
|
|
||||
void LCD_IO_WaitSequence_Async() { |
|
||||
SPI_TFT_CS_L; |
|
||||
SPI_TFT_DC_H; |
|
||||
SPI.dmaSendAsync(NULL, 0, true); |
|
||||
SPI_TFT_CS_H; |
|
||||
} |
|
||||
#endif |
|
||||
|
|
||||
static uint8_t msgInitCount = 2; // Ignore all messages until 2nd U8G_COM_MSG_INIT
|
|
||||
|
|
||||
#ifndef LCD_READ_ID |
|
||||
#define LCD_READ_ID 0x04 // Read display identification information (0xD3 on ILI9341)
|
|
||||
#endif |
|
||||
|
|
||||
uint8_t u8g_com_stm32duino_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) { |
|
||||
if (msgInitCount) { |
|
||||
if (msg == U8G_COM_MSG_INIT) msgInitCount--; |
|
||||
if (msgInitCount) return -1; |
|
||||
} |
|
||||
|
|
||||
static uint8_t isCommand; |
|
||||
|
|
||||
LCD_IO_Init(-1, -1); |
|
||||
|
|
||||
switch (msg) { |
|
||||
case U8G_COM_MSG_STOP: break; |
|
||||
case U8G_COM_MSG_INIT: |
|
||||
u8g_SetPIOutput(u8g, U8G_PI_RESET); |
|
||||
|
|
||||
u8g_Delay(50); |
|
||||
|
|
||||
if (arg_ptr) { |
|
||||
spi1Init(SPI_EIGHTH_SPEED); |
|
||||
*((uint32_t *)arg_ptr) = (LCD_READ_ID << 24) | LCD_IO_ReadData(LCD_READ_ID, 3); |
|
||||
spi1Init(SPI_FULL_SPEED); |
|
||||
} |
|
||||
isCommand = 0; |
|
||||
break; |
|
||||
|
|
||||
case U8G_COM_MSG_ADDRESS: // define cmd (arg_val = 0) or data mode (arg_val = 1)
|
|
||||
isCommand = arg_val == 0 ? 1 : 0; |
|
||||
break; |
|
||||
|
|
||||
case U8G_COM_MSG_RESET: |
|
||||
u8g_SetPILevel(u8g, U8G_PI_RESET, arg_val); |
|
||||
break; |
|
||||
|
|
||||
case U8G_COM_MSG_WRITE_BYTE: |
|
||||
if (isCommand) |
|
||||
LCD_IO_WriteReg(arg_val); |
|
||||
else |
|
||||
LCD_IO_WriteData((uint16_t)arg_val); |
|
||||
break; |
|
||||
|
|
||||
case U8G_COM_MSG_WRITE_SEQ: |
|
||||
for (uint8_t i = 0; i < arg_val; i += 2) |
|
||||
LCD_IO_WriteData(*(uint16_t *)(((uint32_t)arg_ptr) + i)); |
|
||||
break; |
|
||||
|
|
||||
} |
|
||||
return 1; |
|
||||
} |
|
||||
|
|
||||
#endif // SPI_GRAPHICAL_TFT && !FORCE_SOFT_SPI
|
|
||||
#endif // STM32F1
|
|
@ -0,0 +1,112 @@ |
|||||
|
/**
|
||||
|
* Marlin 3D Printer Firmware |
||||
|
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
||||
|
* |
||||
|
* 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 <https://www.gnu.org/licenses/>.
|
||||
|
* |
||||
|
*/ |
||||
|
|
||||
|
#include "../../inc/MarlinConfig.h" |
||||
|
|
||||
|
#if HAS_TOUCH_XPT2046 |
||||
|
|
||||
|
#include "touch_buttons.h" |
||||
|
#include "../scaled_tft.h" |
||||
|
|
||||
|
#include HAL_PATH(../../HAL, tft/xpt2046.h) |
||||
|
XPT2046 touchIO; |
||||
|
|
||||
|
#include "../../lcd/ultralcd.h" // For EN_C bit mask |
||||
|
|
||||
|
/**
|
||||
|
* Draw and Touch processing |
||||
|
* |
||||
|
* LCD_PIXEL_WIDTH/HEIGHT (128x64) is the (emulated DOGM) Pixel Drawing resolution. |
||||
|
* TOUCH_SENSOR_WIDTH/HEIGHT (320x240) is the Touch Area resolution. |
||||
|
* TFT_WIDTH/HEIGHT (320x240 or 480x320) is the Actual (FSMC) Display resolution. |
||||
|
* |
||||
|
* - All native (u8g) drawing is done in LCD_PIXEL_* (128x64) |
||||
|
* - The DOGM pixels are is upscaled 2-3x (as needed) for display. |
||||
|
* - Touch coordinates use TOUCH_SENSOR_* resolution and are converted to |
||||
|
* click and scroll-wheel events (emulating of a common DOGM display). |
||||
|
* |
||||
|
* TOUCH_SCREEN resolution exists to fit our calibration values. The original touch code was made |
||||
|
* and originally calibrated for 320x240. If you decide to change the resolution of the touch code, |
||||
|
* new calibration values will be needed. |
||||
|
* |
||||
|
* The Marlin menus are drawn scaled in the upper region of the screen. The bottom region (in a |
||||
|
* fixed location in TOUCH_SCREEN* coordinate space) is used for 4 general-purpose buttons to |
||||
|
* navigate and select menu items. Both regions are touchable. |
||||
|
* |
||||
|
* The Marlin screen touchable area starts at TFT_PIXEL_OFFSET_X/Y (translated to SCREEN_PCT_LEFT/TOP) |
||||
|
* and spans LCD_PIXEL_WIDTH/HEIGHT (scaled to SCREEN_PCT_WIDTH/HEIGHT). |
||||
|
*/ |
||||
|
|
||||
|
// Touch sensor resolution independent of display resolution
|
||||
|
#define TOUCH_SENSOR_WIDTH 320 |
||||
|
#define TOUCH_SENSOR_HEIGHT 240 |
||||
|
|
||||
|
#define SCREEN_PCT_WIDE(X) ((X) * (TOUCH_SENSOR_WIDTH) / (TFT_WIDTH)) |
||||
|
#define SCREEN_PCT_HIGH(Y) ((Y) * (TOUCH_SENSOR_HEIGHT) / (TFT_HEIGHT)) |
||||
|
|
||||
|
#define SCREEN_PCT_LEFT SCREEN_PCT_WIDE(TFT_PIXEL_OFFSET_X) |
||||
|
#define SCREEN_PCT_TOP SCREEN_PCT_HIGH(TFT_PIXEL_OFFSET_Y) |
||||
|
#define SCREEN_PCT_WIDTH SCREEN_PCT_WIDE((GRAPHICAL_TFT_UPSCALE) * (LCD_PIXEL_WIDTH)) |
||||
|
#define SCREEN_PCT_HEIGHT SCREEN_PCT_HIGH((GRAPHICAL_TFT_UPSCALE) * (LCD_PIXEL_HEIGHT)) |
||||
|
|
||||
|
// Coordinates in terms of 240-unit-tall touch area
|
||||
|
#define BUTTON_AREA_TOP 175 |
||||
|
#define BUTTON_AREA_BOT 234 |
||||
|
|
||||
|
TouchButtons touch; |
||||
|
|
||||
|
void TouchButtons::init() { touchIO.Init(); } |
||||
|
|
||||
|
uint8_t TouchButtons::read_buttons() { |
||||
|
#ifdef HAS_SPI_LCD |
||||
|
int16_t x, y; |
||||
|
|
||||
|
if (!touchIO.getRawPoint(&x, &y)) return 0; |
||||
|
|
||||
|
x = uint16_t((uint32_t(x) * XPT2046_X_CALIBRATION) >> 16) + XPT2046_X_OFFSET; |
||||
|
y = uint16_t((uint32_t(y) * XPT2046_Y_CALIBRATION) >> 16) + XPT2046_Y_OFFSET; |
||||
|
|
||||
|
#if ENABLED(GRAPHICAL_TFT_ROTATE_180) |
||||
|
x = TOUCH_SENSOR_WIDTH - x; |
||||
|
y = TOUCH_SENSOR_HEIGHT - y; |
||||
|
#endif |
||||
|
|
||||
|
// Touch within the button area simulates an encoder button
|
||||
|
if (y > BUTTON_AREA_TOP && y < BUTTON_AREA_BOT) |
||||
|
return WITHIN(x, 14, 77) ? EN_D |
||||
|
: WITHIN(x, 90, 153) ? EN_A |
||||
|
: WITHIN(x, 166, 229) ? EN_B |
||||
|
: WITHIN(x, 242, 305) ? EN_C |
||||
|
: 0; |
||||
|
|
||||
|
if ( !WITHIN(x, SCREEN_PCT_LEFT, SCREEN_PCT_LEFT + SCREEN_PCT_WIDTH) |
||||
|
|| !WITHIN(y, SCREEN_PCT_TOP, SCREEN_PCT_TOP + SCREEN_PCT_HEIGHT) |
||||
|
) return 0; |
||||
|
|
||||
|
// Column and row above BUTTON_AREA_TOP
|
||||
|
int8_t col = (x - (SCREEN_PCT_LEFT)) * (LCD_WIDTH) / (SCREEN_PCT_WIDTH), |
||||
|
row = (y - (SCREEN_PCT_TOP)) * (LCD_HEIGHT) / (SCREEN_PCT_HEIGHT); |
||||
|
|
||||
|
// Send the touch to the UI (which will simulate the encoder wheel)
|
||||
|
MarlinUI::screen_click(row, col, x, y); |
||||
|
#endif |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
#endif // HAS_TOUCH_XPT2046
|
@ -1,251 +0,0 @@ |
|||||
/**
|
|
||||
* Marlin 3D Printer Firmware |
|
||||
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
|
||||
* |
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
|
||||
* |
|
||||
*/ |
|
||||
|
|
||||
#include "../../inc/MarlinConfig.h" |
|
||||
|
|
||||
#if HAS_TOUCH_XPT2046 |
|
||||
|
|
||||
#include "xpt2046.h" |
|
||||
#include "../scaled_tft.h" |
|
||||
|
|
||||
#ifndef XPT2046_Z1_THRESHOLD |
|
||||
#define XPT2046_Z1_THRESHOLD 10 |
|
||||
#endif |
|
||||
|
|
||||
/**
|
|
||||
* Draw and Touch processing |
|
||||
* |
|
||||
* LCD_PIXEL_WIDTH/HEIGHT (128x64) is the (emulated DOGM) Pixel Drawing resolution. |
|
||||
* TOUCH_SENSOR_WIDTH/HEIGHT (320x240) is the Touch Area resolution. |
|
||||
* LCD_FULL_PIXEL_WIDTH/HEIGHT (320x240 or 480x320) is the Actual (FSMC) Display resolution. |
|
||||
* |
|
||||
* - All native (u8g) drawing is done in LCD_PIXEL_* (128x64) |
|
||||
* - The DOGM pixels are is upscaled 2-3x (as needed) for display. |
|
||||
* - Touch coordinates use TOUCH_SENSOR_* resolution and are converted to |
|
||||
* click and scroll-wheel events (emulating of a common DOGM display). |
|
||||
* |
|
||||
* TOUCH_SCREEN resolution exists to fit our calibration values. The original touch code was made |
|
||||
* and originally calibrated for 320x240. If you decide to change the resolution of the touch code, |
|
||||
* new calibration values will be needed. |
|
||||
* |
|
||||
* The Marlin menus are drawn scaled in the upper region of the screen. The bottom region (in a |
|
||||
* fixed location in TOUCH_SCREEN* coordinate space) is used for 4 general-purpose buttons to |
|
||||
* navigate and select menu items. Both regions are touchable. |
|
||||
* |
|
||||
* The Marlin screen touchable area starts at LCD_PIXEL_OFFSET_X/Y (translated to SCREEN_START_LEFT/TOP) |
|
||||
* and spans LCD_PIXEL_WIDTH/HEIGHT (scaled to SCREEN_WIDTH/HEIGHT). |
|
||||
*/ |
|
||||
|
|
||||
// Coordinates in terms of touch area
|
|
||||
#define BUTTON_AREA_TOP 175 |
|
||||
#define BUTTON_AREA_BOT 234 |
|
||||
|
|
||||
// Touch sensor resolution independent of display resolution
|
|
||||
#define TOUCH_SENSOR_WIDTH 320 |
|
||||
#define TOUCH_SENSOR_HEIGHT 240 |
|
||||
|
|
||||
#define SCREEN_WIDTH_PCT(X) ((X) * (TOUCH_SENSOR_WIDTH) / (LCD_FULL_PIXEL_WIDTH)) |
|
||||
#define SCREEN_HEIGHT_PCT(Y) ((Y) * (TOUCH_SENSOR_HEIGHT) / (LCD_FULL_PIXEL_HEIGHT)) |
|
||||
|
|
||||
#define SCREEN_START_LEFT SCREEN_WIDTH_PCT(LCD_PIXEL_OFFSET_X) |
|
||||
#define SCREEN_START_TOP SCREEN_HEIGHT_PCT(LCD_PIXEL_OFFSET_Y) |
|
||||
#define SCREEN_WIDTH SCREEN_WIDTH_PCT((LCD_PIXEL_WIDTH) * (FSMC_UPSCALE)) |
|
||||
#define SCREEN_HEIGHT SCREEN_HEIGHT_PCT((LCD_PIXEL_HEIGHT) * (FSMC_UPSCALE)) |
|
||||
|
|
||||
#define TOUCHABLE_X_WIDTH SCREEN_WIDTH |
|
||||
#define TOUCHABLE_Y_HEIGHT SCREEN_HEIGHT |
|
||||
|
|
||||
#ifndef TOUCH_INT_PIN |
|
||||
#define TOUCH_INT_PIN -1 |
|
||||
#endif |
|
||||
#ifndef TOUCH_MISO_PIN |
|
||||
#define TOUCH_MISO_PIN MISO_PIN |
|
||||
#endif |
|
||||
#ifndef TOUCH_MOSI_PIN |
|
||||
#define TOUCH_MOSI_PIN MOSI_PIN |
|
||||
#endif |
|
||||
#ifndef TOUCH_SCK_PIN |
|
||||
#define TOUCH_SCK_PIN SCK_PIN |
|
||||
#endif |
|
||||
#ifndef TOUCH_CS_PIN |
|
||||
#define TOUCH_CS_PIN CS_PIN |
|
||||
#endif |
|
||||
|
|
||||
XPT2046 touch; |
|
||||
|
|
||||
void XPT2046::init() { |
|
||||
SET_INPUT(TOUCH_MISO_PIN); |
|
||||
SET_OUTPUT(TOUCH_MOSI_PIN); |
|
||||
SET_OUTPUT(TOUCH_SCK_PIN); |
|
||||
OUT_WRITE(TOUCH_CS_PIN, HIGH); |
|
||||
|
|
||||
#if PIN_EXISTS(TOUCH_INT) |
|
||||
// Optional Pendrive interrupt pin
|
|
||||
SET_INPUT(TOUCH_INT_PIN); |
|
||||
#endif |
|
||||
|
|
||||
// Read once to enable pendrive status pin
|
|
||||
getInTouch(XPT2046_X); |
|
||||
} |
|
||||
|
|
||||
#include "../../lcd/ultralcd.h" // For EN_C bit mask |
|
||||
|
|
||||
uint8_t XPT2046::read_buttons() { |
|
||||
#ifdef HAS_SPI_LCD |
|
||||
int16_t tsoffsets[4] = { 0 }; |
|
||||
|
|
||||
if (tsoffsets[0] + tsoffsets[1] == 0) { |
|
||||
// Not yet set, so use defines as fallback...
|
|
||||
tsoffsets[0] = XPT2046_X_CALIBRATION; |
|
||||
tsoffsets[1] = XPT2046_X_OFFSET; |
|
||||
tsoffsets[2] = XPT2046_Y_CALIBRATION; |
|
||||
tsoffsets[3] = XPT2046_Y_OFFSET; |
|
||||
} |
|
||||
|
|
||||
// We rely on XPT2046 compatible mode to ADS7843, hence no Z1 and Z2 measurements possible.
|
|
||||
|
|
||||
if (!isTouched()) return 0; |
|
||||
uint16_t x = uint16_t(((uint32_t(getInTouch(XPT2046_X))) * tsoffsets[0]) >> 16) + tsoffsets[1], |
|
||||
y = uint16_t(((uint32_t(getInTouch(XPT2046_Y))) * tsoffsets[2]) >> 16) + tsoffsets[3]; |
|
||||
if (!isTouched()) return 0; // Fingers must still be on the TS for a valid read.
|
|
||||
|
|
||||
#if ENABLED(GRAPHICAL_TFT_ROTATE_180) |
|
||||
x = TOUCH_SENSOR_WIDTH - x; |
|
||||
y = TOUCH_SENSOR_HEIGHT - y; |
|
||||
#endif |
|
||||
|
|
||||
// Touch within the button area simulates an encoder button
|
|
||||
if (y > BUTTON_AREA_TOP && y < BUTTON_AREA_BOT) |
|
||||
return WITHIN(x, 14, 77) ? EN_D |
|
||||
: WITHIN(x, 90, 153) ? EN_A |
|
||||
: WITHIN(x, 166, 229) ? EN_B |
|
||||
: WITHIN(x, 242, 305) ? EN_C |
|
||||
: 0; |
|
||||
|
|
||||
if ( !WITHIN(x, SCREEN_START_LEFT, SCREEN_START_LEFT + SCREEN_WIDTH) |
|
||||
|| !WITHIN(y, SCREEN_START_TOP, SCREEN_START_TOP + SCREEN_HEIGHT) |
|
||||
) return 0; |
|
||||
|
|
||||
// Column and row above BUTTON_AREA_TOP
|
|
||||
int8_t col = (x - (SCREEN_START_LEFT)) * (LCD_WIDTH) / (TOUCHABLE_X_WIDTH), |
|
||||
row = (y - (SCREEN_START_TOP)) * (LCD_HEIGHT) / (TOUCHABLE_Y_HEIGHT); |
|
||||
|
|
||||
// Send the touch to the UI (which will simulate the encoder wheel)
|
|
||||
MarlinUI::screen_click(row, col, x, y); |
|
||||
#endif |
|
||||
return 0; |
|
||||
} |
|
||||
|
|
||||
bool XPT2046::isTouched() { |
|
||||
return ( |
|
||||
#if PIN_EXISTS(TOUCH_INT) |
|
||||
READ(TOUCH_INT_PIN) != HIGH |
|
||||
#else |
|
||||
getInTouch(XPT2046_Z1) >= XPT2046_Z1_THRESHOLD |
|
||||
#endif |
|
||||
); |
|
||||
} |
|
||||
|
|
||||
#if ENABLED(TOUCH_BUTTONS_HW_SPI) |
|
||||
|
|
||||
#include <SPI.h> |
|
||||
|
|
||||
static void touch_spi_init(uint8_t spiRate) { |
|
||||
/**
|
|
||||
* 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(TOUCH_BUTTONS_HW_SPI_DEVICE); |
|
||||
SPI.begin(); |
|
||||
SPI.setClockDivider(clock); |
|
||||
SPI.setBitOrder(MSBFIRST); |
|
||||
SPI.setDataMode(SPI_MODE0); |
|
||||
} |
|
||||
#endif // TOUCH_BUTTONS_HW_SPI
|
|
||||
|
|
||||
uint16_t XPT2046::getInTouch(const XPTCoordinate coordinate) { |
|
||||
uint16_t data[3]; |
|
||||
const uint8_t coord = uint8_t(coordinate) | XPT2046_CONTROL | XPT2046_DFR_MODE; |
|
||||
|
|
||||
#if ENABLED(TOUCH_BUTTONS_HW_SPI) |
|
||||
|
|
||||
touch_spi_init(SPI_SPEED_6); |
|
||||
for (uint16_t i = 0; i < 3; i++) { |
|
||||
OUT_WRITE(TOUCH_CS_PIN, LOW); |
|
||||
SPI.transfer(coord); |
|
||||
data[i] = (((SPI.transfer(0xFF) << 8) | SPI.transfer(0xFF)) >> 3) & 0x0FFF; |
|
||||
WRITE(TOUCH_CS_PIN, HIGH); |
|
||||
} |
|
||||
|
|
||||
#else // !TOUCH_BUTTONS_HW_SPI
|
|
||||
|
|
||||
OUT_WRITE(TOUCH_CS_PIN, LOW); |
|
||||
for (uint16_t i = 0; i < 3; i++) { |
|
||||
for (uint8_t j = 0x80; j; j >>= 1) { |
|
||||
WRITE(TOUCH_SCK_PIN, LOW); |
|
||||
WRITE(TOUCH_MOSI_PIN, bool(coord & j)); |
|
||||
WRITE(TOUCH_SCK_PIN, HIGH); |
|
||||
} |
|
||||
|
|
||||
data[i] = 0; |
|
||||
for (uint16_t j = 0x8000; j; j >>= 1) { |
|
||||
WRITE(TOUCH_SCK_PIN, LOW); |
|
||||
if (READ(TOUCH_MISO_PIN)) data[i] |= j; |
|
||||
WRITE(TOUCH_SCK_PIN, HIGH); |
|
||||
} |
|
||||
WRITE(TOUCH_SCK_PIN, LOW); |
|
||||
data[i] >>= 4; |
|
||||
} |
|
||||
WRITE(TOUCH_CS_PIN, HIGH); |
|
||||
|
|
||||
#endif // !TOUCH_BUTTONS_HW_SPI
|
|
||||
|
|
||||
uint16_t delta01 = _MAX(data[0], data[1]) - _MIN(data[0], data[1]), |
|
||||
delta02 = _MAX(data[0], data[2]) - _MIN(data[0], data[2]), |
|
||||
delta12 = _MAX(data[1], data[2]) - _MIN(data[1], data[2]); |
|
||||
|
|
||||
if (delta01 <= delta02 && delta01 <= delta12) |
|
||||
return (data[0] + data[1]) >> 1; |
|
||||
|
|
||||
if (delta02 <= delta12) |
|
||||
return (data[0] + data[2]) >> 1; |
|
||||
|
|
||||
return (data[1] + data[2]) >> 1; |
|
||||
} |
|
||||
|
|
||||
bool XPT2046::getTouchPoint(uint16_t &x, uint16_t &y) { |
|
||||
if (isTouched()) { |
|
||||
x = getInTouch(XPT2046_X); |
|
||||
y = getInTouch(XPT2046_Y); |
|
||||
} |
|
||||
return isTouched(); |
|
||||
} |
|
||||
|
|
||||
#endif // HAS_TOUCH_XPT2046
|
|
Loading…
Reference in new issue