• AVR Freaks

Hot!How do I configure and reset the oscillator with FSCM?

Author
Trunksx64
Engineer
  • Total Posts : 20
  • Reward points : 0
  • Joined: 2013/02/15 08:45:48
  • Location: Colombia
  • Status: offline
2019/06/26 16:07:57 (permalink)
0

How do I configure and reset the oscillator with FSCM?

Good Days or Nights to all.

I have a question about the FSCM of the PIC24HJ128G504 Microcontroller, how do I configure the Trap correctly to return to the source of the previously configured oscillator?

I use a 4Mhz Oscillator in conjunction with the PLL to get the 80Mhz to work at 40Mips like that;
    /* < Configure ::: Osc with PLL to 40Mips... > */
    CLKDIVbits.PLLPRE = 0; // < Set, N1 = 2
    CLKDIVbits.PLLPOST = 0; // < Set, N2 = 2
    PLLFBD = 78; // < Set, M = 80
    /* < Swicth ::: Chague Osc Control... > */
    __builtin_write_OSCCONH(0x03);
    __builtin_write_OSCCONL(0x01);
    /* < Waiting :::: Stable PLL... > */
    while (OSCCONbits.COSC != 0x03);
    while (OSCCONbits.LOCK == 0);

Now I configure the Fuses;
// FOSC
#pragma config POSCMD = XT // Primary Oscillator Source (XT Oscillator Mode)
#pragma config OSCIOFNC = OFF // OSC2 Pin Function (OSC2 pin has clock out function)
#pragma config IOL1WAY = OFF // Peripheral Pin Select Configuration (Allow Multiple Re-configurations)
#pragma config FCKSM = CSECME // Clock Switching and Monitor (Both Clock Switching and Fail-Safe Clock Monitor are enabled)

For the Trap, I declare the function like this;
/* < Function Trap ::: _OscillatorFail > */
void __attribute__((interrupt, no_auto_psv)) _OscillatorFail(void) {
    /* < Change ::: Clock Source > */
    __builtin_write_OSCCONH(0x03);
    __builtin_write_OSCCONL(0x01);
    /* < Waiting :::: Changue Clock... > */
    while (OSCCONbits.COSC != 0x03);
    while (OSCCONbits.LOCK == 0);
    /* < Borra la Bandera del Trap > */
    INTCON1bits.OSCFAIL = pdClear;
}

Now using the ICD3, I try in debug to trap it effectively, but the problem appears because the FCR Oscillator remains, it does not change to the 4Mhz Crystal to Generate the 80Mhz, this does some good times and others badly. To verify this, I generate a 1ms signal which I test with an LED and oscilloscope.

Reading the documentation of the Microcontroller, it says that we must wait for the change of the oscillator to be stable, but I do not know how to measure or wait for that time, I have tried to do it waiting for 1024 cycles as in the 8bit family, disabling the interruption by 10 or more cycles, forcing the FCR oscillator to activate and then activating the oscillator by crystal, but sometimes it does not work. I'm sure I'm misinterpreted the documentation, because I think that at some point the oscillator fails within the Trap, but I do not know how to verify it.
 
The idea that I must implement is that in case of failure of the main oscillator by noise or external agents, the microcontroller is capable of resetting at 40Mips without serious problems, I ask for your help to know which sequence I must actually execute to reactivate the main oscillator.
 
In advance I appreciate the help, I use XC16 with MplabX in its latest versions.
#1

