Skip to content

Commit b3fda4a

Browse files
committed
able to get usbpd irq handler
- handle cc1, cc2 voltage changes - get order set, rx message end interrupt - add segger rtt support for cmake
1 parent a70978e commit b3fda4a

File tree

4 files changed

+196
-37
lines changed

4 files changed

+196
-37
lines changed

hw/bsp/family_support.cmake

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,18 @@ function(family_initialize_project PROJECT DIR)
9797
endfunction()
9898

9999

100+
# Add segger rtt to example
101+
function(family_add_segger_rtt TARGET)
102+
if (NOT TARGET segger_rtt)
103+
add_library(segger_rtt STATIC
104+
${TOP}/lib/SEGGER_RTT/RTT/SEGGER_RTT.c
105+
)
106+
target_include_directories(segger_rtt PUBLIC ${TOP}/lib/SEGGER_RTT/RTT)
107+
endif()
108+
109+
target_link_libraries(${TARGET} PUBLIC segger_rtt)
110+
endfunction()
111+
100112
#------------------------------------
101113
# Main target configure
102114
#------------------------------------
@@ -119,6 +131,9 @@ function(family_configure_common TARGET)
119131
# LOGGER
120132
if (DEFINED LOGGER)
121133
target_compile_definitions(${TARGET} PUBLIC LOGGER_${LOGGER})
134+
if(LOGGER STREQUAL "RTT" OR LOGGER STREQUAL "rtt")
135+
family_add_segger_rtt(${TARGET})
136+
endif ()
122137
endif ()
123138

124139
endfunction()

hw/bsp/stm32g4/boards/b_g474e_dpow1/board.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
// RCC Clock
5959
//--------------------------------------------------------------------+
6060

61-
// CPU Frequency (Core Clock) is 170MHz
61+
// CPU Frequency (Core Clock) is 150MHz
6262
static inline void board_clock_init(void)
6363
{
6464
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
@@ -68,16 +68,16 @@ static inline void board_clock_init(void)
6868
HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1_BOOST);
6969

7070
/* Activate PLL with HSI as source */
71-
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
71+
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
72+
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
7273
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
73-
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
74-
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
75-
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
76-
RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV4;
77-
RCC_OscInitStruct.PLL.PLLN = 85;
78-
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV10;
79-
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
80-
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
74+
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
75+
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
76+
RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV4;
77+
RCC_OscInitStruct.PLL.PLLN = 75;
78+
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
79+
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV4;
80+
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
8181
HAL_RCC_OscConfig(&RCC_OscInitStruct);
8282

8383
// Initializes the CPU, AHB and APB buses clocks
@@ -87,7 +87,7 @@ static inline void board_clock_init(void)
8787
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
8888
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
8989
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
90-
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_8);
90+
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4);
9191

9292
//------------- HSI48 and CRS for USB -------------//
9393
RCC_OscInitTypeDef osc_hsi48 = {0};

hw/bsp/stm32g4/family.c

Lines changed: 157 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,127 @@
2525
*/
2626

2727
#include "stm32g4xx_hal.h"
28+
#include "stm32g4xx_ll_bus.h"
29+
2830
#include "bsp/board.h"
2931
#include "board.h"
3032

