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

@@ -31,7 +31,9 @@
class Message {
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. */
RSSIStatistics = 0,
BasebandStatistics = 1,
@@ -47,22 +49,11 @@ public:
constexpr Message(
ID id
) : id { id },
state { State::Free }
) : id { id }
{
}
enum class State : uint16_t {
Free,
InUse,
};
bool is_free() const {
return state == State::Free;
}
const ID id;
volatile State state;
};
struct RSSIStatistics {
@@ -183,7 +174,7 @@ public:
};
struct ChannelSpectrum {
std::array<uint8_t, 256>* db { nullptr };
std::array<uint8_t, 256> db { { 0 } };
size_t db_count { 256 };
uint32_t sampling_rate { 0 };
uint32_t channel_filter_pass_frequency { 0 };
@@ -255,9 +246,11 @@ public:
}
void send(const Message* const message) {
auto& fn = map_[toUType(message->id)];
if( fn ) {
fn(message);
if( message->id < Message::ID::MAX ) {
auto& fn = map_[toUType(message->id)];
if( fn ) {
fn(message);
}
}
}

View File

@@ -26,56 +26,26 @@
using namespace lpc43xx;
bool MessageQueue::push(Message* const message) {
/* Returns true if success:
* - Message not in use.
* - FIFO wasn't full.
*/
if( message->state == Message::State::Free ) {
message->state = Message::State::InUse;
if( enqueue(message) ) {
signal();
return true;
} else {
// Roll back message state.
message->state = Message::State::Free;
}
bool MessageQueue::push(const void* const buf, const size_t len) {
const auto result = fifo.in_r(buf, len);
const bool success = (result == len);
if( success ) {
signal();
}
return false;
return success;
}
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;
size_t MessageQueue::pop(void* const buf, const size_t len) {
return fifo.out_r(buf, len);
}
#if defined(LPC43XX_M0)
bool MessageQueue::enqueue(Message* const message) {
return fifo.in(&message, 1);
}
void MessageQueue::signal() {
creg::m0apptxevent::assert();
}
#endif
#if defined(LPC43XX_M4)
bool MessageQueue::enqueue(Message* const message) {
return fifo.in(&message, 1);
}
void MessageQueue::signal() {
creg::m4txevent::assert();
}

View File

@@ -29,9 +29,15 @@
class MessageQueue {
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 {
return fifo.len();
@@ -42,9 +48,10 @@ public:
}
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();
};