Learn / Screen and touch · guide 10 of 10

Read the touch screen (I2C)

You read the finger position from the touch controller over I2C4 and draw a dot on the screen where you touch it.

The touch layer is a small chip behind the glass that talks I2C. This guide teaches I2C itself (address, register read, open-drain and pull-ups), shows how the board wires the chip, reads its ID, then reads touch points, first by polling and then with the interrupt line.

You will learn
  • How I2C works: two wires, an address per device, and how a register read looks on the wire
  • Why the lines are open-drain with pull-up resistors (open-drain, pull-up)
  • How the board wires the touch controller: shared bus I2C4 on PD12 and PD13, interrupt on PG2
  • How to set up the I2C4 peripheral (clock, pins, timing) and read registers from a device
  • How to use an EXTI interrupt line instead of polling
You need
  • STM32H750B-DK and a USB cable to CN14 (the ST-LINK connector)
  • The display guide finished (to draw the dot)
  • STM32CubeIDE with the STM32CubeH7 firmware package (the HAL part uses HAL_I2C_Mem_Read)

On this board

Pulled from the datasheet tables and the board schematic. Click a pin for everything the finder knows about it.

PinBallBoard netUsed here asAFOpen
PD12R16IIC4_SCLIIC4_SCL: I2C4 clock line, shared with the touch panel, audio codec, Arduino and STMod+ connectorsAF4 I2C4_SCL
PD13R17IIC4_SDAIIC4_SDA: I2C4 data line, same shared busAF4 I2C4_SDA
PG2H16LCD_INTLCD_INT: interrupt from the touch controller (active low, as in ST’s BSP)
PB12T14LCD_RSTLCD_RST: reset input of the panel module, according to the schematic
1

I2C in five minutes

I2C connects one master (your MCU) to many devices with just two wires: SCL (clock) and SDA (data). Every device has an address of 7 bits. The master starts a transfer by sending the address of the device it wants to talk to, and only that device answers.

Almost every I2C chip is built around registers: numbered one-byte cells inside the chip. To read register 0x02 the master does this on the wire:

  1. START, then the device address with the "write" bit: "I want to send you something."
  2. The register number, 0x02: "from here."
  3. A repeated START, then the address again with the "read" bit: "now I listen."
  4. The device sends its bytes, one after another. The master says ACK after each byte it wants more of, and NACK after the last one.
  5. STOP.

Every byte the device receives is acknowledged by pulling SDA low for one clock. A missing ACK means nobody is there at that address, and the peripheral reports it as a NACK flag. That is the first thing you will see when something is wrong, and it is a useful signal.

Two more things that surprise beginners. The address is 7 bits, but many libraries ask for it shifted left by one (the 8-bit form with the read/write bit in position 0). The touch chip at 7-bit address 0x38 is written 0x70 in the 8-bit form. And I2C lines are open-drain: a chip can pull the line low but never drives it high. A pull-up resistor on each line brings it high when nobody pulls. That is why you can connect many devices to one bus without short circuits, and why a bus without pull-ups does nothing at all. The board has them: you will find them on the schematic in the next step.

master:  START | 0x70 W | 0x02 |  RESTART | 0x71 R |  (reads 5 bytes)       | STOP
device:                ACK     ACK                ACK     b0 b1 b2 b3 b4
                                                  (master ACKs b0..b3, NACKs b4)

0x70 = 7-bit address 0x38 shifted left by one, with the read/write bit 0 (write)
0x71 = the same with the bit set (read)
2

What the board wires

The touch controller is part of the display module. Its connections come out on the LCD RGB connector:

  • SCL and SDA are the pins LCD_SCL and LCD_SDA. On the schematic they are the same nets as IIC4_SCL and IIC4_SDA of the MCU: PD12 and PD13, which carry I2C4 as alternate function 4. The user manual says it plainly: the audio codec communicates over an I2C bus shared with the touch panel and the Arduino and STMod+ connectors.
  • **The interrupt line LCD_INT goes to PG2**. The touch chip pulls it low when it has something to report, so the MCU does not have to ask all the time.
  • **The reset line LCD_RST goes to the panel module. On the schematic it sits on PB12**, and the I/O assignment table of the user manual agrees (the connector table, Table 10, leaves its pin column empty). ST's BSP source files never touch PB12, so it should not be needed for a first test.
  • The pull-ups. Look at sheet 3: each of IIC4_SCL and IIC4_SDA has a 1.5 kilohm resistor (R64 and R67) going up to the supply. That is the pull-up of the open-drain bus, so you must not switch the MCU's internal pull-ups on as well.

