litos工程添加自定义demo_liteos增加自定义的系统调用-程序员宅基地

技术标签: C语言  linux  Litos  

Litos

这篇文章主要描述我们如何在litos工程中,自定义一个demo出来。



前言

1.环境及MCU 小熊派(IOT开发板)+linux环境+litos


提示:以下是本篇文章正文内容,下面案例可供参考

一、前期准备

1.如果你想完成自定义demo,首先你需要去看一下其它demo中是如何定义这个demo的。比如说Kconfig,makefile,需要什么文件等等。
2.你需要对makefile,Kconfig这种文件语法懂一些。
3.自己要会编译下载。(不会的话,参考我的一篇环境搭建的文章,里面包含了如何下载程序)

二、使用步骤

1.查看其它demos文件

在这里插入图片描述

  • 建议你用source insight建立一个工程,查看方便。
  • 上述图片是我写好的调用cjson库的demo,后面我就以这个讲解
  • json-c.mk文件是增加一个当前的头文件路径
DEMOS_JSON_INCLUDE := \
    -I $(LITEOSTOPDIR)/demos/utility/cjson
  • Kconfig文件是增加一个编译选项
config LOSCFG_DEMOS_CJSON
    bool "Enable cJson Demo"
    default n
    help
      Answer y to enable json demo.

  • Makefile 提供一些编译文件名,统计头文件路径称等等,可以细细看看
include $(LITEOSTOPDIR)/config.mk
include $(LITEOSTOPDIR)/demos/utility/cjson/json-c.mk

MODULE_NAME := cjson_demo

JSON_DEMO_SRCS = $(wildcard *.c)

JSON_DEMO_INCLUDE := \
    -I $(LITEOSTOPDIR)/lib/libc/include \
    -I $(LITEOSTOPDIR)/components/lib/cjson/$(CJSON_VERSION)\
    -I $(LITEOSTOPDIR)/demos/utility/cjson

LOCAL_INCLUDE := $(JSON_DEMO_INCLUDE) $(COMPONENTS_JSON_INCLUDE) 

LOCAL_SRCS := $(subst $(CURDIR)/,, $(JSON_DEMO_SRCS))

LOCAL_EXT_FLAG := -Wno-error -fstack-protector-strong

LOCAL_FLAGS := $(LOCAL_EXT_FLAG) $(LOCAL_INCLUDE)

include $(MODULE)

2.修改上层目录的Kconfig和mk

(1)找到utility目录下的Makefile
在这里插入图片描述

(2)Makefile添加这条数据

在这里插入图片描述
(3)同理,此目录中找到Kconfig文件,并添加如下箭头数据
在这里插入图片描述
(3)找到utility.mk文件,添加如下箭头数据
在这里插入图片描述

3.执行make menuconfig选择库和新加demo

(1)使能cjson库
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
(2)使能cjson_demo
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

4.修改cjson_demo.c和.h文件

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cJSON.h"
#include "los_task.h"

#define CJSON_TASK_PRIORITY        3
#define CJSON_TASK_STACK_SIZE     0x1000
STATIC UINT32 g_demoTaskId;

/* Used by some code below as an example datatype. */
struct record
{
    
    const char *precision;
    double lat;
    double lon;
    const char *address;
    const char *city;
    const char *state;
    const char *zip;
    const char *country;
};

/* Create a bunch of objects as demonstration. */
static int print_preallocated(cJSON *root)
{
    
    /* declarations */
    char *out = NULL;
    char *buf = NULL;
    char *buf_fail = NULL;
    size_t len = 0;
    size_t len_fail = 0;

    /* formatted print */
    out = cJSON_Print(root);

    /* create buffer to succeed */
    /* the extra 5 bytes are because of inaccuracies when reserving memory */
    len = strlen(out) + 5;
    buf = (char*)malloc(len);
    if (buf == NULL)
    {
    
        printf("Failed to allocate memory.\n");
        exit(1);
    }

    /* create buffer to fail */
    len_fail = strlen(out);
    buf_fail = (char*)malloc(len_fail);
    if (buf_fail == NULL)
    {
    
        printf("Failed to allocate memory.\n");
        exit(1);
    }

    /* Print to buffer */
    if (!cJSON_PrintPreallocated(root, buf, (int)len, 1)) {
    
        printf("cJSON_PrintPreallocated failed!\n");
        if (strcmp(out, buf) != 0) {
    
            printf("cJSON_PrintPreallocated not the same as cJSON_Print!\n");
            printf("cJSON_Print result:\n%s\n", out);
            printf("cJSON_PrintPreallocated result:\n%s\n", buf);
        }
        free(out);
        free(buf_fail);
        free(buf);
        return -1;
    }

    /* success */
    printf("%s\n", buf);

    /* force it to fail */
    if (cJSON_PrintPreallocated(root, buf_fail, (int)len_fail, 1)) {
    
        printf("cJSON_PrintPreallocated failed to show error with insufficient memory!\n");
        printf("cJSON_Print result:\n%s\n", out);
        printf("cJSON_PrintPreallocated result:\n%s\n", buf_fail);
        free(out);
        free(buf_fail);
        free(buf);
        return -1;
    }

    free(out);
    free(buf_fail);
    free(buf);
    return 0;
}

