[part 1 part 2]
In this post the code I will present the code related to the simple project described in my previous post.
Basically this piece of code send some data through one of the SPI using DMA. This approach has two nice properties: the CPU does not have to wait until the transfer has finished; moreover, if the data at the 595s’ outputs need to be maintained for a certain period of time (e.g.: in order to communicate with some old slower logic) one can use the circular mode of the DMA to manage the timing, again without afflicting CPU performance.
So, first we want to configure the STM32 peripherals for our purpose. For this purpose, using the nice STM32CubeMX from STMicroelectronics is seems perfect.
The STM32CubeMX project can be downloaded from here, but mainly we:
- configure clocking (the external crystal is 8 MHz, this involves the RCC configuration)
- configure some GPIOs (the 4 LEDs and one line to control the RCLK input of the 595s)
- configure the SPI
- configure the DMA to use the SPI
- configure the SWD (Serial Wire Debugging)
- configure the USART (for debug)
The complete code, that can be downloaded from here, mainly performs the following tasks:
- Perform some initialization (mainly generated by the STM32CubeMX utility)
- Enter the main cycle in which:
- Prepare the data to be sent to the 595s
- Move the RCLK pin on PC3 (the setSTCP function)
- Start the DMA transfer
- Collaterally some LEDs on the discovery board are toggled and the time required by the DMA to perform the transfer is measured and sent through the serial port.
Of course, take a deep look at the code to understand what it exactly does before executing it on your device.
Happy coding 🙂