diff --git a/firmware/baseband/spectrum_collector.hpp b/firmware/baseband/spectrum_collector.hpp index 6c50120b..b238c1fe 100644 --- a/firmware/baseband/spectrum_collector.hpp +++ b/firmware/baseband/spectrum_collector.hpp @@ -35,7 +35,8 @@ class SpectrumCollector { public: constexpr SpectrumCollector( - ) : channel_spectrum_decimator { 1 } + ) : channel_spectrum_decimator { 1 }, + fifo { fifo_data, ChannelSpectrumConfigMessage::fifo_k } { } @@ -52,6 +53,7 @@ public: private: BlockDecimator channel_spectrum_decimator; ChannelSpectrumFIFO fifo; + ChannelSpectrum fifo_data[1 << ChannelSpectrumConfigMessage::fifo_k]; volatile bool channel_spectrum_request_update { false }; bool streaming { false }; diff --git a/firmware/common/fifo.hpp b/firmware/common/fifo.hpp index 86c06a73..56e82baa 100644 --- a/firmware/common/fifo.hpp +++ b/firmware/common/fifo.hpp @@ -22,18 +22,24 @@ #ifndef __FIFO_H__ #define __FIFO_H__ +#include #include #include +#include #include /* FIFO implementation inspired by Linux kfifo. */ -template +template class FIFO { public: constexpr FIFO( - ) : _in { 0 }, + T* data, + size_t k + ) : _data { data }, + _size { 1U << k }, + _in { 0 }, _out { 0 } { } @@ -150,15 +156,15 @@ public: } private: - static constexpr size_t size() { - return (1UL << K); + size_t size() const { + return _size; } static constexpr size_t esize() { return sizeof(T); } - static constexpr size_t mask() { + size_t mask() const { return size() - 1; } @@ -224,7 +230,8 @@ private: return buf_len; } - T _data[size()]; + T* const _data; + const size_t _size; volatile size_t _in; volatile size_t _out; }; diff --git a/firmware/common/message.hpp b/firmware/common/message.hpp index c29eb11e..cbce464e 100644 --- a/firmware/common/message.hpp +++ b/firmware/common/message.hpp @@ -244,10 +244,12 @@ struct ChannelSpectrum { uint32_t channel_filter_stop_frequency { 0 }; }; -using ChannelSpectrumFIFO = FIFO; +using ChannelSpectrumFIFO = FIFO; class ChannelSpectrumConfigMessage : public Message { public: + static constexpr size_t fifo_k = 2; + constexpr ChannelSpectrumConfigMessage( ChannelSpectrumFIFO* fifo ) : Message { ID::ChannelSpectrumConfig }, diff --git a/firmware/common/message_queue.hpp b/firmware/common/message_queue.hpp index c4430a23..baba13ef 100644 --- a/firmware/common/message_queue.hpp +++ b/firmware/common/message_queue.hpp @@ -32,10 +32,13 @@ using namespace lpc43xx; #include -template class MessageQueue { public: - MessageQueue() { + MessageQueue( + uint8_t* const data, + size_t k + ) : fifo { data, k } + { chMtxInit(&mutex_write); } @@ -80,7 +83,7 @@ public: } private: - FIFO fifo; + FIFO fifo; Mutex mutex_write; bool push(const void* const buf, const size_t len) { diff --git a/firmware/common/portapack_shared_memory.hpp b/firmware/common/portapack_shared_memory.hpp index a0fd7010..f7c11d0c 100644 --- a/firmware/common/portapack_shared_memory.hpp +++ b/firmware/common/portapack_shared_memory.hpp @@ -23,6 +23,7 @@ #define __PORTAPACK_SHARED_MEMORY_H__ #include +#include #include "message_queue.hpp" @@ -32,8 +33,13 @@ struct TouchADCFrame { /* NOTE: These structures must be located in the same location in both M4 and M0 binaries */ struct SharedMemory { - MessageQueue<12> baseband_queue; - MessageQueue<11> application_queue; + static constexpr size_t baseband_queue_k = 12; + static constexpr size_t application_queue_k = 11; + + MessageQueue baseband_queue; + uint8_t baseband_queue_data[1 << baseband_queue_k]; + MessageQueue application_queue; + uint8_t application_queue_data[1 << application_queue_k]; // TODO: M0 should directly configure and control DMA channel that is // acquiring ADC samples. @@ -44,8 +50,12 @@ extern SharedMemory& shared_memory; #if defined(LPC43XX_M0) inline void init_message_queues() { - new (&shared_memory.baseband_queue) MessageQueue<12>(); - new (&shared_memory.application_queue) MessageQueue<11>(); + new (&shared_memory.baseband_queue) MessageQueue( + shared_memory.baseband_queue_data, SharedMemory::baseband_queue_k + ); + new (&shared_memory.application_queue) MessageQueue( + shared_memory.application_queue_data, SharedMemory::application_queue_k + ); } #endif