rudiratlos
Posts: 109
Joined: Tue May 01, 2012 8:47 am

api to firmware

Tue Feb 13, 2018 8:06 pm

can anyone supply an example which uses the firmware.
e.g. ask the firmware for revision info, cpu temerature...

thnx,
sf

ShiftPlusOne
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5182
Joined: Fri Jul 29, 2011 5:36 pm
Location: The unfashionable end of the western spiral arm of the Galaxy

Re: api to firmware

Tue Feb 13, 2018 9:02 pm

No idea how valid this is, since I wrote it long before I had any idea of what I was doing:

viewtopic.php?f=31&t=20594#p280222

LdB
Posts: 781
Joined: Wed Dec 07, 2016 2:29 pm

Re: api to firmware

Wed Feb 14, 2018 1:50 am

Not sure if you can get to the GPU system mailbox while linux is running, I imagine it may be using it and may not let you memmap it
There is a published interface which we use all the time baremetal

https://github.com/raspberrypi/firmware ... -interface

The structure to represent the mailbox hardware is

Code: Select all

/*--------------------------------------------------------------------------}
;{               RASPBERRY PI MAILBOX HARRDWARE REGISTERS					}
;{-------------------------------------------------------------------------*/
struct __attribute__((__packed__, aligned(4))) MailBoxRegisters {
	const uint32_t Read0;			// 0x00         Read data from VC to ARM
	uint32_t Unused[3];			// 0x04-0x0F
	uint32_t Peek0;				// 0x10
	uint32_t Sender0;			// 0x14
	uint32_t Status0;			// 0x18         Status of VC to ARM
	uint32_t Config0;			// 0x1C        
	uint32_t Write1;			// 0x20         Write data from ARM to VC
	uint32_t Unused2[3];			// 0x24-0x2F
	uint32_t Peek1;				// 0x30
	uint32_t Sender1;			// 0x34
	uint32_t Status1;			// 0x38         Status of ARM to VC
	uint32_t Config1;			// 0x3C 
};
 
#define MAILBOX ((volatile __attribute__((aligned(4))) struct MailBoxRegisters*)(uintptr_t)(RPi_IO_Base_Addr + 0xB880));
RPi_IO_Base_Addr is 0x20000000 on a Pi1, 0x3F000000 on a Pi2 or 3 meaning the mailbox is at 0x2000B880 or 0x3F00B880 respectively.

The enumerated channels are and you are after the tags channel being 0x8

Code: Select all

/*--------------------------------------------------------------------------}
{	                  ENUMERATED MAILBOX CHANNELS							}
{		  https://github.com/raspberrypi/firmware/wiki/Mailboxes			}
{--------------------------------------------------------------------------*/
typedef enum {
	MB_CHANNEL_POWER = 0x0,			// Mailbox Channel 0: Power Management Interface 
	MB_CHANNEL_FB = 0x1,			// Mailbox Channel 1: Frame Buffer
	MB_CHANNEL_VUART = 0x2,			// Mailbox Channel 2: Virtual UART
	MB_CHANNEL_VCHIQ = 0x3,			// Mailbox Channel 3: VCHIQ Interface
	MB_CHANNEL_LEDS = 0x4,			// Mailbox Channel 4: LEDs Interface
	MB_CHANNEL_BUTTONS = 0x5,		// Mailbox Channel 5: Buttons Interface
	MB_CHANNEL_TOUCH = 0x6,			// Mailbox Channel 6: Touchscreen Interface
	MB_CHANNEL_COUNT = 0x7,			// Mailbox Channel 7: Counter
	MB_CHANNEL_TAGS = 0x8,			// Mailbox Channel 8: Tags (ARM to VC)
	MB_CHANNEL_GPU = 0x9,			// Mailbox Channel 9: GPU (VC to ARM)
} MAILBOX_CHANNEL;
The write to mailbox routine is

Code: Select all

#define MAIL_FULL	0x80000000		/* Mailbox Status Register: Mailbox Full  */
/*-[mailbox_write]----------------------------------------------------------}
. This will execute the sending of the given data block message thru the
. mailbox system on the given channel.
. RETURN: True for success, False for failure.
.--------------------------------------------------------------------------*/
bool mailbox_write (MAILBOX_CHANNEL channel, uint32_t message) 
{
	uint32_t value;					// Temporary read value
	if (channel > MB_CHANNEL_GPU)  return false;	// Channel error
	message &= ~(0xF);				// Make sure 4 low channel bits are clear 
	message |= channel;				// OR the channel bits to the value							
	do {
		value = MAILBOX->Status1;		// Read mailbox1 status from GPU	
	} while ((value & MAIL_FULL) != 0);		// Make sure arm mailbox is not full
	MAILBOX->Write1 = message;			// Write value to mailbox
	return true;					// Write success
}
The read mailbox routine is

