Skip to content

Commit bbc76e7

Browse files
authored
Merge pull request hathach#2104 from hathach/g4-pd
Initial support for USB PD stack
2 parents 39a6433 + 41801c2 commit bbc76e7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+2330
-1700
lines changed

.idea/runConfigurations/stm32g474_jlink.xml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.rst

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ Please take a look at the online `documentation <https://docs.tinyusb.org/>`__.
2020
├── docs # Documentation
2121
├── examples # Sample with Makefile build support
2222
├── hw
23-
   ├── bsp # Supported boards source files
24-
   └── mcu # Low level mcu core & peripheral drivers
23+
├── bsp # Supported boards source files
24+
└── mcu # Low level mcu core & peripheral drivers
2525
├── lib # Sources from 3rd party such as freeRTOS, fatfs ...
2626
├── src # All sources files for TinyUSB stack itself.
2727
├── test # Unit tests for the stack
@@ -89,6 +89,13 @@ Host Stack
8989
- Mass Storage Class (MSC)
9090
- Hub with multiple-level support
9191

92+
TypeC PD Stack
93+
==============
94+
95+
- Power Delivery 3.0 (PD3.0) with USB Type-C support (WIP)
96+
- Super early stage, only for testing purpose
97+
- Only support STM32 G4
98+
9299
OS Abstraction layer
93100
====================
94101

examples/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ project(tinyusb_examples C CXX ASM)
88
add_subdirectory(device)
99
add_subdirectory(dual)
1010
add_subdirectory(host)
11+
add_subdirectory(typec)