9 Replies Related Threads

    dan1138
    Super Member
    • Total Posts : 3166
    • Reward points : 0
    • Joined: 2007/02/21 23:04:16
    • Location: 0
    • Status: offline
    Re: How do I configure and reset the oscillator with FSCM? 2019/06/26 16:35:03 (permalink)
    0
    From your description it is not clear what your real problem may be.
     
    I do know that to change any PLL settings the PIC system clock must come from a non-PLL clock source.
     
    Perhaps you need to change the system clock to the FRC (0b000) clock and verify that the Fail Safe Clock Monitor has cleared the clock fault assertion then reconfigure the PLL clock source.
    #2
    Trunksx64
    Engineer
    • Total Posts : 20
    • Reward points : 0
    • Joined: 2013/02/15 08:45:48
    • Location: Colombia
    • Status: offline
    Re: How do I configure and reset the oscillator with FSCM? 2019/06/28 05:36:16 (permalink)
    0
    Good morning and thanks for answering.

    Effectively what I described I have done so, at the time the Trap is generated, I assure myself and change to the source of the FRC, after this I again make the change to the Crystal with the PLL, but it follows the same behavior of random failure.

    Now that I think about it, I'm going to execute the next sequence if it works correctly; At the moment that the Trap is generated, I check that the fault is indicated by the Flag, I set the oscillator by the 4Mhz Crystal and delay it to stabilize, again change the oscillator but this time with the PLL and delay to stabilize the PLL hook correctly.

    When I do the tests, I will comment on the results.
    #3
    Aussie Susan
    Super Member
    • Total Posts : 3608
    • Reward points : 0
    • Joined: 2008/08/18 22:20:40
    • Location: Melbourne, Australia
    • Status: offline
    Re: How do I configure and reset the oscillator with FSCM? 2019/06/30 19:31:39 (permalink)
    0
    The trap is called when the oscillator fails. But you appear to be trying to reset the oscillator to the same setting that has (apparently) failed!
    What attempt are you making to ensure that the wanted oscillator is back working again?
    Also, you could not trap the event (this will fall back to a reset) but look at the INTCON1 register at start up and see if the reset was caused by an oscillator failure, and not re-perform all of the initialisation code; just try to reset the oscillator to the one you want.
    Susan
    #4
    Trunksx64
    Engineer
    • Total Posts : 20
    • Reward points : 0
    • Joined: 2013/02/15 08:45:48
    • Location: Colombia
    • Status: offline
    Re: How do I configure and reset the oscillator with FSCM? 2019/07/03 08:21:42 (permalink)
    0
    Hello Susan, indeed, I'm trying to restart the oscillator that failed, which in this case would be XT with PLL, what I understand that you write to me is that I should not execute the entire oscillator change process, in this case I should only execute the change of oscillator, should not I expect the PLL to stabilize?
    #5
    Aussie Susan
    Super Member
    • Total Posts : 3608
    • Reward points : 0
    • Joined: 2008/08/18 22:20:40
    • Location: Melbourne, Australia
    • Status: offline
    Re: How do I configure and reset the oscillator with FSCM? 2019/07/03 19:52:38 (permalink)
    0
    You have understood partly correctly.
    My point is that, if the Oscillator Failure trap has occurred, then does it make sense to simply reset the oscillator configuration to use it again? Should there not be some attempt to record that this (very serious) issue has occurred and possibly try to fix it?
    The other point I was making is that you already have code at the start of your app to set the oscillator configuration. If you DON'T trap the Oscillator Failure event, then the default behaviour is for the MCU to reset and restart your app again. However this time it will have a bit set in the INTCON1 register to show that an Oscillator Failure has occurred - in that case you may not need to reinitialise everything else (or at least can write your app in such a way that a restart can be tolerated). All you need to do is execute the oscillator configuration part of your initialisation code - but yes, you should wait for the PLL to stabilise unless you can tolerate the master clock varying in frequency for a while.
    All that aside - if the oscillator fails then that is something I would certainly treat as a major problem and not just try to carry on without some level of diagnostic testing or investigation.
    Susan
    #6
    Trunksx64
    Engineer
    • Total Posts : 20
    • Reward points : 0
    • Joined: 2013/02/15 08:45:48
    • Location: Colombia
    • Status: offline
    Re: How do I configure and reset the oscillator with FSCM? 2019/07/04 09:41:07 (permalink)
    0
    Hello Susan ...
     
    I have made your suggestions and especially your final idea to consider as a serious error the failure of the oscillator, say that by pressure of my boss was really focusing badly as must be take the error of the oscillator, I commented the following and as effectively already solve the case thanks to your idea.
     
    I am using the PIC24HJ128Gp504 to make communication by CAN, for which I must have the oscillator stable and for more be able that my firmware is able to cope with this error and be able to solve it automatically, the control works in a noisy environment because it must handle high horsepower engines and in the tests he was doing every time the engine braked, a large amount of inductance is generated that caused the control to fail by the oscillator, here is where he wrongly tried to restart the oscillator to solve the error, then to think about it a lot, it is due to a bad ground in the control and even though the firmware can restart the oscillator, there will be moments that are impossible due to the generated noise.

    For this particular case, let the Trap only notify me of the Oscillator error, then using the FRC to make the change again within another routine, which would tell me the number of interactions that the oscillator failed in a certain time, and depending of the time recorded, block the control and warn about the error;
     
    /* < Function Trap ::: _OscillatorFail > */
    void __attribute__((interrupt, no_auto_psv)) _OscillatorFail(void) {
        /* < Variables ::: Locales > */
        BaseType_t xTaskWokenOscillatorTrap = pdFALSE;
        /* < Break ::: for DEBUG > */
        pdNop();
        /* < Set ::: Flag Event... > */
        xEventGroupSetBitsFromISR(xEventGroupSystemErrorsOne, Flag_Error_One_Ctrl_Oscillator_Trap, &xTaskWokenOscillatorTrap);
        /* < Control de Contexto Rtos > */
        if (xTaskWokenOscillatorTrap) taskYIELD();
        /* < Borra la Bandera del Trap > */
        INTCON1bits.OSCFAIL = pdClear;
    }

     
    void vApplicationIdleHook(void) {
        /* < Break ::: DEBUG > */
        pdNop();
        /* < Lectura ::: Banderas :: Control > */
        Hook_FlagError_Oscillator = xEventGroupWaitBits(xEventGroupSystemErrorsOne, Flag_Error_One_Ctrl_Oscillator_Trap, Flag_HoldOnExit, Flag_NoWaitForAllBits, pdNull);
        /* < Oscillator Failured ::: Changue to Oscillator with PLL... > */
        if ((Hook_FlagError_Oscillator & Flag_Error_One_Ctrl_Oscillator_Trap) == Flag_Error_One_Ctrl_Oscillator_Trap) {
            /* < Suspend All Tasks... > */
            vTaskSuspendAll();
            /* < Enter ::: Critical Code Region > */
            taskENTER_CRITICAL();
            /* < Change ::: Clock Source > */
            __builtin_write_OSCCONH(0x03);
            __builtin_write_OSCCONL(0x01);
              /* < Exit ::: Critical Code Region > */
            taskEXIT_CRITICAL();
            /* < Clear ::: Flag Event... > */
            xEventGroupClearBits(xEventGroupSystemErrorsOne, Flag_Error_One_Ctrl_Oscillator_Trap);
            /* < Resume All Tasks... > */
            xTaskResumeAll();
        }
        /* < Checking ::: Is Oscillator Working... > */
    }

     
    The code is still a bit short, but I'm still adjusting it to improve control before the oscillator fails, and each time it fails, the control is reset in a good way.
     
    Thank you Susan, you made me understand the error.
    post edited by Trunksx64 - 2019/07/04 09:54:31
    #7
    Aussie Susan
    Super Member
    • Total Posts : 3608
    • Reward points : 0
    • Joined: 2008/08/18 22:20:40
    • Location: Melbourne, Australia
    • Status: offline
    Re: How do I configure and reset the oscillator with FSCM? 2019/07/04 20:06:17 (permalink)
    4 (1)
    I see from that you are using freeRTOS (or similar). I doubt it would have made much difference to any responses, but it would have been nice to know.
    Given the situation you have described, I'd say you were fixing the symptoms and not the problem.
    I'd be working on shielding the MCU (and the oscillator), putting chokes into the supply lines and ensuring I had adequate damping of the supply lines etc..
    Microchip make MCUs that are designed to work in automotive environments so it is certainly possible to have your circuit working where there is a lot of (electrical) noise.
    Susan
    #8
    Trunksx64
    Engineer
    • Total Posts : 20
    • Reward points : 0
    • Joined: 2013/02/15 08:45:48
    • Location: Colombia
    • Status: offline
    Re: How do I configure and reset the oscillator with FSCM? 2019/07/05 08:04:49 (permalink)
    0
    Hello Susan, in your answer, you are right, it is not totally solved, since there is a problem of electrical noise that I also consider serious, but it is not my total control, since the design of the PCB demanded that the corresponding protection be made to the oscillator and of course to the main feed and as you can imagine my suggestions were not listened to, because those responsible believe that the software can solve it completely.

    Obviously the idea is that the oscillator remains stable, and in the code that I wrote in the previous post, I change it, but I do not wait for the oscillator to really set and the PLL latch on, because if I create some verification for OSWEN and LOCK, the change remains in a loop and again the microcontroller is blocked, to this I understand that the PLL never hooks, how should I make this revision effectively so that it works ?, since if I do not do it, the oscillator if it is reset correctly, I suppose he spends it rebooting the oscillator until at a point it works.
    #9
    ric
    Super Member
    • Total Posts : 23200
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: How do I configure and reset the oscillator with FSCM? 2019/07/05 16:09:14 (permalink)
    0
    Software cannot totally cure faulty hardware.
    Just make your software wait for a stable clock. If it locks up altogether, then it's the hardware guys who have to fix it.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #10
    Jump to:
    © 2019 APG vNext Commercial Version 4.5