usb_cables: use index for pins to save space
This commit is contained in:
parent
549f1c992f
commit
ff23100dfa
|
@ -331,32 +331,31 @@ static void command_pin(void* argument)
|
||||||
printf("%s (%s):\n", usb_connectors[connector]->name, usb_connectors[connector]->host ? "host" : "device"); // print connector name
|
printf("%s (%s):\n", usb_connectors[connector]->name, usb_connectors[connector]->host ? "host" : "device"); // print connector name
|
||||||
}
|
}
|
||||||
for (uint8_t pin = 0; pin < usb_connectors[connector]->pins_nb; pin++) { // test every pin
|
for (uint8_t pin = 0; pin < usb_connectors[connector]->pins_nb; pin++) { // test every pin
|
||||||
uint32_t pin_port = usb_connectors[connector]->pins[pin].port; // GPIO port corresponding to USB pin
|
const struct usb_pin_t* usb_pin = &usb_pins[usb_connectors[connector]->pins[pin]]; // get pin
|
||||||
uint16_t pin_pin = usb_connectors[connector]->pins[pin].pin; // GPIO pin corresponding to USB pin
|
|
||||||
if (!pin_str || (!pin_level && pin_nb == pin_i)) { // show pin state
|
if (!pin_str || (!pin_level && pin_nb == pin_i)) { // show pin state
|
||||||
printf("%03u %s: ", pin_i, usb_connectors[connector]->pins[pin].name); // print USB pin number
|
printf("%03u %s: ", pin_i, usb_pin->name); // print USB pin number
|
||||||
uint8_t pin_pos = __builtin_ctz(pin_pin); // get the pin number (position of the 1 in the 16-bit)
|
uint8_t pin_pos = __builtin_ctz(usb_pin->pin); // get the pin number (position of the 1 in the 16-bit)
|
||||||
uint8_t offset = (pin_pos < 8) ? (pin_pos * 4) : ((pin_pos - 8) * 4); // get pin offset within port
|
uint8_t offset = (pin_pos < 8) ? (pin_pos * 4) : ((pin_pos - 8) * 4); // get pin offset within port
|
||||||
uint8_t mode = (((pin_pos < 8) ? GPIO_CRL(pin_port) : GPIO_CRH(pin_port)) >> (offset + 0)) & 0x3; // get mode from pin for port
|
uint8_t mode = (((pin_pos < 8) ? GPIO_CRL(usb_pin->port) : GPIO_CRH(usb_pin->port)) >> (offset + 0)) & 0x3; // get mode from pin for port
|
||||||
uint8_t conf = (((pin_pos < 8) ? GPIO_CRL(pin_port) : GPIO_CRH(pin_port)) >> (offset + 2)) & 0x3; // get configuration from pin for port
|
uint8_t conf = (((pin_pos < 8) ? GPIO_CRL(usb_pin->port) : GPIO_CRH(usb_pin->port)) >> (offset + 2)) & 0x3; // get configuration from pin for port
|
||||||
// show set value
|
// show set value
|
||||||
if (0 == mode) { // pin configured as input
|
if (0 == mode) { // pin configured as input
|
||||||
if (1 == conf) { // pin is in floating configuration
|
if (1 == conf) { // pin is in floating configuration
|
||||||
putc('x');
|
putc('x');
|
||||||
} else if (0 == (GPIO_ODR(pin_port) & pin_pin)) {
|
} else if (0 == (GPIO_ODR(usb_pin->port) & usb_pin->pin)) {
|
||||||
putc('l');
|
putc('l');
|
||||||
} else {
|
} else {
|
||||||
putc('h');
|
putc('h');
|
||||||
}
|
}
|
||||||
} else { // pin configured as output
|
} else { // pin configured as output
|
||||||
if (0 == (GPIO_ODR(pin_port) & pin_pin)) {
|
if (0 == (GPIO_ODR(usb_pin->port) & usb_pin->pin)) {
|
||||||
putc('L');
|
putc('L');
|
||||||
} else {
|
} else {
|
||||||
putc('H');
|
putc('H');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// show actual value
|
// show actual value
|
||||||
if (gpio_get(pin_port, pin_pin)) {
|
if (gpio_get(usb_pin->port, usb_pin->pin)) {
|
||||||
putc('1');
|
putc('1');
|
||||||
} else {
|
} else {
|
||||||
putc('0');
|
putc('0');
|
||||||
|
@ -364,27 +363,27 @@ static void command_pin(void* argument)
|
||||||
putc('\n');
|
putc('\n');
|
||||||
}
|
}
|
||||||
if (pin_level && pin_nb == pin_i) { // set pin
|
if (pin_level && pin_nb == pin_i) { // set pin
|
||||||
printf("%03u %s: %s", pin_i, usb_connectors[connector]->pins[pin].name, pin_level);
|
printf("%03u %s: %s", pin_i, usb_pin->name, pin_level);
|
||||||
switch (pin_level[0]) {
|
switch (pin_level[0]) {
|
||||||
case 'h':
|
case 'h':
|
||||||
gpio_set(pin_port, pin_pin);
|
gpio_set(usb_pin->port, usb_pin->pin);
|
||||||
gpio_set_mode(pin_port, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, pin_pin);
|
gpio_set_mode(usb_pin->port, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, usb_pin->pin);
|
||||||
break;
|
break;
|
||||||
case 'H':
|
case 'H':
|
||||||
gpio_set(pin_port, pin_pin);
|
gpio_set(usb_pin->port, usb_pin->pin);
|
||||||
gpio_set_mode(pin_port, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, pin_pin);
|
gpio_set_mode(usb_pin->port, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, usb_pin->pin);
|
||||||
break;
|
break;
|
||||||
case 'l':
|
case 'l':
|
||||||
gpio_clear(pin_port, pin_pin);
|
gpio_clear(usb_pin->port, usb_pin->pin);
|
||||||
gpio_set_mode(pin_port, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, pin_pin);
|
gpio_set_mode(usb_pin->port, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, usb_pin->pin);
|
||||||
break;
|
break;
|
||||||
case 'L':
|
case 'L':
|
||||||
gpio_clear(pin_port, pin_pin);
|
gpio_clear(usb_pin->port, usb_pin->pin);
|
||||||
gpio_set_mode(pin_port, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, pin_pin);
|
gpio_set_mode(usb_pin->port, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, usb_pin->pin);
|
||||||
break;
|
break;
|
||||||
case 'x':
|
case 'x':
|
||||||
default:
|
default:
|
||||||
gpio_set_mode(pin_port, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, pin_pin);
|
gpio_set_mode(usb_pin->port, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, usb_pin->pin);
|
||||||
}
|
}
|
||||||
putc('\n');
|
putc('\n');
|
||||||
}
|
}
|
||||||
|
@ -415,19 +414,18 @@ static void command_test(void* argument)
|
||||||
float_errors = false; // restart test
|
float_errors = false; // restart test
|
||||||
for (uint8_t connector = 0; connector < LENGTH(usb_connectors); connector++) { // test every connector
|
for (uint8_t connector = 0; connector < LENGTH(usb_connectors); connector++) { // test every connector
|
||||||
for (uint8_t pin = 0; pin < usb_connectors[connector]->pins_nb; pin++) { // test every pin
|
for (uint8_t pin = 0; pin < usb_connectors[connector]->pins_nb; pin++) { // test every pin
|
||||||
uint32_t pin_port = usb_connectors[connector]->pins[pin].port; // GPIO port corresponding to USB pin
|
const struct usb_pin_t* usb_pin = &usb_pins[usb_connectors[connector]->pins[pin]]; // get pin
|
||||||
uint16_t pin_pin = usb_connectors[connector]->pins[pin].pin; // GPIO pin corresponding to USB pin
|
gpio_set_mode(usb_pin->port, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, usb_pin->pin); // we will test if the input is floating by checking against a pull up and down
|
||||||
gpio_set_mode(pin_port, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, pin_pin); // we will test if the input is floating by checking against a pull up and down
|
gpio_set(usb_pin->port, usb_pin->pin); // pull up
|
||||||
gpio_set(pin_port, pin_pin); // pull up
|
|
||||||
sleep_us(10); // wait for GPIO/line to settle
|
sleep_us(10); // wait for GPIO/line to settle
|
||||||
bool high = (0 != gpio_get(pin_port, pin_pin)); // test if pin is high
|
bool high = (0 != gpio_get(usb_pin->port, usb_pin->pin)); // test if pin is high
|
||||||
gpio_clear(pin_port, pin_pin); // pull down
|
gpio_clear(usb_pin->port, usb_pin->pin); // pull down
|
||||||
sleep_us(10); // wait for GPIO/line to settle
|
sleep_us(10); // wait for GPIO/line to settle
|
||||||
bool low = (0 == gpio_get(pin_port, pin_pin)); // test if pin is low
|
bool low = (0 == gpio_get(usb_pin->port, usb_pin->pin)); // test if pin is low
|
||||||
gpio_set_mode(pin_port, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, pin_pin); // put back to floating
|
gpio_set_mode(usb_pin->port, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, usb_pin->pin); // put back to floating
|
||||||
if (high && low) { // pull up and down worked
|
if (high && low) { // pull up and down worked
|
||||||
} else { // pull up or down did not work
|
} else { // pull up or down did not work
|
||||||
printf("%s (%s) %s is not floating\n", usb_connectors[connector]->name, usb_connectors[connector]->host ? "host" : "device", usb_connectors[connector]->pins[pin].name); // print erroneous pin
|
printf("%s (%s) %s is not floating\n", usb_connectors[connector]->name, usb_connectors[connector]->host ? "host" : "device", usb_pin->name); // print erroneous pin
|
||||||
float_errors = true; // remember there is an error
|
float_errors = true; // remember there is an error
|
||||||
}
|
}
|
||||||
} // pin
|
} // pin
|
||||||
|
|
916
usb_cables.c
916
usb_cables.c
File diff suppressed because it is too large
Load Diff
10
usb_cables.h
10
usb_cables.h
|
@ -52,7 +52,7 @@ struct usb_connector_t {
|
||||||
const char* name; /*< connector name (A, B, mini-B, micro-B, C, ...) */
|
const char* name; /*< connector name (A, B, mini-B, micro-B, C, ...) */
|
||||||
bool host; /*< if on host or device side */
|
bool host; /*< if on host or device side */
|
||||||
uint8_t pins_nb; /*< number of pins */
|
uint8_t pins_nb; /*< number of pins */
|
||||||
const struct usb_pin_t* pins; /*< all connector pins */
|
const uint8_t* pins; /*< all connector pins indexes */
|
||||||
};
|
};
|
||||||
|
|
||||||
/** USB cable definition */
|
/** USB cable definition */
|
||||||
|
@ -61,13 +61,15 @@ struct usb_cable_t {
|
||||||
uint8_t connectors_nb; /*< number of connectors */
|
uint8_t connectors_nb; /*< number of connectors */
|
||||||
const struct usb_connector_t** connectors; /*< list of connectors this cables uses */
|
const struct usb_connector_t** connectors; /*< list of connectors this cables uses */
|
||||||
uint8_t pin_pairs_nb; /* number of connected pin pairs */
|
uint8_t pin_pairs_nb; /* number of connected pin pairs */
|
||||||
const struct usb_pin_t* (*pin_pairs)[2]; /*< list of connected pin pairs (order does not matter) */
|
const uint8_t (*pin_pairs)[2]; /*< list of connected pin index pairs (order does not matter) */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/** USB pin definitions */
|
||||||
|
extern const struct usb_pin_t usb_pins[105];
|
||||||
/** USB connectors definitions */
|
/** USB connectors definitions */
|
||||||
extern const struct usb_connector_t* usb_connectors[8];
|
extern const struct usb_connector_t* usb_connectors[8];
|
||||||
/** USB cables definitions */
|
/** USB cables definitions */
|
||||||
extern const struct usb_cable_t usb_cables[35];
|
extern const struct usb_cable_t usb_cables[37];
|
||||||
|
|
||||||
/** set every pin of connector to input floating
|
/** set every pin of connector to input floating
|
||||||
* @param[in] connector connector on which to set the pins floating
|
* @param[in] connector connector on which to set the pins floating
|
||||||
|
@ -120,4 +122,4 @@ bool usb_cables_check_load(const struct usb_connector_t* connector);
|
||||||
* @return if the connections of the cable correspond to the definition
|
* @return if the connections of the cable correspond to the definition
|
||||||
* @note each pair is checked in both directions
|
* @note each pair is checked in both directions
|
||||||
*/
|
*/
|
||||||
bool usb_cables_check_cable(const struct usb_cable_t* usb_cable, uint8_t* defined, uint8_t* undefined, bool print);
|
bool usb_cables_check_cable(const struct usb_cable_t* cable, uint8_t* defined, uint8_t* undefined, bool print);
|
||||||
|
|
Loading…
Reference in New Issue