Skip to content

Commit

Permalink
Make it interrupt safe
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanplusplus committed Dec 15, 2024
1 parent a6ba2f4 commit 4acaf1d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
3 changes: 2 additions & 1 deletion include/tiny_gea3_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ typedef struct {

uint8_t send_offset;
uint8_t send_data_length;
volatile bool send_in_progress;
bool send_in_progress;
volatile bool send_completed; // Set by ISR, cleared by background

uint8_t receive_buffer_size;
uint8_t receive_count;
Expand Down
30 changes: 20 additions & 10 deletions src/tiny_gea3_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ enum {
send_state_data,
send_state_crc_msb,
send_state_crc_lsb,
send_state_etx
send_state_etx,
send_state_complete
};

#define needs_escape(_byte) ((_byte & 0xFC) == tiny_gea_esc)
Expand Down Expand Up @@ -154,13 +155,6 @@ static void byte_sent(void* context, const void* args)
reinterpret(self, context, self_t*);
(void)args;

if(!self->send_in_progress) {
if(tiny_queue_count(&self->send_queue) > 0) {
begin_send(self);
}
return;
}

uint8_t byte_to_send = 0;

switch(self->send_state) {
Expand Down Expand Up @@ -228,10 +222,13 @@ static void byte_sent(void* context, const void* args)
break;

case send_state_etx:
tiny_queue_discard(&self->send_queue);
self->send_in_progress = false;
byte_to_send = tiny_gea_etx;
self->send_state = send_state_complete;
break;

case send_state_complete:
self->send_completed = true;
return;
}

tiny_uart_send(self->uart, byte_to_send);
Expand Down Expand Up @@ -344,6 +341,7 @@ void tiny_gea3_interface_init(
self->ignore_destination_address = ignore_destination_address;
self->receive_escaped = false;
self->send_in_progress = false;
self->send_completed = false;
self->send_escaped = false;
self->stx_received = false;
self->receive_packet_ready = false;
Expand All @@ -370,4 +368,16 @@ void tiny_gea3_interface_run(self_t* self)
// Can only be cleared _after_ publication so that the buffer isn't reused
self->receive_packet_ready = false;
}

if(self->send_completed) {
tiny_queue_discard(&self->send_queue);
self->send_completed = false;
self->send_in_progress = false;
}

if(!self->send_in_progress) {
if(tiny_queue_count(&self->send_queue) > 0) {
begin_send(self);
}
}
}
1 change: 1 addition & 0 deletions test/tests/tiny_gea3_interface_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ TEST(tiny_gea3_interface, should_queue_sent_packets)
tiny_gea_etx);

after_send_completes();
after_the_interface_is_run();
}

TEST(tiny_gea3_interface, should_report_failure_to_enqueue)
Expand Down

0 comments on commit 4acaf1d

Please sign in to comment.