sebastianhutter
Posts: 6
Joined: Sun Mar 04, 2018 12:54 pm

Simple Blinking LED program not working on Raspberry PI 3

Sun Mar 04, 2018 1:25 pm

Hi Everyone

I am trying to get a simple blinking LED program to work on my Raspberry PI 3 Model B v1.2.
Unfortunately, I cant make the LED blink no matter what I do. The same code (except for the GPIO register base address) works without a problem for my old Raspberry PI 1.

I manually created an SD-Card with the following files:

- bootcode.bin (downloaded from raspberry firmware github repo - branch: stable)
- fixup.dat (downloaded from raspberry firmware github repo - branch: stable)
- start.elf (downloaded from raspberry firmware github repo - branch: stable)
- kernel.img - compiled c program

The SD card boots without a problem and the kernel img is loaded (verfified with: https://github.com/adamransom/bare_meta ... ster/led01)


Here is the little c program I wrote to let the LED blink (copied the code from here: https://asciich.ch/wordpress/raspberryp ... inrichten/)

Code: Select all

#define GPIOREGS       0x3F200000
 
#define GPFSEL0    0
#define GPFSEL1    1
#define GPFSEL2    2
#define GPFSEL3    3
#define GPFSEL4    4
#define GPFSEL5    5
// 6 is reserved
#define GPSET0     7
#define GPSET1     8
// 9 is reserved
#define GPCLR0     10
#define GPCLR1     11
 
volatile unsigned int* gpioregs;
 
void delay(unsigned int t) {
    static volatile unsigned int time;
    for (time = 0; time < t; time++);
}
 
void blink(unsigned int t, unsigned int l) {
    delay(t);
    gpioregs[GPCLR0] = (1 << l);
    delay(t);
    gpioregs[GPSET0] = (1 << l);
}

int main(void)
{
    // create pointer to the gpioregs;
    gpioregs = (unsigned int*)GPIOREGS;
 
    // set gpio22 as output
    gpioregs[GPFSEL2] |= (1 << 6);
 
    while(1)
    {
        blink(500000,22);
    }
    return 0;
}
I then create the kernel.img file with the following commands:

Code: Select all

arm-none-eabi-gcc -O2 -mfpu=vfp -mfloat-abi=hard -march=armv8-a -mtune=arm8 -nostartfiles pin3.c -o kernel.elf
arm-none-eabi-objcopy kernel.elf -O binary kernel.img
After copyingt he kernel.img to the SD card and powering on the PI3 the LED is not blinking.


Now if I am using the same code with the Raspberry PI 1 he LED blinks as expected.
I am using the following GPIO base addresses for the the Pi1 and Pi3

Code: Select all

# for pi 1
#define GPIOREGS       0x20200000

# for pi 3
#define GPIOREGS       0x3F200000
Any ideas what I am doing wrong ?
Last edited by sebastianhutter on Tue Mar 06, 2018 6:16 pm, edited 2 times in total.

sebastianhutter
Posts: 6
Joined: Sun Mar 04, 2018 12:54 pm

Re: Simple Blinking LED program not working on Raspberry PI 3

Mon Mar 05, 2018 10:40 am

Ok. I just tested the same code on another PI3 and there it works.
It is safe to assume that the GPIO pins on my model are broken

dwelch67
Posts: 925
Joined: Sat May 26, 2012 5:32 pm

Re: [SOLVED] Simple Blinking LED program not working on Raspberry PI 3

Mon Mar 05, 2018 3:29 pm

how did you get it to work on a pi3? the led is not connected to a gpio pin unless they did yet another board layout/rev...

sebastianhutter
Posts: 6
Joined: Sun Mar 04, 2018 12:54 pm

Re: [SOLVED] Simple Blinking LED program not working on Raspberry PI 3

Mon Mar 05, 2018 3:44 pm

I wired up an LED to the GPIO 22 and I am not using the ACT Led or any other on-board LEDs.

Image

sebastianhutter
Posts: 6
Joined: Sun Mar 04, 2018 12:54 pm

Re: Simple Blinking LED program not working on Raspberry PI 3

Tue Mar 06, 2018 6:24 pm

Funny thing, I just bought a brand new raspberry pi 3 (model B v1.2), connected everything up, and the LED is NOT blinking.
It seems like the different PIs behave differently, or I got two defect ones in a row.

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

Re: Simple Blinking LED program not working on Raspberry PI 3

Wed Mar 07, 2018 1:57 am

Two obvious possibilities .. you haven't cleared the bits when you setup the port you assumed they are zero for the OR

try.

Code: Select all

// set gpio22 as output
    gpioregs[GPFSEL2] &= ~(7 << 6);
    gpioregs[GPFSEL2] |= (1 << 6);

There is a second concern which probably isn't the problem but will catch you codeout randomly.

The ARM8 compiler has strict 32 bit alignment and anything that it doesn't know is byte align 4,
it will often write as 2 words which is technically faster and safer on the ARM8 :-)

For safety I would try a slight variation to your register definition to back sure the compiler knows its aligned

Code: Select all

volatile __attribute__((aligned(4))) unsigned int* gpioregs;

sebastianhutter
Posts: 6
Joined: Sun Mar 04, 2018 12:54 pm

Re: Simple Blinking LED program not working on Raspberry PI 3

Fri Mar 09, 2018 10:55 am

I changed the code accordingly. Unfortunately, still no luck.

In the meantime, I set up an sd card with raspbian and wiringPI to test the gpios. I was able to work with every pin on the board. So a hardware defect can be excluded for sure.

I had a look at the different raspberry PI boards I used to test and one difference seems to be a different CPU 'minor' version. I dont really know how to call that - I dont have much experience with all of this ;-) ).

