用龙芯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
- 用龙芯1c库在RT-Thread下实现硬件定时器中断
- 用龙芯1c库在RT-Thread下实现外部中断(GPIO中断、按键中断)
- 用龙芯1c库在RT-Thread下输出PWM
- 用龙芯1c库在RT-Thread下实现点灯(控制gpio)
- 用龙芯1c库在裸机编程环境中实现硬件定时器中断
- 在RT-Thread上使用龙芯1c库中的硬件I2C接口
- RT-Thread 02定时器
- RT-Thread在S3C2410下的移植(1)
- stm32在rt-thread上的SysTick(系统定时器)
- 在龙芯1c上用RT-Thread上的lwip NETCONN接口实现socket编程
- 用龙芯1c库在裸机编程中实现外部中断(GPIO中断、按键中断)
- 在windows上搭建龙芯1c的RT-Thread开发环境
- 在龙芯1c上用RT-Thread打印hello
- 在龙芯1C上移植硬浮点FPU到RT-Thread
- 在龙芯1c上使用rt-thread统一标准的spi接口
- 在龙芯1c上使用RT-Thread统一标准的i2c接口
- 在龙芯1c上使用RT-Thread统一标准的gpio接口
- 在Fedora 12下编译和调试RT-Thread
- 《剑指offer》孩子们的游戏---约瑟夫问题
- java--public_private_void_static功能作用
- 准备提交到cocoapods的遇到的坑
- YII日志管理
- ss学习记录
- 用龙芯1c库在RT-Thread下实现硬件定时器中断
- 关于current transaction is aborted, commands ignored until end of transaction block与ON_ERROR_ROLLBACK
- GCD Table 中国剩余定理、解线性同余方程组
- [!] Pods written in Swift can only be integrated as frameworks; add `use_frameworks!` to your Podfil
- 百练OJ:1017:装箱问题
- 英语系列——词汇暴增
- memcached--查找命令--delete
- 支付宝 java.security.SignatureException: Signature length not correct: got 128 but was expecting 256
- 支付安全注意事项