sonicfirr 发表于 2022-4-16 10:15

【平头哥RVB2601创意应用开发】使用体验08 -- RTC应用

本帖最后由 sonicfirr 于 2022-4-16 10:22 编辑

<p style="text-indent:24.0000pt; text-align:justify"><span style="font-size:12pt"><span style="125%"><span style="font-family:&quot;Times New Roman&quot;"><span style="font-size:12.0000pt"><span style="font-family:宋体"><font face="Times New Roman">NTP</font><font face="宋体">授时成功后,创意项目继续启用</font><font face="Times New Roman">RTC</font><font face="宋体">,利用</font><font face="Times New Roman">NTP</font><font face="宋体">获取的时间初始化</font><font face="Times New Roman">RTC</font><font face="宋体">,以便在系统运行期间可以更快速的获得本地时间。</font></span></span></span></span></span></p>

<h2 style="text-align:justify"><span style="font-size:14pt"><span style="125%"><span style="font-family:&quot;Times New Roman&quot;"><span style="font-weight:bold"><b><span style="font-size:14.0000pt"><span style="font-family:宋体"><span style="font-weight:bold"><font face="Times New Roman">1</font><font face="宋体">、</font><font face="Times New Roman">CSI2</font><font face="宋体">接口</font></span></span></span></b></span></span></span></span></h2>

