• AVR Freaks

Using "pragma config" for configuration bits?

Author
arigead
Super Member
  • Total Posts : 445
  • Reward points : 0
  • Joined: 2011/02/07 06:58:31
  • Location: 0
  • Status: offline
2016/10/28 03:15:15 (permalink)
0

Using "pragma config" for configuration bits?

cleaning up lose ends and still trying to find documentation on using "pragma config" currently I'm using things like:
 
_FOSCSEL( FNOSC_FRC & IESO_OFF )
 
for which I get a deprecated warning. That's fine but when I go to the header file (using dsPIC33EP256MU806) I get the deprecated warning along with the documentation for the way I'm doing it. I've been trying to guess the pragma config settings for the device but so far I've not hit upon the correct answer. I'd be no good at code breaking. I'll include the documentation from the header file, which basically says "Here's how to do it but don't do it this way!". Why would somebody add the deprecated warning and then leave in all the old instructions without updating those?
 
/* Register FOSCSEL (0xf80006)                               */

extern __attribute__((space(prog))) __prog__ unsigned int _FOSCSEL;
#define _FOSCSEL(x) __attribute__((deprecated_definition("consider migrating to #pragma config"),section("__FOSCSEL.sec"),space(prog))) __prog__ unsigned int _FOSCSEL = (x);

/*
** Only one invocation of FOSCSEL should appear in a project,
** at the top of a C source file (outside of any function).
**
** The following constants can be used to set FOSCSEL.
** Multiple options may be combined, as shown:
**
** _FOSCSEL( OPT1_ON & OPT2_OFF & OPT3_PLL )
**
**   Initial Oscillator Source Selection bits:
**     FNOSC_FRC            Internal Fast RC (FRC)
**     FNOSC_FRCPLL         Internal Fast RC with PLL (FRCPLL)
**     FNOSC_PRI            Primary Oscillator (XT, HS, EC)
**     FNOSC_PRIPLL         Primary Oscillator (XT, HS, EC) with PLL
**     FNOSC_SOSC           Secondary Oscillator (SOSC)
**     FNOSC_LPRC           Low-Power RC Oscillator (LPRC)
**     FNOSC_FRCDIV16       Internal Fast RC (FRC) Oscillator with divide-by-16
**     FNOSC_FRCDIVN        Internal Fast RC (FRC) Oscillator with postscaler
**
**   Two-speed Oscillator Start-up Enable bit:
**     IESO_OFF             Start up with user-selected oscillator source
**     IESO_ON              Start up device with FRC, then switch to user-selected oscillator source
**
*/


#1

