2022-04-30 01:37 PM
I have this little project for the STM8L101F3 that just writes an arbitrary value to TIM2_CCR1H and TIM2_CCR1L and then tries to start the timer but I get nothing from the timer output.
If I run the project with a debugger and inspect the contents of the registers, I find them unchanged. I tried this with IAR Embedded Workbench under Windows 10 and with sdcc/openocd/stm8-gdb under Ubuntu 20.04LTS and I see the same behaviour. I am using one of those generic STLINK-V2 dongles.
I have another project that just toggles an IO and it works fine. I am just having trouble with TIM2.
So just yesterday I received a brand new STM8L-Discovery. I then downloaded en-stsw-stm8008.zip from st.com and unzipped it.
When I hook up the discovery board to my PC and run the IAR* project "Discovery", and while the debugger is stopped at main(), I open up a view registers window and bring up TIM2. When I try to modify any of the registers, they just stay at zero.
What am I missing?
*IAR EW STM8 V2.20.2
Solved! Go to Solution.
2022-05-05 02:36 PM
This isn't a highly trafficked forum, and forums generally can be hit-n-miss, you may or may not get responses.
Does the STM8 expect you to enable a clock for TIM2, or select a source ? The part in question has TIM2 ?
2022-05-05 12:03 PM
Anyone?
2022-05-05 02:36 PM
This isn't a highly trafficked forum, and forums generally can be hit-n-miss, you may or may not get responses.
Does the STM8 expect you to enable a clock for TIM2, or select a source ? The part in question has TIM2 ?
2022-05-06 07:22 AM
@Tesla,
Thanks for getting back to me.
Yes STM8L101F3 has TIM2. Data sheet: https://www.st.com/resource/en/datasheet/stm8l101f3.pdf
As I said in latest edit of my question, I also am using an STM8L-Discovery board which also has TIM2 and seeing the same results.
Clock for TIM2: You do have a point there. I see I have omitted setting the clock gating register bit for TIM2. I just found on a chinese-language page the following line in some example source code:
CLK->PCKENR1 |= 0x1; // enable clock for TIM2
However, that doesn't explain why I am unable to write to any of the TIM2 registers and read back the contents in the debugger. If I had gotten past this particular sticking point, then I probably would have gone on to troubleshoot why the timer wasn't counting up. In any case, I'll stick that line into my code and see if it makes a difference and get back to you.
2022-05-06 07:35 AM
@Tesla,
Huh! Just like magic, with that line added, all of a sudden I can now see what I've written in the TIM2 registers!
So, this seems to be the thing I was missing.
I find it rather surprising that I can't even write to the registers with the clock source turned off. Maybe the whole peripheral is turned on or off by this bit - that's what I would call an "undocumented feature"!:)
Well thanks for the hint! I guess this question is answered!
2022-05-06 09:04 AM
>>that's what I would call an "undocumented feature"!
Synchronous logic, and it's pesky clocking..
These types of clock enables allow for the design to be compartmentalized, CMOS currents can be reduced by stopping things switching, but just holding static states.
There's probably a clock tree diagram, these usually help establish what's connected to what.
You'll see this more prevalent in the STM32 designs, and where registers in the debugger just read zeros and are unwritable.
2022-05-06 09:19 AM
2022-05-06 09:48 AM
Another point - I suppose that, if the application turns TA2 on and off periodically, then it would also be prudent to set PCKEN5 on and off accordingly to optimize the power consumption during the periods the timer is off. Right?
2022-05-06 10:24 AM
I'm not much of an STM8 expert, I did 8-bit MCUs as a teenager (6502, Z80, 808x, 680x) and moved promptly to 32-bit MCU. My younger brother had one of the first Acorn RISC Machines (ARM2) based systems. Not to say I haven't done my share of 8-bit work, just that I expanded my horizons very early, and I'm not super-keen on 8051's or PIC's
Most 8-bit machines are relatively simple having a single bi-phase clock, as designs move more to the SoC model, integrating more peripherals and memory, these some times get partitioned, depending on the overall size/complexity. Gating clocks being particularly helpful in reducing power consumption on general purpose MCU, as frequently large chunks go unused in any given use case.
The STM8 has evolved a little, I've seen enough examples that clocks are frequently missing from code snippets online, or where those types of things have been done earlier in startup code.