Files
mayhem-firmware/firmware/application/irq_controls.cpp
T
stafur 39424632bb HackRF Pro (praline) arch-port initial PR (#2958)
* Initial commit and pr for HackRF Pro (praline) arch-port to mayhem-firmware. Please see https://github.com/portapack-mayhem/mayhem-firmware/issues/2957. Added flash specifics for -DBOARD=PRALINE. This firmware only builds with toolchain v9.2.1 if hackrf codebase has -B arm in firmware/hackrf_usb/CMakeLists.txt.

* Updated CMakeLists.txt per coordination with @HtoToo. For -DBOARD=PRALINE FLASH_MB_SIZE and FLASH_MB_LIMIT_SIZE are now 4. Removed praline specific variable for FLASH limits.

* Updated chibios-portapack's board.cpp to support initialization of the HachRF-Pro (praline) FPGA. Added append_fpga_bitstream.py tool to ensure that praline_fgpa.bin bitstream can be appended to -DBOARD=PRALINE produced firmware. In order to ensure successful execution of append_fpga_bitstream.py to append the fpga bitstream we should expect that the bistsream will be located at 0x180000 in flash. This requires that FLASH_MB_LIMIT_SIZE must be 1.5, and FLASH_BYTES_LIMIT_SIZE must be 1535 * 1024. If we want to allow more or less space for the base firmware image sans the fpga bitstream the location of the bistream must be moved to a location other than 0x180000.

* Updated location of praline_fpga.bin bitstream to 0x380000 to allow more room for firmware. Firmware now has 3.5MB, or 2MB more available than before as coordinated with @HTotoo.

* Expanded #ifndef PRALINE to include og and r9 gpio and pin setup as coordinated with @HTotoo.

* Added note for PRALINE FLASH_MB_LIMIT_SIZE and FLASH_BYTES_LIMIT_SIZE to explain why we are using the 3.5 and 3584 values respectively as coordinated with @HTotoo.

* Next round of modifications derived heavily, if not entirely  from work done by @banandana at https://github.com/Banandana/mayhem-firmware. This commit should power on the HackRF Pro (praline) display, power on the fpga, and enable gpio, and provide debug utilties. There is still a lot of work to be done to fully enable the new praline board with this build and firmware architectural porting effort. However, hackrf-one boards do not seem to be adversely impacted by the #ifdef PRALINE statements, and CMakeLists updates, as far as I have been able to test.

* Ran format-code.sh. Updates for this commit are only due to formatting. Tested builds and they seem to work as exptected.

* Addressed fixes in firmware/application and firmware/baseband. Stream now flows to capture and looking glass. Issues were related to thread management. Issues were originally addressed by @banandana.

* Ran format-code.sh to allow for consistency with autoamted clang checks.

* Update hackrf ref repo to mayhem-portapack-hackrf next from https://github.com/portapack-mayhem/hackrf

* Addressed format edits necessary to pass clang-format check.

* Starting addressing Si5351 Clocks for radio sampling. These updates correctly set the Si5351 clock at start up. There appears to be an issue during runtime when testing with RX Test Init, Capture and Looking glass.

* Updated clock_manager.cpp to restore correct function introduced by @banandana when testing with Rx Test Init.

* Switched to using decimation for setting the sample rate without changing the Si5351 clock. This assumes that for the praline board Si5351 CLK0 runs at fixed 8 MHz (constant) and the FPGA decimates to get the desired sample rate. For example, for a 1 MHz sample rate -> Si5351 outputs 8 MHz, FPGA decimates by 8. There is still more work needed here, and potential verification that this is the correct way to operate with this new archteitecture.

* After deliberating on hackrf_usb hackrf_core.c and radio.c, and reviewing firmware/application/hw/si5351.cpp the original approach of using the aproach detailed in hackrf_core.c sample_rate_frac_set() lines 580-582, via the implementation in firmware/application/hw/si5351.cpp seems like the best place to continue testing efforts.

* Tested at ~2.4GHz (2.3 - 2.5) with lookgin glass and was able to receive signals. Added a Signal Path debug app to test gains, and readio mode (receive/transmit).

* Added two debug apps for the RFFC507x. Status View and Tuning View. This helped debug some of the potential issues with tuning.

* update submodule

* format code

* Small touch up merging latest next and ensuring build for HackRF One.

* Reverted edits to re: firmware/baseband/sd_over_usb/scsi.c and firmware/application/portapack.cpp. Source now builds, had to pull latest hackrf submodule.

* Skipped detect hardware for praline board to avoid backscreen in HackRF Pro praline board.

---------

Co-authored-by: gullradriel <gullradriel@users.noreply.github.com>
2026-02-11 09:15:11 +01:00

323 lines
9.7 KiB
C++

/*
* Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc.
* Copyright (C) 2024 Mark Thompson
*
* This file is part of PortaPack.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#include "irq_controls.hpp"
#include "ch.h"
#include "debounce.hpp"
#include "encoder.hpp"
#include "event_m0.hpp"
#include "hal.h"
#include "touch.hpp"
#include "touch_adc.hpp"
#include "utility.hpp"
#include <cstdint>
#include <array>
#include "portapack_io.hpp"
#include "hackrf_hal.hpp"
using namespace hackrf::one;
using namespace portapack;
static Thread* thread_controls_event = NULL;
// Index with the Switch enum.
static std::array<Debounce, 6> switch_debounce;
static EncoderDebounce encoder_debounce;
static_assert(std::size(switch_debounce) == toUType(Switch::Dfu) + 1);
static Encoder encoder;
static volatile uint32_t encoder_position{0};
static volatile uint32_t touch_phase{0};
/* TODO: Change how touch scanning works. It produces a decent amount of noise
* when changing potential on the resistive touch pad. Among other things, I
* see blips of noise when sampling the MAX2837 RSSI signal. And when the
* radio is off (RSSI signal is not driven?), the signal floats a LOT when the
* touch panel potentials are changing.
*
* Ideally, scan only for pressure until a touch is detected. Then scan X/Y.
* Noise will only occur when the panel is being touched. Not ideal, but
* an acceptable improvement.
*/
static std::array<IO::TouchPinsConfig, 3> touch_pins_configs{
/* State machine will pause here until touch is detected. */
IO::TouchPinsConfig::SensePressure,
IO::TouchPinsConfig::SenseX,
IO::TouchPinsConfig::SenseY,
};
static touch::Frame temp_frame;
static touch::Frame touch_frame;
static uint32_t touch_debounce = 0;
static uint32_t touch_debounce_mask = (1U << 4) - 1;
static bool touch_detected = false;
static bool touch_cycle = false;
static bool touch_update() {
const auto samples = touch::adc::get();
const auto current_phase = touch_pins_configs[touch_phase];
switch (current_phase) {
case IO::TouchPinsConfig::SensePressure: {
const auto z1 = samples.xp - samples.xn;
const auto z2 = samples.yp - samples.yn;
const auto touch_raw = (z1 > portapack::touch_threshold) || (z2 > portapack::touch_threshold);
touch_debounce = (touch_debounce << 1) | (touch_raw ? 1U : 0U);
touch_detected = ((touch_debounce & touch_debounce_mask) == touch_debounce_mask);
if (!touch_detected && !touch_cycle) {
temp_frame.pressure = {};
return false;
} else {
temp_frame.pressure += samples;
}
} break;
case IO::TouchPinsConfig::SenseX:
temp_frame.x += samples;
break;
case IO::TouchPinsConfig::SenseY:
temp_frame.y += samples;
break;
default:
break;
}
touch_phase++;
if (touch_phase >= touch_pins_configs.size()) {
/* New iteration, calculate values and flag touch event */
touch_phase = 0;
temp_frame.touch = touch_detected;
touch_cycle = touch_detected;
touch_frame = temp_frame;
temp_frame = {};
return true;
} else {
return false;
}
}
static uint8_t switches_raw = 0;
static uint8_t injected_switch = 0;
static uint8_t injected_encoder = 0;
/* The raw data is not packed in a way that makes looping over it easy.
* One option would be an accessor helper (RawSwitch). Another option
* is to swizzle the bits into a friendlier order. */
// /* Type to access the bits in the raw switch data. */
// struct RawSwitch {
// const uint8_t raw_{0};
// uint8_t right() const { return (raw_ >> 0) & 1; }
// uint8_t left() const { return (raw_ >> 1) & 1; }
// uint8_t down() const { return (raw_ >> 2) & 1; }
// uint8_t up() const { return (raw_ >> 3) & 1; }
// uint8_t select() const { return (raw_ >> 4) & 1; }
// uint8_t rot_a() const { return (raw_ >> 5) & 1; }
// uint8_t rot_b() const { return (raw_ >> 6) & 1; }
// uint8_t dfu() const { return (raw_ >> 7) & 1; }};
uint8_t swizzled_switches() {
uint8_t raw = io.io_update(touch_pins_configs[touch_phase]);
return (raw & 0x1F) | // Keep the bottom 5 bits the same.
((raw >> 2) & 0x20) | // Shift the DFU bit down to bit 6.
((raw << 1) & 0xC0); // Shift the encoder bits up to be 7 & 8.
}
static bool switches_update(const uint8_t raw) {
// TODO: Only fire event on press, not release?
bool switch_changed = false;
for (size_t i = 0; i < switch_debounce.size(); ++i) {
uint8_t bit = (raw >> i) & 0x01;
switch_changed |= switch_debounce[i].feed(bit);
}
return switch_changed;
}
static bool encoder_update(const uint8_t raw) {
return encoder_debounce.feed(raw >> 6);
}
static bool encoder_read() {
auto delta = encoder.update(encoder_debounce.state()) * encoder_debounce.rotation_rate();
if (injected_encoder > 0) {
if (injected_encoder == 1) delta = -1;
if (injected_encoder == 2) delta = 1;
injected_encoder = 0;
}
if (delta != 0) {
encoder_position += delta;
return true;
} else {
return false;
}
}
void timer0_callback(GPTDriver* const) {
eventmask_t event_mask = 0;
if (touch_update()) event_mask |= EVT_MASK_TOUCH;
switches_raw = swizzled_switches();
if (switches_update(switches_raw) || (injected_switch > 0))
event_mask |= EVT_MASK_SWITCHES;
if (encoder_update(switches_raw) || encoder_read())
event_mask |= EVT_MASK_ENCODER;
/* Signal event loop */
#ifdef PRALINE
if (event_mask && thread_controls_event) {
#else
if (event_mask) {
#endif
chSysLockFromIsr();
chEvtSignalI(thread_controls_event, event_mask);
chSysUnlockFromIsr();
}
}
/* TODO: Refactor some/all of this to appropriate shared headers? */
static constexpr uint32_t timer0_count_f = 1000000;
static constexpr uint32_t timer0_prescaler_ratio = (base_m0_clk_f / timer0_count_f);
static constexpr uint32_t ui_interrupt_rate = 1000;
static constexpr uint32_t timer0_match_count = timer0_count_f / ui_interrupt_rate;
/* GPT driver refers to configuration structure during runtime, so make sure
* it sticks around.
*/
static GPTConfig timer0_config{
.callback = timer0_callback,
.pr = timer0_prescaler_ratio - 1,
};
void controls_init() {
thread_controls_event = chThdSelf();
touch::adc::start();
/* GPT timer 0 is used to scan user interface controls -- touch screen,
* navigation switches.
*/
gptStart(&GPTD1, &timer0_config);
gptStartContinuous(&GPTD1, timer0_match_count);
#ifdef PRALINE
// Enable repeat for directional switches only
for (auto i = Switch::Right; i <= Switch::Up; incr(i))
#else
// Enable repeat for directional and Select switches only
for (auto i = Switch::Right; i <= Switch::Sel; incr(i))
#endif
switch_debounce[toUType(i)].enable_repeat();
}
// Note: Called by event handler or apps, not in ISR, so some presses might be missed during high CPU utilization
SwitchesState get_switches_state() {
SwitchesState result;
// TODO: Ignore multiple keys at the same time?
for (size_t i = 0; i < result.size(); i++)
result[i] = switch_debounce[i].state();
if (injected_switch > 0 && injected_switch <= 6) {
result[injected_switch - 1] = 1;
injected_switch = 0xff;
} else if (injected_switch == 0xff) {
injected_switch = 0x00;
}
return result;
}
/* Gets the repeat enabled state for all the switches. */
SwitchesState get_switches_repeat_config() {
SwitchesState result;
for (size_t i = 0; i < result.size(); i++)
result[i] = switch_debounce[i].get_repeat_enabled();
return result;
}
/* Configures which switches support repeat.*/
void set_switches_repeat_config(SwitchesState switch_config) {
for (size_t i = 0; i < switch_config.size(); i++)
switch_debounce[i].set_enable_repeat(switch_config[i]);
}
/* Gets the long press enabled state for all the switches. */
SwitchesState get_switches_long_press_config() {
SwitchesState result;
for (size_t i = 0; i < result.size(); i++)
result[i] = switch_debounce[i].get_long_press_enabled();
return result;
}
/* Configures which switches support long press.
* NB: those switches will not support Repeat function. */
void set_switches_long_press_config(SwitchesState switch_config) {
for (size_t i = 0; i < switch_config.size(); i++)
switch_debounce[i].set_long_press_enabled(switch_config[i]);
}
bool switch_is_long_pressed(Switch s) {
return switch_debounce[toUType(s)].long_press_occurred();
}
EncoderPosition get_encoder_position() {
return encoder_position;
}
touch::Frame get_touch_frame() {
return touch_frame;
}
namespace control {
namespace debug {
uint8_t switches() {
return switches_raw;
}
void inject_switch(uint8_t button) {
if (button <= 6)
injected_switch = button;
else if (button > 6)
injected_encoder = button - 6;
}
} // namespace debug
} // namespace control