<p style="text-indent:24.0000pt; text-align:justify"><span style="font-size:12pt"><span style="125%"><span style="font-family:&quot;Times New Roman&quot;"><span style="font-size:12.0000pt"><span style="font-family:宋体"><font face="宋体">在</font><font face="Times New Roman">AOS API</font><font face="宋体">(</font><font face="Times New Roman">https://yoc.docs.t-head.cn/yocbook/Chapter3-AliOS/</font><font face="宋体">)中包含两套设备驱动接口</font><font face="Times New Roman">CSI2</font><font face="宋体">和</font><font face="Times New Roman">CSI1</font><font face="宋体">,通过对</font><font face="Times New Roman">RVB2601</font><font face="宋体">的</font><font face="Times New Roman">SDK</font><font face="宋体">查看,发现板子实现的是</font><font face="Times New Roman">CSI2</font><font face="宋体">接口。</font></span></span></span></span></span></p>

<p>&nbsp;</p>

<p class="imagemiddle" style="text-align: center;"></p>

<p>&nbsp;</p>

<p align="center" style="text-align:center"></p>

<p align="center" style="text-align:center">&nbsp;</p>

<p class="imagemiddle" style="text-align: center;"></p>

<p align="center" style="text-align:center"><span style="font-size:12pt"><span style="125%"><span style="font-family:&quot;Times New Roman&quot;"><span style="font-size:10.5000pt"><span style="font-family:宋体"><font face="宋体">图</font><font face="Times New Roman">8-1 </font><font face="宋体">文档中的</font><font face="Times New Roman">CSI2</font><font face="宋体">接口和</font><font face="Times New Roman">SDK</font><font face="宋体">中的</font><font face="Times New Roman">RTC</font><font face="宋体">实现</font></span></span></span></span></span></p>

<p>&nbsp;</p>

<p style="text-indent:24.0000pt; text-align:justify"><span style="font-size:12pt"><span style="125%"><span style="font-family:&quot;Times New Roman&quot;"><span style="font-size:12.0000pt"><span style="font-family:宋体"><font face="宋体">依据文档说明,本项目使用了</font><font face="Times New Roman">RTC</font><font face="宋体">的三个功能:初始化</font><font face="Times New Roman">csi_rtc_init()</font><font face="宋体">,设置时间</font><font face="Times New Roman">csi_rtc_set_time()</font><font face="宋体">和获取时间</font><font face="Times New Roman">csi_rtc_get_time()</font><font face="宋体">。</font></span></span></span></span></span></p>

<p>&nbsp;</p>

<p class="imagemiddle" style="text-align: center;"></p>

<p align="center" style="text-align:center">&nbsp;<span style="font-size:12pt"><span style="125%"><span style="font-family:&quot;Times New Roman&quot;"><span style="font-size:10.5000pt"><span style="font-family:宋体"><font face="宋体">图</font><font face="Times New Roman">8-2 </font><font face="宋体">文档中的</font><font face="Times New Roman">csi_rtc_init()</font><font face="宋体">描述</font></span></span></span></span></span></p>

<p>&nbsp;</p>

<p class="imagemiddle" style="text-align: center;"></p>

<p align="center" style="text-align:center"><span style="font-size:12pt"><span style="125%"><span style="font-family:&quot;Times New Roman&quot;"><span style="font-size:10.5000pt"><span style="font-family:宋体"><font face="宋体">图</font><font face="Times New Roman">8-3 </font><font face="宋体">文档中的</font><font face="Times New Roman">csi_rtc_set_time()</font><font face="宋体">描述</font></span></span></span></span></span></p>

<p>&nbsp;</p>

<p class="imagemiddle" style="text-align: center;"></p>

<p align="center" style="text-align:center"><span style="font-size:12pt"><span style="125%"><span style="font-family:&quot;Times New Roman&quot;"><span style="font-size:10.5000pt"><span style="font-family:宋体"><font face="宋体">图</font><font face="Times New Roman">8-4 </font><font face="宋体">文档中的</font><font face="Times New Roman">csi_rtc_get_time()</font><font face="宋体">描述</font></span></span></span></span></span></p>

<p>&nbsp;</p>

<p><span style="font-size:14pt"><span style="125%"><span style="font-family:&quot;Times New Roman&quot;"><span style="font-weight:bold"><b><span style="font-size:14.0000pt"><span style="font-family:宋体"><span style="font-weight:bold"><font face="Times New Roman">2</font><font face="宋体">、</font><font face="Times New Roman">newlib</font><font face="宋体">包中的</font><font face="Times New Roman">time</font><font face="宋体">库</font></span></span></span></b></span></span></span></span></p>

<p style="text-indent:24.0000pt; text-align:justify"><span style="font-size:12pt"><span style="125%"><span style="font-family:&quot;Times New Roman&quot;"><span style="font-size:12.0000pt"><span style="font-family:宋体"><font face="Times New Roman">RVB2601</font><font face="宋体">的</font><font face="Times New Roman">SDK</font><font face="宋体">中,</font><font face="Times New Roman">newlib</font><font face="宋体">包提供了</font><font face="Times New Roman">C</font><font face="宋体">标准库。说来惭愧,本人搞了多年嵌入式,</font><font face="Times New Roman">C</font><font face="宋体">语言是主要生产工具,但是</font><font face="Times New Roman">C</font><font face="宋体">标准库却是比较陌生,尤其是</font><font face="Times New Roman">time</font><font face="宋体">库,因此这里也走了一点弯路(因为不熟悉,也搞不清楚是不是</font><font face="Times New Roman">bug</font><font face="宋体">,所以不敢称为&ldquo;踩坑&rdquo;)。</font></span></span></span></span></span></p>

<p style="text-indent:24.0000pt; text-align:justify"><span style="font-size:12pt"><span style="125%"><span style="font-family:&quot;Times New Roman&quot;"><span style="font-size:12.0000pt"><span style="font-family:宋体"><font face="宋体">之前修正的</font><font face="Times New Roman">NTP</font><font face="宋体">功能,有获取网络时间并调用</font><font face="Times New Roman">settimeofday()</font><font face="宋体">的语句,通过网上检索了解到这是设置系统时间戳的函数。还有</font><font face="Times New Roman">gettimeofday()</font><font face="宋体">则是获取系统时间,于是想在</font><font face="Times New Roman">ntp_sync_time()</font><font face="宋体">调用后,通过</font><font face="Times New Roman">gettimeofday()</font><font face="宋体">即获得本地时间。但是,这样操作得到的时间是错误的,于是又联想到向平头哥反馈</font><font face="Times New Roman">NTP bug</font><font face="宋体">时,技术小哥给了一个官方修正</font><font face="Times New Roman">NTP bug</font><font face="宋体">的操作文档,其中包含有对</font><font face="Times New Roman">time</font><font face="宋体">库的修改。所以,按照文档修改了一下</font><font face="Times New Roman">SDK</font><font face="宋体">,结果依然不成功。</font></span></span></span></span></span></p>

<p style="text-indent:24.0000pt; text-align:justify"><span style="font-size:12pt"><span style="125%"><span style="font-family:&quot;Times New Roman&quot;"><span style="font-size:12.0000pt"><span style="font-family:宋体"><font face="宋体">所以,最后放弃了使用</font><font face="Times New Roman">gettimeofday()</font><font face="宋体">的思路,当然</font><font face="Times New Roman">time</font><font face="宋体">库中还是用到了几个</font><font face="Times New Roman">API</font><font face="宋体">,可以更方便的进行时间格式转换。</font><font face="Times New Roman">API</font><font face="宋体">具体有:类型重定义</font><font face="Times New Roman">time_t</font><font face="宋体">、结构体</font><font face="Times New Roman">struct tm</font><font face="宋体">、本地时间函数</font><font face="Times New Roman">localtime()</font><font face="宋体">和时间格式化函数</font><font face="Times New Roman">strftime()</font><font face="宋体">。</font></span></span></span></span></span></p>

<p style="text-indent:24.0000pt; text-align:justify"><span style="font-size:12pt"><span style="125%"><span style="font-family:&quot;Times New Roman&quot;"><span style="font-size:12.0000pt"><span style="font-family:宋体"><font face="宋体">数据结构类型定义和函数声明都位于</font>&ldquo;<font face="Times New Roman">../newlib/v7.4.3/include/time.h</font><font face="宋体">&rdquo;头文件中,具体如下。</font></span></span></span></span></span></p>

<p>&nbsp;</p>

<pre>
<code>//by author. omit other codes
typedef int32_t  time_t;         /* Holds time in seconds */

struct tm
{
  int tm_sec;     /* Seconds (0-61, allows for leap seconds) */
  int tm_min;     /* Minutes (0-59) */
  int tm_hour;    /* Hours (0-23) */
  int tm_mday;    /* Day of the month (1-31) */
  int tm_mon;     /* Month (0-11) */
  int tm_year;    /* Years since 1900 */
  int tm_wday;    /* Day of the week (0-6) */
  int tm_yday;    /* Day of the year (0-365) */
  int tm_isdst;   /* Non-0 if daylight savings time is in effect */
};

size_t strftime(char *s, size_t max, const char *format, const struct tm *tm);

inline struct tm *localtime(const time_t *tim_p) {return gmtime(tim_p);}</code></pre>

<p><span style="font-size:14pt"><span style="125%"><span style="font-family:&quot;Times New Roman&quot;"><span style="font-weight:bold"><b><span style="font-size:14.0000pt"><span style="font-family:宋体"><span style="font-weight:bold"><font face="Times New Roman">3</font><font face="宋体">、</font><font face="Times New Roman">RTC</font><font face="宋体">实现与</font><font face="Times New Roman">NTP</font><font face="宋体">修改</font></span></span></span></b></span></span></span></span></p>

<p style="text-indent:24.0000pt; text-align:justify"><span style="font-size:12pt"><span style="125%"><span style="font-family:&quot;Times New Roman&quot;"><span style="font-size:12.0000pt"><span style="font-family:宋体"><font face="宋体">本例的思路是:系统启动进行</font><font face="Times New Roman">RTC</font><font face="宋体">初始化和网络初始化,在获得</font><font face="Times New Roman">IP</font><font face="宋体">地址的事件回调中请求</font><font face="Times New Roman">NTP</font><font face="宋体">授时,并将收到的时间值设置到</font><font face="Times New Roman">RTC</font><font face="宋体">中,之后激活定时器每个一段时间读取</font><font face="Times New Roman">RTC</font><font face="宋体">并显示读取值。</font></span></span></span></span></span></p>

<p style="text-indent:24.0000pt; text-align:justify"><span style="font-size:12pt"><span style="125%"><span style="font-family:&quot;Times New Roman&quot;"><span style="font-size:12.0000pt"><span style="font-family:宋体"><font face="宋体">注意</font><font face="Times New Roman">1</font><font face="宋体">、</font><font face="Times New Roman">2</font><font face="宋体">节中展示的结构体</font><font face="Times New Roman">csi_rtc_time_t</font><font face="宋体">和</font><font face="Times New Roman">struct tm</font><font face="宋体">,两者相差一个成员,即</font><font face="Times New Roman">struct tm</font><font face="宋体">有分量&ldquo;</font><font face="Times New Roman">int tm_isdst</font><font face="宋体">&rdquo;,于是本例采用建立联合体来保存时间数据结构,以同时适配</font><font face="Times New Roman">CSI RTC</font><font face="宋体">接口和</font><font face="Times New Roman">time C</font><font face="宋体">库。</font></span></span></span></span></span></p>

<p style="text-indent:24.0000pt; text-align:justify"><span style="font-size:12pt"><span style="125%"><span style="font-family:&quot;Times New Roman&quot;"><span style="font-size:12.0000pt"><span style="font-family:宋体"><font face="宋体">另外,本例仿照</font><font face="Times New Roman">NTP</font><font face="宋体">的原</font><font face="Times New Roman">API</font><font face="宋体">,编写了可</font><font face="Times New Roman">NTP</font><font face="宋体">授时同时设置</font><font face="Times New Roman">RTC</font><font face="宋体">的函数。联合体类型定义和函数声明放在了&ldquo;</font><font face="Times New Roman">../ntp/v7.4.3/include/ntp.h</font><font face="宋体">&rdquo;中,其完整代码如下。</font></span></span></span></span></span></p>

<p>&nbsp;</p>

<pre>
<code>#ifndef __NTP_H__
#define __NTP_H__

#include &lt;drv/rtc.h&gt;

#ifdef __cplusplus
extern "C"
{
#endif

int ntp_sync_time(char *server);
int aita_ntp_sync_time(char *server); //by author. ntp sync &amp; set rtc function

typedef struct {
        csi_rtc_time_t rtc;
        int            isdst;
} aita_rtc_t;

typedef union {
        struct tm  libc_tm;
        aita_rtc_t rtc_tm;
} aita_tm_u;

extern aita_tm_u aita_dt;

#ifdef __cplusplus
}
#endif
#endif</code></pre>

<p>&nbsp;</p>

<p style="text-indent:24.0000pt; text-align:justify"><span style="font-size:12pt"><span style="125%"><span style="font-family:&quot;Times New Roman&quot;"><span style="font-size:12.0000pt"><span style="font-family:宋体"><font face="宋体">对应的</font><font face="Times New Roman">ntp.c</font><font face="宋体">,增加的代码如下。</font></span></span></span></span></span></p>

<p>&nbsp;</p>

<pre>
<code>#define AITA_LOCAL 8*3600
aita_tm_u aita_dt;
void aita_print_ntp(struct ntphdr *ntp) {
    time_t time;
//by author. get ntp time, set into rtc then show datetime once
    time = ntohl(ntp-&gt;ntp_recvts.intpart) - JAN_1970 + AITA_LOCAL;  //with timezone offset
        char tbuf;
        aita_dt.libc_tm = *(localtime(&amp;time));                          //set system datetime variable
        extern csi_rtc_t aita_rtc;                                      //rtc device descriptor
        csi_rtc_set_time(&amp;aita_rtc, &amp;(aita_dt.rtc_tm.rtc));             //set datetime variable into rtc
    strftime(tbuf, sizeof(tbuf), "%Y-%m-%d %T", &amp;(aita_dt.libc_tm));//datetime to string
        printf("aita datetime: %s\n", tbuf);
}

int aita_ntp_sync_time(char *server) {
    char               buf;
    size_t             nbytes;
    int                sockfd, maxfd1;
    struct sockaddr_in servaddr = {0,};
    fd_set             readfds;
    struct timeval     timeout, recvtv, tv, rcvtimeout = {3, 0};
    double             offset;

    servaddr.sin_family = AF_INET;
    servaddr.sin_port   = htons(NTP_PORT);

    if (server == NULL) {
        servaddr.sin_addr.s_addr = inet_host("ntp1.aliyun.com");
        LOGD(TAG, "ntp1.aliyun.com\t---------------------&gt; aita_ntp_sync_time()\n");
    } else {
        servaddr.sin_addr.s_addr = inet_host(server);
        LOGD(TAG, "%s\t---------------------&gt; aita_ntp_sync_time()\n", server);
    }

    if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) &lt; 0) {
        LOGE(TAG, "socket error\t---------------------&gt; aita_ntp_sync_time()\n");
        return -1;
    }
    setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &amp;rcvtimeout, sizeof(struct timeval));
    if (connect(sockfd, (struct sockaddr *)&amp;servaddr, sizeof(struct sockaddr)) != 0) {
        LOGE(TAG, "connect error\t---------------------&gt; aita_ntp_sync_time()\n");
        close(sockfd);
        return -errno;
    }

    nbytes = BUFSIZE;
    if (get_ntp_packet(buf, &amp;nbytes) != 0) {
        LOGE(TAG, "construct ntp request errorr\t---------------------&gt; aita_ntp_sync_time()\n");
        close(sockfd);
        return -1;
    }
    send(sockfd, buf, nbytes, 0);

    FD_ZERO(&amp;readfds);
    FD_SET(sockfd, &amp;readfds);
    maxfd1 = sockfd + 1;

    timeout.tv_sec  = TIMEOUT;
    timeout.tv_usec = 0;

    if (select(maxfd1, &amp;readfds, NULL, NULL, &amp;timeout) &gt; 0) {
        if (FD_ISSET(sockfd, &amp;readfds)) {
            if ((nbytes = recv(sockfd, buf, BUFSIZE, 0)) &lt; 0) {
                LOGE(TAG, "recv error\t---------------------&gt; aita_ntp_sync_time()\n");
                close(sockfd);
                return -1;
            }

            aita_print_ntp((struct ntphdr *) buf);
            gettimeofday(&amp;recvtv, NULL);
            offset = get_offset((struct ntphdr *)buf, &amp;recvtv);

            gettimeofday(&amp;tv, NULL);
            tv.tv_sec += (int)offset;
            tv.tv_usec += offset - (int)offset;

            if (settimeofday(&amp;tv, NULL) != 0) {
                LOGE(TAG, "set time error\t---------------------&gt; aita_ntp_sync_time()\n");
                close(sockfd);
                return -1;
            }
        }
    } else {
        close(sockfd);
                printf("select body error!\t---------------------&gt; aita_ntp_sync_time()\n");
        return -1;
    }
    close(sockfd);
        printf("whole done!\t---------------------&gt; aita_ntp_sync_time()\n");
    return 0;
}</code></pre>

