PDA

View Full Version : SD Multiple Write?



jimbostlawrence
- 21st October 2011, 16:58
Hi I'm struggling through SD card writing stuff and can't seem to get the multiple block write to work... I've had a single block write to the card and can read that back no problem. I've now changed the command to multiple block $59, start token to $FC and written one block, followed by 16 1's for CRC. This is accepted and it writes it to the card sitting busy for a short period of time. Once all ok, it looks back to start sending the next 512 bytes but the card rejects this next block!


hserout[$d,$a,"Multi Sector write"]
SDC_sector_addr = 409
SDC_cmd = WRITE_MULTI_BLOCK
SDC_address = SDC_sector_addr << 9
HSEROUT[$d,$a,"B3:",# SDC_address.byte3]
HSEROUT[$d,$a,"B2:",# SDC_address.byte2]
HSEROUT[$d,$a,"B1:",# SDC_address.byte1]
HSEROUT[$d,$a,"B0:",# SDC_address.byte0]
gosub send_receive_SD_loop
If (SDC_response != 0) Then
hserout[$d,$a,"Not accepted!",$d,$a]
gosub stop_read
else
hserout[$d,$a,"MULTI BLOCK Accepted!",$d,$a]
endif
SDC_data_out = DATA_START_TOKEN_MULTI
Gosub write_byte

for multi_loop = 1 to 10
For SDC_index = 0 To 63

i2cread i2cSDA,i2cSCL,$a7,50,[accx,accx2],error
hserout [$d,$a,#SDC_index," ",#accx," ",#accx2,$d]

SDC_data_out = 48 + accx dig 2
Gosub write_byte
SDC_data_out = 48 + accx dig 1
Gosub write_byte
SDC_data_out = 48 + accx dig 0
Gosub write_byte

SDC_data_out = 48 + accx2 dig 2
Gosub write_byte
SDC_data_out = 48 + accx2 dig 1
Gosub write_byte
SDC_data_out = 48 + accx2 dig 0
Gosub write_byte

SDC_data_out = 13
Gosub write_byte
SDC_data_out = 10
Gosub write_byte

Next SDC_index
' Send the CRC bytes.
SDC_data_out = $ff
gosub write_byte ' Just sends 16 1s.
gosub write_byte
SDC_data_in = SSPBUF ' Clear the buffer.
SSPIF = 0 ' Clear the interrupt flag.
SSPBUF = $ff ' Shift out a dummy byte.
While !SSPIF ' Wait for receive byte.
Wend
SDC_data_in = SSPBUF ' Get the byte.
If ((SDC_data_in & $0f) != DATA_ACCEPTED) Then
SDC_status = sdcCardDataRejected
hserout[$d,$a,"Rejected!"]
gosub stop_read
Else
hserout[$d,$a,"Data accepted, writing to storage media!"]
' The card is writing data into the storage media.
' Wait for the card to return non-zero (not busy) or a timeout.
For SDC_timeout = 0 To 10000
SDC_data_in = SSPBUF ' Clear the buffer.
SSPIF = 0 ' Clear the interrupt flag.
SSPBUF = $ff ' Shift out a dummy byte.
While !SSPIF ' Wait for receive byte.
Wend
SDC_data_in = SSPBUF ' Get the byte.
If (SDC_data_in != 0) Then
hserout[$d,$a,"Got a write response at ",#SDC_timeout]
SDC_timeout = 10000 ' Got a response, end the loop.
endif
Next SDC_timeout
hserout[$d,$a,"SDC data in = ",#SDC_data_in]
If (SDC_data_in = 0) Then
hserout[$d,$a,"SDC Card timeout"]
gosub stop_read
endif
Endif
next multi_loop



832
sdc timeout: 1
sdc timeout: 2
sdc timeout: 3
sdc timeout: 4
sdc timeout: 5
sdc timeout: 6
sdc timeout: 7
sdc timeout: 8
sdc timeout: 9
sdc timeout: 10
Go idle
Test card for response:
Countdown timeout: 8
Got a response at: 8
SDC_response: 3
Error unknown
835
sdc timeout: 1
sdc timeout: 2
sdc timeout: 3
sdc timeout: 4
sdc timeout: 5
sdc timeout: 6
sdc timeout: 7
sdc timeout: 8
sdc timeout: 9
sdc timeout: 10
Go idle
Test card for response:
Countdown timeout: 8
Countdown timeout: 7
Got a response at: 7
SDC_response: 1
Op cond
Test card for response:
Countdown timeout: 8
Countdown timeout: 7
Got a response at: 7
SDC_response: 1
Op cond
Test card for response:
Countdown timeout: 8
Countdown timeout: 7
Got a response at: 7
SDC_response: 0
CRC On/Off
Test card for response:
Countdown timeout: 8
Countdown timeout: 7
Got a response at: 7
SDC_response: 0
Set Block Length
Test card for response:
Countdown timeout: 8
Countdown timeout: 7
Got a response at: 7
SDC_response: 0
Multi Sector write
B3:0
B2:3
B1:50
B0:0
Test card for response:
Countdown timeout: 8
Countdown timeout: 7
Got a response at: 7
SDC_response: 0
MULTI BLOCK Accepted!
0 68 255
1 69 255
2 68 255
3 70 255
4 69 255
5 68 255
6 68 255
7 71 255
8 72 255
9 70 255
10 67 255
11 67 255
12 67 255
13 67 255
14 67 255
15 73 255
16 69 255
17 69 255
18 64 255
19 69 255
20 70 255
21 70 255
22 68 255
23 68 255
24 68 255
25 66 255
26 73 255
27 69 255
28 66 255
29 71 255
30 68 255
31 68 255
32 72 255
33 66 255
34 66 255
35 72 255
36 70 255
37 66 255
38 68 255
39 67 255
40 66 255
41 67 255
42 69 255
43 69 255
44 69 255
45 67 255
46 65 255
47 66 255
48 64 255
49 66 255
50 69 255
51 70 255
52 66 255
53 67 255
54 65 255
55 65 255
56 66 255
57 73 255
58 67 255
59 68 255
60 67 255
61 71 255
62 71 255
63 69 255
Data accepted, writing to storage media!
Got a write response at 0
SDC data in = 3
0 71 255
1 67 255
2 67 255
3 68 255
4 71 255
5 71 255
6 70 255
7 70 255
8 71 255
9 73 255
10 65 255
11 69 255
12 69 255
13 73 255
14 71 255
15 67 255
16 72 255
17 68 255
18 72 255
19 66 255
20 67 255
21 70 255
22 70 255
23 67 255
24 62 255
25 70 255
26 73 255
27 65 255
28 69 255
29 67 255
30 65 255
31 70 255
32 70 255
33 67 255
34 71 255
35 71 255
36 68 255
37 68 255
38 64 255
39 66 255
40 70 255
41 64 255
42 66 255
43 66 255
44 67 255
45 64 255
46 63 255
47 70 255
48 68 255
49 67 255
50 68 255
51 70 255
52 69 255
53 69 255
54 67 255
55 69 255
56 66 255
57 64 255
58 68 255
59 69 255
60 69 255
61 71 255
62 67 255
63 67 255
Rejected!


Anyone got any ideas on why the SD card would reject the next block???

Many thanks in advance...

Jimbo

jimbostlawrence
- 21st October 2011, 17:45
SOLVED! Literature out there is bloody sparse! It's all just 'simply do this, simply do that' !!! Well, most documentation doesn't state that you have to send a start token before EVERY block. It might seem obvious, it might not, but I found one piece which mentioned it in words but omitted it from the timing diagrams!!! ARGHHHHHHHHHHHHH I was dead set on heading home by 6pm. Looks like I'll be going hom 15 minutes early to a flaggon of tea! Hope this helps someone...
If anyone out there has experience on using SD cardss in PicBasic, please get in touch. Might be able to swap ideas!