硬汉嵌入式论坛

 找回密码
 立即注册
查看: 316|回复: 4
收起左侧

[Embedded Studio] 关于SEGGER ES中使用strcmp,串口格式化等的问题

[复制链接]

2

主题

4

回帖

10

积分

新手上路

积分
10
发表于 2025-10-10 23:29:21 | 显示全部楼层 |阅读模式
最近在做IMX6ULL裸机相关的学习,使用SEGGER Embedded Studio V8.24创建的工程,目前遇到一些问题,想询问论坛的各位是否有解决办法。

1、自己写的串口格式化uart_printf不能使用uart_printf("%2d",12);类似的写法,只能用%d,不能限制位数。
[C] 纯文本查看 复制代码
// 自定义的 printf 函数
void uart_printf(const char *format, ...) {
    char buffer[512]; 
    va_list args;
    va_start(args, format);
    vsnprintf(buffer, sizeof(buffer), format, args);
    for (size_t i = 0; buffer[i] != '\0'; i++) {
        uart_putc(buffer[i]);
    }
    va_end(args);
}


2、在使用strcmp一类标准库的函数时候,部分函数会卡死,不知道是什么原因。自己重写代码后,覆盖原代码是可以执行的,不知道为什么strcmp执行不了。
[C] 纯文本查看 复制代码
int my_strcmp(const char *s1, const char *s2)
{
    while (*s1 && (*s1 == *s2))
    {
        s1++;
        s2++;
    }
    return (unsigned char)*s1 - (unsigned char)*s2;
}

回复

使用道具 举报

1万

主题

7万

回帖

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
119429
QQ
发表于 2025-10-11 09:48:50 | 显示全部楼层
使用printf试试

Embedded Studio做串口重定向方法
https://forum.anfulai.cn/forum.p ... 3705&fromuid=58
(出处: 硬汉嵌入式论坛)
回复

使用道具 举报

2

主题

4

回帖

10

积分

新手上路

积分
10
 楼主| 发表于 2025-10-12 12:13:06 | 显示全部楼层
eric2013 发表于 2025-10-11 09:48
使用printf试试

Embedded Studio做串口重定向方法

感谢指导,但是我最后的解决方案如下。
1、关于printf的问题,SEGGER ES8.24的项目Option->Code->printf/scanf中可以选择是否打开小数和位数
C:\Users\LQH\Pictures\printf.png
2、关于strcpy的问题,应该是4字节对齐,这个问题我还没有很好的办法,暂时的解决方案如下。在创建字符串的时候先做一下四字节对齐,这样就可以了,但是我不知道这个软件是否有可以直接设置的地方。
[C] 纯文本查看 复制代码
typedef struct
{
    const char *name;
    void (*func)(int argc, char **argv);
    const char *help;
} shell_cmd_t;
 
const char help_str[] __attribute__((aligned(4))) = "help";
const char led_str[]  __attribute__((aligned(4))) = "led";

const shell_cmd_t cmd_table[] = {
    {help_str, cmd_help, "显示所有命令"},
    {led_str,  cmd_led,  "控制LED: led on/off"},
};
void shell_exec(char *cmdline)
{
    char *argv[8] = {0};
    memset(argv, 0, sizeof(argv));
    int argc = 0;
      
    char *token = strtok(cmdline, " ");
 
    while (token && argc < 8)
    {
        argv[argc++] = token;
        token = strtok(NULL, " ");
    } 
    if (argc == 0)
        return; 

    for (int i = 0; i < CMD_COUNT; i++)
    { 
        if (strcmp(argv[0], cmd_table[i].name) == 0)
        {
            cmd_table[i].func(argc, argv);
            return;
        }
    }
 
    uart_printf("unknow cmd: %s\r\n", argv[0]);
}
回复

使用道具 举报

1万

主题

7万

回帖

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
119429
QQ
发表于 2025-10-13 09:29:26 | 显示全部楼层
liqinghuaxx 发表于 2025-10-12 12:13
感谢指导,但是我最后的解决方案如下。
1、关于printf的问题,SEGGER ES8.24的项目Option->Code->printf ...

这种函数没有对齐需求,估计是cache类的配置问题导致的
回复

使用道具 举报

2

主题

4

回帖

10

积分

新手上路

积分
10
 楼主| 发表于 2025-10-14 20:06:40 | 显示全部楼层
eric2013 发表于 2025-10-13 09:29
这种函数没有对齐需求,估计是cache类的配置问题导致的

