TIM2 PWM newbie needs help with bare metal code

Post here first, or if you can't find a relevant section!
dannyf
Posts: 447
Joined: Sat Jul 04, 2020 7:46 pm

Re: TIM2 PWM newbie needs help with bare metal code

Post by dannyf »

if you are brave, try this:

Code: Select all

	tmr2Init(100); 								//initialize the timer
	tmr2OC1SetPR(1000+ 0); tmr2OC1AttachISR(led_flp);	//flip led
	tmr2OC2SetPR(1000+10); tmr2OC2AttachISR(led_flp);	//flip led, at a slightly different rate
dannyf
Posts: 447
Joined: Sat Jul 04, 2020 7:46 pm

Re: TIM2 PWM newbie needs help with bare metal code

Post by dannyf »

so to invoke an isr 10us, you need something like this:

Code: Select all

	tmr2Init(1); tmr2OC1SetPR(10*F_CPU / 1000000ul); tmr2OC1AttachISR(led_flp);
To test this code, set the tmr2 prescaler sufficiently large, using 50000ul. you will see led_flp() getting invoke about 50000 * 10*F_CPU / 1000000ul. With F_CPU = 72Mhz, that should be 0.5 sec :)
RobertoBerner
Posts: 36
Joined: Tue May 09, 2023 10:45 pm

Re: TIM2 PWM newbie needs help with bare metal code

Post by RobertoBerner »

Thank you all, dear friends, for your replies.
I am sorry for the delay in responding, but I am still travelling for business reasons, and sometimes I remain absent for a little while.
As I had to solve my problem fast because I need STM32 for new pending jobs, I decided to do the following:

1. installed Visual Studio Code.
2. added the PlatformIO extension to it.
3. added the Arduino extension to it.
4. added the STM32F103C8T6 board to it.
5. also some other useful and necessary extensions, boards such as Arduino NANO.
6. some time ago I have also installed the STM32 tools STM32CubeIDE, STM32CubeMX, STM32CubePogrammer.
7. fully erased the FLASH of my 3 Bluepills (fake versions), eliminating the HID bootloader
8. return to use the ST-Link V2 (chinese version)

last time I visited this community the status was that, surely because of my ignorance about STM32 as a newbie, I was unable to implement an interrupt generated by TIM2_CH1 in PWM mode. I appreciate all the information gathered but it resulted overwhelming for a newbie in STM32.

My STM32 experience started some time ago using STM32CubeIDE with HAL and using the builtin initialization wizard. At that time I didn't know that the chips on my Bluepills, although marked "ST", were fake. Now I know CPUTAPID is 0x2ba01477 for fakes vs. 0x1ba01477 for originals.) STM32CubeIDE refused to program fake parts, even after many attempts of editing configuration files to avoid checks. This once worked but ST probably has other methods to know what kind of chip is connected as target. The STM32CubeProgrammer perfectly FLASHED and erased all of my parts, but ST doesn't allow debugging of fakes. Bad news for me. Totally reasonable for ST.

Also, using Arduino + STM32 for learning and trying so many frustrating experiments to make my program work, I reached the point in which I had to re FLASH the HID bootloader each time before Arduino uploads. Later I found that this is a very well known issue. Sometimes the bootloader simply fails to switch between USB mode to COM mode so Arduino can't program the part.

With these elements I have, I found that one possible working formula if you need to quickly start using STM32 is to use STM32CubeMX to generate the code needed to completely initialize the chip and also to provide the main(), the basic calls and all the few modules needed for the interrupts including the empty user handlers. These is simply a couple of .c and .h files.
Later, and with the examples working, it is much easier to sit down and study step by step how al this works with a coffee mug, bottom-up. Not the other way around because of the complexity and flexibility of these machines compared to their classic 8 bits counterparts, and with a very different learning curve.
After this, I created a PlatformIO STM32F103C8T6 project. Then simply drag and drop the generated initialization files into the project. All of the comments that STM32CubeMX and STM32CubeIDE generate can be eliminated as they are useless in a VSCode/PlatformIO project. My last learning step was that, although STM32CubeMX perfectly generates all the needed code, HAL function calls are needed in main() to start timers and enable interrupts, one for each timer or peripheral. Once there, is simply C programming as usual.

