S08RN TIMER

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

S08RN TIMER

Jump to solution
2,542 Views
Robinwithu
Senior Contributor I

Hi All,

 

please can anybody help me .

 

In the following code i am using S08RN8 Flex Timer and  Just switching ON and OFF the PORTB4 .

 

My problem is when i am flashing the code in  micro controller (S08RN8) and Testing the code on Oscilloscope i am getting the switching Total time ON n OFF 11.34 msec and as soon as I Reset the power supply of controller the Total time I am getting 12.34 msec i.e. around 1msec more,please can anybody tell me what is the reason behind that?

 

   struct _Timer_

    {

      unsigned  int active;                                         // FRA Timer, zur zeitlichen Steuerung des Wischens, 100µs Auflösung

    };

    typedef struct _Timer_ Timer;

 

 

  // prototypes

 

    void                      main(void);

    static void             startTimer(void);

    static void             TMR_CNTRL(void);

    void                      REFRESH_WDT(void);

    void                      delay(unsigned int k);

 

 

 

 

void main(void)

 

    {

   

         DisableInterrupts;

 

       __RESET_WATCHDOG();                                                            // Watchdog Timer is enabled : is having 1 Khz Internal Default clock and Default Values of  appx.4 msec but

                                                                                                                                

        ICS_C1 = 0x04;                                                                         // internal reference clock to FLL and FLL is generating 16000-20000 Khz  ; Ref. Freqeuncy  is set at 31250-39250 Hz * 512 = Appx.16-20 Mhz

        ICS_C2 = 0x00;                                                                        // BDIV = 00, Freq is now 16-20Mhz

     //  ICS_C3 = 0x80;                                                                      //Trimmed Value of Internal System Frequency

 

        PORT_PTBOE_PTBOE4= 1;                                   // PortB4 as Output enable

        PORT_PTBD_PTBD4 = 0;                                        // Initilise PortB4

  

        timer_.active = 0;                                                       // Initilise Active Timer

 

        startTimer();                                                                 // Start Flex Timer

 

        timer_.active=1;                                                      // Start Active timer

        while(1)

 

          {

            TMR_CNTRL();                                                                     // Increment Active Timer

            REFRESH_WDT();                                                            // Refresh Watchdog Timer

 

                 if(timer_.active >= 1850){timer_.active=1;}                                       //Restart Timer                                   

           

            

                else if(timer_.active >=1825 ){PORT_PTBD_PTBD4 = 0;}              //Switch OFF PortB4

                else if(timer_.active >= 1225) {PORT_PTBD_PTBD4 = 1;}                 //Switch ON PortB4

    

 

                else if(timer_.active >= 625){PORT_PTBD_PTBD4 = 0;}                      //  Switch OFF PortB4                                                                            

                else if(timer_.active >= 25)  {PORT_PTBD_PTBD4 = 1;}                      // Switch ON PortB4                               

       

       

        }

        }

 

 

static void startTimer(void)

    {

        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 (Deafult) 16  Mhz, 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   = 0xA602;                                        // write the 1st refresh word

        WDOG_CNT   = 0xB480;                                            //write the 2nd refresh word to refresh counter

 

    //    EnableInterrupts;                                            //Disable interrupt

   

    }

 

 

static void TMR_CNTRL(void)

    {

        unsigned int x = 0;

 

        x = FTM0_CNT ;                                                                    // Initilise FTM0 Counter or  Flex Modulo Timer

                                                   

        FTM0_CNT = FTM0_CNT - x;

 

        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;

            }

        }

 

    }

 

 

void delay(unsigned int k) {

         unsigned int delay ;

         for (delay=0;delay<k;delay++);

       }

Labels (1)
Tags (2)
1 Solution
1,916 Views
arpitaagarwal-b
NXP Employee
NXP Employee

Hello,

I am also not able to locate the address in the document.

This basic controller initialization is done by default in PE.

Generate a code through PE and check _EntryPoint(void) function in Cpu.c file.

Regarding flashing with the button query:

Case 1: Without trimming in code:

When you are using debugger option, debugger itself calculates and trim the MCU. But after POR, that control and memory is lost. So trimming goes to the default value in the trim registers. This might be different from the debugger programmed value and thus you will see the timing difference. (1ms in your case).

Now if you will program through a button, debugger doesn't come into action. Clock is defined through the default values in trim registers, which will not change with POR. Thus you are not observing any timing difference after POR. Though clock might not be of the exact value as expected, ie, clock is not accurately trimmed.

Case 2: With trimming in code:

In this case, clock will be trimmed properly with the factory programmed trim value. This is overwriting every case. Every time your code loads, it is loading the trim registers. Timings will be same as with debugger, after POR, with button flashing and then even after POR. Moreover, timings will be precise as clock is accurately trimmed.

It is always advisable to use trimming in code. This might not affect you in LED flashing code. But if you will use it for any communication, you can see the baud-rate mismatch and you may see some issues.

Hope this answer your question.

Regards,

Arpita

View solution in original post

29 Replies
1,619 Views
arpitaagarwal-b
NXP Employee
NXP Employee

Hello Robin,

This can be the possibility that while flashing the code, debugger is writing the CLK trim values, which is not the case when you reset the system.

In your main function, where you have ICS_C3 line commented, try adding this code:

if (*(uint8_t*)0xFF6FU != 0xFFU) {   /* Test if the device trim value is stored on the specified address */

    ICS_C3 = *(uint8_t*)0xFF6FU;       /* Initialize ICS_C3 register from a non volatile memory */

    ICS_C4 = (uint8_t)((*(uint8_t*)0xFF6EU) & (uint8_t)0x01U); /* Initialize ICS_C4 register from a non volatile memory */

  }

This way, trim values will be same whether debugger is connected or not. Try this and see if you observe constant time?

Thanks and Regards

Arpita Agarwal

1,619 Views
Robinwithu
Senior Contributor I

Hello Arpita,

The Timing is looking fine now but please can you tell me from where you got the address of this Trim Registers (FF6E & FF6F) for 9S08RN8 µC

The code i have modified cause i am not using PE (Processor Expert)

first i defined this variables

volatile byte NV_FTRIM_INIT  @0x0000FF6E  ;        // LSB     ICS_C4

  volatile byte NV_ICSTRM_INIT @0x0000FF6F  ;     // MSB     ICS_C3      

and in main loop in place of ICS_C3 Register , having following code

if ( NV_ICSTRM_INIT != 0xFFU) {                                                       // 0xFF6FU  Test if the device trim value is stored on the specified address

//  ICS_C3 = *(uint8_t*)0xFF6FU;                                // Initialize ICS_C3 register from a non volatile memory
//  ICS_C4 = (uint8_t)((*(uint8_t*)0xFF6EU) & (uint8_t)0x01U);  // Initialize ICS_C4 register from a non volatile memory
ICS_C3= NV_ICSTRM_INIT;
ICS_C4= ((NV_FTRIM_INIT) & 0x01U);                     // Trim the internal clock
}

please can you tell me is that code correct?

is that writing the Default values of ICS_C3 and ICS_C4 ??

Thank you and Kind Regards,

Robin

0 Kudos
1,619 Views
arpitaagarwal-b
NXP Employee
NXP Employee

Hello Robin,

I got these addresses from the PE initialization code for S08RN8.

Generally, this address is also mentioned in device RM/datasheet.

This way we are writing the factory programmed trim values from the memory to the trimming registers.

Your code also seems fine. But why you want to do it this way and not the way PE generates the code?

-Arpita

1,619 Views
Robinwithu
Senior Contributor I

Hello Arpita,

I am using the Free version of Code warrior 10.5  and may be it's not supporting that function (PE Initialization)and also the  problem is in  derivative.h  file of micro controller MC9S08RN8  this " Environment variables"  are not defined so the compiler is not able to find out Address "  *(uint8_t*)0xFF6EU & *(uint8_t*)0xFF6FU "  and and it's giving me Error which is logical so that i defined it in my code  , may be i am wrong but i didn't find this address and NVM variable for trim and fine trim register in derivative.h file.

I Read the complete Data sheet for MC9S08RN16/8/4/2 but it  doesn't mentioned anywhere in the Datasheet the " Reserved Address for NVM Trim and Fine Trim Value " . please can you look the datasheet  and tell me where it's mentioned?

Can you also send me some link  how to initialize the controller in PE? or does it initialize by default in PE ?

One more Question  :-

I used the following method to program µC        Flashing with a Button (and a Magic Wand) | MCU on Eclipse  with and Without Trimming code i am getting the same Delay of 12 m sec , their is no change in timing even after Power on Reset.

But if i am Using Debugger Method to program µC &  i am getting 11 m sec with Trimming code before and After Power on Reset and 12 m sec Without Trimming code  before &  after Power on Reset.

please could you tell me the reason??

Thanks and Kind Regards,

