Page 1 of 1

Getting framebuffer to work on Raspberry Pi

Posted: Tue Mar 17, 2020 10:49 pm
by itsmevjnk
I've been trying to get framebuffer working on Raspberry Pi 2. This is the code that I used to initialize it:

Code: Select all

typedef struct {
	uint32_t width, height;
	uint32_t vwidth, vheight;
	uint32_t pitch, depth;
	uint32_t xoff, yoff;
	uint32_t ptr, size;
} __attribute__ ((packed)) pifb_info_t;

pifb_info_t __attribute__ ((aligned(16))) pifb_info;
fb_device_t pifb_device;

void pifb_init(uint32_t width, uint32_t height) {
	pifb_info.width = width; pifb_info.height = height;
	pifb_info.vwidth = width; pifb_info.vheight = height;
	pifb_info.depth = 24;
	pifb_info.xoff = 0; pifb_info.yoff = 0; pifb_info.pitch = 0; pifb_info.ptr = 0; pifb_info.size = 0;
	uint32_t dat = ((uint32_t) &pifb_info) >> 4;
	if(rpi_board >= 2) dat |= 0xC000000; // convert to GPU address
	mbox_write(1, dat);
	//pifb_info_t *pifb_out = (pifb_info_t*) ((mbox_read(1) << 4) & ((rpi_board >= 2) ? ~0xC0000000 : 0xFFFFFFFF));
	pifb_info_t *pifb_out = &pifb_info;
	pifb_device.width = width; pifb_device.height = height;
	pifb_device.pitch = pifb_out->pitch; pifb_device.bpp = pifb_out->depth;
	pifb_device.color.endian = 0; pifb_device.color.bytes = 3;
	pifb_device.color.r_start = 16; pifb_device.color.r_bits = 8;
	pifb_device.color.g_start = 8; pifb_device.color.g_bits = 8;
	pifb_device.color.b_start = 0; pifb_device.color.b_bits = 8;
	pifb_device.buffer = (uint8_t*) (pifb_out->ptr & ((rpi_board >= 2) ? ~0xC0000000 : 0xFFFFFFFF));
	if(fb_default == NULL) fb_default = &pifb_device;
}
The problem is that it works well on Qemu, but not on real hardware. Anyone know how to fix it?
BTW, here's the hardware implementations for the Raspberry Pi on my OS: https://gitlab.com/weedboi6969/fusion/- ... rpi-common

Re: Getting framebuffer to work on Raspberry Pi

Posted: Wed Mar 18, 2020 3:53 am
by bzt
Hi,

I believe you should align the struct. Otherwise I would not recommend to use the framebuffer channel, rather use the property channel. It's more flexible and works on all Pis. Here's the corresponding part of my tutorial series, tested by many many users both on qemu and real hardware: 09_framebuffer.

Note that my mbox[] array is 16 bytes aligned. This code is intended for AArch64, but there's nothing in the mbox/lfb part that wouldn't compile under AArch32. Also note that I only convert the returned GPU address to CPU address unconditionally, and never do the other way around: mbox[28] &= 0x3FFFFFFF;

And here's dwelch's tutorial video01.c. Written for AArch32 and Raspi2, uses the framebuffer channel, although a little bit less readable source (uses hardcoded GPU addresses).

Cheers,
bzt

Re: Getting framebuffer to work on Raspberry Pi

Posted: Thu Mar 19, 2020 1:49 am
by itsmevjnk
Thanks for your reply! Apparently it's the lack of BSS clearing code that's causing the problem. However, I figured out that it's best to switch to the Mailbox property interface for framebuffer initialization, so I've adapted your code into my OS and now everything is working nicely on real hardware now.