As in Arduino, there is no need to worry about fake chips and programming dongles. PlatformIO fixes this by simply a dding a configuration line in the platform.ini file (there is one for each user project). Here is the way STM32F103C8T6 is configured in my project:

Code: Select all

[env:bluepill_f103c8]
platform = ststm32
board = bluepill_f103c8
framework = stm32cube
upload_protocol = stlink
debug_tool = stlink
upload_flags = -c set CPUTAPID 0x2ba01477
; change MCU frequency
board_build.f_cpu = 72000000L
Notice that the single upload_flags = -c set CPUTAPID 0x2ba01477 works to recognize fake chips.

Finally I could make my TIM2_CH1 interrupt work easily.

To finish my report, I just want to say 2 things:

1. I hate the fake industry and I understand that manufacturers such as ST need to fight against it being 65% fakes of the chips in the market . In the other hand the common man from the plain also needs to survive, specially when the origin of the parts is uncertain at first sight.

2. I tried Arduino because I sometimes use it for the 8 bit family and I consider it an extremely valuable tool. I appreciate each and every contribution and time dedicated to help me start. But it is important for contributors to understand what happens in the newbie's head. The examples given should be, IMHO, short, concise, given in step by step fashion considering the level of the information, and should be working examples for the family component in focus. Equivalent examples with pieces of code are hard to fit and make them work, because there are differences between components of the same family (e.g.: F1 to F4, etc) impossible to handle at start level with an ARM chip. I am a contributor myself in lots of forums, and as a teacher, I always keep this in mind.

Thank you all - problem solved

Roberto
dannyf
Posts: 447
Joined: Sat Jul 04, 2020 7:46 pm

Re: TIM2 PWM newbie needs help with bare metal code

Post by dannyf »

Two routes here.

1. HAL (including Arduino and platformio): much easier to get in and can solve most of your needs easily. A mighty struggle if you ever wonder out. It is meant for beginners or people are not comfortable with reading 1k page datasheet

2. Bare metal: very simple once you have mastered it. But a steep learning curve to master it.

Pros and cons both ways. No right or wrong answers here.
RobertoBerner
Posts: 36
Joined: Tue May 09, 2023 10:45 pm

Re: TIM2 PWM newbie needs help with bare metal code

Post by RobertoBerner »

Hello dannyf , sure, you are right. In my case at my age 68, having programmed (very skillfully and in absolute assembler) my first M6800 in the 70s, I have spent most of my life programming in the 8 bit world. And made a living out of it, despite being an analog and discrete digital design engineer myself in the first place.

So, interrupts, threads, semaphores, state machines, C language, communications, protocols, peripherals, etc. are common every day tools. I think that I have found the best personal learning curve in STM32 combining Arduino, ST and VSCode and PlatformIO. I am now playing with regular programs for industrial control which has been and still is my specialty, but with a powerful, state of the art machine. I am not quitting 8 bits. There is still a lot to do there. But STM32 ARMs are a must for many of us. And in this subject , I am a newbie !

Arduino is a very good starting point and a very valuable tool, not only for newbies. An original idea and a solution for lots of hobbysts and professionals. So thanks a lot to all contributors for their dedication and patience with people like me.

Cheers !
Roberto
dannyf
Posts: 447
Joined: Sat Jul 04, 2020 7:46 pm

Re: TIM2 PWM newbie needs help with bare metal code

Post by dannyf »

I have spent most of my life programming in the 8 bit world
not much difference vs. life back then. maybe more complexity and better tools. But so long as you are willing to reach the datasheet (reference manual in st talk), you should be up and running fast.
RobertoBerner
Posts: 36
Joined: Tue May 09, 2023 10:45 pm

Re: TIM2 PWM newbie needs help with bare metal code

Post by RobertoBerner »

Sure dannyf, is true.
Post Reply

Return to “General discussion”