Robin

0 Kudos
1,917 Views
arpitaagarwal-b
NXP Employee
NXP Employee

Hello,

I am also not able to locate the address in the document.

This basic controller initialization is done by default in PE.

Generate a code through PE and check _EntryPoint(void) function in Cpu.c file.

Regarding flashing with the button query:

Case 1: Without trimming in code:

When you are using debugger option, debugger itself calculates and trim the MCU. But after POR, that control and memory is lost. So trimming goes to the default value in the trim registers. This might be different from the debugger programmed value and thus you will see the timing difference. (1ms in your case).

Now if you will program through a button, debugger doesn't come into action. Clock is defined through the default values in trim registers, which will not change with POR. Thus you are not observing any timing difference after POR. Though clock might not be of the exact value as expected, ie, clock is not accurately trimmed.

Case 2: With trimming in code:

In this case, clock will be trimmed properly with the factory programmed trim value. This is overwriting every case. Every time your code loads, it is loading the trim registers. Timings will be same as with debugger, after POR, with button flashing and then even after POR. Moreover, timings will be precise as clock is accurately trimmed.

It is always advisable to use trimming in code. This might not affect you in LED flashing code. But if you will use it for any communication, you can see the baud-rate mismatch and you may see some issues.

Hope this answer your question.

Regards,

Arpita

1,619 Views
Robinwithu
Senior Contributor I

Hi Arpita,

I am having  question Regarding FLASH programming:-

Now i am using the above Trimming code in my main program and programming the MCU with Debugger , i am getting the same timing before and After POR (133msec) .but if i am using Button programming/ flash programming please refer the  link (Flashing with a Button (and a Magic Wand) | MCU on Eclipse) than the Timing is different from Debugger timing but it's same before and After POR (144 msec).

With Debugger programming the internal reference frequency is 39062.5 Hz  so the BUS Freq. 512 X 39062.5 = ca. 20Mhz and I found that with Button or Flash programming it's Internal reference frequency is 31250 so the Bus Freq. is 31250 X 512= 16Mhz .it means that adding the trimming  code in main programming which u send me doesn't have any effect on Button programing / Flash Programing .

Can you tell me in button programming/ Flash programing  from where I can change the internal Reference frequency and why it's not taking the default internal reference frequency (39062.5) even after adding the trimming code in main program?

I tried to change the Internal frequency from Run Config and also from Debug config but Flash programing doesn't have any effect.

Thanks and Kind Regards,

Robin

0 Kudos
1,619 Views
arpitaagarwal-b
NXP Employee
NXP Employee

Hello Robin,

There should not be any difference while programming with debugger or using Flash programmer, if you have copied trim values.

I don't have any clue why this is the case.

Just for clarification, please confirm below queries, may be we will get some hint for your observation:

1. The time you are observing is the output of FTM?

2. Check by removing the trim code and provide the following time values:

     - when the code is flashed through debugger

     - after POR when the code is flashed through debugger

     - when code is flashed through flash programmer

     - after POR when code is flashed through flash programmer.

As you mentioned, with the trim code, first two timings are 133ms and rest two are 144 ms.

-Arpita

1,619 Views
Robinwithu
Senior Contributor I

Hello arpitaagarwal-b37570 ,

have you found why flash programming is trimming an Internal reference frequency to 31250hz?

Thank you n kind Regards,

Robin

0 Kudos
1,619 Views
arpitaagarwal-b
NXP Employee
NXP Employee

Hello Robin,

I am still not able to find the reason for the same.

As per my understanding, it should not be the case.

I will let you know if I will find the cause.

-Arpita

0 Kudos
1,619 Views
Robinwithu
Senior Contributor I

Hello Arpita,

Please have a look for this discussion Re: Trim value

may be the Flash programmer is taking it's value from 9S08rn8.s8p file .

09BIT_TRIM=303A/303B/01/FF6F/FF6E/001312D0/

Here is the path for this PE file C:\Program Files (x86)\Code Worrior\CW for MCU 10.5\CW MCU v10.5\MCU\bin\plugins\support\HC08\gdi\P&E

please have a look and would be grateful, if you help me further on this topic.

Thanks and Kind Regards,

Robin

0 Kudos
1,619 Views
arpitaagarwal-b
NXP Employee
NXP Employee

Hello Robin,

Since I don't have S08RN board with me, kindly do the following steps and share your observation:

1. Flash the MCU using debugger with trim code.

2. In debugger mode, check the value at these memory locations: 0xFF6EU, 0xFF6FU (location where trim value is stored).

