当前位置: 首页 > 工具软件 > Contiki > 使用案例 >

Contiki内核的STM32移植

温嘉玉
2023-12-01

  1. 找一个STM32UART的打印例程,最好支持printf函数。
  2. 将contiki\core\sys目录下的autostart.cetimer.cprocess.ctimer.c加入到工程中。
  3. 在include路径中,加入contiki\corecontiki\core\syscontiki\core\libcontiki\cpu
  4. 把cpu\arm\stm32f103目录下的clock.c加入到工程中。
  5. 在clock.c中,用#include "stm32f10x.h"#include "stm32f10x_it.h"替换原来的#include <stm32f10x_map.h>#include <nvic.h>
  6. 把platform\stm32test目录下的contiki_conf.h加入到include路径中。
  7. 把systick初始化函数改为:

void clock_init() {
    if ( SysTick_Config ( SystemCoreClock / CLOCK_SECOND ) ) {
        while ( 1 );
    }
}

  把systick中断函数改为:

void SysTick_Handler ( void ) {
    current_clock++;

    if ( etimer_pending() && etimer_next_expiration_time() <= current_clock ) {
        etimer_request_poll();
        // printf( "%d, %d\n", clock_time(), etimer_next_expiration_time() );
    }

    if ( --second_countdown == 0 ) {
        current_seconds++;
        second_countdown = CLOCK_SECOND;
    }
}

最后把stm32f10x_it.cvoid SysTick_Handler(void){}删除。
  8. 将main.c修改如下:

#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "led.h"
#include <sys/process.h>
#include <sys/procinit.h>
#include <sys/etimer.h>
#include <sys/autostart.h>
#include <sys/clock.h>

PROCESS ( blink_process, "Blink" );
PROCESS_THREAD ( blink_process, ev, data ) {
    PROCESS_BEGIN();

    while ( 1 ) {
        static struct etimer et;
        etimer_set ( &et, CLOCK_SECOND );
        PROCESS_WAIT_EVENT_UNTIL ( etimer_expired ( &et ) );
        LED0 = !LED0;
    }

    PROCESS_END();
}

PROCESS ( blink_process_2, "Blink" );
PROCESS_THREAD ( blink_process_2, ev, data ) {
    PROCESS_BEGIN();

    while ( 1 ) {
        static struct etimer et_2;
        etimer_set ( &et_2, CLOCK_SECOND / 2 );
        printf ( "I am running\r\n" );
        PROCESS_WAIT_EVENT_UNTIL ( etimer_expired ( &et_2 ) );
        LED1 = !LED1;
    }

    PROCESS_END();
}

unsigned int idle_count = 0;
AUTOSTART_PROCESSES ( &blink_process, &blink_process_2 );

int main ( void ) {
    NVIC_Configuration();
    uart_init ( 9600 );
    LED_Init();
    clock_init();
    process_init();
    process_start ( &etimer_process, NULL );
    autostart_start ( autostart_processes );
    printf ( "Processes running\n" );

    while ( 1 ) {
        do {
        } while ( process_run() > 0 );

        idle_count++;
        /* Idle! */
        /* Stop processor clock */
        /* asm("wfi"::); */
    }
}
 类似资料: