This project aims to investigate and implement SPI communication between two STM32 microcontrollers (Master and Slave) using three distinct data handling modes: Polling, Interrupt, and DMA. The goal is to analyze the impact of each mode on CPU utilization and overall system efficiency during data transfer. By simulating the entire system in Proteus, we can visually observe the performance characteristics and behavior under each condition.
In Polling Mode, the Master CPU actively and continuously checks the status flags of the SPI peripheral to determine if it can send the next data byte or if a new byte has been received. This method is simple to implement but highly inefficient, as it completely occupies the CPU, making it suitable only for low-speed or simple applications.
In Interrupt Mode, the SPI peripheral is configured to generate an interrupt when a data transfer is complete or when new data is received. This allows the CPU to work on other tasks in its main loop and only handle SPI communication when necessary, significantly improving efficiency for event-driven systems with medium data rates.
In DMA Mode, the Direct Memory Access controller is configured to manage the entire data transfer between a memory buffer and the SPI data register. This method offloads the CPU completely, allowing it to execute complex algorithms or enter low-power modes while the DMA handles the high-speed data streaming with zero overhead.
STM32CubeMX ConfigurationMaster Board Setup:
- Create new STM32F103C6 project.
- Configure SPI1 in Full-Duplex Master mode.
- Assign Hardware NSS to Disable.
- GPIO Configuration:
- PA4 Output CS (Chip select) , PB12 Output
- PB13 .. PB15 Input
Slave Board Setup:
- Create new STM32F103C6 project.
- Configure SPI1 in Full-Duplex Slave mode.
- Assign Hardware NSS to Disable.
- For Interrupt Mode: Enable SPI1 global interrupt in NVIC settings.
- For DMA Mode: Add DMA requests for SPI1_RX in Normal mode.
- Configure an LCD (e.g., on GPIO Port B) to display received data
Master Board (Polling Mode Only):
- Use HAL_SPI_Transmit().
- Manually control NSS pin (LOW before transmit, HIGH after).
- Trigger transmission based on buttons/timers.
Slave Board (Polling, Interrupt, DMA Modes):
- Polling Mode: Use HAL_SPI_Receive() in a loop (inefficient).
- Interrupt Mode:
- Call HAL_SPI_Receive_IT() once to start listening.
- Implement HAL_SPI_RxCpltCallback() to handle received data.
- DMA Mode:
- Call HAL_SPI_Receive_DMA() once to start listening.
- Data is automatically written to buffer. Process it in main loop or a callback.
- se GPIO pins to select the desired receive mode.
- Add two STM32F103C6 components to the schematic.
- Wiring:
- Connect Master SCK (PB3) to Slave SCK (PB3).
- Connect Master MOSI (PB5) to Slave MOSI (PB5).
- Connect Master MISO (PB4) to Slave MISO (PB4).
- Connect a common ground between both microcontrollers.
- Add components: LCD to the Slave board, buttons to the Master to select transfer mode.
- Add a virtual oscilloscope probe to the SCK and MOSI lines to observe the SPI waveform.
- Load the respective compiled .hex files into each MCU.
- Start the simulation and test each communication mode, observing the CPU load and transfer efficiency.
If you have any questions or suggestions don't hesitate to leave a comment below
Comments