Skip to content

Commit 048f0f0

Browse files
authored
Merge pull request adafruit#414 from tannewt/new_opt
Turn on -Os and tweak neopixel for new code output.
2 parents e2867eb + 182a946 commit 048f0f0

File tree

3 files changed

+19
-21
lines changed

3 files changed

+19
-21
lines changed

ports/atmel-samd/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ CFLAGS = -Os -DNDEBUG
8585
endif
8686

8787
ifeq ($(CHIP_FAMILY), samd51)
88-
CFLAGS = -O2 -DNDEBUG
88+
CFLAGS = -Os -DNDEBUG
8989
endif
9090

9191
#Debugging/Optimization

ports/atmel-samd/common-hal/neopixel_write/__init__.c

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,17 @@
3232
#include "tick.h"
3333

3434
#ifdef SAMD51
35-
static inline void delay_cycles(uint8_t cycles) {
36-
uint32_t start = SysTick->VAL;
37-
uint32_t stop = start - cycles;
38-
if (start < cycles) {
39-
stop = 0xffffff + start - cycles;
40-
while (SysTick->VAL < start || SysTick->VAL > stop) {}
41-
} else {
42-
// Make sure the systick value is between start and stop in case it
43-
// wraps around before we read its value less than stop.
44-
while (SysTick->VAL > stop && SysTick->VAL <= start) {}
35+
// This magical macro makes sure the delay isn't optimized out and is the
36+
// minimal three instructions.
37+
#define delay_cycles(cycles) \
38+
{ \
39+
uint32_t t; \
40+
asm volatile ( \
41+
"movs %[t], %[c]\n\t" \
42+
"loop%=:\n\t" \
43+
"subs %[t], #1\n\t" \
44+
"bne.n loop%=" : [t] "=r"(t) : [c] "I" (cycles)); \
4545
}
46-
}
4746
#endif
4847

4948
uint64_t next_start_tick_ms = 0;
@@ -88,7 +87,7 @@ void common_hal_neopixel_write(const digitalio_digitalinout_obj_t* digitalinout,
8887
asm("nop; nop;");
8988
#endif
9089
#ifdef SAMD51
91-
delay_cycles(18);
90+
delay_cycles(3);
9291
#endif
9392
if(p & bitMask) {
9493
// This is the high delay unique to a one bit.
@@ -97,7 +96,7 @@ void common_hal_neopixel_write(const digitalio_digitalinout_obj_t* digitalinout,
9796
asm("nop; nop; nop; nop; nop; nop; nop;");
9897
#endif
9998
#ifdef SAMD51
100-
delay_cycles(25);
99+
delay_cycles(11);
101100
#endif
102101
*clr = pinMask;
103102
} else {
@@ -108,7 +107,7 @@ void common_hal_neopixel_write(const digitalio_digitalinout_obj_t* digitalinout,
108107
asm("nop; nop;");
109108
#endif
110109
#ifdef SAMD51
111-
delay_cycles(25);
110+
delay_cycles(3);
112111
#endif
113112
}
114113
if((bitMask >>= 1) != 0) {
@@ -119,7 +118,7 @@ void common_hal_neopixel_write(const digitalio_digitalinout_obj_t* digitalinout,
119118
asm("nop; nop; nop; nop; nop;");
120119
#endif
121120
#ifdef SAMD51
122-
delay_cycles(44);
121+
delay_cycles(20);
123122
#endif
124123
} else {
125124
if(ptr >= end) break;
@@ -130,7 +129,7 @@ void common_hal_neopixel_write(const digitalio_digitalinout_obj_t* digitalinout,
130129
// above operations take.
131130
// For the SK6812 its 0.6us +- 0.15us
132131
#ifdef SAMD51
133-
delay_cycles(50);
132+
delay_cycles(15);
134133
#endif
135134
}
136135
}

ports/atmel-samd/usb_mass_storage.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,9 @@ int32_t usb_msc_xfer_done(uint8_t lun) {
259259
if (active_read) {
260260
active_addr += 1;
261261
active_nblocks--;
262+
if (active_nblocks == 0) {
263+
active_read = false;
264+
}
262265
}
263266

264267
if (active_write) {
@@ -272,10 +275,6 @@ int32_t usb_msc_xfer_done(uint8_t lun) {
272275
// The start_read callback begins a read transaction which we accept but delay our response until the "main thread" calls usb_msc_background. Once it does, we read immediately from the drive into our cache and trigger the USB DMA to output the sector. Once the sector is transmitted, xfer_done will be called.
273276
void usb_msc_background(void) {
274277
if (active_read && !usb_busy) {
275-
if (active_nblocks == 0) {
276-
active_read = false;
277-
return;
278-
}
279278
fs_user_mount_t * vfs = get_vfs(active_lun);
280279
disk_read(vfs, sector_buffer, active_addr, 1);
281280
// TODO(tannewt): Check the read result.

0 commit comments

Comments
 (0)