33+
34+
//--------------------------------------------------------------------+
35+
// USB PD
36+
//--------------------------------------------------------------------+
37+
38+
void usbpd_init(uint8_t port_num, tusb_typec_port_type_t port_type) {
39+
(void) port_num;
40+
41+
// Initialization phase: CFG1
42+
UCPD1->CFG1 = (0x0d << UCPD_CFG1_HBITCLKDIV_Pos) | (0x10 << UCPD_CFG1_IFRGAP_Pos) | (0x07 << UCPD_CFG1_TRANSWIN_Pos) |
43+
(0x01 << UCPD_CFG1_PSC_UCPDCLK_Pos) | (0x1f << UCPD_CFG1_RXORDSETEN_Pos) |
44+
( 0 << UCPD_CFG1_TXDMAEN_Pos) | (0 << UCPD_CFG1_RXDMAEN_Pos);
45+
UCPD1->CFG1 |= UCPD_CFG1_UCPDEN;
46+
47+
// General programming sequence (with UCPD configured then enabled)
48+
if (port_type == TUSB_TYPEC_PORT_SNK) {
49+
// Enable both CC Phy
50+
UCPD1->CR = (0x01 << UCPD_CR_ANAMODE_Pos) | (0x03 << UCPD_CR_CCENABLE_Pos);
51+
52+
// Read Voltage State on CC1 & CC2 fore initial state
53+
uint32_t vstate_cc[2];
54+
vstate_cc[0] = (UCPD1->SR >> UCPD_SR_TYPEC_VSTATE_CC1_Pos) & 0x03;
55+
vstate_cc[1] = (UCPD1->SR >> UCPD_SR_TYPEC_VSTATE_CC2_Pos) & 0x03;
56+
57+
TU_LOG1_INT(vstate_cc[0]);
58+
TU_LOG1_INT(vstate_cc[1]);
59+
60+
// Enable CC1 & CC2 Interrupt
61+
UCPD1->IMR = UCPD_IMR_TYPECEVT1IE | UCPD_IMR_TYPECEVT2IE;
62+
}
63+
64+
// Enable interrupt
65+
//NVIC_SetPriority(UCPD1_IRQn, 0);
66+
// NVIC_EnableIRQ(UCPD1_IRQn);
67+
}
68+
69+
uint8_t pd_rx_buf[262];
70+
uint32_t pd_rx_count = 0;
71+
72+
void UCPD1_IRQHandler(void) {
73+
uint32_t sr = UCPD1->SR;
74+
sr &= UCPD1->IMR;
75+
76+
TU_LOG1("UCPD1_IRQHandler: sr = 0x%08X\n", sr);
77+
78+
if (sr & (UCPD_SR_TYPECEVT1 | UCPD_SR_TYPECEVT2)) {
79+
uint32_t vstate_cc[2];
80+
vstate_cc[0] = (UCPD1->SR >> UCPD_SR_TYPEC_VSTATE_CC1_Pos) & 0x03;
81+
vstate_cc[1] = (UCPD1->SR >> UCPD_SR_TYPEC_VSTATE_CC2_Pos) & 0x03;
82+
83+
TU_LOG1("VState CC1 = %u, CC2 = %u\n", vstate_cc[0], vstate_cc[1]);
84+
85+
uint32_t cr = UCPD1->CR;
86+
87+
if ((sr & UCPD_SR_TYPECEVT1) && (vstate_cc[0] == 3)) {
88+
TU_LOG1("Attach CC1\n");
89+
cr &= ~UCPD_CR_PHYCCSEL;
90+
cr |= UCPD_CR_PHYRXEN;
91+
} else if ((sr & UCPD_SR_TYPECEVT2) && (vstate_cc[1] == 3)) {
92+
TU_LOG1("Attach CC2\n");
93+
cr |= UCPD_CR_PHYCCSEL;
94+
cr |= UCPD_CR_PHYRXEN;
95+
} else {
96+
TU_LOG1("Detach\n");
97+
cr &= ~UCPD_CR_PHYRXEN;
98+
}
99+
100+
// Enable Interrupt
101+
UCPD1->IMR |= UCPD_IMR_TXMSGDISCIE | UCPD_IMR_TXMSGSENTIE | UCPD_IMR_TXMSGABTIE | UCPD_IMR_TXUNDIE |
102+
/*UCPD_IMR_RXNEIE |*/ UCPD_IMR_RXORDDETIE | UCPD_IMR_RXHRSTDETIE | UCPD_IMR_RXOVRIE | UCPD_IMR_RXMSGENDIE |
103+
UCPD_IMR_HRSTDISCIE | UCPD_IMR_HRSTSENTIE;
104+
105+
// Enable PD RX
106+
UCPD1->CR = cr;
107+
108+
// ack
109+
UCPD1->ICR = UCPD_ICR_TYPECEVT1CF | UCPD_ICR_TYPECEVT2CF;
110+
}
111+
112+
if (sr & UCPD_SR_RXORDDET) {
113+
// SOP: Start of Packet
114+
// TODO DMA later
115+
uint8_t order_set = UCPD1->RX_ORDSET & UCPD_RX_ORDSET_RXORDSET_Msk;
116+
TU_LOG1_HEX(order_set);
117+
118+
// ack
119+
UCPD1->ICR = UCPD_ICR_RXORDDETCF;
120+
}
121+
122+
if ( sr & UCPD_SR_RXMSGEND) {
123+
// End of message
124+
// uint32_t payload_size = UCPD1->RX_PAYSZ;
125+
// TU_LOG1_HEX(payload_size);
126+
//
127+
// for(uint32_t i=0; i<payload_size; i++) {
128+
// pd_rx_buf[i] = UCPD1->RXDR;
129+
//
130+
// TU_LOG1("0x%02X ", pd_rx_buf[i]);
131+
// }
132+
// TU_LOG1("\n");
133+
134+
// ack
135+
UCPD1->ICR = UCPD_ICR_RXMSGENDCF;
136+
}
137+
138+
// if (sr & UCPD_SR_RXNE) {
139+
// uint8_t data = UCPD1->RXDR;
140+
// pd_rx_buf[pd_rx_count++] = data;
141+
// TU_LOG1_HEX(data);
142+
// }
143+
144+
// else {
145+
// TU_LOG_LOCATION();
146+
// }
147+
}
148+
31149
//--------------------------------------------------------------------+
32150
// Forward USB interrupt events to TinyUSB IRQ Handler
33151
//--------------------------------------------------------------------+
@@ -79,35 +197,35 @@ void board_init(void)
79197
NVIC_SetPriority(USBWakeUp_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY);
80198
#endif
81199

82-
GPIO_InitTypeDef gpio_init;
200+
GPIO_InitTypeDef GPIO_InitStruct;
83201

