Skip to content

Commit 2c3cd86

Browse files
committed
Optimize turnLed function.
The main optimization eliminates the double MODER register write when on=true. Previously: MODER &= ~(0xFFFFFF) then MODER |= pin_bits (two writes) Now: MODER = 0 when off, or MODER = pin_bits when on (single write each) Otherwise, the compiler is already doing a pretty good job. Additional improvements: - Remove unused idxToPin() function (was just returning idx) - Remove unused reverse() function - Use direct pin array access and bit shifts for efficiency Signed-off-by: iabdalkader <[email protected]>
1 parent d89881e commit 2c3cd86

File tree

1 file changed

+12
-24
lines changed

1 file changed

+12
-24
lines changed

loader/matrix.inc

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -112,38 +112,26 @@ static const uint8_t pins[][2] = {
112112
};
113113

114114

115-
const int idxToPin(int idx) {
116-
return idx;
117-
}
118-
119115
#define NUM_MATRIX_LEDS 104
120116
static uint8_t __attribute__((aligned)) framebuffer[NUM_MATRIX_LEDS / 8];
121117
static uint8_t __attribute__((aligned)) framebuffer_color[NUM_MATRIX_LEDS];
122118

123-
static void turnLed(int idx, bool on) {
124-
GPIOF->MODER &= ~(0xFFFFFF);
119+
static bool color = false;
120+
static uint8_t _max_grayscale_bits = 3;
125121

126-
if (on) {
127-
GPIOF->BSRR |= (1 << (idxToPin(pins[idx][0])) | 1 << (idxToPin(pins[idx][1]) + 16));
128-
GPIOF->MODER |= (1 << (idxToPin(pins[idx][0]) * 2) | 1 << (idxToPin(pins[idx][1]) * 2));
122+
static void turnLed(int idx, bool on) {
123+
if (!on) {
124+
GPIOF->MODER = 0U;
125+
} else {
126+
uint8_t pin0 = pins[idx][0];
127+
uint8_t pin1 = pins[idx][1];
128+
129+
GPIOF->BSRR |= (1U << pin0) | (1U << (pin1 + 16));
130+
GPIOF->MODER = (1U << (pin0 << 1)) | (1U << (pin1 << 1));
129131
}
130132
}
131133

132-
static uint32_t reverse(uint32_t x)
133-
{
134-
x = ((x >> 1) & 0x55555555u) | ((x & 0x55555555u) << 1);
135-
x = ((x >> 2) & 0x33333333u) | ((x & 0x33333333u) << 2);
136-
x = ((x >> 4) & 0x0f0f0f0fu) | ((x & 0x0f0f0f0fu) << 4);
137-
x = ((x >> 8) & 0x00ff00ffu) | ((x & 0x00ff00ffu) << 8);
138-
x = ((x >> 16) & 0xffffu) | ((x & 0xffffu) << 16);
139-
return x;
140-
}
141-
142-
static bool color = false;
143-
static uint8_t _max_grayscale_bits = 3;
144-
145-
static void timer_irq_handler_fn(const struct device *counter_dev, void *user_data)
146-
{
134+
static void timer_irq_handler_fn(const struct device *counter_dev, void *user_data) {
147135
static volatile int i_isr = 0;
148136
if (color) {
149137
static volatile int counter = 0;

0 commit comments

Comments
 (0)