Question (Huawei HiSilicon Chip Development Written Exam Question)The following two code snippets are used to configure the clock frequency of the SPI controller (target 1MHz), but the code using library functions experiences data loss during high frame rate camera transmission, while the code using direct pointer manipulation works correctly. Please analyze the reason:
Code 1 (Library Function Implementation)
|
#include “hi_spi.h” void spi_set_clk() { // Call the library function provided by HiSilicon to configure the clock HI_SPI_SetClkFreq(SPI_DEV_0, 1000000); // Target 1MHz } |
Code 2 (Direct Pointer Manipulation)
|
#include <stdint.h> // SPI Clock Configuration Register Address #define SPI_CLK_REG (*(volatile uint32_t *)0x12050018) void spi_set_clk() { // Directly write to the register for configuration (assuming the division factor is calculated as 0x20) SPI_CLK_REG = 0x20; // After configuration, the clock is 1MHz } |
Answer Analysis
1. Where is the problem? The “redundant operations” in the library function slow down the speed.I was stuck on this issue for three days while debugging the HiSilicon SPI camera driver. When measuring the SPI clock with an oscilloscope, I found that although the library function configured the clock to 1MHz, during actual transmission, there were frequent “glitches” and “delays”, leading to data loss from the camera. After decompiling the library function, I discovered:◦ The library function added a lot of redundant checks for compatibility with various chip models (such as checking if the current chip supports that frequency, checking if other peripherals are occupying the bus);◦ Each call goes through 8 layers of function nesting, consuming over 20 clock cycles just for stack operations, and triggering multiple memory reads and writes;◦ The high frame rate camera requires the SPI clock configuration to be completed within 10us, but the library function takes over 50us, causing the configuration to not complete before the camera starts sending data, naturally leading to data loss.2. Why is pointer manipulation faster?Direct pointer manipulation of the register is equivalent to “one step to completion”:
Later, I switched to pointer manipulation, and the stability of the SPI clock significantly improved, and the camera data was no longer lost.
◦ After compilation, it becomes a singleSTR instruction (ARM architecture), directly writing 0x20 to address 0x12050018, taking only 1 clock cycle;◦ No redundant checks or function calls, completely aligned with hardware timing, meeting the real-time requirements of high-speed peripherals.
Key Point AnalysisThe chips from Huawei HiSilicon (such as security chips, AI chips) are widely used in high-speed data transmission scenarios, requiring high performance for low-level operations. The core of this question is to understand the “essential difference between library functions and direct register operations”:
• Advantages and Disadvantages of Library Functions
• Advantages: Strong compatibility (supports multiple chip models), encapsulates complex logic (such as error handling, boundary checks), suitable for rapid development;• Disadvantages: Many redundant operations, longer execution time, may not be suitable in high-speed peripheral scenarios (such as SPI, I2C above 100Mbps).
• When is direct manipulation necessary?
• High-speed data transmission (such as cameras, high-speed ADC/DAC): Requires register configuration to be completed in microseconds;• Real-time control systems (such as motor drives, industrial sensors): Need precise control of operation timing;• Resource-constrained embedded systems (such as small-capacity MCUs): Library functions may occupy too much RAM/ROM.
• Real Case: The “Frame Rate Bottleneck” of HiSilicon Security ChipsIn a project using HiSilicon’s HI3519 chip for 4K camera acquisition, the initial use of library functions to configure SPI resulted in a maximum frame rate of only 25fps. After switching to direct register manipulation, the frame rate increased to 30fps (meeting the real-time requirements of the security industry). Later, I checked the chip manual and found that HiSilicon’s official driver also recommends using direct register manipulation in high-performance scenarios.
Tomorrow’s PreviewZTE Written Exam Question – Why callingmallocin an interrupt service function can lead to system crashes? The “taboo” of embedded memory management.