Sergey
4 years ago
439 changed files with 19946 additions and 6403 deletions
@ -0,0 +1,153 @@ |
|||
/**
|
|||
* 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/>.
|
|||
* |
|||
*/ |
|||
|
|||
#include "../../../inc/MarlinConfig.h" |
|||
|
|||
#if HAS_SPI_TFT |
|||
|
|||
#include "tft_spi.h" |
|||
|
|||
//TFT_SPI tft;
|
|||
|
|||
SPIClass TFT_SPI::SPIx(1); |
|||
|
|||
#define TFT_CS_H WRITE(TFT_CS_PIN, HIGH) |
|||
#define TFT_CS_L WRITE(TFT_CS_PIN, LOW) |
|||
|
|||
#define TFT_DC_H WRITE(TFT_DC_PIN, HIGH) |
|||
#define TFT_DC_L WRITE(TFT_DC_PIN, LOW) |
|||
|
|||
#define TFT_RST_H WRITE(TFT_RESET_PIN, HIGH) |
|||
#define TFT_RST_L WRITE(TFT_RESET_PIN, LOW) |
|||
|
|||
#define TFT_BLK_H WRITE(TFT_BACKLIGHT_PIN, HIGH) |
|||
#define TFT_BLK_L WRITE(TFT_BACKLIGHT_PIN, LOW) |
|||
|
|||
void TFT_SPI::Init() { |
|||
#if PIN_EXISTS(TFT_RESET) |
|||
SET_OUTPUT(TFT_RESET_PIN); |
|||
TFT_RST_H; |
|||
delay(100); |
|||
#endif |
|||
|
|||
#if PIN_EXISTS(TFT_BACKLIGHT) |
|||
SET_OUTPUT(TFT_BACKLIGHT_PIN); |
|||
TFT_BLK_H; |
|||
#endif |
|||
|
|||
SET_OUTPUT(TFT_DC_PIN); |
|||
SET_OUTPUT(TFT_CS_PIN); |
|||
|
|||
TFT_DC_H; |
|||
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 |
|||
*/ |
|||
#if 0 |
|||
#if SPI_DEVICE == 1 |
|||
#define SPI_CLOCK_MAX SPI_CLOCK_DIV4 |
|||
#else |
|||
#define SPI_CLOCK_MAX SPI_CLOCK_DIV2 |
|||
#endif |
|||
uint8_t clock; |
|||
uint8_t spiRate = SPI_FULL_SPEED; |
|||
switch (spiRate) { |
|||
case SPI_FULL_SPEED: clock = SPI_CLOCK_MAX ; 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
|
|||
} |
|||
#endif |
|||
|
|||
#if TFT_MISO_PIN == BOARD_SPI1_MISO_PIN |
|||
SPIx.setModule(1); |
|||
#elif TFT_MISO_PIN == BOARD_SPI2_MISO_PIN |
|||
SPIx.setModule(2); |
|||
#endif |
|||
SPIx.setClock(SPI_CLOCK_MAX); |
|||
SPIx.setBitOrder(MSBFIRST); |
|||
SPIx.setDataMode(SPI_MODE0); |
|||
} |
|||
|
|||
void TFT_SPI::DataTransferBegin(uint16_t DataSize) { |
|||
SPIx.setDataSize(DataSize); |
|||
SPIx.begin(); |
|||
TFT_CS_L; |
|||
} |
|||
|
|||
uint32_t TFT_SPI::GetID() { |
|||
uint32_t id; |
|||
id = ReadID(LCD_READ_ID); |
|||
if ((id & 0xFFFF) == 0 || (id & 0xFFFF) == 0xFFFF) |
|||
id = ReadID(LCD_READ_ID4); |
|||
return id; |
|||
} |
|||
|
|||
uint32_t TFT_SPI::ReadID(uint16_t Reg) { |
|||
uint32_t data = 0; |
|||
|
|||
#if PIN_EXISTS(TFT_MISO) |
|||
uint8_t d = 0; |
|||
SPIx.setDataSize(DATASIZE_8BIT); |
|||
SPIx.setClock(SPI_CLOCK_DIV64); |
|||
SPIx.begin(); |
|||
TFT_CS_L; |
|||
WriteReg(Reg); |
|||
|
|||
LOOP_L_N(i, 4) { |
|||
SPIx.read((uint8_t*)&d, 1); |
|||
data = (data << 8) | d; |
|||
} |
|||
|
|||
DataTransferEnd(); |
|||
SPIx.setClock(SPI_CLOCK_MAX); |
|||
#endif |
|||
|
|||
return data >> 7; |
|||
} |
|||
|
|||
bool TFT_SPI::isBusy() { |
|||
return false; |
|||
} |
|||
|
|||
void TFT_SPI::Abort() { |
|||
DataTransferEnd(); |
|||
} |
|||
|
|||
void TFT_SPI::Transmit(uint16_t Data) { |
|||
SPIx.transfer(Data); |
|||
} |
|||
|
|||
void TFT_SPI::TransmitDMA(uint32_t MemoryIncrease, uint16_t *Data, uint16_t Count) { |
|||
DataTransferBegin(DATASIZE_16BIT); //16
|
|||
TFT_DC_H; |
|||
SPIx.dmaSend(Data, Count, MemoryIncrease); |
|||
DataTransferEnd(); |
|||
} |
|||
|
|||
#endif // HAS_SPI_TFT
|
@ -0,0 +1,77 @@ |
|||
/**
|
|||
* 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/>.
|
|||
* |
|||
*/ |
|||
#pragma once |
|||
|
|||
#include "../../../inc/MarlinConfig.h" |
|||
|
|||
#include <SPI.h> |
|||
#include <lpc17xx_ssp.h> |
|||
// #include <lpc17xx_gpdma.h>
|
|||
|
|||
#ifndef LCD_READ_ID |
|||
#define LCD_READ_ID 0x04 // Read display identification information (0xD3 on ILI9341)
|
|||
#endif |
|||
#ifndef LCD_READ_ID4 |
|||
#define LCD_READ_ID4 0xD3 // Read display identification information (0xD3 on ILI9341)
|
|||
#endif |
|||
|
|||
#define DATASIZE_8BIT SSP_DATABIT_8 |
|||
#define DATASIZE_16BIT SSP_DATABIT_16 |
|||
#define TFT_IO TFT_SPI |
|||
|
|||
#define DMA_MINC_ENABLE 1 |
|||
#define DMA_MINC_DISABLE 0 |
|||
|
|||
class TFT_SPI { |
|||
private: |
|||
static uint32_t ReadID(uint16_t Reg); |
|||
static void Transmit(uint16_t Data); |
|||
static void TransmitDMA(uint32_t MemoryIncrease, uint16_t *Data, uint16_t Count); |
|||
|
|||
public: |
|||
static SPIClass SPIx; |
|||
|
|||
static void Init(); |
|||
static uint32_t GetID(); |
|||
static bool isBusy(); |
|||
static void Abort(); |
|||
|
|||
static void DataTransferBegin(uint16_t DataWidth = DATASIZE_16BIT); |
|||
static void DataTransferEnd() { OUT_WRITE(TFT_CS_PIN, HIGH); SPIx.end(); }; |
|||
static void DataTransferAbort(); |
|||
|
|||
static void WriteData(uint16_t Data) { Transmit(Data); } |
|||
static void WriteReg(uint16_t Reg) { OUT_WRITE(TFT_A0_PIN, LOW); Transmit(Reg); OUT_WRITE(TFT_A0_PIN, HIGH); } |
|||
|
|||
static void WriteSequence(uint16_t *Data, uint16_t Count) { TransmitDMA(DMA_MINC_ENABLE, Data, Count); } |
|||
// static void WriteMultiple(uint16_t Color, uint16_t Count) { static uint16_t Data; Data = Color; TransmitDMA(DMA_MINC_DISABLE, &Data, Count); }
|
|||
static void WriteMultiple(uint16_t Color, uint32_t Count) { |
|||
static uint16_t Data; Data = Color; |
|||
//LPC dma can only write 0xFFF bytes at once.
|
|||
#define MAX_DMA_SIZE (0xFFF - 1) |
|||
while (Count > 0) { |
|||
TransmitDMA(DMA_MINC_DISABLE, &Data, Count > MAX_DMA_SIZE ? MAX_DMA_SIZE : Count); |
|||
Count = Count > MAX_DMA_SIZE ? Count - MAX_DMA_SIZE : 0; |
|||
} |
|||
#undef MAX_DMA_SIZE |
|||
} |
|||
}; |
@ -0,0 +1,129 @@ |
|||
/**
|
|||
* Marlin 3D Printer Firmware |
|||
* Copyright (c) 2019 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_TFT_XPT2046 || HAS_TOUCH_XPT2046 |
|||
|
|||
#include "xpt2046.h" |
|||
#include <SPI.h> |
|||
|
|||
uint16_t delta(uint16_t a, uint16_t b) { return a > b ? a - b : b - a; } |
|||
|
|||
#if ENABLED(TOUCH_BUTTONS_HW_SPI) |
|||
#include <SPI.h> |
|||
|
|||
SPIClass XPT2046::SPIx(TOUCH_BUTTONS_HW_SPI_DEVICE); |
|||
|
|||
static void touch_spi_init(uint8_t spiRate) { |
|||
XPT2046::SPIx.setModule(TOUCH_BUTTONS_HW_SPI_DEVICE); |
|||
XPT2046::SPIx.setClock(SPI_CLOCK_DIV128); |
|||
XPT2046::SPIx.setBitOrder(MSBFIRST); |
|||
XPT2046::SPIx.setDataMode(SPI_MODE0); |
|||
XPT2046::SPIx.setDataSize(DATA_SIZE_8BIT); |
|||
} |
|||
#endif |
|||
|
|||
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 |
|||
|
|||
TERN_(TOUCH_BUTTONS_HW_SPI, touch_spi_init(SPI_SPEED_6)); |
|||
|
|||
// Read once to enable pendrive status pin
|
|||
getRawData(XPT2046_X); |
|||
} |
|||
|
|||
bool XPT2046::isTouched() { |
|||
return isBusy() ? false : ( |
|||
#if PIN_EXISTS(TOUCH_INT) |
|||
READ(TOUCH_INT_PIN) != HIGH |
|||
#else |
|||
getRawData(XPT2046_Z1) >= XPT2046_Z1_THRESHOLD |
|||
#endif |
|||
); |
|||
} |
|||
|
|||
bool XPT2046::getRawPoint(int16_t *x, int16_t *y) { |
|||
if (isBusy()) return false; |
|||
if (!isTouched()) return false; |
|||
*x = getRawData(XPT2046_X); |
|||
*y = getRawData(XPT2046_Y); |
|||
SERIAL_ECHOLNPAIR("X: ", *x, ", Y: ", *y); |
|||
return isTouched(); |
|||
} |
|||
|
|||
uint16_t XPT2046::getRawData(const XPTCoordinate coordinate) { |
|||
uint16_t data[3]; |
|||
|
|||
DataTransferBegin(); |
|||
TERN_(TOUCH_BUTTONS_HW_SPI, SPIx.begin()); |
|||
|
|||
for (uint16_t i = 0; i < 3 ; i++) { |
|||
IO(coordinate); |
|||
data[i] = (IO() << 4) | (IO() >> 4); |
|||
} |
|||
|
|||
TERN_(TOUCH_BUTTONS_HW_SPI, SPIx.end()); |
|||
DataTransferEnd(); |
|||
|
|||
uint16_t delta01 = delta(data[0], data[1]), |
|||
delta02 = delta(data[0], data[2]), |
|||
delta12 = delta(data[1], data[2]); |
|||
|
|||
if (delta01 > delta02 || delta01 > delta12) |
|||
data[delta02 > delta12 ? 0 : 1] = data[2]; |
|||
|
|||
return (data[0] + data[1]) >> 1; |
|||
} |
|||
|
|||
uint16_t XPT2046::IO(uint16_t data) { |
|||
return TERN(TOUCH_BUTTONS_HW_SPI, HardwareIO, SoftwareIO)(data); |
|||
} |
|||
|
|||
extern uint8_t spiTransfer(uint8_t b); |
|||
|
|||
#if ENABLED(TOUCH_BUTTONS_HW_SPI) |
|||
uint16_t XPT2046::HardwareIO(uint16_t data) { |
|||
return SPIx.transfer(data & 0xFF); |
|||
} |
|||
#endif |
|||
|
|||
uint16_t XPT2046::SoftwareIO(uint16_t data) { |
|||
uint16_t result = 0; |
|||
|
|||
for (uint8_t j = 0x80; j; j >>= 1) { |
|||
WRITE(TOUCH_SCK_PIN, LOW); |
|||
WRITE(TOUCH_MOSI_PIN, data & j ? HIGH : LOW); |
|||
if (READ(TOUCH_MISO_PIN)) result |= j; |
|||
WRITE(TOUCH_SCK_PIN, HIGH); |
|||
} |
|||
WRITE(TOUCH_SCK_PIN, LOW); |
|||
|
|||
return result; |
|||
} |
|||
|
|||
#endif // HAS_TFT_XPT2046
|
@ -0,0 +1,80 @@ |
|||
/**
|
|||
* Marlin 3D Printer Firmware |
|||
* Copyright (c) 2019 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/>.
|
|||
* |
|||
*/ |
|||
#pragma once |
|||
|
|||
#include "../../../inc/MarlinConfig.h" |
|||
|
|||
#if ENABLED(TOUCH_BUTTONS_HW_SPI) |
|||
#include <SPI.h> |
|||
#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 |
|||
#ifndef TOUCH_INT_PIN |
|||
#define TOUCH_INT_PIN -1 |
|||
#endif |
|||
|
|||
#define XPT2046_DFR_MODE 0x00 |
|||
#define XPT2046_SER_MODE 0x04 |
|||
#define XPT2046_CONTROL 0x80 |
|||
|
|||
enum XPTCoordinate : uint8_t { |
|||
XPT2046_X = 0x10 | XPT2046_CONTROL | XPT2046_DFR_MODE, |
|||
XPT2046_Y = 0x50 | XPT2046_CONTROL | XPT2046_DFR_MODE, |
|||
XPT2046_Z1 = 0x30 | XPT2046_CONTROL | XPT2046_DFR_MODE, |
|||
XPT2046_Z2 = 0x40 | XPT2046_CONTROL | XPT2046_DFR_MODE, |
|||
}; |
|||
|
|||
#if !defined(XPT2046_Z1_THRESHOLD) |
|||
#define XPT2046_Z1_THRESHOLD 10 |
|||
#endif |
|||
|
|||
class XPT2046 { |
|||
private: |
|||
static bool isBusy() { return false; } |
|||
|
|||
static uint16_t getRawData(const XPTCoordinate coordinate); |
|||
static bool isTouched(); |
|||
|
|||
static inline void DataTransferBegin() { WRITE(TOUCH_CS_PIN, LOW); }; |
|||
static inline void DataTransferEnd() { WRITE(TOUCH_CS_PIN, HIGH); }; |
|||
#if ENABLED(TOUCH_BUTTONS_HW_SPI) |
|||
static uint16_t HardwareIO(uint16_t data); |
|||
#endif |
|||
static uint16_t SoftwareIO(uint16_t data); |
|||
static uint16_t IO(uint16_t data = 0); |
|||
|
|||
public: |
|||
#if ENABLED(TOUCH_BUTTONS_HW_SPI) |
|||
static SPIClass SPIx; |
|||
#endif |
|||
|
|||
static void Init(); |
|||
static bool getRawPoint(int16_t *x, int16_t *y); |
|||
}; |
@ -0,0 +1,180 @@ |
|||
/**
|
|||
* 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/>.
|
|||
* |
|||
*/ |
|||
|
|||
#include "../../../inc/MarlinConfig.h" |
|||
|
|||
#if HAS_FSMC_TFT |
|||
|
|||
#include "tft_fsmc.h" |
|||
#include "pinconfig.h" |
|||
|
|||
SRAM_HandleTypeDef TFT_FSMC::SRAMx; |
|||
DMA_HandleTypeDef TFT_FSMC::DMAtx; |
|||
LCD_CONTROLLER_TypeDef *TFT_FSMC::LCD; |
|||
|
|||
void TFT_FSMC::Init() { |
|||
uint32_t controllerAddress; |
|||
|
|||
#if PIN_EXISTS(TFT_RESET) |
|||
OUT_WRITE(TFT_RESET_PIN, HIGH); |
|||
HAL_Delay(100); |
|||
#endif |
|||
|
|||
#if PIN_EXISTS(TFT_BACKLIGHT) |
|||
OUT_WRITE(TFT_BACKLIGHT_PIN, HIGH); |
|||
#endif |
|||
|
|||
FSMC_NORSRAM_TimingTypeDef Timing, ExtTiming; |
|||
|
|||
uint32_t NSBank = (uint32_t)pinmap_peripheral(digitalPinToPinName(TFT_CS_PIN), PinMap_FSMC_CS); |
|||
|
|||
SRAMx.Instance = FSMC_NORSRAM_DEVICE; |
|||
SRAMx.Extended = FSMC_NORSRAM_EXTENDED_DEVICE; |
|||
/* SRAMx.Init */ |
|||
SRAMx.Init.NSBank = NSBank; |
|||
SRAMx.Init.DataAddressMux = FSMC_DATA_ADDRESS_MUX_DISABLE; |
|||
SRAMx.Init.MemoryType = FSMC_MEMORY_TYPE_SRAM; |
|||
SRAMx.Init.MemoryDataWidth = FSMC_NORSRAM_MEM_BUS_WIDTH_16; |
|||
SRAMx.Init.BurstAccessMode = FSMC_BURST_ACCESS_MODE_DISABLE; |
|||
SRAMx.Init.WaitSignalPolarity = FSMC_WAIT_SIGNAL_POLARITY_LOW; |
|||
SRAMx.Init.WrapMode = FSMC_WRAP_MODE_DISABLE; |
|||
SRAMx.Init.WaitSignalActive = FSMC_WAIT_TIMING_BEFORE_WS; |
|||
SRAMx.Init.WriteOperation = FSMC_WRITE_OPERATION_ENABLE; |
|||
SRAMx.Init.WaitSignal = FSMC_WAIT_SIGNAL_DISABLE; |
|||
SRAMx.Init.ExtendedMode = FSMC_EXTENDED_MODE_ENABLE; |
|||
SRAMx.Init.AsynchronousWait = FSMC_ASYNCHRONOUS_WAIT_DISABLE; |
|||
SRAMx.Init.WriteBurst = FSMC_WRITE_BURST_DISABLE; |
|||
#ifdef STM32F4xx |
|||
SRAMx.Init.PageSize = FSMC_PAGE_SIZE_NONE; |
|||
#endif |
|||
/* Read Timing - relatively slow to ensure ID information is correctly read from TFT controller */ |
|||
/* Can be decreases from 15-15-24 to 4-4-8 with risk of stability loss */ |
|||
Timing.AddressSetupTime = 15; |
|||
Timing.AddressHoldTime = 15; |
|||
Timing.DataSetupTime = 24; |
|||
Timing.BusTurnAroundDuration = 0; |
|||
Timing.CLKDivision = 16; |
|||
Timing.DataLatency = 17; |
|||
Timing.AccessMode = FSMC_ACCESS_MODE_A; |
|||
/* Write Timing */ |
|||
/* Can be decreases from 8-15-8 to 0-0-1 with risk of stability loss */ |
|||
ExtTiming.AddressSetupTime = 8; |
|||
ExtTiming.AddressHoldTime = 15; |
|||
ExtTiming.DataSetupTime = 8; |
|||
ExtTiming.BusTurnAroundDuration = 0; |
|||
ExtTiming.CLKDivision = 16; |
|||
ExtTiming.DataLatency = 17; |
|||
ExtTiming.AccessMode = FSMC_ACCESS_MODE_A; |
|||
|
|||
__HAL_RCC_FSMC_CLK_ENABLE(); |
|||
|
|||
for(uint16_t i = 0; PinMap_FSMC[i].pin != NC; i++) |
|||
pinmap_pinout(PinMap_FSMC[i].pin, PinMap_FSMC); |
|||
pinmap_pinout(digitalPinToPinName(TFT_CS_PIN), PinMap_FSMC_CS); |
|||
pinmap_pinout(digitalPinToPinName(TFT_RS_PIN), PinMap_FSMC_RS); |
|||
|
|||
controllerAddress = FSMC_BANK1_1; |
|||
#ifdef PF0 |
|||
switch (NSBank) { |
|||
case FSMC_NORSRAM_BANK2: controllerAddress = FSMC_BANK1_2 ; break; |
|||
case FSMC_NORSRAM_BANK3: controllerAddress = FSMC_BANK1_3 ; break; |
|||
case FSMC_NORSRAM_BANK4: controllerAddress = FSMC_BANK1_4 ; break; |
|||
} |
|||
#endif |
|||
|
|||
controllerAddress |= (uint32_t)pinmap_peripheral(digitalPinToPinName(TFT_RS_PIN), PinMap_FSMC_RS); |
|||
|
|||
HAL_SRAM_Init(&SRAMx, &Timing, &ExtTiming); |
|||
|
|||
__HAL_RCC_DMA2_CLK_ENABLE(); |
|||
|
|||
#ifdef STM32F1xx |
|||
DMAtx.Instance = DMA2_Channel1; |
|||
#elif defined(STM32F4xx) |
|||
DMAtx.Instance = DMA2_Stream0; |
|||
DMAtx.Init.Channel = DMA_CHANNEL_0; |
|||
DMAtx.Init.FIFOMode = DMA_FIFOMODE_ENABLE; |
|||
DMAtx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL; |
|||
DMAtx.Init.MemBurst = DMA_MBURST_SINGLE; |
|||
DMAtx.Init.PeriphBurst = DMA_PBURST_SINGLE; |
|||
#endif |
|||
|
|||
DMAtx.Init.Direction = DMA_MEMORY_TO_MEMORY; |
|||
DMAtx.Init.MemInc = DMA_MINC_DISABLE; |
|||
DMAtx.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD; |
|||
DMAtx.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD; |
|||
DMAtx.Init.Mode = DMA_NORMAL; |
|||
DMAtx.Init.Priority = DMA_PRIORITY_HIGH; |
|||
|
|||
LCD = (LCD_CONTROLLER_TypeDef *)controllerAddress; |
|||
} |
|||
|
|||
uint32_t TFT_FSMC::GetID() { |
|||
uint32_t id; |
|||
WriteReg(0x0000); |
|||
id = LCD->RAM; |
|||
|
|||
if (id == 0) |
|||
id = ReadID(LCD_READ_ID); |
|||
if ((id & 0xFFFF) == 0 || (id & 0xFFFF) == 0xFFFF) |
|||
id = ReadID(LCD_READ_ID4); |
|||
return id; |
|||
} |
|||
|
|||
uint32_t TFT_FSMC::ReadID(uint16_t Reg) { |
|||
uint32_t id; |
|||
WriteReg(Reg); |
|||
id = LCD->RAM; // dummy read
|
|||
id = Reg << 24; |
|||
id |= (LCD->RAM & 0x00FF) << 16; |
|||
id |= (LCD->RAM & 0x00FF) << 8; |
|||
id |= LCD->RAM & 0x00FF; |
|||
return id; |
|||
} |
|||
|
|||
bool TFT_FSMC::isBusy() { |
|||
if (__IS_DMA_ENABLED(&DMAtx)) |
|||
if (__HAL_DMA_GET_FLAG(&DMAtx, __HAL_DMA_GET_TC_FLAG_INDEX(&DMAtx)) != 0 || __HAL_DMA_GET_FLAG(&DMAtx, __HAL_DMA_GET_TE_FLAG_INDEX(&DMAtx)) != 0) |
|||
Abort(); |
|||
return __IS_DMA_ENABLED(&DMAtx); |
|||
} |
|||
|
|||
void TFT_FSMC::TransmitDMA(uint32_t MemoryIncrease, uint16_t *Data, uint16_t Count) { |
|||
DMAtx.Init.PeriphInc = MemoryIncrease; |
|||
HAL_DMA_Init(&DMAtx); |
|||
|
|||
__HAL_DMA_CLEAR_FLAG(&DMAtx, __HAL_DMA_GET_TC_FLAG_INDEX(&DMAtx)); |
|||
__HAL_DMA_CLEAR_FLAG(&DMAtx, __HAL_DMA_GET_TE_FLAG_INDEX(&DMAtx)); |
|||
|
|||
#ifdef STM32F1xx |
|||
DMAtx.Instance->CNDTR = Count; |
|||
DMAtx.Instance->CPAR = (uint32_t)Data; |
|||
DMAtx.Instance->CMAR = (uint32_t)&(LCD->RAM); |
|||
#elif defined(STM32F4xx) |
|||
DMAtx.Instance->NDTR = Count; |
|||
DMAtx.Instance->PAR = (uint32_t)Data; |
|||
DMAtx.Instance->M0AR = (uint32_t)&(LCD->RAM); |
|||
#endif |
|||
__HAL_DMA_ENABLE(&DMAtx); |
|||
} |
|||
|
|||
#endif // HAS_FSMC_TFT
|
@ -0,0 +1,160 @@ |
|||
/**
|
|||
* 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/>.
|
|||
* |
|||
*/ |
|||
#pragma once |
|||
|
|||
#ifdef STM32F1xx |
|||
#include "stm32f1xx_hal.h" |
|||
#elif defined(STM32F4xx) |
|||
#include "stm32f4xx_hal.h" |
|||
#else |
|||
#error FSMC TFT is currently only supported on STM32F1 and STM32F4 hardware. |
|||
#endif |
|||
|
|||
#ifndef LCD_READ_ID |
|||
#define LCD_READ_ID 0x04 // Read display identification information (0xD3 on ILI9341)
|
|||
#endif |
|||
#ifndef LCD_READ_ID4 |
|||
#define LCD_READ_ID4 0xD3 // Read display identification information (0xD3 on ILI9341)
|
|||
#endif |
|||
|
|||
#define DATASIZE_8BIT SPI_DATASIZE_8BIT |
|||
#define DATASIZE_16BIT SPI_DATASIZE_16BIT |
|||
#define TFT_IO TFT_FSMC |
|||
|
|||
#ifdef STM32F1xx |
|||
#define __IS_DMA_ENABLED(__HANDLE__) ((__HANDLE__)->Instance->CCR & DMA_CCR_EN) |
|||
#elif defined(STM32F4xx) |
|||
#define __IS_DMA_ENABLED(__HANDLE__) ((__HANDLE__)->Instance->CR & DMA_SxCR_EN) |
|||
#endif |
|||
|
|||
typedef struct { |
|||
__IO uint16_t REG; |
|||
__IO uint16_t RAM; |
|||
} LCD_CONTROLLER_TypeDef; |
|||
|
|||
class TFT_FSMC { |
|||
private: |
|||
static SRAM_HandleTypeDef SRAMx; |
|||
static DMA_HandleTypeDef DMAtx; |
|||
|
|||
static LCD_CONTROLLER_TypeDef *LCD; |
|||
|
|||
static uint32_t ReadID(uint16_t Reg); |
|||
static void Transmit(uint16_t Data) { LCD->RAM = Data; __DSB(); } |
|||
static void TransmitDMA(uint32_t MemoryIncrease, uint16_t *Data, uint16_t Count); |
|||
|
|||
public: |
|||
static void Init(); |
|||
static uint32_t GetID(); |
|||
static bool isBusy(); |
|||
static void Abort() { __HAL_DMA_DISABLE(&DMAtx); } |
|||
|
|||
static void DataTransferBegin(uint16_t DataWidth = DATASIZE_16BIT) {} |
|||
static void DataTransferEnd() {}; |
|||
|
|||
static void WriteData(uint16_t Data) { Transmit(Data); } |
|||
static void WriteReg(uint16_t Reg) { LCD->REG = Reg; __DSB(); } |
|||
|
|||
static void WriteSequence(uint16_t *Data, uint16_t Count) { TransmitDMA(DMA_PINC_ENABLE, Data, Count); } |
|||
static void WriteMultiple(uint16_t Color, uint16_t Count) { static uint16_t Data; Data = Color; TransmitDMA(DMA_PINC_DISABLE, &Data, Count); } |
|||
}; |
|||
|
|||
|
|||
#ifdef STM32F1xx |
|||
#define FSMC_PIN_DATA STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, AFIO_NONE) |
|||
#elif defined(STM32F4xx) |
|||
#define FSMC_PIN_DATA STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF12_FSMC) |
|||
#define FSMC_BANK1_1 0x60000000U |
|||
#define FSMC_BANK1_2 0x64000000U |
|||
#define FSMC_BANK1_3 0x68000000U |
|||
#define FSMC_BANK1_4 0x6C000000U |
|||
#else |
|||
#error No configuration for this MCU |
|||
#endif |
|||
|
|||
const PinMap PinMap_FSMC[] = { |
|||
{PD_14, FSMC_NORSRAM_DEVICE, FSMC_PIN_DATA}, // FSMC_D00
|
|||
{PD_15, FSMC_NORSRAM_DEVICE, FSMC_PIN_DATA}, // FSMC_D01
|
|||
{PD_0, FSMC_NORSRAM_DEVICE, FSMC_PIN_DATA}, // FSMC_D02
|
|||
{PD_1, FSMC_NORSRAM_DEVICE, FSMC_PIN_DATA}, // FSMC_D03
|
|||
{PE_7, FSMC_NORSRAM_DEVICE, FSMC_PIN_DATA}, // FSMC_D04
|
|||
{PE_8, FSMC_NORSRAM_DEVICE, FSMC_PIN_DATA}, // FSMC_D05
|
|||
{PE_9, FSMC_NORSRAM_DEVICE, FSMC_PIN_DATA}, // FSMC_D06
|
|||
{PE_10, FSMC_NORSRAM_DEVICE, FSMC_PIN_DATA}, // FSMC_D07
|
|||
{PE_11, FSMC_NORSRAM_DEVICE, FSMC_PIN_DATA}, // FSMC_D08
|
|||
{PE_12, FSMC_NORSRAM_DEVICE, FSMC_PIN_DATA}, // FSMC_D09
|
|||
{PE_13, FSMC_NORSRAM_DEVICE, FSMC_PIN_DATA}, // FSMC_D10
|
|||
{PE_14, FSMC_NORSRAM_DEVICE, FSMC_PIN_DATA}, // FSMC_D11
|
|||
{PE_15, FSMC_NORSRAM_DEVICE, FSMC_PIN_DATA}, // FSMC_D12
|
|||
{PD_8, FSMC_NORSRAM_DEVICE, FSMC_PIN_DATA}, // FSMC_D13
|
|||
{PD_9, FSMC_NORSRAM_DEVICE, FSMC_PIN_DATA}, // FSMC_D14
|
|||
{PD_10, FSMC_NORSRAM_DEVICE, FSMC_PIN_DATA}, // FSMC_D15
|
|||
{PD_4, FSMC_NORSRAM_DEVICE, FSMC_PIN_DATA}, // FSMC_NOE
|
|||
{PD_5, FSMC_NORSRAM_DEVICE, FSMC_PIN_DATA}, // FSMC_NWE
|
|||
{NC, NP, 0} |
|||
}; |
|||
|
|||
const PinMap PinMap_FSMC_CS[] = { |
|||
{PD_7, (void *)FSMC_NORSRAM_BANK1, FSMC_PIN_DATA}, // FSMC_NE1
|
|||
#ifdef PF0 |
|||
{PG_9, (void *)FSMC_NORSRAM_BANK2, FSMC_PIN_DATA}, // FSMC_NE2
|
|||
{PG_10, (void *)FSMC_NORSRAM_BANK3, FSMC_PIN_DATA}, // FSMC_NE3
|
|||
{PG_12, (void *)FSMC_NORSRAM_BANK4, FSMC_PIN_DATA}, // FSMC_NE4
|
|||
#endif |
|||
{NC, NP, 0} |
|||
}; |
|||
|
|||
#define FSMC_RS(A) (void *)((2 << A) - 2) |
|||
|
|||
const PinMap PinMap_FSMC_RS[] = { |
|||
#ifdef PF0 |
|||
{PF_0, FSMC_RS( 0), FSMC_PIN_DATA}, // FSMC_A0
|
|||
{PF_1, FSMC_RS( 1), FSMC_PIN_DATA}, // FSMC_A1
|
|||
{PF_2, FSMC_RS( 2), FSMC_PIN_DATA}, // FSMC_A2
|
|||
{PF_3, FSMC_RS( 3), FSMC_PIN_DATA}, // FSMC_A3
|
|||
{PF_4, FSMC_RS( 4), FSMC_PIN_DATA}, // FSMC_A4
|
|||
{PF_5, FSMC_RS( 5), FSMC_PIN_DATA}, // FSMC_A5
|
|||
{PF_12, FSMC_RS( 6), FSMC_PIN_DATA}, // FSMC_A6
|
|||
{PF_13, FSMC_RS( 7), FSMC_PIN_DATA}, // FSMC_A7
|
|||
{PF_14, FSMC_RS( 8), FSMC_PIN_DATA}, // FSMC_A8
|
|||
{PF_15, FSMC_RS( 9), FSMC_PIN_DATA}, // FSMC_A9
|
|||
{PG_0, FSMC_RS(10), FSMC_PIN_DATA}, // FSMC_A10
|
|||
{PG_1, FSMC_RS(11), FSMC_PIN_DATA}, // FSMC_A11
|
|||
{PG_2, FSMC_RS(12), FSMC_PIN_DATA}, // FSMC_A12
|
|||
{PG_3, FSMC_RS(13), FSMC_PIN_DATA}, // FSMC_A13
|
|||
{PG_4, FSMC_RS(14), FSMC_PIN_DATA}, // FSMC_A14
|
|||
{PG_5, FSMC_RS(15), FSMC_PIN_DATA}, // FSMC_A15
|
|||
#endif |
|||
{PD_11, FSMC_RS(16), FSMC_PIN_DATA}, // FSMC_A16
|
|||
{PD_12, FSMC_RS(17), FSMC_PIN_DATA}, // FSMC_A17
|
|||
{PD_13, FSMC_RS(18), FSMC_PIN_DATA}, // FSMC_A18
|
|||
{PE_3, FSMC_RS(19), FSMC_PIN_DATA}, // FSMC_A19
|
|||
{PE_4, FSMC_RS(20), FSMC_PIN_DATA}, // FSMC_A20
|
|||
{PE_5, FSMC_RS(21), FSMC_PIN_DATA}, // FSMC_A21
|
|||
{PE_6, FSMC_RS(22), FSMC_PIN_DATA}, // FSMC_A22
|
|||
{PE_2, FSMC_RS(23), FSMC_PIN_DATA}, // FSMC_A23
|
|||
#ifdef PF0 |
|||
{PG_13, FSMC_RS(24), FSMC_PIN_DATA}, // FSMC_A24
|
|||
{PG_14, FSMC_RS(25), FSMC_PIN_DATA}, // FSMC_A25
|
|||
#endif |
|||
{NC, NP, 0} |
|||
}; |
@ -0,0 +1,212 @@ |
|||
/**
|
|||
* 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/>.
|
|||
* |
|||
*/ |
|||
|
|||
#include "../../../inc/MarlinConfig.h" |
|||
|
|||
#if HAS_SPI_TFT |
|||
|
|||
#include "tft_spi.h" |
|||
#include "pinconfig.h" |
|||
|
|||
SPI_HandleTypeDef TFT_SPI::SPIx; |
|||
DMA_HandleTypeDef TFT_SPI::DMAtx; |
|||
|
|||
void TFT_SPI::Init() { |
|||
SPI_TypeDef *spiInstance; |
|||
|
|||
#if PIN_EXISTS(TFT_RESET) |
|||
OUT_WRITE(TFT_RESET_PIN, HIGH); |
|||
HAL_Delay(100); |
|||
#endif |
|||
|
|||
#if PIN_EXISTS(TFT_BACKLIGHT) |
|||
OUT_WRITE(TFT_BACKLIGHT_PIN, HIGH); |
|||
#endif |
|||
|
|||
OUT_WRITE(TFT_A0_PIN, HIGH); |
|||
OUT_WRITE(TFT_CS_PIN, HIGH); |
|||
|
|||
if ((spiInstance = (SPI_TypeDef *)pinmap_peripheral(digitalPinToPinName(TFT_SCK_PIN), PinMap_SPI_SCLK)) == NP) return; |
|||
if (spiInstance != (SPI_TypeDef *)pinmap_peripheral(digitalPinToPinName(TFT_MOSI_PIN), PinMap_SPI_MOSI)) return; |
|||
|
|||
#if PIN_EXISTS(TFT_MISO) && (TFT_MISO_PIN != TFT_MOSI_PIN) |
|||
if (spiInstance != (SPI_TypeDef *)pinmap_peripheral(digitalPinToPinName(TFT_MISO_PIN), PinMap_SPI_MISO)) return; |
|||
#endif |
|||
|
|||
SPIx.Instance = spiInstance; |
|||
SPIx.State = HAL_SPI_STATE_RESET; |
|||
SPIx.Init.NSS = SPI_NSS_SOFT; |
|||
SPIx.Init.Mode = SPI_MODE_MASTER; |
|||
SPIx.Init.Direction = |
|||
#if TFT_MISO_PIN == TFT_MOSI_PIN |
|||
SPI_DIRECTION_1LINE; |
|||
#else |
|||
SPI_DIRECTION_2LINES; |
|||
#endif |
|||
SPIx.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2; |
|||
SPIx.Init.CLKPhase = SPI_PHASE_1EDGE; |
|||
SPIx.Init.CLKPolarity = SPI_POLARITY_LOW; |
|||
SPIx.Init.DataSize = SPI_DATASIZE_8BIT; |
|||
SPIx.Init.FirstBit = SPI_FIRSTBIT_MSB; |
|||
SPIx.Init.TIMode = SPI_TIMODE_DISABLE; |
|||
SPIx.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; |
|||
SPIx.Init.CRCPolynomial = 10; |
|||
|
|||
pinmap_pinout(digitalPinToPinName(TFT_SCK_PIN), PinMap_SPI_SCLK); |
|||
pinmap_pinout(digitalPinToPinName(TFT_MOSI_PIN), PinMap_SPI_MOSI); |
|||
#if PIN_EXISTS(TFT_MISO) && (TFT_MISO_PIN != TFT_MOSI_PIN) |
|||
pinmap_pinout(digitalPinToPinName(TFT_MISO_PIN), PinMap_SPI_MISO); |
|||
#endif |
|||
pin_PullConfig(get_GPIO_Port(STM_PORT(digitalPinToPinName(TFT_SCK_PIN))), STM_LL_GPIO_PIN(digitalPinToPinName(TFT_SCK_PIN)), GPIO_PULLDOWN); |
|||
|
|||
#ifdef SPI1_BASE |
|||
if (SPIx.Instance == SPI1) { |
|||
__HAL_RCC_SPI1_CLK_ENABLE(); |
|||
__HAL_RCC_DMA1_CLK_ENABLE(); |
|||
SPIx.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4; |
|||
DMAtx.Instance = DMA1_Channel3; |
|||
} |
|||
#endif |
|||
#ifdef SPI2_BASE |
|||
if (SPIx.Instance == SPI2) { |
|||
__HAL_RCC_SPI2_CLK_ENABLE(); |
|||
__HAL_RCC_DMA1_CLK_ENABLE(); |
|||
DMAtx.Instance = DMA1_Channel5; |
|||
} |
|||
#endif |
|||
#ifdef SPI3_BASE |
|||
if (SPIx.Instance == SPI3) { |
|||
__HAL_RCC_SPI3_CLK_ENABLE(); |
|||
__HAL_RCC_DMA2_CLK_ENABLE(); |
|||
DMAtx.Instance = DMA2_Channel2; |
|||
} |
|||
#endif |
|||
|
|||
HAL_SPI_Init(&SPIx); |
|||
|
|||
DMAtx.Init.Direction = DMA_MEMORY_TO_PERIPH; |
|||
DMAtx.Init.PeriphInc = DMA_PINC_DISABLE; |
|||
DMAtx.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD; |
|||
DMAtx.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD; |
|||
DMAtx.Init.Mode = DMA_NORMAL; |
|||
DMAtx.Init.Priority = DMA_PRIORITY_LOW; |
|||
} |
|||
|
|||
void TFT_SPI::DataTransferBegin(uint16_t DataSize) { |
|||
SPIx.Init.DataSize = DataSize == DATASIZE_8BIT ? SPI_DATASIZE_8BIT : SPI_DATASIZE_16BIT; |
|||
HAL_SPI_Init(&SPIx); |
|||
WRITE(TFT_CS_PIN, LOW); |
|||
} |
|||
|
|||
uint32_t TFT_SPI::GetID() { |
|||
uint32_t id; |
|||
id = ReadID(LCD_READ_ID); |
|||
|
|||
if ((id & 0xFFFF) == 0 || (id & 0xFFFF) == 0xFFFF) |
|||
id = ReadID(LCD_READ_ID4); |
|||
return id; |
|||
} |
|||
|
|||
uint32_t TFT_SPI::ReadID(uint16_t Reg) { |
|||
#if !PIN_EXISTS(TFT_MISO) |
|||
return 0; |
|||
#else |
|||
uint32_t BaudRatePrescaler = SPIx.Init.BaudRatePrescaler; |
|||
uint32_t i, Data = 0; |
|||
|
|||
SPIx.Init.BaudRatePrescaler = SPIx.Instance == SPI1 ? SPI_BAUDRATEPRESCALER_8 : SPI_BAUDRATEPRESCALER_4; |
|||
DataTransferBegin(DATASIZE_8BIT); |
|||
WriteReg(Reg); |
|||
|
|||
if (SPIx.Init.Direction == SPI_DIRECTION_1LINE) SPI_1LINE_RX(&SPIx); |
|||
__HAL_SPI_ENABLE(&SPIx); |
|||
|
|||
for (i = 0; i < 4; i++) { |
|||
#if TFT_MISO_PIN != TFT_MOSI_PIN |
|||
//if (hspi->Init.Direction == SPI_DIRECTION_2LINES) {
|
|||
while ((SPIx.Instance->SR & SPI_FLAG_TXE) != SPI_FLAG_TXE) {} |
|||
SPIx.Instance->DR = 0; |
|||
//}
|
|||
#endif |
|||
while ((SPIx.Instance->SR & SPI_FLAG_RXNE) != SPI_FLAG_RXNE) {} |
|||
Data = (Data << 8) | SPIx.Instance->DR; |
|||
} |
|||
|
|||
__HAL_SPI_DISABLE(&SPIx); |
|||
DataTransferEnd(); |
|||
|
|||
SPIx.Init.BaudRatePrescaler = BaudRatePrescaler; |
|||
|
|||
return Data >> 7; |
|||
#endif |
|||
} |
|||
|
|||
bool TFT_SPI::isBusy() { |
|||
if (DMAtx.Instance->CCR & DMA_CCR_EN) |
|||
if (__HAL_DMA_GET_FLAG(&DMAtx, __HAL_DMA_GET_TC_FLAG_INDEX(&DMAtx)) != 0 || __HAL_DMA_GET_FLAG(&DMAtx, __HAL_DMA_GET_TE_FLAG_INDEX(&DMAtx)) != 0) |
|||
Abort(); |
|||
return DMAtx.Instance->CCR & DMA_CCR_EN; |
|||
} |
|||
|
|||
void TFT_SPI::Abort() { |
|||
__HAL_DMA_DISABLE(&DMAtx); |
|||
DataTransferEnd(); |
|||
} |
|||
|
|||
void TFT_SPI::Transmit(uint16_t Data) { |
|||
#if TFT_MISO_PIN == TFT_MOSI_PIN |
|||
SPI_1LINE_TX(&SPIx); |
|||
#endif |
|||
|
|||
__HAL_SPI_ENABLE(&SPIx); |
|||
|
|||
SPIx.Instance->DR = Data; |
|||
|
|||
while ((SPIx.Instance->SR & SPI_FLAG_TXE) != SPI_FLAG_TXE) {} |
|||
while ((SPIx.Instance->SR & SPI_FLAG_BSY) == SPI_FLAG_BSY) {} |
|||
|
|||
#if TFT_MISO_PIN != TFT_MOSI_PIN |
|||
__HAL_SPI_CLEAR_OVRFLAG(&SPIx); /* Clear overrun flag in 2 Lines communication mode because received is not read */ |
|||
#endif |
|||
} |
|||
|
|||
void TFT_SPI::TransmitDMA(uint32_t MemoryIncrease, uint16_t *Data, uint16_t Count) { |
|||
DMAtx.Init.MemInc = MemoryIncrease; |
|||
HAL_DMA_Init(&DMAtx); |
|||
|
|||
DataTransferBegin(); |
|||
|
|||
#if TFT_MISO_PIN == TFT_MOSI_PIN |
|||
SPI_1LINE_TX(&SPIx); |
|||
#endif |
|||
|
|||
DMAtx.DmaBaseAddress->IFCR = (DMA_ISR_GIF1 << DMAtx.ChannelIndex); |
|||
DMAtx.Instance->CNDTR = Count; |
|||
DMAtx.Instance->CPAR = (uint32_t)&(SPIx.Instance->DR); |
|||
DMAtx.Instance->CMAR = (uint32_t)Data; |
|||
__HAL_DMA_ENABLE(&DMAtx); |
|||
__HAL_SPI_ENABLE(&SPIx); |
|||
|
|||
SET_BIT(SPIx.Instance->CR2, SPI_CR2_TXDMAEN); /* Enable Tx DMA Request */ |
|||
} |
|||
|
|||
#endif // HAS_SPI_TFT
|
@ -0,0 +1,67 @@ |
|||
/**
|
|||
* 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/>.
|
|||
* |
|||
*/ |
|||
#pragma once |
|||
|
|||
#ifdef STM32F1xx |
|||
#include "stm32f1xx_hal.h" |
|||
#elif defined(STM32F4xx) |
|||
#include "stm32f4xx_hal.h" |
|||
#else |
|||
#error SPI TFT is currently only supported on STM32F1 and STM32F4 hardware. |
|||
#endif |
|||
|
|||
#ifndef LCD_READ_ID |
|||
#define LCD_READ_ID 0x04 // Read display identification information (0xD3 on ILI9341)
|
|||
#endif |
|||
#ifndef LCD_READ_ID4 |
|||
#define LCD_READ_ID4 0xD3 // Read display identification information (0xD3 on ILI9341)
|
|||
#endif |
|||
|
|||
#define DATASIZE_8BIT SPI_DATASIZE_8BIT |
|||
#define DATASIZE_16BIT SPI_DATASIZE_16BIT |
|||
#define TFT_IO TFT_SPI |
|||
|
|||
class TFT_SPI { |
|||
private: |
|||
static SPI_HandleTypeDef SPIx; |
|||
static DMA_HandleTypeDef DMAtx; |
|||
|
|||
static uint32_t ReadID(uint16_t Reg); |
|||
static void Transmit(uint16_t Data); |
|||
static void TransmitDMA(uint32_t MemoryIncrease, uint16_t *Data, uint16_t Count); |
|||
|
|||
public: |
|||
static void Init(); |
|||
static uint32_t GetID(); |
|||
static bool isBusy(); |
|||
static void Abort(); |
|||
|
|||
static void DataTransferBegin(uint16_t DataWidth = DATASIZE_16BIT); |
|||
static void DataTransferEnd() { WRITE(TFT_CS_PIN, HIGH); }; |
|||
static void DataTransferAbort(); |
|||
|
|||
static void WriteData(uint16_t Data) { Transmit(Data); } |
|||
static void WriteReg(uint16_t Reg) { WRITE(TFT_A0_PIN, LOW); Transmit(Reg); WRITE(TFT_A0_PIN, HIGH); } |
|||
|
|||
static void WriteSequence(uint16_t *Data, uint16_t Count) { TransmitDMA(DMA_MINC_ENABLE, Data, Count); } |
|||
static void WriteMultiple(uint16_t Color, uint16_t Count) { static uint16_t Data; Data = Color; TransmitDMA(DMA_MINC_DISABLE, &Data, Count); } |
|||
}; |
@ -0,0 +1,185 @@ |
|||
/**
|
|||
* Marlin 3D Printer Firmware |
|||
* Copyright (c) 2019 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_TFT_XPT2046 |
|||
|
|||
#include "xpt2046.h" |
|||
#include "pinconfig.h" |
|||
|
|||
uint16_t delta(uint16_t a, uint16_t b) { return a > b ? a - b : b - a; } |
|||
|
|||
SPI_HandleTypeDef XPT2046::SPIx; |
|||
DMA_HandleTypeDef XPT2046::DMAtx; |
|||
|
|||
void XPT2046::Init() { |
|||
SPI_TypeDef *spiInstance; |
|||
|
|||
OUT_WRITE(TOUCH_CS_PIN, HIGH); |
|||
|
|||
#if PIN_EXISTS(TOUCH_INT) |
|||
// Optional Pendrive interrupt pin
|
|||
SET_INPUT(TOUCH_INT_PIN); |
|||
#endif |
|||
|
|||
spiInstance = (SPI_TypeDef *)pinmap_peripheral(digitalPinToPinName(TOUCH_SCK_PIN), PinMap_SPI_SCLK); |
|||
if (spiInstance != (SPI_TypeDef *)pinmap_peripheral(digitalPinToPinName(TOUCH_MOSI_PIN), PinMap_SPI_MOSI)) spiInstance = NP; |
|||
if (spiInstance != (SPI_TypeDef *)pinmap_peripheral(digitalPinToPinName(TOUCH_MISO_PIN), PinMap_SPI_MISO)) spiInstance = NP; |
|||
|
|||
SPIx.Instance = spiInstance; |
|||
|
|||
if (SPIx.Instance) { |
|||
SPIx.State = HAL_SPI_STATE_RESET; |
|||
SPIx.Init.NSS = SPI_NSS_SOFT; |
|||
SPIx.Init.Mode = SPI_MODE_MASTER; |
|||
SPIx.Init.Direction = SPI_DIRECTION_2LINES; |
|||
SPIx.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8; |
|||
SPIx.Init.CLKPhase = SPI_PHASE_2EDGE; |
|||
SPIx.Init.CLKPolarity = SPI_POLARITY_HIGH; |
|||
SPIx.Init.DataSize = SPI_DATASIZE_8BIT; |
|||
SPIx.Init.FirstBit = SPI_FIRSTBIT_MSB; |
|||
SPIx.Init.TIMode = SPI_TIMODE_DISABLE; |
|||
SPIx.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; |
|||
SPIx.Init.CRCPolynomial = 10; |
|||
|
|||
pinmap_pinout(digitalPinToPinName(TOUCH_SCK_PIN), PinMap_SPI_SCLK); |
|||
pinmap_pinout(digitalPinToPinName(TOUCH_MOSI_PIN), PinMap_SPI_MOSI); |
|||
pinmap_pinout(digitalPinToPinName(TOUCH_MISO_PIN), PinMap_SPI_MISO); |
|||
|
|||
#ifdef SPI1_BASE |
|||
if (SPIx.Instance == SPI1) { |
|||
__HAL_RCC_SPI1_CLK_ENABLE(); |
|||
SPIx.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16; |
|||
#ifdef STM32F1xx |
|||
DMAtx.Instance = DMA1_Channel3; |
|||
#elif defined(STM32F4xx) |
|||
DMAtx.Instance = DMA2_Stream3; // DMA2_Stream5
|
|||
#endif |
|||
//SERIAL_ECHO_MSG(" Touch Screen on SPI1");
|
|||
} |
|||
#endif |
|||
#ifdef SPI2_BASE |
|||
if (SPIx.Instance == SPI2) { |
|||
__HAL_RCC_SPI2_CLK_ENABLE(); |
|||
#ifdef STM32F1xx |
|||
DMAtx.Instance = DMA1_Channel5; |
|||
#elif defined(STM32F4xx) |
|||
DMAtx.Instance = DMA1_Stream4; |
|||
#endif |
|||
//SERIAL_ECHO_MSG(" Touch Screen on SPI2");
|
|||
} |
|||
#endif |
|||
#ifdef SPI3_BASE |
|||
if (SPIx.Instance == SPI3) { |
|||
__HAL_RCC_SPI3_CLK_ENABLE(); |
|||
#ifdef STM32F1xx |
|||
DMAtx.Instance = DMA2_Channel2; |
|||
#elif defined(STM32F4xx) |
|||
DMAtx.Instance = DMA1_Stream5; // DMA1_Stream7
|
|||
#endif |
|||
//SERIAL_ECHO_MSG(" Touch Screen on SPI3");
|
|||
} |
|||
#endif |
|||
} |
|||
else { |
|||
SPIx.Instance = NULL; |
|||
SET_INPUT(TOUCH_MISO_PIN); |
|||
SET_OUTPUT(TOUCH_MOSI_PIN); |
|||
SET_OUTPUT(TOUCH_SCK_PIN); |
|||
//SERIAL_ECHO_MSG(" Touch Screen on Software SPI");
|
|||
} |
|||
|
|||
getRawData(XPT2046_Z1); |
|||
} |
|||
|
|||
bool XPT2046::isTouched() { |
|||
return isBusy() ? false : ( |
|||
#if PIN_EXISTS(TOUCH_INT) |
|||
READ(TOUCH_INT_PIN) != HIGH |
|||
#else |
|||
getRawData(XPT2046_Z1) >= XPT2046_Z1_THRESHOLD |
|||
#endif |
|||
); |
|||
} |
|||
|
|||
bool XPT2046::getRawPoint(int16_t *x, int16_t *y) { |
|||
if (isBusy()) return false; |
|||
if (!isTouched()) return false; |
|||
*x = getRawData(XPT2046_X); |
|||
*y = getRawData(XPT2046_Y); |
|||
return isTouched(); |
|||
} |
|||
|
|||
uint16_t XPT2046::getRawData(const XPTCoordinate coordinate) { |
|||
uint16_t data[3]; |
|||
|
|||
DataTransferBegin(); |
|||
|
|||
for (uint16_t i = 0; i < 3 ; i++) { |
|||
IO(coordinate); |
|||
data[i] = (IO() << 4) | (IO() >> 4); |
|||
} |
|||
|
|||
DataTransferEnd(); |
|||
|
|||
uint16_t delta01 = delta(data[0], data[1]); |
|||
uint16_t delta02 = delta(data[0], data[2]); |
|||
uint16_t delta12 = delta(data[1], data[2]); |
|||
|
|||
if (delta01 > delta02 || delta01 > delta12) { |
|||
if (delta02 > delta12) |
|||
data[0] = data[2]; |
|||
else |
|||
data[1] = data[2]; |
|||
} |
|||
|
|||
return (data[0] + data[1]) >> 1; |
|||
} |
|||
|
|||
uint16_t XPT2046::HardwareIO(uint16_t data) { |
|||
__HAL_SPI_ENABLE(&SPIx); |
|||
while((SPIx.Instance->SR & SPI_FLAG_TXE) != SPI_FLAG_TXE) {} |
|||
SPIx.Instance->DR = data; |
|||
while((SPIx.Instance->SR & SPI_FLAG_RXNE) != SPI_FLAG_RXNE) {} |
|||
__HAL_SPI_DISABLE(&SPIx); |
|||
|
|||
return SPIx.Instance->DR; |
|||
} |
|||
|
|||
uint16_t XPT2046::SoftwareIO(uint16_t data) { |
|||
uint16_t result = 0; |
|||
|
|||
for (uint8_t j = 0x80; j > 0; j >>= 1) { |
|||
WRITE(TOUCH_SCK_PIN, LOW); |
|||
__DSB(); |
|||
WRITE(TOUCH_MOSI_PIN, data & j ? HIGH : LOW); |
|||
__DSB(); |
|||
if (READ(TOUCH_MISO_PIN)) result |= j; |
|||
__DSB(); |
|||
WRITE(TOUCH_SCK_PIN, HIGH); |
|||
__DSB(); |
|||
} |
|||
WRITE(TOUCH_SCK_PIN, LOW); |
|||
__DSB(); |
|||
|
|||
return result; |
|||
} |
|||
|
|||
#endif // HAS_TFT_XPT2046
|
@ -0,0 +1,86 @@ |
|||
/**
|
|||
* Marlin 3D Printer Firmware |
|||
* Copyright (c) 2019 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/>.
|
|||
* |
|||
*/ |
|||
#pragma once |
|||
|
|||
#ifdef STM32F1xx |
|||
#include <stm32f1xx_hal.h> |
|||
#elif defined(STM32F4xx) |
|||
#include <stm32f4xx_hal.h> |
|||
#endif |
|||
|
|||
#include "../../../inc/MarlinConfig.h" |
|||
|
|||
// Not using regular SPI interface by default to avoid SPI mode conflicts with other SPI devices
|
|||
|
|||
#if !PIN_EXISTS(TOUCH_MISO) |
|||
#error "TOUCH_MISO_PIN is not defined." |
|||
#elif !PIN_EXISTS(TOUCH_MOSI) |
|||
#error "TOUCH_MOSI_PIN is not defined." |
|||
#elif !PIN_EXISTS(TOUCH_SCK) |
|||
#error "TOUCH_SCK_PIN is not defined." |
|||
#elif !PIN_EXISTS(TOUCH_CS) |
|||
#error "TOUCH_CS_PIN is not defined." |
|||
#endif |
|||
|
|||
#ifndef TOUCH_INT_PIN |
|||
#define TOUCH_INT_PIN -1 |
|||
#endif |
|||
|
|||
#define XPT2046_DFR_MODE 0x00 |
|||
#define XPT2046_SER_MODE 0x04 |
|||
#define XPT2046_CONTROL 0x80 |
|||
|
|||
enum XPTCoordinate : uint8_t { |
|||
XPT2046_X = 0x10 | XPT2046_CONTROL | XPT2046_DFR_MODE, |
|||
XPT2046_Y = 0x50 | XPT2046_CONTROL | XPT2046_DFR_MODE, |
|||
XPT2046_Z1 = 0x30 | XPT2046_CONTROL | XPT2046_DFR_MODE, |
|||
XPT2046_Z2 = 0x40 | XPT2046_CONTROL | XPT2046_DFR_MODE, |
|||
}; |
|||
|
|||
#if !defined(XPT2046_Z1_THRESHOLD) |
|||
#define XPT2046_Z1_THRESHOLD 10 |
|||
#endif |
|||
|
|||
#ifdef STM32F1xx |
|||
#define __IS_DMA_ENABLED(__HANDLE__) ((__HANDLE__)->Instance->CCR & DMA_CCR_EN) |
|||
#elif defined(STM32F4xx) |
|||
#define __IS_DMA_ENABLED(__HANDLE__) ((__HANDLE__)->Instance->CR & DMA_SxCR_EN) |
|||
#endif |
|||
|
|||
|
|||
class XPT2046 { |
|||
private: |
|||
static SPI_HandleTypeDef SPIx; |
|||
static DMA_HandleTypeDef DMAtx; |
|||
|
|||
static bool isBusy() { return SPIx.Instance ? __IS_DMA_ENABLED(&DMAtx) : false; } |
|||
|
|||
static uint16_t getRawData(const XPTCoordinate coordinate); |
|||
static bool isTouched(); |
|||
|
|||
static inline void DataTransferBegin() { if (SPIx.Instance) { HAL_SPI_Init(&SPIx); } WRITE(TOUCH_CS_PIN, LOW); }; |
|||
static inline void DataTransferEnd() { WRITE(TOUCH_CS_PIN, HIGH); }; |
|||
static uint16_t HardwareIO(uint16_t data); |
|||
static uint16_t SoftwareIO(uint16_t data); |
|||
static uint16_t IO(uint16_t data = 0) { return SPIx.Instance ? HardwareIO(data) : SoftwareIO(data); } |
|||
|
|||
public: |
|||
static void Init(); |
|||
static bool getRawPoint(int16_t *x, int16_t *y); |
|||
}; |
@ -1,198 +0,0 @@ |
|||
/**
|
|||
|
|||
AT24C16, 16K SERIAL EEPROM: |
|||
Internally organized with 128 pages of 16 bytes each (2048 bytes) |
|||
|
|||
16K requires an 11-bit data word address for random word addressing. |
|||
|
|||
The 16K does not use any device address bits but instead the 3 bits are used for mem- |
|||
ory page addressing. These page addressing bits on the 4K, 8K and 16K devices |
|||
should be considered the most significant bits of the data word address which follows. |
|||
The A0, A1 and A2 pins are no connect. |
|||
|
|||
*/ |
|||
#include "../../inc/MarlinConfig.h" |
|||
|
|||
#if ENABLED(I2C_EEPROM_AT24C16) |
|||
|
|||
#include "../HAL.h" |
|||
#include "../../module/mks_wifi/small_cmsis.h" |
|||
#include "../../module/mks_wifi/dwt.h" |
|||
|
|||
#define DEV_ADDR 0xA0 |
|||
#define FSMC_DISABLE RCC->AHBENR &= ~RCC_AHBENR_FSMCEN //Конфликт на ноге FSMC_NADV с I2C. На время передачи приходится отключать FSMC
|
|||
#define FSMC_RESTORE RCC->AHBENR |= RCC_AHBENR_FSMCEN; |
|||
|
|||
#define I2C_TIMEOUT 2000 //таймаут на ожидание опереций I2C.
|
|||
|
|||
#define CHECK_TIMEOUT do{if(dwt_get_timeout() == 0){ERROR("Timeout");return 0;}}while(0) |
|||
|
|||
static bool waitSRBitSet(uint32_t Bit); |
|||
static uint8_t i2c_write(const uint8_t hw_adr, uint8_t *data, uint32_t len); |
|||
static uint8_t i2c_read(const uint8_t hw_adr, uint16_t addr, uint8_t *data, uint32_t len); |
|||
|
|||
void eeprom_hw_deinit(void){ |
|||
DEBUG("Finish I2C"); |
|||
} |
|||
|
|||
void eeprom_init(void){ |
|||
/*
|
|||
PB6 SCL Alternate function open drain |
|||
PB7 SDA Alternate function open drain |
|||
*/ |
|||
DEBUG("Init I2C"); |
|||
dwt_init(); |
|||
|
|||
RCC->APB2ENR |= RCC_APB2ENR_IOPBEN|RCC_APB2ENR_AFIOEN; |
|||
PORTB->CRL |= (GPIO_CRL_MODE6|GPIO_CRL_MODE7|GPIO_CRL_CNF6|GPIO_CRL_CNF7); |
|||
|
|||
AFIO->MAPR2 |= AFIO_MAPR2_FSMC_NADV_REMAP; //Remap по факту не работает, бит не устанавливается.
|
|||
RCC->APB1ENR|= RCC_APB1ENR_I2C1EN; |
|||
|
|||
I2C1->CR1 = I2C_CR1_SWRST; |
|||
I2C1->CR1 = 0; |
|||
|
|||
//Тактовая 72Mhz, PCLK 36Mhz
|
|||
I2C1->CCR = (180 << I2C_CCR_CCR_Pos); |
|||
I2C1->CR2 = (36 << I2C_CR2_FREQ_Pos); |
|||
I2C1->TRISE = 37; |
|||
I2C1->CR1 = I2C_CR1_PE; |
|||
} |
|||
|
|||
void eeprom_write_byte(uint8_t *pos, unsigned char value){ |
|||
uint8_t data[2]; |
|||
|
|||
FSMC_DISABLE; |
|||
|
|||
data[0]=(uint8_t)((unsigned)pos % 256); |
|||
data[1]=(uint8_t)(value); |
|||
|
|||
if(!i2c_write((DEV_ADDR+(uint8_t)(((unsigned)pos/256)<<1)),data,2)){ |
|||
ERROR("write failed"); |
|||
} |
|||
safe_delay(20); //Задержка на время пока eeprom пишет.
|
|||
FSMC_RESTORE; |
|||
} |
|||
|
|||
uint8_t eeprom_read_byte(uint8_t *pos) { |
|||
uint8_t data; |
|||
|
|||
FSMC_DISABLE; |
|||
|
|||
if(!i2c_read(DEV_ADDR, (uint16_t)((unsigned)pos), &data, 1)){ |
|||
ERROR("read failed"); |
|||
data=0; |
|||
} |
|||
|
|||
safe_delay(1); //небольшая пауза перед включением FSMC, чтобы состояние STOP успело выставиться на линии.
|
|||
FSMC_RESTORE; |
|||
return data; |
|||
} |
|||
|
|||
void eeprom_read_block(void *__dst, const void *__src, size_t __n){ |
|||
ERROR("Call to missing function"); |
|||
}; |
|||
|
|||
void eeprom_update_block(const void *__src, void *__dst, size_t __n){ |
|||
ERROR("Call to missing function"); |
|||
}; |
|||
|
|||
static uint8_t i2c_write(const uint8_t hw_adr, uint8_t *data, uint32_t len){ |
|||
|
|||
//DEBUG("i2c write at %d val %0X",data[0],data[1]);
|
|||
|
|||
dwt_settimeout(I2C_TIMEOUT); |
|||
//DEBUG("Wait busy");
|
|||
while(I2C1->SR2 & I2C_SR2_BUSY) {CHECK_TIMEOUT;}; |
|||
|
|||
|
|||
I2C1->CR1 = I2C_CR1_PE | I2C_CR1_START; |
|||
dwt_settimeout(I2C_TIMEOUT); |
|||
//DEBUG("Wait SB");
|
|||
while(!(I2C1->SR1 & I2C_SR1_SB)) {CHECK_TIMEOUT;}; |
|||
I2C1->DR = (hw_adr & 0xFE); |
|||
|
|||
if(!waitSRBitSet(I2C_SR1_ADDR)) return false; |
|||
(void)I2C1->SR2; |
|||
|
|||
while(len--){ |
|||
if(!waitSRBitSet(I2C_SR1_TXE)) return false; |
|||
I2C1->DR = *data++; |
|||
} |
|||
|
|||
dwt_settimeout(I2C_TIMEOUT); |
|||
//DEBUG("Wait BTF");
|
|||
while(!((I2C1->SR1 & I2C_SR1_TXE) && (I2C1->SR1 & I2C_SR1_BTF))) {CHECK_TIMEOUT;}; |
|||
I2C1->CR1 = I2C_CR1_PE | I2C_CR1_STOP; |
|||
|
|||
return 1; |
|||
} |
|||
|
|||
static uint8_t i2c_read(const uint8_t hw_adr, uint16_t addr, uint8_t *data, uint32_t len){ |
|||
|
|||
dwt_settimeout(I2C_TIMEOUT); |
|||
//DEBUG("Wait busy");
|
|||
while(I2C1->SR2 & I2C_SR2_BUSY) {CHECK_TIMEOUT;}; |
|||
|
|||
//Запись адреса
|
|||
I2C1->CR1 = I2C_CR1_PE | I2C_CR1_START; |
|||
|
|||
dwt_settimeout(I2C_TIMEOUT); |
|||
//DEBUG("Wait SB");
|
|||
while(!(I2C1->SR1 & I2C_SR1_SB)) {CHECK_TIMEOUT;}; //Условие старт
|
|||
|
|||
|
|||
I2C1->DR = ((hw_adr & 0xFE) + ((addr/256) << 1)); |
|||
if(!waitSRBitSet(I2C_SR1_ADDR)) return false; //i2c адрес отправлен
|
|||
I2C1->SR2; |
|||
|
|||
if(!waitSRBitSet(I2C_SR1_TXE)) return false; |
|||
I2C1->DR = addr%256; //адрес в памяти отправлен
|
|||
|
|||
dwt_settimeout(I2C_TIMEOUT); |
|||
//DEBUG("Wait BTF");
|
|||
while(!((I2C1->SR1 & I2C_SR1_TXE) && (I2C1->SR1 & I2C_SR1_BTF))) {CHECK_TIMEOUT;}; |
|||
|
|||
//Чтение
|
|||
I2C1->CR1 = I2C_CR1_PE | I2C_CR1_START | I2C_CR1_ACK; |
|||
|
|||
dwt_settimeout(I2C_TIMEOUT); |
|||
//DEBUG("Wait SB");
|
|||
while(!(I2C1->SR1 & I2C_SR1_SB)) {CHECK_TIMEOUT;}; |
|||
I2C1->DR = hw_adr|1; |
|||
|
|||
if(!waitSRBitSet(I2C_SR1_ADDR)) return false; |
|||
I2C1->SR2; |
|||
I2C1->CR1 = I2C_CR1_PE | I2C_CR1_STOP; |
|||
|
|||
if(!waitSRBitSet(I2C_SR1_RXNE)) return false; |
|||
*data = I2C1->DR; |
|||
|
|||
return true; |
|||
} |
|||
|
|||
|
|||
static bool waitSRBitSet(uint32_t Bit){ |
|||
uint32_t sr; |
|||
|
|||
dwt_settimeout(I2C_TIMEOUT); |
|||
do{ |
|||
sr = I2C1->SR1; |
|||
if(sr & ( I2C_SR1_AF | I2C_SR1_ARLO | I2C_SR1_BERR)){ |
|||
I2C1->CR1 = I2C_CR1_PE | I2C_CR1_STOP; |
|||
I2C1->SR1 = 0; |
|||
ERROR("I2C Error flag %0X",sr); |
|||
return false; |
|||
} |
|||
if(dwt_get_timeout() == 0){ |
|||
ERROR("Timeout %0X",Bit); |
|||
return false; |
|||
} |
|||
|
|||
}while(!(sr & Bit)); |
|||
|
|||
return true; |
|||
}; |
|||
|
|||
|
|||
#endif // I2C_EEPROM
|
@ -1,51 +0,0 @@ |
|||
#include "small_spi.h" |
|||
#include "../../inc/MarlinConfig.h" |
|||
|
|||
/*
|
|||
SPI2 |
|||
MISO - PB14 Input floating / Input pull-up |
|||
MOSI - PB15 Alternate function push-pull |
|||
SCK - PB13 Alternate function push-pull |
|||
W25Q CS - PB12 Out push-pull |
|||
TOUCH CS - PA7 |
|||
*/ |
|||
|
|||
void spi2_init(uint8_t prescaler){ |
|||
uint32_t tmp; |
|||
|
|||
if(prescaler > SPI_FREQ_140Khz){ |
|||
prescaler = SPI_FREQ_140Khz; |
|||
} |
|||
|
|||
RCC->APB2ENR |= RCC_APB2ENR_IOPBEN|RCC_APB2ENR_IOPAEN|RCC_APB2ENR_AFIOEN; |
|||
|
|||
tmp = PORTB->CRH; |
|||
tmp &= ~(GPIO_CRH_MODE14|GPIO_CRH_CNF14|GPIO_CRH_CNF12|GPIO_CRH_CNF13|GPIO_CRH_CNF14|GPIO_CRH_CNF15); |
|||
tmp |= (GPIO_CRH_MODE12|GPIO_CRH_MODE13|GPIO_CRH_MODE15|GPIO_CRH_CNF13_1|GPIO_CRH_CNF15_1|GPIO_CRH_CNF14_0); |
|||
PORTB->CRH = tmp; |
|||
|
|||
//CS PIN
|
|||
tmp= PORTA->CRL; |
|||
tmp &= ~GPIO_CRL_CNF7; |
|||
tmp |= GPIO_CRL_MODE7; |
|||
PORTA->CRL = tmp; |
|||
|
|||
SPI2_STOP_ALL; |
|||
|
|||
RCC->APB1ENR|= RCC_APB1ENR_SPI2EN; |
|||
|
|||
SPI2->CR1 = SPI_CR1_SSM|\ |
|||
SPI_CR1_SSI|\ |
|||
(prescaler << SPI_CR1_BR_Pos)|\ |
|||
SPI_CR1_MSTR; |
|||
|
|||
SPI2->CR1 |= SPI_CR1_SPE; |
|||
} |
|||
|
|||
uint8_t spi_send(uint8_t data){ |
|||
while((SPI2->SR & SPI_SR_TXE) == 0){NOP;}; |
|||
SPI2->DR = data; |
|||
|
|||
while((SPI2->SR & SPI_SR_RXNE) == 0){NOP;}; |
|||
return SPI2->DR; |
|||
} |
@ -1,34 +0,0 @@ |
|||
#ifndef SMALL_SPI_H |
|||
#define SMALL_SPI_H |
|||
|
|||
#include "../HAL.h" |
|||
#include "../../module/mks_wifi/small_cmsis.h" |
|||
|
|||
#define SPI_FREQ_18Mhz (uint8_t)0 |
|||
#define SPI_FREQ_9Mhz (uint8_t)1 |
|||
#define SPI_FREQ_4_5Mhz (uint8_t)2 |
|||
#define SPI_FREQ_2_25Mhz (uint8_t)3 |
|||
#define SPI_FREQ_1_125Mhz (uint8_t)4 |
|||
#define SPI_FREQ_560KHz (uint8_t)5 |
|||
#define SPI_FREQ_280KHz (uint8_t)6 |
|||
#define SPI_FREQ_140Khz (uint8_t)7 |
|||
|
|||
#define SPI_DIR_READ 0 |
|||
#define SPI_DIR_WRITE 1 |
|||
|
|||
#define W25Q_START do{PORTB->BSRR=GPIO_BSRR_BR12;PORTA->BSRR=GPIO_BSRR_BS7;}while(0) |
|||
#define W25Q_STOP PORTB->BSRR=GPIO_BSRR_BS12 |
|||
|
|||
#define TOUCH_CS_START do{PORTB->BSRR=GPIO_BSRR_BS12;PORTA->BSRR=GPIO_BSRR_BR7;}while(0) |
|||
#define TOUCH_CS_STOP PORTA->BSRR=GPIO_BSRR_BS7 |
|||
|
|||
#define SPI2_STOP_ALL do{PORTB->BSRR=GPIO_BSRR_BS12;PORTA->BSRR=GPIO_BSRR_BS7;}while(0) |
|||
|
|||
|
|||
uint8_t spi_send(uint8_t data); |
|||
void spi_read(uint32_t addr, uint8_t *buf, uint32_t len); |
|||
void spi_write(uint32_t addr, uint8_t *buf, uint32_t len); |
|||
void spi2_init(uint8_t prescaler); |
|||
|
|||
|
|||
#endif |
@ -0,0 +1,71 @@ |
|||
/**
|
|||
* 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/>.
|
|||
* |
|||
*/ |
|||
#pragma once |
|||
|
|||
#ifndef LCD_READ_ID |
|||
#define LCD_READ_ID 0x04 // Read display identification information (0xD3 on ILI9341)
|
|||
#endif |
|||
#ifndef LCD_READ_ID4 |
|||
#define LCD_READ_ID4 0xD3 // Read display identification information (0xD3 on ILI9341)
|
|||
#endif |
|||
|
|||
#include <libmaple/dma.h> |
|||
|
|||
#define DATASIZE_8BIT DMA_SIZE_8BITS |
|||
#define DATASIZE_16BIT DMA_SIZE_16BITS |
|||
#define TFT_IO TFT_FSMC |
|||
|
|||
typedef struct { |
|||
__IO uint16_t REG; |
|||
__IO uint16_t RAM; |
|||
} LCD_CONTROLLER_TypeDef; |
|||
|
|||
class TFT_FSMC { |
|||
private: |
|||
static LCD_CONTROLLER_TypeDef *LCD; |
|||
|
|||
static uint32_t ReadID(uint16_t Reg); |
|||
static void Transmit(uint16_t Data); |
|||
static void TransmitDMA(uint32_t MemoryIncrease, uint16_t *Data, uint16_t Count); |
|||
|
|||
public: |
|||
static void Init(); |
|||
static uint32_t GetID(); |
|||
static bool isBusy(); |
|||
static void Abort(); |
|||
|
|||
static void DataTransferBegin(uint16_t DataWidth = DATASIZE_16BIT) {}; |
|||
static void DataTransferEnd() {}; |
|||
|
|||
static void WriteData(uint16_t Data) { Transmit(Data); } |
|||
static void WriteReg(uint16_t Reg); |
|||
|
|||
static void WriteSequence(uint16_t *Data, uint16_t Count) { TransmitDMA(DMA_PINC_MODE, Data, Count); } |
|||
static void WriteMultiple(uint16_t Color, uint16_t Count) { static uint16_t Data; Data = Color; TransmitDMA(DMA_CIRC_MODE, &Data, Count); } |
|||
static void WriteMultiple(uint16_t Color, uint32_t Count) { |
|||
static uint16_t Data; Data = Color; |
|||
while (Count > 0) { |
|||
TransmitDMA(DMA_CIRC_MODE, &Data, Count > 0xFFFF ? 0xFFFF : Count); |
|||
Count = Count > 0xFFFF ? Count - 0xFFFF : 0; |
|||
} |
|||
} |
|||
}; |
@ -0,0 +1,149 @@ |
|||
/**
|
|||
* 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/>.
|
|||
* |
|||
*/ |
|||
|
|||
#include "../../../inc/MarlinConfig.h" |
|||
|
|||
#if HAS_SPI_TFT |
|||
|
|||
#include "tft_spi.h" |
|||
|
|||
// TFT_SPI tft;
|
|||
|
|||
SPIClass TFT_SPI::SPIx(1); |
|||
|
|||
#define TFT_CS_H OUT_WRITE(TFT_CS_PIN, HIGH) |
|||
#define TFT_CS_L OUT_WRITE(TFT_CS_PIN, LOW) |
|||
|
|||
#define TFT_DC_H OUT_WRITE(TFT_DC_PIN, HIGH) |
|||
#define TFT_DC_L OUT_WRITE(TFT_DC_PIN, LOW) |
|||
|
|||
#define TFT_RST_H OUT_WRITE(TFT_RST_PIN, HIGH) |
|||
#define TFT_RST_L OUT_WRITE(TFT_RST_PIN, LOW) |
|||
|
|||
#define TFT_BLK_H OUT_WRITE(TFT_BACKLIGHT_PIN, HIGH) |
|||
#define TFT_BLK_L OUT_WRITE(TFT_BACKLIGHT_PIN, LOW) |
|||
|
|||
void TFT_SPI::Init() { |
|||
#if PIN_EXISTS(TFT_RESET) |
|||
// OUT_WRITE(TFT_RESET_PIN, HIGH);
|
|||
TFT_RST_H; |
|||
delay(100); |
|||
#endif |
|||
|
|||
#if PIN_EXISTS(TFT_BACKLIGHT) |
|||
// OUT_WRITE(TFT_BACKLIGHT_PIN, HIGH);
|
|||
TFT_BLK_H; |
|||
#endif |
|||
|
|||
TFT_DC_H; |
|||
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 |
|||
*/ |
|||
#if SPI_DEVICE == 1 |
|||
#define SPI_CLOCK_MAX SPI_CLOCK_DIV4 |
|||
#else |
|||
#define SPI_CLOCK_MAX SPI_CLOCK_DIV2 |
|||
#endif |
|||
uint8_t clock; |
|||
uint8_t spiRate = SPI_FULL_SPEED; |
|||
switch (spiRate) { |
|||
case SPI_FULL_SPEED: clock = SPI_CLOCK_MAX ; 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
|
|||
} |
|||
SPIx.setModule(1); |
|||
SPIx.setClockDivider(clock); |
|||
SPIx.setBitOrder(MSBFIRST); |
|||
SPIx.setDataMode(SPI_MODE0); |
|||
} |
|||
|
|||
void TFT_SPI::DataTransferBegin(uint16_t DataSize) { |
|||
SPIx.setDataSize(DataSize); |
|||
SPIx.begin(); |
|||
TFT_CS_L; |
|||
} |
|||
|
|||
uint32_t TFT_SPI::GetID() { |
|||
uint32_t id; |
|||
id = ReadID(LCD_READ_ID); |
|||
|
|||
if ((id & 0xFFFF) == 0 || (id & 0xFFFF) == 0xFFFF) |
|||
id = ReadID(LCD_READ_ID4); |
|||
return id; |
|||
} |
|||
|
|||
uint32_t TFT_SPI::ReadID(uint16_t Reg) { |
|||
#if !PIN_EXISTS(TFT_MISO) |
|||
return 0; |
|||
#else |
|||
uint8_t d = 0; |
|||
uint32_t data = 0; |
|||
SPIx.setClockDivider(SPI_CLOCK_DIV16); |
|||
DataTransferBegin(DATASIZE_8BIT); |
|||
WriteReg(Reg); |
|||
|
|||
LOOP_L_N(i, 4) { |
|||
SPIx.read((uint8_t*)&d, 1); |
|||
data = (data << 8) | d; |
|||
} |
|||
|
|||
DataTransferEnd(); |
|||
SPIx.setClockDivider(SPI_CLOCK_MAX); |
|||
|
|||
return data >> 7; |
|||
#endif |
|||
} |
|||
|
|||
bool TFT_SPI::isBusy() { |
|||
return false; |
|||
} |
|||
|
|||
void TFT_SPI::Abort() { |
|||
DataTransferEnd(); |
|||
} |
|||
|
|||
void TFT_SPI::Transmit(uint16_t Data) { |
|||
SPIx.send(Data); |
|||
} |
|||
|
|||
void TFT_SPI::TransmitDMA(uint32_t MemoryIncrease, uint16_t *Data, uint16_t Count) { |
|||
DataTransferBegin(); |
|||
TFT_DC_H; |
|||
if (MemoryIncrease == DMA_MINC_ENABLE) { |
|||
SPIx.dmaSend(Data, Count, true); |
|||
} |
|||
else { |
|||
SPIx.dmaSend(Data, Count, false); |
|||
} |
|||
|
|||
DataTransferEnd(); |
|||
} |
|||
|
|||
#endif // HAS_SPI_TFT
|
@ -0,0 +1,72 @@ |
|||
/**
|
|||
* 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/>.
|
|||
* |
|||
*/ |
|||
#pragma once |
|||
|
|||
#include "../../../inc/MarlinConfig.h" |
|||
|
|||
#include <SPI.h> |
|||
|
|||
#ifndef LCD_READ_ID |
|||
#define LCD_READ_ID 0x04 // Read display identification information (0xD3 on ILI9341)
|
|||
#endif |
|||
#ifndef LCD_READ_ID4 |
|||
#define LCD_READ_ID4 0xD3 // Read display identification information (0xD3 on ILI9341)
|
|||
#endif |
|||
|
|||
#define DATASIZE_8BIT DATA_SIZE_8BIT |
|||
#define DATASIZE_16BIT DATA_SIZE_16BIT |
|||
#define TFT_IO TFT_SPI |
|||
|
|||
#define DMA_MINC_ENABLE 1 |
|||
#define DMA_MINC_DISABLE 0 |
|||
|
|||
class TFT_SPI { |
|||
private: |
|||
static uint32_t ReadID(uint16_t Reg); |
|||
static void Transmit(uint16_t Data); |
|||
static void TransmitDMA(uint32_t MemoryIncrease, uint16_t *Data, uint16_t Count); |
|||
|
|||
public: |
|||
static SPIClass SPIx; |
|||
|
|||
static void Init(); |
|||
static uint32_t GetID(); |
|||
static bool isBusy(); |
|||
static void Abort(); |
|||
|
|||
static void DataTransferBegin(uint16_t DataWidth = DATA_SIZE_16BIT); |
|||
static void DataTransferEnd() { WRITE(TFT_CS_PIN, HIGH); SPIx.end(); }; |
|||
static void DataTransferAbort(); |
|||
|
|||
static void WriteData(uint16_t Data) { Transmit(Data); } |
|||
static void WriteReg(uint16_t Reg) { WRITE(TFT_A0_PIN, LOW); Transmit(Reg); WRITE(TFT_A0_PIN, HIGH); } |
|||
|
|||
static void WriteSequence(uint16_t *Data, uint16_t Count) { TransmitDMA(DMA_MINC_ENABLE, Data, Count); } |
|||
static void WriteMultiple(uint16_t Color, uint16_t Count) { static uint16_t Data; Data = Color; TransmitDMA(DMA_MINC_DISABLE, &Data, Count); } |
|||
static void WriteMultiple(uint16_t Color, uint32_t Count) { |
|||
static uint16_t Data; Data = Color; |
|||
while (Count > 0) { |
|||
TransmitDMA(DMA_MINC_DISABLE, &Data, Count > 0xFFFF ? 0xFFFF : Count); |
|||
Count = Count > 0xFFFF ? Count - 0xFFFF : 0; |
|||
} |
|||
} |
|||
}; |
@ -0,0 +1,141 @@ |
|||
/**
|
|||
* Marlin 3D Printer Firmware |
|||
* Copyright (c) 2019 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_TFT_XPT2046 || HAS_TOUCH_XPT2046 |
|||
|
|||
#include "xpt2046.h" |
|||
#include <SPI.h> |
|||
|
|||
uint16_t delta(uint16_t a, uint16_t b) { return a > b ? a - b : b - a; } |
|||
|
|||
#if ENABLED(TOUCH_BUTTONS_HW_SPI) |
|||
#include <SPI.h> |
|||
|
|||
SPIClass XPT2046::SPIx(TOUCH_BUTTONS_HW_SPI_DEVICE); |
|||
|
|||
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
|
|||
} |
|||
XPT2046::SPIx.setModule(TOUCH_BUTTONS_HW_SPI_DEVICE); |
|||
XPT2046::SPIx.setClockDivider(clock); |
|||
XPT2046::SPIx.setBitOrder(MSBFIRST); |
|||
XPT2046::SPIx.setDataMode(SPI_MODE0); |
|||
} |
|||
#endif // TOUCH_BUTTONS_HW_SPI
|
|||
|
|||
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 |
|||
|
|||
TERN_(TOUCH_BUTTONS_HW_SPI, touch_spi_init(SPI_SPEED_6)); |
|||
|
|||
// Read once to enable pendrive status pin
|
|||
getRawData(XPT2046_X); |
|||
} |
|||
|
|||
bool XPT2046::isTouched() { |
|||
return isBusy() ? false : ( |
|||
#if PIN_EXISTS(TOUCH_INT) |
|||
READ(TOUCH_INT_PIN) != HIGH |
|||
#else |
|||
getRawData(XPT2046_Z1) >= XPT2046_Z1_THRESHOLD |
|||
#endif |
|||
); |
|||
} |
|||
|
|||
bool XPT2046::getRawPoint(int16_t *x, int16_t *y) { |
|||
if (isBusy()) return false; |
|||
if (!isTouched()) return false; |
|||
*x = getRawData(XPT2046_X); |
|||
*y = getRawData(XPT2046_Y); |
|||
return isTouched(); |
|||
} |
|||
|
|||
uint16_t XPT2046::getRawData(const XPTCoordinate coordinate) { |
|||
uint16_t data[3]; |
|||
|
|||
DataTransferBegin(); |
|||
TERN_(TOUCH_BUTTONS_HW_SPI, SPIx.begin()); |
|||
|
|||
for (uint16_t i = 0; i < 3 ; i++) { |
|||
IO(coordinate); |
|||
data[i] = (IO() << 4) | (IO() >> 4); |
|||
} |
|||
|
|||
TERN_(TOUCH_BUTTONS_HW_SPI, SPIx.end()); |
|||
DataTransferEnd(); |
|||
|
|||
uint16_t delta01 = delta(data[0], data[1]), |
|||
delta02 = delta(data[0], data[2]), |
|||
delta12 = delta(data[1], data[2]); |
|||
|
|||
if (delta01 > delta02 || delta01 > delta12) |
|||
data[delta02 > delta12 ? 0 : 1] = data[2]; |
|||
|
|||
return (data[0] + data[1]) >> 1; |
|||
} |
|||
|
|||
uint16_t XPT2046::IO(uint16_t data) { |
|||
return TERN(TOUCH_BUTTONS_HW_SPI, HardwareIO, SoftwareIO)(data); |
|||
} |
|||
|
|||
#if ENABLED(TOUCH_BUTTONS_HW_SPI) |
|||
uint16_t XPT2046::HardwareIO(uint16_t data) { |
|||
uint16_t result = SPIx.transfer(data); |
|||
return result; |
|||
} |
|||
#endif |
|||
|
|||
uint16_t XPT2046::SoftwareIO(uint16_t data) { |
|||
uint16_t result = 0; |
|||
|
|||
for (uint8_t j = 0x80; j; j >>= 1) { |
|||
WRITE(TOUCH_SCK_PIN, LOW); |
|||
WRITE(TOUCH_MOSI_PIN, data & j ? HIGH : LOW); |
|||
if (READ(TOUCH_MISO_PIN)) result |= j; |
|||
WRITE(TOUCH_SCK_PIN, HIGH); |
|||
} |
|||
WRITE(TOUCH_SCK_PIN, LOW); |
|||
|
|||
return result; |
|||
} |
|||
|
|||
#endif // HAS_TFT_XPT2046
|
@ -0,0 +1,80 @@ |
|||
/**
|
|||
* Marlin 3D Printer Firmware |
|||
* Copyright (c) 2019 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/>.
|
|||
* |
|||
*/ |
|||
#pragma once |
|||
|
|||
#include "../../../inc/MarlinConfig.h" |
|||
|
|||
#if ENABLED(TOUCH_BUTTONS_HW_SPI) |
|||
#include <SPI.h> |
|||
#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 |
|||
#ifndef TOUCH_INT_PIN |
|||
#define TOUCH_INT_PIN -1 |
|||
#endif |
|||
|
|||
#define XPT2046_DFR_MODE 0x00 |
|||
#define XPT2046_SER_MODE 0x04 |
|||
#define XPT2046_CONTROL 0x80 |
|||
|
|||
enum XPTCoordinate : uint8_t { |
|||
XPT2046_X = 0x10 | XPT2046_CONTROL | XPT2046_DFR_MODE, |
|||
XPT2046_Y = 0x50 | XPT2046_CONTROL | XPT2046_DFR_MODE, |
|||
XPT2046_Z1 = 0x30 | XPT2046_CONTROL | XPT2046_DFR_MODE, |
|||
XPT2046_Z2 = 0x40 | XPT2046_CONTROL | XPT2046_DFR_MODE, |
|||
}; |
|||
|
|||
#if !defined(XPT2046_Z1_THRESHOLD) |
|||
#define XPT2046_Z1_THRESHOLD 10 |
|||
#endif |
|||
|
|||
class XPT2046 { |
|||
private: |
|||
static bool isBusy() { return false; } |
|||
|
|||
static uint16_t getRawData(const XPTCoordinate coordinate); |
|||
static bool isTouched(); |
|||
|
|||
static inline void DataTransferBegin() { WRITE(TOUCH_CS_PIN, LOW); }; |
|||
static inline void DataTransferEnd() { WRITE(TOUCH_CS_PIN, HIGH); }; |
|||
#if ENABLED(TOUCH_BUTTONS_HW_SPI) |
|||
static uint16_t HardwareIO(uint16_t data); |
|||
#endif |
|||
static uint16_t SoftwareIO(uint16_t data); |
|||
static uint16_t IO(uint16_t data = 0); |
|||
|
|||
public: |
|||
#if ENABLED(TOUCH_BUTTONS_HW_SPI) |
|||
static SPIClass SPIx; |
|||
#endif |
|||
|
|||
static void Init(); |
|||
static bool getRawPoint(int16_t *x, int16_t *y); |
|||
}; |
@ -1,167 +0,0 @@ |
|||
#include "w25q64.h" |
|||
|
|||
volatile uint8_t *spi_eeprom=shared_mem; |
|||
|
|||
volatile uint32_t spi_cr; |
|||
|
|||
void w25q_set_spi_speed(void){ |
|||
|
|||
spi_cr = SPI2->CR1; |
|||
spi2_init(SPI_FREQ_18Mhz); |
|||
|
|||
} |
|||
|
|||
void w25q_restore_spi_speed(void){ |
|||
|
|||
spi2_init(SPI_FREQ_280KHz); |
|||
|
|||
} |
|||
|
|||
|
|||
void w25q_init(void){ |
|||
uint8_t device_id, manuf_id; |
|||
uint16_t chip_id; |
|||
|
|||
dwt_init(); |
|||
|
|||
spi2_init(SPI_FREQ_4_5Mhz); |
|||
|
|||
//Wake up
|
|||
W25Q_START; |
|||
spi_send(W25X_ReleasePowerDown); |
|||
W25Q_STOP; |
|||
|
|||
for(uint32_t i=0; i<0x1000; i++){NOP;} //3us для выхода из power down
|
|||
|
|||
//Device ID
|
|||
W25Q_START; |
|||
spi_send(W25X_DeviceID); |
|||
for(uint32_t i=0; i<3; i++){spi_send(0);} |
|||
device_id = spi_send(0); |
|||
W25Q_STOP; |
|||
|
|||
//Jedec ID
|
|||
W25Q_START; |
|||
spi_send(W25X_JedecDeviceID); |
|||
manuf_id = spi_send(0); |
|||
chip_id = spi_send(0) << 8; |
|||
chip_id |= spi_send(0); |
|||
DEBUG("W25Q Device ID %0X Manuf ID: %0X Chip ID %0X",device_id,manuf_id,chip_id); |
|||
W25Q_STOP; |
|||
} |
|||
|
|||
void w25q_read(uint32_t addr, uint8_t *buf, uint32_t len){ |
|||
|
|||
if( (len == 0) || (len > SPI_EEPROM_SIZE) ){ |
|||
ERROR("Len size error: %d",len); |
|||
return; |
|||
} |
|||
|
|||
addr &= 0xFFFFFF; //24bit address
|
|||
|
|||
W25Q_START; |
|||
if(len == 1){ |
|||
spi_send(W25X_ReadData); |
|||
spi_send((addr >> 16) & 0xFF); |
|||
spi_send((addr >> 8) & 0xFF); |
|||
spi_send(addr & 0xFF); |
|||
}else{ |
|||
spi_send(W25X_FastReadData); |
|||
spi_send((addr >> 16) & 0xFF); |
|||
spi_send((addr >> 8) & 0xFF); |
|||
spi_send(addr & 0xFF); |
|||
spi_send(0); |
|||
} |
|||
|
|||
while (len--){ |
|||
*buf++ = spi_send(0); |
|||
} |
|||
W25Q_STOP; |
|||
} |
|||
|
|||
void w25q_write(uint32_t addr, uint8_t *buf, uint32_t len){ |
|||
|
|||
uint16_t bytes_in_page = SPIFLASH_PAGESIZE - (addr % SPIFLASH_PAGESIZE); |
|||
uint16_t offset = 0; |
|||
|
|||
addr &= 0xFFFFFF; //24bit address
|
|||
|
|||
while (w25q_read_status() & 1){ //Busy
|
|||
NOP; |
|||
} |
|||
//Write Enable
|
|||
|
|||
while (len > 0){ |
|||
uint16_t batch_size = (len <= bytes_in_page) ? len : bytes_in_page; |
|||
|
|||
W25Q_START; |
|||
spi_send(W25X_WriteEnable); |
|||
W25Q_STOP; |
|||
|
|||
while (w25q_read_status() & 1){ //Busy
|
|||
NOP; |
|||
} |
|||
|
|||
W25Q_START; |
|||
spi_send(W25X_PageProgram); |
|||
spi_send((addr >> 16) & 0xFF); |
|||
spi_send((addr >> 8) & 0xFF); |
|||
spi_send(addr & 0xFF); |
|||
|
|||
for (uint32_t i = 0; i < batch_size; i++){ |
|||
spi_send(((uint8_t*)buf)[offset + i]); |
|||
} |
|||
|
|||
W25Q_STOP; |
|||
|
|||
//wait till it's programmed
|
|||
while (w25q_read_status() & 2){ //Busy
|
|||
NOP; |
|||
} |
|||
|
|||
addr += batch_size; |
|||
offset += batch_size; |
|||
len -= batch_size; |
|||
bytes_in_page = SPIFLASH_PAGESIZE; |
|||
} |
|||
|
|||
} |
|||
|
|||
|
|||
uint8_t w25q_read_status(void){ |
|||
uint8_t data; |
|||
|
|||
W25Q_START; |
|||
spi_send(W25X_ReadStatusReg); |
|||
data = spi_send(0); |
|||
W25Q_STOP; |
|||
return data; |
|||
} |
|||
|
|||
void w25q_write_enable(void){ |
|||
//Write Enable
|
|||
W25Q_START; |
|||
spi_send(W25X_WriteEnable); |
|||
W25Q_STOP; |
|||
|
|||
while (w25q_read_status() & 1){ //Busy
|
|||
NOP; |
|||
} |
|||
} |
|||
|
|||
|
|||
void w25q_sector_erase(uint32_t addr){ |
|||
|
|||
//Erase 4K
|
|||
W25Q_START; |
|||
spi_send(W25X_SectorErase); |
|||
spi_send((addr >> 16) & 0xFF); |
|||
spi_send((addr >> 8) & 0xFF); |
|||
spi_send(addr & 0xFF); |
|||
W25Q_STOP; |
|||
|
|||
while (w25q_read_status() & 1){ //Busy
|
|||
NOP; |
|||
} |
|||
|
|||
} |
@ -1,44 +0,0 @@ |
|||
#ifndef W25Q_HAL_H |
|||
#define W25Q_HAL_H |
|||
#include "../../inc/MarlinConfig.h" |
|||
#include "small_spi.h" |
|||
#include "../../module/mks_wifi/dwt.h" |
|||
#include "../../module/shared_mem/shared_mem.h" |
|||
|
|||
#define SPI_HI_SPEED SPI_FREQ_18Mhz |
|||
#define SPI_LOW_SPEED SPI_FREQ_1_125Mhz |
|||
|
|||
#define W25X_WriteEnable 0x06 |
|||
#define W25X_WriteDisable 0x04 |
|||
#define W25X_ReadStatusReg 0x05 |
|||
#define W25X_WriteStatusReg 0x01 |
|||
#define W25X_ReadData 0x03 |
|||
#define W25X_FastReadData 0x0B |
|||
#define W25X_PageProgram 0x02 |
|||
#define W25X_BlockErase 0xD8 |
|||
#define W25X_SectorErase 0x20 |
|||
#define W25X_ChipErase 0xC7 |
|||
#define W25X_ReleasePowerDown 0xAB |
|||
#define W25X_DeviceID 0xAB |
|||
#define W25X_ManufactDeviceID 0x90 |
|||
#define W25X_JedecDeviceID 0x9F |
|||
|
|||
#define SPI_EEPROM_SIZE MARLIN_EEPROM_SIZE |
|||
|
|||
#define SPIFLASH_PAGESIZE 256 |
|||
#define SPI_TIMEOUT 2000 //таймаут на ожидание опереций
|
|||
#define CHECK_TIMEOUT do{if(dwt_get_timeout() == 0){ERROR("Timeout");return 0;}}while(0) |
|||
|
|||
extern volatile uint8_t *spi_eeprom; |
|||
|
|||
void w25q_init(void); |
|||
void w25q_read(uint32_t addr, uint8_t *buf, uint32_t len); |
|||
void w25q_write(uint32_t addr, uint8_t *buf, uint32_t len); |
|||
uint8_t w25q_read_status(void); |
|||
void w25q_write_enable(void); |
|||
void w25q_sector_erase(uint32_t addr); |
|||
|
|||
void w25q_set_spi_speed(void); |
|||
void w25q_restore_spi_speed(void); |
|||
|
|||
#endif |
@ -0,0 +1,58 @@ |
|||
/**
|
|||
* 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/>.
|
|||
* |
|||
*/ |
|||
|
|||
#include "../../inc/MarlinConfigPre.h" |
|||
|
|||
#if ENABLED(PASSWORD_FEATURE) |
|||
|
|||
#include "password.h" |
|||
#include "../../gcode/gcode.h" |
|||
#include "../../core/serial.h" |
|||
|
|||
Password password; |
|||
|
|||
// public:
|
|||
bool Password::is_set, Password::is_locked; |
|||
uint32_t Password::value, Password::value_entry; |
|||
|
|||
//
|
|||
// Authenticate user with password.
|
|||
// Called from Setup, after SD Prinitng Stops/Aborts, and M510
|
|||
//
|
|||
void Password::lock_machine() { |
|||
is_locked = true; |
|||
TERN_(HAS_LCD_MENU, authenticate_user(ui.status_screen, screen_password_entry)); |
|||
} |
|||
|
|||
//
|
|||
// Authentication check
|
|||
//
|
|||
void Password::authentication_check() { |
|||
if (value_entry == value) |
|||
is_locked = false; |
|||
else |
|||
SERIAL_ECHOLNPGM(STR_WRONG_PASSWORD); |
|||
|
|||
TERN_(HAS_LCD_MENU, authentication_done()); |
|||
} |
|||
|
|||
#endif // PASSWORD_FEATURE
|
@ -0,0 +1,57 @@ |
|||
/**
|
|||
* 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/>.
|
|||
* |
|||
*/ |
|||
#pragma once |
|||
|
|||
#include "../../lcd/ultralcd.h" |
|||
|
|||
class Password { |
|||
public: |
|||
static bool is_set, is_locked; |
|||
static uint32_t value, value_entry; |
|||
|
|||
Password() { is_locked = false; } |
|||
|
|||
static void lock_machine(); |
|||
|
|||
#if HAS_LCD_MENU |
|||
static void access_menu_password(); |
|||
static void authentication_check(); |
|||
static void authentication_done(); |
|||
static void media_gatekeeper(); |
|||
|
|||
private: |
|||
static void authenticate_user(const screenFunc_t, const screenFunc_t); |
|||
static void menu_password(); |
|||
static void menu_password_entry(); |
|||
static void screen_password_entry(); |
|||
static void screen_set_password(); |
|||
static void start_over(); |
|||
|
|||
static void digit_entered(); |
|||
static void set_password_done(); |
|||
static void menu_password_report(); |
|||
|
|||
static void remove_password(); |
|||
#endif |
|||
}; |
|||
|
|||
extern Password password; |
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue