summaryrefslogtreecommitdiff
path: root/board/keyborg/spi_comm.h
blob: 4a1abb632a530e1a6da7a0f355b549582a269037 (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
/* Copyright (c) 2014 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.
 */

#ifndef __BOARD_KEYBORG_SPI_COMM_H
#define __BOARD_KEYBORG_SPI_COMM_H

#define SPI_PACKET_MAX_SIZE 64

enum ts_command {
	TS_CMD_HELLO = 0,
};

struct spi_comm_packet {
	uint8_t size;
	uint8_t cmd_sts;
	uint8_t data[0];
};

#define SPI_PACKET_HEADER_SIZE 2

/* Initialize SPI interface for the master chip */
void spi_master_init(void);

/* Initialize SPI interface for the slave chip */
void spi_slave_init(void);

/*
 * Calculate checksum and send command packet to the slave.
 *
 * @param cmd		Pointer to the command packet.
 *
 * @return		EC_SUCCESS, or non-zero if any error.
 */
int spi_master_send_command(struct spi_comm_packet *cmd);

/*
 * Wait for slave response and verify checksum.
 *
 * @return		Pointer to the response packet, or NULL if any error.
 */
const struct spi_comm_packet *spi_master_wait_response(void);

/*
 * Start receiving slave response, but don't wait for full transaction.
 * The caller is responsible for calling spi_master_wait_response_done()
 * to ensure the response is fully received.
 *
 * @return		EC_SUCCESS, or non-zero if any error.
 */
int spi_master_wait_response_async(void);

/*
 * Wait for slave response to complete.
 *
 * @return		Pointer to the response packet, or NULL if any error.
 */
const struct spi_comm_packet *spi_master_wait_response_done(void);

/*
 * Calculate checksum and send response packet to the master.
 *
 * @param resp		Pointer to the response packet.
 *
 * @return		EC_SUCCESS, or non-zero if any error.
 */
int spi_slave_send_response(struct spi_comm_packet *resp);

/*
 * Start sending response to the master, but don't block. The caller is
 * responsible for calling spi_slave_send_response_flush() to ensure
 * the response is fully transmitted.
 *
 * @return		EC_SUCCESS, or non-zero if any error.
 */
int spi_slave_send_response_async(struct spi_comm_packet *resp);

/*
 * Wait until the last response is sent out.
 *
 * @return		EC_SUCCESS, or non-zero if any error.
 */
int spi_slave_send_response_flush(void);

/*
 * Perform random back-to-back hello test. Master only.
 *
 * @param iteration	Number of hello messages to send.
 *
 * @return		EC_SUCCESS, or non-zero if any error.
 */
int spi_hello_test(int iteration);

#endif /* __BOARD_KEYBORG_SPI_COMM_H */