Skip to content

Commit 914e82b

Browse files
committed
able to receive first PD message
1 parent e02eee7 commit 914e82b

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

hw/bsp/stm32g4/family.c

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ void UCPD1_IRQHandler(void) {
7272
uint32_t sr = UCPD1->SR;
7373
sr &= UCPD1->IMR;
7474

75-
TU_LOG1("UCPD1_IRQHandler: sr = 0x%08X\n", sr);
75+
// TU_LOG1("UCPD1_IRQHandler: sr = 0x%08X\n", sr);
7676

7777
if (sr & (UCPD_SR_TYPECEVT1 | UCPD_SR_TYPECEVT2)) {
7878
uint32_t vstate_cc[2];
@@ -83,6 +83,7 @@ void UCPD1_IRQHandler(void) {
8383

8484
uint32_t cr = UCPD1->CR;
8585

86+
// TODO only support SNK for now, required highest voltage for now
8687
if ((sr & UCPD_SR_TYPECEVT1) && (vstate_cc[0] == 3)) {
8788
TU_LOG1("Attach CC1\n");
8889
cr &= ~UCPD_CR_PHYCCSEL;
@@ -96,10 +97,12 @@ void UCPD1_IRQHandler(void) {
9697
cr &= ~UCPD_CR_PHYRXEN;
9798
}
9899

99-
// Enable Interrupt
100-
UCPD1->IMR |= UCPD_IMR_TXMSGDISCIE | UCPD_IMR_TXMSGSENTIE | UCPD_IMR_TXMSGABTIE | UCPD_IMR_TXUNDIE |
101-
/*UCPD_IMR_RXNEIE |*/ UCPD_IMR_RXORDDETIE | UCPD_IMR_RXHRSTDETIE | UCPD_IMR_RXOVRIE | UCPD_IMR_RXMSGENDIE |
102-
UCPD_IMR_HRSTDISCIE | UCPD_IMR_HRSTSENTIE;
100+
if (cr & UCPD_CR_PHYRXEN) {
101+
// Enable Interrupt
102+
UCPD1->IMR |= UCPD_IMR_TXMSGDISCIE | UCPD_IMR_TXMSGSENTIE | UCPD_IMR_TXMSGABTIE | UCPD_IMR_TXUNDIE |
103+
UCPD_IMR_RXNEIE | UCPD_IMR_RXORDDETIE | UCPD_IMR_RXHRSTDETIE | UCPD_IMR_RXOVRIE |
104+
UCPD_IMR_RXMSGENDIE | UCPD_IMR_HRSTDISCIE | UCPD_IMR_HRSTSENTIE;
105+
}
103106

104107
// Enable PD RX
105108
UCPD1->CR = cr;
@@ -108,32 +111,40 @@ void UCPD1_IRQHandler(void) {
108111
UCPD1->ICR = UCPD_ICR_TYPECEVT1CF | UCPD_ICR_TYPECEVT2CF;
109112
}
110113

114+
//------------- Receive -------------//
111115
if (sr & UCPD_SR_RXORDDET) {
112-
// SOP: Start of Packet
113-
// TODO DMA later
114-
uint8_t order_set = UCPD1->RX_ORDSET & UCPD_RX_ORDSET_RXORDSET_Msk;
115-
TU_LOG1_HEX(order_set);
116+
// SOP: Start of Packet.
117+
// uint8_t order_set = UCPD1->RX_ORDSET & UCPD_RX_ORDSET_RXORDSET_Msk;
118+
119+
// reset count when received SOP
120+
pd_rx_count = 0;
116121

117122
// ack
118123
UCPD1->ICR = UCPD_ICR_RXORDDETCF;
119124
}
120125

121-
if ( sr & UCPD_SR_RXMSGEND) {
126+
if (sr & UCPD_SR_RXNE) {
127+
// TODO DMA later
128+
do {
129+
pd_rx_buf[pd_rx_count++] = UCPD1->RXDR;
130+
} while (UCPD1->SR & UCPD_SR_RXNE);
131+
}
132+
133+
if (sr & UCPD_SR_RXMSGEND) {
122134
// End of message
123-
// uint32_t payload_size = UCPD1->RX_PAYSZ;
124-
// TU_LOG1_HEX(payload_size);
125-
//
126-
// for(uint32_t i=0; i<payload_size; i++) {
127-
// pd_rx_buf[i] = UCPD1->RXDR;
128-
//
129-
// TU_LOG1("0x%02X ", pd_rx_buf[i]);
130-
// }
131-
// TU_LOG1("\n");
135+
uint32_t payload_size = UCPD1->RX_PAYSZ;
132136

133137
// ack
134138
UCPD1->ICR = UCPD_ICR_RXMSGENDCF;
135139
}
136140

141+
if (sr & UCPD_SR_RXOVR) {
142+
TU_LOG1("RXOVR\n");
143+
TU_LOG1_HEX(pd_rx_count);
144+
// ack
145+
UCPD1->ICR = UCPD_ICR_RXOVRCF;
146+
}
147+
137148
// if (sr & UCPD_SR_RXNE) {
138149
// uint8_t data = UCPD1->RXDR;
139150
// pd_rx_buf[pd_rx_count++] = data;

0 commit comments

Comments
 (0)