Sunday, March 29, 2020

Start Developing STM32 on Linux

Step 1: Download Tools

You need to download three parts for everything to work:
Compiler is the main piece of software that compiles our C code with all other library files into machine language that our stm32 controller can understand. Download the latest pre-compiled version of this compiler.
Folder containing STM32 firmware is the one that holds all the startup and core files needed for the operation of the main processor. We will be using Standard Peripheral Library that has been surpassed by HAL. I like StPeriphLibrary more as companies that work on this processors use them because it is robust and older and supported. It is also more rugged. It doesn't cut the work that you have to do to initialize a peripheral or turn on an LED but, so it forces you to learn how these processors work. With that you have more knowledge of inner workings thus making sense of programming any task.
Last piece of software to download is st-link utility. It is maintained on github and is used to transfer compiled binary files to the processor using stlink IC on the board serving as a SWD / JTAG programmer/debugger.

Step 2: Installing Software

Installing Software
Installing Software
After you have downloaded all the files I suggest you put them inside a common folder as they are all used together for the same purpose. I put all the folders inside a folder called "Embedded" in my HOME directory.
We will start with with the easiest, the STM32 libraries. The folder that you have downloaded can be just left there. Just make sure to dig around to see where the appropriate files are stored. Therefore you can change and edit the main MakeFile so it will work with your platform.
Second easiest is the compiler. You also don't need to do anything to it, but we will make the compiler a globally accessible function so you can call the compiler from any folder regardless of the path. All the steps can be done in terminal or in gui, but I like to use terminal as when you get experienced it gets faster and easier and I encourage you to use terminal more, if you are afraid of it. Here are the steps:
  1. Go into your home folder "/home/YOURUSERNAME/" or "~/" or type cd into terminal
  2. open file ".bashrc" by typing: nano .bashrc
  3. scroll down to the end of the file and add this line:
    export PATH=$PATH:~/Embedded/gcc-arm-none-eabi-8-2018-q4/bin
  4. exit by saving: CTRL+X, click Y, ENTER
  5. run command: source .bashrc to refresh terminal sources
  6. check if everything is working by typing: arm-none-eabi-gcc --version, it should display the latest version of the compiler
To install st-link, extract the archive that you have downloaded into the Embedded folder. Then follow these steps:
  1. Run: make
  2. Go into folder "build/Release": cd build/Release
  3. Type ls and you will see two executables called "st-flash" and "st-util"
  4. Move those two into the parent directory stlink: mv st-flash st-util ../../
  5. You can, if you would like to use these two functions globally edit ".bashrc" file again by adding:
    export PATH=$PATH:~/Embedded/stlink/
That's all! You have everything you need. Now go grab yourself your favourite text editor. Use just a standard one, a smarter one like SublimeText or Atom, it is what I use.
We will now create a sample project that you can use to start every project. It is like a template with all the main setting already handled.
You can download it on my MEGA, the link is on the first step of this instructable and under every youtube video of mine. Inside is the empty main.c file along with some startup files for this processor and the Makefile. Makefile is the one that tells the C compiler where to find the arm compiler, how to compile and where are all the libraries. To get these appropriate files for your project, you can go into the STM32 library folder and check for a "project" or "examples" folders. Inside you will see and copy these files: main.cMakefile and XXX_conf.hsystem_XXX.c.Also you will need stm32_flash.ld linker file that can be found in folder :
"/FLASH_Program/TrueSTUDIO/FLASH_Program/" that is inside the example folder or just search for the file.
Makefile can be found online or copied from my folder, but you will need to change a few things. Let's tale a look at my make file and what you could change.
# Path to stlink folder for uploading code to board
STLINK=~/Embedded/stlink

# Put your source files here (*.c)
SRCS=main.c system_stm32f4xx.c

# Libraries source files
#SRCS += stm32f4xx_rcc.c
#SRCS += stm32f4xx_gpio.c

# Binaries will be generated with this name (.elf, .bin, .hex)
PROJ_NAME=test

# Put your STM32F4 library code directory here, change YOURUSERNAME to yours
STM_COMMON=/home/matej/Embedded/STM32F4-Discovery_FW_V1.1.0

# Compiler settings. Only edit CFLAGS to include other header files.

CC=arm-none-eabi-gcc
OBJCOPY=arm-none-eabi-objcopy