84202
// LED
85-
memset(&gpio_init, 0, sizeof(gpio_init));
86-
gpio_init.Pin = LED_PIN;
87-
gpio_init.Mode = GPIO_MODE_OUTPUT_PP;
88-
gpio_init.Pull = GPIO_PULLUP;
89-
gpio_init.Speed = GPIO_SPEED_FREQ_HIGH;
90-
HAL_GPIO_Init(LED_PORT, &gpio_init);
203+
memset(&GPIO_InitStruct, 0, sizeof(GPIO_InitStruct));
204+
GPIO_InitStruct.Pin = LED_PIN;
205+
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
206+
GPIO_InitStruct.Pull = GPIO_PULLUP;
207+
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
208+
HAL_GPIO_Init(LED_PORT, &GPIO_InitStruct);
91209

92210
board_led_write(false);
93211

94212
// Button
95-
memset(&gpio_init, 0, sizeof(gpio_init));
96-
gpio_init.Pin = BUTTON_PIN;
97-
gpio_init.Mode = GPIO_MODE_INPUT;
98-
gpio_init.Pull = BUTTON_STATE_ACTIVE ? GPIO_PULLDOWN : GPIO_PULLUP;
99-
gpio_init.Speed = GPIO_SPEED_FREQ_HIGH;
100-
HAL_GPIO_Init(BUTTON_PORT, &gpio_init);
213+
memset(&GPIO_InitStruct, 0, sizeof(GPIO_InitStruct));
214+
GPIO_InitStruct.Pin = BUTTON_PIN;
215+
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
216+
GPIO_InitStruct.Pull = BUTTON_STATE_ACTIVE ? GPIO_PULLDOWN : GPIO_PULLUP;
217+
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
218+
HAL_GPIO_Init(BUTTON_PORT, &GPIO_InitStruct);
101219

102220
#ifdef UART_DEV
103221
// UART
104-
memset(&gpio_init, 0, sizeof(gpio_init));
105-
gpio_init.Pin = UART_TX_PIN | UART_RX_PIN;
106-
gpio_init.Mode = GPIO_MODE_AF_PP;
107-
gpio_init.Pull = GPIO_PULLUP;
108-
gpio_init.Speed = GPIO_SPEED_FREQ_HIGH;
109-
gpio_init.Alternate = UART_GPIO_AF;
110-
HAL_GPIO_Init(UART_GPIO_PORT, &gpio_init);
222+
memset(&GPIO_InitStruct, 0, sizeof(GPIO_InitStruct));
223+
GPIO_InitStruct.Pin = UART_TX_PIN | UART_RX_PIN;
224+
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
225+
GPIO_InitStruct.Pull = GPIO_PULLUP;
226+
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
227+
GPIO_InitStruct.Alternate = UART_GPIO_AF;
228+
HAL_GPIO_Init(UART_GPIO_PORT, &GPIO_InitStruct);
111229

112230
UartHandle = (UART_HandleTypeDef){
113231
.Instance = UART_DEV,
@@ -124,18 +242,31 @@ void board_init(void)
124242

125243
// USB Pins TODO double check USB clock and pin setup
126244
// Configure USB DM and DP pins. This is optional, and maintained only for user guidance.
127-
memset(&gpio_init, 0, sizeof(gpio_init));
128-
gpio_init.Pin = (GPIO_PIN_11 | GPIO_PIN_12);
129-
gpio_init.Mode = GPIO_MODE_INPUT;
130-
gpio_init.Pull = GPIO_NOPULL;
131-
gpio_init.Speed = GPIO_SPEED_FREQ_HIGH;
132-
HAL_GPIO_Init(GPIOA, &gpio_init);
245+
memset(&GPIO_InitStruct, 0, sizeof(GPIO_InitStruct));
246+
GPIO_InitStruct.Pin = (GPIO_PIN_11 | GPIO_PIN_12);
247+
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
248+
GPIO_InitStruct.Pull = GPIO_NOPULL;
249+
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
250+
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
133251

134252
__HAL_RCC_USB_CLK_ENABLE();
135253

136254
board_vbus_sense_init();
137255

256+
#if 0
138257
// USB PD
258+
/* PWR register access (for disabling dead battery feature) */
259+
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);
260+
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_CRC);
261+
262+
__HAL_RCC_UCPD1_CLK_ENABLE();
263+
264+
// Default CC1/CC2 is PB4/PB6
265+
// PB4 ------> UCPD1_CC2
266+
// PB6 ------> UCPD1_CC1
267+
268+
usbpd_init(0, TUSB_TYPEC_PORT_SNK);
269+
#endif
139270

140271
}
141272

src/common/tusb_types.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,19 @@ enum {
232232

233233
#define TUSB_DESC_CONFIG_POWER_MA(x) ((x)/2)
234234

235+
//--------------------------------------------------------------------+
236+
// TYPE-C
237+
//--------------------------------------------------------------------+
238+
239+
typedef enum {
240+
TUSB_TYPEC_PORT_SRC,
241+
TUSB_TYPEC_PORT_SNK,
242+
TUSB_TYPEC_PORT_DRP
243+
} tusb_typec_port_type_t;
244+
245+
//--------------------------------------------------------------------+
246+
//
247+
//--------------------------------------------------------------------+
235248
typedef enum
236249
{
237250
XFER_RESULT_SUCCESS = 0,

0 commit comments

Comments
 (0)