Skip to content

Instantly share code, notes, and snippets.

@ChuckM
Last active September 11, 2017 23:32
Show Gist options
  • Save ChuckM/2494109352b9042c0e57 to your computer and use it in GitHub Desktop.
Save ChuckM/2494109352b9042c0e57 to your computer and use it in GitHub Desktop.
STMPE811 Touch Controller example
/*
* Look at using the i2c functions in the utility library
*
* Copyright (c) 2015, Chuck McManis, all rights reserved.
*
* This example talks to the XY screen touch controller
* (its a peripheral already on the board) and prints
* the co-ordinates pressed on the serial port.
*/
#include <stdint.h>
#include <stdio.h>
#include <utilloc3/stm32/console.h>
#include <utilloc3/stm32/term.h>
#include <utilloc3/stm32/pins.h>
#include <utilloc3/stm32/clock.h>
#include <utilloc3/stm32/i2c.h>
#include "stmpe811.h"
/*
* Data about the chip on the Discovery board ...
* USER Manual:
* i2c (3) SCLK - PA8
* i2c (3) SDA - PC9
* Interrupt - PA15
*
*/
/*
* Initialization lifted from the App Note #2807
*/
uint8_t init_sequence[] = {
STMPE_REG_SYS_CTRL2, 0x0c,
STMPE_REG_INT_EN, 0x07, /* group reading */
STMPE_REG_ADC_CTRL1, 0x49,
STMPE_REG_ADC_CTRL2, 0x01,
STMPE_REG_GPIO_AF, 0x00,
STMPE_REG_TSC_CFG, 0x9a,
STMPE_REG_FIFO_TH, 0x5,
STMPE_REG_FIFO_STA, 0x1, /* reset the FIFO */
STMPE_REG_FIFO_STA, 0x0,
STMPE_REG_TSC_FRACTION_Z, 0x7,
STMPE_REG_TSC_I_DRIVE, 0x1,
STMPE_REG_TSC_CTRL, 1,
STMPE_REG_INT_STA, 0xff,
STMPE_REG_INT_CTRL, 0x1,
0, 0
};
int
main(void)
{
uint32_t rval;
int chan;
int i;
clock_setup(168000000, 8000000);
console(PA9, PA10, 115200);
printf("I2C Example code.\n");
printf("Version 0.5\n");
chan = i2c_master(PA8, PC9, I2C_400KHZ);
pin_input(PA15, PXX); /* interrupt pin */
pin_attributes(PIN_PULLUP | PIN_OPENDRAIN, PA8, PC9, PA15, PXX);
pin_output(PD10, PD14, PXX);
pin_clear(PD10, PD14, PXX);
i = read_register_i2c(chan, STMPE_ADDR, STMPE_REG_CHIP_ID, &rval, 2);
if (i < 0) {
printf("Touch Screen controller didn't respond.\n");
while (1);
}
if (rval != 0x811) {
printf("I don't think this is an STMPE811, expected 0x811 but got 0x%x\n",
(unsigned int) rval);
while(1);
}
printf("Initializing touch screen controller.\n");
for (i = 0; init_sequence[i] != 0; i += 2) {
printf("Init: 0x%x => 0x%x\n", (unsigned int) init_sequence[i],
(unsigned int) init_sequence[i+1]);
write_register_i2c(chan, STMPE_ADDR, init_sequence[i], init_sequence[i+1], 1);
if (init_sequence[i] == STMPE_REG_ADC_CTRL1) {
msleep(2);
}
}
printf("Init sequence complete, waiting for touch events\n");
while(1) {
uint32_t reg;
uint32_t touch_xyz;
uint32_t cnt;
uint32_t x, y;
int err;
/* wait for an event (you could take an interrupt here) */
while (pin_get(PA15, PXX) == 1) ;
err = read_register_i2c(chan, STMPE_ADDR, STMPE_REG_INT_STA, &reg, 1);
if (err < 0) {
printf("i2c crashed.\n");
while (1) ;
}
/* call out the events detected, we care about 'touch' and stuff
* showing up in the FIFO, we alert on FIFO overflow or filling
* up completely.
*/
if (reg & 0x1) {
printf("Touch detect!\n");
}
if (reg & 0x2) {
read_register_i2c(chan, STMPE_ADDR, STMPE_REG_FIFO_SIZE, &cnt, 1);
x = 0; y = 0;
/* Read value out of the fifo */
while (cnt-- > 0) {
read_register_i2c(chan, STMPE_ADDR, STMPE_REG_TSC_DATA_XYZ, &touch_xyz, 4);
}
/* only keep the last one, its a demo */
y = (touch_xyz >> 8) & 0xfff;
x = (touch_xyz >> 20) & 0xfff;
printf("Touched Point X: %d, Y: %d\n", (int) x, (int) y);
read_register_i2c(chan, STMPE_ADDR, STMPE_REG_FIFO_SIZE, &cnt, 1);
}
if (reg & 0x4) {
printf("Warning: FIFO overflow, lost some touch points.\n");
}
if (reg & 0x8) {
printf("Warning: FIFO FULL!, lost some touch points.\n");
}
/* reset all the bits that were set */
write_register_i2c(chan, STMPE_ADDR, STMPE_REG_INT_STA, reg, 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment