Show Posts
Pages: 1 ... 68 69 [70] 71 72 ... 99
1036  Development / Other Software Development / Re: Small SD card code? on: February 01, 2012, 04:38:44 pm

The library is an attachment to the message with the readme.txt quote.  You must be logged in to see it.
1037  Development / Other Software Development / Re: Small SD card code? on: February 01, 2012, 11:05:12 am
Here is a more complete SD read program that might be small enough to use in a boot loader.  You need to login to download the attached file.

It works with FAT16 formatted standard SD cards on 168/328 Arduinos.  It opens a file and reads it a byte at a time.

It appears to take about 1200 bytes of flash.  This sketch takes 1618 bytes on 0022.
#include <SdBoot.h>
void setup() {
  // open TEST.BIN
  fileOpen((uint8_t*)"TEST    BIN");
  // read a byte
void loop() {}
An empty sketch takes 450 bytes:
void setup() {}
void loop() {}
Here is the readme.txt file:
Here is a small file read program.  It appears to take about 1200 bytes of flash.

It could be optimized more and the retry timeouts need adjusting.

It runs on a 168/328 Arduino with an SD module that uses pin 10 for chip select.

To change the chip select pin edit this section of SdBoot.cpp

// use pin 10 for chip select
#define CS_DDR DDRB
uint8_t const CS_BIT = 2;

It is packaged as a library for testing. 

Just copy the SdBoot folder to your libraries folder and run the examples.

There are three examples:

writeTest.pde write a four MB test file. It requires SdFat.

readTest.pde reads the test file.

sizeTest.pde shows approximately how much flash is used.
1038  Using Arduino / Storage / Re: SDFat and interrupts on: February 01, 2012, 08:00:07 am
You can't close the file in an ISR.  If you write data, an SD write may be in progress when the interrupt happens.  The call to sync in the ISR will corrupt the file.  An SD write can be required for the next byte that is written after a sync call.  In fact writing a single byte can cause several SD blocks to be written if a cluster is allocated for the byte. 
1039  Development / Other Software Development / Re: Small SD card code? on: January 31, 2012, 10:08:28 am
How much flash could be allowed for SD read?

I think raw SD read could be quit small if specialized for the avr processor.

If you limited the file to being in the root directory and contiguous, a lot of code would be eliminated.

Files are contiguous unless you have deleted other files and the SD is nearly full.

I will look at what can be done.

Edit:  I did some tests and for a 2GB standard SD card formatted FAT16 It takes about 550 bytes of flash to initialize the card, read the master boot record, volume boot block, and first directory block.

Here is the raw init/read code for a 328.  It is not very neat, just a prototype test.
#include <avr/io.h>
uint8_t const ACMD41   = 0X29;
uint8_t const CMD0 = 0;
//uint8_t const CMD8 = 8;
uint8_t const CMD17    = 0X11;
/** APP_CMD - escape for application specific command */
uint8_t const CMD55    = 0X37;

uint8_t const R1_IDLE_STATE = 1;
uint8_t const R1_READY_STATE = 0;
// start data token for read or write
uint8_t const DATA_START_BLOCK = 0XFE;

uint8_t const SS_BIT = 2;
uint8_t const MOSI_BIT = 3;
uint8_t const MISO_BIT = 4;
uint8_t const SCK_BIT = 5;
uint8_t SdCommand(uint8_t cmd, uint32_t arg);
// inline SPI functions
/** Send a byte to the card */
static void spiSend(uint8_t b) {SPDR = b; while(!(SPSR & (1 << SPIF)));}
/** Receive a byte from the card */
static uint8_t spiRec(void) {spiSend(0XFF); return SPDR;}
/** Set Slave Select high */
static void chipSelectHigh(void) {
  PORTB |= (1 << SS_BIT);
/** Set chip select low */
static void chipSelectLow(void) {
  PORTB &= ~(1 << SS_BIT);
static bool waitForToken(uint8_t token) {
  for (uint8_t i = 0; i != 0XFF; i++) {
    if (spiRec() == token) return true;
  return false;
static uint8_t SdAcmd(uint8_t cmd, uint32_t arg) {
  SdCommand(CMD55, 0);
  return SdCommand(cmd, arg);
uint8_t SdCommand(uint8_t cmd, uint32_t arg) {
  uint8_t r1;
  // select card
  // send command
  spiSend(cmd | 0x40);

  // send argument
  for (int8_t s = 24; s >= 0; s -= 8) spiSend(arg >> s);

  // send CRC
  uint8_t crc = 0XFF;
  if (cmd == CMD0) crc = 0X95; // correct crc for CMD0 with arg 0
//  if (cmd == CMD8) crc = 0X87; // correct crc for CMD8 with arg 0X1AA

  // wait for response
  for (uint8_t retry = 0; ((r1 = spiRec()) & 0X80) && retry != 0XFF; retry++);

  return r1;
bool SdInit() {
  uint8_t r;
  uint16_t retry = 0;
  DDRB = (1 << SS_BIT) | (1 << MOSI_BIT) | (1 << SCK_BIT);
  // Enable SPI, Master, clock rate f_osc/64
  SPCR = (1 << SPE) | (1 << MSTR) | (1 << SPR1);
 // must supply min of 74 clock cycles with CS high.
  for (uint8_t i = 0; i < 10; i++) spiSend(0XFF);
  // command to go idle in SPI mode
  while ((r = SdCommand(CMD0, 0)) != R1_IDLE_STATE) {
    if (++retry == 0) return false;
  // start initialization and wait for completed initialization
  while ((r = SdAcmd(ACMD41, 0)) != R1_READY_STATE) {
    if (++retry == 0) return false;
  return true;
bool SdRead(uint32_t blockNumber, uint8_t* dst) {
  if (SdCommand(CMD17, blockNumber << 9)) {
    return false;
  // wait for start of data
  if (!waitForToken(DATA_START_BLOCK)) return false;
  for (uint16_t i = 0; i < 512; i++) {
    dst[i] = spiRec();
  // first CRC byte
  // second CRC byte
  return true;
Here is the include to make it a library:
#include <avr/io.h>
bool SdInit();
bool SdRead(uint32_t blockNumber, uint8_t* dst);
And here is a test sketch to read the first directory block:
#include <SdBoot.h>
uint8_t block[512];

#ifdef DBG_PRINT
void printBlock() {
  for( uint16_t i = 0; i < 512; i++) {

    if ((i & 15) == 0){
    }else {
      Serial.write(' ');
    Serial.print(block[i] >> 4, HEX);
    Serial.print(block[i] & 15, HEX);   
#endif  // DBG_PRINT
void setup() {
  bool b;
#ifdef DBG_PRINT
  while (!Serial.available());
#endif  // DBG_PRINT
  b = SdInit();
//  Serial.println(b, DEC);
  // read MBR
  b = SdRead(0, block);
//  Serial.println(b, DEC);
  // address of FAT volume
  uint32_t s = *(uint32_t*)&block[454];
  // read volume boot block
  b = SdRead(s, block);
  // FAT size
  uint16_t f = *(uint16_t*)&block[22];
  // address of directory
  uint32_t d = s + 2*f + 1;
  // read first block of directory
  b = SdRead(d, block);
#ifdef DBG_PRINT
#endif  // DBG_PRINT
void loop() {}
1040  Using Arduino / Storage / Re: Fast serial to SD card data logger on: January 30, 2012, 09:26:28 am
SdFat for Arduino 1.0 is here  The file is

A newer version,, is included with the Serial Logger and I will soon post it as the current version for 1.0.
1041  Development / Other Software Development / Re: Fast alternative to digitalRead/digitalWrite on: January 26, 2012, 01:17:17 pm
I have defined pins for 168/328, Mega, 644/1284 (Sanguino style), Leonardo, Teensy, and Teensy++.  I haven't tested Leonardo yet.

I am currently doing a lot of development on STM32 and have been thinking about digital I/O for STM32.  STM32 is very different than AVR.  Pins have 15 modes. and there are neat registers to safely set and clear bits in a port.

Here are the modes:
STM32 pin modes
0 - Analog input.
1 - Push Pull output 10MHz.
2 - Push Pull output 2MHz.
3 - Push Pull output 50MHz.
4 - Digital input.
5 - Open Drain output 10MHz.
6 - Open Drain output 2MHz.
7 - Open Drain output 50MHz.
8 - Digital input with PullUp or PullDown resistor depending on ODR.
9 - Alternate Push Pull output 10MHz.
A - Alternate Push Pull output 2MHz.
B - Alternate Push Pull output 50MHz.
C - Reserved.
D - Alternate Open Drain output 10MHz.
E - Alternate Open Drain output 2MHz.
F - Alternate Open Drain output 50MHz.
1042  Development / Other Software Development / Re: Fast alternative to digitalRead/digitalWrite on: January 25, 2012, 07:33:45 am
I will be posting another version of digitalRead/digitalWrite.  The current version is not working well in a general implementation of software SPI master.  I am also using it for a fast software I2C master. 
1043  Using Arduino / Storage / Re: Inheriting from SdFat/Sd2Card on: January 24, 2012, 01:33:38 pm
Looks like an ugly problem.  If card_ is a member of SdFat with type Sd2Card, card_.init() will call Sd2Card::init().

This statement won't change what member functions get called.
card_ = card;
It will copy values into the member variables of SdFat::card_.

card_ is not a pointer so you don't change what gets called.

Sorry if my explanation is not too clear.
1044  Development / Other Software Development / Re: The Due toolchain on: January 23, 2012, 05:45:14 pm
Thanks, I have had good luck with Code Sourcery and guess I should be comfortable with Mentor Graphics continuing to provide the free lite version. 

Most of the large physics experiments I have been involved with use Mentor Graphics tools for advanced circuit and semiconductor design, simulation, verification, and emulation.  Mentor Graphics has good relationships with labs and universities.

Guess I should be thankful for how well Code Sourcery works.
1045  Community / Products and Services / Re: Highly Anticipated 32-bit "Due" due When? on: January 23, 2012, 02:37:01 pm
Ultimately, the "Open Source" argument is academic.

What does this statement mean?  "Open Source" has no value or Arduino is not open or what?


Arduino is a business so what is strange about a relationship with Atmel?
"Open Source" cultures do not mix well with regimented, secretive, proprietary, corporate cultures like ATMEL.

Arduino has relationships with distributors and the company or companies that manufacture hardware.  See the web for video tour:

1046  Development / Other Software Development / The Due toolchain on: January 23, 2012, 01:00:02 pm
Does anyone have a clue what the Arduino Team is using as a toolchain for Due.  In the past the answer has been "it is in alpha so don't ask".

What are the truly free open options for ARM development on Windows?
I was using YAGARTO for ARM but when I went to the website I found:
Task completed

This domain was registered in July 2006 and started with the motivation
to create a toolchain with the following features:

not based on Cygwin
working with Eclipse
cheap for beginners

This sounds like YAGARTO is no longer supported.

I have also used the Sourcery CodeBench Lite Edition from Mentor Graphics.

Are there other options? 

I would use Linux but STMicroelectronics releases most of its tools for Windows and I mostly develop for STM32.
1047  Using Arduino / Storage / Re: Inheriting from SdFat/Sd2Card on: January 23, 2012, 12:33:50 pm
Would changing the private members of SdFat to protected help?  Then your derived classes could access the SdFat protected members.

That's a minimal mod to SdFat.

I developed SdFat without a lot of design.  It started as personal project to log some data to SD cards and then evolved.  I didn't think about people deriving classes from it.

Now it has a lot of users so changing the structure is hard.  It is amazing what impacts existing software.
1048  Community / Products and Services / Re: Highly Anticipated 32-bit "Due" due When? on: January 23, 2012, 10:45:40 am
I agree, open hardware is not like open software.  That's the problem, a model for true open hardware needs to be developed.

There are first attempts in science.  Here is an effort at CERN:
1049  Community / Products and Services / Re: Highly Anticipated 32-bit "Due" due When? on: January 23, 2012, 09:57:39 am
Arduino is very different than free open source software, FOSS,  projects I have been involved with.

Open source software projects sometimes have a benevolent dictator governance model  Even in this model there is active engagement and contribution by the community.

The benevolent dictator model works because members are free to take the code and create alternative projects. In fact, this ability to fork is very important to the health of open source communities, it ensures that those involved in project governance strive to make the right decisions for the community,

This is not the case with Arduino. The fact that it is "open hardware" and most of the community consists of beginners means that there is little pressure on the Arduino team to listen to the community.  There is no threat of their authority being overturned by users forking the project.

I don't see this situation changing as it did with early projects I was involved with.  I worked on BSD Unix at Berkeley in the 1970s because control of Unix by AT&T was unacceptable.  This finally evolved to Linux. 

The Linux kernel is controlled by Linus Torvalds as a benevolent dictator. The Debian version of Linux has evolved to be very open and elects its leaders

The situation is out fault.  Our community is too weak to do anything about it. 

Remember the Unix "Live Free Or Die" license plate

1050  Using Arduino / Storage / Re: SD card became unrecognized by card readers on: January 21, 2012, 11:27:26 am
I wouldn't worry much, SD cards rarely fail unless they are severely abused .  Companies like SanDisk have labs that analyze cards that fail under warranty and keep making SD cards more idiot proof.

There was probably a marginal part in your card.  Only SanDisk could diagnose the fault since the card doesn't return a detailed status, just busy.
Since your SanDisk card was made, SD cards have been improved so their expected life is now well over 10 years.

Normally the worst that happens when a card is not shutdown gracefully is a file is corrupted.

About 30,000 people have downloaded the various SD libraries I have written and the Arduino SD.h library is based on my code.  I get a lot of email about problems and this is the first card I have seen that would respond to mode and identification commands but not go ready.

Many high end cards like the SanDisk Extreme now have a lifetime warranty.
Pages: 1 ... 68 69 [70] 71 72 ... 99