# Compiler flags
CFLAGS  = -g -O2 -Wall -Tstm32_flash.ld
CFLAGS += -DUSE_STDPERIPH_DRIVER
CFLAGS += -mlittle-endian -mthumb -mcpu=cortex-m4 -mthumb-interwork
CFLAGS += -mfloat-abi=hard -mfpu=fpv4-sp-d16
CFLAGS += -I.

# Include files from STM libraries
CFLAGS += -I$(STM_COMMON)/Libraries/CMSIS/Include
CFLAGS += -I$(STM_COMMON)/Libraries/CMSIS/ST/STM32F4xx/Include
CFLAGS += -I$(STM_COMMON)/Libraries/STM32F4xx_StdPeriph_Driver/inc
CFLAGS += -I$(STM_COMMON)/Utilities/STM32F4-Discovery

# add startup file to build
SRCS += $(STM_COMMON)/Libraries/CMSIS/ST/STM32F4xx/Source/Templates/TrueSTUDIO/startup_stm32f4xx.s
OBJS = $(SRCS:.c=.o)

vpath %.c $(STM_COMMON)/Libraries/STM32F4xx_StdPeriph_Driver/src \

.PHONY: proj

all: proj

proj: $(PROJ_NAME).elf

$(PROJ_NAME).elf: $(SRCS)
 $(CC) $(CFLAGS) $^ -o $@
 $(OBJCOPY) -O ihex $(PROJ_NAME).elf $(PROJ_NAME).hex
 $(OBJCOPY) -O binary $(PROJ_NAME).elf $(PROJ_NAME).bin

clean:
 rm -f *.o $(PROJ_NAME).elf $(PROJ_NAME).hex $(PROJ_NAME).bin

# Flash the STM32F4
burn: proj
 $(STLINK)/st-flash write $(PROJ_NAME).bin 0x80000000
  • You could edit first line to change path to the your stlink utility folder
  • You can change line to the destination of your folder with libraries and YOURUSERNAME
    STM_COMMON=/home/YOURUSERNAME/Embedded/STM32F4-Discovery_FW_V1.1.0<br>
  • Also check out section where all the libraries are linked. This can change depending on the platform that you are using so please check for changes in the file tree. Everithing else that includes any paths to certain files, like with next line with startup file can be changed.
After you have edited all these things inside the Makefile you can check if it is working by opening a terminal inside your directory and typing: make. If it compiles every thing with no problem, then you are set. If not, look at the compiler errors and edit the Makefile.

Hướng dẫn lập trình STM32 trên Linux

Chuẩn bị

- Kit STM32F4
- Download gcc-arm-none-eabi: 


Cài Toolchain

Tạo folder ,giải nén file gcc và add path

sudo apt-get install flex bison libgmp3-dev libmpfr-dev libncurses5-dev libmpc-dev autoconf \texinfo build-essential libftdi-dev libsgutils2-dev zlib1g-dev libusb-1.0-0-dev git

mkdir ~/stm32f4
cd ~/stm32f4

Trỏ đến folder chứa file gcc vừa download về
cd ~/Downloads
tar -xvf gcc-arm-none-eabi-9-2019-q4-major-aarch64-linux.tar.bz2 -C ~/Embedded

- Add đường dẫn các thư viện gcc vào trong biến môi trường.
Để chính xác các bạn có thể cd đến đúng folder bin rồi export đúng.
export PATH=$PATH:$HOME/Embbeded/gcc-arm-none-eabi-9-2019-q4/bin

echo $PATH để xem bạn export thành công chưa.

Cài đặt ST-link

Cài libusb,compile và cho phép user có thể truy cập đến st-link... Mục đích chính là để nạp được chương trình xuống kit ST
sudo apt-get install libusb-1.0-0-dev git

cd ~/stm32f4

cd stlink
git checkout texane/pipe
./autogen.sh
./configure

Phần này sau khi configure xong sẽ sinh ra Makefile, bạn copy thư viện libusb.h vào thư mục st-link và chỉnh sửa lại makefile dòng 235 LIBS = -lusb-1.0
Sau đó chạy lệnh make bên dưới

make
 
sudo mkdir -v /opt/stlink
sudo cp -v 49-stlinkv*.rules /etc/udev/rules.d/
sudo udevadm control --reload-rules

Chạy thử chương trình

