Skip to content

Commit

Permalink
Add DAP and UART LED options. Use a debounce for UART LEDs.
Browse files Browse the repository at this point in the history
Signed-off-by: Jonathan Bell <[email protected]>
  • Loading branch information
P33M committed Feb 27, 2023
1 parent 156a33f commit a289056
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 8 deletions.
12 changes: 10 additions & 2 deletions include/DAP_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -493,14 +493,22 @@ It is recommended to provide the following LEDs for status indication:
- 1: Connect LED ON: debugger is connected to CMSIS-DAP Debug Unit.
- 0: Connect LED OFF: debugger is not connected to CMSIS-DAP Debug Unit.
*/
__STATIC_INLINE void LED_CONNECTED_OUT (uint32_t bit) {}
__STATIC_INLINE void LED_CONNECTED_OUT (uint32_t bit) {
#ifdef PICOPROBE_DAP_CONNECTED_LED
gpio_put(PICOPROBE_DAP_CONNECTED_LED, bit);
#endif
}

/** Debug Unit: Set status Target Running LED.
\param bit status of the Target Running LED.
- 1: Target Running LED ON: program execution in target started.
- 0: Target Running LED OFF: program execution in target stopped.
*/
__STATIC_INLINE void LED_RUNNING_OUT (uint32_t bit) {}
__STATIC_INLINE void LED_RUNNING_OUT (uint32_t bit) {
#ifdef PICOPROBE_DAP_RUNNING_LED
gpio_put(PICOPROBE_DAP_RUNNING_LED, bit);
#endif
}

///@}

Expand Down
41 changes: 38 additions & 3 deletions src/cdc_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ TickType_t last_wake, interval = 100;

static uint8_t tx_buf[CFG_TUD_CDC_TX_BUFSIZE];
static uint8_t rx_buf[CFG_TUD_CDC_RX_BUFSIZE];
// Actually s^-1 so 25ms
#define DEBOUNCE_MS 40
static uint debounce_ticks = 5;
static uint tx_led_debounce;
static uint rx_led_debounce;

void cdc_uart_init(void) {
gpio_set_function(PICOPROBE_UART_TX, GPIO_FUNC_UART);
Expand All @@ -61,21 +66,43 @@ void cdc_task(void)
/* Implicit overflow if we don't write all the bytes to the host.
* Also throw away bytes if we can't write... */
if (rx_len) {
#ifdef PICOPROBE_UART_RX_LED
gpio_put(PICOPROBE_UART_RX_LED, 1);
rx_led_debounce = debounce_ticks;
#endif
written = MIN(tud_cdc_write_available(), rx_len);
if (written > 0) {
tud_cdc_write(rx_buf, written);
tud_cdc_write_flush();
}
} else {
#ifdef PICOPROBE_UART_RX_LED
if (rx_led_debounce)
rx_led_debounce--;
else
gpio_put(PICOPROBE_UART_RX_LED, 0);
#endif
}

/* Reading from a firehose and writing to a FIFO. */
size_t watermark = MIN(tud_cdc_available(), sizeof(tx_buf));
if (watermark > 0) {
size_t tx_len;
#ifdef PICOPROBE_UART_TX_LED
gpio_put(PICOPROBE_UART_TX_LED, 1);
tx_led_debounce = debounce_ticks;
#endif
/* Batch up to half a FIFO of data - don't clog up on RX */
watermark = MIN(watermark, 16);
tx_len = tud_cdc_read(tx_buf, watermark);
uart_write_blocking(PICOPROBE_UART_INTERFACE, tx_buf, tx_len);
} else {
#ifdef PICOPROBE_UART_TX_LED
if (tx_led_debounce)
tx_led_debounce--;
else
gpio_put(PICOPROBE_UART_TX_LED, 0);
#endif
}
} else if (was_connected) {
tud_cdc_write_clear();
Expand All @@ -102,10 +129,10 @@ void tud_cdc_line_coding_cb(uint8_t itf, cdc_line_coding_t const* line_coding)
* fill up half a FIFO. Millis is too coarse for integer divide.
*/
uint32_t micros = (1000 * 1000 * 16 * 10) / MAX(line_coding->bit_rate, 1);

/* Modifying state, so park the thread before changing it. */
vTaskSuspend(uart_taskhandle);
interval = MAX(1, micros / ((1000 * 1000) / configTICK_RATE_HZ));
debounce_ticks = MAX(1, configTICK_RATE_HZ / (interval * DEBOUNCE_MS));
picoprobe_info("New baud rate %d micros %d interval %u\n",
line_coding->bit_rate, micros, interval);
uart_deinit(PICOPROBE_UART_INTERFACE);
Expand All @@ -119,8 +146,16 @@ void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts)
{
/* CDC drivers use linestate as a bodge to activate/deactivate the interface.
* Resume our UART polling on activate, stop on deactivate */
if (!dtr && !rts)
if (!dtr && !rts) {
vTaskSuspend(uart_taskhandle);
else
#ifdef PICOPROBE_UART_RX_LED
gpio_put(PICOPROBE_UART_RX_LED, 0);
rx_led_debounce = 0;
#endif
#ifdef PICOPROBE_UART_RX_LED
gpio_put(PICOPROBE_UART_TX_LED, 0);
tx_led_debounce = 0;
#endif
} else
vTaskResume(uart_taskhandle);
}
19 changes: 16 additions & 3 deletions src/led.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,27 @@ static uint32_t led_count;

void led_init(void) {
led_count = 0;

gpio_init(PICOPROBE_LED);
gpio_set_dir(PICOPROBE_LED, GPIO_OUT);
gpio_put(PICOPROBE_LED, 1);
#ifdef PICOPROBE_DAP_CONNECTED_LED
gpio_init(PICOPROBE_DAP_CONNECTED_LED);
gpio_set_dir(PICOPROBE_DAP_CONNECTED_LED, GPIO_OUT);
#endif
#ifdef PICOPROBE_DAP_RUNNING_LED
gpio_init(PICOPROBE_DAP_RUNNING_LED);
gpio_set_dir(PICOPROBE_DAP_RUNNING_LED, GPIO_OUT);
#endif
#ifdef PICOPROBE_UART_RX_LED
gpio_init(PICOPROBE_UART_RX_LED);
gpio_set_dir(PICOPROBE_UART_RX_LED, GPIO_OUT);
#endif
#ifdef PICOPROBE_UART_TX_LED
gpio_init(PICOPROBE_UART_TX_LED);
gpio_set_dir(PICOPROBE_UART_TX_LED, GPIO_OUT);
#endif
}



void led_task(void) {
if (led_count != 0) {
--led_count;
Expand Down

0 comments on commit a289056

Please sign in to comment.