Change IPC to exchange data, not pointers.

This commit is contained in:
Jared Boone 2015-08-20 13:13:12 -07:00
parent f99016d78f
commit 4126f1ab1f
7 changed files with 76 additions and 126 deletions

View File

@ -116,11 +116,12 @@ private:
void handle_application_queue() { void handle_application_queue() {
while( !shared_memory.application_queue.is_empty() ) { while( !shared_memory.application_queue.is_empty() ) {
auto message = shared_memory.application_queue.pop(); std::array<uint8_t, Message::MAX_SIZE> message_buffer;
const Message* const message = reinterpret_cast<Message*>(message_buffer.data());
const auto message_size = shared_memory.application_queue.pop(message_buffer.data(), message_buffer.size());
if( message_size ) {
context.message_map.send(message); context.message_map.send(message);
}
message->state = Message::State::Free;
} }
} }

View File

@ -147,8 +147,7 @@ void ReceiverModel::disable() {
.decimation_factor = 1, .decimation_factor = 1,
} }
}; };
shared_memory.baseband_queue.push(&message); shared_memory.baseband_queue.push(message);
while( !message.is_free() );
radio::disable(); radio::disable();
} }
@ -187,10 +186,7 @@ void ReceiverModel::update_baseband_configuration() {
radio::set_baseband_decimation_by(baseband_oversampling()); radio::set_baseband_decimation_by(baseband_oversampling());
BasebandConfigurationMessage message { baseband_configuration }; BasebandConfigurationMessage message { baseband_configuration };
shared_memory.baseband_queue.push(&message); shared_memory.baseband_queue.push(message);
// Block until message is consumed, since we allocated it on the stack.
while( !message.is_free() );
if( baseband_configuration.mode == 3 ) { if( baseband_configuration.mode == 3 ) {
update_fsk_configuration(); update_fsk_configuration();
@ -222,8 +218,5 @@ static constexpr FSKConfiguration fsk_configuration_tpms_a = {
void ReceiverModel::update_fsk_configuration() { void ReceiverModel::update_fsk_configuration() {
FSKConfigurationMessage message { fsk_configuration_ais }; FSKConfigurationMessage message { fsk_configuration_ais };
shared_memory.baseband_queue.push(&message); shared_memory.baseband_queue.push(message);
// Block until message is consumed, since we allocated it on the stack.
while( !message.is_free() );
} }

View File

@ -201,16 +201,15 @@ public:
const ChannelSpectrum& spectrum const ChannelSpectrum& spectrum
) { ) {
/* TODO: static_assert that message.spectrum.db.size() >= pixel_row.size() */ /* TODO: static_assert that message.spectrum.db.size() >= pixel_row.size() */
const auto& db = *spectrum.db;
std::array<Color, 240> pixel_row; std::array<Color, 240> pixel_row;
for(size_t i=0; i<120; i++) { for(size_t i=0; i<120; i++) {
const auto pixel_color = spectrum_rgb3_lut[db[256 - 120 + i]]; const auto pixel_color = spectrum_rgb3_lut[spectrum.db[256 - 120 + i]];
pixel_row[i] = pixel_color; pixel_row[i] = pixel_color;
} }
for(size_t i=120; i<240; i++) { for(size_t i=120; i<240; i++) {
const auto pixel_color = spectrum_rgb3_lut[db[i - 120]]; const auto pixel_color = spectrum_rgb3_lut[spectrum.db[i - 120]];
pixel_row[i] = pixel_color; pixel_row[i] = pixel_color;
} }

View File

@ -273,10 +273,8 @@ private:
AudioStatisticsMessage audio_stats_message; AudioStatisticsMessage audio_stats_message;
void post_channel_stats_message(const ChannelStatistics statistics) { void post_channel_stats_message(const ChannelStatistics statistics) {
if( channel_stats_message.is_free() ) {
channel_stats_message.statistics = statistics; channel_stats_message.statistics = statistics;
shared_memory.application_queue.push(&channel_stats_message); shared_memory.application_queue.push(channel_stats_message);
}
} }
void post_channel_spectrum_message(const buffer_c16_t data) { void post_channel_spectrum_message(const buffer_c16_t data) {
@ -298,10 +296,8 @@ private:
} }
void post_audio_stats_message(const AudioStatistics statistics) { void post_audio_stats_message(const AudioStatistics statistics) {
if( audio_stats_message.is_free() ) {
audio_stats_message.statistics = statistics; audio_stats_message.statistics = statistics;
shared_memory.application_queue.push(&audio_stats_message); shared_memory.application_queue.push(audio_stats_message);
}
} }
}; };
@ -585,11 +581,9 @@ private:
const std::bitset<256>& payload, const std::bitset<256>& payload,
const size_t bits_received const size_t bits_received
) { ) {
if( message.is_free() ) {
message.packet.payload = payload; message.packet.payload = payload;
message.packet.bits_received = bits_received; message.packet.bits_received = bits_received;
shared_memory.application_queue.push(&message); shared_memory.application_queue.push(message);
}
} }
}; };
@ -602,7 +596,6 @@ static __attribute__((noreturn)) msg_t baseband_fn(void *arg) {
chRegSetThreadName("baseband"); chRegSetThreadName("baseband");
BasebandStatsCollector stats; BasebandStatsCollector stats;
BasebandStatisticsMessage message;
while(true) { while(true) {
// TODO: Place correct sampling rate into buffer returned here: // TODO: Place correct sampling rate into buffer returned here:
@ -616,11 +609,10 @@ static __attribute__((noreturn)) msg_t baseband_fn(void *arg) {
} }
stats.process(buffer, stats.process(buffer,
[&message](const BasebandStatistics statistics) { [](const BasebandStatistics statistics) {
if( message.is_free() ) { BasebandStatisticsMessage message;
message.statistics = statistics; message.statistics = statistics;
shared_memory.application_queue.push(&message); shared_memory.application_queue.push(message);
}
} }
); );
} }
@ -632,7 +624,6 @@ static __attribute__((noreturn)) msg_t rssi_fn(void *arg) {
chRegSetThreadName("rssi"); chRegSetThreadName("rssi");
RSSIStatisticsCollector stats; RSSIStatisticsCollector stats;
RSSIStatisticsMessage message;
while(true) { while(true) {
// TODO: Place correct sampling rate into buffer returned here: // TODO: Place correct sampling rate into buffer returned here:
@ -643,11 +634,10 @@ static __attribute__((noreturn)) msg_t rssi_fn(void *arg) {
stats.process( stats.process(
buffer, buffer,
[&message](const RSSIStatistics statistics) { [](const RSSIStatistics statistics) {
if( message.is_free() ) { RSSIStatisticsMessage message;
message.statistics = statistics; message.statistics = statistics;
shared_memory.application_queue.push(&message); shared_memory.application_queue.push(message);
}
} }
); );
} }
@ -731,43 +721,40 @@ public:
private: private:
MessageHandlerMap message_map; MessageHandlerMap message_map;
ChannelSpectrumMessage spectrum_message;
std::array<uint8_t, 256> spectrum_db;
void handle_baseband_queue() { void handle_baseband_queue() {
while( !shared_memory.baseband_queue.is_empty() ) { while( !shared_memory.baseband_queue.is_empty() ) {
auto message = shared_memory.baseband_queue.pop(); std::array<uint8_t, Message::MAX_SIZE> message_buffer;
const Message* const message = reinterpret_cast<Message*>(message_buffer.data());
const auto message_size = shared_memory.baseband_queue.pop(message_buffer.data(), message_buffer.size());
if( message_size ) {
message_map.send(message); message_map.send(message);
}
message->state = Message::State::Free;
} }
} }
void handle_spectrum() { void handle_spectrum() {
if( channel_spectrum_request_update ) { if( channel_spectrum_request_update ) {
/* Decimated buffer is full. Compute spectrum. */ /* Decimated buffer is full. Compute spectrum. */
std::array<std::complex<float>, 256> samples_swapped; std::array<std::complex<float>, channel_spectrum.size()> samples_swapped;
fft_swap(channel_spectrum, samples_swapped); fft_swap(channel_spectrum, samples_swapped);
channel_spectrum_request_update = false; channel_spectrum_request_update = false;
fft_c_preswapped(samples_swapped); fft_c_preswapped(samples_swapped);
if( spectrum_message.is_free() ) {
for(size_t i=0; i<spectrum_db.size(); i++) { ChannelSpectrumMessage spectrum_message;
for(size_t i=0; i<spectrum_message.spectrum.db.size(); i++) {
const auto mag2 = magnitude_squared(samples_swapped[i]); const auto mag2 = magnitude_squared(samples_swapped[i]);
const float db = complex16_mag_squared_to_dbv_norm(mag2); const float db = complex16_mag_squared_to_dbv_norm(mag2);
constexpr float mag_scale = 5.0f; constexpr float mag_scale = 5.0f;
const unsigned int v = (db * mag_scale) + 255.0f; const unsigned int v = (db * mag_scale) + 255.0f;
spectrum_db[i] = std::max(0U, std::min(255U, v)); spectrum_message.spectrum.db[i] = std::max(0U, std::min(255U, v));
} }
/* TODO: Rename .db -> .magnitude, or something more (less!) accurate. */ /* TODO: Rename .db -> .magnitude, or something more (less!) accurate. */
spectrum_message.spectrum.db = &spectrum_db; spectrum_message.spectrum.db_count = spectrum_message.spectrum.db.size();
spectrum_message.spectrum.db_count = spectrum_db.size();
spectrum_message.spectrum.sampling_rate = channel_spectrum_sampling_rate; spectrum_message.spectrum.sampling_rate = channel_spectrum_sampling_rate;
spectrum_message.spectrum.channel_filter_pass_frequency = channel_filter_pass_frequency; spectrum_message.spectrum.channel_filter_pass_frequency = channel_filter_pass_frequency;
spectrum_message.spectrum.channel_filter_stop_frequency = channel_filter_stop_frequency; spectrum_message.spectrum.channel_filter_stop_frequency = channel_filter_stop_frequency;
shared_memory.application_queue.push(&spectrum_message); shared_memory.application_queue.push(spectrum_message);
}
} }
} }
}; };

