My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

xpt2046.h 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. *
  18. */
  19. #pragma once
  20. #ifdef STM32F1xx
  21. #include <stm32f1xx_hal.h>
  22. #elif defined(STM32F4xx)
  23. #include <stm32f4xx_hal.h>
  24. #endif
  25. #include "../../../inc/MarlinConfig.h"
  26. // Not using regular SPI interface by default to avoid SPI mode conflicts with other SPI devices
  27. #if !PIN_EXISTS(TOUCH_MISO)
  28. #error "TOUCH_MISO_PIN is not defined."
  29. #elif !PIN_EXISTS(TOUCH_MOSI)
  30. #error "TOUCH_MOSI_PIN is not defined."
  31. #elif !PIN_EXISTS(TOUCH_SCK)
  32. #error "TOUCH_SCK_PIN is not defined."
  33. #elif !PIN_EXISTS(TOUCH_CS)
  34. #error "TOUCH_CS_PIN is not defined."
  35. #endif
  36. #ifndef TOUCH_INT_PIN
  37. #define TOUCH_INT_PIN -1
  38. #endif
  39. #define XPT2046_DFR_MODE 0x00
  40. #define XPT2046_SER_MODE 0x04
  41. #define XPT2046_CONTROL 0x80
  42. enum XPTCoordinate : uint8_t {
  43. XPT2046_X = 0x10 | XPT2046_CONTROL | XPT2046_DFR_MODE,
  44. XPT2046_Y = 0x50 | XPT2046_CONTROL | XPT2046_DFR_MODE,
  45. XPT2046_Z1 = 0x30 | XPT2046_CONTROL | XPT2046_DFR_MODE,
  46. XPT2046_Z2 = 0x40 | XPT2046_CONTROL | XPT2046_DFR_MODE,
  47. };
  48. #if !defined(XPT2046_Z1_THRESHOLD)
  49. #define XPT2046_Z1_THRESHOLD 10
  50. #endif
  51. #ifdef STM32F1xx
  52. #define __IS_DMA_ENABLED(__HANDLE__) ((__HANDLE__)->Instance->CCR & DMA_CCR_EN)
  53. #elif defined(STM32F4xx)
  54. #define __IS_DMA_ENABLED(__HANDLE__) ((__HANDLE__)->Instance->CR & DMA_SxCR_EN)
  55. #endif
  56. class XPT2046 {
  57. private:
  58. static SPI_HandleTypeDef SPIx;
  59. static DMA_HandleTypeDef DMAtx;
  60. static bool isBusy() { return SPIx.Instance ? __IS_DMA_ENABLED(&DMAtx) : false; }
  61. static uint16_t getRawData(const XPTCoordinate coordinate);
  62. static bool isTouched();
  63. static inline void DataTransferBegin() { if (SPIx.Instance) { HAL_SPI_Init(&SPIx); } WRITE(TOUCH_CS_PIN, LOW); };
  64. static inline void DataTransferEnd() { WRITE(TOUCH_CS_PIN, HIGH); };
  65. static uint16_t HardwareIO(uint16_t data);
  66. static uint16_t SoftwareIO(uint16_t data);
  67. static uint16_t IO(uint16_t data = 0) { return SPIx.Instance ? HardwareIO(data) : SoftwareIO(data); }
  68. public:
  69. static void Init();
  70. static bool getRawPoint(int16_t *x, int16_t *y);
  71. };