Skip to content

Commit ead2b6f

Browse files
committed
Ignore SOP' messages
1 parent 3e0b51d commit ead2b6f

File tree

9 files changed

+143
-11
lines changed

9 files changed

+143
-11
lines changed

.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"files.associations": {
3+
"functional": "cpp"
4+
}
5+
}

library.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222
"examples": [
2323
{
2424
"name": "Protocol Analyzer",
25-
"base": "pio-examples/ProtocolAnalyzer",
26-
"files": [ "platformio.ini", "main.cpp" ]
25+
"base": "examples/ProtocolAnalyzer",
26+
"files": [ "ProtocolAnalyzer.ino" ]
2727
},
2828
{
2929
"name": "Trigger Board",
30-
"base": "pio-examples/TriggerBoard",
31-
"files": [ "platformio.ini", "main.cpp" ]
30+
"base": "examples/TriggerBoard",
31+
"files": [ "TriggerBoard.ino" ]
3232
}
3333
]
3434
}

src/PDController.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ void PDController::onNoGoodCrcReceived() {
151151
void PDController::onMessageReceived(PDMessage* message) {
152152
int messageId = message->messageId();
153153
PDMessageType type = message->type();
154+
PDSOPSequence sopSeq = message->sopSequence;
154155
bool reportTransmissionFailed = false;
155156

156157
log(PDLogEntryType::messageReceived, message);
@@ -164,7 +165,7 @@ void PDController::onMessageReceived(PDMessage* message) {
164165

165166
if (isTransmitting()) {
166167
// a GoodCRC message is expected
167-
if (type != PDMessageType::controlGoodCrc || messageId != txMessageId) {
168+
if (type != PDMessageType::controlGoodCrc || sopSeq != PDSOPSequence::sop || messageId != txMessageId) {
168169
log(PDLogEntryType::transmissionFailed);
169170
reportTransmissionFailed = true;
170171
}
@@ -174,7 +175,7 @@ void PDController::onMessageReceived(PDMessage* message) {
174175
lastMessage = nullptr;
175176
}
176177

177-
if (type != PDMessageType::controlGoodCrc && !isMonitorOnly) {
178+
if (type != PDMessageType::controlGoodCrc && sopSeq == PDSOPSequence::sop && !isMonitorOnly) {
178179

179180
// if the message has the same message ID like the previous one,
180181
// 'lastMessage' is not set to prevent notifying the event handler twice

src/ProtoclAnalyzer.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,22 @@ static const char* getSOPSequenceName(PDSOPSequence sequence) {
105105
return SOPSequenceNames[index];
106106
}
107107

108+
static const char* getSender(const PDMessage* message) {
109+
auto seq = message->sopSequence;
110+
if (seq == PDSOPSequence::sop) {
111+
return (message->header & 0x0100) != 0 ? "Source" : "Sink";
112+
} else if (seq == PDSOPSequence::sop1 || seq == PDSOPSequence::sop2) {
113+
return (message->header & 0x0100) != 0 ? "Cable" : "Port";
114+
}
115+
116+
return "";
117+
}
118+
108119
static void printMessage(const PDMessage* message) {
109-
Serial.printf("CC%d %-5s %-20s %d %04x", message->cc, getSOPSequenceName(message->sopSequence),
110-
getMessageName(message->type()), message->messageId(), message->header);
120+
Serial.printf("CC%d %-5s %-7s %-20s %d %04x",
121+
message->cc, getSOPSequenceName(message->sopSequence),
122+
getSender(message),getMessageName(message->type()),
123+
message->messageId(), message->header);
111124
int numObjects = message->numObjects();
112125
for (int i = 0; i < numObjects; i++)
113126
Serial.printf(" %08x", message->objects[i]);

test/ListCapabilities/.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 Capabilities\""
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 Capabilities\""
38+
39+
[env:nucleo_g431kb]
40+
board = nucleo_g431kb

test/ListCapabilities/src/main.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
// --- List the capabilities of the connected power supply
11+
//
12+
13+
#include <Arduino.h>
14+
#include "PowerDelivery.h"
15+
16+
static void handleEvent(PDSinkEventType eventType);
17+
static void listCapabilities();
18+
static const char* getSupplyTypeName(PDSupplyType type);
19+
20+
void setup() {
21+
Serial.begin(115200);
22+
PowerSink.start(handleEvent);
23+
}
24+
25+
void loop() {
26+
PowerSink.poll();
27+
}
28+
29+
void handleEvent(PDSinkEventType eventType) {
30+
if (eventType == PDSinkEventType::sourceCapabilitiesChanged && PowerSink.isConnected())
31+
listCapabilities();
32+
}
33+
34+
void listCapabilities() {
35+
Serial.println("USB PD capabilities:");
36+
Serial.println("__Type_________Vmin____Vmax____Imax");
37+
38+
for (int i = 0; i < PowerSink.numSourceCapabilities; i += 1) {
39+
auto cap = PowerSink.sourceCapabilities[i];
40+
Serial.printf(" %-9s %6d %6d %6d", getSupplyTypeName(cap.supplyType), cap.minVoltage, cap.maxVoltage, cap.maxCurrent);
41+
Serial.println();
42+
}
43+
44+
Serial.println("(voltage in mV, current in mA)");
45+
Serial.println();
46+
}
47+
48+
static const char* const SupplyTypeNames[] = {
49+
[(int)PDSupplyType::fixed] = "Fixed",
50+
[(int)PDSupplyType::battery] = "Battery",
51+
[(int)PDSupplyType::variable] = "Variable",
52+
[(int)PDSupplyType::pps] = "PPS",
53+
};
54+
55+
const char* getSupplyTypeName(PDSupplyType type) {
56+
unsigned int arraySize = sizeof(SupplyTypeNames) / sizeof(SupplyTypeNames[0]);
57+
if ((unsigned int)type < arraySize)
58+
return SupplyTypeNames[(unsigned int)type];
59+
else
60+
return "<unknown>";
61+
}

test/VoltageChange/src/main.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@
1111
//
1212

1313
#include <Arduino.h>
14-
#include "TaskScheduler.h"
15-
#include "ProtocolAnalyzer.h"
1614
#include "PowerDelivery.h"
17-
#include "CRC32.h"
1815

1916
static void handleEvent(PDSinkEventType eventType);
2017
static void switchVoltage();

0 commit comments

Comments
 (0)