cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to write to TIM2 Registers

AMacd.1
Associate III

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

1 ACCEPTED SOLUTION

Accepted Solutions

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 ?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

8 REPLIES 8
AMacd.1
Associate III

Anyone?

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 ?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
AMacd.1
Associate III

@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.

AMacd.1
Associate III

@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!

>>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.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Sure that makes sense, I guess. Thanks for the additional explanation. By that logic, though, this issue would probably apply to any peripheral that needs a clock; for example, if the USART clock source isn't enabled, you won't be able to set up any of the registers on that peripheral either. (I tried it in the debugger and the USART registers can't be modified until PCKEN5 bit is set).
Good to know!

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?

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.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..