<p>&nbsp;</p>

<p style="text-indent:24.0000pt; text-align:justify"><span style="font-size:12pt"><span style="125%"><span style="font-family:&quot;Times New Roman&quot;"><span style="font-size:12.0000pt"><span style="font-family:宋体"><font face="宋体">案例基于</font>&ldquo;<font face="Times New Roman">Hello World Demo</font><font face="宋体">&rdquo;,</font><font face="Times New Roman">init.c</font><font face="宋体">中定义相关初始化和功能函数,相较前几篇代码变动较多,这里直接给出全部代码。</font></span></span></span></span></span></p>

<p>&nbsp;</p>

<pre>
<code>#include &lt;stdbool.h&gt;
#include &lt;stdlib.h&gt;

#include &lt;aos/aos.h&gt;
#include &lt;aos/cli.h&gt;
#include &lt;aos/kv.h&gt;
#include &lt;yoc/yoc.h&gt;
#include &lt;yoc/partition.h&gt;
#include &lt;yoc/netmgr.h&gt;
#include &lt;yoc/netmgr_service.h&gt;
#include &lt;devices/devicelist.h&gt;
#include &lt;devices/w800.h&gt;
#include &lt;uservice/eventid.h&gt;
#include &lt;ntp.h&gt;

#include "board.h"
#include "app_init.h"
#include "drv/pin.h"
#include "drv/rtc.h"
#include "../w800_api.h"

