KE18 clock driver doesn't work

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

KE18 clock driver doesn't work

789 Views
davidsherman
Senior Contributor I

After using the clock driver to get the KE18F512 working in MCUXpresso, I thought I would share that there's a bug that prevents it from working.  In fsl_clock.h, there is a structure for the SCG system clock divider:

typedef struct _scg_sys_clk_config
{
    uint32_t divSlow : 4; /*!< Slow clock divider, see @ref scg_sys_clk_div_t. */
    uint32_t divBus : 4;  /*!< Bus clock divider, see @ref scg_sys_clk_div_t.  */
    uint32_t : 4;         /*!< Reserved. */
    uint32_t : 4;         /*!< Reserved. */
    uint32_t divCore : 4; /*!< Core clock divider, see @ref scg_sys_clk_div_t. */
    uint32_t : 4;         /*!< Reserved. */
    uint32_t src : 4;     /*!< System clock source, see @ref scg_sys_clk_src_t. */
    uint32_t : 4;         /*!< reserved. */
} scg_sys_clk_config_t;

Unfortunately, GCC does not include the unnamed "reserved" fields, so the structure is not 32 bits wide as is needed to write the SCG_RCCR register, so it does not get changed.  It is necessary to name the reserved fields to make it work:

typedef struct _scg_sys_clk_config
{
    uint32_t divSlow : 4; /*!< Slow clock divider, see @ref scg_sys_clk_div_t. */
    uint32_t divBus : 4;  /*!< Bus clock divider, see @ref scg_sys_clk_div_t.  */
    uint32_t reserved1: 4;         /*!< Reserved. */
    uint32_t reserved2: 4;         /*!< Reserved. */
    uint32_t divCore : 4; /*!< Core clock divider, see @ref scg_sys_clk_div_t. */
    uint32_t reserved3: 4;         /*!< Reserved. */
    uint32_t src : 4;     /*!< System clock source, see @ref scg_sys_clk_src_t. */
    uint32_t reserved4: 4;         /*!< reserved. */
} scg_sys_clk_config_t;

0 Kudos
2 Replies

642 Views
jingpan
NXP TechSupport
NXP TechSupport

Hi, David

Thanks for your sharing. I was surprise when I see your report. Because bit-field structure is not only used here, you can see it in many place. So I checked it in internet, like http://en.cppreference.com/w/cpp/language/bit_field. It seems that unnamed bit_field are kept. Then I made a test on MCUXpresso.

struct S {
    unsigned int b1 : 2;
    unsigned int :3; 
    unsigned int b2 : 2;
    unsigned int b3 : 2;
};

union teststruct
{
 unsigned int data;
 struct S bitfield;
}ts;


/*******************************************************************************
 * Code
 ******************************************************************************/
/*!
 * @brief Main function
 */
int main(void)
{
    char ch;

    /* Init board hardware. */
    BOARD_InitPins();
    BOARD_BootClockRUN();
    BOARD_InitDebugConsole();

    PRINTF("hello world.\r\n");
    ts.bitfield.b1=1;
    ts.bitfield.b2=1;
    ts.bitfield.b3=1;
    PRINTF("%x",ts.data);
    while (1)
    {
        ch = GETCHAR();
        PUTCHAR(ch);
    }
}

 The out put is "a1", not "15". As you know, MCNXpresso use GCC.

I'm not sure what happened to your code. Can you give more detail?

Regards

Joph

0 Kudos

642 Views
davidsherman
Senior Contributor I

I believe I see what the problem was.  I had been using a previous version of KSDK for this part, and the bit fields in the scg_sys_clk_config_t structure WERE named, so the initializer I created had those fields accounted for.  I reused the same startup code and the fields were not populated correctly as a result of the now-unnamed bitfields.  If I take out the unneeded parameters for the unused bitfields it works properly.  Thank you.

0 Kudos