usb_cables: add faster inter-connector test using ground

This commit is contained in:
King Kévin 2019-12-08 14:56:06 +01:00
parent 84c70a61d1
commit 4735911444
2 changed files with 192 additions and 2 deletions

View File

@ -38,51 +38,61 @@ static const struct usb_pin_t usb_host_a_pins[] = {
.name = "shield",
.port = GPIOD,
.pin = GPIO0,
.gnd = false,
},
{
.name = "VBUS",
.port = GPIOC,
.pin = GPIO12,
.gnd = false,
},
{
.name = "D-",
.port = GPIOA,
.pin = GPIO15,
.gnd = false,
},
{
.name = "D+",
.port = GPIOG,
.pin = GPIO6,
.gnd = false,
},
{
.name = "GND",
.port = GPIOG,
.pin = GPIO2,
.gnd = true,
},
{
.name = "SSRX-",
.port = GPIOD,
.pin = GPIO14,
.gnd = false,
},
{
.name = "SSRX+",
.port = GPIOG,
.pin = GPIO4,
.gnd = false,
},
{
.name = "GND_DRAIN",
.port = GPIOG,
.pin = GPIO8,
.gnd = true,
},
{
.name = "SSTX-",
.port = GPIOC,
.pin = GPIO10,
.gnd = false,
},
{
.name = "SSTX+",
.port = GPIOC,
.pin = GPIO11,
.gnd = false,
},
};
@ -92,126 +102,151 @@ static const struct usb_pin_t usb_host_c_pins[] = {
.name = "shield",
.port = GPIOG,
.pin = GPIO13,
.gnd = false,
},
{
.name = "GND-A1",
.port = GPIOD,
.pin = GPIO15,
.gnd = true,
},
{
.name = "SSTXp1",
.port = GPIOG,
.pin = GPIO3,
.gnd = false,
},
{
.name = "SSTXn1",
.port = GPIOG,
.pin = GPIO5,
.gnd = false,
},
{
.name = "VBUS-A4",
.port = GPIOG,
.pin = GPIO7,
.gnd = false,
},
{
.name = "CC1",
.port = GPIOC,
.pin = GPIO6,
.gnd = false,
},
{
.name = "Dp1",
.port = GPIOC,
.pin = GPIO8,
.gnd = false,
},
{
.name = "Dn1",
.port = GPIOA,
.pin = GPIO8,
.gnd = false,
},
{
.name = "SBU1",
.port = GPIOA,
.pin = GPIO10,
.gnd = false,
},
{
.name = "VBUS-A9",
.port = GPIOG,
.pin = GPIO11,
.gnd = false,
},
{
.name = "SSRXn2",
.port = GPIOA,
.pin = GPIO9,
.gnd = false,
},
{
.name = "SSRXp2",
.port = GPIOC,
.pin = GPIO9,
.gnd = false,
},
{
.name = "GND-A12",
.port = GPIOC,
.pin = GPIO7,
.gnd = true,
},
{
.name = "GND-B12",
.port = GPIOG,
.pin = GPIO12,
.gnd = true,
},
{
.name = "SSRXp1",
.port = GPIOG,
.pin = GPIO14,
.gnd = false,
},
{
.name = "SSRXn1",
.port = GPIOG,
.pin = GPIO10,
.gnd = false,
},
{
.name = "VBUS-B9",
.port = GPIOG,
.pin = GPIO9,
.gnd = false,
},
{
.name = "SBU2",
.port = GPIOG,
.pin = GPIO15,
.gnd = false,
},
{
.name = "Dn2",
.port = GPIOD,
.pin = GPIO7,
.gnd = false,
},
{
.name = "Dp2",
.port = GPIOD,
.pin = GPIO6,
.gnd = false,
},
{
.name = "CC2",
.port = GPIOD,
.pin = GPIO1,
.gnd = false,
},
{
.name = "VBUS-B4",
.port = GPIOD,
.pin = GPIO5,
.gnd = false,
},
{
.name = "SSTXn2",
.port = GPIOD,
.pin = GPIO4,
.gnd = false,
},
{
.name = "SSTXp2",
.port = GPIOD,
.pin = GPIO2,
.gnd = false,
},
{
.name = "GND-B1",
.port = GPIOD,
.pin = GPIO3,
.gnd = true,
},
};
@ -223,51 +258,61 @@ static const struct usb_pin_t usb_device_a_pins[] = {
.name = "shield",
.port = GPIOB,
.pin = GPIO3,
.gnd = false,
},
{
.name = "VBUS",
.port = GPIOE,
.pin = GPIO3,
.gnd = false,
},
{
.name = "D-",
.port = GPIOE,
.pin = GPIO1,
.gnd = false,
},
{
.name = "D+",
.port = GPIOB,
.pin = GPIO9,
.gnd = false,
},
{
.name = "GND",
.port = GPIOB,
.pin = GPIO5,
.gnd = true,
},
{
.name = "SSRX-",
.port = GPIOB,
.pin = GPIO4,
.gnd = false,
},
{
.name = "SSRX+",
.port = GPIOB,
.pin = GPIO8,
.gnd = false,
},
{
.name = "GND_DRAIN",
.port = GPIOE,
.pin = GPIO0,
.gnd = false,
},
{
.name = "SSTX-",
.port = GPIOC,
.pin = GPIO13,
.gnd = false,
},
{
.name = "SSTX+",
.port = GPIOE,
.pin = GPIO2,
.gnd = false,
},
};
@ -277,51 +322,61 @@ static const struct usb_pin_t usb_device_b_pins[] = {
.name = "shield",
.port = GPIOF,
.pin = GPIO1,
.gnd = false,
},
{
.name = "VBUS",
.port = GPIOC,
.pin = GPIO3,
.gnd = false,
},
{
.name = "D-",
.port = GPIOA,
.pin = GPIO0,
.gnd = false,
},
{
.name = "D+",
.port = GPIOA,
.pin = GPIO4,
.gnd = false,
},
{
.name = "GND",
.port = GPIOC,
.pin = GPIO0,
.gnd = true,
},
{
.name = "SSTX-",
.port = GPIOA,
.pin = GPIO2,
.gnd = false,
},
{
.name = "SSTX+",
.port = GPIOA,
.pin = GPIO1,
.gnd = false,
},
{
.name = "GND_DRAIN",
.port = GPIOF,
.pin = GPIO6,
.gnd = false,
},
{
.name = "SSRX-",
.port = GPIOC,
.pin = GPIO2,
.gnd = false,
},
{
.name = "SSRX+",
.port = GPIOC,
.pin = GPIO1,
.gnd = false,
},
};
@ -333,31 +388,37 @@ static const struct usb_pin_t usb_device_minib_pins[] = {
.name = "shield",
.port = GPIOC,
.pin = GPIO14,
.gnd = false,
},
{
.name = "VBUS",
.port = GPIOE,
.pin = GPIO4,
.gnd = false,
},
{
.name = "D-",
.port = GPIOE,
.pin = GPIO5,
.gnd = false,
},
{
.name = "D+",
.port = GPIOE,
.pin = GPIO6,
.gnd = false,
},
{
.name = "ID",
.port = GPIOF,
.pin = GPIO0,
.gnd = false,
},
{
.name = "GND",
.port = GPIOC,
.pin = GPIO15,
.gnd = true,
},
};
@ -367,56 +428,67 @@ static const struct usb_pin_t usb_device_microb_pins[] = {
.name = "shield",
.port = GPIOD,
.pin = GPIO12,
.gnd = false,
},
{
.name = "VBUS",
.port = GPIOD,
.pin = GPIO11,
.gnd = false,
},
{
.name = "D-",
.port = GPIOD,
.pin = GPIO10,
.gnd = false,
},
{
.name = "D+",
.port = GPIOD,
.pin = GPIO9,
.gnd = false,
},
{
.name = "ID",
.port = GPIOD,
.pin = GPIO8,
.gnd = false,
},
{
.name = "GND",
.port = GPIOB,
.pin = GPIO15,
.gnd = true,
},
{
.name = "SSTX-",
.port = GPIOB,
.pin = GPIO14,
.gnd = false,
},
{
.name = "SSTX+",
.port = GPIOB,
.pin = GPIO13,
.gnd = false,
},
{
.name = "GND_DRAIN",
.port = GPIOB,
.pin = GPIO11,
.gnd = false,
},
{
.name = "SSRX-",
.port = GPIOB,
.pin = GPIO10,
.gnd = false,
},
{
.name = "SSRX+",
.port = GPIOB,
.pin = GPIO12,
.gnd = false,
},
};
@ -426,126 +498,151 @@ static const struct usb_pin_t usb_device_c_pins[] = {
.name = "shield",
.port = GPIOE,
.pin = GPIO11,
.gnd = false,
},
{
.name = "GND-A1",
.port = GPIOF,
.pin = GPIO13,
.gnd = true,
},
{
.name = "SSTXp1",
.port = GPIOF,
.pin = GPIO12,
.gnd = false,
},
{
.name = "SSTXn1",
.port = GPIOF,
.pin = GPIO11,
.gnd = false,
},
{
.name = "VBUS-A4",
.port = GPIOB,
.pin = GPIO2,
.gnd = false,
},
{
.name = "CC1",
.port = GPIOB,
.pin = GPIO1,
.gnd = false,
},
{
.name = "Dp1",
.port = GPIOB,
.pin = GPIO0,
.gnd = false,
},
{
.name = "Dn1",
.port = GPIOC,
.pin = GPIO5,
.gnd = false,
},
{
.name = "SBU1",
.port = GPIOC,
.pin = GPIO4,
.gnd = false,
},
{
.name = "VBUS-A9",
.port = GPIOA,
.pin = GPIO7,
.gnd = false,
},
{
.name = "SSRXn2",
.port = GPIOA,
.pin = GPIO6,
.gnd = false,
},
{
.name = "SSRXp2",
.port = GPIOA,
.pin = GPIO5,
.gnd = false,
},
{
.name = "GND-A12",
.port = GPIOA,
.pin = GPIO3,
.gnd = true,
},
{
.name = "GND-B12",
.port = GPIOE,
.pin = GPIO10,
.gnd = true,
},
{
.name = "SSRXp1",
.port = GPIOE,
.pin = GPIO12,
.gnd = false,
},
{
.name = "SSRXn1",
.port = GPIOE,
.pin = GPIO9,
.gnd = false,
},
{
.name = "VBUS-B9",
.port = GPIOE,
.pin = GPIO8,
.gnd = false,
},
{
.name = "SBU2",
.port = GPIOE,
.pin = GPIO13,
.gnd = false,
},
{
.name = "Dn2",
.port = GPIOE,
.pin = GPIO7,
.gnd = false,
},
{
.name = "Dp2",
.port = GPIOG,
.pin = GPIO1,
.gnd = false,
},
{
.name = "CC2",
.port = GPIOE,
.pin = GPIO14,
.gnd = false,
},
{
.name = "VBUS-B4",
.port = GPIOG,
.pin = GPIO0,
.gnd = false,
},
{
.name = "SSTXn2",
.port = GPIOF,
.pin = GPIO15,
.gnd = false,
},
{
.name = "SSTXp2",
.port = GPIOE,
.pin = GPIO15,
.gnd = false,
},
{
.name = "GND-B1",
.port = GPIOF,
.pin = GPIO14,
.gnd = true,
},
};
@ -1407,6 +1504,88 @@ void usb_cables_check_inter(const struct usb_connector_t** connectors, uint8_t c
usb_cables_connectors_float(connectors, connectors_nb); // go back to safe state
}
void usb_cables_check_ground(const struct usb_connector_t** connectors, uint8_t connectors_nb, bool* connected)
{
// verify input arguments
if (NULL == connectors || 0 == connectors_nb) {
return;
}
// initialize output result
if (connected) {
for (uint8_t connector = 0; connector < connectors_nb; connector++) {
connected[connector] = false;
}
}
// pull up all ground pins
for (uint8_t connector = 0; connector < connectors_nb; connector++) {
for (uint8_t pin = 0; pin < connectors[connector]->pins_nb; pin++) {
if (connectors[connector]->pins[pin].gnd) {
uint32_t pin_port = connectors[connector]->pins[pin].port;
uint16_t pin_pin = connectors[connector]->pins[pin].pin;
gpio_set_mode(pin_port, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, pin_pin);
gpio_set(pin_port, pin_pin);
}
}
}
bool gnd_found = false; // if we found the ground connection
for (uint8_t connector_from = 0; connector_from < connectors_nb; connector_from++) { // test from every connector
for (uint8_t pin_from = 0; pin_from < connectors[connector_from]->pins_nb; pin_from++) { // test from every pin
if (!connectors[connector_from]->pins[pin_from].gnd) { // only consider ground connections
continue;
}
// set pin low
uint32_t pin_from_port = connectors[connector_from]->pins[pin_from].port;
uint16_t pin_from_pin = connectors[connector_from]->pins[pin_from].pin;
gpio_set_mode(pin_from_port, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, pin_from_pin);
gpio_clear(pin_from_port, pin_from_pin);
for (uint8_t connector_to = connector_from + 1; connector_to < connectors_nb; connector_to++) { // test every next connector
if (connected && connected[connector_to]) { // we already found a connection to this connector
continue;
}
for (uint8_t pin_to = 0; pin_to < connectors[connector_to]->pins_nb; pin_to++) { // test to every pin
if (!connectors[connector_to]->pins[pin_to].gnd) { // only consider ground connections
continue;
}
uint32_t pin_to_port = connectors[connector_to]->pins[pin_to].port;
uint16_t pin_to_pin = connectors[connector_to]->pins[pin_to].pin;
if (0 == gpio_get(pin_to_port, pin_to_pin)) { // we found a ground interconnection
if (connected) { // remember they are connected
connected[connector_from] = true;
connected[connector_to] = true;
} else {
printf("%s (%s) %s connected to %s (%s) %s\n", connectors[connector_from]->name, connectors[connector_from]->host ? "host" : "device", connectors[connector_from]->pins[pin_from].name, connectors[connector_to]->name, connectors[connector_to]->host ? "host" : "device", connectors[connector_to]->pins[pin_to].name);
}
gnd_found = true; // remember we found a ground connection
}
}
}
// pull pin back up
gpio_set_mode(pin_from_port, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, pin_from_pin);
gpio_set(pin_from_port, pin_from_pin);
if (gnd_found) { // we found all ground connections
break; // search is finished
}
}
if (gnd_found) { // we found all ground connections
break; // search is finished
}
}
// put all ground pins back to float
for (uint8_t connector = 0; connector < connectors_nb; connector++) {
for (uint8_t pin = 0; pin < connectors[connector]->pins_nb; pin++) {
if (connectors[connector]->pins[pin].gnd) {
uint32_t pin_port = connectors[connector]->pins[pin].port;
uint16_t pin_pin = connectors[connector]->pins[pin].pin;
gpio_set_mode(pin_port, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, pin_pin);
}
}
}
}
bool usb_cables_check_cable(const struct usb_cable_t* usb_cable, uint8_t* defined, uint8_t* undefined, uint8_t* disconnected, uint8_t* error)
{
if (NULL == usb_cable) {

View File

@ -23,6 +23,7 @@ struct usb_pin_t {
const char* name; /*< pin name */
uint32_t port; /*< on which MCU port is this pin connected */
uint16_t pin; /*< on which MCU pin is this pin connected */
bool gnd; /*< if this is a ground pin */
};
/** USB connector definition */
@ -58,16 +59,26 @@ void usb_cables_pins_float(const struct usb_connector_t* connector);
void usb_cables_connectors_float(const struct usb_connector_t** connectors, uint8_t connectors_nb);
/** check connector for connections between pins of this connector
* @param[in] connector connector to check
* @param[out] connected which of the pins are connected (NULL to just print the pinss)
* @param[out] connected which of the pins are connected (NULL to just print the pins)
*/
void usb_cables_check_intra(const struct usb_connector_t* connector, bool* connected);
/** check connectors for connections between pins of the connectors
* @param[in] connectors connectors to check
* @param[in] connectors_nb numbers of connectors
* @param[out] connected which of the connectors are connected (NULL to just print the connections)
* @note connection between pin on the same connector are not checked
* @note connection between pin of the same connector are not checked
* @note when connected is NULL all pin connections will be tested, else some might be skipped to speed scan
*/
void usb_cables_check_inter(const struct usb_connector_t** connectors, uint8_t connectors_nb, bool* connected);
/** check connectors for connections between ground pins of the connectors
* @param[in] connectors connectors to check
* @param[in] connectors_nb numbers of connectors
* @param[out] connected which of the connectors are connected (NULL to just print the connections)
* @note connection between pin on the same connector are not checked
* @note this is faster than usb_cables_check_inter but only checks the ground pins
* @note it assumes all grounds are connected (e.g. only one cable is connected)
*/
void usb_cables_check_ground(const struct usb_connector_t** connectors, uint8_t connectors_nb, bool* connected);
/** check USB cable connections
* @param[in] usb_cable USB cable to check
* @param[out] defined number of pin pairs that are connected according to definition