View File

@ -31,7 +31,9 @@
class Message { class Message {
public: public:
enum class ID : uint16_t { static constexpr size_t MAX_SIZE = 276;
enum class ID : uint32_t {
/* Assign consecutive IDs. IDs are used to index array. */ /* Assign consecutive IDs. IDs are used to index array. */
RSSIStatistics = 0, RSSIStatistics = 0,
BasebandStatistics = 1, BasebandStatistics = 1,
@ -47,22 +49,11 @@ public:
constexpr Message( constexpr Message(
ID id ID id
) : id { id }, ) : id { id }
state { State::Free }
{ {
} }
enum class State : uint16_t {
Free,
InUse,
};
bool is_free() const {
return state == State::Free;
}
const ID id; const ID id;
volatile State state;
}; };
struct RSSIStatistics { struct RSSIStatistics {
@ -183,7 +174,7 @@ public:
}; };
struct ChannelSpectrum { struct ChannelSpectrum {
std::array<uint8_t, 256>* db { nullptr }; std::array<uint8_t, 256> db { { 0 } };
size_t db_count { 256 }; size_t db_count { 256 };
uint32_t sampling_rate { 0 }; uint32_t sampling_rate { 0 };
uint32_t channel_filter_pass_frequency { 0 }; uint32_t channel_filter_pass_frequency { 0 };
@ -255,11 +246,13 @@ public:
} }
void send(const Message* const message) { void send(const Message* const message) {
if( message->id < Message::ID::MAX ) {
auto& fn = map_[toUType(message->id)]; auto& fn = map_[toUType(message->id)];
if( fn ) { if( fn ) {
fn(message); fn(message);
} }
} }
}
private: private:
using MapType = std::array<MessageHandler, toUType(Message::ID::MAX)>; using MapType = std::array<MessageHandler, toUType(Message::ID::MAX)>;

View File

@ -26,56 +26,26 @@
using namespace lpc43xx; using namespace lpc43xx;
bool MessageQueue::push(Message* const message) { bool MessageQueue::push(const void* const buf, const size_t len) {
/* Returns true if success: const auto result = fifo.in_r(buf, len);
* - Message not in use. const bool success = (result == len);
* - FIFO wasn't full. if( success ) {
*/
if( message->state == Message::State::Free ) {
message->state = Message::State::InUse;
if( enqueue(message) ) {
signal(); signal();
return true;
} else {
// Roll back message state.
message->state = Message::State::Free;
} }
return success;
} }
return false; size_t MessageQueue::pop(void* const buf, const size_t len) {
} return fifo.out_r(buf, len);
Message* MessageQueue::pop() {
/* TODO: Because of architecture characteristics, the two LSBs of the
* message pointer will always be 0. Other (non-pointer) message types
* could be encoded by setting these two bits to non-zero values.
* One of the bits could also be used as an "ack" flag... In fact, a
* pointer message could be turned into an "ack" message, or something
* like that...
* Might be better though to use formal operating structures in the
* message to do synchronization between processors.
*/
Message* message { nullptr };
const auto success = fifo.out(&message, 1);
return success ? message : nullptr;
} }
#if defined(LPC43XX_M0) #if defined(LPC43XX_M0)
bool MessageQueue::enqueue(Message* const message) {
return fifo.in(&message, 1);
}
void MessageQueue::signal() { void MessageQueue::signal() {
creg::m0apptxevent::assert(); creg::m0apptxevent::assert();
} }
#endif #endif
#if defined(LPC43XX_M4) #if defined(LPC43XX_M4)
bool MessageQueue::enqueue(Message* const message) {
return fifo.in(&message, 1);
}
void MessageQueue::signal() { void MessageQueue::signal() {
creg::m4txevent::assert(); creg::m4txevent::assert();
} }

View File

@ -29,9 +29,15 @@
class MessageQueue { class MessageQueue {
public: public:
bool push(Message* const message); template<typename T>
bool push(const T& message) {
static_assert(sizeof(T) <= Message::MAX_SIZE, "Message::MAX_SIZE too small for message type");
static_assert(std::is_base_of<Message, T>::value, "type is not based on Message");
Message* pop(); return push(&message, sizeof(message));
}
size_t pop(void* const buf, const size_t len);
size_t len() const { size_t len() const {
return fifo.len(); return fifo.len();
@ -42,9 +48,10 @@ public:
} }
private: private:
FIFO<Message*, 8> fifo; FIFO<uint8_t, 11> fifo;
bool push(const void* const buf, const size_t len);
bool enqueue(Message* const message);
void signal(); void signal();
}; };