Code: Select all

#define MAIL_EMPTY	0x40000000		/* Mailbox Status Register: Mailbox Empty */
/*-[mailbox_read]-----------------------------------------------------------}
. This will read any pending data on the mailbox system on the given channel.
. RETURN: The read value for success, 0xFEEDDEAD for failure.
.--------------------------------------------------------------------------*/
uint32_t mailbox_read (MAILBOX_CHANNEL channel) 
{
	uint32_t value;							// Temporary read value
	if (channel > MB_CHANNEL_GPU)  return 0xFEEDDEAD;		// Channel error
	do {
		do {
			value = MAILBOX->Status0;			// Read mailbox0 status
		} while ((value & MAIL_EMPTY) != 0);			// Wait for data in mailbox
		value = MAILBOX->Read0;					// Read the mailbox	
	} while ((value & 0xF) != channel);				// We have response back
	value &= ~(0xF);						// Lower 4 low channel bits are not part of message
	return value;							// Return the value
}
A typical function make look like

Code: Select all

uint32_t ReadCPUSpeed(void){
	/* message must be align 16 */
	uint32_t __attribute__((aligned(16))) message[8] = 
	{
		sizeof(message),  /* size of message */
		0x0,		/* response */
		0x00030002,	/* mailbox get clock rates */
		8,		/* request is 8 bytes long */
		8,		/* response expects 8 bytes back */
		3,		/* clock ID = 3 .. ARM CPU */
		0,		/* empty data field */
		0		/* structure terminator*/
	};
	mailbox_write(MB_CHANNEL_TAGS, (uint32_t)&message[0]);	// Send the message
	mailbox_read(MB_CHANNEL_TAGS);				// Clear the response
	if (message[1] == 0x80000000) {				// Message success
		return(message[6]);				// Message[6] was zero will have ARM CPU speed
	}
	return 0;						// Failed
}
Calling the function and print result

Code: Select all

int main (void) {
	printf("CPU speed = %u\r\n", ReadCPUSpeed());
}

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5372
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: api to firmware

Wed Feb 14, 2018 9:49 am

Yes you can get to the mailbox API, but don't go directly mmaping registers. As in ShiftPlusOne's sample, /dev/vcio is your friend.
See https://github.com/6by9/rpi3-gpiovirtbuf for another example which is controlling the LEDs on the Pi3.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
Please don't send PMs asking for support - use the forum.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

rudiratlos
Posts: 109
Joined: Tue May 01, 2012 8:47 am

Re: api to firmware

Thu Feb 15, 2018 2:15 pm

thanks,
https://github.com/6by9/rpi3-gpiovirtbuf
thats exactly what I'm looking for.

but I couldn't find a tag for mac addr of wlan adapter, gpu temperature ...
from where can I get these missing infos?
querying from the os like
/opt/vc/bin/vcgencmd measure_temp|awk -F "=" '{print $2}'
is to time consuming.
takes 2000 times longer

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5372
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: api to firmware

Thu Feb 15, 2018 2:56 pm

rudiratlos wrote:
Thu Feb 15, 2018 2:15 pm
thanks,
https://github.com/6by9/rpi3-gpiovirtbuf
thats exactly what I'm looking for.

but I couldn't find a tag for mac addr of wlan adapter, gpu temperature ...
from where can I get these missing infos?
querying from the os like
/opt/vc/bin/vcgencmd measure_temp|awk -F "=" '{print $2}'
is to time consuming.
takes 2000 times longer
https://github.com/raspberrypi/linux/bl ... firmware.h is the definitive list of mailbox calls.

https://github.com/raspberrypi/linux/bl ... ware.h#L50

Code: Select all

RPI_FIRMWARE_GET_BOARD_MAC_ADDRESS
for the wired ethernet address. The wifi is that XOR 0x555555.
TBH you ought to be asking the network interface for the MAC address as it can be changed via the command line. The mailbox/firmware only provides the default value.

https://github.com/raspberrypi/linux/bl ... ware.h#L63

Code: Select all

RPI_FIRMWARE_GET_TEMPERATURE
Better to use the kernel driver for it though - https://github.com/raspberrypi/linux/bl ... -thermal.c / https://github.com/raspberrypi/linux/bl ... _thermal.c
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
Please don't send PMs asking for support - use the forum.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

rudiratlos
Posts: 109
Joined: Tue May 01, 2012 8:47 am

Re: api to firmware

Fri Feb 16, 2018 12:59 pm

i can get the info for the actual temp and maxtemp value through the firmware api.
But I couldn't find to retrieve:
wlan signal strenth
gpu temperature
reset hw watchdog

how to get these infos?

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5372
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: api to firmware

Fri Feb 16, 2018 1:57 pm

