Error when trying to read/change MCG_C1 value.

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

Error when trying to read/change MCG_C1 value.

Jump to solution
731 Views
donovanlee
Contributor I

Hello, my first post! I am trying to read the value of MCG_C1 but having bad luck.  I am using a KL25Z Freescale Mbed board and trying to do some baremetal stuff.

I have the following in my header file:

#define MCG_C1                (*(volatile unsigned long *)(0x40064000)) // MCG Register

And I am simply trying to read it at this point in my main.cpp file:

unsigned int FU_TIM=0;

FU_TIM=MCG_C1;

pc.printf("MCG_C1 = %x \n",FU_TIM);

I am expecting an 8-bit Hex number per the datasheet (p. 372) but I get "C84241A" which translates to: 1100100001000010010000011010 ... clearly more than 8 bits.

Questions:

1) Why am I getting more than 8-bits on read?

2) How do I write to this register?  I want to select the slow IRC eventually.

3) Moreover, if I try to read MCG_C2 at 0x40064001, my board just hangs... what gives?

Thank you,

Donovan

0 Kudos
1 Solution
614 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi, Donovan,

as you know, the MCG_C1 register has only 8 bits length, it is a byte register, if you want to access it, you should use a byte variable and define the address of  MCG_C1 register as a Byte address.

Pls modify in this way:

#define MCG_C1                (*(volatile unsigned long *)(0x40064000)) // MCG Register

TO

#define MCGG_C1                (*(volatile unsigned char*)(0x40064000)) // MCG Register

in  your main code, you can declare a global variable as:

unsigned char c0;

main()

{

    c0=MCGG_C1;
    asm("nop");

//the above code is okay

}

I do not know the tools you are using, I use CodeWarrior for mcu ver10.5 tools, the MCG_C1 name has been defined, you can use the name directly. So I use another name MCGG_C1.

If you declare as #define MCG_C1                (*(volatile unsigned long *)(0x40064000)) // MCG Register, you will get four bytes data from the following address:

0x40064000, 0x40064001,0x40064002,0x40064003.

View solution in original post

0 Kudos
2 Replies
615 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi, Donovan,

as you know, the MCG_C1 register has only 8 bits length, it is a byte register, if you want to access it, you should use a byte variable and define the address of  MCG_C1 register as a Byte address.

Pls modify in this way:

#define MCG_C1                (*(volatile unsigned long *)(0x40064000)) // MCG Register

TO

#define MCGG_C1                (*(volatile unsigned char*)(0x40064000)) // MCG Register

in  your main code, you can declare a global variable as:

unsigned char c0;

main()

{

    c0=MCGG_C1;
    asm("nop");

//the above code is okay

}

I do not know the tools you are using, I use CodeWarrior for mcu ver10.5 tools, the MCG_C1 name has been defined, you can use the name directly. So I use another name MCGG_C1.

If you declare as #define MCG_C1                (*(volatile unsigned long *)(0x40064000)) // MCG Register, you will get four bytes data from the following address:

0x40064000, 0x40064001,0x40064002,0x40064003.

0 Kudos
614 Views
donovanlee
Contributor I

Thank you! What a silly mistake on my part.

Well now, I can read the value of MCG_C1, but when I try to set it, like

MCG_C1=0x3c;

The system hangs. 

I am using the online MBED compiler.  My full code is below:

head.h:

#define SIM_SDID              (*(volatile unsigned long *)(0x40048024)) // Kinetis ID register

#define PORTC_PCR3            (*(volatile unsigned long *)(0x4004B00C)) // PORTC_PCR3 Mux

#define SIM_SOPT2             (*(volatile unsigned long *)(0x40048004)) // SIM Option Mux           

#define MCG_C1                (*(volatile unsigned char *)(0x40064000)) // MCG Register

#define MCG_C2                (*(volatile unsigned char *)(0x40064001))

main.cpp:

#include "mbed.h"

#include "head.h"

Serial pc(USBTX, USBRX); // tx, rx for debug terminal

Serial testOut(PTC4,PTC3);

DigitalOut myled(LED1);

int main() {

   

    int cycle = 0;

    PORTC_PCR3=0x501;

    SIM_SOPT2=0x4010080;

   

    while(true){

        pc.baud(9600);

  

        myled = 1;

        wait(0.2);

        myled = 0;

        wait(0.2);

        unsigned int ID=0;

        unsigned int FU=0;

        unsigned int FU2=0;

        unsigned char FU_TIM=0;

        cycle++;

   

    ID=SIM_SDID;

     FU=SIM_SOPT2;

    // PORTC_PCR3=0x501;

    FU2=PORTC_PCR3;

    //MCG_C1=0x3c;

    FU_TIM=MCG_C1;

   

   

    pc.printf("Kinetis ID = %x \n",ID);

    pc.printf("SIM_SOPT2 = %x \n",FU);

    pc.printf("PORTC_PCR3 = %x \n",FU2);

    pc.printf("MCG_C1 = %x \n",FU_TIM);

    pc.printf("CycleNo = %d \n", cycle);

   

    }

}

0 Kudos