小学生
最后登录1970-1-1
在线时间 小时
注册时间2022-6-30
|
鄙人新手,在STM32F103开发板上试用freeRTOS,创建了两个任务和一个定时器中断。从中断中发出一个信号量给一个任务,让这个任务打印一串字符。另一个任务周期打印字符作为对照。当不加信号量时,两个任务都周期打印,定时器也定时中断打印,工作正常。加入信号量之后,编译正常不报错,但是下载到开发板就没有任何反应。使用Debug跟踪,发现在刚刚运行到定时器初始化时就跑到HardFault_Handler去了。麻烦请大家帮我看看怎么回事。下面是程序:/* main.c */
#include "stm32f10x.h"
#include "stm32f10x_tim.h"
#include "stm32f10x_conf.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_usart.h"
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
#include "list.h"
#include "misc.h"
#include "portable.h"
#include "FreeRTOSConfig.h"
#include "usart1.h"
#include "timerConfig.h"
#define stackSize_task1 1024
#define stackSize_task2 1024
static void vUART1_1_Task(void *pvParameters);
static void vUART1_2_Task(void *pvParameters);
SemaphoreHandle_t xBinarySemaphore;
int main(void)
{
USART1_Config();
TIM2_Init();
xBinarySemaphore = xSemaphoreCreateBinary();
if (xBinarySemaphore != NULL)
{
xTaskCreate(vUART1_1_Task, (const portCHAR*)"UART1_1", stackSize_task1, NULL, tskIDLE_PRIORITY, NULL );
xTaskCreate(vUART1_2_Task, (const portCHAR*)"UART1_2", stackSize_task2, NULL, tskIDLE_PRIORITY, NULL );
vTaskStartScheduler();
}
return 0;
}
void vUART1_1_Task(void *pvParameters)
{
TickType_t xDelay = pdMS_TO_TICKS(2000);
while(1)
{
printf("\r\n This is task1 ! \r\n");
vTaskDelay(xDelay); //ÑÓʱ2s
}
}
void vUART1_2_Task(void *pvParameters)
{
while(1)
{
if(xSemaphoreTake(xBinarySemaphore, portMAX_DELAY))
{
printf("Semaphore works!\n\r");
}
}
// TickType_t xDelay = pdMS_TO_TICKS(2000);
// while(1)
// {
// printf("\r\n This is task2 !!! \r\n");
// vTaskDelay(xDelay); //ÑÓʱ2s
// }
}
/* timerConfig.c*/
#include "FreeRTOS.h"
#include "semphr.h"
#include "timerConfig.h"
#include "misc.h"
#include "usart1.h"
#include "stm32f10x_tim.h"
void TIM2_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_TimeBaseStructure.TIM_Period = TIM2_Period;
TIM_TimeBaseStructure.TIM_Prescaler = TIM2_Prescaler;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE );
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM2, ENABLE);
}
void TIM2_IRQHandler(void)
{
extern SemaphoreHandle_t xBinarySemaphore;
BaseType_t xHigherPriorityTaskWoken;
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update );
xHigherPriorityTaskWoken = pdTRUE;
xSemaphoreGive(xBinarySemaphore);
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
// printf("\r\n Timer !!! \r\n");
}
}
|
|