Skip to content

Commit 83bad6c

Browse files
committed
Centralize I2S mux slot-to-bit calculations
Add helper functions in i2s_constants.h for consistent bit position calculations across step, direction, and enable pins in I2S mux mode.
1 parent aa5cb84 commit 83bad6c

3 files changed

Lines changed: 21 additions & 8 deletions

File tree

src/pd_esp32/esp32_queue.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ StepperQueue* StepperQueue::tryAllocateQueue(FasDriver driver,
209209
StepperQueue* q = new StepperQueue();
210210
q->use_i2s = true;
211211
q->i2s_mgr = StepperQueue::_i2s_mux_manager;
212-
q->_i2s_mux_step_byte_offset = slot / 8;
213-
q->_i2s_mux_step_bit_mask = 1 << (7 - (slot % 8));
212+
q->_i2s_mux_step_byte_offset = i2s_mux_byte_offset(slot);
213+
q->_i2s_mux_step_bit_mask = i2s_mux_bit_mask(slot);
214214
return q;
215215
}
216216

src/pd_esp32/i2s_constants.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,22 @@
4343
#define I2S_DMA_DESC_NUM 2
4444
#define I2S_DMA_FRAME_NUM I2S_FRAMES_PER_BLOCK
4545

46+
// Convert I2S mux slot (0-31) to 32-bit bit position.
47+
// Matches byte/bit access: slot S → byte S/8, bit (7 - S%8)
48+
// slot 0 -> bit 7, slot 7 -> bit 0, slot 8 -> bit 15, slot 15 -> bit 8
49+
static inline uint8_t i2s_mux_slot_to_bit_pos(uint8_t slot) { return slot ^ 7; }
50+
51+
static inline uint8_t i2s_mux_byte_offset(uint8_t slot) {
52+
return i2s_mux_slot_to_bit_pos(slot) >> 3;
53+
}
54+
55+
static inline uint8_t i2s_mux_bit_mask(uint8_t slot) {
56+
return 1 << (i2s_mux_slot_to_bit_pos(slot) & 7);
57+
}
58+
59+
static inline uint32_t i2s_mux_bit_mask_u32(uint8_t slot) {
60+
return 1UL << i2s_mux_slot_to_bit_pos(slot);
61+
}
62+
4663
#endif // SUPPORT_ESP32_I2S
4764
#endif // PD_ESP32_I2S_CONSTANTS_H

src/pd_esp32/i2s_manager.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,7 @@ void I2sManager::i2sMuxSetBit(uint8_t slot, bool value) {
105105
if (slot >= 32) {
106106
return;
107107
}
108-
uint8_t byte_offset = slot / 8;
109-
uint8_t bit_within_byte = 7 - (slot % 8);
110-
uint32_t bit = 1UL << (byte_offset * 8 + bit_within_byte);
108+
uint32_t bit = i2s_mux_bit_mask_u32(slot);
111109
if (value) {
112110
_mux_state |= bit;
113111
} else {
@@ -119,9 +117,7 @@ bool I2sManager::i2sMuxGetBit(uint8_t slot) {
119117
if (slot >= 32) {
120118
return false;
121119
}
122-
uint8_t byte_offset = slot / 8;
123-
uint8_t bit_within_byte = 7 - (slot % 8);
124-
return (_mux_state & (1UL << (byte_offset * 8 + bit_within_byte))) != 0;
120+
return (_mux_state & i2s_mux_bit_mask_u32(slot)) != 0;
125121
}
126122

127123
void IRAM_ATTR I2sManager::handleTxDone(uint8_t* buf) {

0 commit comments

Comments
 (0)