15 Replies Related Threads

    Vinetou
    Starting Member
    • Total Posts : 50
    • Reward points : 0
    • Joined: 2015/09/15 11:41:11
    • Location: Brazil
    • Status: offline
    Re: Using "pragma config" for configuration bits? 2016/10/28 03:31:28 (permalink)
    +1 (1)
    If you're using XC16 compiler you should use:
     
    #include    "xc.h"
     
    Here's the config code I use for the dsPIC33EV256GM102
     
    #include    "xc.h"
    #include    "libpic30.h"
    #pragma config FNOSC = FRCPLL
    #pragma config POSCMD = NONE
    #pragma config OSCIOFNC = OFF
    #pragma config FWDTEN = OFF
    #pragma config BOREN0 = OFF

    etc. etc.


    #2
    qhb
    Superb Member
    • Total Posts : 9999
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: Using "pragma config" for configuration bits? 2016/10/28 05:35:56 (permalink)
    +4 (4)
    and if you are using MPLABX, it has a dedicated Window for doing all the configuration bit setting, then generating the source code to paste back into your C file.
     
    #3
    Mysil
    Super Member
    • Total Posts : 3809
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re: Using "pragma config" for configuration bits? 2016/10/28 07:02:53 (permalink)
    +1 (1)
    Hi,
    Description of keywords and parameters for the #pragma config specifications
    is in the documentation directory installed with the compiler:
    .../program files/microchip/xc16/version/docs/config_docs/33EP256MC806.html
    there is a index file in the directory above.
     
    Regards,
       Mysil
    #4
    Vinetou
    Starting Member
    • Total Posts : 50
    • Reward points : 0
    • Joined: 2015/09/15 11:41:11
    • Location: Brazil
    • Status: offline
    Re: Using "pragma config" for configuration bits? 2016/10/28 11:53:11 (permalink)
    0
    For more information on this subject, please refer to Microchip XC16 C Compile User's Guide, sections 6.3 and 6.5.
    You can download it from the link below:
     
    http://www.microchip.com/...aspx?ddocname=en559023
    #5
    RISC
    Super Member
    • Total Posts : 5778
    • Reward points : 0
    • Status: offline
    Re: Using "pragma config" for configuration bits? 2016/10/28 13:37:22 (permalink)
    +2 (2)
    Hi,
    The configuration bits for each processor may also be in :
    .../program files (x86)/microchip/xc16/version/docs/config_docs/33EP256MC806.html
    if you have a 64 bits processor
    Regards
    #6
    arigead
    Super Member
    • Total Posts : 445
    • Reward points : 0
    • Joined: 2011/02/07 06:58:31
    • Location: 0
    • Status: offline
    Re: Using "pragma config" for configuration bits? 2016/11/01 12:47:47 (permalink)
    0
    Thanks for all your help on this topic. I've tried to use MPLAB-X which seems confused by the configuration bits. I'm powering up with the Internal RC clock source:
    #pragma config FNOSC = FRC
    But then I want to switch to the Primary Oscillator with PLL. in MPLAB-X if I set POSCMD to HS (I've a 16MHz crystal) then there's an error on OSCIOFNC which can either be "Off" (OSC2 is clock output) or "On" (OSC2 is an general purpose IO) Both of those are error conditions.
    So I've been unable to set configuration bits in MPLAB-X an have it be happy with selections. If I select a clock there's an error.
    #7
    qhb
    Superb Member
    • Total Posts : 9999
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: Using "pragma config" for configuration bits? 2016/11/01 13:54:51 (permalink)
    +2 (2)
    How exactly are you trying to "set POSCMD" ?
    It saves confusion all round if you show the actual code you wrote, and the actual error message you received.
     
    #8
    arigead
    Super Member
    • Total Posts : 445
    • Reward points : 0
    • Joined: 2011/02/07 06:58:31
    • Location: 0
    • Status: offline
    Re: Using "pragma config" for configuration bits? 2016/11/10 04:47:12 (permalink)
    0
    Sorry I was setting configuration bits in MPLAB-X and when I say that there was an error setting a clock source set other options to red so I assumed, possibly wrongly, that the red colour indicated an error. Maybe red don't mean error.
     
    But it might be considered an issue that the OSCIOFNC bit can be set to zero or one, indicating that pin OSC2 is either IO or Clock Output. How would that bit be set for using a High Speed oscillator and pin OSC2 is Neither IO or Clock Output.
     
     
    #9
    qhb
    Superb Member
    • Total Posts : 9999
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: Using "pragma config" for configuration bits? 2016/11/10 04:52:17 (permalink)
    +3 (3)
    Please show the actual code as you have it in your source, don't just describe what you think you did.
    #10
    Nikolay_Po
    Super Member
    • Total Posts : 1941
    • Reward points : 0
    • Joined: 2012/04/01 13:49:27
    • Location: Russia, Novorossiysk
    • Status: offline
    Re: Using "pragma config" for configuration bits? 2019/01/07 10:54:46 (permalink)
    0
    Let I resurrect the topic a bit. At a glance the OP's problem is not clear. Now I have almost the same problem as the OP. I can't setup the configuration I need because of disability of XC16 v1.36 chip support packs.
    -=-
    I'm planning to start dsPIC30F6014A on FRC oscillator with PLL disabled. Then I need change the clock to XT_PLL16 (crystal with x16 PLL). It is possible to switch from FRC to XT+PLL in firmware. None of both, Datasheet and FRM prohibiting me to do this. But PLL is controlled only by configuration bits, so I can't change setting in run time and had to define correct value for FOSC word.
    -=-
    The problem is my inability to setup for XC16 the configuration needed nor by MPLABX GUI nor by manual in "C" code, including deprecated way. All I need is to configure the FOSC for 0x79E7 (corresponding to assembler include file). The left octet are FRC and CSW_ON_FSCM_OFF masks, the right - XT_PLL16 mask. Together they are starting the PIC at FRC and making possible the switching to XT_PLL16 by writing 0b111 into NOSC btis of OSCCON register (with subsequent OSWEN bit setup). Note, the FRM table (see attached picture), allows operation from FRC irrespectively any of FPR bits and the configuration with FRC on left and XT_PLL on right of table is perfectly valid.
    The variants in Target memory views/Configuration Memory of MPLABX IDE are strictly the same as in compiler document from /xc16/v1.36/docs/config_docs/ folder, here are FOSC settings:
    FOSFPR Oscillator
    FOSFPR = LPOSC Low-Power 32KHz Osc (No change to Primary Osc Mode bits)
    FOSFPR = FRC Internal Fast RC (No change to Primary Osc Mode bits)
    FOSFPR = LPRC Internal Low-Power RC (No change to Primary Osc Mode bits)
    FOSFPR = XTL XTL
    FOSFPR = HS HS
    FOSFPR = XT XT
    FOSFPR = ERCIO ERCIO
    FOSFPR = ERC ERC
    FOSFPR = EC EC
    FOSFPR = ECIO ECIO
    FOSFPR = FRC_PLL4 FRC w/PLL 4x
    FOSFPR = FRC_PLL16 FRC w/PLL 16x
    FOSFPR = XT_PLL4 XT w/PLL 4x
    FOSFPR = XT_PLL8 XT w/PLL 8x
    FOSFPR = XT_PLL16 XT w/PLL 16x
    FOSFPR = FRC_PLL8 FRC w/PLL 8x
    FOSFPR = ECIO_PLL4 ECIO w/PLL 4x
    FOSFPR = ECIO_PLL8 ECIO w/PLL 8x
    FOSFPR = ECIO_PLL16 ECIO w/PLL 16x
    FOSFPR = HS2_PLL4 HS2 w/PLL 4x
    FOSFPR = HS2_PLL8 HS2 w/PLL 8x
    FOSFPR = HS2_PLL16 HS2 w/PLL 16x
    FOSFPR = HS3_PLL4 HS3 w/PLL 4x
    FOSFPR = HS3_PLL8 HS3 w/PLL 8x
    FOSFPR = HS3_PLL16 HS3 w/PLL 16x
    FCKSMEN Clock Switching and Monitor
    FCKSMEN = CSW_FSCM_ON Sw Enabled, Mon Enabled
    FCKSMEN = CSW_ON_FSCM_OFF Sw Enabled, Mon Disabled
    FCKSMEN = CSW_FSCM_OFF Sw Disabled, Mon Disabled

    None of there variants in single satisfies my needs. The combination of settings is required.
    -=-
    I've checked compiler manual. It saying:

    Configuration Settings may be made using the preprocessor directive #pragma config and settings macros specified under the docs subdirectory of the compiler install directory. The directive format options are:
    #pragma config setting = state|value
    #pragma config register = value
    where setting is a configuration setting descriptor (e.g., WDT), state is a descriptive value (e.g., ON) and value is a numerical value. The register token may represent a whole configuration word register, e.g., CONFIG1L.

    But as I've show above, none of the values appropriate in single. I've tried second variant, setup the value for whole register. But the compiler claims it doesn't know such name:
    #pragma config FOSC = 0x79E7

    "config.c:8:9: error: unknown configuration setting: 'FOSC'". Note, FOSC is a datasheet name of this configuration word.
    As an alternative to single pre-defined values, I've tried to combine them. But with no success:
    If I'm trying
    #pragma config FOSFPR = FRC & XT_PLL16

    The result is "config.c:9:9: error: ',' or end of line expected in configuration pragma". If I try to close the values in a brackets:
    #pragma config FOSFPR = (FRC & XT_PLL16)

    the compiler segfaulting: "config.c:9:9: internal compiler error: Segmentation fault"
    -=-
    I've red all the documentation that assume related to configuration words. But have none examples how to combine the settings like it was done in previous compiler versions, for example,
    _FOSC( FRC & XT_PLL16 & CSW_ON_FSCM_OFF)
     

    is yelling: "config.c:48:0: error: '_FOSC' definition has been deprecated: consider migrating to #pragma config". Of course, I can't turn off a feature "make warning into an errors", but it doesn't work another way. The resulting value in configuration is invalid, 0x4100 instead of 0x79E7 (checked both in memory view window and in hex file - the same).
    -=-
    The question is short: how to setup FOSC=0x79E7 in XC16 source code?
     -=-
    Just for a reference, here is assembler definition of FOSC:
    ;----- FOSC (0xf80000) --------------------------------------------------
    ;
    ;  The following settings are available for FOSC:
    ;
    ;   Oscillator:
    ;     LPOSC                Low-Power 32KHz Osc (No change to Primary Osc Mode bits)
    ;     LP                   Low-Power 32KHz Osc (No change to Primary Osc Mode bits)
    ;     FRC                  Internal Fast RC (No change to Primary Osc Mode bits)
    ;     LPRC                 Internal Low-Power RC (No change to Primary Osc Mode bits)
    ;     XTL                  XTL
    ;     HS                   HS
    ;     XT                   XT
    ;     ERCIO                ERCIO
    ;     ERC                  ERC
    ;     EC                   EC
    ;     ECIO                 ECIO
    ;     FRC_PLL4             FRC w/PLL 4x
    ;     FRC_PLL16            FRC w/PLL 16x
    ;     XT_PLL4              XT w/PLL 4x
    ;     XT_PLL8              XT w/PLL 8x
    ;     XT_PLL16             XT w/PLL 16x
    ;     FRC_PLL8             FRC w/PLL 8x
    ;     ECIO_PLL4            ECIO w/PLL 4x
    ;     ECIO_PLL8            ECIO w/PLL 8x
    ;     ECIO_PLL16           ECIO w/PLL 16x
    ;     HS2_PLL4             HS2 w/PLL 4x
    ;     HS2_PLL8             HS2 w/PLL 8x
    ;     HS2_PLL16            HS2 w/PLL 16x
    ;     HS3_PLL4             HS3 w/PLL 4x
    ;     HS3_PLL8             HS3 w/PLL 8x
    ;     HS3_PLL16            HS3 w/PLL 16x
    ;
    ;   Clock Switching and Monitor:
    ;     CSW_FSCM_ON          Sw Enabled, Mon Enabled
    ;     CSW_ON_FSCM_OFF      Sw Enabled, Mon Disabled
    ;     CSW_FSCM_OFF         Sw Disabled, Mon Disabled
    ;
            .equiv LPOSC,           0xF8E0 ; Low-Power 32KHz Osc (No change to Primary Osc Mode bits)
            .equiv LP,              0xF8E0 ; Low-Power 32KHz Osc (No change to Primary Osc Mode bits)
            .equiv FRC,             0xF9E0 ; Internal Fast RC (No change to Primary Osc Mode bits)
            .equiv LPRC,            0xFAE0 ; Internal Low-Power RC (No change to Primary Osc Mode bits)
            .equiv XTL,             0xFBE0 ; XTL
            .equiv HS,              0xFBE2 ; HS
            .equiv XT,              0xFBE4 ; XT
            .equiv ERCIO,           0xFBE8 ; ERCIO
            .equiv ERC,             0xFBE9 ; ERC
            .equiv EC,              0xFBEB ; EC
            .equiv ECIO,            0xFBEC ; ECIO
            .equiv FRC_PLL4,        0xFFE1 ; FRC w/PLL 4x
            .equiv FRC_PLL16,       0xFFE3 ; FRC w/PLL 16x
            .equiv XT_PLL4,         0xFFE5 ; XT w/PLL 4x
            .equiv XT_PLL8,         0xFFE6 ; XT w/PLL 8x
            .equiv XT_PLL16,        0xFFE7 ; XT w/PLL 16x
            .equiv FRC_PLL8,        0xFFEA ; FRC w/PLL 8x
            .equiv ECIO_PLL4,       0xFFED ; ECIO w/PLL 4x
            .equiv ECIO_PLL8,       0xFFEE ; ECIO w/PLL 8x
            .equiv ECIO_PLL16,      0xFFEF ; ECIO w/PLL 16x
            .equiv HS2_PLL4,        0xFFF1 ; HS2 w/PLL 4x
            .equiv HS2_PLL8,        0xFFF2 ; HS2 w/PLL 8x
            .equiv HS2_PLL16,       0xFFF3 ; HS2 w/PLL 16x
            .equiv HS3_PLL4,        0xFFF5 ; HS3 w/PLL 4x
            .equiv HS3_PLL8,        0xFFF6 ; HS3 w/PLL 8x
            .equiv HS3_PLL16,       0xFFF7 ; HS3 w/PLL 16x

            .equiv CSW_FSCM_ON,     0x3FFF ; Sw Enabled, Mon Enabled
            .equiv CSW_ON_FSCM_OFF, 0x7FFF ; Sw Enabled, Mon Disabled
            .equiv CSW_FSCM_OFF,    0xBFFF ; Sw Disabled, Mon Disabled

     

    Attached Image(s)

    #11
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 4003
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: Using "pragma config" for configuration bits? 2019/01/08 08:25:30 (permalink)
    0
    Each bit or bit group inside a fuse register is set like this:
    //FOSC
    #pragma config POSCMD = HS
    #pragma config FCKSM = CSECMD
    #pragma config OSCIOFNC = OFF
    #pragma config PLLKEN = PLLKEN_OFF   // PLL enable signal


    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #12
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 4003
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: Using "pragma config" for configuration bits? 2019/01/08 08:42:27 (permalink)
    0
    Best to look at the configuration options.
    file:///C:/Program Files (x86)/Microchip/xc16/v1.35/docs/config_docs/33CH128MP508.html  (my chip)
    post edited by Gort2015 - 2019/01/08 08:43:41

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #13
    Nikolay_Po
    Super Member
    • Total Posts : 1941
    • Reward points : 0
    • Joined: 2012/04/01 13:49:27
    • Location: Russia, Novorossiysk
    • Status: offline
    Re: Using "pragma config" for configuration bits? 2019/01/08 15:24:39 (permalink)
    0
    Thank you for attention, Gort. Of course I've checked the documents, including /microchip/xc16/v1.36/docs/config_docs/30F6014A.html. The first code block in my message above is about FOSFPR Oscillator right from this file.
    -=-
    In short: I need to program FOS to FRC and FPR to XT_PLL16. But these bit fields combined into one FOSFPR which disallow required settings.
    After an hours of trials and errors, I've managed the problem. Deprecated way with _FOSC(x) macros and bit masks:
    _FOSC( FRC & XT_PLL16 & CSW_ON_FSCM_OFF)
    doesn't work anymore. The masks in p30F6014A.h doesn't coincide with the same names of assembler p30F6014A.inc and producing incorrect value. But _FOSC(0x79E7) do the job! When programmed FOSC word by the value 0x79E7, I got desired. The controller is starting at FRC (OSCCONbits.COSC=0b001). Then, when it needed, is switching to XT_PLL16 by changing COSC for 0b111. It is working in hardware, I've checked by actual instruction speed, by reading COSC value in debug mode and by power consumption.
    In my opinion the problem itself is in a fact that FOS and FPR are combined together. They should be different settings. Or, at least, there should be a way to program the register by arbitrary value. I found no way to do the job with #pragma config. #pragma config for these chips nor allows to program FOS and FPR separately, by different states, nor allows to program the register by arbitrary value. Only deprecated way with _FOSC(x) still work partially.
    #14
    qhb
    Superb Member
    • Total Posts : 9999
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: Using "pragma config" for configuration bits? 2019/01/08 16:27:14 (permalink)
    +1 (1)
    Nikolay_Po
    ...
    After an hours of trials and errors, I've managed the problem. Deprecated way with _FOSC(x) macros and bit masks:
    _FOSC( FRC & XT_PLL16 & CSW_ON_FSCM_OFF)
    doesn't work anymore. The masks in p30F6014A.h doesn't coincide with the same names of assembler p30F6014A.inc and producing incorrect value.

    Agree. The relevant lines from p30F6014A.h are:
    #define FRC                  0xF9E0
    #define XT_PLL16             0xF9E0
    #define CSW_ON_FSCM_OFF      0x79E0

    and I think they should be:
    #define FRC                  0xF9FF    //10-8=001
    #define XT_PLL16             0xFFE7    //10-8=111, 4-0 = 00111
    #define CSW_ON_FSCM_OFF      0x7FFF    //15 = 0

     

    Nearly there...
    #15
    Nikolay_Po
    Super Member
    • Total Posts : 1941
    • Reward points : 0
    • Joined: 2012/04/01 13:49:27
    • Location: Russia, Novorossiysk
    • Status: offline
    Re: Using "pragma config" for configuration bits? 2019/01/09 00:28:07 (permalink)
    0
    Moreover, in p30F6014A.h of XC16 v1.36 these masks are defined completely different:
    #define XT_PLL16             0xC707
    #define FRC                  0xC100
    #define CSW_ON_FSCM_OFF      0x471F

    This is ether a fail or they have changed the principle the masks are used.
     
    In p30F6014A.inc
            .equiv FRC,             0xF9E0 ; Internal Fast RC (No change to Primary Osc Mode bits)
            .equiv XT_PLL16,        0xFFE7 ; XT w/PLL 16x
            .equiv CSW_ON_FSCM_OFF, 0x7FFF ; Sw Enabled, Mon Disabled

    The values are correct except FRC doesn't comply the comment "No change to Primary Osc Mode bits". It does. It clears all 5 bits of FPR. Should be 0xF9FF. There is no happiness in the world (how to say this in English?)...
    -=-
    Anyway assembler definitions is hard to use in "C" code. May be is it possible to make an assembly insert? How would yo do in asm FOSC = (CSW_ON_FSCM_OFF & FRC & XT_PLL16)? After they will remove _FOSC(x) macro it will be the last chance.
    post edited by Nikolay_Po - 2019/01/09 01:58:53
    #16
    Jump to:
    © 2020 APG vNext Commercial Version 4.5