How do I setup the debug console to use the Virtual COM port (USB CDC driver)?

cancel
Showing results for 
Search instead for 
Did you mean: 

How do I setup the debug console to use the Virtual COM port (USB CDC driver)?

3,643 Views
markdobrosielsk
Contributor II

I know that DbgConsole_Init() has to be called with a debug_console_device_type_t == kDebugConsoleUSBCDC, but what else?

- Do I need to include the virtual_com.c & virtual_com.h files in my project?

- Do I need to grab the usb callback routines from the virtual com demo and put them somewhere?

- Where do I define BOARD_USE_VIRTUALCOM?

 

I'm trying to use the Virtual COM port for my console I/O on a FRDM-KL27Z running mqx-lite. I've successfully converted the virtual_com usb demo to use mqx-lite, and now I need to write my "real" application with real console I/O (not just echoed characters).

Labels (1)
7 Replies

819 Views
wesleyhunter
Contributor II

I have written a blog on exactly how to do this at: KSDK USB CDC PRINTF | Centaurian

Hope it helps.

0 Kudos

819 Views
kirantujare
Contributor IV

Hi Wesley,

How to use the above link for scanf? I got it working for my (MK50DX256CLK10) custom board for printf. I want to make it work for scanf.
-Kiran

0 Kudos

819 Views
Rick_Li
NXP Employee
NXP Employee

Hi,

I don't fully understand your question!

As my understanding, the debugger console should be configured by the IDE itself.

When using a usb stack, you may make a USB works as a virtual_com, but "Debugging" is IDE's job.

could you give me more description on this issue?

0 Kudos

819 Views
markdobrosielsk
Contributor II

I use the IAR IDE. For any given bit of example code (lwdemo, for example), there is a workspace comprising several different projects. There's one project for each library (mqx library, ksdk library, etc)and then one for the example itself. In many (maybe all) of the example projects, there's a Debug Console sub-directory containing a number of files, including:

   fsl_debug_console.c

   fsl_debug_console.h

   print_scan.c

   print_scan.h

Maybe they're named improperly, and the fsl_debug_console files should just be named fsl_console.c & fsl_console.h. The point is, these files appear to let you redirect stdio to whichever port you'd like. In board.h, for the FRDM-KL43Z & -27Z, you'll find the line

   #define BOARD_USE_LPUART

which makes stdio (printf, scanf) use the LPUART. On the FRDM boards, this actually gets sent out the SDA port. By using this line instead

   #define BOARD_USE_VIRTUALCOM

the fsl_debug_console module should redirect stdio to the USB port using the CDC Virtual COM port functionality.

BUT it's not as simple as just changing the #define in board.h. I've added the USB Device library to the workspace, and link it into the example code. I've added virtual_com.c & .h and usb_descriptor.c & .h to the example project. I've merged the main functions together (virtual.c has its own main).

The first big problem is that the init_bsp.c function _bsp_pre_init() has code to configure the UART pins for BOARD_USE_LPSCI, BOARD_USE_LPUART, and BOARD_USE_UART. Anything else, like BOARD_USE_VIRTUALCOM, causes a compile error by way of this statement:

   #error Default serial module is unsupported or undefined.

So, I need to know what else I need to do to make my USB port be used for stdio.

819 Views
macl
Senior Contributor I

Hi,

Try the steps from this example:

For example on FRDM-K27,  the steps to make USB virtual com work:

  • - Build USB lib from folder Freescale\KSDK_1.2.0\usb\usb_core\device\build\iar\usbd_sdk_frdmkl27z_bm\usbd_sdk_frdmkl27z_bm.eww . If you use other rtos, you must build corresponding lib.
  • - Add file  virtual_com.c, virtual_com.h , usb_descriptor.c, usb_descriptor.c.h from Freescale\KSDK_1.2.0\platform\utilities\src virtual com to project

image001.png

  • - In project option  C/C++ Complier -> Preprocessor include path

$PROJ_DIR$/../../../../../platform/osa/inc

$PROJ_DIR$/../../../../../platform/utilities/inc

$PROJ_DIR$/../../../../../platform/CMSIS/Include

$PROJ_DIR$/../../../../../platform/devices

$PROJ_DIR$/../../../../../platform/devices/MKL27Z644/include

$PROJ_DIR$/../../../../../platform/devices/MKL27Z644/startup

$PROJ_DIR$/../../../../../platform/hal/inc

$PROJ_DIR$/../../../../../platform/drivers/inc

$PROJ_DIR$/../../../../../platform/system/inc

$PROJ_DIR$/../../../../../platform/utilities/inc

$PROJ_DIR$/../../..

$PROJ_DIR$/../../../../../platform/utilities/inc/virtual_com

$PROJ_DIR$/../../../../../usb/usb_core/device/include/frdmkl27z

$PROJ_DIR$/../../../../../usb/usb_core/include

$PROJ_DIR$/../../../../../usb/adapter/sources

