sora
New Member
Posts: 6
|
Post by sora on Jan 12, 2019 10:14:22 GMT
Hello,
I just moved my project from Arduino Due to ShieldBuddy so I want to find something similar to DueTimer library. When a timer ends its period, an interrupt handler should appear and allow me to do something.
I tried CreateTimerInterrupt(ContinuousTimerInterrupt, 10000, STM0_inttest); from ShieldBuddy TC275 Development Platform but there is no function to stop and resume it so I assume it does not work like DueTimer.
I also tried the Timer example in Aurix Unleashed but it lacks something and doesn’t work too. Can anyone explain or give an example of how to connect the output of TOM Timer to a ISR handler?
Thank you!
|
|
|
Post by Admin on Jan 12, 2019 14:09:24 GMT
There is a one shot mode for CreateTimerInterrupt(ContinuousTimerInterrupt, 10000, STM0_inttest);
Please check the user manual!
|
|
sora
New Member
Posts: 6
|
Post by sora on Jan 12, 2019 16:16:30 GMT
Thank you for your reply. I checked both Continuous mode and One Shot mode. They can make a few loops but do not last a few milliseconds. Is there another way to call a specific function from within itself periodically for longer, maybe a few minutes to one hour? I am considering using different Timers because they can provide stable frequency. Do you have example of TOM Timer with ISR for Arduino? I found only examples for Eclipse and other debugger.
|
|
|
Post by Admin on Jan 14, 2019 8:18:56 GMT
You cannot call a function from itself! It is not clear what you are trying to do. There is another way to create interrupts using the General Timer system. There are nine general purpose timers available (based on the ATOMs in the GTM) that can be used to call an user-defined function from a periodic interrupts. The longest time is about 170 seconds and the shortest is around 1us. The units of time are by default based on 0.02us per count (50MHz).
Example
We want call this function every 100us using timer 2: void UserTimer2Handler(int i) { digitalWrite(13,!digitalRead(13)); }
First, set the name of the function to be called:
// Set user handlers TimerChannelConfig[2].user_inthandler = UserTimer2Handler; Then initialize the Timer 2 channel:
// Initialise general timer channels InitialiseTimerChannel(2);
Finally, set the period of the interrupt, in units of 0.02us: // Set period of timers SetTimerChannelPeriod(2, 5000);
The function UserTimer2Handler() will now be called every 100us.
You can temporarily disable a timer channel using:
DisableTimerChannelInt(2)
And restart it with:
EnableTimerChannelInt(2)
|
|
sora
New Member
Posts: 6
|
Post by sora on Jan 18, 2019 2:29:31 GMT
Thank you, this works. Here is what I am trying to do: I want to create pulse with adjustable width ranges from 10 ns to 1000 ns, frequency is adjustable from 1 Hz to 200 Hz. This 10 ns pulse width is the reason why I moved from Arduino Due to ShieldBuddy. With this short pulse width, pulse generation frequency must be from FXCLK 1. However, I could not generate low and adjustable frequency. Lower than 1.6 kHz, the frequency does not output correctly. My previous approach is: 1. Run a timer with period corresponding to the desired frequency. 2. When timer is up, stop it in handler, update new frequency and pulse-width. 3. Create an output pulse with input width, using another timer/counter (with ShieldBuddy, I am trying to use TOM), and after its duty-cycle is over, this internal timer/counter is stopped. 4. In outer timer handler, start it again with input frequency(period) This approach allows me to update frequency and pulse-width regularly with Due. Low frequency and above 1us pulse can be created. I am not able to do the same with ShieldBuddy currently.
|
|
|
Post by Admin on Jan 18, 2019 8:40:45 GMT
It is possible to create a 10ns pulse on the GTM as it can run at 100MHz. It might be better to draw a diagram of what you want to do as it is not clear from your description!
BTW: This should generate a 10ns pulse at 390kHz on pin 2!
analogWrite(2, 1);
|
|
sora
New Member
Posts: 6
|
Post by sora on Jan 22, 2019 2:19:29 GMT
Sorry for not showing more details. I have input buttons for controlling the frequency and pulse width. Frequency set has 200 values: 1Hz, 2Hz, 3Hz…200Hz. Pulse width set has 100 values: 10ns, 20ns, 30ns…1000ns. I tried your suggestion. It can generate 10ns pulse and other values. Now I am stuck at controlling frequency. In the attached file, every time the timer finish 1 period, it is stopped to update new input of period and width. Period is calculated from input frequency. The new value can be the same or different from the old value. After the first pulse is generated, pulse generator should stop, wait for timer to stop after approximately correct period and then generate the next pulse.
|
|
|
Post by Admin on Jan 24, 2019 9:04:28 GMT
You can control the frequency by writing directly to the GTM ATOM SFR used to generate the period for the analogWrite() function. For pin 2 as the pulse output, use:
GTM_ATOM0_CH0_SR0.U = value;
Value will set the period (1/frequency) in units of 10ns so 1Hz would be 100E6, 200Hz would be 500000 and so on.
|
|
sora
New Member
Posts: 6
|
Post by sora on Mar 3, 2019 10:00:05 GMT
I decided to go back to the basic. Please correct me if I am wrong somewhere. I put the below code in setup() and hope it can generate pulses from pin 13. Sorry, I need pulse on pin 13, but it does not work.
void TomPwmSetup2(){ uint32 reg1; GTM_TOM0_CH2_CTRL.B.CLK_SRC_SR = 1; GTM_TOM0_CH2_CTRL.B.SL = 1; GTM_TOM0_CH2_CTRL.B.OSM = 0; GTM_TOM0_CH2_CM0.B.CM0 = 120; GTM_TOM0_CH2_CM1.B.CM1 = 60; GTM_TOM0_CH2_SR0.B.SR0 = 120; GTM_TOM0_CH2_SR1.B.SR1 = 60;
GTM_TOM0_CH2_CN0.B.CN0 = 10;
GTM_TOM0_TGC0_FUPD_CTRL.B.FUPD_CTRL2 = 2; GTM_TOM0_TGC0_GLB_CTRL.B.HOST_TRIG = 1; GTM_TOM0_TGC0_FUPD_CTRL.B.FUPD_CTRL2 = 1;
GTM_TOM0_TGC0_ENDIS_CTRL.B.ENDIS_CTRL2 = 2; GTM_TOM0_TGC0_ENDIS_STAT.B.ENDIS_STAT2 = 2;
GTM_TOM0_TGC0_OUTEN_CTRL.B.OUTEN_CTRL2 = 2; GTM_TOM0_TGC0_OUTEN_STAT.B.OUTEN_STAT2 = 2;
GTM_TOM0_TGC0_GLB_CTRL.B.UPEN_CTRL2 = 2; reg1 = GTM_TOM0_CH2_CN0.B.CN0; SerialASC.print("reg1: "); SerialASC.println(reg1); }
|
|