mirror of
https://github.com/portapack-mayhem/mayhem-firmware.git
synced 2026-08-12 19:03:40 +00:00
39424632bb
* 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>
698 lines
19 KiB
C++
698 lines
19 KiB
C++
/*
|
|
* Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc.
|
|
*
|
|
* 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 "portapack.hpp"
|
|
#include "portapack_hal.hpp"
|
|
#include "portapack_dma.hpp"
|
|
#include "portapack_cpld_data.hpp"
|
|
#include "portapack_persistent_memory.hpp"
|
|
|
|
#include "hackrf_hal.hpp"
|
|
#include "hackrf_gpio.hpp"
|
|
using namespace hackrf::one;
|
|
#include "jtag_target_gpio.hpp"
|
|
|
|
#include "clock_manager.hpp"
|
|
#include "event_m0.hpp"
|
|
|
|
#include "backlight.hpp"
|
|
#include "touch_adc.hpp"
|
|
#include "audio.hpp"
|
|
|
|
#include "wm8731.hpp"
|
|
using wolfson::wm8731::WM8731;
|
|
|
|
#include "ak4951.hpp"
|
|
using asahi_kasei::ak4951::AK4951;
|
|
|
|
#include "cpld_update.hpp"
|
|
|
|
#include "optional.hpp"
|
|
#include "irq_controls.hpp"
|
|
|
|
#include "file.hpp"
|
|
#include "sd_card.hpp"
|
|
#include "string_format.hpp"
|
|
#include "bitmap.hpp"
|
|
#include "ui_widget.hpp"
|
|
#include "i2cdevmanager.hpp"
|
|
#include "battery.hpp"
|
|
|
|
#ifndef PRALINE
|
|
extern "C" {
|
|
#include "platform_detect.h"
|
|
}
|
|
#endif
|
|
|
|
namespace portapack {
|
|
|
|
const char* init_error = nullptr;
|
|
|
|
portapack::IO io{
|
|
portapack::gpio_dir,
|
|
portapack::gpio_lcd_rdx,
|
|
portapack::gpio_lcd_wrx,
|
|
portapack::gpio_io_stbx,
|
|
portapack::gpio_addr,
|
|
portapack::gpio_lcd_te,
|
|
portapack::gpio_dfu,
|
|
};
|
|
|
|
portapack::BacklightCAT4004 backlight_cat4004;
|
|
portapack::BacklightOnOff backlight_on_off;
|
|
|
|
lcd::ILI9341 display;
|
|
|
|
I2C i2c0(&I2CD0);
|
|
SPI ssp1(&SPID2);
|
|
portapack::USBSerial usb_serial;
|
|
|
|
si5351::Si5351 clock_generator{
|
|
i2c0, hackrf::one::si5351_i2c_address};
|
|
|
|
ClockManager clock_manager{
|
|
i2c0, clock_generator};
|
|
|
|
WM8731 audio_codec_wm8731{i2c0, 0x1a};
|
|
AK4951 audio_codec_ak4951{i2c0, 0x12};
|
|
|
|
ReceiverModel receiver_model;
|
|
TransmitterModel transmitter_model;
|
|
|
|
TemperatureLogger temperature_logger;
|
|
|
|
bool antenna_bias{false};
|
|
uint32_t bl_tick_counter{0};
|
|
uint16_t touch_threshold{32};
|
|
|
|
void set_antenna_bias(const bool v) {
|
|
antenna_bias = v;
|
|
}
|
|
|
|
bool get_antenna_bias() {
|
|
return antenna_bias;
|
|
}
|
|
|
|
static constexpr uint32_t systick_count(const uint32_t clock_source_f) {
|
|
return clock_source_f / CH_FREQUENCY;
|
|
}
|
|
|
|
static constexpr uint32_t systick_load(const uint32_t clock_source_f) {
|
|
return systick_count(clock_source_f) - 1;
|
|
}
|
|
|
|
constexpr uint32_t i2c0_bus_f = 400000;
|
|
constexpr uint32_t i2c0_high_period_ns = 900;
|
|
|
|
typedef struct {
|
|
uint32_t clock_f;
|
|
uint32_t systick_count;
|
|
uint32_t idivb;
|
|
uint32_t idivc;
|
|
} clock_config_t;
|
|
|
|
static constexpr uint32_t idiv_config(const cgu::CLK_SEL clk_sel, const uint32_t idiv) {
|
|
return cgu::IDIV_CTRL{0, idiv - 1, 1, clk_sel};
|
|
}
|
|
|
|
constexpr clock_config_t clock_config_irc{
|
|
12000000,
|
|
systick_load(12000000),
|
|
idiv_config(cgu::CLK_SEL::IRC, 1),
|
|
idiv_config(cgu::CLK_SEL::IRC, 1),
|
|
};
|
|
|
|
constexpr clock_config_t clock_config_pll1_boot{
|
|
96000000,
|
|
systick_load(96000000),
|
|
idiv_config(cgu::CLK_SEL::PLL1, 9),
|
|
idiv_config(cgu::CLK_SEL::PLL1, 3),
|
|
};
|
|
|
|
constexpr clock_config_t clock_config_pll1_step{
|
|
100000000,
|
|
systick_load(100000000),
|
|
idiv_config(cgu::CLK_SEL::PLL1, 1),
|
|
idiv_config(cgu::CLK_SEL::PLL1, 1),
|
|
};
|
|
|
|
constexpr clock_config_t clock_config_pll1{
|
|
200000000,
|
|
systick_load(200000000),
|
|
idiv_config(cgu::CLK_SEL::PLL1, 2),
|
|
idiv_config(cgu::CLK_SEL::PLL1, 1),
|
|
};
|
|
|
|
constexpr I2CClockConfig i2c_clock_config_400k_boot_clock{
|
|
.clock_source_f = clock_config_pll1_boot.clock_f,
|
|
.bus_f = i2c0_bus_f,
|
|
.high_period_ns = i2c0_high_period_ns,
|
|
};
|
|
|
|
constexpr I2CClockConfig i2c_clock_config_400k_fast_clock{
|
|
.clock_source_f = clock_config_pll1.clock_f,
|
|
.bus_f = i2c0_bus_f,
|
|
.high_period_ns = i2c0_high_period_ns,
|
|
};
|
|
|
|
constexpr I2CConfig i2c_config_boot_clock{
|
|
.high_count = i2c_clock_config_400k_boot_clock.i2c_high_count(),
|
|
.low_count = i2c_clock_config_400k_boot_clock.i2c_low_count(),
|
|
};
|
|
|
|
constexpr I2CConfig i2c_config_fast_clock{
|
|
.high_count = i2c_clock_config_400k_fast_clock.i2c_high_count(),
|
|
.low_count = i2c_clock_config_400k_fast_clock.i2c_low_count(),
|
|
};
|
|
|
|
enum class PortaPackModel {
|
|
R1_20150901,
|
|
R2_20170522,
|
|
AGM,
|
|
AUTODETECT,
|
|
};
|
|
|
|
static bool save_config(int8_t value) {
|
|
persistent_memory::set_config_cpld(value);
|
|
portapack::persistent_memory::cache::persist();
|
|
return true;
|
|
}
|
|
|
|
static int load_config() {
|
|
static Optional<int> config_value;
|
|
if (!config_value.is_valid()) {
|
|
int8_t value = portapack::persistent_memory::config_cpld();
|
|
config_value = value;
|
|
}
|
|
return config_value.value();
|
|
}
|
|
|
|
static PortaPackModel portapack_model() {
|
|
static Optional<PortaPackModel> model;
|
|
|
|
if (!model.is_valid()) {
|
|
jtag::GPIOTarget target{
|
|
portapack::gpio_cpld_tck,
|
|
portapack::gpio_cpld_tms,
|
|
portapack::gpio_cpld_tdi,
|
|
portapack::gpio_cpld_tdo};
|
|
jtag::JTAG jtag{target};
|
|
portapack::cpld::CPLD cpld{jtag};
|
|
|
|
cpld.reset();
|
|
cpld.run_test_idle();
|
|
uint32_t idcode = cpld.get_idcode();
|
|
if (idcode == 0x25610) {
|
|
model = PortaPackModel::AGM;
|
|
} else {
|
|
const auto switches_state = swizzled_switches();
|
|
// chDbgPanic(to_string_hex((uint32_t)switches_state.count(), 8).c_str());
|
|
// Only save config if no other multi key boot action is triggered (like pmem reset)
|
|
if (((switches_state >> (size_t)ui::KeyEvent::Up) & 1) == 1) {
|
|
save_config(1);
|
|
// model = PortaPackModel::R2_20170522; // Commented these out as they should be set down below anyway
|
|
} else if (((switches_state >> (size_t)ui::KeyEvent::Down) & 1) == 1) {
|
|
save_config(2);
|
|
// model = PortaPackModel::R1_20150901;
|
|
} else if (((switches_state >> (size_t)ui::KeyEvent::Left) & 1) == 1) {
|
|
save_config(3);
|
|
// model = PortaPackModel::R1_20150901;
|
|
} else if (((switches_state >> (size_t)ui::KeyEvent::Right) & 1) == 1) {
|
|
save_config(4);
|
|
// model = PortaPackModel::R2_20170522;
|
|
} else if (((switches_state >> (size_t)ui::KeyEvent::Select) & 1) == 1) {
|
|
save_config(0);
|
|
}
|
|
|
|
if (load_config() == 1) {
|
|
model = PortaPackModel::R2_20170522;
|
|
} else if (load_config() == 2) {
|
|
model = PortaPackModel::R1_20150901;
|
|
} else if (load_config() == 3) {
|
|
model = PortaPackModel::R1_20150901;
|
|
} else if (load_config() == 4) {
|
|
model = PortaPackModel::R2_20170522;
|
|
} else {
|
|
model = PortaPackModel::AUTODETECT;
|
|
}
|
|
}
|
|
}
|
|
|
|
return model.value();
|
|
}
|
|
|
|
// audio_codec_wm8731 = H1R1 & H2+
|
|
// audio_codec_ak4951 = H1R2
|
|
|
|
static audio::Codec* portapack_audio_codec() {
|
|
/* I2C ready OK, Automatic recognition of audio chip */
|
|
return (audio_codec_wm8731.detected())
|
|
? static_cast<audio::Codec*>(&audio_codec_wm8731)
|
|
: static_cast<audio::Codec*>(&audio_codec_ak4951);
|
|
}
|
|
|
|
static const portapack::cpld::Config& portapack_cpld_config() {
|
|
return (portapack_model() == PortaPackModel::R2_20170522)
|
|
? portapack::cpld::rev_20170522::config
|
|
: portapack::cpld::rev_20150901::config;
|
|
}
|
|
|
|
Backlight* backlight() {
|
|
return (portapack_model() == PortaPackModel::R2_20170522)
|
|
? static_cast<portapack::Backlight*>(&backlight_cat4004) // R2_20170522
|
|
: static_cast<portapack::Backlight*>(&backlight_on_off); // R1_20150901
|
|
}
|
|
|
|
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
|
|
|
|
static LPC_CGU_BASE_CLK_Type* const base_clocks_idivc[] = {
|
|
&LPC_CGU->BASE_PERIPH_CLK,
|
|
&LPC_CGU->BASE_M4_CLK,
|
|
&LPC_CGU->BASE_APB1_CLK,
|
|
&LPC_CGU->BASE_APB3_CLK,
|
|
&LPC_CGU->BASE_SDIO_CLK,
|
|
&LPC_CGU->BASE_SSP1_CLK,
|
|
};
|
|
|
|
static void set_idivc_base_clocks(const cgu::CLK_SEL clock_source) {
|
|
for (uint32_t i = 0; i < ARRAY_SIZE(base_clocks_idivc); i++) {
|
|
base_clocks_idivc[i]->AUTOBLOCK = 1;
|
|
base_clocks_idivc[i]->CLK_SEL = toUType(clock_source);
|
|
}
|
|
}
|
|
|
|
static void set_clock_config(const clock_config_t& config) {
|
|
LPC_CGU->IDIVB_CTRL.word = config.idivb;
|
|
LPC_CGU->IDIVC_CTRL.word = config.idivc;
|
|
systick_adjust_period(config.systick_count);
|
|
halLPCSetSystemClock(config.clock_f);
|
|
}
|
|
|
|
static void shutdown_base() {
|
|
i2c0.stop();
|
|
|
|
set_clock_config(clock_config_irc);
|
|
|
|
cgu::pll1::disable();
|
|
|
|
set_idivc_base_clocks(cgu::CLK_SEL::IRC);
|
|
|
|
cgu::pll1::ctrl({
|
|
.pd = 1,
|
|
.bypass = 0,
|
|
.fbsel = 0,
|
|
.direct = 1,
|
|
.psel = 0,
|
|
.autoblock = 1,
|
|
.nsel = 0,
|
|
.msel = 23,
|
|
.clk_sel = cgu::CLK_SEL::IRC,
|
|
});
|
|
|
|
cgu::pll1::enable();
|
|
while (!cgu::pll1::is_locked());
|
|
|
|
set_clock_config(clock_config_pll1_boot);
|
|
|
|
i2c0.start(i2c_config_boot_clock);
|
|
|
|
clock_manager.shutdown();
|
|
}
|
|
|
|
static void set_cpu_clock_speed() {
|
|
/* Incantation from LPC43xx UM10503 section 12.2.1.1, to bring the M4
|
|
* core clock speed to the 110 - 204MHz range.
|
|
*/
|
|
|
|
/* Step into the 90-110MHz M4 clock range */
|
|
#ifdef PRALINE
|
|
/* PRALINE: Enable and use 12MHz XTAL directly (no GP_CLKIN from Si5351) */
|
|
|
|
/* Step 1: Enable the crystal oscillator */
|
|
LPC_CGU->XTAL_OSC_CTRL.ENABLE = 0; // 0 = enable (active low)
|
|
LPC_CGU->XTAL_OSC_CTRL.HF = 0; // 0 = low frequency mode (1-20MHz)
|
|
|
|
/* Step 2: Wait for oscillator to stabilize (~250us at IRC speed) */
|
|
volatile uint32_t delay = 3000; // ~250us at 12MHz IRC
|
|
while (delay--);
|
|
|
|
/* Step 3: Configure PLL1 from XTAL
|
|
* Fclkin = 12M, /N=1 = 12M, Fcco = 12M * 17 = 204M
|
|
* Fclk = Fcco / (2*(P=1)) = 102M
|
|
*/
|
|
cgu::pll1::ctrl({
|
|
.pd = 1,
|
|
.bypass = 0,
|
|
.fbsel = 0,
|
|
.direct = 0,
|
|
.psel = 0,
|
|
.autoblock = 1,
|
|
.nsel = 0, // N = 1
|
|
.msel = 16, // M = 17, so 12MHz * 17 = 204MHz
|
|
.clk_sel = cgu::CLK_SEL::XTAL,
|
|
});
|
|
#else
|
|
/* OG:
|
|
* Fclkin = 40M, /N=2 = 20M, Fcco = 20M * 10 = 200M
|
|
* r9:
|
|
* Fclkin = 10M, /N=1 = 10M, Fcco = 10M * 20 = 200M
|
|
* Fclk = Fcco / (2*(P=1)) = 100M
|
|
*/
|
|
cgu::pll1::ctrl({
|
|
.pd = 1,
|
|
.bypass = 0,
|
|
.fbsel = 0,
|
|
.direct = 0,
|
|
.psel = 0,
|
|
.autoblock = 1,
|
|
.nsel = hackrf_r9 ? 0UL : 1UL,
|
|
.msel = hackrf_r9 ? 19UL : 9UL,
|
|
.clk_sel = cgu::CLK_SEL::GP_CLKIN,
|
|
});
|
|
#endif
|
|
|
|
cgu::pll1::enable();
|
|
#ifndef PRALINE
|
|
while (!cgu::pll1::is_locked());
|
|
|
|
set_clock_config(clock_config_pll1_step);
|
|
/* Delay >50us at 90-110MHz clock speed */
|
|
volatile uint32_t delay = 1400;
|
|
while (delay--);
|
|
set_clock_config(clock_config_pll1);
|
|
|
|
/* Remove /2P divider from PLL1 output to achieve full speed */
|
|
cgu::pll1::direct();
|
|
#else
|
|
// Wait for PLL1 to lock with timeout
|
|
{
|
|
uint32_t timeout = 100000;
|
|
while (!cgu::pll1::is_locked() && timeout > 0) {
|
|
timeout--;
|
|
}
|
|
}
|
|
|
|
if (cgu::pll1::is_locked()) {
|
|
set_clock_config(clock_config_pll1_step);
|
|
|
|
/* Delay >50us at 90-110MHz clock speed */
|
|
volatile uint32_t delay = 1400;
|
|
while (delay--);
|
|
set_clock_config(clock_config_pll1);
|
|
|
|
/* Remove /2P divider from PLL1 output to achieve full speed */
|
|
cgu::pll1::direct();
|
|
}
|
|
#endif
|
|
}
|
|
|
|
static void draw_splash_screen_icon(int16_t n, const ui::Bitmap& bitmap) {
|
|
ui::Painter painter;
|
|
|
|
painter.draw_bitmap(
|
|
{portapack::display.width() / 2 - 8 - 40 + (n * 20), portapack::display.height() / 2 - 8 + 40},
|
|
bitmap,
|
|
Theme::getInstance()->bg_darkest->foreground,
|
|
Theme::getInstance()->bg_darkest->background);
|
|
}
|
|
|
|
static bool is_portapack_present() {
|
|
systime_t timeout = 50;
|
|
uint8_t wm8731_reset_command[] = {0x0f, 0x00};
|
|
if (i2c0.transmit(0x1a /* wm8731 */, wm8731_reset_command, 2, timeout) == false) {
|
|
audio_codec_ak4951.reset();
|
|
uint8_t ak4951_init_command[] = {0x00, 0x00};
|
|
i2c0.transmit(0x12 /* ak4951 */, ak4951_init_command, 2, timeout);
|
|
chThdSleepMilliseconds(10);
|
|
if (i2c0.transmit(0x12 /* ak4951 */, ak4951_init_command, 2, timeout) == false) {
|
|
shutdown_base();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
static bool check_portapack_cpld() {
|
|
switch (portapack_model()) {
|
|
case PortaPackModel::AUTODETECT: {
|
|
portapack::cpld::CpldUpdateStatus result = portapack::cpld::update_autodetect(
|
|
portapack::cpld::rev_20150901::config, portapack::cpld::rev_20170522::config);
|
|
if (result != portapack::cpld::CpldUpdateStatus::Success) {
|
|
shutdown_base();
|
|
return false;
|
|
}
|
|
} break;
|
|
|
|
case PortaPackModel::R1_20150901:
|
|
case PortaPackModel::R2_20170522: {
|
|
portapack::cpld::CpldUpdateStatus result = portapack::cpld::update_if_necessary(portapack_cpld_config());
|
|
if (result == portapack::cpld::CpldUpdateStatus::Program_failed) {
|
|
chThdSleepMilliseconds(10);
|
|
// Mode left (R1) and right (R2,H2,H2+) bypass going into hackrf mode after failing CPLD update
|
|
// Mode center (autodetect), up (R1) and down (R2,H2,H2+) will go into hackrf mode after failing CPLD update
|
|
if (load_config() != 3 /* left */ && load_config() != 4 /* right */) {
|
|
shutdown_base();
|
|
return false;
|
|
}
|
|
}
|
|
} break;
|
|
|
|
case PortaPackModel::AGM:
|
|
// the AGM devices are always factory flashed. so do nothing
|
|
break;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
static void initialize_boot_splash_screen() {
|
|
ui::Painter painter;
|
|
portapack::display.init();
|
|
|
|
painter.fill_rectangle(
|
|
{0, 0, portapack::display.width(), portapack::display.height()},
|
|
Theme::getInstance()->bg_darkest->background);
|
|
|
|
chThdSleepMilliseconds(17);
|
|
portapack::backlight()->on();
|
|
|
|
painter.draw_bitmap(
|
|
{portapack::display.width() / 2 - 40, portapack::display.height() / 2 - 8},
|
|
ui::bitmap_titlebar_image,
|
|
Theme::getInstance()->bg_darkest->foreground,
|
|
Theme::getInstance()->bg_darkest->background);
|
|
}
|
|
|
|
/* Clock scheme after exiting bootloader in SPIFI mode:
|
|
*
|
|
* XTAL_OSC = powered down
|
|
*
|
|
* PLL0USB = powered down
|
|
* PLL0AUDIO = powered down
|
|
* PLL1 = IRC * 24 = 288 MHz
|
|
*
|
|
* IDIVA = IRC / 1 = 12 MHz
|
|
* IDIVB = PLL1 / 9 = 32 MHz
|
|
* IDIVC = PLL1 / 3 = 96 MHz
|
|
* IDIVD = IRC / 1 = 12 MHz
|
|
* IDIVE = IRC / 1 = 12 MHz
|
|
*
|
|
* BASE_USB0_CLK = PLL0USB
|
|
* BASE_PERIPH_CLK = IRC
|
|
* BASE_M4_CLK = IDIVC (96 MHz)
|
|
* BASE_SPIFI_CLK = IDIVB (32 MHZ)
|
|
*
|
|
* everything else = IRC
|
|
*/
|
|
|
|
/* Clock scheme during PortaPack operation:
|
|
*
|
|
* XTAL_OSC = powered down
|
|
*
|
|
* PLL0USB = XTAL, 480 MHz
|
|
* PLL0AUDIO = GP_CLKIN, Fcco=491.52 MHz, Fout=12.288 MHz
|
|
* PLL1 =
|
|
* OG: GP_CLKIN * 10 = 200 MHz
|
|
* r9: GP_CLKIN * 20 = 200 MHz
|
|
* IDIVA = IRC / 1 = 12 MHz
|
|
* IDIVB = PLL1 / 2 = 100 MHz
|
|
* IDIVC = PLL1 / 1 = 200 MHz
|
|
* IDIVD = PLL0AUDIO / N (where N is varied depending on decimation factor)
|
|
* IDIVE = IRC / 1 = 12 MHz
|
|
*
|
|
* BASE_USB0_CLK = PLL0USB
|
|
* BASE_PERIPH_CLK = IRC
|
|
* BASE_M4_CLK = IDIVC (200 MHz)
|
|
* BASE_SPIFI_CLK = IDIVB (100 MHZ)
|
|
* BASE_AUDIO_CLK = IDIVD
|
|
*
|
|
* everything else = IRC
|
|
*/
|
|
|
|
init_status_t init() {
|
|
set_idivc_base_clocks(cgu::CLK_SEL::IDIVC);
|
|
|
|
i2c0.start(i2c_config_boot_clock);
|
|
|
|
chThdSleepMilliseconds(100);
|
|
|
|
#ifndef PRALINE
|
|
detect_hardware_platform();
|
|
finalize_detect_hardware_platform();
|
|
|
|
chThdSleepMilliseconds(100);
|
|
#endif
|
|
|
|
configure_pins_portapack();
|
|
|
|
portapack::io.init();
|
|
persistent_memory::cache::init();
|
|
|
|
const auto switches_state = swizzled_switches() & (~(0xC0 | 0x80));
|
|
bool lcd_fast_setup = switches_state == 0 && portapack::display.read_display_status();
|
|
|
|
if (lcd_fast_setup) {
|
|
initialize_boot_splash_screen();
|
|
} else {
|
|
if (check_portapack_cpld() == false)
|
|
return init_status_t::INIT_PORTAPACK_CPLD_FAILED;
|
|
}
|
|
|
|
/* Cache some configuration data from persistent memory. */
|
|
rtc_time::dst_init();
|
|
chThdSleepMilliseconds(10);
|
|
|
|
clock_manager.init_clock_generator();
|
|
|
|
i2c0.stop();
|
|
|
|
chThdSleepMilliseconds(10);
|
|
|
|
set_clock_config(clock_config_irc);
|
|
cgu::pll1::disable();
|
|
|
|
set_cpu_clock_speed();
|
|
/* sample max: 1023 sample_t AKA uint16_t
|
|
* touch_sensitivity: range: 1 to 128
|
|
* threshold range: 1023/1 to 1023/128 = 1023 to 8
|
|
*/
|
|
|
|
touch_threshold = portapack::persistent_memory::touchscreen_threshold();
|
|
|
|
if (lcd_fast_setup)
|
|
draw_splash_screen_icon(0, ui::bitmap_icon_memory);
|
|
|
|
usb_serial.initialize();
|
|
|
|
i2c0.start(i2c_config_fast_clock);
|
|
chThdSleepMilliseconds(10);
|
|
|
|
/* Check if portapack is attached by checking if any of the two audio chips is present. */
|
|
if (lcd_fast_setup == false && is_portapack_present() == false)
|
|
return init_status_t::INIT_NO_PORTAPACK;
|
|
|
|
if (lcd_fast_setup)
|
|
draw_splash_screen_icon(1, ui::bitmap_icon_remote);
|
|
|
|
touch::adc::init();
|
|
controls_init();
|
|
chThdSleepMilliseconds(10);
|
|
|
|
clock_manager.set_reference_ppb(persistent_memory::correction_ppb());
|
|
|
|
clock_manager.enable_if_clocks();
|
|
clock_manager.enable_codec_clocks();
|
|
|
|
radio::init();
|
|
|
|
sdcStart(&SDCD1, nullptr);
|
|
sd_card::poll_inserted();
|
|
|
|
chThdSleepMilliseconds(10);
|
|
|
|
if (lcd_fast_setup)
|
|
draw_splash_screen_icon(2, ui::bitmap_icon_sd);
|
|
|
|
init_status_t return_code = init_status_t::INIT_SUCCESS;
|
|
#ifndef PRALINE
|
|
// HackRF One uses CPLD - load it via JTAG
|
|
if (!hackrf::cpld::load_sram()) {
|
|
if (lcd_fast_setup)
|
|
chDbgPanic("HACKRF CPLD FAILED");
|
|
|
|
return_code = init_status_t::INIT_HACKRF_CPLD_FAILED;
|
|
}
|
|
#else
|
|
// HackRF Pro (PRALINE) uses FPGA - already loaded in board.cpp __early_init()
|
|
// via fpga_bridge_init(), so nothing to do here
|
|
#endif
|
|
|
|
if (lcd_fast_setup)
|
|
draw_splash_screen_icon(3, ui::bitmap_icon_hackrf);
|
|
|
|
chThdSleepMilliseconds(10); // This delay seems to solve white noise audio issues
|
|
|
|
LPC_CREG->DMAMUX = portapack::gpdma_mux;
|
|
gpdma::controller.enable();
|
|
|
|
chThdSleepMilliseconds(10);
|
|
|
|
audio::init(portapack_audio_codec());
|
|
|
|
battery::BatteryManagement::set_calc_override(persistent_memory::ui_override_batt_calc());
|
|
i2cdev::I2CDevManager::init();
|
|
|
|
if (lcd_fast_setup)
|
|
draw_splash_screen_icon(4, ui::bitmap_icon_speaker);
|
|
else {
|
|
portapack::display.init();
|
|
portapack::backlight()->on();
|
|
}
|
|
|
|
return return_code;
|
|
}
|
|
|
|
void shutdown(const bool leave_screen_on) {
|
|
gpdma::controller.disable();
|
|
|
|
if (!leave_screen_on) {
|
|
backlight()->off();
|
|
display.shutdown();
|
|
}
|
|
|
|
receiver_model.disable();
|
|
transmitter_model.disable();
|
|
audio::shutdown();
|
|
|
|
hackrf::cpld::init_from_eeprom();
|
|
|
|
shutdown_base();
|
|
}
|
|
|
|
void setEventDispatcherToUSBSerial(EventDispatcher* evt) {
|
|
usb_serial.setEventDispatcher(evt);
|
|
}
|
|
|
|
bool async_tx_enabled = false; // this is for serial tx things, globally
|
|
|
|
} /* namespace portapack */
|