查看: 2176|回复: 0

Contiki——小型的,开源的,极易移植的多任务操作系统

[复制链接]

该用户从未签到

发表于 2016-2-25 09:21:19 | 显示全部楼层 |阅读模式
分享到:
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)文件


2)头文件路径


3)配置文件
\AD6LoWPAN\contiki-2.5\platform\aducrf101\contiki-conf.h
\AD6LoWPAN\contiki-2.5\platform\aducrf101\clock.c


\AD6LoWPAN\contiki-2.5\platform\aducrf101\contiki-conf.h

  • typedef uint8_t u8_t;
  • typedef uint16_t u16_t;
  • typedef uint32_t u32_t;

  • typedef int8_t s8_t;
  • typedef int16_t s16_t;
  • typedef int32_t s32_t;

  • typedef unsigned int clock_time_t;
  • typedef unsigned int uip_stats_t;

  • typedef unsigned long off_t;

  • void clock_delay(unsigned int us2);
  • void clock_wait(int ms10);
  • void clock_set_seconds(unsigned long s);
  • unsigned long clock_seconds(void);

  • extern uint8_t get_node_id();
  • extern uint8_t get_host_id();
  • extern uint16_t get_src_pan_id();
  • extern uint16_t get_dst_pan_id();
  • extern uint8_t get_current_dtsn();
  • extern void increament_and_store_dtsn();
  • extern void store_prefix();
复制代码
\AD6LoWPAN\contiki-2.5\platform\aducrf101\clock.c
只需要修改一个
void clock_init()
void GP_Tmr1_Int_Handler(void) 中断相关的部分即可

  • /**
  • * \file
  • *       Clock implementation for ADucRF101.
  • * \author
  • *       Analog Devices
  • */

  • #include "sys/clock.h"
  • #include "sys/etimer.h"

  • #include "device.h"

  • #include "gpt.h"

  • static volatile clock_time_t current_clock = 0;
  • static volatile unsigned long current_seconds = 0;
  • static unsigned int second_countdown = CLOCK_SECOND;


  • /*---------------------------------------------------------------------------*/
  • /* ADucRF101 general purpose Timer 1 interrupt handler */
  • void
  • GP_Tmr1_Int_Handler(void)
  • {
  •         ((ADI_TIMER_TypeDef *)ADI_TM1_ADDR)->CLRI = TCLRI_TMOUT_CLR;//中断函数清除中断

  •         current_clock++;

  •         if(etimer_pending() && etimer_next_expiration_time() <= current_clock) {
  •          etimer_request_poll();
  •         }

  •         if (--second_countdown == 0) {
  •          current_seconds++;
  •          second_countdown = CLOCK_SECOND;
  •         }
  • }
  • /*---------------------------------------------------------------------------*/
  • void
  • clock_init()
  • {
  •     ADI_GPT_HANDLE hTimer;
  •     ADI_GPT_RESULT_TYPE result;

  •     result = adi_GPT_Init(ADI_GPT_DEVID_1, &hTimer );

  •     if (ADI_GPT_SUCCESS == result)
  •      result = adi_GPT_SetPrescaler(hTimer, ADI_GPT_PRESCALER_16);

  •     if (ADI_GPT_SUCCESS == result)
  •      result = adi_GPT_SetClockSelect(hTimer, ADI_GPT_CLOCK_SELECT_PCLK);

  •     if (ADI_GPT_SUCCESS == result)
  •      result = adi_GPT_SetPeriodicMode( hTimer, true, 1000);

  •     if (ADI_GPT_SUCCESS == result)
  •      result = adi_GPT_SetCountMode(hTimer, ADI_GPT_COUNT_DOWN);

  •     if (ADI_GPT_SUCCESS == result)
  •      adi_GPT_SetTimerEnable(hTimer, true);
  • }

  • /*---------------------------------------------------------------------------*/
  • clock_time_t
  • clock_time(void)
  • {
  •         return current_clock;
  • }
  • /*---------------------------------------------------------------------------*/
  • unsigned long
  • clock_seconds(void)
  • {
  •         return current_seconds;
  • }
  • /*---------------------------------------------------------------------------*/
  • void
  • clock_delay(unsigned int d)
  • {
  • /* Does not do anything. */
  • }
  • /*---------------------------------------------------------------------------*/
  • void
  • clock_time_update ( uint32_t count )
  • {
  • current_clock += ( count*CLOCK_SECOND );
  • current_seconds += count;
  • }

  • void
  • clock_time_update_ms ( uint32_t count )
  • {
  •     while(count)
  •     {
  •      current_clock ++;

  •      if (--second_countdown == 0) {
  •      current_seconds++;
  •      second_countdown = CLOCK_SECOND;
  •      }
  •      count--;
  •     }
  • }
复制代码
回复

使用道具 举报

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

本版积分规则

关闭

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

手机版|小黑屋|与非网

GMT+8, 2024-4-29 12:36 , Processed in 0.118214 second(s), 18 queries , MemCache On.

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

苏公网安备 32059002001037号

Powered by Discuz! X3.4

Copyright © 2001-2024, Tencent Cloud.