adhaliwal
Posts: 2
Joined: Sun Sep 13, 2015 11:55 pm

Seriously at my wit's end (Baking Pi)

Mon Sep 14, 2015 12:28 am

I've recently gotten myself a RPi 2 in an attempt to learn how computers work on a much lower level, and to this end I've been trying to do the Baking Pi tutorials.

Now, knowing that the Baking Pi tutorials are were created for an older model of the RPi, I searched a little bit online beforehand, and found the new correct address for the GPIO controller was 0x3F000000. I wrote the code for the OK (ACT?) LED blink, and everything compiled just fine on my Linux laptop.

The real problem came when I had to load this onto my RPi, as the tutorial was not very clear on this. First I thought I'd be fine just copying the kernel.img on. Obviously this did not work, so I did more research and found out that I needed some extra files. I formatted my microSD card to FAT, downloaded the RPi firmware off of github, and copied the boot folder onto the SD card, replacing both kernel.img and kernel7.img with my own compiled kernel.img.

Now, all the research i've done seems to indicate that this should work, but it just doesn't. The red PWR light comes on, I get some halfhearted blinking from the ACT LED, and... nothing.

So my question is: How do I properly use and boot the kernel image I've compiled given a blank FAT32 SD card? I'm seriously at my wits end - I really want to move on with this and I have no idea what to do!

Here's my ASM code:

Code: Select all

.section .init
.globl _start
_start:

ldr r0,=0x3F000000

mov r1,#1
lsl r1,#18

