Real-time thermal prediction using a linear regression model on STM32F767ZI, demonstrating TCM (Tightly Coupled Memory) performance optimization.
This project implements a simple thermal management system that predicts internal component temperature based on:
- Ambient temperature (°C)
- Heat sink temperature (°C)
- Drive current (A)
The system uses a pre-trained linear regression model and provides cycle-accurate performance measurements via UART output.
- MCU: STM32F767ZI (Cortex-M7, 216 MHz)
- Board: STM32 Nucleo-144 or compatible
- Peripherals:
- USART3 for debug output (115200 baud)
- HSE crystal/oscillator (8 MHz)
- TCM Memory: Code and data placed in zero-wait-state memory
- ITCM (16 KB): Fast instruction execution
- DTCM (64 KB): Fast data access
- MPU Configuration: Non-cacheable AXI SRAM region for DMA operations
- Cycle Counter: DWT-based performance measurement
- Standard build: Code runs from Flash with normal SRAM
- Fast RAM build (
USE_FAST_RAM): Critical code/data in TCM for maximum performance
firmware/
└── app/
├── include/
│ ├── mem_place.h # Memory placement macros (FAST_CODE, FAST_DATA)
│ ├── project_init.h # Hardware initialization (TCM, MPU, DWT)
│ ├── regression_model.h # Linear model interface
│ └── thermal_demo.h # Demo application API
└── src/
├── regression_model.cpp # Model implementation
└── thermal_demo.cpp # Demo logic and UART output
Application/
└── User/
└── Core/
└── main.cpp # Application entry point
- Open the project in STM32CubeIDE
- Generate BSP
- Build the project (Ctrl+B)
For TCM optimization, define USE_FAST_RAM:
-DUSE_FAST_RAMBased on STM32F767ZI architecture and linker script:
ITCM (16 KB):
0x00000000 - 0x00003FFF .fast_code section (instruction TCM)
DTCM (64 KB):
0x20000000 - 0x2000FFFF .fast_data, .fast_bss sections (data TCM)
SRAM1 (368 KB):
0x20010000 - 0x2006BFFF Main RAM (.data, .bss, heap, stack)
SRAM2 (16 KB):
0x2006C000 - 0x2006FFFF Additional SRAM
Flash (2 MB):
0x08000000 - 0x081FFFFF Program code and constants
- Flash the firmware to the STM32F767
- Connect a serial terminal to USART3 (115200 baud, 8N1)
- Observe real-time predictions with timing information:
[Thermal Regression] init
Tamb=24.1C Tsink=23.0C I=0.58A -> T_int_hat=26.45C | 152 cyc (~0.704 us)
Tamb=24.2C Tsink=23.1C I=0.62A -> T_int_hat=26.78C | 148 cyc (~0.685 us)
...
// Initialize with UART handle
ThermalDemo_Init(&huart3);
// Enable simulated inputs (default: enabled)
ThermalDemo_EnableSimulatedInputs(true);
// Or provide real sensor values
ThermalDemo_SetInputs(25.0f, 24.0f, 0.65f);
// Run one inference cycle
ThermalDemo_Step();
// Get last prediction without printing
float temp = ThermalDemo_GetLastPredictionC();Typical inference timing (216 MHz, -O0):
| Configuration | Cycles | Time (μs) |
|---|---|---|
| Flash/SRAM | ~308 | ~1.43 |
| TCM (FAST_RAM) | ~274 | ~1.27 |
Performance gain: ~11% faster with TCM
The linear regression model was trained offline (see Temperature Model Training.ipynb) with the following coefficients:
// Model: T_internal = w0*T_amb + w1*T_sink + w2*I_drive + bias
w = [0.594197, 0.308914, 4.995486]
b = 1.945341Critical initialization order in main():
1. MPU_SetAXI_NoCache() // Configure non-cacheable region
2. EnableTCM() // Enable ITCM/DTCM
3. CopyTCM() // Copy code/data to TCM
4. HAL_Init() // HAL initialization (can now use FAST_CODE/DATA)
5. SystemClock_Config() // Configure 216 MHz clock
6. Peripheral init // GPIO, UART, etc.
7. DWT_ForceEnable() // Enable cycle counterImportant: TCM must be initialized before HAL_Init() to ensure any HAL code/data marked with FAST_CODE/FAST_DATA is properly placed.
The linker script must define the following sections for TCM operation:
/* ITCM section for fast code */
.fast_code : {
_sitcm = .;
*(.fast_code)
_eitcm = .;
} >ITCM AT>FLASH
/* DTCM section for fast data */
.fast_data : {
_sdtcm = .;
*(.fast_data)
_edtcm = .;
} >DTCM AT>FLASH
/* DTCM BSS section */
.fast_bss (NOLOAD) : {
_sdtcmbss = .;
*(.fast_bss)
_edtcmbss = .;
} >DTCM