xlar54
Posts: 51
Joined: Tue Aug 20, 2013 5:08 am

SD card access

Mon Sep 23, 2013 5:05 pm

With my recent C64 emulator running natively on the Pi, it got me thinking about a new OS based on the old 8-bit style (boot to a BASIC interpreter), but running natively. Video can be peeked and poked to, just like on the C64 family of machines. I have all the tools (keyboard driver, framebuffer, graphics, etc), but one important thing is missing - the ability to read and write to the SDcard. I havent seen much here related to that. Is there any code out there which would allow a bare metal OS to r/w from the SD?

sprinkmeier
Posts: 410
Joined: Mon Feb 04, 2013 10:48 am
Contact: Website

Re: SD card access

Mon Sep 23, 2013 9:31 pm

There are Arduino libraries that do 'bare metal C' SD-card IO, I'd look at those.

jnc100
Posts: 54
Joined: Wed Feb 20, 2013 10:10 pm

Re: SD card access

Mon Sep 23, 2013 9:57 pm

https://github.com/jncronin/rpi-boot/blob/master/emmc.c for my SD block driver. Alternatively, download the whole repository and run make libfs.a for a reasonably complete FAT/ext2 system. Unfortunately, fat/ext2 writes are not supported, but the SD block layer does support writing so you could adapt one of the freely available fat libraries to it instead.

Regards,
John.

User avatar
DavidS
Posts: 4334
Joined: Thu Dec 15, 2011 6:39 am
Location: USA
Contact: Website

Re: SD card access

Mon Sep 23, 2013 10:04 pm

Have a look in the RPi BCM2835 Datasheet at section 10 SPI (Pages 148 through 159 in my copy) for the RPi specific component(We use SPi to communicate with the SD Card as this avoids needing a license). Then look at http://elm-chan.org/docs/mmc/mmc_e.html, (you may need to follow some of the links if you know absolutely nothing about MMC and SD) and then try to get a read only driver going for what ever FS you choose to use, if you have any questions at that point I believe that they will be easier to answer, as you should have some basic background.

I am sure that some one will come allong with a bigginer tutorial for SD that I am not aware of.

My biggest warning would be DO NOT USE FAT32 [or FAT16], as these File systems are terible in so many ways (there are many articles on the limitations of these file systems so I will not go into detail here). And FAT is actually more difficult to implement than many simple extents based File systems that are much more reliable, and more effecient.

And as always there is the possibility of overwriting your SD, blowing UP your Pi etc, I take no responcability for any damage, loss, or other negitive consequence of doing any of this.
RPi = The best ARM based RISC OS computer around
More than 95% of posts made from RISC OS on RPi 1B/1B+ computers. Most of the rest from RISC OS on RPi 2B/3B/3B+ computers

User avatar
DexOS
Posts: 876
Joined: Wed May 16, 2012 6:32 pm
Contact: Website

Re: SD card access

Wed Sep 25, 2013 8:31 am

There is code for read/write to SD card in DexBasic
http://www.dex-os.com/DexBasic/DexBasic.htm
Batteries not included, Some assembly required.

User avatar
DavidS
Posts: 4334
Joined: Thu Dec 15, 2011 6:39 am
Location: USA
Contact: Website

Re: SD card access

Tue Oct 01, 2013 3:54 am

Your macros have been helpful.

Though do you or any one know for sure if fasmarm can be compiled and ran on the RPi?

I was thinking about recomending your DexBasic to some friends to give them a feel for the RPi on bare metal, though I wish to do so in such a way that they do not have to buy a PC or use one of mine. So a simple OS selector that I have written for my own Projects plus Raspbian or RISC OS plus DexBasic seems to be the ideal solution.

Also I would like to rewrite my boot loader as an open source project that is a little more user friendly in DexBasic, and I do not remember seeing any licensing information last I looked (it may have just slipped my mind, so forgive me if it is obvious), so what is the license under which DexBasic is released.
RPi = The best ARM based RISC OS computer around
More than 95% of posts made from RISC OS on RPi 1B/1B+ computers. Most of the rest from RISC OS on RPi 2B/3B/3B+ computers

User avatar
DexOS
Posts: 876
Joined: Wed May 16, 2012 6:32 pm
Contact: Website

Re: SD card access

Wed Oct 02, 2013 6:53 pm

FasmArm only runs on x86, so for now can only be run on a x86 or x86 emulator.
The license is as follows

Code: Select all

DexBasic version 0.01
Copyright (c) 2002-2013, Craig Bamford.
All rights reserved.

This program is free for commercial and non-commercial use as long as
the following conditions are adhered to.

Copyright remains Craig Bamford, and as such any Copyright notices
in the code are not to be removed.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

1. Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

The license and distribution terms for any publically available
version or derivative of this code cannot be changed. i.e. this code
cannot simply be copied and put under another distribution license
(including the GNU Public License).
I will be releasing a Raspbian and RISC OS ver of DexBasic soon, so you can run the same app on bare metal or Raspbian, RISC OS by using the right include.
Batteries not included, Some assembly required.

User avatar
DavidS
Posts: 4334
Joined: Thu Dec 15, 2011 6:39 am
Location: USA
Contact: Website

Re: SD card access

Wed Oct 02, 2013 7:08 pm

@DexOS:
Thank you. I like that license.
RPi = The best ARM based RISC OS computer around
More than 95% of posts made from RISC OS on RPi 1B/1B+ computers. Most of the rest from RISC OS on RPi 2B/3B/3B+ computers

vchristo
Posts: 7
Joined: Wed Aug 28, 2013 12:23 am

Re: SD card access

Sat Oct 26, 2013 2:34 am

Hi, I modified the code from https://github.com/vanvught/OpenILDA/tr ... card-mmcbb to works with chibios-rpi, and it works fine using fat file system. It is work without spi driver, it is not so fast, but works very fine. if you want I can share my changes. the original lines are commented.

Code: Select all

#define		CS_H()		palSetPad(GPIO7_PORT, GPIO7_PAD)		 		
#define		CS_L()		palClearPad(GPIO7_PORT, GPIO7_PAD)
#define 	CK_H()		palSetPad(GPIO11_PORT, GPIO11_PAD)
#define		CK_L()		palClearPad(GPIO11_PORT, GPIO11_PAD)
#define 	DI_H()		palSetPad(GPIO10_PORT, GPIO10_PAD)
#define 	DI_L()		palClearPad(GPIO10_PORT, GPIO10_PAD)
#define 	DO		palReadPad(GPIO9_PORT, GPIO9_PAD)

/*#define	CS_H()		bcm2835_gpio_set(RPI_GPIO_P1_26)	 Set MMC CS "high" */
/*#define 	CS_L()		bcm2835_gpio_clr(RPI_GPIO_P1_26)	 Set MMC CS "low" */
/*#define 	CK_H()		bcm2835_gpio_set(RPI_GPIO_P1_23)	 Set MMC SCLK "high" */
/*#define	CK_L()		bcm2835_gpio_clr(RPI_GPIO_P1_23)	 Set MMC SCLK "low" */
/*#define 	DI_H()		bcm2835_gpio_set(RPI_GPIO_P1_19)	 Set MMC DI "high" */
/*#define 	DI_L()		bcm2835_gpio_clr(RPI_GPIO_P1_19)	 Set MMC DI "low" */
/*#define 	DO		bcm2835_gpio_lev(RPI_GPIO_P1_21)	 Test for MMC DO ('H':true, 'L':false) */



void init_port(void) {
/*	bcm2835_init();  */
	palSetPadMode(GPIO11_PORT, GPIO11_PAD, PAL_MODE_OUTPUT);
	palClearPad(GPIO11_PORT, GPIO11_PAD);
/*	bcm2835_gpio_fsel(RPI_GPIO_P1_23, BCM2835_GPIO_FSEL_OUTP); // CLK */
/*	bcm2835_gpio_clr(RPI_GPIO_P1_23); */
	palSetPadMode(GPIO10_PORT, GPIO10_PAD, PAL_MODE_OUTPUT);
	palClearPad(GPIO10_PORT, GPIO10_PAD);
/*	bcm2835_gpio_fsel(RPI_GPIO_P1_19, BCM2835_GPIO_FSEL_OUTP); // MOSI
	bcm2835_gpio_set(RPI_GPIO_P1_19);  */
	
	palSetPadMode(GPIO7_PORT, GPIO7_PAD, PAL_MODE_OUTPUT);
	palSetPad(GPIO7_PORT, GPIO7_PAD);
	
/*	bcm2835_gpio_fsel(RPI_GPIO_P1_26, BCM2835_GPIO_FSEL_OUTP); // CE1
	bcm2835_gpio_set(RPI_GPIO_P1_26);  */
	
	palSetPadMode(GPIO8_PORT, GPIO8_PAD, PAL_MODE_OUTPUT);
	palSetPad(GPIO8_PORT, GPIO8_PAD);
	
/*	bcm2835_gpio_fsel(RPI_GPIO_P1_21, BCM2835_GPIO_FSEL_INPT); // MISO */
	palSetPadMode(GPIO9_PORT, GPIO9_PAD, PAL_MODE_INPUT_PULLUP);
}

Return to “Bare metal, Assembly language”