Với thư viện STM32 thì hầu như là không có hỗ trợ cài đặt nhiều trên linux cũng như các IDE miễn phí, tuy nhiên có ông  Karl Palsson có tâm đã viết lại khá nhiều project mẫu mà chúng ta có thể tham khảo và tiện đường thì mình cũng lấy của ổng xài luôn 😀
1
2
3
4
5
6
7
cd ~/stm32f4
cd kkstm32_base/example/stm32f4/STM32F4xx_StdPeriph_Driver/build/
make
cd ../../Projects/IO_Toggle/
make
st-flash write IO_Toggle.bin 0x08000000
Đây là kết quả sau khi nạp thành công, cứ có dòng “…Flash written and verified! jolly good!” là ok rồi, bạn có thể xem ở bên dưới kit F4 đang chớp LED ầm ầm nhé

Saturday, March 14, 2020

SDRAM timing issues

The timing of SDRAM focuses on three times:
CL=2: CAS Latency, CAS latency, CAS and read command issued to the first data output time ---- read operation
tRCD=2: RAS to CAS Delay, the delay time of the address after the row address is sent ---- the row and column address delay
tRP=2: Close the existing work line, ready to open a new line, after a period of time to allow the RAS line to send a valid command to open a new work line ---- Precharge time
OK, all three times are clear.
In the external interface, the row address and column address multiplexing mode is adopted, and two interfaces are added for this: row address strobe RAS, column address strobe CAS. tRCD (typically 2-3 cycles) is the latency between the row strobe RAS and the column strobe CAS, and CL is the delay from the column address strobe to the first data read (typically 2-3 cycles) ,
DDR is from SDRAM. Strictly speaking, it should be called DDR SDRAM. DDR SDRAM is the abbreviation of Double Data Rate SDRAM. It is the meaning of double rate synchronous dynamic random access memory. Therefore, there is a big part, the two are the same, understand SDRAM, Then come to understand the improvement of DDR on SDRAM, the effect should be better, here to thank the great god of Chinaunix.net - Tekkaman Ninja, my memory learning materials areTekkaman Ninja's blogIntroduced in the introduction. The following is some personal finishing of my recommendation document for Great God, OK, first a structure diagram of SDRAM.

Below is a simple SDRAM work flow chart I drew

The red number in the picture is the main time we need to find, and now we are starting to look at the picture.
1. Chip initialization
The SDRAM chip has a logic control unit inside and a mode register to provide control parameters. Therefore, SDRAM must first initialize this control logic core each time it is turned on.

2. Line is valid
After initializing, to address an array in an L-Bank, first determine the row (Row), make it active (Active), and then determine the column. . Simply understand the first pass address.

3. Column read and write
After the row address is determined, the column address is addressed. The read and write signals and column addresses are sent at the same time. The operation of reading and writing depends on the WE# pin. When it is enabled, it is written, otherwise it is read.
in the send columnThere must be an interval between the read and write commands and the line valid commands. This interval is defined as tRCD, which is RAS to CAS Delay. You can also understand the line strobe period. Simple understanding means that After the address is sent, when the address and read/write signals are sent, it needs to be delayed. This should be based on the delay of the chip storage array electronic component response time (the process from one state to another).
The generalized tRCD is in units of clock cycles (tCK, Clock Time). For example, tRCD=2 means that the delay period is two clock cycles. To the exact time, it depends on Depending on the clock frequency, for PC100 SDRAM, tRCD=2 represents a delay of 1000/100 * 2 = 20ns, and the figure below is a timing diagram of tRCD=3.

4. Data output (read)
After the column address is selected, the specific memory location has been determined. The only thing left is that the data is output to the memory bus through the data I/O channel (DQ).
However, after the CAS is issued, it still takes a certain period of time to have data output. The time from the CAS and the read command to the first data output is defined as CL (CAS Latency, CAS latency). Since CL appears only when reading, CL is also called read latency (RL, Read Latency), and the figure below is a schematic diagram of CL=2.

5. Data input (write)
The data write operation is also performed after tRCD.But there is no CL at this time (remember, CL only appears in the read operation),The timing diagram for row addressing and column addressing is the same as above, except that in column addressing, WE# is active.
In order to ensure reliable writing of data, sufficient write/correction time (tWR, Write Recovery Time) is left.The operation is also called Write Back. tWR takes at least one clock cycle or a little more (the higher the clock frequency, the more tWR cycles)
 
6.Burst Lengths
Burst meansMethod of continuously transmitting data between adjacent storage units in the same rowContinuous transmission involves storageThe number of storage units (columns) is the Burst Lengths (BL).
As long as the starting column address and burst length are specified, the memory will automatically perform read/write operations on the corresponding number of subsequent memory cells in turn without the need for the controller to continuously provide column addresses. Thus, except that the transmission of the first data requires several cycles (mainly the previous delay, typically tRCD+CL), each data can be obtained in only one cycle.

7. Precharge
Since the addressing of SDRAM is specific, it is effective (working) if you want to address another row of the same bank after reading and writing. The line is closed and the row/column address is resent. Bank closes the existing work line, and the operation to open a new line is precharge.
After the precharge command is issued, it takes a period of time to allow the RAS line valid command to open a new work line. This interval is called tRP (Precharge command Period). . Like tRCD and CL, the unit of tRP is also the number of clock cycles, depending on the clock frequency.

8. Refresh
is called DRAM because it keeps refreshing to retain data, so it is the most important operation of DRAM. The refresh operation is the same as the rewrite operation in precharge, which is read and rewritten with S-AMP.
But why do you need to refresh the precharge operation? Because precharge is a work line operation in one or all L-Banks, and it is irregular, and refresh has a fixed period, and all rows are operated in order to retain those banks that have not undergone rewriting for a long time. The data in . However, unlike all L-Bank precharges, the row here refers to the same address in all L-Banks, and the work row addresses in each L-Bank in precharge are not necessarily the same. For example, I have four pieces. The refresh is that I refresh one of the four memory addresses and then the next one; the precharged work line address can be different.
So how often do you want to refresh again? The currently accepted standard is that the effective data retention period of the capacitor in the bank is 64ms (milliseconds, 1/1000 second), which means that the refresh cycle of each row is 64ms. This refresh rate is: the number of rows / 64ms. When we look at the memory specifications, we often see the 4096 Refresh Cycles/64ms or 8192 RefreshCycles/64ms flags, where 4096 and 8192 represent the number of rows per L-Bank in the chip. The refresh command is valid for one line at a time, and the transmission interval varies with the total number of lines, which is 15.625 μs (microseconds, 1/1000 milliseconds) for 4096 lines and 7.8125 μs for 8192 lines.
There are two types of refresh operations: Auto Refresh (AR) and Self Refresh (SR).
SR is mainly used for data storage in sleep mode low power state. The most famous application in this aspect is STR (Suspend to RAM). When the AR command is issued, CKE is put into an invalid state, and the SR mode is entered. At this time, the system clock is no longer operated, but the refresh operation is performed according to the internal clock. All external signals except CKE are invalid during SR (no external refresh command is required), and only when CKE is valid again can the self-refresh mode be exited and the normal operation state is entered.
 

The above is the main working steps of SDRAM. Comparing the simple working flow chart at the top, is the time clear?

CL=6: CAS Latency, CAS latency, CAS and read command time to the first data output ---- Read operation
tRCD=6: RAS to CAS Delay, the delay time of the address after the row address is sent ---- the row and column address delay
tRP=6: Close the existing work line, ready to open a new line, after a period of time to allow the RAS line to send a valid command to open a new work line ---- pre-charge time
OK, all three times are clear.

The basic knowledge of SDRAM theory and the operation timing explanation

SDRAM (Synchronous Dynamic Random Access Memory), synchronous dynamic random access memory. Synchronization means that the Memory work needs to synchronize the clock. The internal command transmission and data transmission are based on it. Dynamic means that the storage array needs constant refresh to ensure that the stored data is not lost, because the data stored in the SDRAM is through the capacitor. Working, we all know that the capacitor will be discharged in the natural state. If the power is discharged, it means that the data in the SDRAM is lost. Therefore, the SDRAM needs to be refreshed before the power of the capacitor is discharged. Random means that the data is not The data is stored in order, but the address is freely specified for reading and writing data.

About the internal structure of SDRAM

We borrowed some of the text from the Advanced Advanced Memory Technology Guide to explain:
SDRAM is actually a storage array. You can imagine that if SDRAM is not in the form of an array but in the form of a pipe, SDRAM is difficult to achieve random access. . The array is like a table, filling in the data. As with the retrieval principle of the table, first determine a row (Row), and then determine a column (Col), we can accurately find the required cell, which is the basic principle of memory chip addressing. For memory, this cell can be a storage unit, so what is this table (storage array) called? This is the logical bank (Logic Bank, hereinafter referred to as Bank).
sdram_bank
Seeing this, everyone should not imagine that the way we address SDRAM is: first determine the address of the bank, then determine the address of the row and column.

SDRAM memory capacity calculation method

Regarding the calculation of memory, let's look at the manual first.sdram_addr
The 128Mb in the figure indicates the total capacity of the SDRAM. 2M: indicates that 1 bank has 2M addresses, that is, one bank has 2M memory cells; 4Bank indicates 4 banks; 16 I/O means that the bit width is 16 bits, the data bus uses 16 bits, and the maximum number that a memory cell can store is 2^16-1=65535. Then calculate the capacity according to each parameter: 2M x 4 x 16b = 128Mb.

SDRAM chip pin introduction

The first picture is the pin diagram of SDRAM, and the second picture is the description information of each pin signal.
sdram_pin
pin_description
The above two pictures can be found in the manual of the SDRAM chip. Below Kevin, we will briefly introduce the signals that we need to focus on when doing FPGA development.
CLK: The clock for SDRAM operation, and all input signals are detected on the rising edge of CLK, that is, any command we give to SDRAM must be raised in CLK. The edge stays stable so that SDRAM does not get the error when we get the command we gave.
CKE: The clock enable signal is a signal used to control whether the internal clock of the SDRAM is working (there is also a clock inside the SDRAM)
CS: Chip select signal, it should be noted here that if you want to operate on SDRAM, you must lower the chip select signal.
BA0, BA1: Bank address line, used to give the bank address, can control 4 banks of SDRAM
A0~A11: Address line. When we select the Row address of a bank of SDRAM, we need to go to 12 address lines (A0~A11); when we choose the address of Col, only Use 9 lines A0 ~ A8; A10 this signal can be used to control Auto-precharge.
RAS, CAS, WE: These three lines are used to send commands to SDRAM, including initialization, read, write, auto charge and other commands.
UDQM, LDQM: Data input/output mask.
DQ0~DQ15: The data lines of SDRAM are bidirectional. The data written to or read from SDRAM is transmitted on DQ.
You can see that the above address bus A0~A11 can control both row and col, and the number of lines used to control col address and control row address is different. There may be some doubts, Kevin explained to you below:
SDRAM vendors generally use the same bus to address SDRAM in order to save costs.Nothing wrongFor the Row address, 12 lines are used, that is, there are 2^12=4096 Row addresses in total, and the col address uses 9 lines, that is, there are 2^9=512 col addresses, and if added together, A bank has 4096 × 512 = 2,097,152 (2x1024x1024), that is, there are 2M addresses, in this case, it is in line with our previous calculation of SDRAM capacity.
If you have another one, you may not understand the role of the mask (UDQM, LDQM). Please listen to Kevin's explanation below:
Take the SDRAM chip we use, there are 16 data lines, which means that the number of bits of our data can reach 16 bits, but please note that maybe we are When using SDRAM, maybe when we write data to SDRAM, we generate only 8 bits of data, but the FPGA is connected with 16 data lines of SDRAM. At this time, the data stored in SDRAM is still 16 bits. So, in order to avoid this problem, we can use the mask to mask the upper 8 bits. Of course, the role played by the mask when reading data is similar.

SDRAM related operation timing

In front of so much SDRAM related knowledge, I believe that you can't wait to know how to operate SDRAM, you look at the official and listen to Kevin in detail.
sdram_state
The above picture is a state jump diagram inside SDRAM (the pictures that may be seen on the web page are not very clear, you can download these in the “Documentation Manual” under the navigation menu “Welfare” Document), the thick black line indicates that it will automatically jump to another state in this state, and the thin black line indicates that the command needs to be jumped.

SDRAM initialization

We first find the state of the SDRAM "POWER ON", which is just power-on. After the "POWER ON" status is given to the 'Precharge' command, it will jump to the "Precharge" state and then automatically jump to the "IDLE" state. In the "IDLE" state, we need to give the SDRAM twice the Auto-refresh command, and then we need to set the mode register. After the mode register is designed, our initialization process is over. Having said that, we still don't know how to initialize the SDRAM. At that time, Kevin still didn't know how to write the code to initialize the SDRAM after reading the "Advanced Advanced Memory Technology Guide". In this blog post, Kevin will not talk about how to write code for the time being. I will only explain the timing and principle related to the initialization process. The principle is well understood. I believe everyone will have some idea how to write the code. For a code on how to write, you can also refer to Kevin's other blog post.
We continue to the picture in the official document. This time we can have a waveform diagram. You should look at it carefully. It is very helpful for you to write the code:
sdram_init_timming
It may be a headache for everyone to come into contact with this timing diagram for the first time, but it doesn't matter. Please listen to Kevin for a slow explanation. While reading the documentation, the status is above. The diagrams are easier to understand. For each instruction that appears in the diagram, we can find it in the device manual we use:
According to the official documentation, SDRAM can not send other commands to SDRAM except for “COMMAND INHIBIT” or “NOP” during the 100us power-on period, so everyone can see the transmission. The minimum interval of the "Precharge" command and the power-on interval "T" is 100us. This 100us is actually equivalent to a stable period of SDRAM. A friend who has read the article "Master Advanced Memory Technology Guide" may find that the stabilizer written in the "Advanced Advanced Memory Technology Guide" is 200us, but here is 100us, so it does not appear. Contradictions, Kevin just appeared in the manual, this question also appeared. What we can be sure of is that the possibility of an official manual error is very small. If even the official documentation is wrong, how can we let those who use their devices live? ( T □ T )╮. Is that the author of the Advanced Advanced Memory Technology Guide wrong? Such a long-tested classic article, if there is a mistake, must have been raised long ago. In the end, Kevin found the answer in the official handbook:0@[]7E~02L}W$56V1NH89XT
For this English, Kevin understands that SDRAM is likely to execute the "COMMAND INHIBIT" or "NOP" command during 200us, so here we simply get the stabilizer Into 200us. (If you understand the mistake, please criticize it)
After the stabilizer has passed, we will give the "Precharge" command, then the "tRP" time and then the "Auto Refresh" command, and then the "tRC" time. The "Auto Refresh" command is then used for the "tRP" time to set the mode register. When giving the command, we need to pay attention to the fact that the three commands "Precharge", "Auto Refresh", and "Load Mode REGISITER" appear only once, and the "NOP" command is given when these commands are not given. .
When giving the "Precharge" command, we need to specify the A10 and Bank addresses. If A10 is High (All Banks), it means that all banks are pre-charged. To the bank address, if A10 is low (SINGLE BANK), you need to specify the address of a certain bank.
When you give the "Auto Refresh" command, you do not need to specify the bank address (we notice that there is a description in the lower right corner, we do not need to care about the gray part of the data).
When setting the mode register, the instructions to be given are slightly more complicated. The manual shows A0~A11 and BA0, BA1 is used. Let's see how the mode register should be used. Settings, please see the picture:mode_register_set
A9: Used to specify the operation mode: high for burst read/burst write, low for burst read/single write;
A4~A6: Specify the length of the incubation period. For the incubation period, there is a very detailed introduction to the Advanced Advanced Memory Technical Guide. However, the actual effect of the latency setting is if the latency is set. Yes, when we perform SDRAM read operation, the read data will be delayed by 3 cycles relative to the "READ" command. If the latency is 2, it will be delayed by 2 cycles.
A3: Set the type of burst, continuous and non-continuous.
A2~A0: Used to specify the length of the burst.
For the above explanation, you may think that Kevin may not be straightforward because you don’t understand what is suddenly. Kevin gives an example: A9 is set to 0. The latency is set to 3, the burst type A3 is 0, and the burst length (A2~A0) is set to 4. When we write, the data is written once every 4 data, that is, we give a write command. Will write 4 data into SDRAM, and the four addresses are continuous (if the burst type is set to be non-contiguous, the address will not be continuous, the specific address will be a kind of law, Kevin did not engage himself Understand); when we perform a read operation, after three cycles of giving a read command (latency of 3), four data will collapse continuously.
At this point, for the initialization process, it is basically finished here.

SDRAM write operation

