Skip to content

Commit 9128024

Browse files
committed
move protocol message to stack, disable battery in PWR's CR3
there is still issue with CC1/CC2 pull down resistor and vstate is not correct.
1 parent 8181d47 commit 9128024

File tree

6 files changed

+162
-75
lines changed

6 files changed

+162
-75
lines changed

hw/bsp/stm32g4/family.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,13 @@ void board_init(void)
145145
#if 1
146146
// USB PD
147147
// Default CC1/CC2 is PB4/PB6
148-
/* PWR register access (for disabling dead battery feature) */
149-
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);
150-
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_CRC);
151148

149+
// Enable pwr for disabling dead battery feature in Power's CR3
150+
__HAL_RCC_PWR_CLK_ENABLE();
151+
__HAL_RCC_CRC_CLK_ENABLE();
152152
__HAL_RCC_UCPD1_CLK_ENABLE();
153153

154-
// Enable DMA clock
154+
// Enable DMA for USB PD
155155
__HAL_RCC_DMAMUX1_CLK_ENABLE();
156156
__HAL_RCC_DMA1_CLK_ENABLE();
157157
#endif

src/device/dcd.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ TU_ATTR_WEAK bool dcd_edpt_iso_alloc(uint8_t rhport, uint8_t ep_addr, uint16_t l
189189

190190
// Configure and enable an ISO endpoint according to descriptor
191191
TU_ATTR_WEAK bool dcd_edpt_iso_activate(uint8_t rhport, tusb_desc_endpoint_t const * p_endpoint_desc);
192+
192193
//--------------------------------------------------------------------+
193194
// Event API (implemented by stack)
194195
//--------------------------------------------------------------------+

src/portable/st/typec/typec_stm32.c

Lines changed: 56 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
#if CFG_TUSB_MCU == OPT_MCU_STM32G4
3333
#include "stm32g4xx.h"
34-
#include "stm32g4xx_ll_dma.h" // for UCLP REQID
34+
#include "stm32g4xx_ll_dma.h" // for UCPD REQID
3535
#else
3636
#error "Unsupported STM32 family"
3737
#endif
@@ -62,9 +62,17 @@ enum {
6262
#define PHY_ORDERED_SET_SOP_PP_DEBUG (PHY_SYNC1 | (PHY_RST2<<5u) | (PHY_SYNC3<<10u) | (PHY_SYNC2<<15u)) // SOP'' Debug Ordered set coding
6363

6464

65-
static uint8_t rx_buf[262] TU_ATTR_ALIGNED(4);
66-
static uint8_t tx_buf[262] TU_ATTR_ALIGNED(4);
67-
static uint32_t tx_index;
65+
static uint8_t const* _rx_buf;
66+
67+
static tusb_pd_header_t _good_crc = {
68+
.msg_type = TUSB_PD_CTRL_GOOD_CRC,
69+
.data_role = 0, // UFP
70+
.specs_rev = TUSB_PD_REV20,
71+
.power_role = 0, // Sink
72+
.msg_id = 0,
73+
.n_data_obj = 0,
74+
.extended = 0
75+
};
6876

6977
// address of DMA channel rx, tx for each port
7078
#define CFG_TUC_STM32_DMA { { DMA1_Channel1_BASE, DMA1_Channel2_BASE } }
@@ -73,10 +81,10 @@ static uint32_t tx_index;
7381
// DMA
7482
//--------------------------------------------------------------------+
7583

76-
static const uint32_t dma_addr_arr[TUP_TYPEC_RHPORTS_NUM][2] = CFG_TUC_STM32_DMA;
84+
static const uint32_t _dma_addr_arr[TUP_TYPEC_RHPORTS_NUM][2] = CFG_TUC_STM32_DMA;
7785

7886
TU_ATTR_ALWAYS_INLINE static inline uint32_t dma_get_addr(uint8_t rhport, bool is_rx) {
79-
return dma_addr_arr[rhport][is_rx ? 0 : 1];
87+
return _dma_addr_arr[rhport][is_rx ? 0 : 1];
8088
}
8189

8290
static void dma_init(uint8_t rhport, bool is_rx) {
@@ -133,10 +141,6 @@ TU_ATTR_ALWAYS_INLINE static inline void dma_stop(uint8_t rhport, bool is_rx) {
133141
dma_ch->CCR &= ~DMA_CCR_EN;
134142
}
135143

136-
TU_ATTR_ALWAYS_INLINE static inline void dma_rx_start(uint8_t rhport) {
137-
dma_start(rhport, true, rx_buf, sizeof(rx_buf));
138-
}
139-
140144
TU_ATTR_ALWAYS_INLINE static inline void dma_tx_start(uint8_t rhport, void const* buf, uint16_t len) {
141145
UCPD1->TX_ORDSET = PHY_ORDERED_SET_SOP;
142146
UCPD1->TX_PAYSZ = len;
@@ -147,35 +151,37 @@ TU_ATTR_ALWAYS_INLINE static inline void dma_tx_start(uint8_t rhport, void const
147151
//
148152
//--------------------------------------------------------------------+
149153

150-
151154
bool tcd_init(uint8_t rhport, tusb_typec_port_type_t port_type) {
152155
(void) rhport;
153156

154157
// Init DMA for RX, TX
155158
dma_init(rhport, true);
156159
dma_init(rhport, false);
157160

158-
// Initialization phase: CFG1
161+
// Initialization phase: CFG1, detect all SOPs
159162
UCPD1->CFG1 = (0x0d << UCPD_CFG1_HBITCLKDIV_Pos) | (0x10 << UCPD_CFG1_IFRGAP_Pos) | (0x07 << UCPD_CFG1_TRANSWIN_Pos) |
160163
(0x01 << UCPD_CFG1_PSC_UCPDCLK_Pos) | (0x1f << UCPD_CFG1_RXORDSETEN_Pos);
161164
UCPD1->CFG1 |= UCPD_CFG1_UCPDEN;
162165

163166
// General programming sequence (with UCPD configured then enabled)
164167
if (port_type == TUSB_TYPEC_PORT_SNK) {
165-
// Enable both CC Phy
166-
UCPD1->CR = (0x01 << UCPD_CR_ANAMODE_Pos) | UCPD_CR_CCENABLE_0 | UCPD_CR_CCENABLE_1;
168+
// Set analog mode enable both CC Phy
169+
UCPD1->CR = (0x01 << UCPD_CR_ANAMODE_Pos) | (UCPD_CR_CCENABLE_0 | UCPD_CR_CCENABLE_1);
167170

168171
// Read Voltage State on CC1 & CC2 fore initial state
169-
uint32_t vstate_cc[2];
170-
vstate_cc[0] = (UCPD1->SR >> UCPD_SR_TYPEC_VSTATE_CC1_Pos) & 0x03;
171-
vstate_cc[1] = (UCPD1->SR >> UCPD_SR_TYPEC_VSTATE_CC2_Pos) & 0x03;
172+
uint32_t v_cc[2];
173+
v_cc[0] = (UCPD1->SR >> UCPD_SR_TYPEC_VSTATE_CC1_Pos) & 0x03;
174+
v_cc[1] = (UCPD1->SR >> UCPD_SR_TYPEC_VSTATE_CC2_Pos) & 0x03;
172175

173-
TU_LOG1("Initial VState CC1 = %u, CC2 = %u\r\n", vstate_cc[0], vstate_cc[1]);
176+
TU_LOG1("Initial VState CC1 = %u, CC2 = %u\r\n", v_cc[0], v_cc[1]);
174177

175178
// Enable CC1 & CC2 Interrupt
176179
UCPD1->IMR = UCPD_IMR_TYPECEVT1IE | UCPD_IMR_TYPECEVT2IE;
177180
}
178181

182+
// Disable dead battery in PWR's CR3
183+
PWR->CR3 |= PWR_CR3_UCPD_DBDIS;
184+
179185
return true;
180186
}
181187

@@ -192,9 +198,8 @@ void tcd_int_disable(uint8_t rhport) {
192198
}
193199

194200
bool tcd_rx_start(uint8_t rhport, uint8_t* buffer, uint16_t total_bytes) {
195-
(void) rhport;
196-
(void) buffer;
197-
(void) total_bytes;
201+
_rx_buf = buffer;
202+
dma_start(rhport, true, buffer, total_bytes);
198203
return true;
199204
}
200205

@@ -212,27 +217,28 @@ void tcd_int_handler(uint8_t rhport) {
212217
sr &= UCPD1->IMR;
213218

214219
if (sr & (UCPD_SR_TYPECEVT1 | UCPD_SR_TYPECEVT2)) {
215-
uint32_t vstate_cc[2];
216-
vstate_cc[0] = (UCPD1->SR >> UCPD_SR_TYPEC_VSTATE_CC1_Pos) & 0x03;
217-
vstate_cc[1] = (UCPD1->SR >> UCPD_SR_TYPEC_VSTATE_CC2_Pos) & 0x03;
220+
uint32_t v_cc[2];
221+
v_cc[0] = (UCPD1->SR >> UCPD_SR_TYPEC_VSTATE_CC1_Pos) & 0x03;
222+
v_cc[1] = (UCPD1->SR >> UCPD_SR_TYPEC_VSTATE_CC2_Pos) & 0x03;
218223

219-
TU_LOG1("VState CC1 = %u, CC2 = %u\n", vstate_cc[0], vstate_cc[1]);
224+
TU_LOG3("VState CC1 = %u, CC2 = %u\n", v_cc[0], v_cc[1]);
220225

221226
uint32_t cr = UCPD1->CR;
222227

223228
// TODO only support SNK for now, required highest voltage for now
224-
// Enable PHY on correct CC and disable Rd on other CC
225-
if ((sr & UCPD_SR_TYPECEVT1) && (vstate_cc[0] == 3)) {
226-
TU_LOG1("Attach CC1\n");
227-
229+
// Enable PHY on active CC and disable Rd on other CC
230+
// FIXME somehow CC2 is vstate is not correct, always 1 even not attached.
231+
// on DPOW1 board, it is connected to PA10 (USBPD_DBCC2), we probably miss something.
232+
if ((sr & UCPD_SR_TYPECEVT1) && (v_cc[0] == 3)) {
233+
TU_LOG3("Attach CC1\n");
228234
cr &= ~(UCPD_CR_PHYCCSEL | UCPD_CR_CCENABLE);
229235
cr |= UCPD_CR_PHYRXEN | UCPD_CR_CCENABLE_0;
230-
} else if ((sr & UCPD_SR_TYPECEVT2) && (vstate_cc[1] == 3)) {
231-
TU_LOG1("Attach CC2\n");
236+
} else if ((sr & UCPD_SR_TYPECEVT2) && (v_cc[1] == 3)) {
237+
TU_LOG3("Attach CC2\n");
232238
cr &= ~UCPD_CR_CCENABLE;
233239
cr |= (UCPD_CR_PHYCCSEL | UCPD_CR_PHYRXEN | UCPD_CR_CCENABLE_1);
234240
} else {
235-
TU_LOG1("Detach\n");
241+
TU_LOG3("Detach\n");
236242
cr &= ~UCPD_CR_PHYRXEN;
237243
cr |= UCPD_CR_CCENABLE_0 | UCPD_CR_CCENABLE_1;
238244
}
@@ -241,14 +247,15 @@ void tcd_int_handler(uint8_t rhport) {
241247
// Attached
242248
UCPD1->IMR |= IMR_ATTACHED;
243249
UCPD1->CFG1 |= UCPD_CFG1_RXDMAEN | UCPD_CFG1_TXDMAEN;
244-
245-
dma_rx_start(rhport);
246250
}else {
247251
// Detached
248252
UCPD1->CFG1 &= ~(UCPD_CFG1_RXDMAEN | UCPD_CFG1_TXDMAEN);
249253
UCPD1->IMR &= ~IMR_ATTACHED;
250254
}
251255

256+
// notify stack
257+
tcd_event_cc_changed(rhport, v_cc[0], v_cc[1], true);
258+
252259
UCPD1->CR = cr;
253260

254261
// ack
@@ -258,6 +265,7 @@ void tcd_int_handler(uint8_t rhport) {
258265
//------------- RX -------------//
259266
if (sr & UCPD_SR_RXORDDET) {
260267
// SOP: Start of Packet.
268+
TU_LOG3("SOP\n");
261269
// UCPD1->RX_ORDSET & UCPD_RX_ORDSET_RXORDSET_Msk;
262270

263271
// ack
@@ -266,48 +274,39 @@ void tcd_int_handler(uint8_t rhport) {
266274

267275
// Received full message
268276
if (sr & UCPD_SR_RXMSGEND) {
269-
277+
TU_LOG3("RX MSG END\n");
270278
dma_stop(rhport, true);
271279

272-
// Skip if CRC failed
273-
if (!(sr & UCPD_SR_RXERR)) {
274-
uint32_t payload_size = UCPD1->RX_PAYSZ;
275-
// TU_LOG1("RXMSGEND: payload_size = %u, rx count = %u\n", payload_size, pd_rx_count);
276-
277-
tusb_pd_header_t const* rx_header = (tusb_pd_header_t const*) rx_buf;
278-
(*(tusb_pd_header_t*) tx_buf) = (tusb_pd_header_t) {
279-
.msg_type = TUSB_PD_CTRL_GOOD_CRC,
280-
.data_role = 0, // UFP
281-
.specs_rev = TUSB_PD_REV20,
282-
.power_role = 0, // Sink
283-
.msg_id = rx_header->msg_id,
284-
.n_data_obj = 0,
285-
.extended = 0
286-
};
280+
uint8_t result;
287281

282+
if (!(sr & UCPD_SR_RXERR)) {
288283
// response with good crc
289-
dma_tx_start(rhport, tx_buf, 2);
290-
284+
_good_crc.msg_id = ((tusb_pd_header_t const*) _rx_buf)->msg_id;
285+
dma_tx_start(rhport, &_good_crc, 2);
291286
UCPD1->CR |= UCPD_CR_TXSEND;
292287

293-
// notify stack
288+
result = XFER_RESULT_SUCCESS;
289+
}else {
290+
// CRC failed
291+
result = XFER_RESULT_FAILED;
294292
}
295293

296-
// prepare next receive
297-
dma_rx_start(rhport);
294+
// notify stack
295+
tcd_event_rx_complete(rhport, UCPD1->RX_PAYSZ, result, true);
298296

299297
// ack
300298
UCPD1->ICR = UCPD_ICR_RXMSGENDCF;
301299
}
302300

303301
if (sr & UCPD_SR_RXOVR) {
304-
TU_LOG1("RXOVR\n");
302+
TU_LOG3("RXOVR\n");
305303
// ack
306304
UCPD1->ICR = UCPD_ICR_RXOVRCF;
307305
}
308306

309307
//------------- TX -------------//
310308
if (sr & UCPD_SR_TXMSGSENT) {
309+
TU_LOG3("TX MSG SENT\n");
311310
// all byte sent
312311
dma_stop(rhport, false);
313312

@@ -316,7 +315,7 @@ void tcd_int_handler(uint8_t rhport) {
316315
}
317316

318317
if (sr & (UCPD_SR_TXMSGDISC | UCPD_SR_TXMSGABT | UCPD_SR_TXUND)) {
319-
TU_LOG1("TX Error\n");
318+
TU_LOG3("TX Error\n");
320319
dma_stop(rhport, false);
321320
UCPD1->ICR = UCPD_SR_TXMSGDISC | UCPD_SR_TXMSGABT | UCPD_SR_TXUND;
322321
}

src/typec/tcd.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,28 @@ extern "C" {
3838
//--------------------------------------------------------------------+
3939
//
4040
//--------------------------------------------------------------------+
41+
42+
enum {
43+
TCD_EVENT_INVALID = 0,
44+
TCD_EVENT_CC_CHANGED,
45+
TCD_EVENT_RX_COMPLETE,
46+
};
47+
48+
4149
typedef struct {
4250
uint8_t rhport;
4351
uint8_t event_id;
4452

53+
union {
54+
struct {
55+
uint8_t cc_state[2];
56+
} cc_changed;
57+
58+
struct {
59+
uint16_t xferred_bytes;
60+
uint8_t result;
61+
} rx_complete;
62+
};
4563

4664
} tcd_event_t;;
4765

@@ -68,4 +86,38 @@ void tcd_int_handler(uint8_t rhport);
6886
bool tcd_rx_start(uint8_t rhport, uint8_t* buffer, uint16_t total_bytes);
6987
bool tcd_tx_start(uint8_t rhport, uint8_t const* buffer, uint16_t total_bytes);
7088

89+
//--------------------------------------------------------------------+
90+
// Event API (implemented by stack)
91+
// Called by TCD to notify stack
92+
//--------------------------------------------------------------------+
93+
94+
extern void tcd_event_handler(tcd_event_t const * event, bool in_isr);
95+
96+
TU_ATTR_ALWAYS_INLINE static inline
97+
void tcd_event_cc_changed(uint8_t rhport, uint8_t cc1, uint8_t cc2, bool in_isr) {
98+
tcd_event_t event = {
99+
.rhport = rhport,
100+
.event_id = TCD_EVENT_CC_CHANGED,
101+
.cc_changed = {
102+
.cc_state = {cc1, cc2 }
103+
}
104+
};
105+
106+
tcd_event_handler(&event, in_isr);
107+
}
108+
109+
TU_ATTR_ALWAYS_INLINE static inline
110+
void tcd_event_rx_complete(uint8_t rhport, uint16_t xferred_bytes, uint8_t result, bool in_isr) {
111+
tcd_event_t event = {
112+
.rhport = rhport,
113+
.event_id = TCD_EVENT_RX_COMPLETE,
114+
.rx_complete = {
115+
.xferred_bytes = xferred_bytes,
116+
.result = result
117+
}
118+
};
119+
120+
tcd_event_handler(&event, in_isr);
121+
}
122+
71123
#endif

0 commit comments

Comments
 (0)