data write speeds and broken SD cards

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Martin
Posts: 8
Joined: Tue May 05, 2015 5:11 pm

Re: data write speeds and broken SD cards

Post by Martin » Tue May 26, 2015 6:53 pm

Just finished the SMT rework to mount the ADXL375. Nasty work, even with professional equipment. But it works, at the first try! I did not even have to change the code, as both ADXLs are nearly identical interface-wise.
Attachments
untitled.jpg
untitled.jpg (53.41 KiB) Viewed 1327 times

kma_jg
Posts: 3
Joined: Sun Oct 28, 2018 11:49 pm

Re: data write speeds and broken SD cards

Post by kma_jg » Sat Apr 06, 2019 5:20 am

Hi,
I know this a very old thread, but I find it still very useful.
I've been looking at the ADXL345 data using the I2C interface, but the SPI interface you use here seems better.
Would you mind sharing the ADXL SPI code?
Thanks

Martin
Posts: 8
Joined: Tue May 05, 2015 5:11 pm

Re: data write speeds and broken SD cards

Post by Martin » Wed Apr 10, 2019 6:24 pm

Now this was four years ago... I really don't remember much about that project!
I dug out this code:

Code: Select all

# main.py -- put your code here!
#INT1: Y4
#INT2: Y3
#/CS: Y5
#SDO: Y8  
#SDA: Y7
#SCL: Y6

import os
import pyb
from pyb import SPI

CMD_ID = bytearray((0x80,0)) #read device ID
#write BW_RATE, max speed, POWER_CTL measure=1, interrupts enabled watermark,16g
CMD_INI = bytearray((0x6C,0x0f,0x08,2,0,0,0b00001011)) 
CMD_INI2 = bytearray((0x38,0b01010100)) #watermark interrupt at 20 samples
#read six acceleration bytes (X0 X1 Y0 Y1 Z0 Z1)
CMD_RD = bytearray((0xF2,0,0,0,0,0,0))
b2=bytearray(2)
b7=bytearray(7)

#init interrupt pins as input
int1pin=pyb.Pin.board.Y4
int2pin=pyb.Pin.board.Y3
int1pin.init(pyb.Pin.IN)
int2pin.init(pyb.Pin.IN)
# Chip select (not used by driver!)
nCS=pyb.Pin(pyb.Pin.board.Y5,pyb.Pin.OUT_PP)
nCS.high()

spi = SPI(2, SPI.MASTER, baudrate=5000000, polarity=1, phase=1,firstbit=SPI.MSB)

#query ID
nCS.low();spi.send_recv(CMD_ID,b2);nCS.high()
#print(b2)

# Initialize accelerometer
nCS.low();spi.send(CMD_INI);nCS.high()
nCS.low();spi.send(CMD_INI2);nCS.high()

#pyb.delay(20) #1 ms delay

f=open('/sd/test.bin','wb')
# wait for FiFo to be filled
for k in range(160*5):
    while int1pin.value()==False:
        pass
    # read 20 values and write to SD card
    for i in range(20):
        nCS.low();spi.send_recv(CMD_RD,b7);nCS.high()
        f.write(b7)

f.close()
- Martin

Sgt_Pepper
Posts: 6
Joined: Fri Jun 07, 2019 10:05 pm

Re: data write speeds and broken SD cards

Post by Sgt_Pepper » Thu Feb 20, 2020 8:00 pm

This is great work! I am also trying to make a data logger with an ADXL375 for my mountain bike. I spent a lot of time writing a driver although I can't get it quite correct. I think that I am interpreting the data incorrectly. Would you be willing to share your ADXL375 driver?

Martin
Posts: 8
Joined: Tue May 05, 2015 5:11 pm

Re: data write speeds and broken SD cards

Post by Martin » Sat Feb 22, 2020 5:43 pm

There is no "driver". The code I published does all the interfacing.

I used a matlab script to display the results:

Code: Select all

fileID = fopen('test.bin');
A = fread(fileID);
fclose(fileID)
A=reshape(A,7,[]);
x=A(2,:)+256*A(3,:);
y=A(4,:)+256*A(5,:);
z=A(6,:)+256*A(7,:);
for i=1:length(x)
    if x(i)>32767
        x(i)=x(i)-65536;
    end
    if y(i)>32767
        y(i)=y(i)-65536;
    end
    if z(i)>32767
        z(i)=z(i)-65536;
    end
end
sc=49e-3;
x=x*sc;y=y*sc;z=z*sc;

t=(1:length(x))/3200;
plot(t,x,t,y,t,z)
legend('x','y','z')
 grid on;
 xlabel('Time (s)')
 ylabel('Acceleration (g)')
 title('ADXL375 acceleration logger');
I also attached one sample file.

- Martin
Attachments
test.zip
(44.29 KiB) Downloaded 12 times

User avatar
rcolistete
Posts: 187
Joined: Thu Dec 31, 2015 3:12 pm
Location: Brazil

Re: data write speeds and broken SD cards

Post by rcolistete » Mon Feb 24, 2020 3:05 pm

blmorris wrote:
Fri Sep 12, 2014 3:47 pm
However, there is a problem passing floating point objects to 'bytes()' which I found after my last post; I tried to pass a list of floats to bytes() the same way that you did and the file just didn't look right. It seems that bytes() really only accepts list of 8-bit integers (either decimal or hex strings) or ASCII characters and escape sequences (I'm probably not getting this exactly right…)
When another type of object is passed to bytes(), only a single byte gets stored; I suspect that this single byte may be the low-order byte of the pointer to that object, but I'm not certain of this.
You may need to figure out a way to convert your floats to integers or just go ahead with storing strings anyway. There must be a way to convert the 32-bit value from a float to a set of 4 bytes, but I haven't found it yet.
...
See the MicroPython forum topic "Convert float to hex string" and Pycom forum topic "Bytearray() and structures for floating point values".

Sgt_Pepper
Posts: 6
Joined: Fri Jun 07, 2019 10:05 pm

Re: data write speeds and broken SD cards

Post by Sgt_Pepper » Wed Mar 04, 2020 11:44 pm

Martin wrote:
Mon May 25, 2015 2:29 pm
At 3200 Hz sampling rate, the INT1 goes high every 20/3200 = 6.25 ms. The readout/file write takes 1.3 ms, so there's plenty of time. If the write buffer has filled up to 512, a sector is written to the SD card, which takes about 1.2 ms (427 kB/s).
I have been trying to get similar speeds using an Adafruit microSD card breakout board and the sdcard.py driver from GitHub https://github.com/micropython/micropyt ... /sdcard.py

My test code is as follows:

Code: Select all

READ_COMMAND = bytearray((0x32,0,0,0,0,0,0))
data1 = bytearray(7)

def speedTest():
    
    startTime = utime.ticks_us()
    
    # ~30ms
    file = open('/sd/log.bin', 'wb')

    for k in range(20):
        # ~0.13ms
        SPI2_CS1.low(); spi_2.send_recv(READ_COMMAND, data1); SPI2_CS1.high()

        # ~0.053ms
        file.write(data1)
    
    # ~25ms
    file.close()

    deltaTime = utime.ticks_diff(utime.ticks_us(), startTime)
    print(deltaTime/1000000)
The result of this test is about 66ms, much much slower than 1.2ms speeds you were getting to write 20 data points. I timed each line of code individually and wrote the approximate execution times in a comment directly above. I found that opening and closing the file took by far the longest amount of time.

The only thing that I can think of at this point is that the sdcard.py driver is much slower than what you were using? Is there a built-in SD driver that uses SDIO and allows for much faster speeds?

Any thoughts are much appreciated!

Post Reply