中断交互数据的方式

本文深入探讨了硬件设备交互中的关键组件——缓冲区(fifo)和直接内存访问(DMA),解释了它们如何在数据传输中协同工作,以及在不同应用场景下的功能分析。特别关注了DMA设备如何通过参数控制数据搬移过程,以及与CPU之间的同步机制,如中断和DMA的使用。此外,文章还阐述了系统设计中如何利用这些机制提高效率并减少CPU负载。

 概念:

A,带有buffer缓存的设备硬件描述:

1fifo

2dma

 

1fifo可以使用任何存储空间,包括外设的一些寄存器组,只要构成一个cpu指令无法完成的读写操作(原子操作)都可以构成buffer

2dma是设备,不是存储空间。dma的作用是操作和管理fifo的行为。

 

DMA介绍:

    Dma 设备受到参数的影响,表现为数据搬移的原始地址,目的地址,以及搬移数据大小。搬移的效率是否占有总线的效率是由硬件设计而定。不由软件控制。

    Fifo是一块任意位置的存储区域。或者被cpu占有,或者被dma占有。Cpudma两者同时存在通过总线访问fifo的权利(auth),这样就会产生竞争现象。

这种微观的竞争延时等待,是由硬件设计考虑。在芯片内部逻辑生成之前会有匹配度测试(最常规的事列:高性能的显卡配置在普通的主板上,未必可以发挥出最好的性能)。

 

系统设计:

外设与cpu之间的交互可以有很多种类型:中断,dmafifo计数等等。只要可以同步双方的数据有效都可以称之为同步机制。

典型的中断同步:写中断,读中断,中断写,中端读。(CPU侧)

写中断:在中断中对外设写。(没有参考)

中断写:通过中断写入外设。(dma

读中断:在中断中读取外设。(dma

中断读:通过中断读取外设。(网络设备,键盘等)

 

DMA的功能分析:

DMA是一个总线上的设备。具有自己的逻辑。通常对于驱动开发来说只要进行适当配置。就可以自己运作。配合中断一起完成数据搬移和交互的过程。DMA通常会自带内部fifo。对这个fifo进行管理。当fifo满或者空时,会触发中断。告知cpu可以往里面写数据或告知cpu将下一组数据慢点写入。

通常的情况下DMA中断只是给出数据搬移的结果。告知cpu上一组数据已近搬移完毕,并且可以进行下一组数据的处理。这样,cpu就可以知道外设是否是出于忙碌状态。现在的DMA有编程(指令)接口。给DMAController正确的指令:一个死循环。在这个循环中DMA自己完成已经由软件设置的逻辑进行数据搬移和数据有效性检查。在不同的状态下给出中断告知CPU目前处理的过程。

DMA是一个设备,当DMA拿到总线的控制权进行数据搬移的时候CPU是处于无法访问总线的状态,这种状态是互斥的。一定降低了CPU使用总线的效率。因此很多外设自带fifo。这些fifo有外部设备自行进行管理。当DMA操作的时候CPU不能对某一个外设的fifo操作,颗粒化了总线的等待时间,而不是一视同仁长时间等待。同时在DMA设计的时候也会考虑到效率的问题,应尽可能的快,从而缩短总线的控制权。

以上的描述可以配合其它的外设中断对数据有效进行检查。

具体分析:

soc devkit为例:hps写入fifo后,fpga感知fifo中有数据。当数据达到阈值后,获得总线的控制权,产生视屏贞搬移。在搬移数据结束后产生特定中断,用于告知cpu数据已近搬移完毕。Cpu获得这个中断后知道数据已经处理完毕,可以对贞缓存进行下一步操作。

操作系统会使用软件同步机制:Complete。在中断处理程序和内核驱动中保持同步。中断处理中complete置位。退出中断后,在驱动中等待这个表示位。如果置位,继续运行更新视屏缓存,否则切换其它线程。

对于数据量不大的情况下,在很短的时间内把数据处理完成的应用场景中,如中断读取gpio状态等。可以马上返回,不需要更多的机制保证。

就系统而言,cpu和外设的状态交互越密切,同步机制越是得到保证。但是频繁中断会影响cpu的效率问题。当cpu频繁响应外设的中断请求时,用户的使用感受会降低。常常表现为鼠标键盘延时等。因此有了DMADMA的逻辑简单,功能单一。在不大范围影响总线的情况下进行外设交互,达到了,减轻cpu负担的作用。

试想,如果任何事情,数据的有效性,缓存的满空,数据的搬移等功能都是由cpu自己来完成,那么就不需要外设模块。这些模块包括DMA。在soc 的应用场景中。Vip就是一个用fpga来实现dma的功能。以上就是cpu和外设数据交互的基本框架。在这个框架下,可以进行修改和自己实现逻辑功能,但是跳出这个框架,一定要自己实现。功能当然可以,但是效率未必会高。

 

soc设计为例:

1Fpga 在数据进行搬移之后给出中断告知hps上一贞数据处理完毕。可以进行下一组数据处理。数据的有效性检查(fifo满或者空)之后,给出状态中断。

2Hps在中断处理过程中设置complete的变量,在这个变量得到改变后(即上一贞数据处理完成后)。进行更新显示缓存的操作。如果这个变量未更新,那么表示这个区域还不能更新,线程或用户进程进行主动切换。让其它更需要运行的程序接管hps,完成其它的工作。

 

 

以上的分析并不止局限于soc的开发。其分析的原理可以参考其它任何一款开发板。DM368,三星2440等。

 

分析中难免有不足之处。欢迎批评指正。

 

陆斌

1. What is an IDE (Integrated Development Environment), and what are its main components? 2. What is the role of a compiler in the C++ development process? 3. What is the difference between source code (e.g., a .cpp file) and an executable file? 4. In the "Hello, World!" program, what is the purpose of the line #include <iostream>? 5. What is special about the main() function in a C++ program? 6. Why do computers fundamentally operate using the binary (base-2) system? 7. What is the base of the hexadecimal system? Why is it often used by programmers as a shorthand for binary numbers? 8. Explain the "triad" method for converting an octal number to binary. 9. Briefly describe the "division by 2" method for converting a decimal number to binary. 10. What is the decimal value of the binary number 1011? 1. What is the purpose of the std::cout object? Which header file must be included to use it? 2.What is the difference between an escape sequence like \n and a manipulator like std::endl? (Hint: Both create a new line, but they have a subtle difference). 3.How would you print the following text to the console, including the quotes and the backslash: He said: "The file is in C:\Users\"? 4.Is it possible to write an entire multi-line text output using only one std::cout statement? If yes, how? 5.What is a syntax error? Give an example of a syntax error from Task 2. (Task 2: Debugging The following program contains several syntax errors. Copy the code into your IDE, identify the errors, fix them, and run the program to ensure it works correctly. Incorrect Code: */ Now you should not forget your glasses // #include <stream> int main { cout << "If this text" , cout >> " appears on your display, cout << " endl;" cout << 'you can pat yourself on ' << " the back!" << endl. return 0; "; ) Hint: Pay close attention to comments, header files, brackets ({}), operators (<<), semicolons, and how strings and manipulators are written.) 1. What is the difference between variable declaration and initialization? 2.What will be the result of the expression 7 / 2 in C++? Why? 3.What will be the result of the expression 10 % 3? What is the main purpose of the modulus operator? 4. What is the purpose of std::cin and the >> operator? 5. A beginner tries to swap two integer variables a and b with the code a = b; b = a;. Why will this not work correctly? 1. What is an algorithm? Name the primary ways to represent an algorithm. 2.List the main flowchart symbols and explain their purpose. 3.What are the three fundamental types of algorithm structures? Briefly describe each. 4.In a branching algorithm, what determines the flow of execution? 5.What is the key characteristic of a linear algorithm? 6.When is a cyclic algorithm structure used?7. 8. 9. 7.Explain the purpose of a connector in a flowchart. 8.What is the difference between a predefined process block and a standard process block? 9.In the context of solving a quadratic equation algorithm, what condition must be checked before calculating the roots? Why? 1. What are the three main approaches to data input and output offered by C++? 2. What is the purpose of the SetConsoleOutputCP(65001) and SetConsoleCP(65001)
functions in the provided C++ program example? 3. Explain the difference between the cin and cout objects in Stream 1/0. 4. When using formatted 1/0, which header file must be included to use manipulators like setw and setprecision? 5. List three manipulators used for data output in C++ and briefly describe what each one does. 6. In Formatted I/0 using printf), what are the conversion specifications for a decimal integer and a real number in exponential form? 7. What is the difference in how the & (address-of) operator is used when inputting a value for an integer variable versus a string variable using the scanf() function? 8. Which Character I/O function is used to output a single character to the screen, and which is used to output a string? 9. Describe the syntax and function of the ternary operator in C++. 10. What is the difference between the logical AND (&&) and logical OR (I|) operators when combining multiple conditions? 11. When is the default label executed in a C++ switch statement? 12. What is the primary purpose of the break statement within a switch block? 1. What is the main purpose of using loops in programming? 2. Explain the key difference between the for, while, and do while loops. 3. What happens if you forget to include the increment/decrement statement in a while loop? 4. How can you interrupt an infinite loop during program execution? 5. What is the role of the setw() and setfill) manipulators in C++? 6. In a nested loop, how does the inner loop behave relative to the outer loop? 7. What is type casting, and why is it used in loop calculations? 8. How does the do while loop differ from the while loop in terms of condition checking? 9. What output formatting options can be used to align numerical results in columns? 10*. How would you modify a loop to skip certain iterations based on a condition? 1. List the six main biwise operators in C++ and explain the function of each. 2. Why cannot bitwise operations be applied to variables of floating-point type? 3. Explain the purpose of the << (left shift) and >> (right shift) operators. What is the typical effect on the decimal value of a number when it is shifted left by 1? Shifted right by 1? 4. Describe the process of using a mask to check the value of a specific bit within an
integer. 5. How can you use the bitwise AND operator (&) to check if a number is even or odd?
Explain the logic. 6. What is the difference between the logical AND (&&) and the bitwise AND (&)? Provide an example scenario for each. 7. Explain the purpose of the ~ (bitwise NOT) operator. What is the result of applying it to a mask, and how can this be useful? 1. What is the primary goal of program debugging? What types of errors can it help identify? 2. Describe the difference between Step Over (F10) and Step Into (F11) debugging commands. When would you choose one over the other? 3. What is the purpose of a breakpoint in planned debugging? How do you set and remove a breakpoint in Visual Studio? 4. Explain the utility of the "Watch" window compared to the "Autos" or "Locals" windows during a debugging session. 5. What is the key difference between the Debug and Release configurations when building a project? Why is it necessary to create a Release version after successful debugging? 6. List at least three types of files commonly found in a project's Debug folder and briefly state their purpose (e.g., *.pdb). 7. During debugging, you notice a variable has an incorrect value. How can you change its value during runtime to test a hypothesis without modifying the source code? 8. What command is used to exit the debug mode and stop the current debugging session? 1. What is an array in C++? List its three main characteristics. 2. How are array elements numbered in C++? What is the valid index range for an array declared as int data[25];? 3. Explain the difference between array declaration and initialization. Provide an example of each. 4. What is an initializer list? What happens if the initializer list is shorter than the array size? 5. How can you let the compiler automatically determine the size of an array during initialization? 6. What values do elements of a local array contain if it is declared but not explicitly initialized? How does this differ from a global array? 7. What is an array out-of-bounds error? Why is it dangerous, and what are its potential consequences? 8. How do you calculate the number of elements in an array using the sizeof operator?
Provide the formula. What is a significant limitation of this method? 9. Why is it impossible to copy the contents of one array into another using the assignment
operator (arrayB = arrayA;)? What is the correct way to perform this operation? 10. Why does comparing two arrays using the equality operator (arrayA == arrayB) not check if their elements are equal? How should array comparison be done correctly? 11. What does the name of an array represent in terms of memory? 1. What is a pointer in C++ and what are its two main attributes? 2. Explain the difference between the & and * operators when working with pointers. 3. Why is pointer initialization critical and what dangers do uninitialized pointers pose? 4. What is the fundamental relationship between arrays and pointers in C++? 5. How does pointer arithmetic work and why does ptr + 1 advance by the size of the pointed type rather than 1 byte? 6. What is the difference between an array name and a pointer variable? Why can't you increment an array name? 7. What are the differences between const int*, int* const, and const int* const? 8. How can you safely iterate through an array using pointers, and what are the boundary risks? 9. What is a null pointer and why should you check for nullptr before dereferencing? 10. How do you access array elements using pointer syntax, and how does the compiler translate arr[i] internally? 1. What is a multidimensional array? How is a two-dimensional array structured in memory? 2. Explain the concept of an "array of arrays". How does this relate to the declaration int arr/ROWS//COLS;? 3. The name of a two-dimensional array without indices is a pointer constant. What does this pointer point to? What do the expressions *(A + i) and *(*(A + i) +j) mean for a two-dimensional array A? 4. Describe the different ways to access the element A/1/[2/ of a two-dimensional array
using pointers. 5. What is the rule for omitting the size of dimensions when initializing and when passing a multidimensional array to a function? Why is it allowed to omit only the first dimension? 6. Explain the principle of "row-major order" for storing two-dimensional arrays in memory.
How does this affect element access? 7. Why are nested loops the standard tool for processing multidimensional arrays?
Describe the typical pattern for iterating through a matrix. 1. How is a character string stored in memory in C++? What is the role of the null terminator (10), and why is it critical for C-style strings? 2. Why must the size of a char array declared to hold a string be at least one greater than the number of characters you intend to store? 3. The array name without an index is a pointer constant. What does the name of a char array point to? 4. What are the two main ways to initialize a C-style string? What is a common mistake when using the initializer list method, and what is its consequence? 5. Why is it necessary to add _CRT_SECURE_NO_WARNINGS to the preprocessor definitions in Visual Studio when working with many standard C library functions?
What is the alternative approach? 6. What is the key difference between stropy and strncpy? Why might strncpy be considered safer? 7. How does the stremp function determine if one string is "less than" another? Why can't you use the == operator to compare two C-style strings for content equality? 8. Describe the purpose and parameters of the strok function. How do you get all tokens from a string? 9. What do the functions strchr and strrchr do? How do they differ? 10. Explain what the strstr function returns and what it is commonly used for. 11. What is the purpose of the functions in the < cctype> header? Give three examples of such functions and their use. 12. What is the difference between tolower(c) and_tolower(c)? When should you use each? 1. What is a function in C++? Name the three core benefits of using functions in a program. 2. What is the difference between a function declaration (prototype) and a function definition? Provide examples. 3. What is a function signature? Which elements are part of the signature, and which are not? 4. What methods of passing parameters to a function do you know? Explain the difference between pass-by-value, pass-by-pointer, and pass-by-reference. 5. Why can't you pass an array to a function by value? What is the correct way to pass an array to a function? 6. What is variable scope? How is it related to functions? 7. How does a function return a value? What happens if a function with a non-void return type does not return a value on all control paths? 8. Can you use multiple return statements in a single function? Provide an example. 9. What is function overloading? What is it based on? 10. How is interaction between functions organized in a program? Provide an example program with several functions. 11. What are default parameters? How are they specified, and in what cases are they useful? 12. How can you prevent a function from modifying the data passed to it? What modifiers are used for this? 13. What is recursion? Provide an example of a recursive function. 14. What common errors occur when working with functions? How can they be avoided? 15. How do you use pointers to functions? Provide an example of declaring and calling a function through a pointer. 用中文回答
最新发布
11-18
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值