0:09
everyone welcome to another video of
0:15
i got lot of requests from you guys to
0:17
make a video on the eeprom
0:19
and today i decided to finally make one
0:23
i will try to write the generalized code
0:25
so that most of the eeproms can use the
0:28
that i am going to use in this video
0:32
i am using a-t-24 c 256
0:35
e-prompt which works with i-2-c and the
0:38
code can work across all the stm32
0:42
let's start by creating the project in
0:46
first i am using stm32f446re controller
0:53
give some name to the project and click
1:02
in the cube m x first of all i am using
1:05
the external crystal for the clock
1:08
now the only thing we need to do is
1:19
i am shifting these pins for the ease of
1:22
connection on my board
1:24
i am going to use the fast mode
1:28
why did i use the fast mode well
1:31
i will answer that but first let's see
1:33
the data sheet for this eprom
1:42
these are the i2c characteristics and
1:46
this rom supports 400 kilohertz rate and
1:52
since it can support these high speed
1:55
i chose the fast mode that's all the
2:01
now let's set up the clock i have 8
2:04
megahertz external crystal
2:06
and i want the system to run at 180
2:11
click save to generate the project
2:14
this is our main file first of all
2:18
let's copy the library files in our
2:22
you can get these files from the link in
2:26
put the source file in the source folder
2:29
and header file in the include folder
2:39
let's take a look at the e prom dot c
2:43
here you need to define the i-2-c that
2:48
next to find the address of the eeprom
2:51
you need to define the 8-bit address
2:54
that includes the read and write bit
2:57
to know the address you can check the
2:59
data sheet for your device
3:02
as you can see here the higher bits of
3:05
the address are fixed
3:06
1 0 1 0. the lower bits can be modified
3:16
i have connected the all three of them
3:19
so all three are zero
3:22
also the read or right bit should be
3:24
kept as zero for the right operation
3:27
it will be automatically modified by the
3:29
hall functions during the read
3:31
operations this makes up the address for
3:34
my devices zero cross ay
3:36
zero next we need to define the page
3:41
and the number of pages
3:49
as you can see this eprom have 512 pages
3:53
and each page is 64 bytes in size
3:57
actually the memory in a rom is
3:59
generally divided into pages
4:01
and each page of some size in bytes
4:05
as i said in the beginning that i am
4:07
trying to write the most generalized
4:10
so that it can be used with other
4:13
for that purpose you have to input these
4:16
values according to your rom
4:19
this is all about the defines we will
4:22
talk about this function in a while
4:24
but first let's talk about write
4:28
obviously this function is used to write
4:30
data to the respective location in the
4:34
this function takes the following
4:38
the page where you want to start the
4:41
any offset inside the page the data that
4:46
and the size of the data
4:50
before we jump inside this function
4:52
let's understand the memory locations
5:00
as you can see here the memory address
5:02
is divided into two bytes
5:12
the first 6 bits are responsible for the
5:16
this byte address can vary between 0 to
5:20
and from bit 6 to bit 14 are responsible
5:26
this can vary between 0 to 511.
5:30
this way a page have the 64 bytes space
5:34
which can be controlled separately for
5:43
now when we continuously write data to
5:45
this rom the byte address gets
5:47
incremented automatically
5:48
but this is not the case with the page
5:52
because of this in one write cycle we
5:55
can only write data to a single page
5:59
if you try to write data which is more
6:03
the data will roll over to the beginning
6:07
to handle this situation we have to
6:10
manually increment the page address
6:12
and that can be done by modifying bit 6
6:16
i will demonstrate all these situations
6:19
let's go through the write function
6:22
first of all we are going to find out
6:24
the bit number where the page address
6:28
here 2 to the power x will be the page
6:33
you do the rest of the math in my case
6:36
this bit will be bit 6. next we set the
6:40
start page and the end page for the data
6:43
here end page also depends on the offset
6:48
for example if you use the start page as
6:52
and an offset of 50 and then write 20
6:56
in that case the end page will be page 2
7:00
since you are going outside the boundary
7:04
then we calculate the number of pages
7:06
that we are going to write
7:08
now until the required number of pages
7:11
this loop will continue repeating
7:14
inside this loop first thing we do is
7:17
calculate the address of the memory
7:20
to do so we will shift the start page by
7:23
the address position bit
7:25
that we calculated earlier for example
7:28
if my start page is 2 it will shift the
7:33
and that makes the a6 as 0 and a7 as 1.
7:37
in case the start page is 4 this 4 will
7:41
places making the bit a8 as one
7:46
similarly if i shift 511 by 6
7:49
places all these bits will be 1 making
7:52
the page address as 511.
7:55
we add this page address with the offset
7:57
value thus controlling the byte address
8:01
next we calculate the remaining bytes
8:06
the arguments are the size of the data
8:10
let's see this function
8:16
if the size plus offset are still less
8:20
it will return the size itself for
8:24
if the size is 20 bytes and offset is 30
8:28
this adds up to 50 which is still less
8:33
in this case the number of bytes to
8:35
write will be the size itself
8:37
that is 20 bytes but if the size and
8:40
offset are greater than the page
8:42
size then the remaining bytes will be
8:44
page size minus offset
8:47
say size is 30 bytes and offset is 40.
8:51
this adds up higher than 64. since we
8:55
are starting at 40th position
8:57
we can only write 24 more bytes to the
9:01
that's why this is 64 minus 40 giving us
9:06
in a single operation the rest of the
9:09
bytes will be written in next write
9:13
now we will write the data to the device
9:16
2 c memrite function the arguments are
9:20
the i2c handle the device address
9:24
the memory address size of the memory
9:28
which is 2 bytes as we have 15-bit
9:33
the data to be written this position
9:35
parameter here defines the start
9:38
as we need to update the location in the
9:42
size of the data which is the remaining
9:51
now we will increment the start page and
9:54
set the offset to zero
9:55
since we will be starting from the
9:57
beginning in the new page
10:00
update the size and the position in the
10:14
after performing the right to the memory
10:18
delay the reason is mentioned here
10:27
eeprom will program the data into the
10:31
and it needs some time to do that
10:47
this time is defined here as you can see
10:51
and that explains the delay
11:04
this completes the write function
11:09
the read function is exactly the same
11:13
we perform all those operations except
11:16
here we read the data instead of write
11:19
since we are reading this time the delay
11:22
has been removed here
11:24
you need to pass the address of the
11:26
buffer where you want to save the data
11:29
page erase function can be used to erase
11:32
a single page in the rom
11:35
the argument is the page number that you
11:39
here we first find the memory location
11:42
using the page number
11:49
now create a buffer equal to the page
11:53
copy the data into the buffer you can
11:57
some other data also now we will write
12:01
this data to the given location in the
12:05
since it's a write cycle we will give 5
12:10
if you want to erase the entire rom call
12:12
this function in a for loop
12:15
this is it for the explanation of this
12:19
now let's write our program
12:27
let's include the eeprom.h file here
12:31
let's erase the entire rom first
12:34
i have 512 pages and therefore
12:38
i am calling the page arrays function
12:43
before going any further let's create a
12:45
variable that can store the data
12:47
that we are going to read
12:54
let's read the data from the memory now
12:57
we will use hali 2 c mem read to read
13:00
the data from the memory
13:03
the arguments are the i2c address of the
13:07
address of the memory block here i am
13:10
reading the third page
13:12
i have already covered the significance
13:14
of shifting this by 6 here
13:17
memory address size is 2 bytes
13:21
where we want to store the data the data
13:24
size will be the page size
13:26
that is 64 bytes and the time
13:32
everything is good let's debug our code
13:36
let's add the data read to the watch
13:43
i am adding a break point here
13:51
it hit the break point let's step it
13:55
you can see the data in the read buffer
13:59
it's the decimal value for zero cross
14:03
and the data received is 64 bytes
14:07
now let's try to write some data to the
14:12
before that we will comment this out
14:16
c memrite will be used to write the data
14:23
i am choosing the page 3 again
14:31
let's create an array to store the data
14:33
that we are going to write
14:43
here we will store some data to that
14:52
i am storing the ascii characters
15:01
we will write 64 bytes again
15:06
here we need to give 5 milliseconds
15:09
for the right cycle let's debug this and
15:27
you can see the data is starting from
15:30
which is hex for 10.
15:38
and the 64th byte is zero cross 49
15:46
now let's see what happens when we try
15:48
to write more than the page size
15:57
according to the data sheet the data
15:59
will start overlapping from the
16:01
beginning of the same page
16:12
as expected the first byte is not zero
16:15
because it's been overlapped by the 64th
16:19
and this overlapping is up to the 5th
16:22
63rd byte is still the same one since it
16:26
hasn't been overlapped
16:28
you saw the issues that i talked about
16:31
that the page address doesn't increment
16:35
and to handle this situation i wrote
16:38
functions let's test those functions
16:42
too keep on right we'll take the
16:47
the start page any offset in that page
16:51
the data and the size of the data here
16:54
also i am going to write 70 bytes
16:57
eeprom read also takes the same
17:09
let's build and debug our program now
17:25
here you can see the first byte is zero
17:29
and the data continues for more than 64
17:33
that means the page got incremented this
17:35
time and these last 6 bytes are from
17:40
this means the functions are working as
17:46
everything is handled by these write and
17:51
now let's try to write some string like
17:53
i was waiting for this part
18:39
this is long enough to cross the
18:43
change the data and its size
18:53
seems like i forgot to include the
18:57
here i will try to read 100 bytes
19:15
here you can see the string so the
19:18
string was successfully written to the
19:20
and that 2 in 2 different pages
19:28
it's not complete because i am only
19:38
let's make some more changes i will make
19:51
and this time let's give an offset of 10
19:55
note here that i am not keeping any
19:57
offset in the read function
20:00
so the read will anyway happen from the
20:03
beginning of the page
20:19
you can see here the first 10 bytes are
20:23
and then we have data from the 11th
20:31
this string continues and we are
20:33
receiving this data from two different
20:37
and seems like we are short on buffer
20:41
anyway you can just increase the read
20:43
buffer size to avoid this
20:46
this is it for this video i hope
20:49
things were clear and you understood the
20:53
this code is somewhat generalized for
20:57
with 16 bytes of memory address
21:00
if you have any other variant leave the
21:03
i will try writing another version of it
21:07
that's all for now you can download the
21:10
code from the link in the description
21:13
keep watching and have a nice day ahead