控制台的三种连接方式:
1、IP网络
2、USB
3、UART
一:介绍USB CDC方式:
1、控制台配置如下:
2、USB
Product ID 可以是:0x0000/0x5300/0x0238
不同的值代表不同的COM口
3、CDC配置
二:介绍UART配置:
注意UART配置通道、波特率以及中断优先级,如下:
#include "g_testThread.h" void led_toggle_callback(sf_console_cb_args_t * p_args); const sf_console_command_t g_sf_console_commands[] = { { .command = (uint8_t *)"TOGGLE", //CMD .help = (uint8_t *)"Toggle an LED", .callback = led_toggle_callback, //控制台命令回调 .context = NULL }, { .command = (uint8_t *)"cell", .help = (uint8_t *)"Cell Provisioning info to be saved\r\n" " Usage:\r\n" " cell <APN> <Context ID> <PDP Type>", .callback = led_toggle_callback, .context = NULL }, }; /* 1. Create Menu Structure */ const sf_console_menu_t g_sf_console_root_menu = { .menu_prev = NULL, .menu_name = (uint8_t *)"Command", .num_commands = (sizeof(g_sf_console_commands)) / (sizeof(g_sf_console_commands[0])), .command_list = &g_sf_console_commands[0] }; /*********************************************************************************************************************** * Function Name: led_toggle_callback * Description : Implement Callbacks * Callback function provided to g_sf_console_commands[0]. Function is invoked when user inputs * TOGGLE<CR> in the Console * Arguments : p_args - * Pointer to an instance type sf_console_cb_args_t * Return Value : None ***********************************************************************************************************************/ void led_toggle_callback(sf_console_cb_args_t * p_args) { bsp_leds_t leds; ioport_level_t level; /* Get LED list from BSP */ R_BSP_LedsGet(&leds); /* Read current level */ g_ioport.p_api->pinRead(leds.p_leds[0], &level); /* Invert level */ g_ioport.p_api->pinWrite(leds.p_leds[0], (ioport_level_t)!level); } /* Test Thread entry function */ void g_testThread_entry(void) { /* TODO: add your own code here */ while (1) { g_sf_console0.p_api->prompt(g_sf_console0.p_ctrl, NULL, TX_WAIT_FOREVER);//TX_WAIT_FOREVER//TX_NO_WAIT //tx_thread_sleep (50); } }
测试如下:
控制台支持CMD:
“TOGGLE”
“CELL”
“?”
“?”:为帮助信息
可变形参:
1、_vsnprintf,_vsnprintf是C库函数之一,属于可变参数。用于向字符串中打印数据、数据格式用户自定义。头文件是#include <stdarg.h>。
-
char *str [out],把生成的格式化的字符串存放在这里.
-
size_t size [in], str可接受的最大字符数 [1] (非字节数,UNICODE一个字符两个字节),防止产生数组越界.
-
const char *format [in], 指定输出格式的字符串,它决定了你需要提供的可变参数的类型、个数和顺序。
-
va_list ap [in], va_list变量. va:variable-argument:可变参数
-
void print_to_console(const char *pFmt, ...) { UINT status; char str[128]; uint8_t u8Len; va_list args; memset(str,0,128); status = tx_mutex_get(&g_console_print_mutex, TX_WAIT_FOREVER); if (status != TX_SUCCESS) return; va_start(args, pFmt); u8Len = (uint8_t)vsnprintf(str, 128, pFmt, args); va_end(args); if (u8Len > 0) { g_sf_console0.p_api->write(g_sf_console0.p_ctrl, (const uint8_t *)str, TX_WAIT_FOREVER); } tx_mutex_put(&g_console_print_mutex); }
如使用宏实现:
//#define SEMI_HOSTING #ifdef SEMI_HOSTING #ifdef __GNUC__ extern void initialise_monitor_handles (void); #endif #endif #ifdef SEMI_HOSTING #define xp_log(format, args...) printf(format, ##args) #else #define xp_log(format, args...) print_to_console(format, ##args) #endif