Giter Site home page Giter Site logo

briansidebotham / arm-tutorial-rpi Goto Github PK

View Code? Open in Web Editor NEW
583.0 583.0 178.0 10.72 MB

Raspberry-Pi Bare Metal Programming in C Tutorial

Home Page: https://www.valvers.com/open-software/raspberry-pi/bare-metal-programming-in-c-part-1/

License: MIT License

CMake 5.99% C 76.87% Assembly 8.92% Shell 7.80% Dockerfile 0.10% Python 0.32%

arm-tutorial-rpi's People

Contributors

arcturus140 avatar ark76r avatar briansidebotham avatar j-marjanovic avatar nickbegley avatar nicola-masarone avatar paulwratt avatar software2000 avatar timgates42 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

arm-tutorial-rpi's Issues

part-1 / armc-03 : _start defaulting to 0x8024 (small stack @ 0x8000)

Hi Brian,

Thanks for your great tutorials! I wanted to alert you to a problem I just encountered, in case others experience similar...

I was compiling (build.sh) and trying to run part-1 / armc-03 and watching for a blinking LED, with no joy. A clue as to the problem was coming out of the linker:

/arm-none-eabi/bin/ld: warning: cannot find entry symbol _start; defaulting to 0000000000008024.

Using objdump to produce a disassembly listing, I found the linker was putting a small stack (space reservation) at 0x8000 and following that with the main() entry point. If I understand Pi booting correctly, that would have the CPU jumping into the stack instead of to main().

I've gotten relief by adding some linker directives: (file linker.ld)

MEMORY
{
    ram : ORIGIN = 0x8000, LENGTH = 20K
}

SECTIONS
{
    .text : { *(.text*) } > ram
    .note.gnu.build-id : { *(.text*) } > ram    
    .bss : { *(.bss*) } > ram
}

And adding this, via the -T option, to the executable build:

${toolchain}gcc ${cflags} ${scriptdir}/*.c -T linker.ld -o ${kernel_elf}

In short, I had to force the .text section (i.e. the main() program) to be at the top of the img. This (finally) yielded a satisfying blink. :-)

For what it matters: I'm cross-compiling for pi0 from Ubuntu 18.04 using ARM Corp's gnu

    armc-03$ arm-none-eabi-gcc --version
    arm-none-eabi-gcc (GCC) 8.2.0
    Copyright (C) 2018 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Cheers!

part-5 is not rpi4 compatible

Currently part-5 of the tutorial does not support all rpi variants. It's necessary to make changes to the tutorial in order to get part-5 working on all raspberry pi models.

Travis CI Failing because firmware repository is not cloned

Perhaps due to some caching or something that Travis-CI integration is not working correctly as is showing part-3 is failing to build.

This shouldn't be the case. Only part-5 should currently fail as it has not been improved to include rpi4 support.

malloc (_sbrk) hangs in arm013

Hi,

Since you moved the stack from the end of the RAM to before the code (and before the heap) the _sbrk function will hang always because the following condition will be always true:
(heap_end + incr) > _get_stack_pointer()

If the stack is before the heap then I would check something like this:
(heap_end + incr) > END_OF_RAM

Regards,
Bertalan

Fedora 33 - Cannot create an SD Card Image

The sfdisk command in make_card.sh fails with the following message:

>>> Script header accepted.
>>> Created a new DOS disklabel with disk identifier 0x857bb0cc.
/tmp/tmp.MOPKz1DYEZ1: No free sectors available.
Failed to add #1 partition: No space left on device
Leaving.

This is due to a bug in sfdisk from util-linux 2.36 which is the current version available in Fedora 33.

A fix for this issue has been committed upstream. Just waiting for the fix to filter down into the Fedora repositories..

https://bugzilla.redhat.com/show_bug.cgi?id=1860461

Apparently it has already filtered down, but as of right now this bug is still present.

CMake CMAKE_FORCE_C_COMPILER macro is deprecated

The use of CMAKE_FORCE_C_COMPILER is now deprecated.

Instead, we need to guide CMake to discover the compiler more sanely by letting it come a test program. This will sometimes fail for embedded compilers, especially those without libc. However, in this case we can swap the test compilation to a static library instead of an executable to prevent such failures occurring.

CMake Deprecation Warning at /usr/share/cmake/Modules/CMakeForceCompiler.cmake:75 (message):
  The CMAKE_FORCE_C_COMPILER macro is deprecated.  Instead just set
  CMAKE_C_COMPILER and allow CMake to identify the compiler.
Call Stack (most recent call first):
  /home/bjs/d/github/valvers/arm-tutorial-rpi/compiler/cmake-toolchains/toolchain-arm-none-eabi-rpi4.cmake:33 (CMAKE_FORCE_C_COMPILER)
  /usr/share/cmake/Modules/CMakeDetermineSystem.cmake:93 (include)
  CMakeLists.txt:35 (project)


CMake Deprecation Warning at /usr/share/cmake/Modules/CMakeForceCompiler.cmake:75 (message):
  The CMAKE_FORCE_C_COMPILER macro is deprecated.  Instead just set
  CMAKE_C_COMPILER and allow CMake to identify the compiler.
Call Stack (most recent call first):
  /home/bjs/d/github/valvers/arm-tutorial-rpi/compiler/cmake-toolchains/toolchain-arm-none-eabi-rpi4.cmake:33 (CMAKE_FORCE_C_COMPILER)
  build/CMakeFiles/3.18.3/CMakeSystem.cmake:6 (include)
  CMakeLists.txt:35 (project)

I'm using cmake version 3.18.3

Setting MAXPAGESIZE instead of changing linker script (BMC Part 2 - The C Runtime)

First off, absolutely love this tutorial!

Not sure this is a huge deal, but in part 2 you adjust the linked script to get rid of an odd page-alignment behavior. This behavior is governed by the PAGESIZE linker constant that can be controlled by a linked '-z max-page-size=' CLI option, which is also described here.
I tried this and the page size was reduced accordingly, yielding a smaller image once the .data section is aligned to the new size.
Perhaps a note in the README would be appropriate?

I compile with a '-c' on arm-none-eabi-gcc and then use the '-z' with arm-none-eabi-ld

Part-1 doesn't work on RPI-2

Hi Brian, thank you for your great work on this tutorial !
I'm quite new to Raspberry Pi programming and I apologize in advance if I misunderstood something in your tutorial.

I tried to run your code on my Raspberry PI 2 and the LED remains off. I tried your pre-build binary which is not working for me. I also tried to build the code for Rasperry PI (the old one) and it's working great.

I also noticed something strange :

The following is working on the RPI2 is waiting a few seconds before turning on the LED (which is normal) :

while(1)
{
for(tim = 0; tim < 5000000; tim++);
gpio[LED_GPSET] = (1 << LED_GPIO_BIT);
for(tim = 0; tim < 5000000; tim++);
//gpio[LED_GPCLR] = (1 << LED_GPIO_BIT);
}

Whereas , the following code is not working at all on the RPI2 (I have uncommented the instruction to turn off the LED) :

while(1)
{
for(tim = 0; tim < 5000000; tim++);
gpio[LED_GPSET] = (1 << LED_GPIO_BIT);
for(tim = 0; tim < 5000000; tim++);
gpio[LED_GPCLR] = (1 << LED_GPIO_BIT);
}

NB : The rest of the code is just like yours

NB2 : I've tested it with your start.elf and bootcode.bin AND the latest start.elf and bootcode.bin from RPI firmware.

Louis (Original Comment )

Add license

From the headers of some files it would appear that this project is licensed under the 2-Clause BSD License. It would be helpful to add a LICENSE file.

Broken / stale web reference

In tutorial BMC Part 2 (The C Runtime) part-2/armc-06, the student is encouraged to look at the ARM instruction set quick reference. ARM did you a dis-service by removing that PDF at http://infocenter.arm.com/help/topic/com.arm.doc.qrc0001l/QRC0001_UAL.pdf but copies of the PDF are still easily-found via Google. E.g. Cambridge has it at: https://www.cl.cam.ac.uk/teaching/2004/ECADArch/datasheets/arm_quick.pdf

Admittedly, as far as problems go, this is on-par with mouse-nuts. (I.e. in the global scheme of things, mouse-nuts are quite tiny... but they're still important to the mouse.) Feel free to dissuade me, if such (well-intended) observations are getting tedious!

gcc version 9.2.1 20191025 (release) Regression - armc-00.c

Thanks for a fantastic tutorial. Just want to let you know that the newest version of arm-none-eabi-gcc fails to compile arm-00.c with an undefined reference to _exit.[gopal] ~/projects/oldRaspBerryPI/arm-tutorial-rpi/part-1/armc-00 > ./build.sh
usage: build.sh
pi-model options: rpi0, rpi1, rpi1bp, rpi2, rpi3, rpi4, rpibp
[gopal] ~/projects/oldRaspBerryPI/arm-tutorial-rpi/part-1/armc-00 > ./build.sh rpi1
arm-none-eabi-gcc -mfloat-abi=hard -mfpu=vfp -march=armv6zk -mtune=arm1176jzf-s /Users/gopal/projects/oldRaspBerryPI/arm-tutorial-rpi/part-1/armc-00/*.c -o /Users/gopal/projects/oldRaspBerryPI/arm-tutorial-rpi/part-1/armc-00/kernel.armc-00.rpi1.elf
/usr/local/Cellar/arm-none-eabi-gcc/9-2019-q4-major/gcc/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld: /usr/local/Cellar/arm-none-eabi-gcc/9-2019-q4-major/gcc/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/lib/arm/v5te/hard/libc.a(lib_a-exit.o): in function exit': exit.c:(.text.exit+0x2c): undefined reference to _exit'
collect2: error: ld returned 1 exit status
ERROR: Failed to compile!

part02 - failed to simulate gdb

in Step02, Simulation:

after running (gdb) target sim

I get the following output:
Target byte order unspecified
unable to create simulator instance

how to fix:
(gdb) set endian little
(gdb) target sim

RPI B+ problems with part-1

The bin located in part-1/armc-03/bin-rpi-bplus/kernel.img is not working on my RPI B+.

I think it is a code problem (maybe is not actualizated with the information given by florin on his coment).

When I change the kernel.img and I put the raspbian img its working so... I think the problem is not in my device or something like that.

thank you

can't find kernel.img in new ubuntu isos

To use the compiled kernel CMU tutorial says:

To install your operating system, first of all get a Raspberry PI SD card which has an operating system installed already. If you browse the files in the SD card, you should see one called kernel.img. Rename this file to something else, such as kernel_linux.img. Then, copy the file kernel.img that make generated onto the SD Card. You've just replaced the existing operating system with your own. To switch back, simply delete your kernel.img file, and rename the other one back to kernel.img. I find it is always helpful to keep a backup of you original Raspberry Pi operating system, in case you need it again.

I downloaded snappy-ubuntu-core and raspbian, and neither had kernel.img file in it.

How do you suggest I can proceed? Let me know if I missed something obvious somewhere.

ARMC-09 : gdb simulator gives Unhandled v6 thumb insn: 4601

When debugging armc-09 with the arm-none-eabi-gdb simulator the error message

Unhandled v6 thumb insn: 4601

To repeat:

arm-none-eabi-gdb
file kernel.elf
target sim
load
break _start
run
s
....
s

is thrown from inside malloc in newlib.

Is it possible to run the framebuffer & UART example through qemu?

I am having trouble viewing both the framebuffer animation and connecting to the UART socket.

The command I am using to build is
./build.sh rpi3bp

And running qemu:
qemu-system-aarch64 -M raspi3b -kernel build/kernel.armc-014.rpi3bp.img -serial stdio

I care not so much about viewing the framebuffer but would like to see the output of the printf commands but nothing is spat out :/

Any help would be appreciated, thanks

Can I use a sub-function to control led in bare metal code?

Hello ! I followed the instructions in the document BMC Part 1-Getting Started to write a bare metal code to apply to my Raspi zero w. When I write all code in the main function, everything is normal, but when I control the LED as a sub-function and call the form in main(), after the board is powered on, the LED only lights up once and then goes out forever. At present, I have not found any grammatical problems in my program. I really want to know the reasons for this phenomenon. If you can answer me, I would be very grateful!

Problem with interrupts on the Pi2

I am having a problem with interrupts on the Pi2. It appears that the first interrupt causes execution to jump to somewhere and not return. I have checked the disassembly listing and the vector table looks ok.Has anyone had this problem and fixed it?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.