wlan signal strength is nothing to do with the GPU (same as the MAC address isn't) - use the Linux networking APIs.
GPU temperature I've already quoted, although you're better off using the Linux thermal APIs.
The watchdog is also under Linux control - driver is at https://github.com/raspberrypi/linux/bl ... 2835_wdt.c
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
Please don't send PMs asking for support - use the forum.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

rudiratlos
Posts: 109
Joined: Tue May 01, 2012 8:47 am

Re: api to firmware

Fri Mar 23, 2018 2:50 pm

Hi,
I was successful in using rpi3-gpiovirtbuf.
My new challange I don't get the API example from LdB to work.

my ram mapping is:
VCmem (size): 0x04C00000 79691776 Bytes @ 0x3B400000
ARMmem(size): 0x3B400000 994050048 Bytes @ 0x00000000

mailbox_read get a timeout. I think that I'm offering the wrong buffer address to mailbox_write I know that I have to map the ARM buffer memory addr to VCMem. I tried to adjust memorypointer of my paramater-array ORing with 0xC0000000 than with 0x40000000 and after that with 0x3B400000 nothing was succesful.

rudiratlos
Posts: 109
Joined: Tue May 01, 2012 8:47 am

Re: api to firmware

Fri Mar 23, 2018 2:54 pm

can someone so kind, to write a small c program (similar to LDB's), that retrieves the mailbox with SoC Temperature Tag. so I can run it on my rpi3 for further investigation.

rudiratlos
Posts: 109
Joined: Tue May 01, 2012 8:47 am

Re: api to firmware

Sat Apr 07, 2018 4:59 pm

is there a good guy that can provide a working complete source code?

User avatar
DougieLawson
Posts: 33384
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: api to firmware

Sat Apr 07, 2018 6:09 pm

rudiratlos wrote:
Fri Mar 23, 2018 2:54 pm
can someone so kind, to write a small c program (similar to LDB's), that retrieves the mailbox with SoC Temperature Tag. so I can run it on my rpi3 for further investigation.
Why not use the built in command vcgencmd measure_temp or simply read it from cat /sys/devices/virtual/thermal/thermal_zone0/temp

You get the source code for that vcgencmd program and it must have a mailbox call in it to retrieve the CPU temperature.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

rudiratlos
Posts: 109
Joined: Tue May 01, 2012 8:47 am

Re: api to firmware

Mon Apr 09, 2018 2:46 pm

currently I'm already using vcgencmd and/or cat /sys/devices/virtual/thermal/thermal_zone0/temp
but these commands will access the OS level and this takes to long. I want to have direct mailbox access.

rudiratlos
Posts: 109
Joined: Tue May 01, 2012 8:47 am

Re: api to firmware

Tue Apr 10, 2018 8:14 am

whould be really nice, if someone could supply a working c code.
I've spent days to get it working, with no luck. I'm the maintainer of rpi_hal (free pascal library) and want to include mailbox api to the library.
Looks like that I have problems in my memory mapping from ARM <-> core.

SteveSpencer
Posts: 344
Joined: Thu Mar 28, 2013 9:19 am
Location: Nottingham, UK

Re: api to firmware

Tue Apr 10, 2018 8:34 am

DougieLawson wrote:
Sat Apr 07, 2018 6:09 pm

You get the source code for that vcgencmd program and it must have a mailbox call in it to retrieve the CPU temperature.
Where is, or how would I find the source for vcgencmd? I'd be interested in extracting some configuration/settings stuff for display on a small OLED, and rather than executing another command, would like to include the functionality in my own code.

I have it running using vcgencmd, but would prefer to speed it up a little.
Steve S
No, I can't think of anything funny that won't offend someone if they want it to...

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5372
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: api to firmware

Tue Apr 10, 2018 9:49 am

SteveSpencer wrote:
Tue Apr 10, 2018 8:34 am
DougieLawson wrote:
Sat Apr 07, 2018 6:09 pm

You get the source code for that vcgencmd program and it must have a mailbox call in it to retrieve the CPU temperature.
Where is, or how would I find the source for vcgencmd? I'd be interested in extracting some configuration/settings stuff for display on a small OLED, and rather than executing another command, would like to include the functionality in my own code.

I have it running using vcgencmd, but would prefer to speed it up a little.
https://github.com/raspberrypi/userland ... d/gencmd.c for the app
https://github.com/raspberrypi/userland ... i_gencmd.c for the library that does most of the work.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
Please don't send PMs asking for support - use the forum.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

SteveSpencer
Posts: 344
Joined: Thu Mar 28, 2013 9:19 am
Location: Nottingham, UK

Re: api to firmware

Tue Apr 10, 2018 11:07 am

Thanks for that. I had completely forgotten about "userland", I was only looking at linux and firmware....
Steve S
No, I can't think of anything funny that won't offend someone if they want it to...

Return to “C/C++”

Who is online

Users browsing this forum: No registered users and 12 guests