diff options
-rw-r--r-- | common_features.mk | 24 | ||||
-rw-r--r-- | drivers/chibios/usbpd_stm32g4.c | 76 | ||||
-rw-r--r-- | drivers/usbpd.h | 29 | ||||
-rw-r--r-- | quantum/quantum.h | 4 |
4 files changed, 133 insertions, 0 deletions
diff --git a/common_features.mk b/common_features.mk index ecb4f5576..8338ce408 100644 --- a/common_features.mk +++ b/common_features.mk | |||
@@ -631,3 +631,27 @@ endif | |||
631 | ifeq ($(strip $(JOYSTICK_ENABLE)), digital) | 631 | ifeq ($(strip $(JOYSTICK_ENABLE)), digital) |
632 | OPT_DEFS += -DDIGITAL_JOYSTICK_ENABLE | 632 | OPT_DEFS += -DDIGITAL_JOYSTICK_ENABLE |
633 | endif | 633 | endif |
634 | |||
635 | USBPD_ENABLE ?= no | ||
636 | VALID_USBPD_DRIVER_TYPES = custom vendor | ||
637 | USBPD_DRIVER ?= vendor | ||
638 | ifeq ($(strip $(USBPD_ENABLE)), yes) | ||
639 | ifeq ($(filter $(strip $(USBPD_DRIVER)),$(VALID_USBPD_DRIVER_TYPES)),) | ||
640 | $(error USBPD_DRIVER="$(USBPD_DRIVER)" is not a valid USBPD driver) | ||
641 | else | ||
642 | OPT_DEFS += -DUSBPD_ENABLE | ||
643 | ifeq ($(strip $(USBPD_DRIVER)), vendor) | ||
644 | # Vendor-specific implementations | ||
645 | OPT_DEFS += -DUSBPD_VENDOR | ||
646 | ifeq ($(strip $(MCU_SERIES)), STM32G4xx) | ||
647 | OPT_DEFS += -DUSBPD_STM32G4 | ||
648 | SRC += usbpd_stm32g4.c | ||
649 | else | ||
650 | $(error There is no vendor-provided USBPD driver available) | ||
651 | endif | ||
652 | else ifeq ($(strip $(USBPD_DRIVER)), custom) | ||
653 | OPT_DEFS += -DUSBPD_CUSTOM | ||
654 | # Board designers can add their own driver to $(SRC) | ||
655 | endif | ||
656 | endif | ||
657 | endif \ No newline at end of file | ||
diff --git a/drivers/chibios/usbpd_stm32g4.c b/drivers/chibios/usbpd_stm32g4.c new file mode 100644 index 000000000..54c1e1fb3 --- /dev/null +++ b/drivers/chibios/usbpd_stm32g4.c | |||
@@ -0,0 +1,76 @@ | |||
1 | /* Copyright 2021 Nick Brassel (@tzarc) | ||
2 | * | ||
3 | * This program is free software: you can redistribute it and/or modify | ||
4 | * it under the terms of the GNU General Public License as published by | ||
5 | * the Free Software Foundation, either version 2 of the License, or | ||
6 | * (at your option) any later version. | ||
7 | * | ||
8 | * This program is distributed in the hope that it will be useful, | ||
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
11 | * GNU General Public License for more details. | ||
12 | * | ||
13 | * You should have received a copy of the GNU General Public License | ||
14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
15 | */ | ||
16 | |||
17 | #include <quantum.h> | ||
18 | |||
19 | #ifndef USBPD_UCPD1_CFG1 | ||
20 | # define USBPD_UCPD1_CFG1 (UCPD_CFG1_PSC_UCPDCLK_0 | UCPD_CFG1_TRANSWIN_3 | UCPD_CFG1_IFRGAP_4 | UCPD_CFG1_HBITCLKDIV_4) | ||
21 | #endif // USBPD_UCPD1_CFG1 | ||
22 | |||
23 | // Initialises the USBPD subsystem | ||
24 | __attribute__((weak)) void usbpd_init(void) { | ||
25 | // Disable dead-battery signals | ||
26 | PWR->CR3 |= PWR_CR3_UCPD_DBDIS; | ||
27 | // Enable the clock for the UCPD1 peripheral | ||
28 | RCC->APB1ENR2 |= RCC_APB1ENR2_UCPD1EN; | ||
29 | |||
30 | // Copy the existing value | ||
31 | uint32_t CFG1 = UCPD1->CFG1; | ||
32 | // Force-disable UCPD1 before configuring | ||
33 | CFG1 &= ~UCPD_CFG1_UCPDEN; | ||
34 | // Configure UCPD1 | ||
35 | CFG1 = USBPD_UCPD1_CFG1; | ||
36 | // Apply the changes | ||
37 | UCPD1->CFG1 = CFG1; | ||
38 | // Enable UCPD1 | ||
39 | UCPD1->CFG1 |= UCPD_CFG1_UCPDEN; | ||
40 | |||
41 | // Copy the existing value | ||
42 | uint32_t CR = UCPD1->CR; | ||
43 | // Clear out ANASUBMODE (irrelevant as a sink device) | ||
44 | CR &= ~UCPD_CR_ANASUBMODE_Msk; | ||
45 | // Advertise our capabilities as a sink, with both CC lines enabled | ||
46 | CR |= UCPD_CR_ANAMODE | UCPD_CR_CCENABLE_Msk; | ||
47 | // Apply the changes | ||
48 | UCPD1->CR = CR; | ||
49 | } | ||
50 | |||
51 | // Gets the current state of the USBPD allowance | ||
52 | __attribute__((weak)) usbpd_allowance_t usbpd_get_allowance(void) { | ||
53 | uint32_t CR = UCPD1->CR; | ||
54 | |||
55 | int ucpd_enabled = (UCPD1->CFG1 & UCPD_CFG1_UCPDEN_Msk) >> UCPD_CFG1_UCPDEN_Pos; | ||
56 | int anamode = (CR & UCPD_CR_ANAMODE_Msk) >> UCPD_CR_ANAMODE_Pos; | ||
57 | int cc_enabled = (CR & UCPD_CR_CCENABLE_Msk) >> UCPD_CR_CCENABLE_Pos; | ||
58 | |||
59 | if (ucpd_enabled && anamode && cc_enabled) { | ||
60 | uint32_t SR = UCPD1->SR; | ||
61 | int vstate_cc1 = (SR & UCPD_SR_TYPEC_VSTATE_CC1_Msk) >> UCPD_SR_TYPEC_VSTATE_CC1_Pos; | ||
62 | int vstate_cc2 = (SR & UCPD_SR_TYPEC_VSTATE_CC2_Msk) >> UCPD_SR_TYPEC_VSTATE_CC2_Pos; | ||
63 | int vstate_max = vstate_cc1 > vstate_cc2 ? vstate_cc1 : vstate_cc2; | ||
64 | switch (vstate_max) { | ||
65 | case 0: | ||
66 | case 1: | ||
67 | return USBPD_500MA; // Note that this is 500mA (i.e. max USB 2.0), not 900mA, as we're not using USB 3.1 as a sink device. | ||
68 | case 2: | ||
69 | return USBPD_1500MA; | ||
70 | case 3: | ||
71 | return USBPD_3000MA; | ||
72 | } | ||
73 | } | ||
74 | |||
75 | return USBPD_500MA; | ||
76 | } \ No newline at end of file | ||
diff --git a/drivers/usbpd.h b/drivers/usbpd.h new file mode 100644 index 000000000..df4f29bb9 --- /dev/null +++ b/drivers/usbpd.h | |||
@@ -0,0 +1,29 @@ | |||
1 | /* Copyright 2021 Nick Brassel (@tzarc) | ||
2 | * | ||
3 | * This program is free software: you can redistribute it and/or modify | ||
4 | * it under the terms of the GNU General Public License as published by | ||
5 | * the Free Software Foundation, either version 2 of the License, or | ||
6 | * (at your option) any later version. | ||
7 | * | ||
8 | * This program is distributed in the hope that it will be useful, | ||
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
11 | * GNU General Public License for more details. | ||
12 | * | ||
13 | * You should have received a copy of the GNU General Public License | ||
14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
15 | */ | ||
16 | |||
17 | #pragma once | ||
18 | |||
19 | typedef enum { | ||
20 | USBPD_500MA, | ||
21 | USBPD_1500MA, | ||
22 | USBPD_3000MA, | ||
23 | } usbpd_allowance_t; | ||
24 | |||
25 | // Initialises the USBPD subsystem | ||
26 | void usbpd_init(void); | ||
27 | |||
28 | // Gets the current state of the USBPD allowance | ||
29 | usbpd_allowance_t usbpd_get_allowance(void); \ No newline at end of file | ||
diff --git a/quantum/quantum.h b/quantum/quantum.h index 370a65fe0..b7bf5be31 100644 --- a/quantum/quantum.h +++ b/quantum/quantum.h | |||
@@ -193,6 +193,10 @@ extern layer_state_t layer_state; | |||
193 | # include "wpm.h" | 193 | # include "wpm.h" |
194 | #endif | 194 | #endif |
195 | 195 | ||
196 | #ifdef USBPD_ENABLE | ||
197 | # include "usbpd.h" | ||
198 | #endif | ||
199 | |||
196 | // Function substitutions to ease GPIO manipulation | 200 | // Function substitutions to ease GPIO manipulation |
197 | #if defined(__AVR__) | 201 | #if defined(__AVR__) |
198 | typedef uint8_t pin_t; | 202 | typedef uint8_t pin_t; |