Which chip is it? The user manual only says "capacitive touch panel". Sheet 12 notes a default I2C address of 01110000 for the panel module. ST's BSP, which is not part of these documents, uses the FT5336 driver with the 8-bit address 0x70 (7-bit 0x38) for this panel, and the numbers match. Everything specific to the FT5336 in this guide, registers included, comes from ST's driver files (ft5336.c and ft5336_reg.h in the STM32CubeH7 package), not from the PDFs, and it is not verified on this board. If you have a logic analyser or scope, look at the bus once: it is the best way to see how I2C really works.

3

Set up I2C4

Four things to configure, each of which you know from the other guides:

  1. Clocks. I2C4EN in RCC_APB4ENR for the peripheral, and the GPIO port D clock for the pins. I2C4 also has its own kernel clock, chosen in RCC_D3CCIPR (I2C4SEL): after reset it is the APB4 bus clock, which changes with your clock setup. To make the timing independent of that, the code below selects the HSI (64 MHz, which does not move).
  2. The pins. PD12 and PD13 in alternate function 4, output type open-drain (GPIOx_OTYPER), no internal pull (GPIOx_PUPDR = 00).
  3. The timing. The bus speed is not a single divider but a register of five fields, I2C_TIMINGR: a prescaler (PRESC), the high and low times of SCL (SCLH, SCLL), and two small delays around the data edge (SCLDEL, SDADEL). The reference manual gives ready-made examples. For 100 kHz it lists SCLL = 0x13, SCLH = 0xF, SDADEL = 0x2 and SCLDEL = 0x4 with a 250 ns prescaler period. With the 64 MHz kernel clock a prescaler of PRESC = 15 gives 250 ns (16 / 64 MHz), so the register becomes 0xF0420F13. CubeMX has an I2C window that computes and checks this value for any clock.
  4. Switch it on. Write TIMINGR while PE in I2C_CR1 is 0, then set PE.

The next step uses it to read a register.

#include "stm32h7xx.h"
#include <stdint.h>

#define FT_ADDR7   0x38U          /* 7-bit address (ST's BSP writes it as 0x70, the 8-bit form) */

void i2c4_init(void)
{
    RCC->AHB4ENR |= RCC_AHB4ENR_GPIODEN;
    RCC->APB4ENR |= RCC_APB4ENR_I2C4EN;
    RCC->D3CCIPR = (RCC->D3CCIPR & ~RCC_D3CCIPR_I2C4SEL) | RCC_D3CCIPR_I2C4SEL_1;   /* kernel clock = hsi_ker_ck (64 MHz) */
    (void)RCC->D3CCIPR;

    /* PD12 = SCL, PD13 = SDA: alternate function 4, open-drain, no internal pull (the board has 1.5 k pull-ups) */
    GPIOD->MODER   = (GPIOD->MODER & ~(GPIO_MODER_MODE12_Msk | GPIO_MODER_MODE13_Msk))
                   | (2U << GPIO_MODER_MODE12_Pos) | (2U << GPIO_MODER_MODE13_Pos);
    GPIOD->OTYPER |= GPIO_OTYPER_OT12 | GPIO_OTYPER_OT13;
    GPIOD->PUPDR  &= ~(GPIO_PUPDR_PUPD12_Msk | GPIO_PUPDR_PUPD13_Msk);
    GPIOD->AFR[1]  = (GPIOD->AFR[1] & ~(0xFU << 16 | 0xFU << 20)) | (4U << 16) | (4U << 20);

    I2C4->CR1 &= ~I2C_CR1_PE;                              /* TIMINGR can only be written while PE = 0 */
    /* 100 kHz from a 64 MHz kernel clock. PRESC = 15 makes tPRESC = 16 / 64 MHz = 250 ns, then the same
       SCLL = 19, SCLH = 15, SCLDEL = 4, SDADEL = 2 as in the 100 kHz column of RM Tables 388 to 390. */
    I2C4->TIMINGR = (15U << I2C_TIMINGR_PRESC_Pos) | (4U << I2C_TIMINGR_SCLDEL_Pos) | (2U << I2C_TIMINGR_SDADEL_Pos)
                  | (15U << I2C_TIMINGR_SCLH_Pos) | (19U << I2C_TIMINGR_SCLL_Pos);
    I2C4->CR1 |= I2C_CR1_PE;
}