const char *TAG = "INIT";

#ifndef CONSOLE_UART_IDX
#define CONSOLE_UART_IDX 0
#endif


/* --------------------by AITA Mr.Fei, global variables--------------------- */


void board_yoc_init()
{
    board_init();
        event_service_init(NULL); //initialize event service for network events
    console_init(CONSOLE_UART_IDX, 115200, 128);
    ulog_init();
    aos_set_log_level(AOS_LL_DEBUG);   
    LOGI(TAG, "Build:%s,%s\n",__DATE__, __TIME__);
        aita_InitRTC();       
       
    /* load partition &amp; init kv function */
        aita_InitKV();
       
        /* set cycle timer for polling job */
        aita_InitTimer();

        /* initialize wifi network */
        aita_InitNetwork();

    board_cli_init();
}


/* --------------------by AITA Mr.Fei, key-value functions------------------ */
//project's global variables
#define ENDIAN_LIMIT   32 //endian counts limit
char GID;            //gateway id
char EID;//endians' id
intendian_count = 1;    //actually endian counts
intperiod = 10;         //polling period -- minutes
inttimeout = 15;      //timeout of waiting USR220 resp -- seconds
intrepeat = 3;          //repeat times when timeout
//Init partition &amp; kv function
int aita_InitKV(void) {
        char buf = {0};
        int ret = -1;
       
        ret = partition_init();
        if (ret &gt; 0) {
                LOGI(TAG, "find %d partitions\n", ret);
        } else {               
                LOGE(TAG, "partition init failed\n");
                return ret;
        }
       
        ret = aos_kv_init("kv");
        if(ret == 0) {
                LOGE(TAG, "kv init done\n");
        } else {
                LOGE(TAG, "kv init failed\n");
                return -1;
        }
               
        ret = aos_kv_getstring("endian_count", &amp;buf, 10);
        if(ret &gt; 0) {
                endian_count = atoi(buf);
                endian_count = (endian_count&lt;ENDIAN_LIMIT) ? endian_count : ENDIAN_LIMIT;
                printf("endian_count: %d\n", endian_count);
                char eid = "EIDx";
                for(int i=0; i&lt;endian_count; i++) {
                        eid = i+0x30;
                        aos_kv_getstring(eid, EID, 9);
                        printf("EID No.%d : %s\n", i, EID);
                }       
        } else {
                LOGE(TAG, "Endian Count load failed\n");
                return ret;
        }
       
        ret = aos_kv_getstring("period", &amp;buf, 10);
        if(ret &gt; 0) {
                period = atoi(buf);
                printf("period: %d\n", period);
        } else {
                LOGE(TAG, "period load failed\n");
                return ret;
        }
        ret = aos_kv_getstring("timeout", &amp;buf, 10);
        if(ret &gt; 0) {
                timeout = atoi(buf);
                printf("timeout: %d\n", timeout);
        } else {
                LOGE(TAG, "timeout load failed\n");
                return ret;
        }
        ret = aos_kv_getstring("repeat", &amp;buf, 10);
        if(ret &gt; 0) {
                repeat = atoi(buf);
                printf("repeat: %d\n", repeat);
        } else {
                LOGE(TAG, "repeat load failed\n");
                return ret;
        }
        return 0;
}


