SD CARD using SPI in STM32 || Cube-IDE || File handling || uart
0 views
Jun 26, 2019
Watch Updated Video ::: https://youtu.be/RgQt4quLh4Q
View Video Transcript
0:04
hello everybody welcome to controllers
0:08
Tech after receiving a lot of requests
0:11
on the topic today I have decided to
0:14
make a video about interfacing SD card
0:16
with stm32 using spi mode in this video
0:21
we will do some basic file handling this
0:24
includes creating opening read write
0:28
update and deleting the files other
0:32
complex functions such as directory
0:35
related and file system related
0:37
functions will be covered in the
0:39
upcoming videos I am using stm32 f103
0:44
controller and stm32 cube ide so let's
0:49
start by creating the project in cube MX
0:57
[Music]
1:20
[Music]
1:36
select the fat F's filesystem and pay
1:40
attention to the settings here
1:45
[Music]
2:09
I will be using SPI one in full-duplex
2:14
master mode also I will use you up to
2:18
transmit data and rest is the usual
2:21
settings
2:24
[Music]
2:37
[Music]
2:54
[Music]
3:16
[Music]
3:45
so the project is created I will just
3:48
quickly build it once so the errors can
3:51
be avoided
3:52
[Music]
4:03
[Music]
4:06
copy the library files that we are going
4:09
to need to the respective folders
4:12
[Music]
4:40
we need to include this file that we
4:43
just copied also to use some string
4:46
functions
4:56
[Music]
4:58
next open fad FSS d dot H file and use a
5:02
disk IO H file
5:10
here we need to link the file system to
5:14
our driver just follow me
5:17
[Music]
6:00
note that the arguments here should be
6:04
same as they are in the function to
6:06
which this is a part of
6:25
let's build it again to check for errors
6:28
and yes we do have some errors but don't
6:31
worry we will get rid of them in our
6:33
next step in the fat FSST dot C file
6:44
make sure you define the CS port and CS
6:47
pin that you are using also the SPI
6:51
handler the default here is to be one
6:53
let's close these as they are done and
6:57
open the interrupt handler here we need
7:01
to define our time of functions
7:09
these timers should run independently
7:12
and that's why it's best if we define
7:15
them in the systick handler
7:29
note here all letters are gone also this
7:33
is it for the configuration let's move
7:35
to our main function now I am creating
7:49
some variables that I am going to use
7:52
these include FS for file system fill
7:56
for the file f result for storing the
7:59
result of each operation and buffer to
8:01
store the data which we can read or
8:04
write
8:09
you
8:14
be R and B W stores the counter to the
8:18
read and write in the file and at last
8:21
some variables to check the capacity of
8:24
the card
8:35
I am writing this function send UART to
8:39
make it easy to write the data to the
8:41
UART
8:44
[Music]
8:53
[Music]
9:05
[Music]
9:13
buf size is used to determine the size
9:16
of the buffer up to which it is filled
9:26
and at last a function to clear the
9:29
buffer
9:39
okay let's start our main program by
9:43
mounting the SD card first and we are
9:46
going to use F mount function to do so
9:49
there is some verbose in case of a
9:51
failure next we are going to check the
9:54
capacity of the card we are going to use
9:57
F get free function to do so the total
10:01
size and the free space will be sent via
10:03
the UART
10:12
now it's time to create our first file F
10:16
open function is used to open a file and
10:19
if the file does not exists it will
10:22
create it and the no puns it here F open
10:25
always does that job of creating file
10:28
also ever read and F awright are the
10:31
access given to the file next we are
10:34
going to write some string to the file
10:36
using F port s function and then close
10:40
the file now in order to read it we need
10:52
to open it with read access and then
10:54
copy the string from the file to the
10:57
buffer using F get s function here fill
11:00
dot F size is the size of the data in
11:03
the file now we need to close the file
11:29
let's build our code and test it
11:36
[Music]
11:43
take a look at the output of the UART it
11:46
shows us the total size and the free
11:49
space in the card it also shows that the
11:52
file was created and the string that was
11:55
written the file this function used F
11:58
get s and F port s to read and write
12:01
string
12:10
[Music]
12:16
next we are going to take a look at the
12:18
F right and F read functions which do
12:22
the same things here I am creating
12:25
another file that is file - dot txt and
12:29
I am going to use F right to write some
12:32
data into it and f read to read the data
12:36
from it also buffer is clear to make
12:39
sure that it is empty before we read the
12:41
data
12:43
[Music]
12:51
fwrite function takes the data from the
12:54
buffer and write it into the file
12:56
remember that it will start writing from
12:59
the starting point in the file and any
13:01
data will be overwritten the BW is the
13:05
pointer to the counter for the number of
13:07
bytes written F read function will read
13:10
the data from the file and save it in
13:12
the buffer fill dot F size is the size
13:15
of the data in the file B R is the
13:19
pointer to the count variable for the
13:21
number of bytes to read from the file
13:24
it's time to test the code now as you
13:32
can see along with the results from the
13:34
previous section it gives us some new
13:37
information also it shows that the file
13:40
- dot txt was created and you can see
13:43
the string that I wrote to the file -
13:48
[Music]
13:56
let's now see how to update an existing
13:59
file here
14:01
I want to update the second file to do
14:04
so I will use the function FL seek this
14:07
function takes two parameters the first
14:11
is the pointer to the file and the
14:13
second is the offset from where you want
14:15
to start the update process here I am
14:18
using the file size as the offset this
14:22
means that I want it to start from the
14:24
end and continue writing the rest of the
14:27
reading process is same open the file
14:30
read the data close the file let's test
14:34
our code and check the results as you
14:42
can see this data is up to the last
14:44
section and this is the updated data
14:47
this string is first printed from the
14:50
file - and after updating the file it
14:53
again gets printed along with the new
14:56
string
14:58
[Music]
15:03
let's see how to delete the files from
15:07
the card
15:10
[Music]
15:13
we are going to use f-fun link function
15:16
which takes only one parameter and that
15:19
is the path to the file itself if the
15:23
results is okay it will be printed in
15:25
the output and at last we will unmount
15:28
the SD card it's time to test it now as
15:36
you can see both the files are removed
15:39
and the SD card is unmounted
15:41
successfully let me just show you what
15:48
happens if we try to remove files which
15:51
are not present in my SD card
15:55
[Music]
16:07
note that the success message is not
16:10
shown now and those repetitions you see
16:13
on the top is due to the fact that the
16:15
string is keep appending in the file
16:21
this is it for this video guys I hope
16:25
you understood it very well you can
16:28
download the code from the link in the
16:30
description leave comment in case of any
16:33
doubt have a nice day
16:36
[Music]
16:38
you
#Programming
#Software