altos/stm32f103-nucleo: Drive a screen too
authorKeith Packard <keithp@keithp.com>
Wed, 8 Mar 2023 23:54:39 +0000 (15:54 -0800)
committerKeith Packard <keithp@keithp.com>
Thu, 1 Feb 2024 01:50:19 +0000 (17:50 -0800)
Use the ST7565 driver to control an LCD screen.

Signed-off-by: Keith Packard <keithp@keithp.com>
src/stm32f103-nucleo/Makefile
src/stm32f103-nucleo/ao_pins.h
src/stm32f103-nucleo/hello.c

index 9459d11d0da24ea2dfa8a80c12ff6b22030e622b..5f2df00f1f19235ee7acf25ff526f9693c45fc57 100644 (file)
@@ -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
index 6de5c9a9f4d3003601690eaf0de89fd554386d4e..f57c0a94fdd6de902a78f58ecc3e47e5b46b629d 100644 (file)
 #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
index 1571eae4f5427c457ba3b352c0dd6ce7796daeef..76b1d995bd1dd84dc4439e2a56d69f58def4530b 100644 (file)
  */
 
 #include <ao.h>
+#include <ao_st7565.h>
 
-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();
 }