Tip. Not sure the bus works? Ask every possible address and see who answers. A scan loop with HAL_I2C_IsDeviceReady(&hi2c4, addr << 1, 1, 10) over 0x08 to 0x77 should find the touch chip at 0x38. The audio codec is on the same bus, so more than one address may answer.

4

Read a register: the chip ID

Time for the first real I2C transfer. The registers of the I2C peripheral map directly to the steps from the first section:

  • I2C_CR2 holds the transfer description: SADD (the device address, shifted left by one), RD_WRN (0 = write, 1 = read), NBYTES (how many bytes), AUTOEND (send STOP automatically at the end) and START (go).
  • I2C_ISR reports progress: TXIS (ready for the next byte to send), TC (transfer complete, bus held), RXNE (a byte arrived), STOPF (stop seen) and NACKF (the device did not answer).
  • I2C_TXDR and I2C_RXDR carry the data, I2C_ICR clears the flags.

A register read is two transfers in a row: write one byte (the register number) with AUTOEND off, wait for TC, then start a read with AUTOEND on. In the HAL it is one call, HAL_I2C_Mem_Read, which does exactly this.

A sensible first test is the chip ID. ST's FT5336 driver says register 0xA8 holds the ID and that the value for this chip is 0x51. If you read 0x51, you have proven the pins, the pull-ups, the timing and the address in one go. If you get a NACK or a timeout, the address is the first suspect, then the pins, then the clock.

static int wait_flag(uint32_t mask)                         /* 0 = flag set, -1 = NACK or timeout */
{
    for (uint32_t t = 0; t < 1000000U; t++) {
        if (I2C4->ISR & mask) return 0;
        if (I2C4->ISR & I2C_ISR_NACKF) return -1;
    }
    return -1;
}

/* Read n bytes starting at register reg of the device at 7-bit address addr7. */
int i2c4_read_regs(uint8_t addr7, uint8_t reg, uint8_t *buf, uint8_t n)
{
    /* 1. START, address + write, one byte: the register number */
    I2C4->CR2 = ((uint32_t)addr7 << 1) | (1U << I2C_CR2_NBYTES_Pos) | I2C_CR2_START;
    if (wait_flag(I2C_ISR_TXIS)) goto fail;
    I2C4->TXDR = reg;
    if (wait_flag(I2C_ISR_TC)) goto fail;                    /* transfer complete, bus held */

    /* 2. repeated START, address + read, n bytes, then STOP by itself (AUTOEND) */
    I2C4->CR2 = ((uint32_t)addr7 << 1) | I2C_CR2_RD_WRN | ((uint32_t)n << I2C_CR2_NBYTES_Pos)
              | I2C_CR2_AUTOEND | I2C_CR2_START;
    for (uint8_t i = 0; i < n; i++) {
        if (wait_flag(I2C_ISR_RXNE)) goto fail;
        buf[i] = (uint8_t)I2C4->RXDR;
    }
    if (wait_flag(I2C_ISR_STOPF)) goto fail;
    I2C4->ICR = I2C_ICR_STOPCF;
    return 0;

fail:
    I2C4->ICR = I2C_ICR_NACKCF | I2C_ICR_STOPCF;
    I2C4->CR2 |= I2C_CR2_STOP;
    return -1;
}

/* usage: uint8_t id = 0; i2c4_read_regs(FT_ADDR7, 0xA8, &id, 1);   expect id == 0x51 */
5