/* --------------------by AITA Mr.Fei, timer functions---------------------- */
#define PERIOD_FACTOR1000
aos_timer_t polling_timer;//timer for polling
//timer callback
void pollingTimerCb(void *arg1, void *arg2) {
        aita_ShowRTC();
}
//Init and startup periodic timer for polling job
void aita_InitTimer(void) {
        int ret = -1;
        LOGI(TAG, "initialize timer for polling job!\n");
       
        ret = aos_timer_new_ext(&amp;polling_timer, pollingTimerCb, "args", period*PERIOD_FACTOR, 1, 0);
        if(ret != 0) {
                LOGE(TAG, "polling_timer create failed\n");
        }
}


/* --------------------by AITA Mr.Fei, network functions-------------------- */
//newwork macros
#define WIFI_SSID      "your ssid"
#define WIFI_PSW       "you password"
#define GW_IP          "192.168.31.77"
#define GW_PORT      1234
#define ONE_HTTP_IP    "183.230.40.33"
#define ONE_HTTP_PORT80

//netmgr_hdl_t actually is void *
netmgr_hdl_t app_netmgr_hdl; //net manager handler for w800
//network event callback function
static void network_event(uint32_t event_id, const void *param, void *context) {
    switch(event_id) {
                case EVENT_NETMGR_GOT_IP: {
                        LOGD(TAG, "EVENT_NETMGR_GOT_IP\n");
                        printf("start polling_timer. periodic: %d min\n", period);                       
                        aita_RequestNTP();
                        aos_timer_start(&amp;polling_timer);
                        break;
                }
                case EVENT_NETMGR_NET_DISCON: {
                        LOGD(TAG, "EVENT_NETMGR_NET_DISCON\n");
                        printf("ready to restart wifi connection...\n");
                        netmgr_reset(app_netmgr_hdl, 30);
                        break;
                }       
        }
}
//w800 input callback function
void aita_w800in_cb(int linkid, void *data, size_t len, char remote_ip, uint16_t remote_ports) {
        uint8_t* d;
        d = (uint8_t *)data;
        printf("linkid: %d\n", linkid);
        printf("len: %d\n", len);
        printf("remote_ip: %s\n", remote_ip);
        printf("remote_port: %d\n", remote_ports);
        printf("data:%s\n", d);
}
//network init: open w800,
void aita_InitNetwork(void) {
        /*
       * by AITA Mr.Fei
       * w800 interface struct
       */
    w800_wifi_param_t w800_param;
    w800_param.reset_pin      = PA21;
    w800_param.baud         = 1*1000000;
    w800_param.cs_pin         = PA15;
    w800_param.wakeup_pin   = PA25;
    w800_param.int_pin      = PA22;
    w800_param.channel_id   = 0;
    w800_param.buffer_size    = 4*1024;
        /*
       * by AITA Mr.Fei
       * from w800_devops.c(drv_wifi_at_w800 pack),
       * include w800_module_init(but there is no task, so create a new one) &amp; driver_register.
       */
    wifi_w800_register(NULL, &amp;w800_param);
        /*
       * by AITA Mr.Fei
       * register uservice for wifi,
       * init netmgr_uservice struct then return the pointer
       */
    app_netmgr_hdl = netmgr_dev_wifi_init();
    if(app_netmgr_hdl) {
                //init uservice for wifi
      utask_t *task = utask_new("netmgr", 2 * 1024, QUEUE_MSG_COUNT, AOS_DEFAULT_APP_PRI);
      netmgr_service_init(task);
                //join AP, WIFI_SSID &amp; WIFI_PSW both are Macro
      netmgr_config_wifi(app_netmgr_hdl, WIFI_SSID, 10, WIFI_PSW, 10);
                //init w800 AT parser
                w800_module_init(task, &amp;w800_param);

                //start uservice
      netmgr_start(app_netmgr_hdl);
                //subscribe events which is managed by netmgr uservice
      event_subscribe(EVENT_NETMGR_GOT_IP, network_event, NULL);
      event_subscribe(EVENT_NETMGR_NET_DISCON, network_event, NULL);
    }
}
//tcp client test func
void aita_tcpclienttest(void) {
        char buf[] = "hello from RVB2601 by W800\n";
       
        int ret = -1;       
        ret = w800_connect_remote(0, NET_TYPE_TCP_CLIENT, GW_IP, GW_PORT);
        if(ret &lt; 0) return;
       
        int stat;
        w800_get_status(&amp;stat);//&lt;D&gt;w800_api link status 2 已获取到ip
        LOGD(TAG, "connect status: %d\n", stat);

        w800_send_data(buf, sizeof(buf), 3000);
}
//ntp test func
void aita_RequestNTP(void) {       
        int ret = -1;
    for(int i = 0; i &lt; 2; i++) {
      ret = aita_ntp_sync_time("cn.pool.ntp.org");
      if (ret == 0) {
            LOGD(TAG, "sync success\n");
            break;
      }
    }

    if(ret &lt; 0) {
      LOGE(TAG, "sync error\n");
    }
       
        //register input event callback for w800 module
        w800_packet_input_cb_register(&amp;aita_w800in_cb);
}