3. Now, modify the trim code to use the direct values which you have just find instead of reading from memory everytime.

4. Flash it using debugger, and observe the frequency.

5. Do POR and observe the frequency.

6. Now use flash programming option (where you are facing an issue) and observe the frequency.

I think it should give you the same frequency and solve the issue.

Let me know your observations.

-Arpita

1,619 Views
Robinwithu
Senior Contributor I

Hello Arpita,

I would also like to bring one more thing in your focus as you said in your previous solution that we can remove the trim code just write the Trim value after reading it from 0XFF6E n 0XFF6F , like in my previous comment it was 0x46 n 0x00, but this value is not same for all the µC.Now i am having another MCU and it's value is 0X57 n 0X00.

So the problem is still not solved . I hope you can understand above explanation, if not than please write me i will elaborate it.

Thanks and Kind Regards,

Robin

0 Kudos
1,619 Views
arpitaagarwal-b
NXP Employee
NXP Employee

Regarding changed value in different MCU, that is why I suggested to write the value directly from the memory in my last to last post.

This way, different trim values for different MCUs will be taken care of!

Infact we have this NV memory for this purpose only.

I am still not sure why with trim code with uncheck "Calculate Trim and Program the Non-volatile Trim Register" box, giving you the same problem. I expected it to work fine. Will let you know as soon as I will be able to find the reason behind it.

-Arpita

0 Kudos
1,619 Views
Robinwithu
Senior Contributor I

Hello arpitaagarwal-b37570

I am having one Question Regarding FEI Mode . (MCU (MC9S08RN8) startup timing with 20Mhz Bus freq.)

After selecting FEI mode must i wait for some time till that internal Oscillator get stable or i can initilise the Port and do the function whatever i want.

Thanks and kind Regards,

Robin

0 Kudos
1,619 Views
arpitaagarwal-b
NXP Employee
NXP Employee

Hello Robin,

Regarding FEI mode, see the reply in:

FEI Mode start time

-Arpita

0 Kudos
1,619 Views
Robinwithu
Senior Contributor I

Hi Arpita,

Actually at the development level you can read and write every MCU NVM Trim value but it's not possible in serial production and i need this for my serial production.

please take your time and help me on this Issue.

Thanks n Kind Regards,

Robin

0 Kudos
1,619 Views
Robinwithu
Senior Contributor I

Hello Arpita,

1) I flashed MCU using Debugger

2) Read the memory location 0XFF6EU n 0XFF6FU

3) Got the following values and modified  Trim code values

volatile byte NV_FTRIM_INIT  @0x0000FF6E=0x0000;

volatile byte NV_FTRIM_INIT  @0x0000FF6F=0x0046;

4) Flashed with Debugger and Frequency is 20Mhz

5) After POR Freq. is 20Mhz

6) Used Flash programming Freq. is 20 Mhz :smileyhappy:

The problem is solved but the root Question still remains, and in case if i don't want to change the trim value and want to use the default value den will face the same problem.

Thanks and Kind Regards,

Robin

0 Kudos
1,619 Views
arpitaagarwal-b
NXP Employee
NXP Employee

Good to know that problem is solved :smileyhappy:

If you want to use the default clock, then remove the trim code completely.

Right now, just with debugger you will see the different value. After POR with debugger and using flash programmer will give you the same result.

If you want the same value with debugger, then I will need to search for an option..

Note: In case the answer helps, kindly mark it as correct or helpful.

-Arpita

1,619 Views
Robinwithu
Senior Contributor I

Hi Arpita,

The root problem is still their :smileysad: (Means problem is not solved 100% :smileysad:).

I wanted to use the Default clock but the problem is in future if i forget that debugger is trimming some another value and i do the calculation with Default value than again the same story will start so it's better for me to include the trim code .

It would be grateful if you will find out the option using debugger i will get the same internal default frequency before and after Power on Reset .

Thanks and Kind Regards,

Robin

0 Kudos
1,619 Views
arpitaagarwal-b
NXP Employee
NXP Employee

Hello Robin,

To disable debugger from writing values at the trim location, go to Debug Configurations--> Select the configuration which you are using --> Click on Edit for connections in "Target Settings" heading --> Click Advanced programming options tab -> Uncheck "Calculate Trim and Program the Non-volatile Trim Register" box --> click on "Done" , "Ok" and "Apply" tab in the respective windows.

Now check the code.. It should give the same values in all the cases.

-Arpita