Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change init for KeDei 3.5" 480x320 V6.3 2018/4/9 #76

Open
wants to merge 62 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
d0ec8c8
Change init for KeDei 3.5" 480x320 V6.3 2018/4/9
kpishere Mar 13, 2019
756190d
apply suggested changes of juj
kpishere Mar 27, 2019
063b60d
Merge branch 'master' of https://github.com/juj/fbcp-ili9341
kpishere Apr 28, 2019
710fcf4
split spi user and kernel parts
kpishere May 11, 2019
5b155f3
split spi file for kernal and user space
kpishere May 11, 2019
9a07fed
both kernel and user code build -- kernal driver does nothing
kpishere May 11, 2019
9f24b56
reduce kernal module to what is needed
kpishere May 11, 2019
54ccff2
kernal part building again and outputting interrupts on screen touch
kpishere May 17, 2019
6495917
add hooks in client to read interrupt from kernel
kpishere May 17, 2019
f832499
user space experimental file writingwq
kpishere May 18, 2019
9f90f5e
read/write working nicely with kernal!
kpishere May 20, 2019
cffb099
kind of works but there is a bug
kpishere May 21, 2019
61424ad
alright, data flows but values not right. Also, touch only responsiv…
kpishere May 25, 2019
6f413a4
no blank screen, looks good! Only get one measurement between cold re…
kpishere May 26, 2019
90b3d99
it is now WORKING! But, is way too slow. Appears that interrupt must…
kpishere May 28, 2019
dcf2b71
kind of working, stil feedback loop on interrupt slows it down
kpishere May 30, 2019
1dcc1a9
it works! touch screen and fast driver.
kpishere May 31, 2019
31485d9
Update README.md
kpishere May 31, 2019
23b846c
timer for poling touch display and a bunch of debugging printf() stat…
kpishere Jun 15, 2019
b773147
Merge branch 'master' of https://github.com/kpishere/fbcp-ili9341
kpishere Jun 15, 2019
d0fd72e
more messing about
kpishere Jun 15, 2019
9e056cf
Added noop when in busy loop, touch works when not drawing
kpishere Jun 17, 2019
5c6e808
tidy up, adjust WAIT delay, enable awake on touch of screen
kpishere Jun 18, 2019
04483f5
blanks screen on inactivity but backlight still on
kpishere Jun 19, 2019
680e33b
added macros for documentation from datasheet
kpishere Jun 20, 2019
5cf3f60
set orientation to match display
kpishere Jun 21, 2019
69754d3
calibration utillity
kpishere Jun 24, 2019
1c4a318
added calibration fnction
kpishere Jun 25, 2019
f3234db
cleanup
kpishere Jun 25, 2019
4cb0e13
cleanup
kpishere Jun 25, 2019
cb182e7
.
kpishere Jun 25, 2019
6a062aa
retain intermediate changes
kpishere Aug 30, 2019
809a41a
Merge branch 'master' of https://github.com/juj/fbcp-ili9341
kpishere Aug 30, 2019
81d95b4
calibration components
kpishere Aug 30, 2019
ff4bead
Merge branch 'master' of https://github.com/kpishere/fbcp-ili9341
kpishere Aug 30, 2019
1965c9f
reamed to cpp
kpishere Aug 30, 2019
e9449c8
Merge branch 'master' of https://github.com/juj/fbcp-ili9341
kpishere Sep 5, 2019
59a92f3
functional calibration utillity with bugs
kpishere Sep 6, 2019
e7d91bc
calibration utility with bugs
kpishere Sep 6, 2019
c6fc74c
Merge branch 'master' of https://github.com/kpishere/fbcp-ili9341
kpishere Sep 6, 2019
438237f
fix merge conflicts
kpishere Sep 6, 2019
972a908
much better response math issue still
kpishere Sep 11, 2019
8901482
calibration utillity working
kpishere Sep 18, 2019
6732e57
add calibration in driver
kpishere Sep 18, 2019
5549b5e
calibration now in driver
kpishere Sep 22, 2019
f83855c
calibration now in driver, readme update
kpishere Sep 22, 2019
fad48e4
calibration utillity complete
kpishere Sep 26, 2019
6f2f007
type-o fix in readme
kpishere Sep 26, 2019
ded9465
done with backlight
kpishere Sep 30, 2019
3b950b8
update Kedei comments
kpishere Oct 2, 2019
be9e548
change to MIT
Links2004 Oct 2, 2019
9b54d8a
Update XPT2046.h
Links2004 Oct 2, 2019
3624ad3
Merge pull request #1 from Links2004/master
kpishere Oct 2, 2019
3eab455
some cosmetic changes for the util part:
Kunzol Oct 5, 2019
317d4ea
full fletched Makefile for kernel with features:
Kunzol Oct 6, 2019
c55dc62
Bugfix: make clean
Kunzol Oct 6, 2019
918c11d
Merge pull request #2 from Kunzol/util_fixes
kpishere Oct 7, 2019
e2ed474
Merge pull request #3 from Kunzol/kernel_makefile
kpishere Oct 7, 2019
82739e0
A first try to configure the repo for building a debian package.
Kunzol Oct 12, 2019
ddc55b7
Merge pull request #4 from Kunzol/debian_package
kpishere Oct 14, 2019
7cced72
remove bloker for uninstalled state
kpishere Oct 29, 2019
b96c4d1
rehide commands
kpishere Oct 29, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@
*.S
*.symvers
*.order
kernel/bcm2835_spi_display.mod.c
build
.DS_Store
util/tcCalib
util/tcTest
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,4 @@ endif()