/* --------------------by AITA Mr.Fei, rtc functions------------------------ */
csi_rtc_t aita_rtc;
void aita_InitRTC(void) {
        csi_error_t ret;
        ret = csi_rtc_init(&amp;aita_rtc, 0);
       
        if(ret == CSI_OK) printf("initialize RTC done!\n");
        else            printf("initialize RTC return: %d\n", ret);
}

void aita_ShowRTC(void) {
        csi_error_t ret;
        char tbuf;
        ret = csi_rtc_get_time(&amp;aita_rtc, &amp;(aita_dt.rtc_tm.rtc));
    strftime(tbuf, sizeof(tbuf), "%Y-%m-%d %T", &amp;(aita_dt.libc_tm));
        printf("show aita datetime in RTC: %s\n", tbuf);
}</code></pre>

<pre>

&nbsp;</pre>

<p class="imagemiddle" style="text-align: center;"></p>

<p align="center" style="text-align:center"><i><i>&nbsp;</i></i><span style="font-size:12pt"><span style="125%"><span style="font-family:&quot;Times New Roman&quot;"><span style="font-size:10.5000pt"><span style="font-family:宋体"><font face="宋体">图</font><font face="Times New Roman">8-5 RTC</font><font face="宋体">应用效果</font></span></span></span></span></span></p>

