Skip to content

Commit

Permalink
pbdrv/ioport: Add debug prints for LPF2 detection events.
Browse files Browse the repository at this point in the history
This makes it easier to see failed device detection events such as in pybricks/support#500.
  • Loading branch information
laurensvalk committed Nov 18, 2021
1 parent f57e76e commit aa5712f
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/pbio/drv/ioport/ioport_lpf2.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@

#if PBDRV_CONFIG_IOPORT_LPF2

#define DEBUG 0
#if DEBUG
#include <inttypes.h>
#define debug_pr(fmt, ...) printf((fmt), __VA_ARGS__)
#define DBG_ERR(expr) expr
#else
#define debug_pr(...)
#define DBG_ERR(expr)
#endif

#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
Expand Down Expand Up @@ -442,6 +452,7 @@ PROCESS_THREAD(pbdrv_ioport_lpf2_process, ev, data) {
ioport_dev_t *ioport = &ioport_devs[i];

if (ioport->iodev == (pbio_iodev_t *)data) {
debug_pr("uartdev (%c): Received stop.\n", i + PBDRV_CONFIG_FIRST_MOTOR_PORT);
ioport->connected_type_id = PBIO_IODEV_TYPE_ID_NONE;
}
}
Expand All @@ -456,14 +467,18 @@ PROCESS_THREAD(pbdrv_ioport_lpf2_process, ev, data) {
}

if (ioport->connected_type_id != ioport->prev_type_id) {
debug_pr("ioport(%c): Type changed from %d to %d.\n", i + PBDRV_CONFIG_FIRST_MOTOR_PORT, ioport->prev_type_id, ioport->connected_type_id);
ioport->prev_type_id = ioport->connected_type_id;
if (ioport->connected_type_id == PBIO_IODEV_TYPE_ID_LPF2_UNKNOWN_UART) {
debug_pr("ioport(%c): UART device detected.\n", i + PBDRV_CONFIG_FIRST_MOTOR_PORT);
ioport_enable_uart(ioport);
pbio_uartdev_get(i, &ioport->iodev);
ioport->iodev->port = i + PBDRV_CONFIG_IOPORT_LPF2_FIRST_PORT;
} else if (ioport->connected_type_id == PBIO_IODEV_TYPE_ID_NONE) {
debug_pr("ioport(%c): Device unplugged.\n", i + PBDRV_CONFIG_FIRST_MOTOR_PORT);
ioport->iodev = NULL;
} else {
debug_pr("ioport(%c): Passive device detected.\n", i + PBDRV_CONFIG_FIRST_MOTOR_PORT);
assert(ioport->connected_type_id < PBIO_IODEV_TYPE_ID_LPF2_UNKNOWN_UART);
ioport->iodev = &basic_devs[i];
ioport->iodev->info = &basic_infos[ioport->connected_type_id].info;
Expand Down

4 comments on commit aa5712f

@dlech
Copy link
Member

@dlech dlech commented on aa5712f Nov 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, using the Pybricks MicroPython implementation of printf can cause issues with reentrancy here since it can be block and will call the contiki event loop from within the contiki event loop.

@laurensvalk
Copy link
Member Author

@laurensvalk laurensvalk commented on aa5712f Nov 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was copied from uartdev, so we might want to fix it there too then.

It usually works well enough for occasional debugging, so perhaps good enough for now.

@dlech
Copy link
Member

@dlech dlech commented on aa5712f Nov 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The uartdev stuff was copied from the Linux kernel drivers, so I left it in.

When using debug prints like this, I go in an modify the Bluetooth stdout code so that it can't possibly block (drops output if buffer overflows instead of returning PBIO_ERROR_AGAIN) and increase the buffer size. This avoids the reentrany issues.

@dlech
Copy link
Member

@dlech dlech commented on aa5712f Nov 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is also a PYBRICKS_HUB_DEBUG option for sending stdout to a UART instead of Bluetooth. This is blocking, so can cause timing issue, but avoids the reentrancy issues.

Please sign in to comment.