313 Commits

Author SHA1 Message Date
Martin Ling
fd073e391f Add USB vendor request to read M0 state, and host support for doing so.
This adds a `hackrf_debug [-S|--state]` option, and the necessary
plumbing to libhackrf and the M4 firmware to support it.

The USB API and libhackrf versions are bumped to reflect the changes.
2022-02-13 16:46:12 +00:00
Martin Ling
838ad0726c Flush bulk endpoints from USB ISR on mode change request.
The previous change moved this flush from the vendor request handler to
the transceiver_shutdown() function which runs outside ISR context.

However, without this flush in the ISR, the firmware can sometimes end up
stuck in TX or RX mode after a request to go IDLE. It's still not clear
how this happens, but keeping the flush in the USB ISR fixes it, and as
soon as a mode change is requested we know we are going to be flushing
anyway, so there is no harm to do so here.

This commit does not remove the flush in transceiver_shutdown(), because
it is possible that the ISR will return just before the transceiver loop
queues a new transfer. Rather than try to avoid that race, we can just
flush again later.
2022-02-13 16:11:31 +00:00
Martin Ling
361c4ac54f Move transceiver mode changes out of USB ISR.
This is a defensive change to make the transceiver code easier to reason
about, and to avoid the possibility of races such as that seen in #1042.

Previously, set_transceiver_mode() was called in the vendor request
handler for the SET_TRANSCEIVER_MODE request, as well in the callback
for a USB configuration change. Both these calls are made from the USB0
ISR, so could interrupt the rx_mode(), tx_mode() and sweep_mode()
functions at any point. It was hard to tell if this was safe.

Instead, set_transceiver_mode() has been removed, and its work is split
into three parts:

- request_transceiver_mode(), which is safe to call from ISR context.
  All this function does is update the requested mode and increment a
  sequence number. This builds on work already done in PR #1029, but
  the interface has been simplified to use a shared volatile structure.

- transceiver_startup(), which transitions the transceiver from an idle
  state to the configuration required for a specific mode, including
  setting up the RF path, configuring the M0, adjusting LEDs and UI etc.

- transceiver_shutdown(), which transitions the transceiver back to an
  idle state.

The *_mode() functions that implement the transceiver modes now call
transceiver_startup() before starting work, and transceiver_shutdown()
before returning, and all this happens in the main thread of execution.

As such, it is now guaranteed that all the steps involved happen in a
consistent order, with the transceiver starting from an idle state, and
being returned to an idle state before control returns to the main loop.

For consistency of interface, an off_mode() function has been added to
implement the behaviour of the OFF transceiver mode. Since the
transceiver is already guaranteed to be in an idle state when this is
called, the only work required is to set the UI mode and wait for a new
mode request.
2022-02-08 13:45:41 +00:00
Michael Ossmann
2c64f05ec9 Merge pull request #1034 from schneider42/rad1o-ui-cleanup
rad1o UI support
2022-02-07 23:25:01 -07:00
Martin Ling
7057235a14 Increment a sequence number when transceiver mode changes.
This fixes bug #1042, which occured when an RX->OFF->RX sequence
happened quickly enough that the loop in rx_mode() did not see the
change. As a result, the enable_baseband_streaming() call at the start
of that function was not repeated for the new RX operation, so RX
progress stalled.

To solve this, the vendor request handler now increments a sequence
number when it changes the transceiver mode. Instead of the RX loop
checking whether the transceiver mode is still RX, it now checks whether
the current sequence number is the same as when it was started. If not,
there must have been at least one mode change, so the loop exits, and
the main loop starts the necessary loop for the new mode. The same
behaviour is implemented for the TX and sweep loops.

For this approach to be reliable, we must ensure that when deciding
which mode and sequence number to use, we take both values from the same
set_transceiver_mode request.

To achieve this, we briefly disable the USB0 interrupt to stop the
vendor request handler from running whilst reading the mode and sequence
number together. Then the loop dispatch proceeds using those pre-read
values.
2022-02-03 07:36:34 +00:00
Mike Walters
8660e44575 Merge pull request #1018 from greatscottgadgets/oc-bugs
Fix Opera Cake bugs
2022-01-12 17:37:58 +00:00
schneider
7bf55dc983 rad1o: Don't update the UI during sweeps
Updating the UI during sweeps significantly increases the time needed to
complete a sweep. Instead simply show "SWEEP" on the display.
2022-01-10 23:26:19 +01:00
schneider
9404226c6a rad1o: UI code cleanup 2022-01-10 23:26:19 +01:00
schneider
0c514839ca hack: add rad1o ui code 2022-01-09 23:27:00 +01:00
Martin Ling
98df8c23be Fix a typo. 2022-01-03 18:48:04 +00:00
Martin Ling
42a7c5ede9 Add a label at the end of the code to indicate the literal pool.
This makes objdump disassembly of the code a bit clearer, by separating
the constants from code following the last label.
2022-01-03 18:48:04 +00:00
Martin Ling
59be1fef5a Add pseudocode for all instructions.
This is intended to make the code possible to follow without knowledge
of the ARM instruction set.
2022-01-03 18:48:04 +00:00
Martin Ling
030898315d Remove unused constants.
Neither of these constants was used in the code.
2022-01-03 18:48:04 +00:00
Martin Ling
14065bb69d Initialise M0 state at startup.
This is not currently essential, since the current M4 code will not
trigger an SGPIO interrupt until the offset and tx fields are set.

