Skip to content

Commit 3e0b51d

Browse files
committed
Test programs
1 parent 35bd858 commit 3e0b51d

File tree

8 files changed

+214
-0
lines changed

8 files changed

+214
-0
lines changed

test/ProtocolAnalyzer/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.pio
2+
.vscode/.browse.c_cpp.db*
3+
.vscode/c_cpp_properties.json
4+
.vscode/launch.json
5+
.vscode/ipch
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
// See http://go.microsoft.com/fwlink/?LinkId=827846
3+
// for the documentation about the extensions.json format
4+
"recommendations": [
5+
"platformio.platformio-ide"
6+
],
7+
"unwantedRecommendations": [
8+
"ms-vscode.cpptools-extension-pack"
9+
]
10+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
[env]
2+
platform = ststm32
3+
framework = arduino
4+
lib_deps =
5+
USB-Power-Delivery=symlink://../..
6+
7+
[common]
8+
build_flags =
9+
-D USBPD_DEBUG_PIN
10+
11+
[env:nucleo_l432kc]
12+
board = nucleo_l432kc
13+
14+
[env:bluepill_f103c8]
15+
board = bluepill_f103c8
16+
build_flags =
17+
-D ${common.build_flags}
18+
-D PIO_FRAMEWORK_ARDUINO_ENABLE_CDC
19+
-D USBCON
20+
-D HAL_PCD_MODULE_ENABLED
21+
-D USBD_VID=0xCAFE
22+
-D USBD_PID=0xDEAD
23+
-D USB_MANUFACTURER_STRING="\"Codecrete\""
24+
-D USB_PRODUCT_STRING="\"USB PD Analyzer\""
25+
26+
[env:blackpill_f401cc]
27+
board = blackpill_f401cc
28+
debug_tool = stlink
29+
build_flags =
30+
-D ${common.build_flags}
31+
-D PIO_FRAMEWORK_ARDUINO_ENABLE_CDC
32+
-D USBCON
33+
-D HAL_PCD_MODULE_ENABLED
34+
-D USBD_VID=0xCAFE
35+
-D USBD_PID=0xDEAD
36+
-D USB_MANUFACTURER_STRING="\"Codecrete\""
37+
-D USB_PRODUCT_STRING="\"USB PD Analyzer\""
38+
39+
[env:nucleo_g431kb]
40+
board = nucleo_g431kb

test/ProtocolAnalyzer/src/main.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// USB Power Delivery for Arduino
3+
// Copyright (c) 2023 Manuel Bleichenbacher
4+
//
5+
// Licensed under MIT License
6+
// https://opensource.org/licenses/MIT
7+
//
8+
9+
//
10+
// --- USB PD Protocol Analyzer
11+
//
12+
13+
#include <Arduino.h>
14+
#include "PowerDelivery.h"
15+
16+
void setup() {
17+
Serial.begin(115200);
18+
PowerController.startMonitor();
19+
}
20+
21+
void loop() {
22+
ProtocolAnalyzer::poll();
23+
}

test/VoltageChange/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.pio
2+
.vscode/.browse.c_cpp.db*
3+
.vscode/c_cpp_properties.json
4+
.vscode/launch.json
5+
.vscode/ipch
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
// See http://go.microsoft.com/fwlink/?LinkId=827846
3+
// for the documentation about the extensions.json format
4+
"recommendations": [
5+
"platformio.platformio-ide"
6+
],
7+
"unwantedRecommendations": [
8+
"ms-vscode.cpptools-extension-pack"
9+
]
10+
}

