fixed -flto problem (usb_dev.c needs -fno-lto)

This commit is contained in:
Edward Emelianov
2026-07-23 21:15:11 +03:00
parent 8252d370a2
commit 9fb0956dcf
7 changed files with 37 additions and 31 deletions

View File

@@ -35,7 +35,7 @@ int main(void){
gpio_setup(); gpio_setup();
usart_setup(115200); usart_setup(115200);
USB_setup(); USB_setup();
bool pressed = false; uint8_t pressed = 0;
uint32_t Tblink = 0, Tkey = 0; // blink and key pressed time uint32_t Tblink = 0, Tkey = 0; // blink and key pressed time
while(1){ while(1){
USART_flags_t f = usart_process(); USART_flags_t f = usart_process();
@@ -56,7 +56,7 @@ int main(void){
} }
if(KEY_PRESSED()){ if(KEY_PRESSED()){
if(!pressed){ // new event if(!pressed){ // new event
pressed = true; pressed = 1;
usart_sendstr(spressed); usart_sendstr(spressed);
USB_putbyte('1'); USB_putbyte('1'); newline(); USB_putbyte('1'); USB_putbyte('1'); newline();
USB_sendstr(spressed); USB_sendstr(spressed);
@@ -64,7 +64,7 @@ int main(void){
Tkey = Tms; Tkey = Tms;
}else{ // key released }else{ // key released
if(pressed && (Tms - Tkey) > TDEBOUNCE){ // wait for debounce if(pressed && (Tms - Tkey) > TDEBOUNCE){ // wait for debounce
pressed = false; pressed = 0;
usart_sendstr(sreleased); usart_sendstr(sreleased);
USB_putbyte('0'); USB_putbyte('0'); newline(); USB_putbyte('0'); USB_putbyte('0'); newline();
USB_sendstr(sreleased); USB_sendstr(sreleased);

View File

@@ -31,9 +31,9 @@ int RB_datalen(ringbuffer *b){
CHK(b); CHK(b);
if(0 == datalen(b)) return 0; // don't block for empty RO operations if(0 == datalen(b)) return 0; // don't block for empty RO operations
if(b->busy) return -1; if(b->busy) return -1;
b->busy = true; b->busy = 1;
int l = datalen(b); int l = datalen(b);
b->busy = false; b->busy = 0;
return l; return l;
} }
@@ -59,9 +59,9 @@ static int hasbyte(ringbuffer *b, uint8_t byte){
int RB_hasbyte(ringbuffer *b, uint8_t byte){ int RB_hasbyte(ringbuffer *b, uint8_t byte){
CHK(b); CHK(b);
if(b->busy) return -1; if(b->busy) return -1;
b->busy = true; b->busy = 1;
int ret = hasbyte(b, byte); int ret = hasbyte(b, byte);
b->busy = false; b->busy = 0;
return ret; return ret;
} }
@@ -100,9 +100,9 @@ int RB_read(ringbuffer *b, uint8_t *s, int len){
if(!s || len < 1) return -1; if(!s || len < 1) return -1;
if(0 == datalen(b)) return 0; if(0 == datalen(b)) return 0;
if(b->busy) return -1; if(b->busy) return -1;
b->busy = true; b->busy = 1;
int r = read(b, s, len); int r = read(b, s, len);
b->busy = false; b->busy = 0;
return r; return r;
} }
@@ -136,14 +136,14 @@ int RB_readto(ringbuffer *b, uint8_t byte, uint8_t *s, int len){
if(!s || len < 1) return -1; if(!s || len < 1) return -1;
if(0 == datalen(b)) return 0; if(0 == datalen(b)) return 0;
if(b->busy) return -1; if(b->busy) return -1;
b->busy = true; b->busy = 1;
int n = 0; int n = 0;
if(s && len > 0){ if(s && len > 0){
n = readto(b, byte, s, len); n = readto(b, byte, s, len);
}else{ }else{
incr(b, &b->head, lento(b, byte)); // just throw data out incr(b, &b->head, lento(b, byte)); // just throw data out
} }
b->busy = false; b->busy = 0;
return n; return n;
} }
@@ -151,9 +151,9 @@ int RB_datalento(ringbuffer *b, uint8_t byte){
CHK(b); CHK(b);
if(0 == datalen(b)) return 0; if(0 == datalen(b)) return 0;
if(b->busy) return -1; if(b->busy) return -1;
b->busy = true; b->busy = 1;
int n = lento(b, byte); int n = lento(b, byte);
b->busy = false; b->busy = 0;
return n; return n;
} }
@@ -184,9 +184,9 @@ int RB_write(ringbuffer *b, const uint8_t *str, int l){
if(!str || l < 1) return -1; if(!str || l < 1) return -1;
if(b->length - datalen(b) < 2) return 0; if(b->length - datalen(b) < 2) return 0;
if(b->busy) return -1; if(b->busy) return -1;
b->busy = true; b->busy = 1;
int w = write(b, str, l); int w = write(b, str, l);
b->busy = false; b->busy = 0;
return w; return w;
} }
@@ -194,10 +194,10 @@ int RB_write(ringbuffer *b, const uint8_t *str, int l){
int RB_clearbuf(ringbuffer *b){ int RB_clearbuf(ringbuffer *b){
CHK(b); CHK(b);
if(b->busy) return -1; if(b->busy) return -1;
b->busy = true; b->busy = 1;
b->head = 0; b->head = 0;
b->tail = 0; b->tail = 0;
memset(b->data, 0, b->length); memset(b->data, 0, b->length);
b->busy = false; b->busy = 0;
return 1; return 1;
} }

View File

@@ -24,7 +24,7 @@ typedef struct{
const int length; // its length const int length; // its length
volatile int head; // head index volatile int head; // head index
volatile int tail; // tail index volatile int tail; // tail index
volatile bool busy; // == TRUE if buffer is busy now volatile uint8_t busy; // == TRUE if buffer is busy now
} ringbuffer; } ringbuffer;
int RB_read(ringbuffer *b, uint8_t *s, int len); int RB_read(ringbuffer *b, uint8_t *s, int len);

View File

@@ -65,8 +65,8 @@ void dma1_channel2_isr() __attribute__ ((alias ("dmatx_isr")));
#define DMARXCCR (DMA_CCR_MINC | DMA_CCR_CIRC | DMA_CCR_TEIE) #define DMARXCCR (DMA_CCR_MINC | DMA_CCR_CIRC | DMA_CCR_TEIE)
#define DMATXCCR (DMA_CCR_MINC | DMA_CCR_DIR | DMA_CCR_TCIE | DMA_CCR_TEIE) #define DMATXCCR (DMA_CCR_MINC | DMA_CCR_DIR | DMA_CCR_TCIE | DMA_CCR_TEIE)
static volatile bool gotstring = true; // got '\n' in input stream -> force data reading to inbuf static volatile uint8_t gotstring = 1; // got '\n' in input stream -> force data reading to inbuf
static volatile bool txrdy = true; // Tx DMA not busy static volatile uint8_t txrdy = 1; // Tx DMA not busy
static volatile USART_flags_t curflags; // current flags (cleared in `usart_process`) static volatile USART_flags_t curflags; // current flags (cleared in `usart_process`)
// rx/tx DMA buffers // rx/tx DMA buffers
static uint8_t dmarxbuf[USARTRXDMABUFSZ]; static uint8_t dmarxbuf[USARTRXDMABUFSZ];
@@ -123,19 +123,19 @@ void usart_setup(uint32_t speed){
* @brief usart_sendbuf - send next data portion * @brief usart_sendbuf - send next data portion
* @return true if sent something * @return true if sent something
*/ */
static bool usart_sendbuf(){ static uint8_t usart_sendbuf(){
if(!txrdy) return false; if(!txrdy) return 0;
int rd = RB_read(&TxRB, dmatxbuf, USARTTXDMABUFSZ); int rd = RB_read(&TxRB, dmatxbuf, USARTTXDMABUFSZ);
if(rd < 1) return false; // nothing to write or busy if(rd < 1) return 0; // nothing to write or busy
// set up DMA // set up DMA
DMACHTX->CCR = DMATXCCR; DMACHTX->CCR = DMATXCCR;
DMACHTX->CMAR = (uint32_t) dmatxbuf; DMACHTX->CMAR = (uint32_t) dmatxbuf;
DMACHTX->CNDTR = rd; DMACHTX->CNDTR = rd;
USARTx->ICR = USART_ICR_TCCF; // clear TC flag USARTx->ICR = USART_ICR_TCCF; // clear TC flag
txrdy = false; txrdy = 0;
// activate DMA // activate DMA
DMACHTX->CCR = DMATXCCR | DMA_CCR_EN; DMACHTX->CCR = DMATXCCR | DMA_CCR_EN;
return true; return 1;
} }
int usart_send(const char *str, int len){ int usart_send(const char *str, int len){
@@ -235,7 +235,7 @@ char *usart_getline(){
// interrupt by '\n' // interrupt by '\n'
static void usart_isr(){ static void usart_isr(){
if(USARTx->ISR & USART_ISR_CMF){ // got '\n' @ USARTx if(USARTx->ISR & USART_ISR_CMF){ // got '\n' @ USARTx
gotstring = true; gotstring = 1;
} }
USARTx->ICR = 0xffffffff; // clear all flags USARTx->ICR = 0xffffffff; // clear all flags
} }
@@ -251,7 +251,7 @@ static void dmarx_isr(){
static void dmatx_isr(){ static void dmatx_isr(){
volatile uint32_t isr = DMAx->ISR; volatile uint32_t isr = DMAx->ISR;
if(isr & DMATXTCF) txrdy = true; if(isr & DMATXTCF) txrdy = 1;
if(isr & DMATXEF) curflags.txerr = 1; if(isr & DMATXEF) curflags.txerr = 1;
DMAx->IFCR = DMATXTCF | DMATXEF; // clear all flags DMAx->IFCR = DMATXTCF | DMATXEF; // clear all flags
} }

View File

@@ -21,6 +21,8 @@
#include "usb_descr.h" #include "usb_descr.h"
#include "usb_dev.h" #include "usb_dev.h"
//#pragma GCC optimize ("O1")
// Class-Specific Control Requests // Class-Specific Control Requests
#define SEND_ENCAPSULATED_COMMAND 0x00 // unused #define SEND_ENCAPSULATED_COMMAND 0x00 // unused
#define GET_ENCAPSULATED_RESPONSE 0x01 // unused #define GET_ENCAPSULATED_RESPONSE 0x01 // unused

Binary file not shown.

View File

@@ -49,7 +49,7 @@ LIB_DIR := $(INC_DIR)/ld
############################################################################### ###############################################################################
# C flags # C flags
CFLAGS += -D__thumb2__=1 -MD CFLAGS += -D__thumb2__=1 -MD
CFLAGS += -Wall -Wextra -Wshadow -Wdouble-promotion CFLAGS += -Wall -Wextra -Wshadow -Wdouble-promotion
CFLAGS += -fshort-enums -ffunction-sections -fdata-sections CFLAGS += -fshort-enums -ffunction-sections -fdata-sections
#CFLAGS += -fno-common -fno-stack-protector #CFLAGS += -fno-common -fno-stack-protector
@@ -57,12 +57,12 @@ CFLAGS += $(ARCH_FLAGS)
############################################################################### ###############################################################################
# Linker flags # Linker flags
LDFLAGS += -nostartfiles --static -specs=nosys.specs -specs=nano.specs LDFLAGS += -nostartfiles --static -specs=nano.specs
LDFLAGS += $(ARCH_FLAGS) LDFLAGS += $(ARCH_FLAGS)
LDFLAGS += -L$(LIB_DIR) LDFLAGS += -L$(LIB_DIR)
#-L$(TOOLCHLIB) #-L$(TOOLCHLIB)
LDFLAGS += -T$(LDSCRIPT) LDFLAGS += -T$(LDSCRIPT)
LDFLAGS += -Wl,-Map=$(MAP),--cref -Wl,--gc-sections -Wl,--print-memory-usage LDFLAGS += -Wl,-Map=$(MAP),--cref -Wl,--gc-sections -Wl,--print-memory-usage -Wl,--trace-symbol=_sbrk
############################################################################### ###############################################################################
# Used libraries # Used libraries
@@ -102,6 +102,8 @@ debug: CFLAGS += -O0 -DEBUG -Werror -g3 -gdwarf-2
debug: TARGET := DEBUG debug: TARGET := DEBUG
debug: $(TARGFILE) bin list size debug: $(TARGFILE) bin list size
all: $(BIN)
$(TARGFILE): $(OBJDIR) $(TARGFILE): $(OBJDIR)
@echo -e "\t\tTARGET: $(TARGET)" @echo -e "\t\tTARGET: $(TARGET)"
@echo "$(TARGET)" > $(TARGFILE) @echo "$(TARGET)" > $(TARGFILE)
@@ -130,13 +132,15 @@ $(VERSION_FILE): *.[ch]
$(OBJDIR)/proto.o: proto.c $(VERSION_FILE) $(OBJDIR)/proto.o: proto.c $(VERSION_FILE)
$(OBJDIR)/usb_dev.o: CFLAGS := $(filter-out -flto,$(CFLAGS)) -fno-lto
$(OBJDIR)/%.o: %.c $(OBJDIR)/%.o: %.c
@echo " CC $<" @echo " CC $<"
$(CC) $(CFLAGS) $(DEFS) $(INCLUDE) -o $@ -c $< $(CC) $(CFLAGS) $(DEFS) $(INCLUDE) -o $@ -c $<
$(OBJDIR)/%.o: %.cpp $(OBJDIR)/%.o: %.cpp
@echo " C++ $<" @echo " C++ $<"
$(CPP) -c $(CFLAGS) -fno-exceptions $(DEFS) $(INCLUDE) -o $@ -c $< $(CPP) -c $(CFLAGS) -std=c++11 -fno-exceptions -fno-rtti -Wl,--trace-symbol=_sbrk $(DEFS) $(INCLUDE) -o $@ -c $<
$(BIN): $(ELF) $(BIN): $(ELF)
@echo " OBJCOPY $(BIN)" @echo " OBJCOPY $(BIN)"