From 1d16cd8aac37a8dda72c3e7ca8af49b5a9fa2f97 Mon Sep 17 00:00:00 2001 From: Jared Boone Date: Sat, 1 Aug 2015 13:42:27 -0700 Subject: [PATCH] Consolidate peripheral declarations. --- firmware/application/Makefile | 1 + firmware/application/main.cpp | 146 +----------------- firmware/application/portapack.cpp | 187 ++++++++++++++++++++++++ firmware/application/portapack.hpp | 45 ++++++ firmware/application/radio.cpp | 5 +- firmware/application/receiver_model.cpp | 9 +- firmware/application/ui_navigation.cpp | 2 +- firmware/application/ui_receiver.cpp | 7 +- 8 files changed, 244 insertions(+), 158 deletions(-) create mode 100644 firmware/application/portapack.cpp create mode 100644 firmware/application/portapack.hpp diff --git a/firmware/application/Makefile b/firmware/application/Makefile index ba3fd78b..f3e7941a 100755 --- a/firmware/application/Makefile +++ b/firmware/application/Makefile @@ -124,6 +124,7 @@ CPPSRC = main.cpp \ event.cpp \ message_queue.cpp \ hackrf_hal.cpp \ + portapack.cpp \ portapack_shared_memory.cpp \ portapack_io.cpp \ i2c_pp.cpp \ diff --git a/firmware/application/main.cpp b/firmware/application/main.cpp index 3af69b99..d74991b6 100755 --- a/firmware/application/main.cpp +++ b/firmware/application/main.cpp @@ -22,42 +22,27 @@ #include "ch.h" #include "test.h" -#include "hackrf_hal.hpp" -#include "hackrf_gpio.hpp" -using namespace hackrf::one; +#include "lpc43xx_cpp.hpp" +using namespace lpc43xx; +#include "portapack.hpp" #include "portapack_shared_memory.hpp" -#include "portapack_hal.hpp" -#include "portapack_io.hpp" #include "cpld_update.hpp" #include "message_queue.hpp" -#include "si5351.hpp" -#include "clock_manager.hpp" - -#include "wm8731.hpp" -#include "radio.hpp" -#include "touch.hpp" -#include "touch_adc.hpp" - #include "ui.hpp" #include "ui_widget.hpp" #include "ui_painter.hpp" #include "ui_navigation.hpp" -#include "receiver_model.hpp" - #include "irq_ipc.hpp" #include "irq_lcd_frame.hpp" #include "irq_controls.hpp" #include "event.hpp" -#include "i2c_pp.hpp" -#include "spi_pp.hpp" - #include "m4_startup.hpp" #include "spi_image.hpp" @@ -68,12 +53,6 @@ using namespace hackrf::one; #include -I2C i2c0(&I2CD0); -SPI ssp0(&SPID1); -SPI ssp1(&SPID2); - -wolfson::wm8731::WM8731 audio_codec { i2c0, portapack::wm8731_i2c_address }; - /* From ChibiOS crt0.c: * Two stacks available for Cortex-M, main stack or process stack. * @@ -147,112 +126,6 @@ static spi_bus_t ssp0 = { * _default_exit() (default is infinite loop) */ -si5351::Si5351 clock_generator { - i2c0, si5351_i2c_address -}; - -ClockManager clock_manager { - i2c0, clock_generator -}; - -ReceiverModel receiver_model { - clock_manager -}; - -class Power { -public: - void init() { - /* VAA powers: - * MAX5864 analog section. - * MAX2837 registers and other functions. - * RFFC5072 analog section. - * - * Beware that power applied to pins of the MAX2837 may - * show up on VAA and start powering other components on the - * VAA net. So turn on VAA before driving pins from MCU to - * MAX2837. - */ - /* Turn on VAA */ - gpio_vaa_disable.clear(); - gpio_vaa_disable.output(); - - /* 1V8 powers CPLD internals. - */ - /* Turn on 1V8 */ - gpio_1v8_enable.set(); - gpio_1v8_enable.output(); - - /* Set VREGMODE for switching regulator on HackRF One */ - gpio_vregmode.set(); - gpio_vregmode.output(); - } - -private: -}; - -static Power power; - -static void init() { - for(const auto& pin : pins) { - pin.init(); - } - - /* Configure other pins */ - LPC_SCU->SFSI2C0 = - (1U << 3) - | (1U << 11) - ; - - power.init(); - - gpio_max5864_select.set(); - gpio_max5864_select.output(); - - gpio_max2837_select.set(); - gpio_max2837_select.output(); - - led_usb.setup(); - led_rx.setup(); - led_tx.setup(); - - clock_manager.init(); - clock_manager.run_at_full_speed(); - - clock_manager.start_audio_pll(); - audio_codec.init(); - - clock_manager.enable_first_if_clock(); - clock_manager.enable_second_if_clock(); - clock_manager.enable_codec_clocks(); - radio::init(); - - touch::adc::init(); -} - -extern "C" { - -void __late_init(void) { - - reset(); - - /* - * System initializations. - * - HAL initialization, this also initializes the configured device drivers - * and performs the board-specific initializations. - * - Kernel initialization, the main() function becomes a thread and the - * RTOS is active. - */ - halInit(); - - /* After this call, scheduler, systick, heap, etc. are available. */ - /* By doing chSysInit() here, it runs before C++ constructors, which may - * require the heap. - */ - chSysInit(); -} - -} - extern "C" { CH_IRQ_HANDLER(RTC_IRQHandler) { @@ -601,18 +474,8 @@ message_handlers[Message::ID::TestResults] = [&system_view](const Message* const }; */ -portapack::IO portapack::io { - portapack::gpio_dir, - portapack::gpio_lcd_rd, - portapack::gpio_lcd_wr, - portapack::gpio_io_stbx, - portapack::gpio_addr, - portapack::gpio_lcd_te, - portapack::gpio_unused, -}; - int main(void) { - init(); + portapack::init(); if( !cpld_update_if_necessary() ) { chSysHalt(); @@ -646,7 +509,6 @@ int main(void) { context.message_map[Message::ID::FSKPacket] = [](const Message* const p) { const auto message = static_cast(p); (void)message; - led_usb.toggle(); }; m4txevent_interrupt_enable(); diff --git a/firmware/application/portapack.cpp b/firmware/application/portapack.cpp new file mode 100644 index 00000000..894b1f0a --- /dev/null +++ b/firmware/application/portapack.cpp @@ -0,0 +1,187 @@ +/* + * 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 "hackrf_hal.hpp" +#include "hackrf_gpio.hpp" +using namespace hackrf::one; + +#include "si5351.hpp" +#include "clock_manager.hpp" + +#include "i2c_pp.hpp" + +#include "touch_adc.hpp" + +#include "m4_startup.hpp" +#include "spi_image.hpp" + +namespace portapack { + +portapack::IO io { + portapack::gpio_dir, + portapack::gpio_lcd_rd, + portapack::gpio_lcd_wr, + portapack::gpio_io_stbx, + portapack::gpio_addr, + portapack::gpio_lcd_te, + portapack::gpio_unused, +}; + +I2C i2c0(&I2CD0); +SPI ssp0(&SPID1); +SPI ssp1(&SPID2); + +wolfson::wm8731::WM8731 audio_codec { i2c0, portapack::wm8731_i2c_address }; + +si5351::Si5351 clock_generator { + i2c0, hackrf::one::si5351_i2c_address +}; + +ClockManager clock_manager { + i2c0, clock_generator +}; + +ReceiverModel receiver_model { + clock_manager +}; + +class Power { +public: + void init() { + /* VAA powers: + * MAX5864 analog section. + * MAX2837 registers and other functions. + * RFFC5072 analog section. + * + * Beware that power applied to pins of the MAX2837 may + * show up on VAA and start powering other components on the + * VAA net. So turn on VAA before driving pins from MCU to + * MAX2837. + */ + /* Turn on VAA */ + gpio_vaa_disable.clear(); + gpio_vaa_disable.output(); + + /* 1V8 powers CPLD internals. + */ + /* Turn on 1V8 */ + gpio_1v8_enable.set(); + gpio_1v8_enable.output(); + + /* Set VREGMODE for switching regulator on HackRF One */ + gpio_vregmode.set(); + gpio_vregmode.output(); + } + + void shutdown() { + gpio_1v8_enable.clear(); + gpio_vaa_disable.set(); + } + +private: +}; + +static Power power; + +void init() { + for(const auto& pin : pins) { + pin.init(); + } + + /* Configure other pins */ + LPC_SCU->SFSI2C0 = + (1U << 3) + | (1U << 11) + ; + + power.init(); + + gpio_max5864_select.set(); + gpio_max5864_select.output(); + + gpio_max2837_select.set(); + gpio_max2837_select.output(); + + led_usb.setup(); + led_rx.setup(); + led_tx.setup(); + + clock_manager.init(); + clock_manager.run_at_full_speed(); + + clock_manager.start_audio_pll(); + audio_codec.init(); + + clock_manager.enable_first_if_clock(); + clock_manager.enable_second_if_clock(); + clock_manager.enable_codec_clocks(); + radio::init(); + + touch::adc::init(); +} + +void shutdown() { + sdcStop(&SDCD1); + + radio::disable(); + audio_codec.reset(); + clock_manager.shutdown(); + + power.shutdown(); + // TODO: Wait a bit for supplies to discharge? + + chSysDisable(); + hackrf::one::reset(); + m4_init(portapack::spi_flash::hackrf, reinterpret_cast(0x10000000)); + + while(true) { + __WFE(); + } +} + +extern "C" { + +void __late_init(void) { + + reset(); + + /* + * System initializations. + * - HAL initialization, this also initializes the configured device drivers + * and performs the board-specific initializations. + * - Kernel initialization, the main() function becomes a thread and the + * RTOS is active. + */ + halInit(); + + /* After this call, scheduler, systick, heap, etc. are available. */ + /* By doing chSysInit() here, it runs before C++ constructors, which may + * require the heap. + */ + chSysInit(); +} + +} + +} /* namespace portapack */ diff --git a/firmware/application/portapack.hpp b/firmware/application/portapack.hpp new file mode 100644 index 00000000..cf885894 --- /dev/null +++ b/firmware/application/portapack.hpp @@ -0,0 +1,45 @@ +/* + * 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_io.hpp" + +#include "receiver_model.hpp" + +#include "spi_pp.hpp" +#include "wm8731.hpp" + +#include "radio.hpp" + +namespace portapack { + +extern portapack::IO io; + +extern SPI ssp0; +extern SPI ssp1; + +extern wolfson::wm8731::WM8731 audio_codec; + +extern ReceiverModel receiver_model; + +void init(); +void shutdown(); + +} /* namespace portapack */ diff --git a/firmware/application/radio.cpp b/firmware/application/radio.cpp index b493f217..f404f76a 100644 --- a/firmware/application/radio.cpp +++ b/firmware/application/radio.cpp @@ -36,8 +36,7 @@ #include "hackrf_gpio.hpp" using namespace hackrf::one; -#include "spi_pp.hpp" -extern SPI ssp1; +#include "portapack.hpp" namespace radio { @@ -75,7 +74,7 @@ static constexpr SPIConfig ssp_config_max5864 = { .cpsr = ssp1_cpsr, }; -static spi::arbiter::Arbiter ssp1_arbiter(ssp1); +static spi::arbiter::Arbiter ssp1_arbiter(portapack::ssp1); static spi::arbiter::Target ssp1_target_max2837 { ssp1_arbiter, diff --git a/firmware/application/receiver_model.cpp b/firmware/application/receiver_model.cpp index 6c642302..51e8c27e 100644 --- a/firmware/application/receiver_model.cpp +++ b/firmware/application/receiver_model.cpp @@ -22,13 +22,8 @@ #include "receiver_model.hpp" #include "portapack_shared_memory.hpp" -#include "radio.hpp" -#include "wm8731.hpp" - -// TODO: Nasty. Put this in an #include somewhere, or a shared system state -// object? - -extern wolfson::wm8731::WM8731 audio_codec; +#include "portapack.hpp" +using namespace portapack; rf::Frequency ReceiverModel::tuning_frequency() const { return tuning_frequency_; diff --git a/firmware/application/ui_navigation.cpp b/firmware/application/ui_navigation.cpp index 68e92833..16a6e45d 100644 --- a/firmware/application/ui_navigation.cpp +++ b/firmware/application/ui_navigation.cpp @@ -27,7 +27,7 @@ #include "ui_debug.hpp" #include "ui_receiver.hpp" -extern ReceiverModel receiver_model; +#include "portapack.hpp" namespace ui { diff --git a/firmware/application/ui_receiver.cpp b/firmware/application/ui_receiver.cpp index 9ef66cac..beb4a2f6 100644 --- a/firmware/application/ui_receiver.cpp +++ b/firmware/application/ui_receiver.cpp @@ -21,11 +21,8 @@ #include "ui_receiver.hpp" -// TODO: Nasty. Put this in an #include somewhere, or a shared system state -// object? - -#include "wm8731.hpp" -extern wolfson::wm8731::WM8731 audio_codec; +#include "portapack.hpp" +using namespace portapack; namespace ui {