/* Create a bunch of objects as demonstration. */
static void create_objects(void)
{
    
    /* declare a few. */
    cJSON *root = NULL;
    cJSON *fmt = NULL;
    cJSON *img = NULL;
    cJSON *thm = NULL;
    cJSON *fld = NULL;
    int i = 0;

    /* Our "days of the week" array: */
    const char *strings[7] =
    {
    
        "Sunday",
        "Monday",
        "Tuesday",
        "Wednesday",
        "Thursday",
        "Friday",
        "Saturday"
    };
    /* Our matrix: */
    int numbers[3][3] =
    {
    
        {
    0, -1, 0},
        {
    1, 0, 0},
        {
    0 ,0, 1}
    };
    /* Our "gallery" item: */
    int ids[4] = {
     116, 943, 234, 38793 };
    /* Our array of "records": */
    struct record fields[2] =
    {
    
        {
    
            "zip",
            37.7668,
            -1.223959e+2,
            "",
            "SAN FRANCISCO",
            "CA",
            "94107",
            "US"
        },
        {
    
            "zip",
            37.371991,
            -1.22026e+2,
            "",
            "SUNNYVALE",
            "CA",
            "94085",
            "US"
        }
    };
    volatile double zero = 0.0;
	 
    /* Here we construct some JSON standards, from the JSON site. */
    printf("Version: %s\n", cJSON_Version());
    /* Our "Video" datatype: */
    root = cJSON_CreateObject();
    cJSON_AddItemToObject(root, "name", cJSON_CreateString("Jack (\"Bee\") Nimble"));
    cJSON_AddItemToObject(root, "format", fmt = cJSON_CreateObject());
    cJSON_AddStringToObject(fmt, "type", "rect");
    cJSON_AddNumberToObject(fmt, "width", 1920);
    cJSON_AddNumberToObject(fmt, "height", 1080);
    cJSON_AddFalseToObject (fmt, "interlace");
    cJSON_AddNumberToObject(fmt, "frame rate", 24);
    
    /* Print to text */
    if (print_preallocated(root) != 0) {
    
        cJSON_Delete(root);
        exit(EXIT_FAILURE);
    }
    cJSON_Delete(root);
    printf("1333331111\n");
    /* Our "days of the week" array: */
    root = cJSON_CreateStringArray(strings, 7);

    if (print_preallocated(root) != 0) {
    
        cJSON_Delete(root);
        exit(EXIT_FAILURE);
    }
    cJSON_Delete(root);

    /* Our matrix: */
    root = cJSON_CreateArray();
    for (i = 0; i < 3; i++)
    {
    
        cJSON_AddItemToArray(root, cJSON_CreateIntArray(numbers[i], 3));
    }

    /* cJSON_ReplaceItemInArray(root, 1, cJSON_CreateString("Replacement")); */

    if (print_preallocated(root) != 0) {
    
        cJSON_Delete(root);
        exit(EXIT_FAILURE);
    }
    cJSON_Delete(root);

    /* Our "gallery" item: */
    root = cJSON_CreateObject();
    cJSON_AddItemToObject(root, "Image", img = cJSON_CreateObject());
    cJSON_AddNumberToObject(img, "Width", 800);
    cJSON_AddNumberToObject(img, "Height", 600);
    cJSON_AddStringToObject(img, "Title", "View from 15th Floor");
    cJSON_AddItemToObject(img, "Thumbnail", thm = cJSON_CreateObject());
    cJSON_AddStringToObject(thm, "Url", "http:/*www.example.com/image/481989943");
    cJSON_AddNumberToObject(thm, "Height", 125);
    cJSON_AddStringToObject(thm, "Width", "100");
    cJSON_AddItemToObject(img, "IDs", cJSON_CreateIntArray(ids, 4));

    if (print_preallocated(root) != 0) {
    
        cJSON_Delete(root);
        exit(EXIT_FAILURE);
    }
    cJSON_Delete(root);

    /* Our array of "records": */
    root = cJSON_CreateArray();
    for (i = 0; i < 2; i++)
    {
    
        cJSON_AddItemToArray(root, fld = cJSON_CreateObject());
        cJSON_AddStringToObject(fld, "precision", fields[i].precision);
        cJSON_AddNumberToObject(fld, "Latitude", fields[i].lat);
        cJSON_AddNumberToObject(fld, "Longitude", fields[i].lon);
        cJSON_AddStringToObject(fld, "Address", fields[i].address);
        cJSON_AddStringToObject(fld, "City", fields[i].city);
        cJSON_AddStringToObject(fld, "State", fields[i].state);
        cJSON_AddStringToObject(fld, "Zip", fields[i].zip);
        cJSON_AddStringToObject(fld, "Country", fields[i].country);
    }

    /* cJSON_ReplaceItemInObject(cJSON_GetArrayItem(root, 1), "City", cJSON_CreateIntArray(ids, 4)); */

    if (print_preallocated(root) != 0) {
    
        cJSON_Delete(root);
        exit(EXIT_FAILURE);
    }
    cJSON_Delete(root);

    root = cJSON_CreateObject();
    cJSON_AddNumberToObject(root, "number", 1.0 / zero);

    if (print_preallocated(root) != 0) {
    
        cJSON_Delete(root);
        exit(EXIT_FAILURE);
    }
    cJSON_Delete(root);
}

