原创 stm32 ucos中断函数的编写方法

2011-7-12 21:07 5106 3 3 分类: MCU/ 嵌入式

今天终于搞清楚了在STM32中用ucos中断的方法了,ucos没有自己移植,用的是别人移植好的例程。之前一直搞不明白ucos中断怎么写,写在哪里。参考了一片网上的论坛,实现了EXTI中断,但是搞不明白在端口的时钟配置要加上 RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);AFIO是端口复用,跟端口的中断有什么关系?

附上论坛地址:http://www.ourdev.cn/bbs/bbs_content.jsp?bbs_sn=4342396&bbs_page_no=1&bbs_id=3020

#include "includes.h"

static  OS_STK App_TaskStartStk[APP_TASK_START_STK_SIZE];

 

static  void App_TaskCreate(void);
static  void App_DispScr_SignOn(void);

static  void App_TaskStart(void* p_arg);
static  void EXTI_ISR(void);//中断处理函数
_Pin_6 );      //LED1
#define LED_LED1_OFF()  GPIO_ResetBits(GPIOC, GPIO_Pin_6 );     //LED1

unsigned char i;
/*
*********************************************************************************************************
*                                                main()
*
* Description : This is the standard entry point for C code.  It is assumed that your code will call
*               main() once you have performed all necessary initialization.
*
* Argument : none.
*
* Return   : none.
*********************************************************************************************************
*/

int main(void)
{
   CPU_INT08U os_err;

   BSP_IntDisAll();                                            /* Disable all ints until we are ready to accept them.  */

   BSP_IntVectSet  (BSP_INT_ID_EXTI2 , EXTI_ISR); //中断设置函数

   BSP_IntEn (BSP_INT_ID_EXTI2); //中断允许函数
  
   
   OSInit();                                                   /* Initialize "uC/OS-II, The Real-Time Kernel".         */

   BSP_Init();                                                 /* Initialize BSP functions.  */
  
  
   App_DispScr_SignOn();


   printf("OS Init OK!\r\n");
   os_err = OSTaskCreate((void (*) (void *)) App_TaskStart,
               /* Create the start task.                               */
                          (void *) 0,
               (OS_STK *) &App_TaskStartStk[APP_TASK_START_STK_SIZE - 1],
               (INT8U) APP_TASK_START_PRIO);
   printf("Creat App_TaskStart!\r\n");
#if (OS_TASK_NAME_SIZE >= 11)
   OSTaskNameSet(APP_TASK_START_PRIO, (CPU_INT08U *) "Start Task", &os_err);
#endif


   OSTimeSet(0);
   OSStart();                                                  /* Start multitasking (i.e. give control to uC/OS-II).  */

   return (0);
}

/*
*********************************************************************************************************
*                                          App_TaskStart()
*
* Description : The startup task.  The uC/OS-II ticker should only be initialize once multitasking starts.
*
* Argument : p_arg       Argument passed to 'App_TaskStart()' by 'OSTaskCreate()'.
*
* Return   : none.
*
* Caller   : This is a task.
*
* Note     : none.
*********************************************************************************************************
*/

static  void App_TaskStart(void* p_arg)
{
  
   (void) p_arg;

   OS_CPU_SysTickInit();                                       /* Initialize the SysTick.       */

#if (OS_TASK_STAT_EN > 0)
   OSStatInit();                                               /* Determine CPU capacity.                              */
#endif

   //App_TaskCreate();
   //App_DispScr_SignOn();
   while (DEF_TRUE)
   {
      LED_LED1_ON();
      OSTimeDlyHMSM(0, 0, 0, 100);
     
      LED_LED1_OFF();
      OSTimeDlyHMSM(0, 0, 0, 100);
//   EXTI_GenerateSWInterrupt(EXTI_Line2);   //EXTI_GenerateSWInterrupt(EXTI_Line6);
   }
}

/*
*********************************************************************************************************
*                                            App_TaskCreate()
*
* Description : Create the application tasks.
*
* Argument : none.
*
* Return   : none.
*
* Caller   : App_TaskStart().
*
* Note     : none.
*********************************************************************************************************
*/

static  void App_TaskCreate(void)
{
   CPU_INT08U os_err;

//   os_err = OSTaskCreate((void (*) (void *)) App_TaskLCD, (void *) 0,
//               (OS_STK *) &App_TaskLCDStk[APP_TASK_LCD_STK_SIZE - 1],
//               (INT8U) APP_TASK_LCD_PRIO);
//   printf("Creat App_TaskLCD!\r\n");
//#if (OS_TASK_NAME_SIZE >= 9)
//   OSTaskNameSet(APP_TASK_LCD_PRIO, "LCD", &os_err);
//#endif

//   os_err = OSTaskCreate((void (*) (void *)) App_TaskKbd, (void *) 0,
//               (OS_STK *) &App_TaskKbdStk[APP_TASK_KBD_STK_SIZE - 1],
//               (INT8U) APP_TASK_KBD_PRIO);
//   printf("Creat App_TaskKbd!\r\n");
//#if (OS_TASK_NAME_SIZE >= 9)
//   OSTaskNameSet(APP_TASK_KBD_PRIO, "KeyBoard", &os_err);
//#endif

//   os_err = OSTaskCreate((void (*) (void *))
//               App_TaskJoystick, (void *)
//               0,
//               (OS_STK *) &App_TaskJoystickStk[APP_TASK_Joystick_STK_SIZE - 1],
//               (INT8U)
//               APP_TASK_Joystick_PRIO);
//   printf("Creat App_TaskJoystick!\r\n");
//#if (OS_TASK_NAME_SIZE >= 9)
//   OSTaskNameSet(APP_TASK_Joystick_PRIO, "Joystick", &os_err);
//#endif
}

 


