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
|
/*
* SPDX-FileCopyrightText: Copyright (c) 2023 Ha Thach (tinyusb.org)
* SPDX-License-Identifier: MIT
*
* This file is part of the TinyUSB stack.
*/
#ifndef TUSB_TCD_H_
#define TUSB_TCD_H_
#include "common/tusb_common.h"
#include "pd_types.h"
#include "osal/osal.h"
#include "common/tusb_fifo.h"
#ifdef __cplusplus
extern "C" {
#endif
//--------------------------------------------------------------------+
//
//--------------------------------------------------------------------+
enum {
TCD_EVENT_INVALID = 0,
TCD_EVENT_CC_CHANGED,
TCD_EVENT_RX_COMPLETE,
TCD_EVENT_TX_COMPLETE,
};
typedef struct TU_ATTR_PACKED {
uint8_t rhport;
uint8_t event_id;
union {
struct {
uint8_t cc_state[2];
} cc_changed;
struct TU_ATTR_PACKED {
uint16_t result : 2;
uint16_t xferred_bytes : 14;
} xfer_complete;
};
} tcd_event_t;
//--------------------------------------------------------------------+
//
//--------------------------------------------------------------------+
// Initialize controller
bool tcd_init(uint8_t rhport, uint32_t port_type);
// Enable interrupt
void tcd_int_enable (uint8_t rhport);
// Disable interrupt
void tcd_int_disable(uint8_t rhport);
// Enable Type-C port terminations
void tcd_connect(uint8_t rhport);
// Disable Type-C port terminations
void tcd_disconnect(uint8_t rhport);
// Interrupt Handler
void tcd_int_handler(uint8_t rhport);
//--------------------------------------------------------------------+
//
//--------------------------------------------------------------------+
bool tcd_msg_receive(uint8_t rhport, uint8_t* buffer, uint16_t total_bytes);
bool tcd_msg_send(uint8_t rhport, uint8_t const* buffer, uint16_t total_bytes);
//--------------------------------------------------------------------+
// Event API (implemented by stack)
// Called by TCD to notify stack
//--------------------------------------------------------------------+
extern void tcd_event_handler(tcd_event_t const * event, bool in_isr);
TU_ATTR_ALWAYS_INLINE static inline
void tcd_event_cc_changed(uint8_t rhport, uint8_t cc1, uint8_t cc2, bool in_isr) {
tcd_event_t event = {
.rhport = rhport,
.event_id = TCD_EVENT_CC_CHANGED,
.cc_changed = {
.cc_state = {cc1, cc2 }
}
};
tcd_event_handler(&event, in_isr);
}
TU_ATTR_ALWAYS_INLINE static inline
void tcd_event_rx_complete(uint8_t rhport, uint16_t xferred_bytes, uint8_t result, bool in_isr) {
tcd_event_t event = {
.rhport = rhport,
.event_id = TCD_EVENT_RX_COMPLETE,
.xfer_complete = {
.xferred_bytes = xferred_bytes,
.result = result
}
};
tcd_event_handler(&event, in_isr);
}
TU_ATTR_ALWAYS_INLINE static inline
void tcd_event_tx_complete(uint8_t rhport, uint16_t xferred_bytes, uint8_t result, bool in_isr) {
tcd_event_t event = {
.rhport = rhport,
.event_id = TCD_EVENT_TX_COMPLETE,
.xfer_complete = {
.xferred_bytes = xferred_bytes,
.result = result
}
};
tcd_event_handler(&event, in_isr);
}
#ifdef __cplusplus
}
#endif
#endif
|