EEPROM and STM32 || I2C || Multi Page Write and Read

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello everyone welcome to another video of controllers tech i got lot of requests from you guys to make a video on the eeprom and today i decided to finally make one i will try to write the generalized code so that most of the eeproms can use the functions that i am going to use in this video i am using a-t-24 c 256 e-prompt which works with i-2-c and the code can work across all the stm32 devices let's start by creating the project in cube id first i am using stm32f446re controller give some name to the project and click finish in the cube m x first of all i am using the external crystal for the clock now the only thing we need to do is enable the i2c i am shifting these pins for the ease of connection on my board i am going to use the fast mode why did i use the fast mode well i will answer that but first let's see the data sheet for this eprom these are the i2c characteristics and you can see this rom supports 400 kilohertz rate and even 1000 kilohertz since it can support these high speed transfers i chose the fast mode that's all the setup we need here now let's set up the clock i have 8 megahertz external crystal and i want the system to run at 180 megahertz click save to generate the project this is our main file first of all let's copy the library files in our project you can get these files from the link in the description put the source file in the source folder and header file in the include folder let's take a look at the e prom dot c file here you need to define the i-2-c that you are using next to find the address of the eeprom you need to define the 8-bit address that includes the read and write bit also to know the address you can check the data sheet for your device as you can see here the higher bits of the address are fixed 1 0 1 0. the lower bits can be modified by modifying the a-2 a-1 and a-0 pins i have connected the all three of them to the ground so all three are zero also the read or right bit should be kept as zero for the right operation it will be automatically modified by the hall functions during the read operations this makes up the address for my devices zero cross ay zero next we need to define the page size and the number of pages as you can see this eprom have 512 pages and each page is 64 bytes in size actually the memory in a rom is generally divided into pages and each page of some size in bytes as i said in the beginning that i am trying to write the most generalized code here so that it can be used with other eeproms too for that purpose you have to input these values according to your rom this is all about the defines we will talk about this function in a while but first let's talk about write function obviously this function is used to write data to the respective location in the rom this function takes the following arguments the page where you want to start the write from any offset inside the page the data that you want to write and the size of the data before we jump inside this function let's understand the memory locations as you can see here the memory address is divided into two bytes the first 6 bits are responsible for the byte address this byte address can vary between 0 to 63. and from bit 6 to bit 14 are responsible for the page address this can vary between 0 to 511. this way a page have the 64 bytes space in it which can be controlled separately for each page now when we continuously write data to this rom the byte address gets incremented automatically but this is not the case with the page address because of this in one write cycle we can only write data to a single page if you try to write data which is more than the page size the data will roll over to the beginning of the same page to handle this situation we have to manually increment the page address and that can be done by modifying bit 6 to 14. i will demonstrate all these situations but first let's go through the write function first of all we are going to find out the bit number where the page address starts here 2 to the power x will be the page size you do the rest of the math in my case this bit will be bit 6. next we set the start page and the end page for the data here end page also depends on the offset that you use for example if you use the start page as 1 and an offset of 50 and then write 20 bytes in that case the end page will be page 2 since you are going outside the boundary of page 1. then we calculate the number of pages that we are going to write now until the required number of pages has been written this loop will continue repeating inside this loop first thing we do is calculate the address of the memory location to do so we will shift the start page by the address position bit that we calculated earlier for example if my start page is 2 it will shift the 2 by 6 bits and that makes the a6 as 0 and a7 as 1. in case the start page is 4 this 4 will be shifted by 6 places making the bit a8 as one similarly if i shift 511 by 6 places all these bits will be 1 making the page address as 511. we add this page address with the offset value thus controlling the byte address also next we calculate the remaining bytes using this function the arguments are the size of the data and the offset let's see this function if the size plus offset are still less than the page size it will return the size itself for example if the size is 20 bytes and offset is 30 this adds up to 50 which is still less than 64. in this case the number of bytes to write will be the size itself that is 20 bytes but if the size and offset are greater than the page size then the remaining bytes will be page size minus offset say size is 30 bytes and offset is 40. this adds up higher than 64. since we are starting at 40th position we can only write 24 more bytes to the current page that's why this is 64 minus 40 giving us 24 bytes to write in a single operation the rest of the bytes will be written in next write cycle now we will write the data to the device using harley 2 c memrite function the arguments are the i2c handle the device address the memory address size of the memory address which is 2 bytes as we have 15-bit address the data to be written this position parameter here defines the start position in the data as we need to update the location in the data also size of the data which is the remaining bytes and timeout now we will increment the start page and set the offset to zero since we will be starting from the beginning in the new page update the size and the position in the data buffer also after performing the right to the memory we must give some delay the reason is mentioned here eeprom will program the data into the non-volatile memory and it needs some time to do that this time is defined here as you can see it's 5 milliseconds and that explains the delay this completes the write function the read function is exactly the same we perform all those operations except here we read the data instead of write since we are reading this time the delay has been removed here you need to pass the address of the buffer where you want to save the data to page erase function can be used to erase a single page in the rom the argument is the page number that you want to erase here we first find the memory location using the page number now create a buffer equal to the page size copy the data into the buffer you can use some other data also now we will write this data to the given location in the rom since it's a write cycle we will give 5 milliseconds delay if you want to erase the entire rom call this function in a for loop this is it for the explanation of this file now let's write our program let's include the eeprom.h file here let's erase the entire rom first i have 512 pages and therefore i am calling the page arrays function 512 times before going any further let's create a variable that can store the data that we are going to read let's read the data from the memory now we will use hali 2 c mem read to read the data from the memory the arguments are the i2c address of the rom address of the memory block here i am reading the third page i have already covered the significance of shifting this by 6 here memory address size is 2 bytes where we want to store the data the data size will be the page size that is 64 bytes and the time out let's build it everything is good let's debug our code let's add the data read to the watch expression i am adding a break point here it hit the break point let's step it over you can see the data in the read buffer it's the decimal value for zero cross zero f and the data received is 64 bytes now let's try to write some data to the memory location before that we will comment this out highlight 2 c memrite will be used to write the data i am choosing the page 3 again let's create an array to store the data that we are going to write here we will store some data to that array i am storing the ascii characters starting from 10. we will write 64 bytes again here we need to give 5 milliseconds delay for the right cycle let's debug this and see you can see the data is starting from zero cross a which is hex for 10. and the 64th byte is zero cross 49 now let's see what happens when we try to write more than the page size according to the data sheet the data will start overlapping from the beginning of the same page as expected the first byte is not zero cross ay because it's been overlapped by the 64th byte and this overlapping is up to the 5th byte 63rd byte is still the same one since it hasn't been overlapped you saw the issues that i talked about in the beginning that the page address doesn't increment on its own and to handle this situation i wrote those functions let's test those functions too keep on right we'll take the following arguments the start page any offset in that page the data and the size of the data here also i am going to write 70 bytes eeprom read also takes the same arguments let's build and debug our program now here you can see the first byte is zero cross ay and the data continues for more than 64 bytes that means the page got incremented this time and these last 6 bytes are from page 4. this means the functions are working as expected everything is handled by these write and read functions now let's try to write some string like i was waiting for this part this is long enough to cross the boundary of a page change the data and its size seems like i forgot to include the string header here i will try to read 100 bytes let's debug it now here you can see the string so the string was successfully written to the rom and that 2 in 2 different pages it's not complete because i am only reading 100 bytes let's make some more changes i will make it 128 bytes and this time let's give an offset of 10 bytes note here that i am not keeping any offset in the read function so the read will anyway happen from the beginning of the page you can see here the first 10 bytes are not written and then we have data from the 11th position this string continues and we are receiving this data from two different pages and seems like we are short on buffer again anyway you can just increase the read buffer size to avoid this this is it for this video i hope things were clear and you understood the logic properly this code is somewhat generalized for the i2c proms with 16 bytes of memory address if you have any other variant leave the comment below i will try writing another version of it that's all for now you can download the code from the link in the description keep watching and have a nice day ahead
Info
Channel: Controllers Tech
Views: 9,510
Rating: undefined out of 5
Keywords: stm32, stm32f4, f103, discovery, nucleo, eeprom, i2c, example, tutorial, page, byte, address, hal, write, read, AT24C256
Id: -tV2pPXZ4VM
Channel Id: undefined
Length: 21min 20sec (1280 seconds)
Published: Tue Feb 23 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.