int CJSON_CDECL cjson_test(void)
{
    
    /* print the version */
	UINT32 ret;
    TSK_INIT_PARAM_S taskInitParam = {
    0};
    taskInitParam.usTaskPrio = CJSON_TASK_PRIORITY;
    taskInitParam.pcName = "CJSONTask";
    taskInitParam.pfnTaskEntry = (TSK_ENTRY_FUNC)create_objects;
    taskInitParam.uwStackSize = CJSON_TASK_STACK_SIZE;
    taskInitParam.uwResved = LOS_TASK_STATUS_DETACHED;
    ret = LOS_TaskCreate(&g_demoTaskId, &taskInitParam);
    if (ret != LOS_OK) {
    
        printf("Create libevent demo task failed.\n");
    }
    /* Now some samplecode for building objects concisely: */
   // create_objects();

    return 0;
}
#ifndef _CJSON_DEMO_H
#define _CJSON_DEMO_H

#include "los_typedef.h"

#ifdef __cplusplus
#if __cplusplus
extern "C" {
    
#endif /* __cplusplus */
#endif /* __cplusplus */

int  cjson_test(void);


#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cplusplus */
#endif /* __cplusplus */

#endif /* _CJSON_DEMO_H */

5.修改demo_entry.c文件

在这里插入图片描述
添加头文件
在这里插入图片描述
添加调用demo的入口
在这里插入图片描述

6.执行Make编译

(1)编译后现象
在这里插入图片描述
(2)下载 ,不会下载的看我上一篇关于litos文章

7.程序运行现象

我只截取了一部分内容,如果有什么不会的,留言,我会回复。觉得ok,点个赞吧。
在这里插入图片描述


版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_50074254/article/details/123546474

智能推荐

mysql 3.23 注入_安全狗SQL注入、上传绕过-2017-3-23-程序员宅基地

文章浏览阅读64次。作者是谁不清楚,看到群里分享出来的,所以转载到博客了。-----收集整理 by: www.nvhack.com 安全狗SQL注入绕过 测试文件,明显的字符型SQL 注入 _mysql注入上传绕开分号

pytorch环境搭建_import pytorch as pytouch-程序员宅基地

文章浏览阅读311次。我也是个新手所以遇到的问题可能不全面,不过确实有很多的坑,写下来,后面再补充第一步:找到b站安装视频https://www.bilibili.com/video/BV1oz411v7cv?from=search&seid=12498239426358158910第二步:按照上述步骤下载anaconda 这里指出官网下载需要注册,而恰巧你没有梯子,那么,你可以去清华的镜像网站 https://mirrors.tuna.tsinghua.edu.cn/anacon..._import pytorch as pytouch

autodesk genuine service卸载不掉,找不到autodesk genuine service.msi 怎么办_autodeskgenuineservice为什么卸载不掉-程序员宅基地

文章浏览阅读1w次。尝试了很多网上的方法,反复提到找到 autodesk genuine service.msi 这个文件,但是电脑里没有这个文件只有autodesk genuine service.conf这个文件把他删了再去控制面板卸载就可以了_autodeskgenuineservice为什么卸载不掉

华为荣耀鸿蒙3.0安装谷歌Play商店,安装谷歌服务三件套GMS,Google_华为鸿蒙安装googleplay三件套-程序员宅基地

文章浏览阅读6.6w次,点赞12次,收藏102次。我们升级到鸿蒙3.0也面临着一个问题,那就是安装谷歌服务框架GMS谷歌play商店。在鸿蒙2.0的时候我们使用工具:华谷套件,列,X2,XS2。那么我们升级到鸿蒙3.0也面临着一个问题,那就是安装谷歌服务框架GMS谷歌play商店。在鸿蒙2.0的时候我们使用工具:华谷套件,同时配置电脑链接手机降级备份就可以安装Play商店。最新的好消息鸿蒙3.0也可以安装谷歌Play商店了,而且不需要使用电脑,只需要一个安卓APP:华谷套件,就可以轻松地安装。同时我找了几款其他的机型,比如mate 40系列都是可以安装的。_华为鸿蒙安装googleplay三件套

【Qt】ubuntu下Qt开发环境的搭建_ubuntu qt环境搭建-程序员宅基地

文章浏览阅读3.4k次,点赞4次,收藏25次。在打开示例工程的过程中,由于示例工程的源码是安装到根目录(/opt/)下的,所以不具有写权限,这时候QtCreator会弹窗提示,我们可以选择将其复制到家目录下自定义的目录中即可。如果是新安装的ubuntu操作系统,需将软件包源更换为国内对应ubuntu版本下的源,方便软件包的安装。注意如果不是以sudo进行安装,则不能在根目录(/)下创建目录,Qt默认安装路径目录也不是在。上图红框中是必须选择的,其他的组件就根据自己的实际需要选择啦,此处我选择了。方式进行安装,或者根据具体的依赖提示进行处理。......_ubuntu qt环境搭建

为什么要malloc()?何时要malloc()?如何使用malloc()?_为什么要使用malloc-程序员宅基地

文章浏览阅读604次。函数原型:void *malloc(unsigned int num_bytes);  //分配长度为num_bytes字节的内存块返回值是void指针,void* 表示未确定类型的指针,void *可以指向任何类型的数据,更明确的说是指申请内存空间时还不知道用户是用这段空间来存储什么类型的数据(比如是char还是int或者其他数据类型),可以通过类型强制转化转化为其他任意类型指针。如果分配成功..._为什么要使用malloc

随便推点

CMake笔记(1)简单语法_cmake if not-程序员宅基地

文章浏览阅读260次。简单语法注释#命令格式COMMAND(参数1 参数2 ...)分隔符A;B;C D #用分号或者空格分割变量set(VAR a b c) 设置变量VAR command(${VAR}) 引用变量VAR的值,等价于command(a b c) command("${VAR}") 等价于 command("a b c") command("/..._cmake if not

Ubuntu18.04安装QGC_ubuntu18.04 安装qgc-程序员宅基地

文章浏览阅读3.2k次,点赞6次,收藏41次。QGC下载下载地址:https://github.com/mavlink/qgroundcontrol/releases进入网站后找到自己需要的版本,我自己使用的为3.5.2和4.1.2后缀.exe为window的安装包,这里我们下载后缀.Applmage和.tar.bz2。安装QGC下载完成后解压tar.bz2 tar -xjf QGroundControl.tar.bz2安装相关依赖sudo apt-get install espeak libespeak-dev libudev-d_ubuntu18.04 安装qgc

好未来---锁定广东卫视,听大师分享人生中的“顿悟时刻”_卢勤 杨永信-程序员宅基地

文章浏览阅读540次。 锁定广东卫视,听大师分享人生中的“顿悟时刻” 你是否还记得,成长路上那些至关重要的时刻?那些忽然醒悟的瞬间?这便是“顿悟”的力量。成长路途中的“顿悟”,离不开师长的点拨。它可能会是一个良好的习惯,会是一份持之以恒的毅力,也会是优秀的品格和综合素养。 ..._卢勤 杨永信

面向对象设计大作业——火车售票系统_铁路售票系统用例图购票用例规约-程序员宅基地

文章浏览阅读744次。用户注册登录:按始发地、目的地查询购票:按车次号查询购票:查看已购车票,退票,注销:管理员登录,查看所有用户信息:录入车次:删除车次:修改车次信息:查看所有车次:管理员注销:点击运行火车售票系统软件,用户输入用户名和密码,或者直接注册一个账号。登录成功后进入用户界面,可以选择按始发地、目的地或者车次号查询购票,点击用户信息里面的已购车票,可以查看已经购买的车票和进行退票,操作完成后进行注销。管理员输入唯一的账号进行登录,可以点击功能,查看所有用户信息或者注销返回登录界面。管理员可以录入车次,修改车次信息,删_铁路售票系统用例图购票用例规约

Dymola多学科系统仿真平台-程序员宅基地

文章浏览阅读279次。Dymola是法国Dassault Systems(达索)公司的多学科系统快速设计和验证工具,广泛应用于国内外汽车、交通、能源等行业的系统总体架构设计、选型及匹配验证、系统优化、控制系统MIL/HIL验证等。_dymola

mac error 2015-10-08-程序员宅基地

文章浏览阅读310次。Anonymous UUID: 9E5F7DE8-3A83-2978-8AC0-2FD1C1DC1171Thu Oct 8 23:36:14 2015*** Panic Report ***panic(cpu 0 caller 0xffffff8000816df2): Kernel trap at 0xffffff8000928ba2, type 14=pag..._fault cr2 fault cpu

推荐文章

热门文章

相关标签