代码之家  ›  专栏  ›  技术社区  ›  Maaz Ali

如何使用clang为arduino正确编译C代码。(超时问题)

  •  0
  • Maaz Ali  · 技术社区  · 2 年前

    我正在尝试为atmega328p编译一些简单的裸机C代码。我正在使用arduino网络上传器进行上传 https://github.com/dbuezas/arduino-web-uploader/tree/master

    这是c代码

    // This blinks an led
    int main() {
            *((volatile unsigned char*)0x24) = 0xFF;
            while (1) {
                    *((volatile unsigned char*)0x25) = 0x00;
                    for (unsigned int i1 = 0;i1 < 65535;i1++)
                    for (unsigned int i2 = 0;i2 < 65535;i2++)
                    for (unsigned int i3 = 0;i3 < 65535;i3++)
                    for (unsigned int i4 = 0;i4 < 65535;i4++)
                            asm volatile("nop");
                    *((volatile unsigned char*)0x25) = 0xFF;
                    for (unsigned int i1 = 0;i1 < 65535;i1++)
                    for (unsigned int i2 = 0;i2 < 65535;i2++)
                    for (unsigned int i3 = 0;i3 < 65535;i3++)
                    for (unsigned int i4 = 0;i4 < 65535;i4++)
                            asm volatile("nop");
            }
    }
    
    
    

    问题是我弄错了 Error: Sending 6400004620: receiveData timeout after 400ms 我已经用一些示例.hex文件测试了它,它似乎可以工作,但每当我自己编译时,我都会收到他的错误。所以我的Makefile一定有错误

    # The microcontroller
    MCU := atmega328p
    # The clock speed in Hz.
    CLOCK := 16000000
    
    CC  := clang
    LLC := llc
    MC  := llvm-mc
    
    CFLAGS := -O -nostdlib -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -flto --target=avr -mmcu=$(MCU) -DF_CPU=$(CLOCK) -DARDUINO=10607 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR
    
    LLCFLAGS := -march=avr -mcpu=$(MCU)
    
    MCFLAGS := -arch=avr -mcpu=$(MCU)
    
    C_SOURCES := $(shell find -name "*.c")
    
    GENERATED_FILES :=
    
    # if there are C sources, use them, otherwise search for IR files
    ifneq "$(strip $(C_SOURCES))" ""
    IR_FILES := $(patsubst %.c,%.ll,$(C_SOURCES))
    GENERATED_FILES += $(IR_FILES)
    else
    IR_FILES := $(shell find -name "*.ll")
    endif
    
    # if there are IR files, use them, otherwise search of asm files
    ifneq "$(strip $(IR_FILES))" ""
    ASM_FILES := $(patsubst %.ll,%.s,$(IR_FILES))
    GENERATED_FILES += $(ASM_FILES)
    else
    ASM_FILES := $(shell find -name "*.s")
    endif
    
    # if there are asm files, use them, otherwise search for obj files
    # NOTE: we do not handle object files right now because the asm parser has a few bugs
    ifneq "$(strip $(ASM_FILES))" ""
    OBJ_FILES := $(patsubst %.s,%.o,$(ASM_FILES))
    GENERATED_FILES += $(OBJ_FILES)
    else
    #OBJ_FILES := $(shell find -name "*.o")
    endif
    
    all: $(IR_FILES) $(ASM_FILES) $(OBJ_FILES) main.hex
    
    rebuild: clean all
    
    clean:
            rm -rf $(GENERATED_FILES)
    
    %.ll: %.c
            $(CC) -c -S -emit-llvm $(CFLAGS) $< -o $@
    
    %.s: %.ll
            $(LLC) -filetype=asm $(LLCFLAGS) $< -o $@
    
    %.o: %.s
            $(MC) -filetype=obj $(MCFLAGS) $< -o $@
    
    %.hex: $(OBJ_FILES)
            llvm-objcopy -O ihex $< $@ -B avr
    

    它产生了一个main.hex,但上传似乎不起作用,顺便说一句,这里是.hex文件

    :100000008FEF84B920E030E015B8F901AF01F901B4
    :10001000BF01F901D90100001196AF3FB80701F403
    :100020003196EF3FF80701F4FB013196EF3FF807F7
    :1000300001F4FA013196EF3FF80701F485B9F901AF
    :10004000AF01F901BF01F901D90100001196AF3FDD
    :10005000B80701F43196EF3FF80701F4FB01319640
    :10006000EF3FF80701F4FA013196EF3FF80701F48A
    :0200700000C0CE
    :00000001FF
    

    我在makefile中做错了什么?

    0 回复  |  直到 2 年前