Metal Detector
 
Loading...
Searching...
No Matches
xpt2046.c File Reference

Implementation file for XPT2046 touch screen controller driver. More...

#include "xpt2046.h"
#include "ili9341.h"

Macros

#define CMD_X_READ   0b10010000
 
#define CMD_Y_READ   0b11010000
 

Functions

static uint8_t transmit_receive_byte (uint8_t data)
 Transmits and receives a byte via SPI.
 
static void manage_cs (uint8_t state)
 Manages the chip select (CS) pin state.
 
static void draw_target (uint32_t x, uint32_t y, uint16_t color)
 Draws a target on the screen at the specified coordinates.
 
uint8_t XPT2046_ReadIRQPin (void)
 Reads the IRQ pin state of the XPT2046.
 
void XPT2046_ReadData (XPT2046Handle_t *handle)
 Reads calibrated touch coordinates from the XPT2046.
 
void XPT2046_ReadRawData (uint16_t *x, uint16_t *y)
 Reads raw touch data from the XPT2046.
 
void XPT2046_IRQHandler (XPT2046Handle_t *handle)
 Handles touch interrupt for the XPT2046.
 
void XPT2046_Calibrate (XPT2046Handle_t *handle)
 Calibrates the XPT2046 touch controller.
 
void XPT2046_SetCalibrationValues (XPT2046Handle_t *handle, XPT2046CalibrationValues_t *x_calib, XPT2046CalibrationValues_t *y_calib)
 Sets the calibration values for the XPT2046 touch controller.
 
void XPT2046_GetCalibrationValues (XPT2046Handle_t *handle, XPT2046CalibrationValues_t *x_calib, XPT2046CalibrationValues_t *y_calib)
 Gets the calibration values from the XPT2046 touch controller.
 
void XPT2046_Test (XPT2046Handle_t *handle)
 Tests the XPT2046 touch controller.
 
 __attribute__ ((weak))
 Weak callback function for the XPT2046 touch controller.
 

Variables

SPI_HandleTypeDef * xpt2046_spi
 
static const float XPT2046_VER_COEF = (float)XPT2046_VER_RES / 4096
 
static const float XPT2046_HOR_COEF = (float)XPT2046_HOR_RES / 4096
 

Detailed Description

Implementation file for XPT2046 touch screen controller driver.

This file contains the implementation of the XPT2046 touch screen controller driver. It includes functions for reading touch coordinates, handling interrupts, calibrating the touch screen, and performing basic tests. The calibration procedure is implemented based on Texas Instruments' application note SLYT277 "Calibration in touch-screen systems".

See also
https://www.ti.com/lit/an/slyt277/slyt277.pdf for calibration details.

Macro Definition Documentation

◆ CMD_X_READ

#define CMD_X_READ   0b10010000

Command for reading X position

◆ CMD_Y_READ

#define CMD_Y_READ   0b11010000

Command for reading Y position

Function Documentation

◆ __attribute__()

__attribute__ ( (weak) )

Weak callback function for the XPT2046 touch controller.

This function is a weakly defined callback that can be overridden by the user. It is called when a touch event is processed.

Parameters
handlePointer to the XPT2046 handle structure.

◆ draw_target()

static void draw_target ( uint32_t x,
uint32_t y,
uint16_t color )
static

Draws a target on the screen at the specified coordinates.

This function draws a small target at the specified (x, y) coordinates on the screen with the given color.

Parameters
xX coordinate of the target.
yY coordinate of the target.
colorColor of the target.

◆ manage_cs()

static void manage_cs ( uint8_t state)
static

Manages the chip select (CS) pin state.

This function sets the state of the CS pin to either low (active) or high (inactive).

Parameters
stateDesired state of the CS pin (GPIO_PIN_RESET or GPIO_PIN_SET).

◆ transmit_receive_byte()

static uint8_t transmit_receive_byte ( uint8_t data)
static

Transmits and receives a byte via SPI.

This function transmits a byte of data via SPI and receives a byte of data in response.

Parameters
dataByte of data to be transmitted.
Return values
uint8_tByte of data received in response.

◆ XPT2046_Calibrate()

void XPT2046_Calibrate ( XPT2046Handle_t * handle)

Calibrates the XPT2046 touch controller.

This function performs calibration of the XPT2046 touch controller using three reference points on the screen. The calibration process is based on the method described in the TI application note SLYT277.

Parameters
handlePointer to the XPT2046 handle structure.

◆ XPT2046_GetCalibrationValues()

void XPT2046_GetCalibrationValues ( XPT2046Handle_t * handle,
XPT2046CalibrationValues_t * x_calib,
XPT2046CalibrationValues_t * y_calib )

Gets the calibration values from the XPT2046 touch controller.

This function retrieves the current calibration values from the XPT2046 touch controller.

Parameters
handlePointer to the XPT2046 handle structure.
x_calibPointer to store the calibration values for the X axis.
y_calibPointer to store the calibration values for the Y axis.

◆ XPT2046_IRQHandler()

void XPT2046_IRQHandler ( XPT2046Handle_t * handle)

Handles touch interrupt for the XPT2046.

This function is the interrupt handler for the XPT2046 touch controller. It reads the touch data and updates the touch state.

Parameters
handlePointer to the XPT2046 handle structure.

◆ XPT2046_ReadData()

void XPT2046_ReadData ( XPT2046Handle_t * handle)

Reads calibrated touch coordinates from the XPT2046.

This function reads raw touch data from the XPT2046 and applies calibration coefficients to convert them to screen coordinates.

Parameters
handlePointer to the XPT2046 handle structure.

◆ XPT2046_ReadIRQPin()

uint8_t XPT2046_ReadIRQPin ( void )

Reads the IRQ pin state of the XPT2046.

This function reads the state of the IRQ pin of the XPT2046 touch controller.

Return values
uint8_t1 if the pin is high, 0 if the pin is low.

◆ XPT2046_ReadRawData()

void XPT2046_ReadRawData ( uint16_t * x,
uint16_t * y )

Reads raw touch data from the XPT2046.

This function reads the raw touch data from the XPT2046 touch controller by sending appropriate commands via SPI and processing the received data. It implements a simple averaging filter to improve the touch data quality.

Parameters
xPointer to store the raw x-coordinate.
yPointer to store the raw y-coordinate.

◆ XPT2046_SetCalibrationValues()

void XPT2046_SetCalibrationValues ( XPT2046Handle_t * handle,
XPT2046CalibrationValues_t * x_calib,
XPT2046CalibrationValues_t * y_calib )

Sets the calibration values for the XPT2046 touch controller.

This function updates the calibration values for the XPT2046 touch controller.

Parameters
handlePointer to the XPT2046 handle structure.
x_calibPointer to the calibration values for the X axis.
y_calibPointer to the calibration values for the Y axis.

◆ XPT2046_Test()

void XPT2046_Test ( XPT2046Handle_t * handle)

Tests the XPT2046 touch controller.

This function tests the XPT2046 touch controller by reading the touch coordinates and drawing a target on the screen at the touch location.

Parameters
handlePointer to the XPT2046 handle structure.

Variable Documentation

◆ xpt2046_spi

SPI_HandleTypeDef* xpt2046_spi
extern

SPI handle for the XPT2046 touch screen