2.寻找一些线索
阅读contiki-2.5 源码中,stm32移植的相关内容分散在两个文件夹中,第一, cpu\arm\stm32f103,这个文件夹存放的stm32移植的相关文件;第二,platform\stm32test,这个文件夹中有一个不是那么完整的例子。具体的源码如下:
[cpp] view plaincopy 
- #include <stm32f10x_map.h>
- #include <stm32f10x_dma.h>
- #include <gpio.h>
- #include <nvic.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <debug-uart.h>
- #include <sys/process.h>
- #include <sys/procinit.h>
- #include <etimer.h>
- #include <sys/autostart.h>
- #include <clock.h>
- unsigned int idle_count = 0;
- int
- main()
- {
- dbg_setup_uart();
- printf("Initialising\n");
- clock_init();
- process_init();
- process_start(&etimer_process,NULL);
- autostart_start(autostart_processes);
- printf("
rocessesrunning\n"); - while(1) {
- do {
- } while(process_run()> 0);
- idle_count++;
- /* Idle! */
- /* Stop processor clock */
- /* asm("wfi":
; */
- }
- return 0;
- }
简单分析一下,首先文件中包含了一些头文件。看着有点熟悉,应该是V2.0库的头文件,后面的移植工作会全部替换掉,使用V3.4的库文件。在main函数中,第一步初始化串口并通过串口发送某些信息。接着,初始化时钟,通过跟踪源代码,发现clock_init函数位于cpu\arm\stm32f103文件夹中的clock文件夹中。具体的函数如下:
[cpp] view plaincopy 
- void
- clock_init()
- {
- NVIC_SET_SYSTICK_PRI(8);
- SysTick->LOAD= MCK/8/CLOCK_SECOND;
- SysTick->CTRL= SysTick_CTRL_ENABLE | SysTick_CTRL_TICKINT;
- }
这段代码的原理也非常的简单,初始化systick定时器。其功能是每秒发生CLOCK_SECOND次溢出。配置了systick也少不了systick中断了,systick的中断的源码如下:
在systick中断中不断更新了etimer,有了时钟contiki就可以运行了。
4.开始移植
先在clock源文件中添加头文件
#include "stm32f10x.h"
#include "stm32f10x_it.h"
删除原来的
#include<stm32f10x_map.h> #include <nvic.h>
把systick初始化改成
[cpp] view plaincopy 
- void
- clock_init()
- {
- if (SysTick_Config(SystemCoreClock / CLOCK_SECOND))
- {
- while(1);
- }
- }
把systick中断改为
[cpp] view plaincopy 
- 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.c的void SysTick_Handler(void){}删除。。 |