As the high-end part of the series, C805IF12X has the fastest peak speed of 100MIPS and integrates the most on-chip resources. Its 128 KB of on-chip Flash and 8 KB of on-chip RAM are sufficient to meet the needs of most applications. Using C8051F12X, only a few additional drivers and interfaces are needed to form a larger complete system. It's just that the 128 KB of Flash memory inevitably has to deal with bank partition problems.
Fortunately, the Keil C51 development environment has good support for the C8051F series, including general program jumps and calls across bank partitions. When used as a data memory, Flash partition reading and writing is entirely something that programmers have to consider and has nothing to do with the development environment. This article only discusses special forced transfer and μC/OS-II migration issues in multi-bank partitions.
1 C8051F12X’s multi-bank partition transfer mechanism in Keil C51
The connection locator of Keil C51 supports group connections, allowing the generation of 8051 target programs _1_ with code lengths greater than 64 KB. The general 8051 system only provides 16 address lines, and additional address lines are needed to implement code group switching. When the compiler generates bank switching code, it is affected by the configuration file L51_BANK. A51 support, so users must modify this configuration file according to their own hardware structure.
The C8051F12X series does not need to consider the hardware part, and there is no problem of address line expansion. Because the four bank areas of 128 KB are all inside the CPU, there is no need to process 1.5l_BANK as a regular cross-bank jump and call. A51 configuration file. But in special cases this issue must be considered, otherwise the program will not work. Let's take C8051F120 as an example to discuss the transparent group switching process of the code.
C805IF120 is divided into 4 banks in the Keil C51 project configuration, each of 32 KB. The public bank address ranges from 0~0x7fff, and the other banks range from 0x8000h~0xffff. In the corresponding configuration file L51_BANK. In A51, it involves the special function register PSBANK (SFR address: 0B1H), SWITCHn macro, B_BANKn, B_SWITCHn group information storage and switching code, and B_CURENTBANK variable.
PSBANK is a special function register in C8051F120, and the bank access of 128KB Flash is realized through it. If you want to transfer to a new bank, you must give PSBANK the correct value, and then transfer to the address in the bank area.
There are 4 SWITCHn macros, namely SwITCH0, SWITCH1, SWITCH2 and SWITCH3, corresponding to switching to 4 banks. The statement corresponding to SWITCH0 is:
MOV PSBANK. #00h; replace 00h with 1Ih, 22h and 33h,
; It is the other three macros
that will be inserted into the B_SWITCHn code to switch to the new bank and restore to the original bank.
All 4 groups of B_BANKn and B_SWlTCHn codes are also implemented using macros, corresponding to 4 bank processing. They are gathered in the BANK SWITCH code segment. The entire bank switching and recovery mechanism is very clever, and can realize mutual calling and nesting of functions between any banks. The following takes the main function in the bank3 area to call the Delay_noOS() delay function in the bankl area as an example to illustrate this mechanism.
void main(void){
MCUInit(); //Initialize CPU
Delay_n00s(10); //Delay 10 ms
Lcmlnition();
:
:
The assembly statement corresponding to the function Delay_noOS(10) called in bank3 is:
LCALL C: The assembly statement at C:5049 in the 5049
public segment (i.e. Common segment, corresponding to bank0) is as follows: MOV dptr, #Delay_noOS AJMP B_BANKl where B_BANKl is the routine where N is 1 in macro?B_BANK&N. Now let’s get to the core of the problem: All cross-bank program switching and recovery processes are implemented by the following assembly code in the BANK SWITCH code section in the public section. The corresponding Ns are 0, 1, 2 and 3. BANK?SWlTCH SEGMENT CODE PAGE ; B_BANK&N: PUSH B_CURRENTBANK (1) MOV A, #HIGH BANK SWITCH (2) PUSH ACC (3) PUSH DPL (4) PUSH DPH (5) B_SWITCH&N: MOV B_CURRENTBANK, #LOW B_SWITCH&N (6) SWlTCH&N (7) RET (8) : The return address of the Delay_noOS(10) function, that is, the entry address of the function LcmIni-tion() (also in bank3), its high and low bytes are represented as ADDH and ADDL. The initial value of the B_CURRENTBANK variable after the program enters main() is the lower 8 bits of B_SWITCH3, and its meaning will be described later. After AJMP B_BANKl, the program executes (1) to (8) of B_BANKl and B_SWITCHl, and the stack structure when executing (5) is shown in Figure 1.
继续执行B_SWITCHl到(7)时,PSBANK变为指向bankl,B_CURRENTBANK变为B_SWITCHl的低8位。执行(8)后,从堆栈结构可以看出,堆栈弹出①作为新的PC值,程序进入Delay_noOS(10)函数,延时功能完成后,函数最后一条RET指令开始返回。这是Keil C51处理bank机制的关键,此时的返回地址为堆栈中的②,此地址即B_SWITCH&H代码的入口,这里对应main()函数所在的bank3分组,也就是B_SWITCH3的人口。
因为所有B_SWITCH&N的高8位地址,即BANK SWITCH代码段的高8位都一样,由语句(2)中的操作符HIGH BANK SWITCH确定;低8位保存在已经压栈的B_CURRENTBANK变量中,此时堆栈中的?B_CURRENTBANK压栈值是B_SWITCH3的低8位,这样②的地址就是B_SWITCH3。
程序继续执行B_SWITCH3,在执行B_SWITCH3的(6)语句之前,B_CURRENTBANK还是前面执行B_SWITCHl时的值,即B_SWITCHl的低8位。执行语句(6)后,B_CURRENTBANK恢复为B_SWITCH3的低8位,为返回main函数做准备。然后PSBANK置为33h,即指向bank3,接着执行RET语句,堆栈③成为RET的返回地址,程序回到了main()中Delay_noOS(10)的下一条语句继续执行,B_CURRENTBANK也已恢复。
这个调用过程中,用了6个堆栈字节,3条RET指令,关键内容就是B_CURRENTBANK变量,它保存了可以恢复调用前bank环境代码的地址低位。从被调用函数返回 到这个地址后,就能恢复调用前的bank环境,即赋予PSBANK正确的值。
不采用直接保存PSBANK值然后再恢复,而是用压栈的方式保存了相关地址(语句(1)~(3)),是为了实现跨bank区的嵌套调用。例如,在Delay_noOS(10)函数中,如果再次跨bank去调用新函数,会再次重复上述过程,堆栈从②往上再长6个字节。Delay_noOS(10)函数之前执行B_SWITCHI产生的B_CURRENTBANK值(B_SWITCHI的低8位)也会进栈,为调用完新函数后返回到bankl继续执行Delay_noOS(10)提供保证。
2 无操作系统bank分区间的强制跳转
通过上面的分析得知,如果要处理跨bank区的跳转、调用和返回,关键是能正确处理好PSBANK中的内容。当程序没有操作系统用于任务切换,而又需要强制退出某一函数进入到另一函数的某一地址时,比如说在中断发生后,结束原来的工作转入到另一工作去,就需要处理好PSBANK。
如果不考虑bank,可以在转入新地址之前执行一段代码,保存该地址处的环境变量[2],包括堆栈指针sP和需要的入口地址。然后在中断返回之前,恢复此环境变量,执行中断返回指令进入该新地址。这个思路和C51库函数setjump和longjump比较相近,但比它们灵活,因为环境变量可以自己处理。
考虑bank后的情况稍微复杂些,环境变量中需增加bank的处理信息,那么只处理PSBANK行不行呢?
如果仅保存和恢复PSBANK,则很简单,在保存环境变量的程序中加入:
JMPEnv[envl][3]=PSBANK;
在恢复环境变量的程序中加入:
PSBANK=JMPEnv[envl][3];
这里环境变量是二维数组JMPEnv,envl代表一个环境变量,即一个返回点。第二维是变量中的参数个数。因此可以保存多个环境变量以供使用。
初看起来这样处理是没有问题的,可实际上不行。因为进入返回点后,虽然PSBANK正确了,但是B_CUR-RENTBANK可能已经被修改,不能和返回点程序的bank区匹配,如果再次出现跨bank调用的话将不能正确返回。
处理方法是有点技巧的,因为C语言不支持汇编变量B_CURRENTBANK的写法,所以在L51_bank.A51中要加上声明:
PUBLIC BLCURRENTBANK
和伪指令:
B_CURRENTBANK EQU ?B_CURRENTBANK
这样就可以在C程序中使用B_CURRENTBANK
了,先声明B_CURRENTBANK:
extern Uchar data B_CURRENTBANK;
然后在保存环境变量程序中加入:
JMPEnv[envl][3]=PSBANK;
JMPEnv[envl][4]=B_CURRENTBANK;
恢复环境变量程序中加入:
PSBANK=JMPEnv[envl][3];
B_CURRENTBANK=JMPEnv[envl][4];
这样恢复环境变量进入到新程序后,也将恢复该程序对应的正确B_cuRRENTBANK值,问题得到解决。
3 The bank partition processing in no/0S-ll transplantation
is very mature in version 51 of μC/OS-II, but all transplanted versions have not dealt with bank issues. This content needs to be added, otherwise it cannot be included in the C8051F12X series and other multi-bank programs. used in.
As mentioned before, Keil C51 provides transparent switching support for cross-bank calls, but when using the operating system, this transparent switching mechanism also needs to provide support for task switching. Due to task switching, the program may need to run in other code groups. At this time, PSBANK and B_CUR-RENTBANK still stay in the original code group, which will cause the program to crash. Obviously, no matter what the situation is, the values of PSBANK and B_CURRENT-BANK need to be saved and restored before the task switch is completed. The solution is to push PS-BANK and B_CURRENTBANK into the user task stack before each task switch.
According to the requirements of μC/OS-II, when a task is created, the task stack must be initialized as if the running task has just been interrupted. The initial value of B_CURRENTBANK depends on the lower 8-bit address of the switching code segment corresponding to the group where the task is located. Therefore, the task stack initialization function OSTaskStkInit needs to add a parameter INT8U bank to indicate which code group the task is in. And since the initialization function of the task stack is called by the task creation function OSTaskCreate, this function also needs to add the parameter INT8U bank.
In the push and pop macros, you need to add:
PUSH PSBANK
PUSH B_CURRENTBANK
:
POP B_CURRENTBANK
POP PSBANK.
In the initialization function OSTaskStkInit of the task stack, you need to add:
*stk++=17; // Increase the stack length by 2 to 17
;
if(bank= =0x22:){ //bank2
*stk++=bank;
*stk++=CurrentBank2();
else if(bank==0x33){ //bank3
*stk++=bank;
*stk++=CurrentBank3();
}
else{ //bankl And common
*stk++=0xll; //PSBANK
*stk++=CurrentBankl();
)
Among them, there is no problem with any PSBANK value for bank0, so the situation of PSBANK value 0x00 is simplified.
The functions INT8U CurrentBankl(void), INT8U Current-Bank2(void) and INT8U CurrentBank3(void) are implemented in assembly language. The return value is passed through R7 in order to obtain the low 8 of the switching code segment (SWITCHn) corresponding to the group where the task is located. bit address. The reason why it is not written in C language is also that B_SWITCH&N is not supported by C.
The code for CurrentBankl(void) is as follows, and the other two are similar.
RSEG PR CurrentBankl Os_CPU_A
CurrentBankl:
MOV DPTR, #B_SWITCHl
MOV R7. DPL
RET
Conclusion
This article introduces the principle of Keil C51's bank group code switching mechanism for programs larger than 64 KB, and proposes a method for handling banks when there is an abnormal transfer without an operating system, as well as the use of μc/os-II operating system in multi-bank partition program transplantation. The measures that should be taken have been well applied in development examples.
Previous article:Application of single chip microcomputer in micro printer
Next article:Partition jump processing of multiple banks in C8051 F12X
Recommended ReadingLatest update time:2024-11-16 17:57
- Popular Resources
- Popular amplifiers
- Semantic Segmentation for Autonomous Driving: Model Evaluation, Dataset Generation, Viewpoint Comparison, and Real-time Performance
- Machine Learning and Embedded Computing in Advanced Driver Assistance Systems (ADAS)
- Intelligent program synthesis framework and key scientific problems for embedded software
- arm_embedded_machine_learning_design_dummies_guide
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Latest review activity! Free trial of Espressif's RISCV board ESP32-C3-DevKitM-1
- Introduction to the method of board-level online compilation and downloading of C6000 DSP code
- C language algorithm 19: Prime factorization
- EEWORLD University - The principle and use of oscilloscope
- F12 debugging, no need to worry about the essence watermark being blocked anymore
- Qorvo will meet you in Beijing
- Has anyone used the MS41909 lens driver chip for network cameras and surveillance cameras?
- 【BLE 5.3 wireless MCU CH582】5. Hardware I2C drive 0.96-inch OLED
- [Top Micro Smart Display Screen Review] 2.2 SGTools Graphics Editing Software: Keyboard, Curve, QR Code Application
- [Topmicro Intelligent Display Module Review] 8. How to download the Lua script for the smart display?