usb_cables: use index for pins to save space

This commit is contained in:
King Kévin 2019-12-10 22:33:32 +01:00
parent 549f1c992f
commit ff23100dfa
3 changed files with 530 additions and 450 deletions

View File

@ -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
}
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
uint16_t pin_pin = usb_connectors[connector]->pins[pin].pin; // GPIO pin corresponding to USB pin
const struct usb_pin_t* usb_pin = &usb_pins[usb_connectors[connector]->pins[pin]]; // get pin
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
uint8_t pin_pos = __builtin_ctz(pin_pin); // get the pin number (position of the 1 in the 16-bit)
printf("%03u %s: ", pin_i, usb_pin->name); // print USB pin number
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 mode = (((pin_pos < 8) ? GPIO_CRL(pin_port) : GPIO_CRH(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 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(usb_pin->port) : GPIO_CRH(usb_pin->port)) >> (offset + 2)) & 0x3; // get configuration from pin for port
// show set value
if (0 == mode) { // pin configured as input
if (1 == conf) { // pin is in floating configuration
putc('x');
} else if (0 == (GPIO_ODR(pin_port) & pin_pin)) {
} else if (0 == (GPIO_ODR(usb_pin->port) & usb_pin->pin)) {
putc('l');
} else {
putc('h');
}
} 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');
} else {
putc('H');
}
}
// show actual value
if (gpio_get(pin_port, pin_pin)) {
if (gpio_get(usb_pin->port, usb_pin->pin)) {
putc('1');
} else {
putc('0');
@ -364,27 +363,27 @@ static void command_pin(void* argument)
putc('\n');
}
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]) {
case 'h':
gpio_set(pin_port, pin_pin);
gpio_set_mode(pin_port, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, pin_pin);
gpio_set(usb_pin->port, usb_pin->pin);
gpio_set_mode(usb_pin->port, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, usb_pin->pin);
break;
case 'H':
gpio_set(pin_port, pin_pin);
gpio_set_mode(pin_port, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, pin_pin);
gpio_set(usb_pin->port, usb_pin->pin);
gpio_set_mode(usb_pin->port, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, usb_pin->pin);
break;
case 'l':
gpio_clear(pin_port, pin_pin);
gpio_set_mode(pin_port, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, pin_pin);
gpio_clear(usb_pin->port, usb_pin->pin);
gpio_set_mode(usb_pin->port, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, usb_pin->pin);
break;
case 'L':
gpio_clear(pin_port, pin_pin);
gpio_set_mode(pin_port, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, pin_pin);
gpio_clear(usb_pin->port, usb_pin->pin);
gpio_set_mode(usb_pin->port, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, usb_pin->pin);
break;
case 'x':
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');
}
@ -415,19 +414,18 @@ static void command_test(void* argument)
float_errors = false; // restart test
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
uint32_t pin_port = usb_connectors[connector]->pins[pin].port; // GPIO port corresponding to USB pin
uint16_t pin_pin = usb_connectors[connector]->pins[pin].pin; // GPIO pin corresponding to USB pin
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(pin_port, pin_pin); // pull up
const struct usb_pin_t* usb_pin = &usb_pins[usb_connectors[connector]->pins[pin]]; // get 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(usb_pin->port, usb_pin->pin); // pull up
sleep_us(10); // wait for GPIO/line to settle
bool high = (0 != gpio_get(pin_port, pin_pin)); // test if pin is high
gpio_clear(pin_port, pin_pin); // pull down
bool high = (0 != gpio_get(usb_pin->port, usb_pin->pin)); // test if pin is high
gpio_clear(usb_pin->port, usb_pin->pin); // pull down
sleep_us(10); // wait for GPIO/line to settle
bool low = (0 == gpio_get(pin_port, 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
bool low = (0 == gpio_get(usb_pin->port, usb_pin->pin)); // test if pin is low
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
} 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
}
} // pin

File diff suppressed because it is too large Load Diff

View File

@ -52,7 +52,7 @@ struct usb_connector_t {
const char* name; /*< connector name (A, B, mini-B, micro-B, C, ...) */
bool host; /*< if on host or device side */
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 */
@ -61,13 +61,15 @@ struct usb_cable_t {
uint8_t connectors_nb; /*< number of connectors */
const struct usb_connector_t** connectors; /*< list of connectors this cables uses */
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 */
extern const struct usb_connector_t* usb_connectors[8];
/** 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
* @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
* @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);