Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
strandstring
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Erik Strand
strandstring
Commits
114dfd62
Commit
114dfd62
authored
May 12, 2019
by
Erik Strand
Browse files
Options
Downloads
Patches
Plain Diff
Read an SD card, if somewhat unreliably
Bit banging is not the way to go.
parent
563f471a
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
sd_tests/neil/read_sd.c
+76
-47
76 additions, 47 deletions
sd_tests/neil/read_sd.c
with
76 additions
and
47 deletions
sd_tests/neil/read_sd.c
+
76
−
47
View file @
114dfd62
...
...
@@ -300,49 +300,22 @@ void SD_read(uint32_t sector,uint16_t offset,uint8_t *buffer,uint16_t size) {
set
(
CS_port
,
CS_pin
);
}
//
// main
//
int
main
(
void
)
{
//
// main
//
void
sd_loop
()
{
static
uint8_t
count
,
sectors_per_cluster
,
FATs
,
attribute
,
result
[
8
],
buffer
[
50
];
static
uint16_t
bytes_per_sector
,
reserved_sectors
,
offset
,
file_cluster_low
,
file_cluster_hi
,
buffer_length
,
sector_count
;
static
uint32_t
partition
,
FAT_sectors
,
fat_sector
,
root_cluster
,
root_sector
,
file_length
,
file_cluster
,
file_sector
,
chars_read
;
// Debug loop to verify we can detect when the card is inserted
// Configure DETECT_pin as an input and led_pin as an output
DETECT_direction
&=
~
DETECT_pin
;
led_direction
|=
led_pin
;
while
(
1
)
{
if
(
PINA
&
DETECT_pin
)
{
led_port
&=
~
led_pin
;
}
else
{
// Wait for card to be inserted
while
(
PINA
&
DETECT_pin
)
{
led_port
|=
led_pin
;
_delay_ms
(
200
);
led_port
&=
~
led_pin
;
_delay_ms
(
200
);
}
}
led_port
|=
led_pin
;
static
uint8_t
count
,
sectors_per_cluster
,
FATs
,
attribute
,
result
[
8
],
buffer
[
50
];
static
uint16_t
bytes_per_sector
,
reserved_sectors
,
offset
,
file_cluster_low
,
file_cluster_hi
,
buffer_length
,
sector_count
;
static
uint32_t
partition
,
FAT_sectors
,
fat_sector
,
root_cluster
,
root_sector
,
file_length
,
file_cluster
,
file_sector
,
chars_read
;
//
// set clock divider to /1
//
CLKPR
=
(
1
<<
CLKPCE
);
CLKPR
=
(
0
<<
CLKPS3
)
|
(
0
<<
CLKPS2
)
|
(
0
<<
CLKPS1
)
|
(
0
<<
CLKPS0
);
//
// initialize output pins
//
set
(
serial_port
,
serial_pin_out
);
output
(
serial_direction
,
serial_pin_out
);
set
(
CS_port
,
CS_pin
);
output
(
CS_direction
,
CS_pin
);
clear
(
MOSI_port
,
MOSI_pin
);
output
(
MOSI_direction
,
MOSI_pin
);
set
(
SCK_port
,
SCK_pin
);
output
(
SCK_direction
,
SCK_pin
);
set
(
MISO_port
,
MISO_pin
);
//turn on pull-up
input
(
MISO_direction
,
MISO_pin
);
//
// put card in SPI mode
//
...
...
@@ -354,6 +327,7 @@ int main(void) {
clear
(
SCK_port
,
SCK_pin
);
SPI_delay
();
}
//
// CMD0: reset and enter idle state
// should return 0x01
...
...
@@ -361,6 +335,7 @@ int main(void) {
put_flash_string
(
PSTR
(
"
\r\n
reset: 0x"
));
SD_command
(
0x40
,
0
,
0x95
,
result
);
put_hex_string
(
result
,
8
);
//
// CMD8: send interface condition, set SDHC
// should return 0x01000001AA
...
...
@@ -368,6 +343,7 @@ int main(void) {
put_flash_string
(
PSTR
(
"
\r\n
set interface: 0x"
));
SD_command
(
0x48
,
0x000001AA
,
0x87
,
result
);
put_hex_string
(
result
,
8
);
//
// initialization loop
//
...
...
@@ -380,6 +356,7 @@ int main(void) {
put_flash_string
(
PSTR
(
"
\r\n
application command: "
));
SD_command
(
0x77
,
0
,
0
,
result
);
put_hex_string
(
result
,
8
);
//
// ACMD41: initialize the card
// should return 0x00 when ready
...
...
@@ -387,12 +364,21 @@ int main(void) {
put_flash_string
(
PSTR
(
"
\r\n
initialize card: 0x"
));
SD_command
(
0x69
,
0x40000000
,
0
,
result
);
put_hex_string
(
result
,
8
);
//
// check if done
//
if
(
result
[
1
]
==
0
)
if
(
result
[
1
]
==
0
)
{
put_string
(
"
\n
card initialized"
);
break
;
}
if
(
PINA
&
DETECT_pin
)
{
put_string
(
"
\n
card removed"
);
return
;
}
}
//
// read the first partition table
//
...
...
@@ -403,6 +389,7 @@ int main(void) {
put_flash_string
(
PSTR
(
"
\r\n
first sector: 0x"
));
put_hex_string
(
buffer
+
8
,
4
);
memcpy
(
&
partition
,
buffer
+
8
,
4
);
//
// read the first partition block
//
...
...
@@ -442,6 +429,7 @@ int main(void) {
put_hex_char
((
root_cluster
>>
16
)
&
0xFF
);
put_hex_char
((
root_cluster
>>
8
)
&
0xFF
);
put_hex_char
(
root_cluster
&
0xFF
);
//
// read the root directory
//
...
...
@@ -498,6 +486,7 @@ int main(void) {
}
offset
+=
32
;
}
//
// read the file
//
...
...
@@ -520,6 +509,7 @@ int main(void) {
put_char_string
(
buffer
,
buffer_length
);
chars_read
+=
buffer_length
;
offset
+=
buffer_length
;
//
// check sector length
//
...
...
@@ -549,9 +539,48 @@ int main(void) {
offset
=
0
;
}
}
//
// sector length not reached, continue
//
}
put_flash_string
(
PSTR
(
"
\r\n\r\n
end of file
\r\n
"
));
led_port
&=
~
led_pin
;
}
//
// main
//
int
main
(
void
)
{
//
// main
//
//
// set clock divider to /1
//
CLKPR
=
(
1
<<
CLKPCE
);
CLKPR
=
(
0
<<
CLKPS3
)
|
(
0
<<
CLKPS2
)
|
(
0
<<
CLKPS1
)
|
(
0
<<
CLKPS0
);
// Configure DETECT_pin as an input and led_pin as an output
DETECT_direction
&=
~
DETECT_pin
;
led_direction
|=
led_pin
;
//
// initialize output pins
//
set
(
serial_port
,
serial_pin_out
);
output
(
serial_direction
,
serial_pin_out
);
set
(
CS_port
,
CS_pin
);
output
(
CS_direction
,
CS_pin
);
clear
(
MOSI_port
,
MOSI_pin
);
output
(
MOSI_direction
,
MOSI_pin
);
set
(
SCK_port
,
SCK_pin
);
output
(
SCK_direction
,
SCK_pin
);
set
(
MISO_port
,
MISO_pin
);
//turn on pull-up
input
(
MISO_direction
,
MISO_pin
);
while
(
1
)
{
sd_loop
();
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment