From c099bcb7402421985e6e8c025e8cde591eaa073a Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Tue, 14 Oct 2025 17:54:17 -0700 Subject: Add MAIL_T macro; read framebuffer allocation response --- src/framebuffer.c | 21 +++++++++++++++++++-- src/mailbox.h | 12 +++++++++--- src/uart.c | 2 +- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/framebuffer.c b/src/framebuffer.c index e3303b6..c3845b1 100644 --- a/src/framebuffer.c +++ b/src/framebuffer.c @@ -2,6 +2,7 @@ #include +#include #include #define WIDTH 640 @@ -9,8 +10,15 @@ #define DEPTH 32 #define ALIGNMENT 16 // Framebuffer byte alignment. +typedef struct Framebuffer { + volatile void* pixels; + size_t size; +} Framebuffer; + +static Framebuffer framebuffer = {}; + bool framebuffer_init(uint32_t* error) { - volatile __attribute__((aligned(MAIL_ALIGN))) uint32_t ConfigureScreen[20] = { + MAIL_T uint32_t ConfigureScreen[20] = { 80, // Size in bytes, aligned to MAIL_ALIGN. MAILBOX_REQUEST, TAG_FRAMEBUFFER_SET_PHYSICAL_SCREEN_SIZE, 8, MAILBOX_REQUEST, WIDTH, HEIGHT, @@ -25,7 +33,7 @@ bool framebuffer_init(uint32_t* error) { goto end; } - volatile __attribute__((aligned(MAIL_ALIGN))) uint32_t InitFramebuffer[8] = { + MAIL_T uint32_t InitFramebuffer[8] = { 32, // Size in bytes, aligned to MAIL_ALIGN. MAILBOX_REQUEST, TAG_FRAMEBUFFER_ALLOCATE, 8, MAILBOX_REQUEST, ALIGNMENT, 0, @@ -33,6 +41,15 @@ bool framebuffer_init(uint32_t* error) { }; mbox_write(PROPERTY_CHANNEL, InitFramebuffer); response = mbox_read(PROPERTY_CHANNEL); + if (response->code != MAILBOX_SUCCESS) { + goto end; + } + + // The input mail is overwritten with the response. + // u32 fb base address + // u32 fb size + framebuffer.pixels = (void*)(uintptr_t)InitFramebuffer[5]; + framebuffer.size = InitFramebuffer[6]; end: *error = response->code; diff --git a/src/mailbox.h b/src/mailbox.h index b35c7a6..2104265 100644 --- a/src/mailbox.h +++ b/src/mailbox.h @@ -1,3 +1,9 @@ +/* +References: + https://github-wiki-see.page/m/raspberrypi/firmware/wiki/Mailbox-property-interface + https://jsandler18.github.io/extra/prop-channel.html + https://jsandler18.github.io/extra/mailbox.html +*/ #pragma once #include @@ -6,6 +12,9 @@ // must be aligned to a 16-byte boundary. #define MAIL_ALIGN 16 +// Type prefix for data arrays used as mail. +#define MAIL_T volatile __attribute__((aligned(MAIL_ALIGN))) + enum { PROPERTY_CHANNEL = 8, @@ -55,9 +64,6 @@ typedef struct __attribute__((aligned(MAIL_ALIGN))) Mail { Tag tags[]; // Variable quantity. } Mail; -// TODO: Remove? Unused. -#define MAIL_SIZE(TYPE) (sizeof(TYPE) + (2 * sizeof(uint32_t))) - void mbox_init(); const Mail* mbox_read(uint8_t channel); void mbox_write(uint8_t channel, volatile const void* mail); diff --git a/src/uart.c b/src/uart.c index 3b107f4..f8be797 100644 --- a/src/uart.c +++ b/src/uart.c @@ -35,7 +35,7 @@ enum }; // A mailbox message with set clock rate of PL011 to 3MHz tag. -static const uint32_t __attribute__((aligned(MAIL_ALIGN))) UART_SET_CLK[9] = { +static const MAIL_T uint32_t UART_SET_CLK[9] = { 9*4, 0, 0x38002, 12, 8, 2, 3000000, 0, 0 }; -- cgit v1.2.3