str r1,[r0,#4]

mov r1,#1
lsl r1,#16

loop$:
str r1,[r0,#40]

mov r2,#0x3F0000
wait1$:
	sub r2,#1
	cmp r2,#0
	bne wait1$

str r1,[r0,#28]

mov r2,#0x3F0000
wait2$:
	sub r2,#1
	cmp r2,#0
	bne wait2$
b loop$

User avatar
rpdom
Posts: 15234
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Seriously at my wit's end (Baking Pi)

Mon Sep 14, 2015 4:18 am

There are some other changes that need to be made because of the difference between the Pi B and 2B.

The ACT LED is now on GPIO 47 instead of GPIO 16, so you need to change the addresses.

Code: Select all

.section .init
.globl _start
_start:

ldr r0,=0x3F000000

mov r1,#1
lsl r1,#21

str r1,[r0,#16]

mov r1,#1
lsl r1,#17

loop$:
str r1,[r0,#44]

mov r2,#0x3F0000
wait1$:
   sub r2,#1
   cmp r2,#0
   bne wait1$

str r1,[r0,#2C]

mov r2,#0x3F0000
wait2$:
   sub r2,#1
   cmp r2,#0
   bne wait2$
b loop$
I think I got those changes right, I didn't look them up and I don't have a 2B I can spare for testing at the moment.

There may be other issues with the CPUs too. I can't remember what it is exactly.

turboscrew
Posts: 174
Joined: Sat Jan 18, 2014 1:50 pm
Location: Nokia (town), Finland

Re: Seriously at my wit's end (Baking Pi)

Mon Sep 14, 2015 10:01 pm

The main sources of info about RPi 2B are:
  • ARM Architecture Reference Manual, ARMv7-A and ARMv7-R edition (cores: instruction sets, modes, states, configuration, MMU, caches, ...)
  • BCM2835-ARM-Peripherals.pdf (integrated peripherals: UART, interrupt controller, GPIO, ...)
  • This forum (board itself, like ACT-LED is GPIO 47, and IO base address is 0x3F000000)
  • BCM2836 ARM-local peripherals (maybe later)
The first document you can find here:
http://infocenter.arm.com/help/index.js ... index.html
and the other two documents here:
https://www.raspberrypi.org/documentati ... /README.md

Most of the other stuff (as needed) you can find in the ARM site.
De-bugging is for sissies - real men do de-monstrations.

adhaliwal
Posts: 2
Joined: Sun Sep 13, 2015 11:55 pm

Re: Seriously at my wit's end (Baking Pi)

Thu Sep 17, 2015 1:39 am

Thanks so much guys. The fixed code didn't work but I'll try some things.

User avatar
rpdom
Posts: 15234
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Seriously at my wit's end (Baking Pi)

Thu Sep 17, 2015 2:41 am

rpdom wrote:I don't have a 2B I can spare for testing at the moment.
I've just ordered another Pi 2B, so I hope to be able to work on some of this code in a few days.

turboscrew
Posts: 174
Joined: Sat Jan 18, 2014 1:50 pm
Location: Nokia (town), Finland

Re: Seriously at my wit's end (Baking Pi)

Fri Sep 18, 2015 9:20 pm

Does this help?

Code: Select all

init_debug_led:
	mov r2, #150 @ delay count
	mov r3, #150 @ delay count
	ldr r1, =0x8000 @ 1 << 15
	mov r0, #0
	ldr r6, =0x3f200000
	str r0, [r6, #0x94] @ 0 -> GPPUD
5:
	subs	r2, r2, #1 @ delay 150 cycles
	bne 5b
	str r1, [r6, #0x9c] @ 0 -> GPPUDCLK1
6:
	subs	r3, r3, #1 @ delay 150 cycles
	bne 6b
	str r0, [r6, #0x94] @ 0 -> GPPUD
	str r0, [r6, #0x9c] @ 0 -> GPPUDCLK1

	str r1, [r6, #0x2c] @ (1<<15) -> GPIO_CLRREG1

	ldr r3, [r6, #0x10] @ read GPFSEL4
	ldr r4, =0x00e00000
	bic r3, r4
	mov r2, #0x1 @ or with 1 << 21 (output)
	lsl r2, #21
	orr r3, r2
	str r3, [r6, #0x10] @ write GPFSEL4
	bx lr

debug_wait: @ ms count in r3
	push {r8, r9, r10, lr}
	mov r8, r3
	bl debug_wait1
	pop {r8, r9, r10, lr}
	bx lr

debug_wait1: @ 'ms' count in r8
	cmp	r8, #0
	bxeq	lr
	mov	r10, #0
loop4$:
	ldr	r9, =0x0000028a
loop5$:
	nop	{0}
	subs	r9, r9, #1
	bne loop5$
	add	r10, r10, #1
	cmp	r10, r8
	bne loop4$
	bx	lr

debug_blink: @ r0=on, r1=off, r2=count, 0x1000 ~ 1s
	push {r0, r1, r6, r7, r8, r9, r10, lr}
	ldr r6, =0x8000 @ 1 << 15
	@mov r7, #0
	@movt r7, #0x3f20
	ldr r7, =0x3f200000 @ GPIO base
loop6$:
	str r6, [r7,#0x20] @ (1<<15) -> GPIO_SETREG1
	mov r8, r0 @ on
	bl debug_wait1
	str r6, [r7,#0x2c] @ (1<<15) -> GPIO_CLRREG1
	mov r8, r1 @ off
	bl debug_wait1
	subs r2, #1
	bne loop6$

	pop {r0, r1, r6, r7, r8, r9, r10, lr}
	bx lr
De-bugging is for sissies - real men do de-monstrations.

Blackjak34
Posts: 1
Joined: Sun Sep 20, 2015 12:59 am

Re: Seriously at my wit's end (Baking Pi)

Sun Sep 20, 2015 1:09 am

I can confirm that this version of the code works on my 2B.

Code: Select all

.section .init
.globl _start
_start: ldr r0, =0x3F200000
        mov r1, #1
        lsl r1, #21
        str r1, [r0, #0x10]
        mov r1, #1
        lsl r1, #15
loop1$: str r1, [r0, #0x20]
        mov r2, #0x3F0000
loop2$: sub r2, #1
        cmp r2, #0
        bne loop2$
        str r1, [r0, #0x2C]
        mov r2, #0x3F0000
loop3$: sub r2, #1
        cmp r2, #0
        bne loop3$
        b loop1$
It also sounds like you might not have set up the fs on the sd card properly. Instead of copying and pasting things I needed to download the raspbian image and then use dd to write it onto the card directly. If it's done right then there's a separate partition on the card for the boot files.

JacobL
Posts: 76
Joined: Sun Apr 15, 2012 2:23 pm

Re: Seriously at my wit's end (Baking Pi)

Sat Oct 10, 2015 9:41 pm

It also sounds like you might not have set up the fs on the sd card properly. Instead of copying and pasting things I needed to download the raspbian image and then use dd to write it onto the card directly. If it's done right then there's a separate partition on the card for the boot files.
Doesn't matter. The first partition must be a fat32 partition, but that is basically it. It might need to be marked as boot, I don't remember that. Anyway, just copying the boot files to the empty fat32 partition works fine for me, I even did it on a brand new SD card with the default fat32 partition. You might need a config.txt though, not sure.

turboscrew
Posts: 174
Joined: Sat Jan 18, 2014 1:50 pm
Location: Nokia (town), Finland

Re: Seriously at my wit's end (Baking Pi)

Sun Oct 11, 2015 8:47 pm

I get some halfhearted blinking from the ACT LED, and... nothing.
Check the blinking against this:
http://elinux.org/R-Pi_Troubleshooting# ... ic_pattern
De-bugging is for sissies - real men do de-monstrations.

User avatar
edargelies
Posts: 7
Joined: Sun Aug 09, 2015 11:47 pm
Location: Oakland, CA
Contact: Website

Re: Seriously at my wit's end (Baking Pi)

Thu Oct 15, 2015 1:33 am

Hi. I hope you've had some success, but if not I've completed most of these lessons on the raspberry pi 2, just about to start screen04. You can check out my solutions and updates to the lessons for the Raspberry Pi 2 here: https://github.com/edargelies/bakingpi-www. You're on the right track with the kernel. All you should have to do if you followed Alex's instructions is make the source and overwrite the kernel image with your image. Later on in the screen lessons you may have to modify the linker script, but that's all I've had to do so far. The raspberry pi 2 has a ARMv7 processor in it. On startup it checks the hardware to figure out which kernel to run. If it detects the ARMv7 it'll run the kernel7.img. What I did was modify the make file to output kernel7.img, just so I didn't have to rename it each time. Then just overwrite the kernel with your kernel7.img. As for the blinking of the LED at startup, that's normal, although if it doesn't stay lit you probably have an issue in your assembly code. Check out my code below, I've confirmed this works on the RPi2.

Code: Select all

-------------------------------------------------------------------------------
MAIN.S
-------------------------------------------------------------------------------
@ Initialization
.section .init
.globl _start
_start:
@ Credit to user nburman of Raspberry Pi forums who found
@ the new base address and GPIO pin for the ACT LED of the
@ Raspberry Pi 2.
 
@ Load the address of the GPIO controller
ldr r0,=0x3f200000
@ enable the 47th GPIO pin
mov r1,#1
lsl r1,#21
str r1,[r0,#16]
@ Turn on the LED
mov r1,#1
lsl r1,#15
str r1,[r0,#32]
@ Give processor loop task
loop$:
b loop$

dark_nite77
Posts: 1
Joined: Thu Oct 22, 2015 4:30 am

Re: Seriously at my wit's end (Baking Pi)

Wed Oct 28, 2015 12:15 am

Hi !

I was able to complete the OK LED series (for rpi2) pretty quickly, maybe coz the changes were simple. Have been trying Screen01 for few days with no success. Finally, I also tried using images from edargelies's baking-pi for rpi-2 with no success.

Are there any changes apart from following ?

1. Peripheral Address (fixed to x3F......)
2. Substraction of xC0000000 to account for uncached bus access for DMA
3. Instead of using the 1024x768, I used 1920x1080 to match the resolution of my screen.
4. When nothing else worked, I also incorporated changes from edargelies's kernel.ld .

Nothing seems work :( Am I missing something here ? Also lack of proper documentation is a big hinderance.

User avatar
edargelies
Posts: 7
Joined: Sun Aug 09, 2015 11:47 pm
Location: Oakland, CA
Contact: Website

Re: Seriously at my wit's end (Baking Pi)

Thu Nov 05, 2015 6:48 am

Hey it's interesting that the kernel images wouldn't work for you. They should work with everything else held constant. What did you use as a base os image? I know there was another person having trouble with the screen series and that was their issue. I used the official raspbian wheezy image for my run through.

Return to “Bare metal, Assembly language”