add_executable(fbcp-ili9341 ${sourceFiles})

target_link_libraries(fbcp-ili9341 pthread bcm_host atomic)
target_link_libraries(fbcp-ili9341 pthread bcm_host atomic rt)
216 changes: 112 additions & 104 deletions README.md

Large diffs are not rendered by default.

290 changes: 290 additions & 0 deletions XPT2046.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,290 @@
/**
* @file XPT2046.cpp
* @date 19.02.2016
* @author Markus Sattler
*
* Copyright (c) 2015 Markus Sattler. All rights reserved.
* This file is originally part of the XPT2046 driver for Arduino.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the “Software”), to deal in the Software without restriction,
* including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/

#include "XPT2046.h"

#include <sys/types.h>
#include <stdio.h>

#define XPT2046_CFG_START 1<<7

#define XPT2046_CFG_MUX(v) ((v&0b111) << (4))

#define XPT2046_CFG_8BIT 1<<3
#define XPT2046_CFG_12BIT (0)

#define XPT2046_CFG_SER 1<<2
#define XPT2046_CFG_DFR (0)

#define XPT2046_CFG_PWR(v) ((v&0b11))

#define XPT2046_MUX_Y 0b101
#define XPT2046_MUX_X 0b001

#define XPT2046_MUX_Z1 0b011
#define XPT2046_MUX_Z2 0b100

#define INTERRUPTDEC 10
#define MAXLINE 128

XPT2046::XPT2046() {
spi_cs = 0;
z_average = 0;
tcfifo = "/tmp/TCfifo";
calibFile = "/etc/xpt2046.conf";

_maxValue = 0x0fff;
_width = 480;
_height = 320;
_rotation = 0;

_minX = 0;
_minY = 0;

_maxX = _maxValue;
_maxY = _maxValue;

_lastX = -1;
_lastY = -1;

_minChange = 10;

interruptpoll = INTERRUPTDEC;

spi_cs =
1 << 0 | //Chip select 1
0 << 3 | //low idle clock polarity
0 << 6 | //chip select active low
0 << 22 | //chip select low polarity
0 << 8 | //DMA disabled
0 << 11; //Manual chip select

// FIFO file path
// Creating the named file(FIFO)
// mkfifo(<pathname>, <permission>)
mkfifo(tcfifo, 0666);
initCalibration();
}


XPT2046::~XPT2046() {
}

int XPT2046::SpiWriteAndRead(unsigned char *data, int length)
{
for (int i = 0; i < length; i++)
{
spi->cs = spi_cs | 1 << 7;//Set TA to high

while (!(spi->cs & (1 << 18))) ; //Poll TX Fifo until it has space

spi->fifo = data[i];

while(! (spi->cs & (1 << 17))) ; //Wait until RX FIFO contains data

uint32_t spi_fifo = spi->fifo;

spi->cs = (spi_cs & (~(1 << 7))) | 1 << 5 | 1 << 4; //Set TA to LOW //Clear Fifo

data[i] = spi_fifo;
}

return 0;
}

void XPT2046::read_touchscreen(bool interruptEnable) {
uint16_t x, y, z;

uint32_t old_spi_cs = spi->cs;
uint32_t old_spi_clk = spi->clk;
//printBits(sizeof(old_spi_cs), (void*)&(old_spi_cs));
spi->clk = 256;

// touch on low
read(&x, &y, &z);
if (abs(x - _lastX) > _minChange || abs(y - _lastY) > _minChange) {
_lastX = x;
_lastY = y;
}

if(interruptEnable) {
// SPI requires 32bit alignment
uint8_t buf[3] = {
// re-enable interrupt
(XPT2046_CFG_START | XPT2046_CFG_12BIT | XPT2046_CFG_DFR | XPT2046_CFG_MUX(XPT2046_MUX_Z2)| XPT2046_CFG_PWR(0)), 0x00, 0x00
};
SpiWriteAndRead(buf, 3);
}

if (z > 100)
{
uint16_t words16Write[4] = { x, y, z, 0x0000FFFF};
fd = open(tcfifo, O_WRONLY | O_NONBLOCK);

write(fd, words16Write, 4 * sizeof(uint16_t));
close(fd);
this->lastTouchTick = tick();
}

//spi->cs = (old_spi_cs | 1 << 4 | 1 << 5) & (~(1 << 7)); //Clear Fifos and TA
spi->cs = old_spi_cs;
spi->clk = old_spi_clk;
}

void XPT2046::setRotation(uint8_t m) {
_rotation = m % 4;
}

void XPT2046::initCalibration() {
FILE *stream;
char *line = NULL;
int reti;
size_t len = 0;
ssize_t nread;

stream = fopen(calibFile, "r");
if(stream != NULL)
{
while ((nread = getline(&line, &len, stream)) != -1) {
//fprintf(stdout,"scanline: '%s'", line);
reti = sscanf((const char*)line,"%d,%d,%d,%d,%d,%d,%d",
&calib.An, &calib.Bn, &calib.Cn, &calib.Dn, &calib.En, &calib.Fn,&calib.Divider);
}
free(line);
fclose(stream);
} else { // Make standard identity calibration -- no translation
// Target on-screen points
POINT pointTargets[3] = {
{15, 15},
{-15, 15},
{15, -15},
};
// Set curser values initially
// Update for screen dimensions
for(int i = 0; i<3;i++ ) {
pointTargets[i].x = (pointTargets[i].x < 0 ? _width + pointTargets[i].x : pointTargets[i].x);
pointTargets[i].y = (pointTargets[i].y < 0 ? _height + pointTargets[i].y : pointTargets[i].y);
fprintf(stdout, "%d,%d\n", pointTargets[i].x, pointTargets[i].y);
}
setCalibrationMatrix( (POINT*)pointTargets, (POINT*)pointTargets, &calib); // initialized 1:1 matrix
}
fprintf(stdout,"M: %d,%d,%d,%d,%d,%d,%d\r\n"
,calib.An,calib.Bn,calib.Cn,calib.Dn,calib.En,calib.Fn,calib.Divider );
}


void XPT2046::read(uint16_t * oX, uint16_t * oY, uint16_t * oZ) {
uint16_t x, y;
POINT pointCorrected, pointRaw;

readRaw(&x, &y, oZ);

pointRaw.x = x;
pointRaw.y = y;

if(pointRaw.x < _minX) {
pointRaw.x = 0;
} else if(pointRaw.x > _maxX) {
pointRaw.x = _width;
} else {
pointRaw.x -= _minX;
pointRaw.x = ((pointRaw.x << 8) / (((_maxX - _minX) << 8) / (_width << 8)) )>> 8;
}

if(pointRaw.y < _minY) {
pointRaw.y = 0;
} else if(pointRaw.y > _maxY) {
pointRaw.y = _height;
} else {
pointRaw.y -= _minY;
pointRaw.y = ((pointRaw.y << 8) / (((_maxY - _minY) << 8) / (_height << 8))) >> 8;
}

getDisplayPoint((POINT *)&pointCorrected,(POINT *)&pointRaw,&calib);

*oX = pointCorrected.x;
*oY = pointCorrected.y;
*oZ = std::max(0,4096 - (int)(*oZ));
}

void XPT2046::readRaw(uint16_t * oX, uint16_t * oY, uint16_t * oZ) {
uint32_t x = 0;
uint32_t y = 0;
uint32_t z1 = 0;
uint32_t z2 = 0;
uint8_t i = 0;

for(; i < 4; i++) { // Sampling
// SPI requires 32bit alignment
uint8_t buf[12] = {
(XPT2046_CFG_START | XPT2046_CFG_12BIT | XPT2046_CFG_DFR | XPT2046_CFG_MUX(XPT2046_MUX_Y) | XPT2046_CFG_PWR(3)), 0x00, 0x00,
(XPT2046_CFG_START | XPT2046_CFG_12BIT | XPT2046_CFG_DFR | XPT2046_CFG_MUX(XPT2046_MUX_X) | XPT2046_CFG_PWR(3)), 0x00, 0x00,
(XPT2046_CFG_START | XPT2046_CFG_12BIT | XPT2046_CFG_DFR | XPT2046_CFG_MUX(XPT2046_MUX_Z1)| XPT2046_CFG_PWR(3)), 0x00, 0x00,
(XPT2046_CFG_START | XPT2046_CFG_12BIT | XPT2046_CFG_DFR | XPT2046_CFG_MUX(XPT2046_MUX_Z2)| XPT2046_CFG_PWR(3)), 0x00, 0x00
};

SpiWriteAndRead(buf, 12);

y += (buf[1] << 8 | buf[2])>>3;
x += (buf[4] << 8 | buf[5])>>3;
z1 += (buf[7] << 8 | buf[8])>>3;
z2 += (buf[10] << 8 | buf[11])>>3;
}

if(i == 0) {
*oX = 0;
*oY = 0;
*oZ = 0;
return;
}

x /= i;
y /= i;
z1 /= i;
z2 /= i;

switch(_rotation) {
case 0:
default:
break;
case 1:
x = (_maxValue - x);
y = (_maxValue - y);
break;
case 2:
y = (_maxValue - y);
break;
case 3:
x = (_maxValue - x);
break;
}

int z = z1 + _maxValue - z2;

*oX = x;
*oY = y;
*oZ = z2;
}
Loading