{"id":2817,"date":"2023-01-08T20:16:04","date_gmt":"2023-01-09T04:16:04","guid":{"rendered":"https:\/\/doubleecpu.com\/?page_id=2817"},"modified":"2023-02-12T17:44:31","modified_gmt":"2023-02-13T01:44:31","slug":"rustonlyraspberrypi","status":"publish","type":"page","link":"https:\/\/doubleecpu.com\/index.php\/rustonlyraspberrypi\/","title":{"rendered":"Rust Only RaspberryPi Embedded"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Setup of Rust Development Environment<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Add Target for Arm architecture<\/h3>\n\n\n\n<p>Add the compiling for arm architecture if not on an armv7 processor<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>rustup target add armv7a-none-eabi<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"638\" height=\"47\" src=\"https:\/\/doubleecpu.com\/wp-content\/uploads\/2023\/01\/image-4.png\" alt=\"\" class=\"wp-image-2829\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Create Rust Package<\/h3>\n\n\n\n<p>Create a Cargo package and include the dependencies <br>Use:  cargo new &#8216;package name&#8217; package<br>This will create a package name directory with Cargo.toml and src directory with main.rs <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Setup rust to compile for Arm without linux<\/h3>\n\n\n\n<p>In package name directory create a directory called .cargo <br>and add a file called config<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"338\" height=\"220\" src=\"https:\/\/doubleecpu.com\/wp-content\/uploads\/2023\/01\/image-2.png\" alt=\"\" class=\"wp-image-2827\"\/><\/figure>\n\n\n\n<p>In the config file add the build configuration for the Processor used in the Raspberry Pi<br>Pi3 B+ uses ARMv7 <\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"595\" height=\"142\" src=\"https:\/\/doubleecpu.com\/wp-content\/uploads\/2023\/01\/image-3.png\" alt=\"\" class=\"wp-image-2828\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Setting up Code File<\/h3>\n\n\n\n<p>Adjust the main.rs to not include any standard libraries <br>adjust the code entry point, to not use main( )  as a start( ) function will be used<br>make the start function globally accessible for linking so it is ordered the right way<br>provide external label C and make it manageable by using #![no mangle]<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Create Panic<\/h3>\n\n\n\n<p>Provide a state panic handler to catch and resolve errors encountered in code<br>use core::panic::PanicInfo;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"490\" height=\"383\" src=\"https:\/\/doubleecpu.com\/wp-content\/uploads\/2023\/01\/image-5.png\" alt=\"\" class=\"wp-image-2830\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>#!&#91;no_std]\n#!&#91;no_main]\n\nuse core::panic::PanicInfo;\n\n#&#91;no_mangle]\npub extern \"C\" fn _start() -&gt; ! {\n    loop{}\n}\n\n#&#91;panic_handler]\nfn panic(_info: &amp;PanicInfo) -&gt; ! {\n    loop{}\n}<\/code><\/pre>\n\n\n\n<p>With this minimum code setup running the command cargo build should complete successfully to indicate Rust is configured.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1205\" height=\"71\" src=\"https:\/\/doubleecpu.com\/wp-content\/uploads\/2023\/01\/image-6.png\" alt=\"\" class=\"wp-image-2831\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Use ELF Binary File Reader<\/h2>\n\n\n\n<p>If using Windows download and install XELFviewer from github<br>https:\/\/github.com\/horsicq\/XELFViewer\/releases <\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/doubleecpu.com\/wp-content\/uploads\/2023\/01\/image-7.png\" alt=\"\" class=\"wp-image-2834\" width=\"502\" height=\"65\"\/><\/figure>\n\n\n\n<p>Extract contents of zip file and run xelfviewer.exe <br>Open the Object Dump File created by Rust (name will match the rust package name)<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1485\" height=\"745\" src=\"https:\/\/doubleecpu.com\/wp-content\/uploads\/2023\/01\/image-8.png\" alt=\"\" class=\"wp-image-2837\"\/><figcaption class=\"wp-element-caption\">Make note of the Entry point address: 000200e4<br>This needs to be changed to 0008000e4 in linker<\/figcaption><\/figure>\n\n\n\n<p>To install in debian based distro Install the following<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt-get install file\nsudo apt-get elfrc elfutils libasm1\nsudo apt-get install gcc-arm-none-eabi<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Create Linker Script<\/h2>\n\n\n\n<p>Next the Hex Code for the start address needs to be set by the Linker Script when building the solution.<br>In the package folder create text file called linker.ld and set the following script<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ENTRY(_start)\n \nSECTIONS\n{\n    . = 0x8000;\n    __start = .;\n    .text :\n    {\n        KEEP(*(.text.boot))\n        *(.text)\n    }\n    . = ALIGN(4096);\n    .rodata :\n    {\n        *(.rodata)\n    }\n    . = ALIGN(4096);\n    .data :\n    {\n        *(.data)\n    }\n    . = ALIGN(4096);\n    __bss_start = .;\n    .bss :\n    {\n        bss = .;\n        *(.bss)\n    }\n    . = ALIGN(4096); \n    __bss_end = .;\n    __bss_size = __bss_end - __bss_start;\n    __end = .;\n}<\/code><\/pre>\n\n\n\n<p>Add to main.rs<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!&#91;no_std]\n#!&#91;no_main]\n\nuse core::panic::PanicInfo;\nmod boot {\n    use core::arch::global_asm;\n\n    global_asm!(\n        \".section .text._start\"\n    );\n}\n#&#91;no_mangle]\npub extern \"C\" fn _start() -&gt; ! {\n    loop{}\n}\n\n#&#91;panic_handler]\nfn panic(_info: &amp;PanicInfo) -&gt; ! {\n    loop{}\n}<\/code><\/pre>\n\n\n\n<p>Run the rust compiler this time with the linker script. *Delete Target Folder whenever you modify the linker.ld file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cargo rustc -- -C link-arg=--script=.\\linker.ld<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1292\" height=\"75\" src=\"https:\/\/doubleecpu.com\/wp-content\/uploads\/2023\/01\/image-9.png\" alt=\"\" class=\"wp-image-2840\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1111\" height=\"317\" src=\"https:\/\/doubleecpu.com\/wp-content\/uploads\/2023\/01\/image-10.png\" alt=\"\" class=\"wp-image-2841\"\/><figcaption class=\"wp-element-caption\">_start is now 0008000<\/figcaption><\/figure>\n\n\n\n<p>Next the unusable code generated for Linux must be removed to run on bare metal RaspberryPi <br>The code to run on the RaspberryPi must be extracted from the ELF file. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> arm-none-eabi-objcopy -O binary rust_pi_embedded .\/rust_pi.img<\/code><\/pre>\n\n\n\n<p>This will create the executable code that will run on the raspberry pi as &#8220;baremetal&#8221; without Linux OS<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Reference Documentation for GPIO<\/h2>\n\n\n\n<p>When designing software for the hardware of RaspberryPi without Linux the Broadcom BCM3837 ARM Peripherals manual should be used. <a href=\"https:\/\/www.raspberrypi.com\/documentation\/computers\/processors.html\">Official RasberryPi Links<\/a> <\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1112\" height=\"417\" src=\"https:\/\/doubleecpu.com\/wp-content\/uploads\/2023\/01\/image-11.png\" alt=\"\" class=\"wp-image-2842\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"593\" height=\"647\" src=\"https:\/\/doubleecpu.com\/wp-content\/uploads\/2023\/01\/image-12.png\" alt=\"\" class=\"wp-image-2843\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"532\" height=\"862\" src=\"https:\/\/doubleecpu.com\/wp-content\/uploads\/2023\/01\/image-13.png\" alt=\"\" class=\"wp-image-2844\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"520\" height=\"638\" src=\"https:\/\/doubleecpu.com\/wp-content\/uploads\/2023\/01\/image-14.png\" alt=\"\" class=\"wp-image-2845\"\/><\/figure>\n\n\n\n<p>Set Pin to GPIO  <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Setting up GPIO pins<\/h3>\n\n\n\n<p>Setting Pin 21 to on and off. <\/p>\n\n\n\n<p>Select Pin From the datasheet and copy the register address <\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Add a do nothing loop<\/p>\n\n\n\n<p>Setting Up i2c<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Firmware Files<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Download Github Firmware files<\/h3>\n\n\n\n<p>From the <a href=\"https:\/\/github.com\/raspberrypi\/firmware\">github repository<\/a> download and install files required to run the image on the RaspberryPi<br>https:\/\/github.com\/raspberrypi\/firmware\/blob\/master\/boot\/bootcode.bin<br>https:\/\/github.com\/raspberrypi\/firmware\/blob\/master\/boot\/fixup.dat<br>https:\/\/github.com\/raspberrypi\/firmware\/blob\/master\/boot\/start.elf<br><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Create Config<\/h3>\n\n\n\n<p>Create a file config.txt include the following configuration settings<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>arm_64bit=0<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Combine the Files into a directory <\/h3>\n\n\n\n<p>copy the files into a directory: bootcode.bin  fixup.dat start.elf config.txt kernel7.img<\/p>\n\n\n\n<p>to boot partition<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Setup of Rust Development Environment Add Target for Arm architecture Add the compiling for arm architecture if not on an armv7 processor Create Rust Package Create a Cargo package and include the dependencies Use: cargo new &#8216;package name&#8217; packageThis will create a package name directory with Cargo.toml and src directory with main.rs Setup rust to &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/doubleecpu.com\/index.php\/rustonlyraspberrypi\/\" class=\"more-link\">Read more<span class=\"screen-reader-text\"> &#8220;Rust Only RaspberryPi Embedded&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2817","page","type-page","status-publish","hentry"],"featured_media_urls":[],"_links":{"self":[{"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/pages\/2817","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/comments?post=2817"}],"version-history":[{"count":9,"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/pages\/2817\/revisions"}],"predecessor-version":[{"id":2872,"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/pages\/2817\/revisions\/2872"}],"wp:attachment":[{"href":"https:\/\/doubleecpu.com\/index.php\/wp-json\/wp\/v2\/media?parent=2817"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}