olikraus / u8g2

U8glib library for monochrome displays, version 2
Other
4.91k stars 1.03k forks source link

Issue setting up u8g2 on non arduino avr atmega328P with st7920 128x64 #2383

Open awu30 opened 4 months ago

awu30 commented 4 months ago

Hi, i am currently working on a project using avr atmega328p with st7920 128x64. Right now im trying to configure the lcd module and get it to display something. I managed to setup a makefile and a simple main.c file to get started but im experiencing an issue surround the font library:

This is my C file

include

include

include

include

include "u8x8.h"

include <util/delay.h>

include <avr/power.h>

include <avr/io.h>

define CS_DDR DDRB

define CS_PORT PORTB

define CS_BIT 2

define RESET_DDR DDRB

define RESET_PORT PORTB

define RESET_BIT 0

u8x8_t u8x8;

uint8_t u8x8_gpio_and_delay(u8x8_t u8x8, uint8_t msg, uint8_t arg_int, void arg_ptr);

uint8_t u8x8_gpio_and_delay (u8x8_t u8x8, uint8_t msg, uint8_t arg_int, void arg_ptr) { switch (msg) { case U8X8_MSG_GPIO_AND_DELAY_INIT: CS_DDR |= _BV(CS_BIT); RESET_DDR |= _BV(RESET_BIT); break; case U8X8_MSG_GPIO_CS: if (arg_int) CS_PORT |= _BV(CS_BIT); else CS_PORT &= ~_BV(CS_BIT); break; case U8X8_MSG_GPIO_RESET: if (arg_int) RESET_PORT |= _BV(RESET_BIT); else RESET_PORT &= ~_BV(RESET_BIT); break; case U8X8_MSG_DELAY_MILLI: // Using switch to work around the compile-time constant requirement switch (arg_int) { case 10: _delay_ms(10); break; case 100: _delay_ms(100); break; // You can add more cases as needed } break; // Handle more cases as needed for your specific hardware setup } return 1; // Command processed successfully. }

int main(void) { u8x8_Setup(&u8x8, u8x8_d_st7920_128x64, u8x8_cad_st7920_spi, u8x8_byte_3wire_sw_spi, u8x8_gpio_and_delay); u8x8_InitDisplay(&u8x8); u8x8_SetPowerSave(&u8x8, 0); u8x8_SetFont(&u8x8, u8x8_font_chroma48medium8_r); u8x8_DrawString(&u8x8, 0, 0, "Hello, u8x8!");

while (1) {
}

return 0;

}

This is my makefile:

Project Name

PROJECT = my_project

U8g2 Source Files

U8G2_SRC = /Users/arthurwu/u8g2/csrc/u8x8_cad.c \ /Users/arthurwu/u8g2/csrc/u8x8_display.c \ /Users/arthurwu/u8g2/csrc/u8x8_byte.c \ /Users/arthurwu/u8g2/csrc/u8x8_gpio.c \ /Users/arthurwu/u8g2/csrc/u8x8_setup.c \ /Users/arthurwu/u8g2/csrc/u8x8_d_st7920.c \ /Users/arthurwu/u8g2/sys/avr/avr-libc/lib/u8x8_avr.c \ /Users/arthurwu/u8g2/csrc/u8x8_8x8.c \

/Users/arthurwu/u8g2/tools/font/build/single_font_files/u8x8_font_chroma48medium8_r.c\

Source Files

SRC = main.c $(U8G2_SRC)

Object Files

OBJ = $(SRC:.c=.o)

Microcontroller Settings

MCU = atmega328p F_CPU = 8000000UL

Programmer Settings

PROGRAMMER_TYPE = avrisp2

Compiler Settings

CC = avr-gcc OBJCOPY = avr-objcopy CFLAGS = -mmcu=$(MCU) -DF_CPU=$(F_CPU) -Os INCLUDES = -I/Users/arthurwu/u8g2/csrc INCLUDES += -I/Users/arthurwu/u8g2 INCLUDES += -I/Users/arthurwu/u8g2/sys/avr/avr-libc/lib

Build Rules

all: $(PROJECT).hex

$(PROJECT).hex: $(PROJECT).elf $(OBJCOPY) -O ihex -R .eeprom $< $@

$(PROJECT).elf: $(OBJ) $(CC) $(CFLAGS) $(INCLUDES) $^ -o $@

%.o: %.c $(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

Flashing/Programming the Microcontroller

flash: all avrdude -c $(PROGRAMMER_TYPE) -p $(MCU) -U flash:w:$(PROJECT).hex

Clean up

clean: rm -f $(OBJ) $(PROJECT).elf $(PROJECT).hex

.PHONY: all flash clean

When I tried compiling the code I had the issue displayed.

Screenshot 2024-03-01 at 1 47 55 pm

Any help and guidance is much appreciated thank you

olikraus commented 4 months ago

You need to patch the single font file, by adding #include "u8g2.h" as a first code line. Instead of using the single font file you could also add the large "fonts.c" file to your makefile and activate the avr-gcc garbage collector: -ffunction-sections -fdata-sections -Wl,--gc-sections

https://interrupt.memfault.com/blog/best-and-worst-gcc-clang-compiler-flags#-ffunction-sections--fdata-sections----gc-sections