MQX中如何使用中断?

来源:互联网 发布:模拟机械运动的软件 编辑:程序博客网 时间:2024/06/06 03:22

飞思卡尔目前MQX最新版本为4.2,本文给大家介绍一下如何在MQX中使用中断功能。
实现的功能是:使用一个PIT定时器,在PIT中断里翻转GPIO,控制LED灯闪烁。
平台:FRDM_K22F+MQX4.2
直接在hello工程里修改,只改动了hello.c 文件,改动的文件如下:

/*HEADER************************************************************************ Copyright 2008 Freescale Semiconductor, Inc.* Copyright 1989-2008 ARC International** This software is owned or controlled by Freescale Semiconductor.* Use of this software is governed by the Freescale MQX RTOS License* distributed with this Material.* See the MQX_RTOS_LICENSE file distributed for more details.** Brief License Summary:* This software is provided in source form for you to use free of charge,* but it is not open source software. You are allowed to use this software* but you cannot redistribute it or derivative works of it in source form.* The software may be used only in connection with a product containing* a Freescale microprocessor, microcontroller, or digital signal processor.* See license agreement file for full license terms including other* restrictions.******************************************************************************* Comments:**   This file contains the source for the hello example program.***END************************************************************************/#include <mqx.h>#include <bsp.h> #include <fio.h>#if ! BSPCFG_ENABLE_IO_SUBSYSTEM#error This application requires BSPCFG_ENABLE_IO_SUBSYSTEM defined non-zero in user_config.h. Please recompile BSP with this option.#endif#ifndef BSP_DEFAULT_IO_CHANNEL_DEFINED#error This application requires BSP_DEFAULT_IO_CHANNEL to be not NULL. Please set corresponding BSPCFG_ENABLE_TTYx to non-zero in user_config.h and recompile BSP with this option.#endif/* Task IDs */#define HELLO_TASK 5#define LED_REG_BIT_MASK    (1 << 1) // FRDM_K22 PTA1-LED REDextern void hello_task(uint32_t);const TASK_TEMPLATE_STRUCT  MQX_template_list[] = {     /* Task Index,   Function,   Stack,  Priority, Name,     Attributes,          Param, Time Slice */    { HELLO_TASK,   hello_task, 1500,   8,        "hello",  MQX_AUTO_START_TASK, 0,     0 },    { 0 }};// PIT ISRvoid int_pit0(void * parameter){    PIT_TFLG0 = 0x01; // clear flag    PIT_TCTRL0; // dummy read to ensure the interrupt,                 // must be executed here, or interrupt will never happen.    GPIOA_PTOR = LED_REG_BIT_MASK; // output toggle}/*TASK*-----------------------------------------------------* * Task Name    : hello_task* Comments     :*    This task prints " Hello World "**END*-----------------------------------------------------*/void hello_task    (        uint32_t initial_data    ){    (void)initial_data; /* disable 'unused variable' warning */    printf("Hello World\n");       // GPIO Init      PORTA_PCR1 &= ~0x700;    PORTA_PCR1 |= PORT_PCR_MUX(1);    GPIOA_PDOR |= LED_REG_BIT_MASK; // output high    GPIOA_PDDR |= LED_REG_BIT_MASK;  // output      // Install PIT Interrupt      _bsp_int_disable(INT_PIT0);      _int_install_isr(INT_PIT0, int_pit0, NULL);      _bsp_int_init(INT_PIT0, 3, 0, TRUE);      _bsp_int_enable(INT_PIT0);       // Init PIT0     SIM_SCGC6 |= SIM_SCGC6_PIT_MASK; // enable PIT module     PIT_MCR = 0x00;     PIT_TCTRL0 = 0x00; // disable PIT0     PIT_LDVAL0 = 0x16E3600; // 0.5s for 48MHz bus clock     PIT_TFLG0 = 0x01; // clear flag     PIT_TCTRL0 = PIT_TCTRL_TIE_MASK; // enable PIT0 and interrupt      PIT_TCTRL0 |= PIT_TCTRL_TEN_MASK;     _task_block();}/* EOF */

简单分析一下代码:GPIO和PIT0初始化部分,这里使用的是直接操作寄存器的方式。中断相关的语句是:

_bsp_int_disable(INT_PIT0);_int_install_isr(INT_PIT0, int_pit0, NULL);_bsp_int_init(INT_PIT0, 3, 0, TRUE);_bsp_int_enable(INT_PIT0);

bsp_int 开头的几个函数定义如下,和NVIC相关:

#define _bsp_int_init(num, prior, subprior, enable)     _nvic_int_init(num, prior, enable)#define _bsp_int_enable(num)                            _nvic_int_enable(num)#define _bsp_int_disable(num)                           _nvic_int_disable(num)

_int_install_isr() 函数解释如下,它是在MQX里安装中断服务函数:

这里写图片描述

还有一种安装中断服务的方式:
这里写图片描述

这种方式是绕过MQX,适用于对实时性要求非常高的场合。

有点需要注意,如果直接调用此函数,那么返回值为NULL

if( _int_install_kernel_isr(INT_PIT0, int_pit0)== NULL){  printf("\n_int_install_isr SPI0_ISR failed!\r\n");}

跟踪进入此函数:
这里写图片描述
需要将此宏设置为0

0 0
原创粉丝点击