Is this a possible cause ?

Is another possible issue the used compiler options ? for the cross compiling ?

Code: Select all

arm-none-eabi-gcc -O2 -mfpu=vfp -mfloat-abi=hard -march=armv8-a -mtune=arm8 -nostartfiles $(SRCDIR)/$(SRC) -o kernel.elf
arm-none-eabi-objcopy kernel.elf -O binary bin/$(KERNELIMG)
Here the different models:

Working Raspberry PI
Processor: BCM2837RIFBG, TN1709 P11 (126 05 N3 V)
2018-03-09 09.17.03.jpg 2018-03-09 11-51-53.jpg
BCM2837RIFBG, TN1709 P11 (126 05 N3 V)
2018-03-09 09.17.03.jpg 2018-03-09 11-51-53.jpg (254.26 KiB) Viewed 1834 times


Non working PIs:
Processor: BCM2837RIFBG, TN1616 P11 (588103-18 N3 W)
2018-03-09 11.43.08.jpg 2018-03-09 11-54-03.jpg
CM2837RIFBG, TN1616 P11 (588103-18 N3
2018-03-09 11.43.08.jpg 2018-03-09 11-54-03.jpg (226.06 KiB) Viewed 1834 times
Processor: BCM2837RIFBG, TN1718 P11 (221-13 N3 W)
2018-03-06 19.21.28.jpg 2018-03-09 11-51-05.jpg
BCM2837RIFBG, TN1718 P11 (221-13 N3 W)
2018-03-06 19.21.28.jpg 2018-03-09 11-51-05.jpg (138.41 KiB) Viewed 1834 times

sebastianhutter
Posts: 6
Joined: Sun Mar 04, 2018 12:54 pm

Re: Simple Blinking LED program not working on Raspberry PI 3

Fri Mar 09, 2018 1:14 pm

Here the link to my gitlab repository:
https://gitlab.com/sebastianhutter/ffhs-gti-colorscan

I am trying to make the code in src/pin3.c work.
Code is compiled and written to sd card via

Code: Select all

make card SRC=src/pin3.c

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

Re: Simple Blinking LED program not working on Raspberry PI 3

Fri Mar 09, 2018 5:41 pm

What is with all the delays they are not needed and seem to be some hope and pray to fix a problem by random guesswork.

You need to explain something to me you reference the valvers work etc but your repository doesn't have the valvers bootstub or the linker file??

So questions
1.) Where exactly have you put the stack pointer and how is it set ?
2.) Can we see the linker file setting the entry point?

As you go on there is a lot more stuff that needs to happen in the bootstub before you go anywhere near C code, which you seem to be fixating on.


Return to “Bare metal, Assembly language”

Who is online

Users browsing this forum: No registered users and 4 guests