PWM pins as TIM

Will I be able to use the PWM pins as timers in pulse counters mode? If yes then how?

I guess you can use marshal to change some registers.

I try change TIMx auto-reload register (TIMx_ARR) and I can’t
I want use 32-bit timer2 or timer5

Address offset: 0x2C
Reset value: 0xFFFF FFFF
The counter is blocked while the auto-reload value is null.

const uint TIM2_BASE = 0x40000000;
uint tim_offset = 0x2C;

int tim_reg = Marshal.ReadInt32((IntPtr)(TIM2_BASE + tim_offset));
Debug.WriteLine("tim_offset=" + tim_offset.ToString("X") + " tim_reg=" + tim_reg.ToString("X")); //tim_offset=2C tim_reg=00

Marshal.WriteInt32((IntPtr)(TIM2_BASE + tim_offset), (unchecked((int)0xFFFFFFFF)));

tim_reg = Marshal.ReadInt32((IntPtr)(TIM2_BASE + tim_offset));
Debug.WriteLine("tim_offset=" + tim_offset.ToString("X") + " tim_reg=" + tim_reg.ToString("X")); //tim_offset=2C tim_reg=00

I try change other registers and also I can’t. Why?

These are the available register to use. If you find an issue we can easily fix for you

https://docs.ghielectronics.com/software/tinyclr/tutorials/marshal.html?q=Marshal

What register did you want to access? we will add if this is needed and not in the current list.

After

  var pwm2 = PwmController.FromName(SC20260.PwmChannel.Controller2.Id);
  var pwm2_1 = pwm2.OpenChannel(SC20260.PwmChannel.Controller2.PA15);

I can read and write registers

const uint TIM2_BASE = 0x40000000; //0x40000000 - 0x400003FF TIM2
uint tim_offset = 0x2C;
int tim_reg = Marshal.ReadInt32((IntPtr)(TIM2_BASE + tim_offset));
Debug.WriteLine("tim_offset=" + tim_offset.ToString("X") + " tim_reg=" + tim_reg.ToString("X")); //tim_offset=2C tim_reg=FFFFFFFF
tim_reg = tim_reg - 0xF;
Marshal.WriteInt32((IntPtr)(TIM2_BASE + tim_offset), tim_reg);
tim_reg = Marshal.ReadInt32((IntPtr)(TIM2_BASE + tim_offset));
Debug.WriteLine("tim_offset=" + tim_offset.ToString("X") + " tim_reg=" + tim_reg.ToString("X")); //tim_offset=2C tim_reg=FFFFFFF0

but this is not correct - the PWM pins are set to output.

I want to use Timer 2 or Timer 5 to measure frequency. I need to get the number of pulses in a time interval. How can I do it?

@Dat_Tran this is related to the frequency counter we have in issues

Yes. I am trying to do this across registers. but this is quite difficult

There are a lot registers need to be change. The pin need to be switched to timer input instead of pwm output also. Anyway, we can take a look if this is important to you.

But we don’t think it is accurate if control stop/start by C#. How accurate do you want?

I understand it. But unfortunately you don’t have a frequency counter yet.

Yet :nerd_face:

Yes, this is important for me because I have an old project based on the G120 that will need to be translated to a new SITCore module (SCM20260N)

Does G120 have frequency counter class?

No but it has RLP :slight_smile:

No. I have only 2 function: InitMeasureFreq(), MeasureFreq()
This functions used Register class for writing and reading timer registers

I used the example “G120 pulse counting using registers” by MikeCormier from Codeshare

in this way, I got a measurement accuracy of 1E-5

As far as I remember, NETMF has a limitation - do not use timer 0. Is there a similar limitation in TinyCLR?

Yes but please wait. We are almost done