查看: 2898|回复: 0

[EVAL-WSN]操作系统篇——Contiki移植分析

[复制链接]
  • TA的每日心情
    奋斗
    2023-7-8 16:17
  • 签到天数: 971 天

    连续签到: 1 天

    [LV.10]以坛为家III

    发表于 2016-1-9 10:05:18 | 显示全部楼层 |阅读模式
    分享到:
    Contiki 是一个小型的,开源的,极易移植的多任务操作系统。它专门设计以适用于一系列的内存优先的网络系统,包括从8位电脑到微型控制器的嵌入系统。它的名字来自于托尔·海尔达尔的康提基号。Contiki只需几kilobyte的代码和几百字节的内存就能提供多任务环境和内建TCP/IP支持
    Contiki的采用事件驱动机制,那么如何才能够产生“事件“呢。答案只有两个:第一,通过时钟定时,定时事件到就产生一个事件;第二,通过某种中断,某个中断发生,就产生某个事件例如外部中断。那么移植contiki到底要做哪些工作呢。
    Contiki的移植很简单,由于contiki是非抢占的操作系统,所以移植时并不需要PendSV中保存上下文。只需要提高一个时钟周期给contiki的以产品定时事件
    源代码下载地址 :github:https://github.com/contiki
    打开Contiki源文件目录,可以看到主要有apps、core、cpu、doc、examples、platform、tools等目录。下面将分别对各个目录进行介绍。
            core
            core目录下是Contiki的核心源代码,包括网络(net)、文件系统(cfs)、外部设备(dev)、链接库(lib)等等,并且包含了时钟、I/O、ELF装载器、网络驱动等的抽象。
            cpu
            cpu目录下是Contiki目前支持的微处理器,例如arm、avr、msp430等等。如果需要支持新的微处理器,可以在这里添加相应的源代码。
            platform
            platform目录下是Contiki支持的硬件平台,例如mx231cc、micaz、sky、win32等等。Contiki的平台移植主要在这个目录下完成。这一部分的代码与相应的硬件平台相关。
            apps
            apps目录下是一些应用程序,例如ftp、shell、webserver等等,在项目程序开发过程中可以直接使用。使用这些应用程序的方式为,在项目的Makefile中,定义APPS = [应用程序名称]。在以后的示例中会具体看到如何使用apps。
            examples
            examples目录下是针对不同平台的示例程序。Smeshlink的示例程序也在其中。
            doc
            doc目录是Contiki帮助文档目录,对Contiki应用程序开发很有参考价值。使用前需要先用Doxygen进行编译。
            tools
            tools目录下是开发过程中常用的一些工具,例如CFS相关的makefsdata、网络相关的tunslip、模拟器cooja和mspsim等等。
            为了获得良好的可移植性,除了cpu和platform中的源代码与硬件平台相关以外,其他目录中的源代码都尽可能与硬件无关。编译时,根据指定的平台来链接对应的代码。


    移植分析步骤如下:
    1)文件
    1.jpg
    2)头文件路径


    3)配置文件
    \AD6LoWPAN\contiki-2.5\platform\aducrf101\contiki-conf.h
    \AD6LoWPAN\contiki-2.5\platform\aducrf101\clock.c
    2.png
    \AD6LoWPAN\contiki-2.5\platform\aducrf101\contiki-conf.h
    1. typedef uint8_t u8_t;
    2. typedef uint16_t u16_t;
    3. typedef uint32_t u32_t;

    4. typedef int8_t s8_t;
    5. typedef int16_t s16_t;
    6. typedef int32_t s32_t;

    7. typedef unsigned int clock_time_t;
    8. typedef unsigned int uip_stats_t;

    9. typedef unsigned long off_t;

    10. void clock_delay(unsigned int us2);
    11. void clock_wait(int ms10);
    12. void clock_set_seconds(unsigned long s);
    13. unsigned long clock_seconds(void);

    14. extern uint8_t get_node_id();
    15. extern uint8_t get_host_id();
    16. extern uint16_t get_src_pan_id();
    17. extern uint16_t get_dst_pan_id();
    18. extern uint8_t get_current_dtsn();
    19. extern void increament_and_store_dtsn();
    20. extern void store_prefix();
    复制代码
    \AD6LoWPAN\contiki-2.5\platform\aducrf101\clock.c
    只需要修改一个
    void clock_init()
    void GP_Tmr1_Int_Handler(void) 中断相关的部分即可
    1. /**
    2. * \file
    3. *         Clock implementation for ADucRF101.
    4. * \author
    5. *         Analog Devices
    6. */

    7. #include "sys/clock.h"
    8. #include "sys/etimer.h"

    9. #include "device.h"

    10. #include "gpt.h"

    11. static volatile clock_time_t current_clock = 0;
    12. static volatile unsigned long current_seconds = 0;
    13. static unsigned int second_countdown = CLOCK_SECOND;


    14. /*---------------------------------------------------------------------------*/
    15. /* ADucRF101 general purpose Timer 1 interrupt handler */
    16. void
    17. GP_Tmr1_Int_Handler(void)
    18. {
    19.         ((ADI_TIMER_TypeDef *)ADI_TM1_ADDR)->CLRI = TCLRI_TMOUT_CLR;//中断函数清除中断

    20.         current_clock++;

    21.         if(etimer_pending() && etimer_next_expiration_time() <= current_clock) {
    22.           etimer_request_poll();
    23.         }

    24.         if (--second_countdown == 0) {
    25.           current_seconds++;
    26.           second_countdown = CLOCK_SECOND;               
    27.         }
    28. }
    29. /*---------------------------------------------------------------------------*/
    30. void
    31. clock_init()
    32. {
    33.     ADI_GPT_HANDLE hTimer;
    34.     ADI_GPT_RESULT_TYPE result;

    35.     result = adi_GPT_Init(ADI_GPT_DEVID_1, &hTimer );

    36.     if (ADI_GPT_SUCCESS == result)
    37.         result = adi_GPT_SetPrescaler(hTimer, ADI_GPT_PRESCALER_16);

    38.     if (ADI_GPT_SUCCESS == result)
    39.         result = adi_GPT_SetClockSelect(hTimer, ADI_GPT_CLOCK_SELECT_PCLK);

    40.     if (ADI_GPT_SUCCESS == result)
    41.         result = adi_GPT_SetPeriodicMode( hTimer, true, 1000);

    42.     if (ADI_GPT_SUCCESS == result)
    43.         result = adi_GPT_SetCountMode(hTimer, ADI_GPT_COUNT_DOWN);

    44.     if (ADI_GPT_SUCCESS == result)
    45.         adi_GPT_SetTimerEnable(hTimer, true);
    46. }

    47. /*---------------------------------------------------------------------------*/
    48. clock_time_t
    49. clock_time(void)
    50. {
    51.         return current_clock;
    52. }
    53. /*---------------------------------------------------------------------------*/
    54. unsigned long
    55. clock_seconds(void)
    56. {
    57.         return current_seconds;
    58. }
    59. /*---------------------------------------------------------------------------*/
    60. void
    61. clock_delay(unsigned int d)
    62. {
    63.   /* Does not do anything. */
    64. }
    65. /*---------------------------------------------------------------------------*/
    66. void
    67. clock_time_update ( uint32_t count )
    68. {
    69.   current_clock += ( count*CLOCK_SECOND );
    70.   current_seconds += count;
    71. }

    72. void
    73. clock_time_update_ms ( uint32_t count )
    74. {
    75.     while(count)
    76.     {
    77.       current_clock ++;
    78.       
    79.       if (--second_countdown == 0) {
    80.         current_seconds++;
    81.         second_countdown = CLOCK_SECOND;               
    82.       }
    83.       count--;
    84.     }
    85. }
    复制代码
    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条

    手机版|小黑屋|与非网

    GMT+8, 2024-4-17 04:10 , Processed in 0.101410 second(s), 15 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.