summaryrefslogtreecommitdiff
path: root/src/typec/usbc.c
blob: fdf2a0cd69dec52ae6328e3f58eb0c557ada3271 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2023 Ha Thach ([email protected])
 *
 * 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.
 *
 * This file is part of the TinyUSB stack.
 */

#include "tusb_option.h"

#if CFG_TUC_ENABLED

#include "tcd.h"
#include "usbc.h"

//--------------------------------------------------------------------+
//
//--------------------------------------------------------------------+

// Debug level of USBD
#define USBC_DEBUG   2
#define TU_LOG_USBC(...)   TU_LOG(USBC_DEBUG, __VA_ARGS__)

// Event queue
// usbc_int_set() is used as mutex in OS NONE config
void usbc_int_set(bool enabled);
OSAL_QUEUE_DEF(usbc_int_set, _usbc_qdef, CFG_TUC_TASK_QUEUE_SZ, tcd_event_t);
tu_static osal_queue_t _usbc_q;

// if stack is initialized
static bool _usbc_inited = false;

// if port is initialized
static bool _port_inited[TUP_TYPEC_RHPORTS_NUM];

// Max possible PD size is 262 bytes
static uint8_t _rx_buf[64] TU_ATTR_ALIGNED(4);
static uint8_t _tx_buf[64] TU_ATTR_ALIGNED(4);

bool usbc_msg_send(uint8_t rhport, pd_header_t const* header, void const* data);
bool parse_msg_data(uint8_t rhport, pd_header_t const* header, uint8_t const* dobj, uint8_t const* p_end);
bool parse_msg_control(uint8_t rhport, pd_header_t const* header);

//--------------------------------------------------------------------+
//
//--------------------------------------------------------------------+
bool tuc_inited(uint8_t rhport) {
  return _usbc_inited && _port_inited[rhport];
}

bool tuc_init(uint8_t rhport, uint32_t port_type) {
  // Initialize stack
  if (!_usbc_inited) {
    tu_memclr(_port_inited, sizeof(_port_inited));

    _usbc_q = osal_queue_create(&_usbc_qdef);
    TU_ASSERT(_usbc_q != NULL);

    _usbc_inited = true;
  }

  // skip if port already initialized
  if ( _port_inited[rhport] ) {
    return true;
  }

  TU_LOG_USBC("USBC init on port %u\r\n", rhport);
  TU_LOG_INT(USBC_DEBUG, sizeof(tcd_event_t));

  TU_ASSERT(tcd_init(rhport, port_type));
  tcd_int_enable(rhport);

  _port_inited[rhport] = true;
  return true;
}

void tuc_task_ext(uint32_t timeout_ms, bool in_isr) {
  (void) in_isr; // not implemented yet

  // Skip if stack is not initialized
  if (!_usbc_inited) return;

  // Loop until there is no more events in the queue
  while (1) {
    tcd_event_t event;
    if (!osal_queue_receive(_usbc_q, &event, timeout_ms)) return;

    switch (event.event_id) {
      case TCD_EVENT_CC_CHANGED:
        break;

      case TCD_EVENT_RX_COMPLETE:
        // TODO process message here in ISR, move to thread later
        if (event.xfer_complete.result == XFER_RESULT_SUCCESS) {
          pd_header_t const* header = (pd_header_t const*) _rx_buf;

          if (header->n_data_obj == 0) {
            parse_msg_control(event.rhport, header);

          }else {
            uint8_t const* p_end = _rx_buf + event.xfer_complete.xferred_bytes;
            uint8_t const * dobj = _rx_buf + sizeof(pd_header_t);

            parse_msg_data(event.rhport, header, dobj, p_end);
          }
        }

        // prepare for next message
        tcd_msg_receive(event.rhport, _rx_buf, sizeof(_rx_buf));
        break;

      case TCD_EVENT_TX_COMPLETE:
        break;

      default: break;
    }
  }
}

bool parse_msg_data(uint8_t rhport, pd_header_t const* header, uint8_t const* dobj, uint8_t const* p_end) {
  if (tuc_pd_data_received_cb) {
    tuc_pd_data_received_cb(rhport, header, dobj, p_end);
  }

  return true;
}

bool parse_msg_control(uint8_t rhport, pd_header_t const* header) {
  if (tuc_pd_control_received_cb) {
    tuc_pd_control_received_cb(rhport, header);
  }

  return true;
}

//--------------------------------------------------------------------+
//
//--------------------------------------------------------------------+

bool usbc_msg_send(uint8_t rhport, pd_header_t const* header, void const* data) {
  // copy header
  memcpy(_tx_buf, header, sizeof(pd_header_t));

  // copy data objcet if available
  uint16_t const n_data_obj = header->n_data_obj;
  if (n_data_obj > 0) {
    memcpy(_tx_buf + sizeof(pd_header_t), data, n_data_obj * 4);
  }

  return tcd_msg_send(rhport, _tx_buf, sizeof(pd_header_t) + n_data_obj * 4);
}

bool tuc_msg_request(uint8_t rhport, void const* rdo) {
  pd_header_t const header = {
      .msg_type = PD_DATA_REQUEST,
      .data_role = PD_DATA_ROLE_UFP,
      .specs_rev = PD_REV_30,
      .power_role = PD_POWER_ROLE_SINK,
      .msg_id = 0,
      .n_data_obj = 1,
      .extended = 0,
  };

  return usbc_msg_send(rhport, &header, rdo);
}

void tcd_event_handler(tcd_event_t const * event, bool in_isr) {
  (void) in_isr;
  switch(event->event_id) {
    case TCD_EVENT_CC_CHANGED:
      if (event->cc_changed.cc_state[0] || event->cc_changed.cc_state[1]) {
        // Attach, start receiving
        tcd_msg_receive(event->rhport, _rx_buf, sizeof(_rx_buf));
      }else {
        // Detach
      }
      break;

    default: break;
  }

  osal_queue_send(_usbc_q, event, in_isr);
}

//--------------------------------------------------------------------+
//
//--------------------------------------------------------------------+
void usbc_int_set(bool enabled) {
  // Disable all controllers since they shared the same event queue
  for (uint8_t p = 0; p < TUP_TYPEC_RHPORTS_NUM; p++) {
    if ( _port_inited[p] ) {
      if (enabled) {
        tcd_int_enable(p);
      }else {
        tcd_int_disable(p);
      }
    }
  }
}

#endif