Read the touch point

With the ID right, reading a touch is one more register read. From ST's driver files, the chip keeps the current touch data in a block starting at register 0x02:

  • 0x02, TD_STATUS: the low four bits are the number of touch points (the chip can report up to 5). 0 means no finger.
  • 0x03 and 0x04: the X position of the first finger. 0x03 holds the high four bits (its top bits carry an event code that you can ignore here), 0x04 the low eight.
  • 0x05 and 0x06: the Y position, the same way.

Read all five bytes in one go (NBYTES = 5 starting at 0x02), mask and combine: x = ((b[1] & 0x0F) << 8) | b[2]. Each coordinate is a 12-bit number; for this 480 × 272 panel you should see values in the range of the screen size, which the corner test below confirms.

Which axis is which? The chip's X and Y may not match the screen's, depending on how the panel is mounted. ST's BSP has an orientation setting for exactly this reason. Do not guess: print the numbers (or draw them) while you touch the four corners, and then swap or mirror in code until the dot lands under your finger.

The loop in the main tab draws a small yellow dot at each touch point with the fill_rect from the display guide, and a green or red square in the corner shows whether the chip ID was found. Polling every 10 ms is plenty for a finger.

/* Touch point 1: TD_STATUS (0x02) holds the number of touches, then X high/low, Y high/low. */
int touch_read(uint16_t *x, uint16_t *y)       /* returns the number of touches, 0 = none, -1 = bus error */
{
    uint8_t b[5];
    if (i2c4_read_regs(FT_ADDR7, 0x02, b, 5)) return -1;
    uint8_t touches = b[0] & 0x0FU;
    if (touches == 0 || touches > 5) return 0;
    *x = (uint16_t)(((b[1] & 0x0FU) << 8) | b[2]);
    *y = (uint16_t)(((b[3] & 0x0FU) << 8) | b[4]);
    return touches;
}

Tip. Printing the raw numbers on the serial port from the UART guide is the quickest way to learn the axes: touch each corner and read the values.

6

Use the interrupt line instead of polling

Polling every 10 ms works, but the touch chip also has the LCD_INT line for exactly this. To use it:

  1. Tell the chip to use it. After reset ST's driver leaves the chip in polling mode, where it never touches the line. Writing 1 to its G_MODE register (0xA4) switches to interrupt-trigger mode (the value comes from ST's driver, not these documents). ST's BSP notes the line is active low.
  2. Configure PG2 as an interrupt input with a pull-up, triggered on the falling edge. Every GPIO pin can be an interrupt source through the EXTI block: pin number 2 means EXTI line 2, and SYSCFG_EXTICR1 chooses which port's pin 2 (here port G) feeds it. Then the edge is selected in EXTI_FTSR1, the line is unmasked in EXTI_CPUIMR1, and the interrupt for line 2 is enabled in the NVIC. The HAL does all of it in HAL_GPIO_Init with GPIO_MODE_IT_FALLING.
  3. Handle it. The interrupt handler runs when the edge arrives, checks and clears the flag (EXTI_CPUPR1; HAL_GPIO_EXTI_IRQHandler does it), and calls your callback. Set a flag there and do the I2C read in the main loop, not inside the interrupt: the read takes a while, and the HAL's polling I2C calls count their timeouts with SysTick, so a handler of equal or higher priority that waits for I2C can lock up.

The main loop then reads touch_pending and does the transfer from the previous step only when the chip asked for it. The rest of the time the CPU is free for the display or anything else.

volatile uint8_t touch_pending;

void touch_irq_init(void)
{
    GPIO_InitTypeDef io = {0};
    uint8_t gmode = 1;               /* FT5336 G_MODE register 0xA4: 1 = interrupt trigger mode (ST's driver) */
    HAL_I2C_Mem_Write(&hi2c4, FT_ADDR, 0xA4, I2C_MEMADD_SIZE_8BIT, &gmode, 1, 50);

    __HAL_RCC_GPIOG_CLK_ENABLE();
    io.Pin = GPIO_PIN_2; io.Mode = GPIO_MODE_IT_FALLING; io.Pull = GPIO_PULLUP;    /* LCD_INT is active low */
    HAL_GPIO_Init(GPIOG, &io);       /* also sets SYSCFG_EXTICR1, EXTI_FTSR1 and EXTI_CPUIMR1 */
    HAL_NVIC_SetPriority(EXTI2_IRQn, 5, 0);
    HAL_NVIC_EnableIRQ(EXTI2_IRQn);
}

