K64F12+KDS3.0+KSDK1.2.0+USB CDC Virtual COM Demo(FreeRTOS) without PE

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

K64F12+KDS3.0+KSDK1.2.0+USB CDC Virtual COM Demo(FreeRTOS) without PE

3,088 次查看
gloria
Contributor III

Hi, I am using USB CDC Virtual COM Demo(FreeRTOS) and my development environment is K64F12+KDS3.0+KSDK1.2.0 without PE. I want try to change the FlexBus clock (CLKOUT) according to PC serial port input. However, when I change the value of ''.vdiv0'' and re-initial the clock again, the virtual serial port cannot find any more and nothing can be wrriten to the port

the code is shown following:

in board.c:

const clock_manager_user_config_t ClockConfigRun30M =

{

    .mcgConfig =

    {

        .mcg_mode           = kMcgModePEE,   // Work in PEE mode.

        .irclkEnable        = true,  // MCGIRCLK enable.

        .irclkEnableInStop  = false, // MCGIRCLK disable in STOP mode.

        .ircs               = kMcgIrcSlow, // Select IRC32k.

        .fcrdiv             = 0U,    // FCRDIV is 0.

        .frdiv   = 7U,

        .drs     = kMcgDcoRangeSelLow,  // Low frequency range

        .dmx32   = kMcgDmx32Default,    // DCO has a default range of 25%

        .oscsel  = kMcgOscselOsc,       // Select OSC

        .pll0EnableInFllMode = false,  // PLL0 disable

        .pll0EnableInStop    = false,  // PLL0 disalbe in STOP mode

        .prdiv0              = 0x13U,

        .vdiv0               = 0x00U,//0x10U, //0x18U

    },

    .simConfig =

    {

        .pllFllSel = kClockPllFllSelPll,    // PLLFLLSEL select PLL.

        .er32kSrc  = kClockEr32kSrcRtc,     // ERCLK32K selection, use RTC.

        .outdiv1   = 0U,

        .outdiv2   = 1U,

        .outdiv3   = 1U,

        .outdiv4   = 4U,

    },

    .oscerConfig =

    {

        .enable       = true,  // OSCERCLK enable.

        .enableInStop = false, // OSCERCLK disable in STOP mode.

    }

};

void BOARD_ClockInit30M(void)

{

    /* Set allowed power mode, allow all. */

    SMC_HAL_SetProtection(SMC, kAllowPowerModeAll);

    /* Setup board clock source. */

    // Setup OSC0 if used.

    // Configure OSC0 pin mux.

    PORT_HAL_SetMuxMode(EXTAL0_PORT, EXTAL0_PIN, EXTAL0_PINMUX);

    BOARD_InitOsc0();

    // Setup RTC external clock if used.

    BOARD_InitRtcOsc();

    /* Set system clock configuration. */

#if (CLOCK_INIT_CONFIG == CLOCK_VLPR)

    CLOCK_SYS_SetConfiguration(&g_defaultClockConfigVlpr);

#else

    CLOCK_SYS_SetConfiguration(&ClockConfigRun30M);

#endif

}

and in virtual_com.c:

void Virtual_Com_App(void)

