Tuesday 27 December 2016

First steps with stm32l152c-discovery. First program generating pwm

As for me, Eclipse IDE is a very huge IDE. Today I want to introduce you a better environment, I want to show how to work with an embedded microcontroller in Qt Creator. Also, we will build our first project and generate a square wave. For all these things we are going to use CubeMX and a little bit of code writing.




Generating PWM.

First of all, we should generate code for our project. Let's open CubeMX tools. I have the next configuration scheme:

Please, set your eyes on TIM10 configuration — on that channel we will generate pwm.
Now, let's go to the tab clock configuration. From the manual we can see, that TIM10 is attached to AHB2 timer, thus, we should configure AHB2 to obtain required frequency:


Now, we are moving to Configuration tab:

Open TIM_10 configuration window and insert parameters:

Prescaler — this is a divider for input timer frequency clock. Since we don't need any prescaler, set it to 0.
Counter period — how many ticks timer makes before reload.
Internal clock division — set it to zero to have no clock division.
Mode — chose PWM mode.
Pulse — duration of a pulse.
Polarity — set it to high, so 'one' will match high level in the output.
 

Make all things work.

Since we generated code for our project, we need to add some files to make the project be compiled. I have added two files: system_stm32l1xx.c into 'Src' folder and STM32L152XC_FLASH.ld into project folder. Now we should create a makefile, I made this one:


try to compile the project, if something goes wrong, read error messages and fix them. After this step you should obtain the file 'square_wave_generation.o' in your project directory.
The last step is to add one line to main.c. Add

HAL_TIM_PWM_Start(&htim10, TIM_CHANNEL_1);

between

/* USER CODE BEGIN 2 */
and
/* USER CODE END 2 */

Loading on the board.
 

To load our new file into the board, first of all we should connect it to out host computer with usb cable and start openocd in a terminal:
openocd -f board/stm32ldiscovery.cfg
you should obtain a similar output:
Open On-Chip Debugger 0.9.0 (2015-06-21-13:00)
Licensed under GNU GPL v2
For bug reports, read
http://openocd.org/doc/doxygen/bugs.html
adapter speed: 300 kHz
adapter_nsrst_delay: 100
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
none separate
srst_only separate srst_nogate srst_open_drain connect_deassert_srst
Info : Unable to match requested speed 300 kHz, using 240 kHz
Info : Unable to match requested speed 300 kHz, using 240 kHz
Info : clock speed 240 kHz
Info : STLINK v2 JTAG v16 API v2 SWIM v0 VID 0x0483 PID 0x3748
Info : using stlink api v2
Info : Target voltage: 2.922857
Info : stm32l1.cpu: hardware has 6 breakpoints, 4 watchpoints

Now we should create in eclipse new debug configuration.
Go to Run → Debug Configurations... . In the list on the left, click the right mouse button on 'GDB Hardware Debugging' and create new configuration. In the tab 'Debugger' change GDB command to: arm-none-eabi-gdb and port number to '3333'. Also, choose 'Using Legacy GDB Hardware Debugging Launcher' at the bottom of the page. We should obtain a similar window:


Now, click on the down arrow next to the bug icon and choose your debug configuration. After some time we will have an ability to execute our application by pressing the button 'Resume'.
After starting the program, we should start Saleae analyzer program and connect hardware analyzer to our board (PB8 pin to any channel of the analyzer). In the analyzer program we should obtain a similar output:


That means that we have obtained a square wave signal using PWM on stm32l152 with a frequency 8 Mhz.

No comments:

Post a Comment