This is a simple library for the STMPE610 touchscreen controller used by the Adafruit 2.8" TFT LCD shield

Dependents:   EasyCAT_LAB

Committer:
EasyCAT
Date:
Wed May 26 19:08:27 2021 +0000
Revision:
0:b5211f22867b
first release

Who changed what in which revision?

UserRevisionLine numberNew contents of line
EasyCAT 0:b5211f22867b 1 //********************************************************************************************
EasyCAT 0:b5211f22867b 2 // *
EasyCAT 0:b5211f22867b 3 // This software is distributed as an example, "AS IS", in the hope that it could *
EasyCAT 0:b5211f22867b 4 // be useful, WITHOUT ANY WARRANTY of any kind, express or implied, included, but *
EasyCAT 0:b5211f22867b 5 // not limited, to the warranties of merchantability, fitness for a particular *
EasyCAT 0:b5211f22867b 6 // purpose, and non infringiment. In no event shall the authors be liable for any *
EasyCAT 0:b5211f22867b 7 // claim, damages or other liability, arising from, or in connection with this software. *
EasyCAT 0:b5211f22867b 8 // *
EasyCAT 0:b5211f22867b 9 //********************************************************************************************/
EasyCAT 0:b5211f22867b 10
EasyCAT 0:b5211f22867b 11 #include "SPI_STMPE610.h"
EasyCAT 0:b5211f22867b 12
EasyCAT 0:b5211f22867b 13
EasyCAT 0:b5211f22867b 14 #define REG_SYS_CTRL1 0x03 // reset control
EasyCAT 0:b5211f22867b 15
EasyCAT 0:b5211f22867b 16 #define REG_SYS_CTRL2 0x04 // clock control
EasyCAT 0:b5211f22867b 17
EasyCAT 0:b5211f22867b 18 #define REG_SPI_CFG 0x08 // SPI interface configuration
EasyCAT 0:b5211f22867b 19
EasyCAT 0:b5211f22867b 20 #define REG_CFG 0x41 // touchscreen controller configuration
EasyCAT 0:b5211f22867b 21 //
EasyCAT 0:b5211f22867b 22 #define CFG_1SAMPLE 0x00 //
EasyCAT 0:b5211f22867b 23 #define CFG_2SAMPLE 0x40 //
EasyCAT 0:b5211f22867b 24 #define CFG_4SAMPLE 0x80 //
EasyCAT 0:b5211f22867b 25 #define CFG_8SAMPLE 0xC0 //
EasyCAT 0:b5211f22867b 26 //
EasyCAT 0:b5211f22867b 27 #define CFG_DELAY_10US 0x00 //
EasyCAT 0:b5211f22867b 28 #define CFG_DELAY_50US 0x08 //
EasyCAT 0:b5211f22867b 29 #define CFG_DELAY_100US 0x10 //
EasyCAT 0:b5211f22867b 30 #define CFG_DELAY_500US 0x18 //
EasyCAT 0:b5211f22867b 31 #define CFG_DELAY_1MS 0x20 //
EasyCAT 0:b5211f22867b 32 #define CFG_DELAY_5MS 0x28 //
EasyCAT 0:b5211f22867b 33 #define CFG_DELAY_10MS 0x30 //
EasyCAT 0:b5211f22867b 34 #define CFG_DELAY_50MS 0x38 //
EasyCAT 0:b5211f22867b 35 //
EasyCAT 0:b5211f22867b 36 #define CFG_SETTLE_10US 0x00 //
EasyCAT 0:b5211f22867b 37 #define CFG_SETTLE_100US 0x01 //
EasyCAT 0:b5211f22867b 38 #define CFG_SETTLE_500US 0x02 //
EasyCAT 0:b5211f22867b 39 #define CFG_SETTLE_1MS 0x03 //
EasyCAT 0:b5211f22867b 40 #define CFG_SETTLE_5MS 0x04 //
EasyCAT 0:b5211f22867b 41 #define CFG_SETTLE_10MS 0x05 //
EasyCAT 0:b5211f22867b 42 #define CFG_SETTLE_50MS 0x06 //
EasyCAT 0:b5211f22867b 43 #define CFG_SETTLE_100MS 0x07 //
EasyCAT 0:b5211f22867b 44
EasyCAT 0:b5211f22867b 45 #define REG_CTRL 0x40 // touchscreen controller control
EasyCAT 0:b5211f22867b 46 //
EasyCAT 0:b5211f22867b 47 #define CTRL_EN 0x01 //
EasyCAT 0:b5211f22867b 48 #define CTRL_XY 0x02 //
EasyCAT 0:b5211f22867b 49
EasyCAT 0:b5211f22867b 50 #define REG_FIFO_SIZE 0x4C // FIFO size
EasyCAT 0:b5211f22867b 51
EasyCAT 0:b5211f22867b 52 #define REG_FIFO_STA 0x4B // FIFO control/status
EasyCAT 0:b5211f22867b 53
EasyCAT 0:b5211f22867b 54 #define REG_DATA_X 0x4D // X point value
EasyCAT 0:b5211f22867b 55 #define REG_DATA_Y 0x4F // Y point value
EasyCAT 0:b5211f22867b 56
EasyCAT 0:b5211f22867b 57
EasyCAT 0:b5211f22867b 58
EasyCAT 0:b5211f22867b 59 #define X_OFFSET 180
EasyCAT 0:b5211f22867b 60 #define X_GAIN 0.090
EasyCAT 0:b5211f22867b 61
EasyCAT 0:b5211f22867b 62 #define Y_OFFSET 207
EasyCAT 0:b5211f22867b 63 #define Y_GAIN 0.066
EasyCAT 0:b5211f22867b 64
EasyCAT 0:b5211f22867b 65
EasyCAT 0:b5211f22867b 66 SPI_STMPE610::SPI_STMPE610(PinName mosi, PinName miso, PinName sclk, PinName cs) :
EasyCAT 0:b5211f22867b 67 m_spi(mosi, miso, sclk), m_cs(cs, 1)
EasyCAT 0:b5211f22867b 68 {
EasyCAT 0:b5211f22867b 69
EasyCAT 0:b5211f22867b 70 m_spi.frequency(1000000) ; // SPI mode 0 - 8 bit - 1MHz
EasyCAT 0:b5211f22867b 71 m_spi.format(8, 0); //
EasyCAT 0:b5211f22867b 72
EasyCAT 0:b5211f22867b 73 Write8(REG_SYS_CTRL1, 0x02) ; // soft reset
EasyCAT 0:b5211f22867b 74 #if (MBED_MAJOR_VERSION != 2) //
EasyCAT 0:b5211f22867b 75 ThisThread::sleep_for(10ms); //
EasyCAT 0:b5211f22867b 76 #else //
EasyCAT 0:b5211f22867b 77 wait_ms(10); //
EasyCAT 0:b5211f22867b 78 #endif //
EasyCAT 0:b5211f22867b 79
EasyCAT 0:b5211f22867b 80 Write8(REG_SYS_CTRL2, 0x00) ; // turn on clocks
EasyCAT 0:b5211f22867b 81
EasyCAT 0:b5211f22867b 82 Write8(REG_SPI_CFG, 0x04); // autoincrement on
EasyCAT 0:b5211f22867b 83
EasyCAT 0:b5211f22867b 84 // touchscreen parameters
EasyCAT 0:b5211f22867b 85 Write8(REG_CFG, CFG_8SAMPLE | CFG_DELAY_1MS | CFG_SETTLE_5MS ) ;
EasyCAT 0:b5211f22867b 86
EasyCAT 0:b5211f22867b 87 Write8(REG_CTRL, CTRL_XY | CTRL_EN); // enable the touchscreen
EasyCAT 0:b5211f22867b 88 // we don't use pressure reporting
EasyCAT 0:b5211f22867b 89 }
EasyCAT 0:b5211f22867b 90
EasyCAT 0:b5211f22867b 91
EasyCAT 0:b5211f22867b 92 //--- write an 8 bits register -----
EasyCAT 0:b5211f22867b 93 //
EasyCAT 0:b5211f22867b 94 void SPI_STMPE610::Write8(uint8_t Addr, uint8_t Data)
EasyCAT 0:b5211f22867b 95 {
EasyCAT 0:b5211f22867b 96 m_cs = 0;
EasyCAT 0:b5211f22867b 97 m_spi.write(0x7F & Addr);
EasyCAT 0:b5211f22867b 98 m_spi.write(Data);
EasyCAT 0:b5211f22867b 99 m_cs = 1;
EasyCAT 0:b5211f22867b 100 }
EasyCAT 0:b5211f22867b 101
EasyCAT 0:b5211f22867b 102
EasyCAT 0:b5211f22867b 103 //--- read an 8 bits register ------
EasyCAT 0:b5211f22867b 104 //
EasyCAT 0:b5211f22867b 105 uint8_t SPI_STMPE610::Read8(uint8_t Addr)
EasyCAT 0:b5211f22867b 106 {
EasyCAT 0:b5211f22867b 107 uint8_t Data;
EasyCAT 0:b5211f22867b 108
EasyCAT 0:b5211f22867b 109 m_cs = 0;
EasyCAT 0:b5211f22867b 110 m_spi.write(0x80 | Addr);
EasyCAT 0:b5211f22867b 111 Data = m_spi.write(0x00);
EasyCAT 0:b5211f22867b 112 m_cs = 1;
EasyCAT 0:b5211f22867b 113
EasyCAT 0:b5211f22867b 114 return(Data);
EasyCAT 0:b5211f22867b 115 }
EasyCAT 0:b5211f22867b 116
EasyCAT 0:b5211f22867b 117 //--- read a 16 bits register ------
EasyCAT 0:b5211f22867b 118 //
EasyCAT 0:b5211f22867b 119 uint16_t SPI_STMPE610::Read16(uint8_t Addr)
EasyCAT 0:b5211f22867b 120 {
EasyCAT 0:b5211f22867b 121 uint16_t Data;
EasyCAT 0:b5211f22867b 122
EasyCAT 0:b5211f22867b 123 m_cs = 0;
EasyCAT 0:b5211f22867b 124 m_spi.write(0x80 | Addr);
EasyCAT 0:b5211f22867b 125 Data = m_spi.write(0x80);
EasyCAT 0:b5211f22867b 126 Data = (Data << 8) & 0xFF00;
EasyCAT 0:b5211f22867b 127 Data |= m_spi.write(0x00);
EasyCAT 0:b5211f22867b 128 m_cs = 1;
EasyCAT 0:b5211f22867b 129
EasyCAT 0:b5211f22867b 130 return(Data);
EasyCAT 0:b5211f22867b 131 }
EasyCAT 0:b5211f22867b 132
EasyCAT 0:b5211f22867b 133
EasyCAT 0:b5211f22867b 134 //--- get a raw point --------------
EasyCAT 0:b5211f22867b 135 //
EasyCAT 0:b5211f22867b 136 bool SPI_STMPE610::GetRawPoint(uint16_t *X, uint16_t *Y)
EasyCAT 0:b5211f22867b 137 {
EasyCAT 0:b5211f22867b 138 if(Read8(REG_FIFO_SIZE)) // if some data is available in the fifo
EasyCAT 0:b5211f22867b 139 {
EasyCAT 0:b5211f22867b 140 *Y = Read16(REG_DATA_X); // read X value
EasyCAT 0:b5211f22867b 141 *X = Read16(REG_DATA_Y); // read Y value
EasyCAT 0:b5211f22867b 142 // X and Y are exchanged because the display is horizontal
EasyCAT 0:b5211f22867b 143
EasyCAT 0:b5211f22867b 144 Write8(REG_FIFO_STA, 0x01); // reset the fifo
EasyCAT 0:b5211f22867b 145 Write8(REG_FIFO_STA, 0x00); // " "
EasyCAT 0:b5211f22867b 146
EasyCAT 0:b5211f22867b 147 //printf("X raw %d\n", *X);
EasyCAT 0:b5211f22867b 148 //printf("Y raw %d\n\n", *Y);
EasyCAT 0:b5211f22867b 149
EasyCAT 0:b5211f22867b 150 return true; // exit with a valid point
EasyCAT 0:b5211f22867b 151 }
EasyCAT 0:b5211f22867b 152
EasyCAT 0:b5211f22867b 153 else
EasyCAT 0:b5211f22867b 154 {
EasyCAT 0:b5211f22867b 155 return false; // exit with no valid point
EasyCAT 0:b5211f22867b 156 }
EasyCAT 0:b5211f22867b 157 }
EasyCAT 0:b5211f22867b 158
EasyCAT 0:b5211f22867b 159
EasyCAT 0:b5211f22867b 160 //--- get a normalized point -------
EasyCAT 0:b5211f22867b 161 //
EasyCAT 0:b5211f22867b 162 bool SPI_STMPE610::GetPoint(uint16_t *X, uint16_t *Y)
EasyCAT 0:b5211f22867b 163 {
EasyCAT 0:b5211f22867b 164 uint16_t RawX, RawY;
EasyCAT 0:b5211f22867b 165
EasyCAT 0:b5211f22867b 166 if(GetRawPoint (&RawX, &RawY)) // if a point is available
EasyCAT 0:b5211f22867b 167 {
EasyCAT 0:b5211f22867b 168 RawX = RawX - X_OFFSET; // remove the offset
EasyCAT 0:b5211f22867b 169 if ((int16_t)RawX < 0) //
EasyCAT 0:b5211f22867b 170 RawX = 0; //
EasyCAT 0:b5211f22867b 171 //
EasyCAT 0:b5211f22867b 172 RawY = RawY - Y_OFFSET; //
EasyCAT 0:b5211f22867b 173 if ((int16_t)RawY < 0) //
EasyCAT 0:b5211f22867b 174 RawY = 0; //
EasyCAT 0:b5211f22867b 175
EasyCAT 0:b5211f22867b 176 *X = X_GAIN * (float)RawX; // apply the gain factor
EasyCAT 0:b5211f22867b 177 *Y = Y_GAIN * (float)RawY; //
EasyCAT 0:b5211f22867b 178
EasyCAT 0:b5211f22867b 179 *Y = 240 - *Y; // we want the 0,0 point in the upper left corner
EasyCAT 0:b5211f22867b 180 if ((int16_t)*Y < 0) // so Y must be inverted
EasyCAT 0:b5211f22867b 181 *Y = 0; //
EasyCAT 0:b5211f22867b 182
EasyCAT 0:b5211f22867b 183 //printf("X %d\n", *X);
EasyCAT 0:b5211f22867b 184 //printf("Y %d\n\n", *Y);
EasyCAT 0:b5211f22867b 185
EasyCAT 0:b5211f22867b 186 return true; // exit with a valid point
EasyCAT 0:b5211f22867b 187 }
EasyCAT 0:b5211f22867b 188
EasyCAT 0:b5211f22867b 189 else
EasyCAT 0:b5211f22867b 190 {
EasyCAT 0:b5211f22867b 191 return false; // exit with no valid point
EasyCAT 0:b5211f22867b 192 }
EasyCAT 0:b5211f22867b 193 }
EasyCAT 0:b5211f22867b 194
EasyCAT 0:b5211f22867b 195
EasyCAT 0:b5211f22867b 196
EasyCAT 0:b5211f22867b 197