compiling kernel.c in raspberry pi tutorial error

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Paletech35
Posts: 8
Joined: Sun Mar 28, 2021 6:30 pm

compiling kernel.c in raspberry pi tutorial error

Post by Paletech35 »

I'm following the tutorial on Raspberry Pi Bare Bones adn trying to compile kernel.c yields many errors:

expected identifier before 'switch' on line 29
In function uart_init, 'UART0_CR' undeclared on line 83 mmio_write(UART0_CR, 0x00000000)

and then many more errors stating things are undeclared, similar to the second one above. I have compiled the cross compiler with arm-none-eabi, and compiling boot.S worked fine. I have no clue whats causing these errors, although at the end of the error list are some warnings as well.
The list of warnings
The list of warnings
Im using cygwin on windows 10 trying to compile for a Pi model 1.
Thanks in advance
Octocontrabass
Member
Member
Posts: 5568
Joined: Mon Mar 25, 2013 7:01 pm

Re: compiling kernel.c in raspberry pi tutorial error

Post by Octocontrabass »

You can't have a switch statement inside an enum declaration.

I'm not sure why such an obvious mistake is in the article's sample code, but you can fix it easily enough by removing the switch statement and choosing an appropriate GPIO_BASE value for your target hardware. (Of course, you'll have to come up with something more clever if you want your code to work on more than a single model of Raspberry Pi.)
User avatar
Schol-R-LEA
Member
Member
Posts: 1925
Joined: Fri Oct 27, 2006 9:42 am
Location: Athens, GA, USA

Re: compiling kernel.c in raspberry pi tutorial error

Post by Schol-R-LEA »

This actually has been mentioned a few different times; while it is clearly an error in the code, I refrained from fixing it the last time it came up because I wasn't sure if the original developer had something important which was supposed to be there.

There was some speculation that it was there as a 'copy trap', that is, it was deliberately mis-coded in a way which would be easy to fix in order to discourage readers from trying to use it without going over the code.
Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
Ordo OS Project
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.
nexos
Member
Member
Posts: 1081
Joined: Tue Feb 18, 2020 3:29 pm
Libera.chat IRC: nexos

Re: compiling kernel.c in raspberry pi tutorial error

Post by nexos »

I personally think this should be fixed. This could greatly confuse some people. I once tried following this tutorial, saw the compiler error, so I figured it was broken. IMO this is just causing problems if it was a copy trap.
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg
kzinti
Member
Member
Posts: 898
Joined: Mon Feb 02, 2015 7:11 pm

Re: compiling kernel.c in raspberry pi tutorial error

Post by kzinti »

I agree. This is just stupid.
User avatar
Schol-R-LEA
Member
Member
Posts: 1925
Joined: Fri Oct 27, 2006 9:42 am
Location: Athens, GA, USA

Re: compiling kernel.c in raspberry pi tutorial error

Post by Schol-R-LEA »

Fair enough, I'll fix it now (assuming that no one has done it already).

EDIT:
The code I propose using, in order to minimize changes to the rest of the code, goes:

Code: Select all


#define RASPI_MODEL 2

#if (RASPI_MODEL == 2) || (RASPI_MODEL == 3)
#define MODEL_MMIO_BASE 0x3F000000
#elif (RASPI_MODEL == 4)
#define MODEL_MMIO_BASE 0xFE000000
#else
#define MODEL_MMIO_BASE 0x20000000
#endif

int raspi = RASPI_MODEL;

// ...

enum
{
    // The MMIO area base address.
    MMIO_BASE = MODEL_MMIO_BASE, 

// ...
While some of this is a bit redundant, I was trying to keep the effects of the changes to a minimum.

However, when I tried testing this under QEMU, I wasn't able to get the "Hello, World!" output.
Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
Ordo OS Project
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.
User avatar
Schol-R-LEA
Member
Member
Posts: 1925
Joined: Fri Oct 27, 2006 9:42 am
Location: Athens, GA, USA

Re: compiling kernel.c in raspberry pi tutorial error

Post by Schol-R-LEA »

I still haven't been able to get the print-out to work in emulation. Does anyone else have a working version of this?
Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
Ordo OS Project
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.
User avatar
bzt
Member
Member
Posts: 1584
Joined: Thu Oct 13, 2016 4:55 pm
Contact:

Re: compiling kernel.c in raspberry pi tutorial error

Post by bzt »

Schol-R-LEA wrote:Fair enough, I'll fix it now (assuming that no one has done it already).

EDIT:
The code I propose using, in order to minimize changes to the rest of the code, goes:
I believe the purpose of the switch is to detect the board in run-time. So either forget about the detection and "int raspi" on this page entirely, and just link to the page talking about the run-time board detection; or keep "int raspi" and the switch and use a static variable and bunch of defines like

Code: Select all

static uint32_t MMIO_BASE;
#define GPFSEL0 ((volatile uint32_t*)(MMIO_BASE+0x00200000))
#define GPFSEL1 ((volatile uint32_t*)(MMIO_BASE+0x00200004))
instead of an enum.
Schol-R-LEA wrote:I still haven't been able to get the print-out to work in emulation. Does anyone else have a working version of this?
Sadly no, not of this. But you should try my raspi3-tutorials, there's two Hello World tutorial in them, one for mini AUX (uart1), and one for the PL011 (uart0). But I can tell you I pissed blood when I was working on those because it is not documented that on later models (around late RPi2 and early RPi3) the uart clock frequency is not fixed, so in order to use a baud divisor, first you must fixate the uart freq with a mailbox call. (For older models, the mini AUX was CPU clock dependent and PL011 wasn't, for newer models it's the other way around, mini AUX is fixed and PL011 isn't. Took me days of trial-and-error to figure this out.) You could use my tutorials as a base, but they are for RPi3 AArch64 only.

(I got "time out" error for the wiki, is something wrong with the server? Had to wait a lot until forum loaded too)

EDIT: I've modified the code, moved MMIO_BASE from the enum into a variable and mmio_read() and mmio_write(). Please give it a try.

Cheers,
bzt
Post Reply