$PROJ_DIR$/../../../../../usb/adapter/sources/sdk

$PROJ_DIR$/../../../../../usb/usb_core/hal

$PROJ_DIR$/../../../../../usb/usb_core/include

$PROJ_DIR$/../../../../../usb/usb_core/device/include

$PROJ_DIR$/../../../../../usb/usb_core/device/sources/classes/audio

$PROJ_DIR$/../../../../../usb/usb_core/device/sources/classes/cdc

$PROJ_DIR$/../../../../../usb/usb_core/device/sources/classes/common

$PROJ_DIR$/../../../../../usb/usb_core/device/sources/classes/composite

$PROJ_DIR$/../../../../../usb/usb_core/device/sources/classes/hid

$PROJ_DIR$/../../../../../usb/usb_core/device/sources/classes/include

$PROJ_DIR$/../../../../../usb/usb_core/device/sources/classes/msd

$PROJ_DIR$/../../../../../usb/usb_core/device/sources/classes/phdc

$PROJ_DIR$/../../../../../usb/usb_core/device/sources/controller/khci

$PROJ_DIR$/../../../../../usb/usb_core/device/sources/controller

  • - Add BOARD_USE_VIRTUALCOM definition in project setting

2image002.png

  • - In linker option, add lib usb to project $PROJ_DIR$/../../../../../lib/ksdk_platform_lib/iar/KL27Z644/debug/libksdk_platform.a

With release target, you must include release usb lib.a

image003.png

  • - In linker/config

__ram_vector_table__=1

__stack_size__=0x400

__heap_size__=0x400

image004.png

  • - Define BOARD_DEBUG_USBCDC_INSTANCE and BOARD_DEBUG_USBCDC_BAUD in board.h

/* The USB_CDC to use for debug messages. */

#ifndef BOARD_DEBUG_USBCDC_INSTANCE

    #define BOARD_DEBUG_USBCDC_INSTANCE   1

#endif

#ifndef BOARD_DEBUG_USBCDC_BAUD

    #define BOARD_DEBUG_USBCDC_BAUD       115200

#endif

  • - In board.c modify dbg_uart_init to use USB virtual com.

void dbg_uart_init(void)

{

    DbgConsole_Init(BOARD_DEBUG_USBCDC_INSTANCE, BOARD_DEBUG_USBCDC_BAUD, kDebugConsoleUSBCDC);

}

819 Views
markdobrosielsk
Contributor II

Not quite there yet…

In this snippet,

debug_console_status_t DbgConsole_Init(

        uint32_t uartInstance, uint32_t baudRate, debug_console_device_type_t device)

{

    if (s_debugConsole.type != kDebugConsoleNone)

    {

        return kStatus_DEBUGCONSOLE_Failed;

    }

    /* Set debug console to initialized to avoid duplicated init operation.*/

    s_debugConsole.type = device;

    s_debugConsole.instance = uartInstance;

    /* Switch between different device. */

    switch (device)

    {

#if (defined(USB_INSTANCE_COUNT) && defined(BOARD_USE_VIRTUALCOM))  /&& defined()/

      case kDebugConsoleUSBCDC:

        {

                VirtualCom_Init();

                s_debugConsole.base = (void*)g_app_handle;

                s_debugConsole.ops.tx_union.USB_Send = VirtualCom_SendDataBlocking;

                s_debugConsole.ops.rx_union.USB_Receive = VirtualCom_ReceiveDataBlocking;

        }

        break;

#endif

In the line

   s_debugConsole.base = (void*)g_app_handle;

g_app_handle = 0, so the program goes off into the weeds on execution of DbgConsole_Init. In the other cases, like the LPUART, s_debugConsole.base is set to the base address of the lpuart instance. What should I do here?

Thanks,

Mark Dobrosielski

0 Kudos

819 Views
macl
Senior Contributor I

Hi Mark,

I have a few more tips that might help.

When the program is downloaded and running, be sure to manually install the virtual cdc driver which is in directory:

C:\Freescale\KSDK_1.2.0\examples\frdmkl27z\demo_apps\usb\device\cdc\virtual_com\inf.

A more detail description is in the readme file of usb virtual cdc example:

C:\Freescale\KSDK_1.2.0\examples\frdmkl27z\demo_apps\usb\device\cdc\virtual_com\readme.pdf

Also, although I only highlighted the last parameter in the DbgConsole_Init function above, please note that all parameters need to be changed.  See below.

void dbg_uart_init(void)

{

    configure_uart_pins(BOARD_DEBUG_UART_INSTANCE);

   /* For UART */

   // DbgConsole_Init(BOARD_DEBUG_UART_INSTANCE, BOARD_DEBUG_UART_BAUD, kDebugConsoleUART);

  

   /* For USB CDC */

     DbgConsole_Init(BOARD_DEBUG_USBCDC_INSTANCE, BOARD_DEBUG_USBCDC_BAUD, kDebugConsoleUSBCDC);

}