andrea.varesio
Posts: 5
Joined: Wed May 29, 2013 11:06 am

memory allocation breaks frame buffer ioctl?

Wed May 29, 2013 11:17 am

Hi

I am new in raspberry PI.
I am trying to use frame buffer.
I configured frame buffer editing /boot/config.txt

framebuffer_depth=32
framebuffer_ignore_alpha=1

When I try to open the device and perform a FBIOPUT_VSCREENINFO ioctl I have an issue related to memory allocation. If I try to allocate memory before accessing frame buffer, FBIOPUT_VSCREENINFO fails with error 16 (I believe EBUSY). if instead I allocate memory after, everything is ok. Hereafter the code
compiled with
g++ a.cpp

running ./a.out 0 ---> FAILS
running ./a.out 1 ---> OK

Thanks in advance

#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <linux/fb.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>

#define DEFAULT_FB_DEVICE ((char*)"/dev/fb0")
#define BPP 32

#define WIDTH 640
#define HEIGHT 480

int m_fd;
void Init()
{
struct fb_var_screeninfo var;
int err;

m_fd = open(DEFAULT_FB_DEVICE, O_RDWR);
if (m_fd < 0)
{
printf ("_open failed 1\n");
exit(1);
}

var.xres = WIDTH;
var.yres = HEIGHT;
var.bits_per_pixel = BPP;

/* set "var" screeninfo */
err = ioctl(m_fd, FBIOPUT_VSCREENINFO, &var);
if (err)
{
printf ("IOCTL VSCREENINFO error %d\n", err);
exit (1);
}
}

int main (int argc, char **argv)
{
if (argc != 2)
{
printf ("usage: %s 0/1\n 0= allocate before init, 1= allocate after init\n", argv[0]);
exit(1);
}
int AllocateAfter = atoi(argv[1]);
unsigned char *pRGB = NULL;

printf ("started\n");

//se alloco prima della Init, la init falliscce con errore 16 che dovrebbe essere "EBUSY"
// e' indipendente dal fatto che uso malloc o new
if (!AllocateAfter)
{
printf ("Allocating before\n");
//pRGB = (unsigned char *)malloc (WIDTH*HEIGHT*4);
pRGB = new unsigned char [WIDTH*HEIGHT*4];
}

Init();

if (AllocateAfter)
{
printf ("Allocate after\n");
//pRGB = (unsigned char *)malloc (WIDTH*HEIGHT*4);
pRGB = new unsigned char [WIDTH*HEIGHT*4];
}

printf ("frame buffer opened\n");


close (m_fd);
printf ("frame buffer closed\n");

delete [] pRGB;
//free (pRGB);

printf ("type enter to exit\n");
getchar();
printf ("Ended\n");
}

andrea.varesio
Posts: 5
Joined: Wed May 29, 2013 11:06 am

Re: memory allocation breaks frame buffer ioctl?

Thu May 30, 2013 6:06 am

I am self responding thanks to a solution arrived from a different channel.

Every structure used in ioctl HAS TO BE MEMSET to 0!

Return to “C/C++”