{

    /* User Code */

    if ((0 != g_recv_size) && (0xFFFFFFFF != g_recv_size))

    {

        int32_t i;

        /* Copy Buffer to Send Buff */

        for (i = 0; i < g_recv_size; i++)

        {

            USB_PRINTF("Copied: %c\n", g_curr_recv_buf[i]);

            g_curr_send_buf[g_send_size++] = g_curr_recv_buf[i];

        }

        g_recv_size = 0;

    }

    if (g_send_size)

    {

        uint8_t error;

        uint32_t size = g_send_size;

        g_send_size = 0;

        error = USB_Class_CDC_Send_Data(g_app_handle, DIC_BULK_IN_ENDPOINT,g_curr_send_buf, size);

         

       //added by myself to change the FlexBus according to serial port input

        switch(g_curr_send_buf[0]){

        case 0x31:

            BOARD_ClockInit30M();

            GPIO_DRV_ClearPinOutput(kGpioLED1);

            g_curr_send_buf[0]=0x00;

            OSA_Init();

            break;

        case 0x32:

            GPIO_DRV_SetPinOutput(kGpioLED1);

            //BOARD_ClockInit40M();

            g_curr_send_buf[0]=0x00;

            break;

        case 0x33:

            GPIO_DRV_ClearPinOutput(kGpioLED2);

            //BOARD_ClockInit35M();

            g_curr_send_buf[0]=0x00;

            break;

        default:

            break;

        }

        if (error != USB_OK)

        {

            /* Failure to send Data Handling code here */

        }

    }

if BOARD_ClockInit30M() is not included in the case statement, the LED can be toggled correctly and serial port is normal. However, when the BOARD_ClockInit30M() is included in the case statement, the serial port can only be inputed once and then when I input again, error message will appear and the serial port cannot be opened anymore.

pastedImage_2.png

pastedImage_0.png

Could anybody can help me about this? I suppose it may because I re-initial the clock but I don't know how to fix it.

Many thanks!

Best Regars,

Gloria

0 项奖励
回复
10 回复数

1,990 次查看
EarlOrlando
Senior Contributor II

Hello Gloria,

Does the clock is changed correctly? If you are debugging, can you continue debugging your application and the terminal is the only thing that crashes?

Due to you are changing the system clock, the bus clock, the flexbus clock and the flash clock, and the USB and UART are depending on those clocks you need to de-initialize the modules, change the clock frequency and then initialize them again.

Best regards,

Earl.

0 项奖励
回复

1,990 次查看
gloria
Contributor III

Hi Earl,

The clock is changed correctly. And yes, if I am debugging, I can continue debugging and the terminal is the only thing that crashes.

Could you tell me how to de-initialize these modules? Do you mean close them first and then after changing the clcok frequency, initialize them again?

Many thanks!

Best Regards,

Gloria

0 项奖励
回复

1,990 次查看
EarlOrlando
Senior Contributor II

Hi Gloria,

Yes, that is what I mean. You need to call DbgConsole_DeInit before to change the clock frequency and then call dbg_uart_init after to change the clock frequency.

Please let me know if this works for you.

Best regards,

Earl.

0 项奖励
回复

1,990 次查看
yasuhikokoumoto
Senior Contributor I

Hello Earl,

by the way, had the Virtual Com program bug reported by the old post Re: KSDK_1.0.0 の「vertual_com.c」の動作で「usb_dev.c」にて HardFault する。  been fixed in KDSK_1.2.0?

Unless it had been fixed, it would be difficult to modify the sample code.

If you are not responsible with it, please inform it to the appropriate person.

Best regards,

Yasuhiko Koumoto.

0 项奖励
回复

1,990 次查看
gloria
Contributor III

Hi Yasuhiko,

Do you mean the virtual com sample code cannot be modified?

Many thanks!

Best Regards,

Gloria

0 项奖励
回复

1,990 次查看
yasuhikokoumoto
Senior Contributor I

Hello Gloria,

Yes.

The bug is that the allocated buffer has not been released after transmitting from the usb_status usb_dci_khci_send() modeule.

However bug fix is too complicated.

Even if the buffer was released at the end of the the usb_status usb_dci_khci_send(), it will be failed because of conflicting ti the buffer release of the receiver.

This is the information from Mr. Naoki Yamazaki.

Best regards,

Yasuhiko Koumoto.

0 项奖励
回复

1,990 次查看
gloria
Contributor III

Hi Earl,

I have tried to do this, but the problem is still not solved.

My code is :

in board.c:

void dbg_uart_init(void)

{

    configure_uart_pins(BOARD_DEBUG_UART_INSTANCE);

    DbgConsole_Init(BOARD_DEBUG_UART_INSTANCE, BOARD_DEBUG_UART_BAUD, kDebugConsoleUART);

}

void dbg_uart_deinit(void)   //added by myself

{

    DbgConsole_DeInit();

}

in virtual_com.c:

void Virtual_Com_App(void)

{

    /* User Code */

    if ((0 != g_recv_size) && (0xFFFFFFFF != g_recv_size))

    {

        int32_t i;

        /* Copy Buffer to Send Buff */

        for (i = 0; i < g_recv_size; i++)

        {

            USB_PRINTF("Copied: %c\n", g_curr_recv_buf[i]);

            g_curr_send_buf[g_send_size++] = g_curr_recv_buf[i];

        }

        g_recv_size = 0;

    }

    if (g_send_size)

    {

        uint8_t error;

        uint32_t size = g_send_size;

        g_send_size = 0;

        error = USB_Class_CDC_Send_Data(g_app_handle, DIC_BULK_IN_ENDPOINT,g_curr_send_buf, size);

        switch(g_curr_send_buf[0]){

        case 0x31:

           dbg_uart_deinit();

            BOARD_ClockInit30M();

            dbg_uart_init();

            GPIO_DRV_ClearPinOutput(kGpioLED1);

            g_curr_send_buf[0]=0x00;

            break;

        case 0x32:

            GPIO_DRV_SetPinOutput(kGpioLED1);

            BOARD_ClockInit20M();

            g_curr_send_buf[0]=0x00;

            break;

        default:

            g_curr_send_buf[0]=0x00;

            break;

        }

        if (error != USB_OK)

        {

            /* Failure to send Data Handling code here */

        }

    }

0 项奖励
回复

1,990 次查看
EarlOrlando
Senior Contributor II

Just to be sure, the demo that you are modifying is the one located at C:\Freescale\KSDK_1.2.0\examples\frdmk64f\demo_apps\usb\device\cdc\virtual_com\freertos\kds?

I am almost sure that the problem is that you are changing the USB module frequency. Remember that it must be always 48 MHz. Please be sure that the clock source for that module is not the PLL (because you are changing the VDIV value which changes the MCGPLLCLK frequency), it is not recommended to use the FLL because it is not very accurate so the solution is to use the USB_CLKIN or the IRC48MCLK lines to feed your USB module.

Best regards,

Earl.

0 项奖励
回复

1,990 次查看
gloria
Contributor III

Hi Earl,

Yes, I am sure I am using C:\Freescale\KSDK_1.2.0\examples\frdmk64f\demo_apps\usb\device\cdc\virtual_com\freertos\kds demo.

Could you please tell me where I could find the file to check which clock I am using to feed the USB?

Many thanks!

Best Regards,

Gloria

0 项奖励
回复

1,990 次查看
EarlOrlando
Senior Contributor II

Hi Gloria,

Another way to change the core/bus/flexbus/flash clocks without change the PLL frequency is modifying the SIM_CLKDIV1[OUTDIVx] register. You could try that. This is done by changing the fields outdivx in the simConfig field in the clock_manager_user_config_t structure and then calling the function CLOCK_SYS_SetConfiguration.

Best regards,

Earl.

0 项奖励
回复