Archive des codes : longan_leds.tar

led_rouge, led_verte, led_bleue, led_blanche

Le longan Nano est un Risc V 32

Installation de base :

# Compilateur RISC-V et outils
sudo apt update
sudo apt install gcc-riscv64-unknown-elf  # Le compilateur principal
sudo apt install binutils-riscv64-unknown-elf  # Outils supplémentaires
# Outils généraux
sudo apt install build-essential  # make, gcc, etc.
sudo apt install make             # Si pas déjà installé
sudo apt install git              # Pour cloner des projets

# Pour flasher le Longan Nano
sudo apt install dfu-util         # Outil de flash DFU

# Outils d'analyse
sudo apt install binutils         # objdump, objcopy, etc.
#en résumé !
sudo apt update && sudo apt install -y \
    gcc-riscv64-unknown-elf \
    binutils-riscv64-unknown-elf \
    build-essential \
    make \
    dfu-util \
    git \
    device-tree-compiler
# Ajoute ton utilisateur aux groupes nécessaires
sudo usermod -a -G dialout $USER
sudo usermod -a -G plugdev $USER

# Déconnecte-toi et reconnecte-toi pour que les changements prennent effet
# Ou exécute :
newgrp dialout

led_blink.c

/* led_blink_simple.c - Ça marche ! */

int main(void) {
    /* Active GPIOC */
    *(volatile unsigned int*)0x40021018 |= (1 << 4);
    
    /* Configure PC13 */
    volatile unsigned int *CRH = (volatile unsigned int*)0x40011004;
    *CRH = (*CRH & ~(0xF << 20)) | (0x1 << 20);
    
    volatile unsigned int *ODR = (volatile unsigned int*)0x4001100C;
    
    while(1) {
        /* LED ON */
        *ODR &= ~(1 << 13);
        
        /* Délai */
        for(volatile unsigned int i = 0; i < 1000000; i++);
        
        /* LED OFF */
        *ODR |= (1 << 13);
        
        /* Délai */
        for(volatile unsigned int i = 0; i < 1000000; i++);
    }
}

void _start(void) {
    asm volatile("la sp, 0x20005000");
    main();
    while(1);
}

gd32vf103.lds

/* Fichier de liaison pour Longan Nano */

ENTRY(_start)

MEMORY {
    FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 64K
    RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 20K
}

SECTIONS {
    .text : {
        *(.text._start)
        *(.text)
        *(.text.*)
    } > FLASH
}

Makefille

PROGRAM = led_blink

CC = riscv64-unknown-elf-gcc
OBJCOPY = riscv64-unknown-elf-objcopy

# IMPORTANT: O0 désactive les optimisations pour préserver les boucles de délai
CFLAGS = -march=rv32imac -mabi=ilp32 -nostdlib -O0 -Wall
LDFLAGS = -T gd32vf103.lds -Wl,--gc-sections

all: $(PROGRAM).bin

$(PROGRAM).elf: $(PROGRAM).c gd32vf103.lds
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $<

$(PROGRAM).bin: $(PROGRAM).elf
	$(OBJCOPY) -O binary $< $@
	@echo "✅ $(PROGRAM).bin créé - Taille: $$(stat -c%s $@) octets"
	@echo "🔍 Vérification:"
	@riscv64-unknown-elf-size $<

clean:
	rm -f $(PROGRAM).elf $(PROGRAM).bin

flash: $(PROGRAM).bin
	@echo "🔌 Mode DFU: BOOT+RESET"
	@echo "⚡ Flash..."
	dfu-util -d 28e9:0189 -a 0 --dfuse-address 0x08000000:leave -D $(PROGRAM).bin

# Pour tester rapidement
test: clean all flash
	@echo "👁️  La LED rouge devrait clignoter!"

.PHONY: all clean flash test