Skip to content

Commit

Permalink
Merge pull request #68 from mndza/adv-edge-count
Browse files Browse the repository at this point in the history
firmware.fpga_adv: fix `fpga_requesting_port()` glitch
  • Loading branch information
mossmann committed May 29, 2024
2 parents 2609935 + 3e44c13 commit 675a817
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions firmware/src/boards/cynthion_d11/fpga_adv.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* This file is part of Apollo.
*
* Copyright (c) 2023 Great Scott Gadgets <[email protected]>
* Copyright (c) 2024 Great Scott Gadgets <[email protected]>
* SPDX-License-Identifier: BSD-3-Clause
*/

Expand All @@ -23,10 +23,16 @@
// Switching the shared USB port to the FPGA is allowed.
static bool fpga_usb_allowed = false;

// Store the timestamp of the last physical port advertisement
// Duration of the time window (in milliseconds).
#define WINDOW_PERIOD_MS 200UL

// Store the timestamp of the last time window update.
static uint32_t last_update = 0;
static uint32_t window_edges = 3; // avoid glitch during startup

// Counter of edges detected within the last time window.
static uint32_t window_edges = 0;

// Counter of edges detected since the last time window update.
static volatile uint32_t edge_counter = 0;

#endif
Expand Down Expand Up @@ -54,9 +60,8 @@ void fpga_adv_init(void)
while (EIC->STATUS.bit.SYNCBUSY);

// Configure EIC to trigger on rising edge.
uint8_t const sense_shift = 7 * 4;
EIC->CONFIG[0].reg &= ~(7 << sense_shift);
EIC->CONFIG[0].reg |= 1 << sense_shift;
EIC->CONFIG[0].reg &= ~EIC_CONFIG_SENSE7_Msk;
EIC->CONFIG[0].reg |= EIC_CONFIG_SENSE7_RISE;

// Enable External Interrupt.
EIC->INTENSET.reg = EIC_INTENSET_EXTINT(1 << 7);
Expand All @@ -76,14 +81,15 @@ void fpga_adv_init(void)
void fpga_adv_task(void)
{
#ifdef BOARD_HAS_USB_SWITCH
// Wait for the defined time window.
if (board_millis() - last_update < WINDOW_PERIOD_MS) return;

// Update edge counts inside time window.
if (board_millis() - last_update >= WINDOW_PERIOD_MS) {
window_edges = edge_counter;
edge_counter = 0;
last_update = board_millis();
}
window_edges = edge_counter;
edge_counter = 0;
last_update = board_millis();

// Take over USB after timeout
// Take over USB if the FPGA is not requesting the port.
if (fpga_requesting_port() == false) {
take_over_usb();
} else if (fpga_usb_allowed) {
Expand Down

0 comments on commit 675a817

Please sign in to comment.