1 overview
1.1 Purpose
This document is used to explain the software optimization of the GPIO module of the Qidian development board.
It is not limited to the realization of hardware functions, but focuses on the realization of high-quality and beautiful software.
2. Optimize content
2.1 Software Features
①Add the relevant configuration of PORTA, PORTB, PORTC, PORTD and PORTAD pins;
②The driver layer and module layer are adjusted according to the newly added PORT;
2.2 Programming Robustness
①Add const modifier before constant;
②Add static before the variables and functions that are only used in this file;
3 Software Implementation
3.1 CodingRule
The details can be seen in the .MC9S12G128_IO_optimizeSourcescode_rules.txt of the source code.
3.2 Driver Layer Optimization
3.2.1 drio_cfg.c
3.2.1.1 drio_dir_reg[] direction register optimization
The changes are as follows:
①Add const modifier;
②Add DRIO_HAS_PORTA2D macro configuration;
③Add two registers, DDR1AD and DDR0AD, for PORTAD pin settings;
Modify reason:
①Reasons for adding const:
According to the coding rule, for constants, const modifier needs to be added;
②Reason for adding DRIO_HAS_PORTA2D:
For MC9S12G128 with different pins, the 100-pin has PORTA to PORTD, but the 64-pin does not, so a macro can be added here to configure it during pre-compilation;
③Add two registers DDR1AD and DDR0AD:
PORTAD has 16 pins, the direction register corresponding to DRIO_PIN0 to DRIO_PIN7 is DDR1AD; the direction register corresponding to DRIO_PIN8 to DRIO_PIN15 is DDR0AD;
3.2.1.2 drio_data_reg[] data register optimization
The changes are as follows:
① Add const modifier;
②Add DRIO_HAS_PORTA2D macro configuration;
③Add two registers, PT1AD and PT0AD, for PORTAD pin settings;
Add PT1AD and PT0AD. Reasons for modification:
PORTAD has 16 pins, the data register corresponding to DRIO_PIN0 to DRIO_PIN7 is PT1AD; the data register corresponding to DRIO_PIN8 to DRIO_PIN15 is PT0AD;
3.2.2 drio_cfg.h
3.2.2.1 Macro definition DRIO_HAS_PORTA2D
The changes are as follows:
As can be seen from the figure below, the pins in MC9S12G128 are not only PORTE, PORTT, PORTS, PORTM, PORTP, PORTJ, but also PORTAD. If the pin number is 100, there are also PORTA, PORTB, PORTC, and PORTD. However, due to the limitation of the number of pins, not all MC9S12G128 have PORTA to PORTD, so macros can be used to configure whether there are PORTA to PORTD.
The original drio_cfg.h only has PORTT, PORTS, PORTM, PORTP, PORTJ, and now PORTAD and PORTA, PORTB, PORTC, PORTD are added.
PORTAD is a special PORT because it has 16 pins. Its registers are as follows:
In order to facilitate the driver layer to call the drio_dir_reg[] level register array for indexing without having to process PORTAD separately, PORTAD is divided into DRIO_PORTAD and DRIO_PORTAD_DUPLICATE. When the upper module layer calls, it only needs to select the DRIO_PORTAD port. When the pin is greater than DRIO_PIN7, the port is increased by one and becomes DRIO_PORTAD_DUPLICATE to index drio_dir_reg[] and drio_data_reg[]. The changes of these two register arrays can be seen in drio_cfg.c.
3.2.2.2 Enumerate DRIO_PIN
The changes are as follows:
Since POATAD consists of 16 pins, when configuring GPIO at the module level, you don't have to worry about whether to use PT0AD or PT1AD when selecting pins, but only need to choose DRIO_PORTAD and DRIO_PIN0 or DRIO_PIN15. Because in the schematic diagram and the chip manual pin configuration diagram, there are only PAD0 to PAD15, as shown below:
As for how to set the corresponding direction register and data register, it has been set in drio_cfg.c and drio.c. For details, please refer to the drio_cfg.c and drio.c chapters.
3.2.2.3 Declaring extern
3.2.3 drio.c
Change log:
3.2.3.1 drio_set_pin_input
The changes are as follows:
In addition to the validity checks for the port range and pin range, port handling is added for DRIO_PIN8 to DRIO_PIN15 of PORTAD. The reasons are visible in drio_cfg.h and drio_cfg.c;
The validity check of the port range and pin range is to prevent the drio_dir_reg[] array from crossing the boundary. In fact, the upper module layer has already done the anti-crossing process, so it can be omitted here.
3.2.3.2 drio_set_pin_output optimization
3.2.3.3 drio_set_pin_value optimization
3.2.3.4 Add drio_read_pin
Prepare for the key acquisition of reading pin levels in the next section.
3.3 Middleware module layer optimization
3.3.1 mdio_cfg.c optimization
Add const modifier to constants.
as follows:
3.3.2 mdio_cfg.h
extern variable declaration
3.3.3 mdio.c
3.3.3.1 Optimization of io initialization function mdio_init
Pointer to a constant
4 Software testing and debugging
4.1 PORTAD Verification Code
After adding PORTAD, in order to verify that PORTAD can correctly set the direction register and data register, add the output settings of PORTAD in drio_cfg.c as follows:
4.2 MCU register
4.2.1 DDR Data Direction Register
PORTAD direction registers DDR1AD and DDR0AD. As can be seen from the figure, we need each bit of DDR1AD and DDR0AD to be 1, that is, output. During debugging, we can see that each bit of DDR1AD and DDR0AD is 1.
4.2.2 Data Register
According to the configuration in mdio_cfg.cz, PT1AD should be 0x55, PT0AD should be 0xff, and debug can show the following:
According to the schematic:
The test shows that the pin levels on the left side from AN0 to AN7 are 1 0 1 0 1 0 1 0, corresponding to PT1AD is 0x55, and the pins on the right side are all high levels.
Previous article:MC9S12G128 modular layered software architecture VI - KEY
Next article:MC9S12G128 modular layered software architecture 2 - IO driver
Recommended ReadingLatest update time:2024-11-16 21:45
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
- Please teach me how to connect a voltage regulator diode to this circuit
- EEWORLD University ---- HVI training series
- There is a difference between the MSP430FR2111 official website routine and the actual running results
- Today, some web pages are black
- Download Gift|ADI System Solution Selection (Summary)
- TM4C123's serial port DMA can only send the first byte of the buffer
- "Clock Termination" to Easily Solve Signal Attenuation
- I'm looking for two books. I searched all over the Internet and only found them in the library. Can anyone provide me with a pdf or second-hand book? I'll give you 20 yuan. Thanks.
- [Mill Edge AI Computing Box FZ5 Review] EasyDL realizes the construction of flower classification and recognition network
- Design examples + a little error for new students to learn from