From bfafd5b04f0cca36724f3de0b0f18ffac93be591 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Wed, 8 Mar 2023 15:54:39 -0800 Subject: [PATCH] altos/stm32f103-nucleo: Drive a screen too Use the ST7565 driver to control an LCD screen. Signed-off-by: Keith Packard --- src/stm32f103-nucleo/Makefile | 15 ++++++ src/stm32f103-nucleo/ao_pins.h | 15 ++++++ src/stm32f103-nucleo/hello.c | 89 +++++++++++++++++++++++++++++++--- 3 files changed, 112 insertions(+), 7 deletions(-) diff --git a/src/stm32f103-nucleo/Makefile b/src/stm32f103-nucleo/Makefile index 9459d11d..5f2df00f 100644 --- a/src/stm32f103-nucleo/Makefile +++ b/src/stm32f103-nucleo/Makefile @@ -7,12 +7,14 @@ INC = \ ao_arch_funcs.h \ ao_exti.h \ ao_product.h \ + ao_st7565.h \ stm32f1.h \ Makefile ALTOS_SRC = \ ao_clock.c \ ao_timer.c \ + ao_mutex.c \ ao_boot_chain.c \ ao_interrupt.c \ ao_product.c \ @@ -23,6 +25,19 @@ ALTOS_SRC = \ ao_stdio.c \ ao_serial_stm.c \ ao_usb_stm.c \ + ao_dma_stm.c \ + ao_spi_stm.c \ + ao_st7565.c \ + ao_rect.c \ + ao_text.c \ + ao_box.c \ + ao_copy.c \ + ao_blt.c \ + ao_line.c \ + ao_logo.c \ + ao_poly.c \ + BitstreamVeraSans-Roman-24.c \ + BenguiatGothicStd-Bold-26.c \ ao_cmd.c PRODUCT=Nucleo-f103 diff --git a/src/stm32f103-nucleo/ao_pins.h b/src/stm32f103-nucleo/ao_pins.h index 6de5c9a9..f57c0a94 100644 --- a/src/stm32f103-nucleo/ao_pins.h +++ b/src/stm32f103-nucleo/ao_pins.h @@ -54,3 +54,18 @@ #define USE_SERIAL_2_STDIN 1 #define SERIAL_2_PA2_PA3 1 #define SERIAL_2_SPEED AO_SERIAL_SPEED_115200 + +#define HAS_SPI_1 1 +#define SPI_1_PA5_PA6_PA7 1 +#define SPI_1_MODE_OUTPUT STM_GPIO_CR_MODE_OUTPUT_10MHZ + +#define AO_ST7565_CS_PORT (&stm_gpioa) /* pin 1 */ +#define AO_ST7565_CS_PIN 4 +#define AO_ST7565_RESET_PORT (&stm_gpioa) /* pin 2 */ +#define AO_ST7565_RESET_PIN 0 +#define AO_ST7565_A0_PORT (&stm_gpioa) /* pin 3 */ +#define AO_ST7565_A0_PIN 1 +#define AO_ST7565_SPI_BUS AO_SPI_1_PA5_PA6_PA7 +#define AO_ST7565_WIDTH 128 +#define AO_ST7565_HEIGHT 64 +#define AO_ST7565_BIAS AO_ST7565_LCD_BIAS_1_9 diff --git a/src/stm32f103-nucleo/hello.c b/src/stm32f103-nucleo/hello.c index 1571eae4..76b1d995 100644 --- a/src/stm32f103-nucleo/hello.c +++ b/src/stm32f103-nucleo/hello.c @@ -17,26 +17,101 @@ */ #include +#include -static void blink(void) +#define WIDTH AO_ST7565_WIDTH +#define HEIGHT AO_ST7565_HEIGHT +#define STRIDE AO_BITMAP_STRIDE(WIDTH) + +static uint32_t image[STRIDE * HEIGHT]; + +static struct ao_bitmap fb = { + .base = image, + .stride = STRIDE, + .width = WIDTH, + .height = HEIGHT, + .damage = AO_BOX_INIT, +}; + +static void +ao_st7565_test(void) +{ + ao_rect(&fb, 0, 0, WIDTH, HEIGHT, AO_WHITE, AO_COPY); + ao_st7565_update(&fb); + ao_text(&fb, &BitstreamVeraSans_Roman_24_font, + 0, 20, "hello world", AO_BLACK, AO_COPY); + ao_st7565_update(&fb); +} + +static int16_t x1 = 32, _y1 = 10, x2 = 32, y2 = 40; +static int16_t dx1 = 2, dy1 = 2, dx2 = -2, dy2 = -1; + +#define bounds(v,m,M,d) \ + if (v < m) { \ + v = m + m - v; \ + d = -d; \ + } else if (v > M) { \ + v = M - (v - M); \ + d = -d; \ + } + +static void +ao_st7565_line(void) { - for (;;) { - ao_led_for(AO_LED_GREEN, 50); - ao_delay(50); + int i; + + for (i = 0; i < 100; i++) { + ao_rect(&fb, 0, 0, WIDTH, HEIGHT, AO_WHITE, AO_COPY); + ao_line(&fb, x1, _y1, x2, y2, AO_BLACK, AO_COPY); + ao_st7565_update(&fb); + x1 += dx1; + _y1 += dy1; + x2 += dx2; + y2 += dy2; + printf("%d,%d - %d,%d\n", x1, _y1, x2, y2); + fflush(stdout); + bounds(x1, 0, WIDTH, dx1); + bounds(x2, 0, WIDTH, dx2); + bounds(_y1, 0, HEIGHT, dy1); + bounds(y2, 0, HEIGHT, dy2); + ao_delay(AO_MS_TO_TICKS(200)); } } -static struct ao_task blink_task; +static const struct ao_transform logo_transform = { + .x_scale = 48, .x_off = 0, + .y_scale = 48, .y_off = 0, +}; + +#define LOGO_FONT BenguiatGothicStd_Bold_26_font + +static void +ao_st7565_poly(void) +{ + ao_rect(&fb, 0, 0, WIDTH, HEIGHT, AO_WHITE, AO_COPY); + ao_logo(&fb, &logo_transform, &LOGO_FONT, 0x00000000, AO_COPY); + ao_st7565_update(&fb); +} + +const struct ao_cmds ao_st7565_cmds[] = { + { ao_st7565_test, "g\0Test ST7565 display" }, + { ao_st7565_line, "l\0Draw lines" }, + { ao_st7565_poly, "p\0Draw polygon" }, + { 0, NULL }, +}; int main(void) { ao_clock_init(); ao_led_init(); ao_timer_init(); + ao_task_init(); + ao_dma_init(); + ao_spi_init(); ao_serial_init(); ao_usb_init(); - ao_task_init(); + ao_st7565_init(); ao_cmd_init(); - ao_add_task(&blink_task, blink, "blink"); + ao_cmd_register(ao_st7565_cmds); ao_start_scheduler(); } -- 2.30.2