Hi ,
please can anybody see my code and tell me why "timer_.cnt" variable is not incrementing in my code?? the code is never coming out of "while(timer_.cnt < tmp_timer)" loop.
in my code i defined some Timer variables in STRACTURE form, assigning them defined values and comparing with the Flex Timer actual values and toggling the pins of PORTC0 and PORTC1
if i am incrementing "timer_.cnt++" in side while loop it's working fine but if i am putting the value of FTM it's not incrementing (I think so).
I tried to debug the code but FTM is FROZEN in Debugging mode :smileysad:
Note : I checked FTM alone working fine and also code till "while(timer_.cnt < tmp_timer)" this loop working fine. (I toggled the PORTC0 and POTC1) pins.
I am working with TRK-KEA128 Board
/*
* main implementation: use this 'C' sample to create your own application
*
*/
#include "derivative.h" /* include peripheral declarations */
#define PRTC0 0x10000
#define PRTC1 0x20000
#define PRTC2 0x40000
#define PRTC3 0x80000
//brief Zeit dier benötigt wird ein Fail zu löschen
#define FAIL_TIMEOUT (unsigned int) 596 // 6.4µsec x 596 = 3.814 mSec => 2500 * 100
#define FAIL_MIN_TIME (unsigned int) 298 // 6.4µsec x 298 = 1.9 mSec => 10 * 100
#define MIN_OFF_TIME (unsigned int) 7450 // 6.4µsec x 7450 = 47.68 mSec => 500 * 100
#define MIN_OFF_TIME_START_UP (unsigned int) 745 // 6.4µsec x 745 = 4.76 mSec => 50 * 100
#define MIN_PROG_RUN_TIME (unsigned int) 373 // 6.4µsec x 373 = 2.38 mSec => 25 * 100
#define MAX_WAIT_FOR_OFF_TIME (unsigned int) 62500 // 6.4µsec x 62500 = 400 mSec => 5000 * 100
#define FRA_DETECT_CYCLES (unsigned char) 150 // 6.4µsec x 150 = 0.960 mSec
#define RECOVERY_CHECKTIME (unsigned int) 2000 // .05µsec x 2000 x 26 (Loop Cycles) = 2.6 mSec
// Timer Variables
struct _Timer_
{
unsigned int cnt; // Umlaufender Timer, für allgemeines Programmtiming, 100µs Auflösung
unsigned int active; // FRA Timer, zur zeitlichen Steuerung des Wischens, 100µs Auflösung
unsigned int fail_up_cnt; // FAIL up counter
unsigned int fail_down_cnt; // FAIL down counter
unsigned int waitoff; // WAITOFF warte die Mindestoff-Zeit ab
unsigned int Recovery_counter;
};
typedef struct _Timer_ Timer;
static volatile Timer timer_ = { (unsigned int)0, (unsigned int)0, (unsigned int)0, (unsigned int)0, (unsigned int)0,(unsigned int)0};
void delay(unsigned long k);
static void startTimer(void);
void REFRESH_WDT(void);
static void TMR_CNTRL(void);
void main(void)
{
asm("CPSID i"); // Disable Interrupt
unsigned int tmp_uint = 0; // zum kurzigen merken von intergern, zB FraState */
unsigned int tmp_timer = 0; // Zwischenmerker für Zeitmessungen, bei Mindestlaufzeit und warten auf Dunkelphase */
ICS_C1|=ICS_C1_IRCLKEN_MASK; /* Enable the internal reference clock*/
ICS_C3= 0x90; /* Reference clock frequency = 39.0625 KHz*/
while(!(ICS_S & ICS_S_LOCK_MASK)); /* Wait for PLL lock, now running at 40 MHz (1024 * 39.0625Khz) */
ICS_C2|=ICS_C2_BDIV(1) ; /*BDIV=2, Bus clock = 20 MHz*/
ICS_S |= ICS_S_LOCK_MASK ; /* Clear Loss of lock sticky bit */
WDOG_CNT = 0x20C5; // write the 1st unlock word
WDOG_CNT = 0x28D9; // write the 2nd unlock word
// delay(10);
WDOG_TOVALL = 30; // 10 x (1 / 1000) = 10 msec but cause of tolerance it's 16msec (Appx. +25 /-35 %)
WDOG_TOVALH = 0; // Always Write the Value for Cortex Controller in LOW & HIGH Byte
WDOG_CS2 = 0x01; // Select clock 1Khz ; to select 32 Khz clock we need to Enable in ICS_C1 register internal reference clock enable bit ICS_C1[IRCLKEN]
WDOG_CS1 = 0x80; // Enable WDT : Feed the Dog : Activate the Dog
GPIOA_PDDR = 0xF07FE; // GPIO PORTC as Output // Define PortA (A1-A3) all Pin As Output ; one is Output and Zero is Input
// After POR : Power on Reset The Port Pins are in High Impedance State
// GPIOA_PIDR= 0xF07FE; // Define PORTA(A0): As FRA_FAIL input pin : LED STATUS Detection pin
// PORT_PTBIE_PTBIE3= 1; // Define PortB(B3): As FRA_TRIG input Pin : one is Input Enabled and Zero is Input Disabled
GPIOA_PDOR = 0x00000; // Initialise the PORTA, PORTB
timer_.cnt = 0; // Initialise all Timer Variables
timer_.active = 0;
timer_.fail_down_cnt = 0;
timer_.fail_up_cnt = 0;
timer_.waitoff = 0;
timer_.Recovery_counter=0;
startTimer();
tmp_timer = MIN_PROG_RUN_TIME; // Min PROGRAM run time is 2.5 mSec
TMR_CNTRL();
tmp_timer += timer_.cnt;
while(timer_.cnt < tmp_timer)
{
TMR_CNTRL();
// timer_.cnt++;
}
for(;;){
GPIOA_PSOR = PRTC0;
GPIOA_PSOR = PRTC1;
}
}
static void TMR_CNTRL(void)
{
unsigned int x =0;
x = FTM0_CNT ; // Initialise FTM0 Counter
FTM0_CNT = FTM0_CNT - x; // Reset Counter
timer_.cnt = timer_.cnt + x; // Increment Counter
if(timer_.active)
{
if((timer_.active + x) < (unsigned int)0xFFFF) // If Active counter is less than 0XFFFF ,increment timer.active
{
timer_.active = timer_.active + x;
}
}
if((timer_.waitoff - x) > 0) // If Waitoff Counter is greater than Zero , Decrement the Waitoff Counter
{
timer_.waitoff = timer_.waitoff - x;
}
}
static void startTimer(void)
{
SIM_SCGC |= SIM_SCGC_FTM0_MASK; // Enable the clock to the FTM0 module;
FTM0_CNT = 0x0000;
FTM0_MOD = 0xFFFF; // The FTM Counter is Free Running Counter by Defining MODH:L=0x0000 or MODH:L=0xFFFF;
FTM0_SC = 0x0F; // CPWMS=0; Counter Mode;System Clock (Default) 20Mhz, FEI mode Default, Prescaler Factor 1:128
}
void REFRESH_WDT(void)
{
// DisableInterrupts; // Refreshing the Watchdog
delay(5); // small Delay before writing refresh word
WDOG_CNT = 0x02A6; // write the 1st refresh word
WDOG_CNT = 0x80B4; //write the 2nd refresh word to refresh counter
}
void delay(unsigned long k)
{
unsigned long delay ;
for (delay=0;delay<k;delay++) ;
}
Thanks and Kind Regards,
Robin
Robin
If the following is called quickly (faster than the FTM increments)
x = FTM0_CNT ; // Initialise FTM0 Counter
FTM0_CNT = FTM0_CNT - x; // Reset Counter
timer_.cnt = timer_.cnt + x; // Increment Counter
x will always be zero and so your variable timer_.cnt will always remain at the same value.
Regards
Mark
Kinetis: µTasker Kinetis support
KE: µTasker FRDM-KE02Z support / µTasker FRDM-KE02Z40M support / µTasker FRDM-KE06Z support
For the complete "out-of-the-box" Kinetis experience and faster time to market
Hello Mark,
I noticed very strange thing , i just changed the Code Optimization from -Os to Code Optimization to None(-O0) and the code is working fine only with delay(25) in while loop as you said. I did setting Code Optimization from None to -Os cause i got really a big code size and after compile i got error for "text_m" bigger in size than allocated memory, SO i read on freescale Forum and changed the Code optimization setting from none to -Os :smileysad: .The above code is just part of that so that their is no Text_m error .
please can you help what's Code Optimization exactly doing and what should be the Ideal setting for Code Optimization :smileysad:?
Thanks
MFG
Robin
" -Os to Code Optimization to None(-O0) and the code is working fine only with delay(25)"
When -O0 works and others don't this is almost always a case of a volatile being missing someplace it is needed.
Also try -O2. I've run into some obscure corner cases where -Os creates code with alignment issues for ARM parts.
Hello Bob,
Thanks for Reply , i tried with every available settings for Optimization but it is working only with -O0 Optimization Only :smileysad: i.e none optimization.
Thanks
MFG
Robin
Hello Mark,
I put delay in my while loop as below
while(timer_.cnt < tmp_timer)
{
delay(10000);
TMR_CNTRL();
// timer_.cnt++;
}
Timer is running at 20Mhz , with Prescaler 128 so increment time should not be more than 1/20Mhz X 128 = 6.4 µSec , delay is also at 20Mhz so 1/20Mhz X 10000 = 500 µsec, I think so in this case counter should increment
but i am getting the same result :smileysad:
Thanks.
MFG
Robin