Show Posts
Pages: 1 ... 57 58 [59] 60 61 ... 114
871  Products / Arduino Due / Re: SD Library - which pins ? on: December 02, 2012, 08:59:22 am
You must use the "ICSP" pins.  Unfortunately this is the only access to SPI.

SPI: SPI header (ICSP header on other Arduino boards)
These pins support SPI communication using the SPI library. The SPI pins are broken out on the central 6-pin header, which is physically compatible with the Uno, Leonardo and Mega2560. The SPI header can be used only to communicate with other SPI devices, not for programming the SAM3X with the In-Circuit-Serial-Programming technique.

From here

872  Products / Arduino Due / Re: SerialEvent() wont work on: December 02, 2012, 08:44:59 am
It appears that serialEventRun() is missing in Due. There is only this definition:
extern void serialEventRun(void) __attribute__((weak));
The idea is that the real serialEventRun() only gets linked if you use Serial.

You can define serialEventRun() and serialEvent() will work.
void serialEventRun(void) {
  if (Serial.available()) serialEvent();
void serialEvent() {
  if (Serial.available()) Serial.print((char);
void setup() {
  Serial.begin(9600) ;
void loop() {}

Type a line in the serial monitor to demonstrate this works then remove serialEventRun() and run the sketch.

Serial input has garbage when a sketch starts.  The reset done by the serial monitor seems to cause this.  I have not found a reliable way to avoid this problem.

873  Products / Arduino Due / Re: Happiness is SdFat with DMA SPI on: December 01, 2012, 09:39:57 pm
You could use DMA or not on any device or any transfer to a given device.  SAM3X DMA is just an engine to move memory to/from a device register with the appropriate handshake/flow control.

I get twice the performance on SAM3X as on Teensy 3.0. I run SAM3X at 42 MHz and Teensy 3.0 at 24 Mhz.  I use a lot of tricks on Teensy so DMA on Due is simpler.

I suspect there would be a slight improvement on Teensy 3.0 with DMA.

DMA could be a real advantage with a RTOS.  If you run the SD task at low priority, more CPU would be available.  Even better a task could wait on a DMA done interrupt.

Finally, I have the option in my SPI layer of using optimized SPI without DMA.  It runs much faster than the standard SPI library but has none of the options.

I don't see what you have against SAM3X DMA.
874  Products / Arduino Due / Re: Happiness is SdFat with DMA SPI on: December 01, 2012, 07:45:56 pm
I think a block transfer function could be added.  Maybe something like

 bool SPIClass::transfer(uint8_t* rxBuf, uint8_t* txBuf, uint16_t size);

You need to allow either rxBuf or txBuf to be null.  The transfer size is limited to a 16-bt field unless you use chained buffers.

The main problem is that SPI.h uses the variable chip select mode.  This mode can be used with DMA but doesn't seem like what users would want.
875  Products / Arduino Due / Re: Garbage using serial functions in Arduino Due on: December 01, 2012, 11:01:35 am
Here is a sketch with a counter.  Looks like Serial has several, maybe 6, bytes of garbage input when a reset from opening a Serial monitor happens.

Load this sketch into the Due:
int n = 0;
void setup() {
void loop() {
  if (Serial.available()) {;

 then open a serial monitor and you get this:
Close and open the serial monitor again and you get a similar result.  Almost always ending with the "N6" line.

Adding a delay(2000) after Serial.begin seems to fix it after reload but not on a reopen of the monitor.

I tried this but still get the problem.  
while (!Serial);
So if you write for Uno, Leonardo, and Due what is the correct answer to avoid this?
876  Products / Arduino Due / Re: Garbage using serial functions in Arduino Due on: December 01, 2012, 10:11:54 am
Edit: after I posted the stuff below, I changed the baud rate on your sketch to 115200 in Serial.begin.  Now the garbage looks like this:

DDÄI recei‹é 0
I received: 0

I have the same problem with many sketches.  The problem also involves, there is garbage when you use Serial.available().

In your sketch, it appears to execute the code after this if statement:
if (Serial.available() > 0) {

Your output is garbled stuff from these print calls:
 Serial.print("I received: ");
  Serial.println(incomingByte, DEC);

Note how often you get "I re" as part of the garbage.  The 255 suggests you read 0XFF.
877  Using Arduino / Storage / Re: SD read -> I2C-DAC; latency and interrupt issues on: December 01, 2012, 09:42:12 am
There will always be reads that take over 1 ms so you need a solution for read that buffers ahead.

There are I2C libraries that don't use interrupts.  I wrote such a library that supports both hardware and software I2C but it is not well documented.  It is included in this file,  here  The library is I2cMaster.

You could look at my libraries for the Adafruit Wave Shield for examples that send data to an DAC at up to 44.1 ksps.

This is a player only library  It uses a SPI DAC but could use a I2C DAC at slower speeds.

Here is a player/recorded library

These libraries solve the read ahead problem with a timer triggered thread.
878  Using Arduino / Storage / Re: Arduino Ethernet Shield microSD max size? on: November 30, 2012, 11:52:07 am
You can use any size SDHC card.  The max for SDHC is 32 GB.

The card class my not translate into performance.  Class 6 and 10 cards are optimized for large buffers and the high speed 4-bit SDIO bus.  These cards may not perform better than class 2 or 4 cards on the small memory Arduino with its slow SPI bus.   
879  Products / Arduino Due / Re: Happiness is SdFat with DMA SPI on: November 29, 2012, 02:46:06 pm

Thanks, I will probably put the Bus Matrix stuff in.  Maybe with conditionals so it's there if problems occur when I post this for test.

I was about to try the Bus Matrix next.

The SAM3X is new for me, I mostly work with STM32.

Configuring these things when your new feels like just try all possible combinations and something will work.  Trouble is the number of combination for n choose m is mighty large.  I tried both DMAC_CFG_FIFOCFG_ASAP_CFG and DMAC_GCFG_ARB_CFG_FIXED but not at the same time.
Edit: I have now run a lot of tests with 2 GB files using just the two DMAC changes.  I have probably have written and read 20 GB of data without an error.  It only takes a bit over eight minutes to read a 2 GB file.
880  Products / Arduino Due / Re: Happiness is SdFat with DMA SPI on: November 29, 2012, 12:28:00 pm
The return from spiRec are error bits, 0X2 means a timeout waiting for DMA to finish and 0X1 means SPI overrun was set.

I tried both changes with SdFat and it seems to work.  I will stress test with some long file reads.

Can't say I feel confident, wish I understood the Bus Matrix and DMAC controller better.

At least a test with 100 MB which is about 200,000 block reads ran O.K.
Type is FAT16
File size 100MB
Buffer size 81920 bytes
Starting write test.
Write 4019.24 KB/sec
Maximum latency: 83141 usec, Minimum Latency: 19453 usec, Avg Latency: 20376 usec

Starting read test.
Read 4387.86 KB/sec
Maximum latency: 18959 usec, Minimum Latency: 18598 usec, Avg Latency: 18667 usec
881  Using Arduino / Storage / Re: SD card latency help? on: November 29, 2012, 10:26:43 am
Chan's website is very good but most of the stuff about performance of SD cards is out of date. 

Wear leveling in modern TLC flash can dominate latency.  Modern cards move data that has not been changed for a long time to a new physical location.  Flash controllers have various amounts of RAM buffering and use different algorithms so generalizations like small cards are faster just isn't true.

Write latency also depends on the access pattern.  328 Arduinos have limited memory so there are not many caching options. 

Small cards are often formatted with small clusters which increases the overhead since the FAT must be accessed often.

For embedded applications cards with SLC flash tends to be best.  Modern consumer cards use MLC or TLC flash.  Industrial SD cards mostly use SLC flash.  I have had good luck with this cheap Industrial card

No card has uniform low write latency.  All cards have occasional longer latencies.  You must design fast data loggers with this in mind to avoid data loss.

I am now working on improved performance for SdFat with Cortex chips and the Mega.  Here is a link to some results for the Due with DMA SPI,134512.0.html.
882  Products / Arduino Due / Re: Happiness is SdFat with DMA SPI on: November 29, 2012, 09:21:50 am

Edit: after posting this I changed RX FIFO handling to DMAC_CFG_FIFOCFG_ASAP_CFG and things seem to work better but still errors.

I attached a zip file with a test library and a sketch that demonstrates the problem (you must be logged in to see the attachment).

Put the DueSPI folder in libraries and run the due_spi sketch to see the problem.

I will be interested to see if changing Bus Matrix arbitration can make this work.


You may be able to increase the speed of the Standard SD library.  I wrote the base code for the Standard SD library about three years ago so I have no interest in that old code.
883  Products / Arduino Due / Re: Happiness is SdFat with DMA SPI on: November 28, 2012, 03:25:57 pm
Looks like the problem with reads at 42 MHz only occurs if the data buffer for the read is in the top 32 KB of memory.

SRAM for the SAM3X is two banks, SRAM0 - 64 KB, and SRAM1 - 32 KB.  The stack is at the top of SRAM1 so when the interrupt for sysTick happens, registers are pushed on the stack in SRAM1.  This blocks access to SRAM1 by the DMA controller and I get an SPI overrun error.  If I disable interrupts during the 512 byte DMA transfer, no error occurs.  Too bad the SPI controller doesn't have a FIFO.  At 42 MHz, the SPI bus delivers a byte about every 200 ns.

It takes about 100 usec for a 512 byte transfer so it's too long to disable interrupts.

Most people will have their buffers in the first 64 KB of SRAM0 and access to to this bank works fine with 42 MHz SPI.  Also SPI at 28 MHz works with buffers in SRAM1.

DMA at 28 MHz is still fast:
Type is FAT16
File size 40MB
Buffer size 61440 bytes
Starting write test.  Please wait up to a minute
Write 2834.49 KB/sec
Maximum latency: 90293 usec, Minimum Latency: 20612 usec, Avg Latency: 21666 usec

Starting read test.  Please wait up to a minute
Read 3056.74 KB/sec
Maximum latency: 20401 usec, Minimum Latency: 19993 usec, Avg Latency: 20098 usec

The sending of 0XFF bytes to drive SPI clock have nothing to do with the problem.
884  Products / Arduino Due / Re: Happiness is SdFat with DMA SPI on: November 28, 2012, 07:47:28 am

You need DMA to get the speedup on Due.  There is no FIFO in the SPI controller.  The Teensy FIFO allows SPI to go at almost full speed, 24 MHz, without DMA.

For Due, I wrote non-DMA SPI optimized for SD cards and got about twice the standard SPI library speed.  The standard SPI library trades speed for features which is probably a good thing.

I will post this version of SdFat soon.  I need to do a lot more stress testing.

I have one puzzle, sometimes a DMA read hangs at 42 MHz.  To drive read, I use one DMA channel to send a stream of 0XFF bytes to the SPI controller.  Data is read from the SPI controller using a second DMA channel.  I sometimes get a hang when the 0XFF byte is in the same SRAM bank as the receive buffer.

I need to investigate options.  I am using byte transfers for SPI.  Other high speed interfaces like Ethernet and HSMCI can use 32-bit transfers. I am using a DMA channel for receive that has an eight deep FIFO.  I will try a channel with a 32 entry FIFO.
885  Using Arduino / Storage / Re: SD card latency help? on: November 27, 2012, 06:31:41 pm
Write latency is inherent to SD cards.  The SD spec allows up to 200 milliseconds latency for a write.  Occasional write latencies of 100 milliseconds are not uncommon.

You must design your logger to allow for latency.  I have posted several example fast loggers.

The cleanest way is to use a RTOS with two threads and read sensors in a high priority thread and buffer the data to be written to the SD in a lower priority thread.

I posted RTOS examples as and here

Another way is to read the sensor in an ISR and buffer the data for loop. posted in the above location is a very fast logger that reads the Arduino ADC in an ISR and write the the data in loop().

Several other demo sketches are in  This file is now a bit old.
Pages: 1 ... 57 58 [59] 60 61 ... 114