aboutsummaryrefslogtreecommitdiffstats
path: root/boards/base/Altera-MAX10-Neek/gmouse_lld_FT5316_board.h
blob: 2c3288d8acc7e2ddb281d48ae60d219878c64459 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
 * This file is subject to the terms of the GFX License. If a copy of
 * the license was not distributed with this file, you can obtain one at:
 *
 *              http://ugfx.org/license.html
 */

#ifndef _GINPUT_LLD_MOUSE_BOARD_H
#define _GINPUT_LLD_MOUSE_BOARD_H

#include <stdio.h>
#include <system.h>
#include "i2c_opencores.h"

#define FT5316_I2C_SLAVE_ADDRESS 0x38

// Resolution and Accuracy Settings
#define GMOUSE_FT5316_PEN_CALIBRATE_ERROR		8
#define GMOUSE_FT5316_PEN_CLICK_ERROR			6
#define GMOUSE_FT5316_PEN_MOVE_ERROR			4
#define GMOUSE_FT5316_FINGER_CALIBRATE_ERROR	14
#define GMOUSE_FT5316_FINGER_CLICK_ERROR		18
#define GMOUSE_FT5316_FINGER_MOVE_ERROR			14

// How much extra data to allocate at the end of the GMouse structure for the board's use
#define GMOUSE_FT5316_BOARD_DATA_SIZE			0

// Set this to GFXON if you want self-calibration.
//	NOTE:	This is not as accurate as real calibration.
//			It requires the orientation of the touch panel to match the display.
//			It requires the active area of the touch panel to exactly match the display size.
#define GMOUSE_FT5316_SELF_CALIBRATE			GFXON

static unsigned int device_write(unsigned char device_address, unsigned char sub_address, unsigned char wdata)
{
	I2C_start(I2C_OPENCORES_0_BASE, device_address, 0);			// Device address in write mode
	I2C_write(I2C_OPENCORES_0_BASE, sub_address, 0);			// Set sub address to read register
	I2C_write(I2C_OPENCORES_0_BASE, wdata, 1);					// Set write register

	return 1;
}

static unsigned int device_read(unsigned char device_address, unsigned char sub_address)
{
	unsigned int rdata;

	I2C_start(I2C_OPENCORES_0_BASE, device_address, 0);			// Device address in write mode
	rdata = I2C_write(I2C_OPENCORES_0_BASE, sub_address, 0);	// Set sub address to read register.
	I2C_start(I2C_OPENCORES_0_BASE, device_address, 1);			// Send start again but this time in read mode
	rdata = I2C_read(I2C_OPENCORES_0_BASE, 1);					// Read the register and send stop

	return rdata;
}

static unsigned int device_read_16(unsigned char device_address, unsigned char sub_address)
{
	unsigned int rdata_l;
	unsigned int rdata_h;

	I2C_start(I2C_OPENCORES_0_BASE, device_address, 0);			// Eccelerometer address in write mode
	rdata_l = I2C_write(I2C_OPENCORES_0_BASE, sub_address, 0);	// Set sub address to read register.
	I2C_start(I2C_OPENCORES_0_BASE, device_address, 1);			// Send start again but this time in read mode
	rdata_l = I2C_read(I2C_OPENCORES_0_BASE, 0);				// Read the register and MACK
	rdata_h = I2C_read(I2C_OPENCORES_0_BASE, 1);				// Read the next register and send stop

	return (0xFFFF & ((rdata_h << 8) | rdata_l));
}

static gBool init_board(GMouse* m, unsigned instance)
{
	(void)m;
	(void)instance;

	I2C_init(I2C_OPENCORES_0_BASE, 50000000, 400000);

	return gTrue;
}

static void write_reg(GMouse* m, uint8_t reg, uint8_t val)
{
	(void)m;

	device_write(FT5316_I2C_SLAVE_ADDRESS, reg, val);
}

static uint8_t read_byte(GMouse* m, uint8_t reg)
{
	(void)m;
	uint8_t ret = 0;

	ret = (uint8_t)device_read(FT5316_I2C_SLAVE_ADDRESS, reg);

	return ret;
}

static uint16_t read_word(GMouse* m, uint8_t reg)
{
	(void)m;
	uint16_t ret = 0;

	ret = (uint16_t)device_read(FT5316_I2C_SLAVE_ADDRESS, reg);

	return ret;
}

#endif /* _GINPUT_LLD_MOUSE_BOARD_H */