我又去看了看官方的工程,我以前没有写过那个MMU_Init()。把这部分加上后,文本就不存在我之前说的对齐的这些了,但是我刚学这部分,还是不知道为什么,还不知道这些配置的作用,后面接着研究一下吧。
[C] 纯文本查看 复制代码
/*******************************************************************************
 * Variables
 ******************************************************************************/
static const mmu_attribute_t s_mmuDevAttr = {.type = MMU_MemoryDevice,
                                             .domain = 0U,
                                             .accessPerm = MMU_AccessRWNA,
                                             .shareable = 0U,
                                             .notSecure = 0U,
                                             .notGlob = 0U,
                                             .notExec = 1U};

static const mmu_attribute_t s_mmuRomAttr = {.type = MMU_MemoryWriteBackWriteAllocate,
                                             .domain = 0U,
                                             .accessPerm = MMU_AccessRORO,
                                             .shareable = 0U,
                                             .notSecure = 0U,
                                             .notGlob = 0U,
                                             .notExec = 0U};

static const mmu_attribute_t s_mmuRamAttr = {.type = MMU_MemoryWriteBackWriteAllocate,
                                             .domain = 0U,
                                             .accessPerm = MMU_AccessRWRW,
                                             .shareable = 0U,
                                             .notSecure = 0U,
                                             .notGlob = 0U,
                                             .notExec = 0U};

#if defined(__IAR_SYSTEMS_ICC__)
#pragma data_alignment = 16384
static uint32_t MMU_L1Table[4096];
#elif defined(__GNUC__)
static uint32_t MMU_L1Table[4096] __attribute__((aligned(16384)));
#else
#error Not supported compiler type
#endif


/*******************************************************************************
 * Code
 ******************************************************************************/
/* Initialize memory system (MMU). */
void BOARD_InitMemory(void)
{
    uint32_t i;

    MMU_Init(MMU_L1Table);

    MMU_ConfigSection(MMU_L1Table, (const void *)0x00000000U, 0x00000000U, &s_mmuRomAttr); /* ROM */
    MMU_ConfigSection(MMU_L1Table, (const void *)0x00900000U, 0x00900000U, &s_mmuRamAttr); /* OCRAM */
    MMU_ConfigSection(MMU_L1Table, (const void *)0x00A00000U, 0x00A00000U, &s_mmuDevAttr); /* GIC */
    MMU_ConfigSection(MMU_L1Table, (const void *)0x00B00000U, 0x00B00000U, &s_mmuDevAttr); /* GPV_0 PL301 */
    MMU_ConfigSection(MMU_L1Table, (const void *)0x00C00000U, 0x00C00000U, &s_mmuDevAttr); /* GPV_1 PL301 */
    MMU_ConfigSection(MMU_L1Table, (const void *)0x00D00000U, 0x00D00000U, &s_mmuDevAttr); /* cpu */
    MMU_ConfigSection(MMU_L1Table, (const void *)0x00E00000U, 0x00E00000U, &s_mmuDevAttr); /* per_m */
    MMU_ConfigSection(MMU_L1Table, (const void *)0x01800000U, 0x01800000U, &s_mmuDevAttr); /* APBH DMA */
    MMU_ConfigSection(MMU_L1Table, (const void *)0x02000000U, 0x02000000U, &s_mmuDevAttr); /* AIPS-1 */
    MMU_ConfigSection(MMU_L1Table, (const void *)0x02100000U, 0x02100000U, &s_mmuDevAttr); /* AIPS-2 */
    MMU_ConfigSection(MMU_L1Table, (const void *)0x02200000U, 0x02200000U, &s_mmuDevAttr); /* AIPS-3 */

    for (i = 0; i < 32; i++)
    {
        MMU_ConfigSection(MMU_L1Table, (const void *)(0x0C000000U + (i << 20)), (0x0C000000U + (i << 20)),
                          &s_mmuDevAttr); /* QSPI Rx Buf */
    }

    for (i = 0; i < 256; i++)
    {
        MMU_ConfigSection(MMU_L1Table, (const void *)(0x50000000U + (i << 20)), (0x50000000U + (i << 20)),
                          &s_mmuRamAttr); /* EIM */
    }

    for (i = 0; i < 256; i++)
    {
        MMU_ConfigSection(MMU_L1Table, (const void *)(0x60000000U + (i << 20)), (0x60000000U + (i << 20)),
                          &s_mmuRomAttr); /* QSPI */
    }

    for (i = 0; i < 2048; i++)
    {
        MMU_ConfigSection(MMU_L1Table, (const void *)(0x80000000U + (i << 20)), (0x80000000U + (i << 20)),
                          &s_mmuRamAttr); /* DDR */
    }

    MMU_Enable();
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2025-11-21 21:03 , Processed in 0.038698 second(s), 25 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表