diff --git a/firmware/application/portapack.cpp b/firmware/application/portapack.cpp index e23a89aab..0157cd4f9 100644 --- a/firmware/application/portapack.cpp +++ b/firmware/application/portapack.cpp @@ -212,24 +212,53 @@ int read_file(std::string name) { } static int load_config(){ - int8_t value = portapack::persistent_memory::config_cpld(); - if(value == 0 && sd_card::status() == sd_card::Status::Mounted){ - int data = read_file("/hardware/settings.txt"); - if(data != -1) { - return data; + static Optional config_value; + if(!config_value.is_valid()){ + int8_t value = portapack::persistent_memory::config_cpld(); + if((value <= 0 || value >= 4) && sd_card::status() == sd_card::Status::Mounted){ + int data = read_file("/hardware/settings.txt"); + if(data != -1) { + config_value = data; + } + } else { + config_value = value; } } - return value; + return config_value.value(); } + static PortaPackModel portapack_model() { static Optional model; if( !model.is_valid() ) { - if( audio_codec_wm8731.detected() ) { - model = PortaPackModel::R1_20150901; // H1R1 + const auto switches_state = get_switches_state(); + if (switches_state[(size_t)ui::KeyEvent::Up]){ + save_config(1); + model = PortaPackModel::R2_20170522; + } + else if (switches_state[(size_t)ui::KeyEvent::Down]){ + save_config(2); + model = PortaPackModel::R1_20150901; + } + else if (switches_state[(size_t)ui::KeyEvent::Left]){ + save_config(3); + } + else if (switches_state[(size_t)ui::KeyEvent::Select]){ + save_config(0); + } + + + if (load_config() == 1) { + model = PortaPackModel::R2_20170522; + } else if (load_config() == 2) { + model = PortaPackModel::R1_20150901; } else { - model = PortaPackModel::R2_20170522; // H1R2, H2+ + if( audio_codec_wm8731.detected() ) { + model = PortaPackModel::R1_20150901; // H1R1 + } else { + model = PortaPackModel::R2_20170522; // H1R2, H2, H2+ + } } } @@ -248,28 +277,6 @@ static audio::Codec* portapack_audio_codec() { } static const portapack::cpld::Config& portapack_cpld_config() { - const auto switches_state = get_switches_state(); - if (switches_state[(size_t)ui::KeyEvent::Up]){ - save_config(1); - return portapack::cpld::rev_20170522::config; - } - if (switches_state[(size_t)ui::KeyEvent::Down]){ - save_config(2); - return portapack::cpld::rev_20150901::config; - } - if (switches_state[(size_t)ui::KeyEvent::Left]){ - save_config(3); - } - if (switches_state[(size_t)ui::KeyEvent::Select]){ - save_config(0); - } - - - if (load_config() == 1) { - return portapack::cpld::rev_20170522::config; - } else if (load_config() == 2) { - return portapack::cpld::rev_20150901::config; - } return (portapack_model() == PortaPackModel::R2_20170522) ? portapack::cpld::rev_20170522::config : portapack::cpld::rev_20150901::config; @@ -453,21 +460,17 @@ bool init() { clock_manager.enable_first_if_clock(); clock_manager.enable_second_if_clock(); clock_manager.enable_codec_clocks(); - radio::init(); + radio::init(); sdcStart(&SDCD1, nullptr); - sd_card::poll_inserted(); + + chThdSleepMilliseconds(1); if( !portapack::cpld::update_if_necessary(portapack_cpld_config()) ) { + chThdSleepMilliseconds(1); // If using a "2021/12 QFP100", press and hold the left button while booting. Should only need to do once. - const auto switches_state = get_switches_state(); - /* - * The LEFT key held check seems redundant as its in the portapack_cpld_config(). - * But for some reason the persistent_memory check fails on some devices if we dont have the extra check in.... - * So dont ask me why that is, but we have to keep this redundant check in for the persistent_memory check to work. - */ - if (!switches_state[(size_t)ui::KeyEvent::Left] && load_config() != 3){ + if (load_config() != 3){ shutdown_base(); return false; } @@ -477,6 +480,8 @@ bool init() { chSysHalt(); } + chThdSleepMilliseconds(1); // This delay seems to solve white noise audio issues + LPC_CREG->DMAMUX = portapack::gpdma_mux; gpdma::controller.enable();