examples/typec/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
cmake_minimum_required(VERSION 3.17)
2+
3+
include(${CMAKE_CURRENT_SOURCE_DIR}/../../hw/bsp/family_support.cmake)
4+
5+
project(tinyusb_host_examples C CXX ASM)
6+
family_initialize_project(tinyusb_host_examples ${CMAKE_CURRENT_LIST_DIR})
7+
8+
# family_add_subdirectory will filter what to actually add based on selected FAMILY
9+
family_add_subdirectory(power_delivery)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
cmake_minimum_required(VERSION 3.17)
2+
3+
include(${CMAKE_CURRENT_SOURCE_DIR}/../../../hw/bsp/family_support.cmake)
4+
5+
# gets PROJECT name for the example (e.g. <BOARD>-<DIR_NAME>)
6+
family_get_project_name(PROJECT ${CMAKE_CURRENT_LIST_DIR})
7+
8+
project(${PROJECT} C CXX ASM)
9+
10+
# Checks this example is valid for the family and initializes the project
11+
family_initialize_project(${PROJECT} ${CMAKE_CURRENT_LIST_DIR})
12+
13+
# Espressif has its own cmake build system
14+
if(FAMILY STREQUAL "espressif")
15+
return()
16+
endif()
17+
18+
add_executable(${PROJECT})
19+
20+
# Example source
21+
target_sources(${PROJECT} PUBLIC
22+
${CMAKE_CURRENT_SOURCE_DIR}/src/main.c
23+
)
24+
25+
# Example include
26+
target_include_directories(${PROJECT} PUBLIC
27+
${CMAKE_CURRENT_SOURCE_DIR}/src
28+
)
29+
30+
# Configure compilation flags and libraries for the example... see the corresponding function
31+
# in hw/bsp/FAMILY/family.cmake for details.
32+
family_configure_device_example(${PROJECT})
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
include ../../make.mk
2+
3+
INC += \
4+
src \
5+
$(TOP)/hw \
6+
7+
# Example source
8+
EXAMPLE_SOURCE += $(wildcard src/*.c)
9+
SRC_C += $(addprefix $(CURRENT_PATH)/, $(EXAMPLE_SOURCE))
10+
11+
include ../../rules.mk
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mcu:STM32G4
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2019 Ha Thach (tinyusb.org)
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*
24+
*/
25+
26+
#include <stdlib.h>
27+
#include <stdio.h>
28+
#include <string.h>
29+
30+
#include "bsp/board.h"
31+
#include "tusb.h"
32+
33+
//--------------------------------------------------------------------+
34+
// MACRO CONSTANT TYPEDEF PROTOTYPES
35+
//--------------------------------------------------------------------+
36+
37+
// Voltage and current for selecting PDO
38+
// DANGEROUS: Please make sure your board can withstand the voltage and current
39+
// defined here. Otherwise, you may damage your board, smoke can come out
40+
#define VOLTAGE_MAX_MV 5000 // maximum voltage in mV
41+
#define CURRENT_MAX_MA 500 // maximum current in mA
42+
#define CURRENT_OPERATING_MA 100 // operating current in mA
43+
44+
/* Blink pattern
45+
* - 250 ms : button is not pressed
46+
* - 1000 ms : button is pressed (and hold)
47+
*/
48+
enum {
49+
BLINK_PRESSED = 250,
50+
BLINK_UNPRESSED = 1000
51+
};
52+
53+
static uint32_t blink_interval_ms = BLINK_UNPRESSED;
54+
55+
void led_blinking_task(void);
56+
57+
#define HELLO_STR "Hello from TinyUSB\r\n"
58+
59+
int main(void)
60+
{
61+
board_init();
62+
board_led_write(true);
63+
64+
tuc_init(0, TUSB_TYPEC_PORT_SNK);
65+
66+
uint32_t start_ms = 0;
67+
bool led_state = false;
68+
69+
while (1) {
70+
led_blinking_task();
71+
72+
// tinyusb typec task
73+
tuc_task();
74+
}
75+
}
76+
77+
#if CFG_TUSB_MCU == OPT_MCU_ESP32S2 || CFG_TUSB_MCU == OPT_MCU_ESP32S3
78+
void app_main(void)
79+
{
80+
main();
81+
}
82+
#endif
83+
84+
//--------------------------------------------------------------------+
85+
// TypeC PD callbacks
86+
//--------------------------------------------------------------------+
87+
88+
bool tuc_pd_data_received_cb(uint8_t rhport, pd_header_t const* header, uint8_t const* dobj, uint8_t const* p_end) {
89+
switch (header->msg_type) {
90+
case PD_DATA_SOURCE_CAP: {
91+
printf("PD Source Capabilities\r\n");
92+
// Examine source capability and select a suitable PDO (starting from 1 with safe5v)
93+
uint8_t selected_pos = 1;
94+
95+
for(size_t i=0; i<header->n_data_obj; i++) {
96+
TU_VERIFY(dobj < p_end);
97+
uint32_t const pdo = tu_le32toh(tu_unaligned_read32(dobj));
98+
99+
switch ((pdo >> 30) & 0x03ul) {
100+
case PD_PDO_TYPE_FIXED: {
101+
pd_pdo_fixed_t const* fixed = (pd_pdo_fixed_t const*) &pdo;
102+
uint32_t const voltage_mv = fixed->voltage_50mv*50;
103+
uint32_t const current_ma = fixed->current_max_10ma*10;
104+
printf("[Fixed] %lu mV %lu mA\r\n", voltage_mv, current_ma);
105+
106+
if (voltage_mv <= VOLTAGE_MAX_MV && current_ma >= CURRENT_MAX_MA) {
107+
// Found a suitable PDO
108+
selected_pos = i+1;
109+
}
110+
111+
break;
112+
}
113+
114+
case PD_PDO_TYPE_BATTERY:
115+
break;
116+
117+
case PD_PDO_TYPE_VARIABLE:
118+
break;
119+
120+
case PD_PDO_TYPE_APDO:
121+
break;
122+
}
123+
124+
dobj += 4;
125+
}
126+
127+
//------------- Response with selected PDO -------------//
128+
// Be careful and make sure your board can withstand the selected PDO
129+
// voltage other than safe5v e.g 12v or 20v
130+
131+
printf("Selected PDO %u\r\n", selected_pos);
132+
133+
// Send request with selected PDO position as response to Source Cap
134+
pd_rdo_fixed_variable_t rdo = {
135+
.current_extremum_10ma = 50, // max 500mA
136+
.current_operate_10ma = 30, // 300mA
137+
.reserved = 0,
138+
.epr_mode_capable = 0,
139+
.unchunked_ext_msg_support = 0,
140+
.no_usb_suspend = 0,
141+
.usb_comm_capable = 1,
142+
.capability_mismatch = 0,
143+
.give_back_flag = 0, // exteremum is max
144+
.object_position = selected_pos,
145+
};
146+
tuc_msg_request(rhport, &rdo);
147+
148+
break;
149+
}
150+
151+
default: break;
152+
}
153+
154+
return true;
155+
}
156+
157+
bool tuc_pd_control_received_cb(uint8_t rhport, pd_header_t const* header) {
158+
switch (header->msg_type) {
159+
case PD_CTRL_ACCEPT:
160+
printf("PD Request Accepted\r\n");
161+
// preparing for power transition
162+
break;
163+
164+
case PD_CTRL_REJECT:
165+
printf("PD Request Rejected\r\n");
166+
// try to negotiate further power
167+
break;
168+
169+
case PD_CTRL_PS_READY:
170+
printf("PD Power Ready\r\n");
171+
// Source is ready to supply power
172+
break;
173+
174+
default:
175+
break;
176+
}
177+
178+
return true;
179+
}
180+
181+
//--------------------------------------------------------------------+
182+
// BLINKING TASK
183+
//--------------------------------------------------------------------+
184+
void led_blinking_task(void)
185+
{
186+
static uint32_t start_ms = 0;
187+
static bool led_state = false;
188+
189+
// Blink every interval ms
190+
if ( board_millis() - start_ms < blink_interval_ms) return; // not enough time
191+
start_ms += blink_interval_ms;
192+
193+
board_led_write(led_state);
194+
led_state = 1 - led_state; // toggle
195+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2019 Ha Thach (tinyusb.org)
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*
24+
*/
25+
26+
#ifndef _TUSB_CONFIG_H_
27+
#define _TUSB_CONFIG_H_
28+
29+
#ifdef __cplusplus
30+
extern "C" {
31+
#endif
32+
33+
// special example that doesn't enable device or host stack
34+
// This can cause some TinyUSB API missing, this define hack to allow us to fill those API
35+
// to pass the compilation process
36+
#define tud_int_handler(x)
37+
38+
//--------------------------------------------------------------------
39+
// COMMON CONFIGURATION
40+
//--------------------------------------------------------------------
41+
42+
// defined by compiler flags for flexibility
43+
#ifndef CFG_TUSB_MCU
44+
#error CFG_TUSB_MCU must be defined
45+
#endif
46+
47+
#ifndef CFG_TUSB_OS
48+
#define CFG_TUSB_OS OPT_OS_NONE
49+
#endif
50+
51+
#define CFG_TUD_ENABLED 0
52+
#define CFG_TUH_ENABLED 0
53+
54+
// Enable TYPEC stack
55+
#define CFG_TUC_ENABLED 1
56+
57+
// CFG_TUSB_DEBUG is defined by compiler in DEBUG build
58+
// #define CFG_TUSB_DEBUG 0
59+
60+
/* USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment.
61+
* Tinyusb use follows macros to declare transferring memory so that they can be put
62+
* into those specific section.
63+
* e.g
64+
* - CFG_TUSB_MEM SECTION : __attribute__ (( section(".usb_ram") ))
65+
* - CFG_TUSB_MEM_ALIGN : __attribute__ ((aligned(4)))
66+
*/
67+
#ifndef CFG_TUSB_MEM_SECTION
68+
#define CFG_TUSB_MEM_SECTION
69+
#endif
70+
71+
#ifndef CFG_TUSB_MEM_ALIGN
72+
#define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4)))
73+
#endif
74+
75+
#ifdef __cplusplus
76+
}
77+
#endif
78+
79+
#endif /* _TUSB_CONFIG_H_ */

hw/bsp/board.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,12 @@ TU_ATTR_USED int sys_write (int fhdl, const void *buf, size_t count)
7171
{
7272
(void) fhdl;
7373
uint8_t const* buf8 = (uint8_t const*) buf;
74-
for(size_t i=0; i<count; i++)
75-
{
74+
75+
for(size_t i=0; i<count; i++) {
7676
ITM_SendChar(buf8[i]);
7777
}
78-
return count;
78+
79+
return (int) count;
7980
}
8081

8182
TU_ATTR_USED int sys_read (int fhdl, char *buf, size_t count)

0 commit comments

Comments
 (0)