void EXTI2_IRQHandler(void) { HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_2); }

void HAL_GPIO_EXTI_Callback(uint16_t pin)
{
    if (pin == GPIO_PIN_2) touch_pending = 1;     /* do the I2C read in the main loop, not here */
}

/* main loop:  if (touch_pending) { touch_pending = 0; touch_read(...); draw...; } */

Common mistakes

  • Using the 7-bit address where the HAL wants the 8-bit one. 0x38 does not answer; 0x70 (0x38 << 1) does. In the register code, SADD also takes the shifted form.
  • Turning on the MCU’s internal pull-ups for SCL and SDA. The board already has 1.5 kilohm resistors; extra pull-ups only add load.
  • Configuring the pins as push-pull. I2C needs open-drain on both lines (GPIOx_OTYPER = 1, or GPIO_MODE_AF_OD).
  • Forgetting that the I2C4 kernel clock moves with the system clock. A TIMINGR value computed for one clock gives a different speed after you change the clocks. Pin the kernel clock to the HSI, as the code does, or recompute TIMINGR (CubeMX does).
  • Reading the I2C inside the EXTI interrupt handler. Set a flag and read in the main loop.
  • Reading the registers in two separate transfers with a STOP in between. Reading 0x02 to 0x06 in one transfer keeps the point data consistent.
  • Assuming X is X. Check the corners and swap or mirror the axes in code if needed.
  • Trusting this guide for chip details. The FT5336 register numbers come from ST’s driver files and are not verified on this board; if the chip ID is not 0x51, read what you get and compare with ST’s ft5336_reg.h.

Check yourself

Try to answer before opening. If you get stuck, the pages linked above have the answer.

The chip answers to 0x70 in ST’s code. What is its 7-bit address, and what would you pass to HAL_I2C_Mem_Read?

The 7-bit address is 0x38 (0x70 is 0x38 shifted left by one). The HAL takes the shifted form, so you pass 0x70, written as 0x38U << 1.

Why do SCL and SDA need pull-up resistors, and where are they on this board?

The lines are open-drain: devices can only pull them low. Something must pull them high when nobody is pulling. The board has R64 and R67 (1.5 kilohm) on IIC4_SCL and IIC4_SDA on schematic sheet 3, so the MCU’s internal pull-ups are not needed.

You get a NACK on the very first transfer. Which three things do you check first?

The address (7-bit vs 8-bit form), the pin configuration (PD12 and PD13 in AF4, open-drain, clocks on), and the timing/clock (kernel clock selected in RCC_D3CCIPR and a TIMINGR value computed for it). A bus scan over all addresses tells you whether anything answers at all.

Which EXTI line does PG2 use, and which register picks port G for it?

The line number equals the pin number, so PG2 uses EXTI line 2. SYSCFG_EXTICR1 (field EXTI2) selects the port: port G for this board. The HAL sets it for you in HAL_GPIO_Init with GPIO_MODE_IT_FALLING.

Reading list

Every page this guide sent you to, by document. Reading them in order is a good revision.

RM0433RM0433 reference manual (STM32H742/743/753/750)p.353p.417p.470p.540p.540p.541p.545p.551p.772p.782p.783p.1951p.1954p.1971p.1982p.2003p.2006p.2009p.2011p.2014p.2015p.2016
DS12556STM32H750xB datasheetp.45
UM2488UM2488 STM32H745I/H750B-DK Discovery kit user manualp.14p.15p.19p.23
MB1381MB1381 Discovery board schematic (H750XB variant)sheet 3sheet 12sheet 12

Search next

Each of these opens the finder with pins, registers, schematic hits and document pages for the term.

Where to go next