Skip to content

Commit ef40fff

Browse files
committed
Remove complex bus state logic
1 parent bc5bc43 commit ef40fff

3 files changed

Lines changed: 26 additions & 36 deletions

File tree

firmware/libsi/include/si/device/commands.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ struct si_command *si_command_find_by_id(uint8_t command);
4949
* Process a single SI command on the bus.
5050
*
5151
* This will read a command from the SI bus and call the registered handler.
52+
*
53+
* @param await_bus_idle if true, will wait for the SI bus to be idle before reading commands
5254
*/
53-
void si_command_process();
55+
void si_command_process(bool await_bus_idle);
5456

5557
/**
5658
* Enable automatic command processing.

firmware/libsi/src/device/commands.c

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,15 @@
22

33
#define COMMAND_TABLE_SIZE 18
44

5-
enum {
6-
BUS_STATE_UNKNOWN = 0,
7-
BUS_STATE_IDLE,
8-
BUS_STATE_BUSY,
9-
BUS_STATE_ERROR,
10-
};
11-
125
// Command table for registered SI commands
136
static struct si_command command_table[COMMAND_TABLE_SIZE] = {0};
147
static struct si_command *current_command = NULL;
158

16-
// Current bus state
17-
static uint8_t bus_state = BUS_STATE_UNKNOWN;
18-
199
// Buffer for reading/writing commands
2010
static uint8_t command_buffer[SI_BLOCK_SIZE];
2111

2212
// Automatically transition back to reading commands
23-
static bool auto_tx_rx_transition = false;
13+
static bool command_processing_enabled = false;
2414

2515
// Vaguely context-aware hash function
2616
// We're hashing to reduce memory usage while keeping O(1) lookup time in most cases
@@ -39,12 +29,10 @@ static inline uint8_t hash_command(uint8_t command)
3929
// Command handler TX completion callback
4030
static void on_tx_complete(int result)
4131
{
42-
// Update bus state based on result
43-
bus_state = (result < 0) ? BUS_STATE_ERROR : BUS_STATE_IDLE;
44-
45-
// Transition back to RX mode if auto transition is enabled
46-
if (auto_tx_rx_transition)
47-
si_command_process();
32+
// Begin listening for the next command if command processing is enabled
33+
// Wait for bus idle if there was an error
34+
if (command_processing_enabled)
35+
si_command_process(result < 0);
4836
}
4937

5038
// Command handler RX completion callback
@@ -56,12 +44,9 @@ static void on_rx_complete(int result)
5644
return;
5745
}
5846

59-
// Otherwise, there was either an error during the read, or handler not found
60-
bus_state = BUS_STATE_ERROR;
61-
62-
// Transition back to RX mode if auto transition is enabled
63-
if (auto_tx_rx_transition)
64-
si_command_process();
47+
// Begin listening for the next command if command processing is enabled
48+
if (command_processing_enabled)
49+
si_command_process(true);
6550
}
6651

6752
static bool command_byte_cb(uint8_t byte, uint8_t byte_index)
@@ -113,32 +98,33 @@ struct si_command *si_command_find_by_id(uint8_t command)
11398
return NULL;
11499
}
115100

116-
void si_command_process()
101+
void si_command_process(bool await_bus_idle)
117102
{
118-
if (bus_state != BUS_STATE_IDLE)
103+
// Await for the bus to be idle if requested
104+
if (await_bus_idle)
119105
si_await_bus_idle();
120106

121-
// Initialize command reading context
122-
current_command = NULL;
123-
124-
bus_state = BUS_STATE_BUSY;
107+
// Begin reading the next command from the SI bus
125108
si_read_bytes(command_buffer, SI_BLOCK_SIZE, command_byte_cb, on_rx_complete);
126109
}
127110

128111
void si_command_processing_enable()
129112
{
130-
if (auto_tx_rx_transition)
113+
if (command_processing_enabled)
131114
return;
132115

133-
auto_tx_rx_transition = true;
134-
si_command_process();
116+
// Mark command processing as enabled
117+
command_processing_enabled = true;
118+
119+
// Start processing commands, waiting for the bus to be idle
120+
si_command_process(true);
135121
}
136122

137123
void si_command_processing_disable()
138124
{
139-
if (!auto_tx_rx_transition)
125+
if (!command_processing_enabled)
140126
return;
141127

142-
auto_tx_rx_transition = false;
143-
bus_state = BUS_STATE_UNKNOWN;
128+
// Mark command processing as disabled
129+
command_processing_enabled = false;
144130
}

firmware/libsi/src/platform/efr32/si_efr32_emlib.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,15 @@ void si_await_bus_idle(void)
197197

198198
while (1) {
199199
// Wait for the line to go high
200+
// TODO: Add a timeout
200201
while (GPIO_PinInGet(si_data_port, si_data_pin) == 0)
201202
;
202203

203204
// Start timing the bus idle period
204205
TIMER_CounterSet(SI_RX_TIMER, 0);
205206

206207
// Wait for either the bus idle period to elapse or line to go low
208+
// TODO: Add a timeout
207209
while (GPIO_PinInGet(si_data_port, si_data_pin) == 1) {
208210
if (TIMER_CounterGet(SI_RX_TIMER) >= rx_bus_idle_period)
209211
goto idle_detected;

0 commit comments

Comments
 (0)