summaryrefslogtreecommitdiff
path: root/cts/i2c/th.c
blob: 78035cb1b2ea96fba5d457064311664125fe9886 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* Copyright 2016 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include <string.h>
#include "common.h"
#include "cts_common.h"
#include "cts_i2c.h"
#include "i2c.h"
#include "registers.h"
#include "task.h"
#include "timer.h"
#include "uart.h"
#include "watchdog.h"

static uint8_t inbox[I2C_MAX_HOST_PACKET_SIZE + 2];
static char data_received;

void i2c_data_received(int port, uint8_t *buf, int len)
{
	memcpy(inbox, buf, len);
	data_received = 1;
}

/* CTS I2C protocol implementation */
int i2c_set_response(int port, uint8_t *buf, int len)
{
	switch (buf[0]) {
	case READ8_OFF:
		buf[0] = READ8_DATA;
		return 1;
	case READ16_OFF:
		buf[0] = READ16_DATA & 0xFF;
		buf[1] = (READ16_DATA >> 8) & 0xFF;
		return 2;
	case READ32_OFF:
		buf[0] = READ32_DATA & 0xFF;
		buf[1] = (READ32_DATA >> 8) & 0xFF;
		buf[2] = (READ32_DATA >> 16) & 0xFF;
		buf[3] = (READ32_DATA >> 24) & 0xFF;
		return 4;
	default:
		return 0;
	}
}

static int wait_for_in_flag(uint32_t timeout_ms)
{
	uint64_t start_time, end_time;

	start_time = get_time().val;
	end_time = start_time + timeout_ms * 1000;

	while (get_time().val < end_time) {
		if (data_received)
			return 0;
		msleep(5);
		watchdog_reload();
	}
	return 1;
}

void clean_state(void)
{
	memset(inbox, 0, sizeof(inbox));
	data_received = 0;
}

enum cts_rc write8_test(void)
{
	int in;

	if (wait_for_in_flag(100))
		return CTS_RC_TIMEOUT;
	if (inbox[0] != WRITE8_OFF)
		return CTS_RC_FAILURE;
	in = inbox[1];
	if (in != WRITE8_DATA)
		return CTS_RC_FAILURE;

	return CTS_RC_SUCCESS;
}

enum cts_rc write16_test(void)
{
	int in;

	if (wait_for_in_flag(100))
		return CTS_RC_TIMEOUT;
	if (inbox[0] != WRITE16_OFF)
		return CTS_RC_FAILURE;
	in = inbox[2] << 8 | inbox[1] << 0;
	if (in != WRITE16_DATA)
		return CTS_RC_FAILURE;

	return CTS_RC_SUCCESS;
}

enum cts_rc write32_test(void)
{
	int in;

	if (wait_for_in_flag(100))
		return CTS_RC_TIMEOUT;
	if (inbox[0] != WRITE32_OFF)
		return CTS_RC_FAILURE;
	in = inbox[4] << 24 | inbox[3] << 16 | inbox[2] << 8 | inbox[1];
	if (in != WRITE32_DATA)
		return CTS_RC_FAILURE;

	return CTS_RC_SUCCESS;
}

enum cts_rc read8_test(void)
{
	if (wait_for_in_flag(100))
		return CTS_RC_TIMEOUT;
	if (inbox[0] != READ8_OFF)
		return CTS_RC_FAILURE;

	return CTS_RC_SUCCESS;
}

enum cts_rc read16_test(void)
{
	if (wait_for_in_flag(100))
		return CTS_RC_TIMEOUT;
	if (inbox[0] != READ16_OFF)
		return CTS_RC_FAILURE;

	return CTS_RC_SUCCESS;
}

enum cts_rc read32_test(void)
{
	if (wait_for_in_flag(100))
		return CTS_RC_TIMEOUT;
	if (inbox[0] != READ32_OFF)
		return CTS_RC_FAILURE;

	return CTS_RC_SUCCESS;
}

#include "cts_testlist.h"

void cts_task(void)
{
	cts_main_loop(tests, "I2C");
	task_wait_event(-1);
}