/*
*********************************************************************************************************
*                                          App_DispScr_SignOn()
*
* Description : Display uC/OS-II system information on the USART.
*
* Argument: none.
*
* Return   : none.
*
* Caller   : App_TaskKbd().
*
* Note    : none.
*********************************************************************************************************
*/

static  void App_DispScr_SignOn(void)
{
   printf("\r\n  Micrium uC/OS-II  \r\n");
   printf("  ST STM32 (Cortex-M3)\r\n\r\n");

   printf("  uC/OS-II:  V%ld.%ld%ld\r\n", OSVersion() / 100,
      (OSVersion() % 100) / 10, (OSVersion() % 10));
   printf("  TickRate: %ld  \r\n", OS_TICKS_PER_SEC);
   printf("  CPU Usage: %ld%    \r\n", OSCPUUsage);
   printf("  CPU Speed:%ld MHz  \r\n", BSP_CPU_ClkFreq() / 1000000L);
   printf("  #Ticks: %ld  \r\n", OSTime);
   printf("  #CtxSw: %ld  \r\n\r\n", OSCtxSwCtr);
}

 


void EXTI_ISR(void)
{
 
 if(EXTI_GetITStatus(EXTI_Line2)!=RESET)
 {
  EXTI_ClearITPendingBit(EXTI_Line2);

  printf("ISR\r\n");
  if(i==1)
  {
   GPIO_ResetBits(GPIOC,GPIO_Pin_7);
 i=0;
  }
  else
  {
   GPIO_SetBits(GPIOC,GPIO_Pin_7);
 i=1;
  }
 }
}

 

 

 

 

 

 

 

 

 

 


/*
*********************************************************************************************************
*********************************************************************************************************
*                                          uC/OS-II APP HOOKS
*********************************************************************************************************
*********************************************************************************************************
*/

#if (OS_APP_HOOKS_EN > 0)
/*
*********************************************************************************************************
*                                      TASK CREATION HOOK (APPLICATION)
*
* Description : This function is called when a task is created.
*
* Argument : ptcb   is a pointer to the task control block of the task being created.
*
* Note     : (1) Interrupts are disabled during this call.
*********************************************************************************************************
*/

void App_TaskCreateHook(OS_TCB* ptcb)
{
}

/*
*********************************************************************************************************
*                                    TASK DELETION HOOK (APPLICATION)
*
* Description : This function is called when a task is deleted.
*
* Argument : ptcb   is a pointer to the task control block of the task being deleted.
*
* Note     : (1) Interrupts are disabled during this call.
*********************************************************************************************************
*/

void App_TaskDelHook(OS_TCB* ptcb)
{
   (void) ptcb;
}

/*
*********************************************************************************************************
*                                      IDLE TASK HOOK (APPLICATION)
*
* Description : This function is called by OSTaskIdleHook(), which is called by the idle task.  This hook
*               has been added to allow you to do such things as STOP the CPU to conserve power.
*
* Argument : none.
*
* Note     : (1) Interrupts are enabled during this call.
*********************************************************************************************************
*/

#if OS_VERSION >= 251
void App_TaskIdleHook(void)
{
}
#endif

/*
*********************************************************************************************************
*                                        STATISTIC TASK HOOK (APPLICATION)
*
* Description : This function is called by OSTaskStatHook(), which is called every second by uC/OS-II's
*               statistics task.  This allows your application to add functionality to the statistics task.
*
* Argument : none.
*********************************************************************************************************
*/

void App_TaskStatHook(void)
{
}

/*
*********************************************************************************************************
*                                        TASK SWITCH HOOK (APPLICATION)
*
* Description : This function is called when a task switch is performed.  This allows you to perform other
*               operations during a context switch.
*
* Argument : none.
*
* Note     : 1 Interrupts are disabled during this call.
*
*            2  It is assumed that the global pointer 'OSTCBHighRdy' points to the TCB of the task that
*                   will be 'switched in' (i.e. the highest priority task) and, 'OSTCBCur' points to the
*                  task being switched out (i.e. the preempted task).
*********************************************************************************************************
*/

#if OS_TASK_SW_HOOK_EN > 0
void App_TaskSwHook(void)
{
}
#endif

/*
*********************************************************************************************************
*                                     OS_TCBInit() HOOK (APPLICATION)
*
* Description : This function is called by OSTCBInitHook(), which is called by OS_TCBInit() after setting
*               up most of the TCB.
*
* Argument : ptcb    is a pointer to the TCB of the task being created.
*
* Note     : (1) Interrupts may or may not be ENABLED during this call.
*********************************************************************************************************
*/

#if OS_VERSION >= 204
void App_TCBInitHook(OS_TCB* ptcb)
{
   (void) ptcb;
}
#endif

#endif

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
我要评论
0
3
关闭 站长推荐上一条 /3 下一条