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
136static struct si_command command_table [COMMAND_TABLE_SIZE ] = {0 };
147static 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
2010static 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
4030static 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
6752static 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
128111void 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
137123void 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}
0 commit comments