In future though, we want to explicitly set up the M0 state here.
2022-01-03 18:48:04 +00:00
Martin Ling
5df28efb3f Assign names to registers used for temporary purposes.
This is just to improve readability; there is no change to the code.
2022-01-03 18:48:04 +00:00
Martin Ling
e531fb507b Use faster way to calculate buffer pointer.
One of the few instructions that can use the high registers (r8-r14) is
the add instruction, which can add any two registers, as long as one of
them is also used as the destination register.

By using this form of add , we can add buf_base (in a high register) to
the offset within the buffer (in a low register), to get the desired
pointer value (buf_ptr) which we want to access.

This saves one cycle by eliminating the need to move buf_base to a low
register first.
2022-01-03 18:48:04 +00:00
Martin Ling
c1c665d5b8 Stash which interrupt bits were set, and use them to clear.
The lsr instruction here shifts the value in r0 right by one bit,
putting the LSB into the carry flag.

By setting the destination register to r1, we can retain the original
unshifted value in r0, and later write this to the INT_CLEAR register in
order to clear all bits that were set.

This saves two cycles by avoiding the need to load an 0xFFFF value to
write to INT_CLEAR.
2022-01-03 18:48:04 +00:00
Martin Ling
bd7d0b9194 Correct a misleading comment.
The effect of the lsr instruction here is to shift the LSB of r0 into
the processor's carry flag. The subsequent bcc instruction ("branch if
carry clear") will then branch if this bit was zero.

The LSB corresponds to the exchange interrupt flag for slice A only. The
other interrupt flag bits are not checked here, contrary to the comment.
2022-01-03 18:48:04 +00:00
Martin Ling
f8ea1e8e56 Use stack pointer to hold base address of state structure.
Keeping the base address of this structure in a register allows us to
use offsets to load individual fields from it, without needing their
individual addresses.

However, the ldr instruction can only use immediate offsets relative to
the low registers (r0-r7), or the stack pointer (r13).

Low registers are in short supply and are needed for other instructions
which can only use r0-r7, so we use the stack pointer here.

It's safe to do this because we do not use the stack. There are no
function calls, interrupt handlers or push/pop instructions in the M0
code.

This change saves four cycles by eliminating loads of the addresses for
the offset & tx registers, plus a further two by eliminating the need to
stash one of these addresses in r8.
2022-01-03 18:48:04 +00:00
Martin Ling
2f26ebffd4 Keep buffer base & size mask in high registers.
The high registers (r8-r14) cannot be used directly by most of the
instructions in the Cortex-M0 instruction set.

One of the few instructions that can use them is mov, which can use
any pair of registers.

This allows saving two cycles, by replacing two loads (2 cycles each)
with moves (1 cycle each), after stashing the required values in high
registers at startup.
2022-01-03 18:48:04 +00:00
Martin Ling
8f43dc1be5 Use a register to hold base address of SGPIO interrupt registers.
This allows us to use ldr/str with an immediate offset to access the
SGPIO interrupt registers, rather than first having to load a register
with the specific address we want to access.

This change saves a total of 6 cycles, by eliminating two loads (2
cycles each), one of which could be executed twice.
2022-01-03 18:48:04 +00:00
Martin Ling
9206a8b752 Free up two registers by accessing SGPIO in two 16-byte chunks.
The current code does reads and writes in two chunks: one of
6 words, followed by one of 2.

Instead, use two chunks of 4 words each. This takes the same number of
total cycles, but frees up two registers for other uses.

Note that we can't do things in one chunk, because we'd need eight
registers to hold the data, plus a ninth to hold the buffer pointer. The
ldm/stm instructions can only use the eight low registers, r0-r7.

So we have to use two chunks, and the most register-efficient way to do
that is to use two equal chunks.
2022-01-03 18:48:04 +00:00
Martin Ling
c6362381d1 Initialise register with a constant value before SGIO loop.
Previously this register was reloaded with the same value during each loop.
Initialising it once, outside the loop, saves two cycles.

Note the separation of the loop start ("loop") from the entry point ("main").
Code between these labels will be run once, at startup.
2022-01-03 18:48:04 +00:00
Martin Ling
f61a03dead Assign names to registers which are used for a single purpose.
This is just to improve readability; there is no change to the code.
2022-01-03 18:48:04 +00:00
Martin Ling
dc0f8f48c5 Use defines for offsets into SGPIO shadow registers.
This is just to make the SGPIO code less cryptic, and to place
the explanation of the offsets closer to where they are defined.
2022-01-03 18:48:04 +00:00
Martin Ling
5b2a390728 Move M0 offset and tx variables into a state struct.
These variables are already placed together; this commit just groups
them into a struct and declares this in a new header.

