用龙芯1c库在RT-Thread下实现硬件定时器中断

来源:互联网 发布:代码格式化 知乎 编辑:程序博客网 时间:2024/06/08 14:41

目前,已经将龙芯1c库中的硬件定时器相关接口移植到了RT-Thread中。再加上RT-Thread中的中断接口,很容易实现硬件定时器中断。

龙芯1c库的git http://git.oschina.net/caogos/OpenLoongsonLib1c

RT-Thread的git https://github.com/RT-Thread/rt-thread

只是需要确认以下中断号是否正确,正确的硬件定时器中断号为

#define LS1C_PWM0_IRQ17#define LS1C_PWM1_IRQ18#define LS1C_PWM2_IRQ19#define LS1C_PWM3_IRQ20

中断号定义在RT-Thread中“libcpu\mips\loongson_1c\ls1c.h”中



源码的大致流程为:在RT-Thread中新建一个线程,在现场中初始化硬件定时器,设置中断入口函数,并使能中断。源码如下

bsp\ls1cdev\applications\application.c

/* * File      : application.c * This file is part of RT-Thread RTOS * COPYRIGHT (C) 2006-2012, RT-Thread Develop Team * * The license and distribution terms for this file may be * found in the file LICENSE in this distribution or at * http://www.rt-thread.org/license/LICENSE * * Change Logs: * Date                Author         Notes * 2010-06-25          Bernard        first version * 2011-08-08          lgnq           modified for Loongson LS1B * 2015-07-06          chinesebear    modified for Loongson LS1C */#include <rtthread.h>#include <components.h>#include "rthw.h"#include "ls1c.h"#include "ls1c_timer.h"// 测试用的线程  #define THREAD_TEST_PRIORITY                    (25)  #define THREAD_TEST_STACK_SIZE                  (4*1024)        // 4k  #define THREAD_TEST_TIMESLICE                   (10)    struct rt_thread thread_test;  ALIGN(8)rt_uint8_t thread_test_stack[THREAD_TEST_STACK_SIZE];  volatile int test_irq_counter = 0;// 定时器0的中断入口函数void ls1c_timer_pwm0_irqhandler(int irqno, void *param){    timer_info_t timer_info = {0};    test_irq_counter++;    // 清中断,否则会一直不停的反复执行该中断处理函数    timer_info.timer    = TIMER_PWM0;    timer_info.time_ns  = 10*1000*1000;    timer_stop(&timer_info);}  // 测试用的线程的入口  void thread_test_entry(void *parameter)  {    timer_info_t timer_info = {0};    // 启动定时器    timer_info.timer    = TIMER_PWM0;    timer_info.time_ns  = 10*1000*1000;    timer_init(&timer_info);    rt_hw_interrupt_install(LS1C_PWM0_IRQ, ls1c_timer_pwm0_irqhandler, RT_NULL, "Timer_PWM0");    rt_hw_interrupt_umask(LS1C_PWM0_IRQ);    while (1)      {          rt_kprintf("[%s] test_irq_counter=%d\n", __FUNCTION__, test_irq_counter);        rt_thread_delay(100);          // 重启定时器        timer_init(&timer_info);    }  }  void rt_init_thread_entry(void *parameter){/* initialization RT-Thread Components */rt_components_init();}int rt_application_init(void){rt_thread_t tid;    rt_err_t result;/* create initialization thread */tid = rt_thread_create("init",rt_init_thread_entry, RT_NULL,4096, RT_THREAD_PRIORITY_MAX/3, 20);if (tid != RT_NULL)rt_thread_startup(tid);      // 初始化测试用的线程      result = rt_thread_init(&thread_test,                               "thread_test",                              thread_test_entry,                              RT_NULL,                              &thread_test_stack[0],                              sizeof(thread_test_stack),                              THREAD_TEST_PRIORITY,                              THREAD_TEST_TIMESLICE);      if (RT_EOK == result)      {          rt_thread_startup(&thread_test);      }      else      {          return -1;      }  return 0;}

其中,线程“init”为RT-Thread自带的,线程“thread_test”才是新建的。

函数timer_init()和timer_stop()为龙芯1c库中的硬件定时器接口,函数rt_hw_interrupt_install()为RT-Thread提供的用于设置中断入口函数的,函数rt_hw_interrupt_umask()用于使能中断。

运行结果如下

Press <Enter> to execute loading image:tftp://192.168.1.3/rtthread.elfPress any other key to abort.00Loading file: tftp://192.168.1.3/rtthread.elf (elf)0x80200000/114680 + 0x8021bff8/12552(z) + 477 syms\Entry address is 80200000g root=/dev/nfs rw nfsroot=192.168.1.4:/nfsramdisk/LS1xrootfs-demo_test noinitrd init=/linuxrc console=ttyS2,115200 ip=192.168.1.254:::::eth0:off   zero      at       v0       v1       a0       a1       a2       a3    00000000 00000000 00000000 00000000 00000008 a10ffbe8 a10ffc0c 8008e650    t0       t1       t2       t3       t4       t5       t6       t7    00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000    s0       s1       s2       s3       s4       s5       s6       s7    00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000    t8       t9       k0       k1       gp       sp       s8       ra    00000000 00000000 00000000 00000000 00000000 a10ffbc8 00000000 8005a840CPU configure: 0x80000482CPU:Loongson 1CDCache 16kb, linesize 32 bytes.ICache 16kb, linesize 32 bytes.enable cpu cache donecurrent sr: 0x20000800 \ | /- RT -     Thread Operating System / | \     2.1.0 build Jul 18 2017 2006 - 2017 Copyright by rt-thread teammsh >[thread_test_entry] test_irq_counter=0[thread_test_entry] test_irq_counter=1[thread_test_entry] test_irq_counter=2[thread_test_entry] test_irq_counter=3[thread_test_entry] test_irq_counter=4[thread_test_entry] test_irq_counter=5[thread_test_entry] test_irq_counter=6[thread_test_entry] test_irq_counter=7[thread_test_entry] test_irq_counter=8


阅读全文
0 0