In the state transition diagram, we can see that we need to have a command "ACT" before the initialization or completion of the SDRAM read or write operation (this command is in our initialization sequence). The figure also appears, but we got it here.) The meaning of this command is that the vernacular is the "effective line" command, which is to let a certain line in SDRAM move, so that we can read or write.
Before the official speaking operation, there is still a little hope that everyone can pay attention to it. Maybe a careful friend has already discovered it. In our state transition diagram, there are "WRITE" and "WRITEA". Two states, in these two states, we can write to the SDRAM, just in the "WRITEA" state, after each burst of data is written, the SDRAM will automatically jump out of this state to refresh, and In the "WRITE" state, it is necessary to give the corresponding instruction before jumping out of the "WRITE" state, so in order to improve the running speed of the SDRAM, we generally do not let the SDRAM enter the "WRITEA" state to increase the speed. Of course, the difference between the two states "READ" and "READA" is also the case.
Let's continue with the timing diagram of the previous write operation. This timing diagram does not enter the "WRITEA" state:write-without_autoprecharge
Following the old routine, Kevin continues to work with everyone to analyze the timing of write operations:
After the initialization between the "tMRD", give an "ACT" command, specify which bank of which bank, and then "tRCD" time to "WRITE" "Instruction, at the same time specify the bank address and column address and pull A10 low, and then write the corresponding amount of data according to the set burst length, thus completing our write operation. After writing four data, what is it, this is completely our own decision. If I don't send any instructions to SDRAM, then SDRAM is still in the "WRITE" state. If I want SDRAM to write, then I can send a write command to the SDRAM. If the time to refresh the SDRAM is reached, then we must perform the SDRAM refresh operation to ensure that the SDRAM data is not lost.
It may be seen here that if I am letting SDRAM write data, is it time for SDRAM to refresh, I have to let SDRAM perform refresh operation immediately? This is certainly not a reality. Let's imagine that SDRAM write data is performed every four or four data. If the second data is being written and the SDRAM is refreshed immediately, it will inevitably lose the remaining two data that have not yet been written. On My God, we are categorically unable to let our data be lost. We can't let our data be lost, and we must ensure that the SDRAM is refreshed to ensure that the data in our corresponding SDRAM is not lost. How should we write the code? God, think of it, is there already a friend who has to blow up? Don't panic, how to write code, Kevin will explain in the next blog post, we will be able to thoroughly understand the timing of SDRAM. The timing of the refresh is described later in this article.
The write operation is basically finished here.

SDRAM read operation

Let's talk about our read operation. In fact, the read operation and the write operation are similar. I believe that everyone has the basis of the write operation, and it is easier to understand the read operation of SDRAM. Let's continue with the picture above:
First of all, still give the "ACT" command, remember not to forget to specify the ROW address and BANK address, and then give the "READ" command, after the set latency, our The data is coming out. Don't forget, don't forget the signals on other signal lines when giving the relevant commands.

SDRAM auto-refresh operation

Kevin has mentioned the automatic refresh of SDRAM many times in the past. Why should SDRAM be automatically refreshed? I believe everyone knows the reason through the previous introduction. It can be said that if SDRAM is not refreshed at the time. Refreshing, no matter how much data we write into SDRAM is a no-brainer, so we must pay great attention to the SDRAM refresh operation, and when it is refreshed, it must give a refresh command. The maximum time for SDRAM internal capacitors to save data is 64ms, and we have a BANK with 4096 lines, 64ms/4096~=15us, which means that in order to ensure that the data inside the SDRAM is not lost, the maximum time interval between refreshes is 15us, so in order to allow SDRAM to have more time to read or write, we set the SDRAM refresh period to 15us.
Here, Kevin also wants to add a little knowledge. Every time SDRAM is refreshed, it operates on each row. It does not charge each capacitor separately, so every time it is refreshed, The capacitors in this row are charged and we can understand that they happen synchronously.
auto_refresh
This is the timing chart for SDRAM to automatically refresh. I believe everyone has the previous foundation, and you should not be afraid of this picture.

In each automatic refresh, we need to give a "Precharge" command. What does this command do? You can look at the previous state diagram. If the SDRAM is in the "WRITE" or "READ" state at this time, the "Precharge" command can cause the SDRAM to jump out of the "WRITE" or "READ" state and enter the "IDLE". status. Next, after the "tRP" time, give an "Auto-Refresh" command. Note that we only need to give the "Auto-Refresh" command once.(inElectronic enthusiast forumwithPunctual atom forumIn the post, Kevin wrote the "Auto-Refresh" command twice, which is wrong. I hope to see the friends of this post pay attention to it.At this point, the auto-refresh operation is complete. The automatic refresh operation here is similar to our initialization process, except that there is no setting of the mode register here.
Ok, for SDRAM initialization, reading, writing, and auto-refresh, Kevin has finished all of this based on his own understanding. If there are some errors in this article, I hope you will criticize it in time. In addition, Kevin will share in the next blog post how to write SDRAM controllers, that is, how to write the operations in this article with code.
Back to Top