This commit should not result in any change to the firmware binary.
Only the names of symbols are changed.
2022-01-03 18:47:58 +00:00
Martin Ling
39c6f3385e Remove usb_bulk_buffer.c containing unused variable definition.
This removes the definition of the offset variable,

volatile uint32_t usb_bulk_buffer_offset = 0;

which is actually superfluous. This variable, along with its neighbour
usb_bulk_buffer_tx, is placed explicitly by the linker script. Its type
is defined by the declaration in usb_bulk_buffer.h.

There is no need to define it here, and doing so gives the misleading
impression that its initial value can be changed by modifying this line!

The initialization to zero never actually takes effect, because the
variable is not placed in the .data or .bss sections which are
initialised by the startup code.

The offset and tx variables are both set in set_transceiver_mode
before SGPIO streaming is started, so the M0 code does not use
them uninitialised.
2022-01-03 18:47:33 +00:00
Martin Ling
3d9802260e Document purpose and timing of existing M0 code.
This commit does not modify the code; it only updates comments.
2022-01-03 18:47:24 +00:00
Michael Ossmann
d0c0270b9c clear frequency ranges before adding new ones 2021-12-12 13:25:44 -07:00
Mike Walters
e41314f130 operacake: add API function to set port dwell times 2021-10-14 14:41:52 +01:00
Schuyler St. Leger
9ee25ab48a operacake: add support for port switching using SCTimer
Based on Schuyler St. Leger's operacake-sctimer branch
2021-10-14 14:36:18 +01:00
Mike Walters
790b5d35cf operacake: add get/set switching mode functions 2021-10-14 14:36:18 +01:00
Mike Walters
7fd3db9b2b operacake: create struct for operacake state 2021-10-14 14:36:18 +01:00
Mike Walters
7f21c93c33 Copy M0 image directly from ROM, instead of shadow region.
This changes m0_rom_to_ram to read directly from the ROM region, rather
than copying from where .text has been copied to RAM.

Previously the second .text section would be merged with the first one,
so the M0 image would be copied to RAM during `pre_main`. However, on
newer linkers the sections are not merged together so the second .text
section did not get copied to RAM.

fixes #936
2021-10-06 18:40:34 +01:00
Mike Walters
0293cf23db Opera Cake: use 0-7 instead of I2C addresses & bump USB API version 2021-07-14 18:35:31 +01:00
Mike Walters
1cb2abf747 Disable USB interrupts during sweep set_freq call
Previously, a USB vendor request could interrupt in the middle of
set_freq and make conflicting changes, leaving the HackRF in an
undefined state.

fixes #772
2021-03-17 13:03:06 +00:00
Mike Walters
54bdb86a89 Fix CPLD update 2020-02-12 18:53:59 +00:00
Michael Ossmann
7d900268de fix compiler warnings 2020-02-11 16:59:59 +00:00
Michael Ossmann
c0aed2edb7 firmware: clean up transceiver modes 2020-02-11 16:59:59 +00:00
Mike Walters
d8250c6396 Don't re-init bulk endpoints on every set_transceiver_mode call
Previously the firmware would re-initialise the bulk endpoints on
every transceiver mode change including a USB data toggle reset,
which could cause the first bulk packet (512-bytes) to be dropped
by the host if the PID no longer matched.
2020-02-10 15:46:55 +00:00
Mike Walters
4aac303480 Add option to disable HackRF UI
Fixes #608
2020-01-22 21:23:30 +00:00
Mike Walters
c6e1a5f7f7 Fix Operacake GPIO mode initialisation 2020-01-22 14:21:03 +00:00
Mike Walters
05b1650a6a Rename hackrf-ui.[ch]
Every other source file uses underscores! :(
2020-01-22 14:20:23 +00:00
Mike Walters
e76eace09d Use the M0 to collect SGPIO samples 2020-01-20 14:22:30 +00:00
Mike Walters
3e2ca4e6c3 Bring up the M0 & blink TX LED 2020-01-20 14:22:30 +00:00
Michael Ossmann
208fae7538 Merge pull request #645 from jboone/master
Sync up PortaPack UI changes.
2020-01-06 16:31:28 -07:00
Michael Ossmann
1569737109 Merge pull request #607 from dominicgs/portapack_ui_opera_cake_coexistence
Allow portapack and opera cake to coexist and still enable the PP UI
2020-01-06 16:28:58 -07:00
Jared Boone
46fd11af5b Si5351C: Extract best block source function into hackrf_core.
It's not an Si5351C driver thing, but a HackRF thing. Also added a driver function to check if CLKIN signal is valid, and made use of it, instead of opaque register read code.
2019-03-20 11:16:44 -07:00
Jared Boone
b4d8ee678e PortaPack: Lots of clean-up
Perform PortaPack initialization separately from and earlier than UI initialization. Track if PortaPack was detected, provide (mostly useless) pointer if so. Put "weak" declarations into respective headers. Moving #includes around.
2019-03-20 10:46:46 -07:00
Jared Boone
12bb516cc0 PortaPack: Extract non-UI code into separate portapack.[ch] module.
I'll be adding some non-UI API functions to the top-level PortaPack structure.
2019-03-18 15:09:11 -07:00