<p>&nbsp;</p>

<p>&nbsp;</p>

lugl4313820 发表于 2022-4-16 11:16

<p>好想借你的NTP用一下,这项目整得没想法了!可否<img height="48" src="https://bbs.eeworld.com.cn/static/editor/plugins/hkemoji/sticker/facebook/congra.gif" width="48" /></p>

sonicfirr 发表于 2022-4-16 11:53

lugl4313820 发表于 2022-4-16 11:16
好想借你的NTP用一下,这项目整得没想法了!可否

<p>别客气,咱就是为了开源来的</p>

UUC 发表于 2022-4-16 19:13

<p>有点疑问:图8-3&nbsp;文档中的csi_rtc_set_time()描述&nbsp; tm_mon 取值范围,为何不是&nbsp;</p>

wangerxian 发表于 2022-4-17 08:04

UUC 发表于 2022-4-16 19:13
有点疑问:图8-3&nbsp;文档中的csi_rtc_set_time()描述&nbsp; tm_mon 取值范围,为何不是&nbsp; ...

<p>有些RTC寄存器就是这么设计的,具体为什么得问官方了。不过这不影响RTC的使用。</p>

sonicfirr 发表于 2022-4-17 09:18

UUC 发表于 2022-4-16 19:13
有点疑问:图8-3&nbsp;文档中的csi_rtc_set_time()描述&nbsp; tm_mon 取值范围,为何不是&nbsp; ...

<p>我想这就是一种习惯吧,从0开始编号,C标准库中时间结构体struct&nbsp;tm也是从0编号月份。</p>

UUC 发表于 2022-4-17 12:58

wangerxian 发表于 2022-4-17 08:04
有些RTC寄存器就是这么设计的,具体为什么得问官方了。不过这不影响RTC的使用。

<p>好的,了解了,谢谢</p>

UUC 发表于 2022-4-17 12:59

sonicfirr 发表于 2022-4-17 09:18
我想这就是一种习惯吧,从0开始编号,C标准库中时间结构体struct&nbsp;tm也是从0编号月份。

<p>好的,了解了,谢谢</p>
页: [1]
查看完整版本: 【平头哥RVB2601创意应用开发】使用体验08 -- RTC应用