-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from mndza/adv-edge-count
firmware.fpga_adv: fix `fpga_requesting_port()` glitch
- Loading branch information
Showing
1 changed file
with
18 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
*/ | ||
|
||
|
@@ -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 | ||
|
@@ -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); | ||
|
@@ -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) { | ||
|