test/VoltageChange/platformio.ini

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
[env]
2+
platform = ststm32
3+
framework = arduino
4+
lib_deps =
5+
USB-Power-Delivery=symlink://../..
6+
7+
[common]
8+
build_flags =
9+
-D USBPD_DEBUG_PIN
10+
11+
[env:nucleo_l432kc]
12+
board = nucleo_l432kc
13+
14+
[env:bluepill_f103c8]
15+
board = bluepill_f103c8
16+
build_flags =
17+
-D ${common.build_flags}
18+
-D PIO_FRAMEWORK_ARDUINO_ENABLE_CDC
19+
-D USBCON
20+
-D HAL_PCD_MODULE_ENABLED
21+
-D USBD_VID=0xCAFE
22+
-D USBD_PID=0xDEAD
23+
-D USB_MANUFACTURER_STRING="\"Codecrete\""
24+
-D USB_PRODUCT_STRING="\"Voltage Switcher\""
25+
26+
[env:blackpill_f401cc]
27+
board = blackpill_f401cc
28+
debug_tool = stlink
29+
build_flags =
30+
-D ${common.build_flags}
31+
-D PIO_FRAMEWORK_ARDUINO_ENABLE_CDC
32+
-D USBCON
33+
-D HAL_PCD_MODULE_ENABLED
34+
-D USBD_VID=0xCAFE
35+
-D USBD_PID=0xDEAD
36+
-D USB_MANUFACTURER_STRING="\"Codecrete\""
37+
-D USB_PRODUCT_STRING="\"Voltage Switcher\""
38+
39+
[env:nucleo_g431kb]
40+
board = nucleo_g431kb

test/VoltageChange/src/main.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
//
2+
// USB Power Delivery for Arduino
3+
// Copyright (c) 2023 Manuel Bleichenbacher
4+
//
5+
// Licensed under MIT License
6+
// https://opensource.org/licenses/MIT
7+
//
8+
9+
//
10+
// --- Test program switching between the available fixed voltages
11+
//
12+
13+
#include <Arduino.h>
14+
#include "TaskScheduler.h"
15+
#include "ProtocolAnalyzer.h"
16+
#include "PowerDelivery.h"
17+
#include "CRC32.h"
18+
19+
static void handleEvent(PDSinkEventType eventType);
20+
static void switchVoltage();
21+
static bool hasExpired(uint32_t time);
22+
23+
static bool isUSBPDSource = false;
24+
static uint32_t nextVoltageChangeTime = 0;
25+
static int voltageIndex = 0;
26+
27+
void setup() {
28+
Serial.begin(115200);
29+
PowerSink.start(handleEvent);
30+
Serial.println();
31+
Serial.println("USB Power Delivery");
32+
}
33+
34+
void loop() {
35+
PowerSink.poll();
36+
ProtocolAnalyzer::poll();
37+
38+
if (isUSBPDSource && hasExpired(nextVoltageChangeTime))
39+
switchVoltage();
40+
}
41+
42+
void switchVoltage() {
43+
// select next fixed voltage
44+
do {
45+
voltageIndex += 1;
46+
if (voltageIndex >= PowerSink.numSourceCapabilities)
47+
voltageIndex = 0;
48+
} while (PowerSink.sourceCapabilities[voltageIndex].supplyType != PDSupplyType::fixed);
49+
50+
PowerSink.requestPower(PowerSink.sourceCapabilities[voltageIndex].maxVoltage);
51+
nextVoltageChangeTime += 3000;
52+
}
53+
54+
void handleEvent(PDSinkEventType eventType) {
55+
switch (eventType) {
56+
case PDSinkEventType::sourceCapabilitiesChanged:
57+
if (PowerSink.isConnected()) {
58+
Serial.println("New source capabilities (USB PD supply)");
59+
isUSBPDSource = true;
60+
voltageIndex = 0;
61+
nextVoltageChangeTime = millis() + 2000;
62+
} else {
63+
isUSBPDSource = false;
64+
Serial.println("New source capabilities (no USB PD supply connected)");
65+
}
66+
break;
67+
68+
case PDSinkEventType::voltageChanged:
69+
Serial.printf("Voltage changd: %5dmV %5dmA (max)", PowerSink.activeVoltage, PowerSink.activeCurrent);
70+
Serial.println();
71+
break;
72+
73+
case PDSinkEventType::powerRejected:
74+
Serial.println("Power request rejected");
75+
break;
76+
}
77+
}
78+
79+
bool hasExpired(uint32_t time) {
80+
return (int32_t)(time - millis()) <= 0;
81+
}

0 commit comments

Comments
 (0)