diff --git a/firmware/application/Makefile b/firmware/application/Makefile index 1281ed95..8b9b5680 100755 --- a/firmware/application/Makefile +++ b/firmware/application/Makefile @@ -168,6 +168,7 @@ CPPSRC = main.cpp \ receiver_model.cpp \ spectrum_color_lut.cpp \ ../common/utility.cpp \ + ../common/chibios_cpp.cpp \ ../common/debug.cpp \ ../common/gcc.cpp \ m4_startup.cpp \ diff --git a/firmware/baseband/Makefile b/firmware/baseband/Makefile index 56a0b33c..06cb1f27 100755 --- a/firmware/baseband/Makefile +++ b/firmware/baseband/Makefile @@ -151,6 +151,7 @@ CPPSRC = main.cpp \ audio_dma.cpp \ touch_dma.cpp \ ../common/utility.cpp \ + ../common/chibios_cpp.cpp \ ../common/debug.cpp \ ../common/gcc.cpp diff --git a/firmware/baseband/baseband_stats_collector.hpp b/firmware/baseband/baseband_stats_collector.hpp index fe5c7fe9..96f7c212 100644 --- a/firmware/baseband/baseband_stats_collector.hpp +++ b/firmware/baseband/baseband_stats_collector.hpp @@ -26,7 +26,7 @@ #include "dsp_types.hpp" #include "message.hpp" -#include "utility.hpp" +#include "utility_m4.hpp" #include #include diff --git a/firmware/baseband/channel_stats_collector.hpp b/firmware/baseband/channel_stats_collector.hpp index 4eecb5c9..64b1596a 100644 --- a/firmware/baseband/channel_stats_collector.hpp +++ b/firmware/baseband/channel_stats_collector.hpp @@ -29,6 +29,8 @@ #include #include +#include + class ChannelStatsCollector { public: template diff --git a/firmware/baseband/dsp_demodulate.cpp b/firmware/baseband/dsp_demodulate.cpp index 9a9023d6..4ee6ed68 100644 --- a/firmware/baseband/dsp_demodulate.cpp +++ b/firmware/baseband/dsp_demodulate.cpp @@ -23,6 +23,7 @@ #include "complex.hpp" #include "fxpt_atan2.hpp" +#include "utility_m4.hpp" #include diff --git a/firmware/baseband/matched_filter.cpp b/firmware/baseband/matched_filter.cpp index 6772ab8c..2c843316 100644 --- a/firmware/baseband/matched_filter.cpp +++ b/firmware/baseband/matched_filter.cpp @@ -21,9 +21,6 @@ #include "matched_filter.hpp" -// TODO: Move the fast complex multiply code to another place. -#include "dsp_fft.hpp" - namespace dsp { namespace matched_filter { diff --git a/firmware/common/chibios_cpp.cpp b/firmware/common/chibios_cpp.cpp new file mode 100644 index 00000000..74f4c5d6 --- /dev/null +++ b/firmware/common/chibios_cpp.cpp @@ -0,0 +1,40 @@ +/* + * 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 "chibios_cpp.hpp" + +#include + +void* operator new(size_t size) { + return chHeapAlloc(0x0, size); +} + +void* operator new[](size_t size) { + return chHeapAlloc(0x0, size); +} + +void operator delete(void* p) noexcept { + chHeapFree(p); +} + +void operator delete[](void* p) noexcept { + chHeapFree(p); +} diff --git a/firmware/common/chibios_cpp.hpp b/firmware/common/chibios_cpp.hpp new file mode 100644 index 00000000..dd423777 --- /dev/null +++ b/firmware/common/chibios_cpp.hpp @@ -0,0 +1,34 @@ +/* + * 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. + */ + +#ifndef __CHIBIOS_CPP_H__ +#define __CHIBIOS_CPP_H__ + +#include + +/* Override new/delete to use Chibi/OS heap functions */ +/* NOTE: Do not inline these, it doesn't work. ;-) */ +void* operator new(size_t size); +void* operator new[](size_t size); +void operator delete(void* p); +void operator delete[](void* p); + +#endif/*__CHIBIOS_CPP_H__*/ diff --git a/firmware/common/complex.hpp b/firmware/common/complex.hpp index e6f3e505..d9ceb139 100644 --- a/firmware/common/complex.hpp +++ b/firmware/common/complex.hpp @@ -26,8 +26,6 @@ #include #include -#include - constexpr float pi { 3.141592653589793238462643383279502884f }; namespace std { @@ -124,20 +122,4 @@ static_assert(sizeof(complex8_t) == 2, "complex8_t size wrong"); static_assert(sizeof(complex16_t) == 4, "complex16_t size wrong"); static_assert(sizeof(complex32_t) == 8, "complex32_t size wrong"); -#if defined(LPC43XX_M4) -static inline complex32_t multiply_conjugate_s16_s32(const complex16_t::rep_type a, const complex16_t::rep_type b) { - // conjugate: conj(a + bj) = a - bj - // multiply: (a + bj) * (c + dj) = (ac - bd) + (bc + ad)j - // conjugate-multiply: (ac + bd) + (bc - ad)j - //return { a.real() * b.real() + a.imag() * b.imag(), a.imag() * b.real() - a.real() * b.imag() }; - const int32_t rr = __SMULBB(a, b); - const int32_t ii = __SMULTT(a, b); - const int32_t r = __QADD(rr, ii); - const int32_t ir = __SMULTB(a, b); - const int32_t ri = __SMULBT(a, b); - const int32_t i = __QSUB(ir, ri); - return { r, i }; -} -#endif - #endif/*__COMPLEX_H__*/ diff --git a/firmware/common/fifo.hpp b/firmware/common/fifo.hpp index 338f9056..f7a490c7 100644 --- a/firmware/common/fifo.hpp +++ b/firmware/common/fifo.hpp @@ -25,6 +25,8 @@ #include #include +#include + /* FIFO implementation inspired by Linux kfifo. */ template diff --git a/firmware/common/utility.cpp b/firmware/common/utility.cpp index 857bbb73..b09cf357 100644 --- a/firmware/common/utility.cpp +++ b/firmware/common/utility.cpp @@ -19,8 +19,6 @@ * Boston, MA 02110-1301, USA. */ -#include - #include "utility.hpp" #include @@ -115,22 +113,3 @@ static constexpr uint32_t gcd_top(const uint32_t u, const uint32_t v) { uint32_t gcd(const uint32_t u, const uint32_t v) { return gcd_top(u, v); } - -/* Memory management, overriding toolchain new, which uses malloc */ -/* TODO: Move to something like "memory.cpp"! Not utility.cpp. */ - -void* operator new(size_t size) { - return chHeapAlloc(0x0, size); -} - -void* operator new[](size_t size) { - return chHeapAlloc(0x0, size); -} - -void operator delete(void* p) noexcept { - chHeapFree(p); -} - -void operator delete[](void* p) noexcept { - chHeapFree(p); -} diff --git a/firmware/common/utility.hpp b/firmware/common/utility.hpp index be341042..b1d00b65 100644 --- a/firmware/common/utility.hpp +++ b/firmware/common/utility.hpp @@ -28,8 +28,6 @@ #include #include -#include - #define LOCATE_IN_RAM __attribute__((section(".ramtext"))) constexpr size_t operator "" _KiB(unsigned long long v) { @@ -62,18 +60,6 @@ inline constexpr T pow(const T base, unsigned const exponent) { return (exponent == 0) ? 1 : (base * pow(base, exponent - 1)); } -#if defined(LPC43XX_M4) -static inline bool m4_flag_saturation() { - return __get_APSR() & (1U << 27); -} - -static inline void clear_m4_flag_saturation() { - uint32_t flags = 1; - __asm volatile ("MSR APSR_nzcvqg, %0" : : "r" (flags)); -} - -#endif - float complex16_mag_squared_to_dbv_norm(const float c16_mag_squared); inline float magnitude_squared(const std::complex c) { @@ -84,13 +70,6 @@ inline float magnitude_squared(const std::complex c) { return r2 + i2; } -/* Override new/delete to use Chibi/OS heap functions */ -/* NOTE: Do not inline these, it doesn't work. ;-) */ -void* operator new(size_t size); -void* operator new[](size_t size); -void operator delete(void* p); -void operator delete[](void* p); - namespace std { /*! Stephan T Lavavej (STL!) implementation of make_unique, which has been accepted into the C++14 standard. diff --git a/firmware/common/utility_m4.hpp b/firmware/common/utility_m4.hpp new file mode 100644 index 00000000..e7d98e90 --- /dev/null +++ b/firmware/common/utility_m4.hpp @@ -0,0 +1,53 @@ +/* + * 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. + */ + +#ifndef __UTILITY_M4_H__ +#define __UTILITY_M4_H__ + +#if defined(LPC43XX_M4) + +#include + +static inline bool m4_flag_saturation() { + return __get_APSR() & (1U << 27); +} + +static inline void clear_m4_flag_saturation() { + uint32_t flags = 1; + __asm volatile ("MSR APSR_nzcvqg, %0" : : "r" (flags)); +} + +static inline complex32_t multiply_conjugate_s16_s32(const complex16_t::rep_type a, const complex16_t::rep_type b) { + // conjugate: conj(a + bj) = a - bj + // multiply: (a + bj) * (c + dj) = (ac - bd) + (bc + ad)j + // conjugate-multiply: (ac + bd) + (bc - ad)j + //return { a.real() * b.real() + a.imag() * b.imag(), a.imag() * b.real() - a.real() * b.imag() }; + const int32_t rr = __SMULBB(a, b); + const int32_t ii = __SMULTT(a, b); + const int32_t r = __QADD(rr, ii); + const int32_t ir = __SMULTB(a, b); + const int32_t ri = __SMULBT(a, b); + const int32_t i = __QSUB(ir, ri); + return { r, i }; +} +#endif /* defined(LPC43XX_M4) */ + +#endif/*__UTILITY_M4_H__*/