本帖最后由 carolwind 于 2018-3-6 15:14 编辑
代码中加入了 PCF8574_Init(); 后运行就卡死了。
代码如下:
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "FreeRTOS.h"
#include "key.h"
#include "queue.h"
#include "timer.h"
#include "semphr.h"
#include "string.h"
#include "malloc.h"
#include "pcf8574.h"
#include "task.h"
//信息装载器
char InfoBuffer[200];
//任务优先级
#define START_TASK_PRIO 1
//任务堆栈大小
#define START_STK_SIZE 250
//任务句柄
TaskHandle_t StartTask_Handler;
//任务函数
void start_task(void *pvParameters);
//任务优先级
#define LED0_TASK_PRIO 2
//任务堆栈大小
#define LED0_STK_SIZE 20
//任务句柄
TaskHandle_t LED0Task_Handler;
//任务函数
void led0_task(void *pvParameters);
//任务优先级
#define LED1_TASK_PRIO 3
//任务堆栈大小
#define LED1_STK_SIZE 20
//任务句柄
TaskHandle_t LED1Task_Handler;
//任务函数
void led1_task(void *pvParameters);
//任务优先级
#define query_task_PRIO 4
//任务堆栈大小
#define QUERY_STK_SIZE 250
//任务句柄
TaskHandle_t QUERYTask_Handler;
//任务函数
void query_task(void *pvParameters);
//任务优先级
#define TASK1_TASK_PRIO 4
//任务堆栈大小
#define TASK1_STK_SIZE 250
//任务堆栈
StackType_t Task1TaskStack[TASK1_STK_SIZE];
//任务控制块
StaticTask_t Task1TaskTCB;
//任务句柄
TaskHandle_t Task1Task_Handler;
//任务函数
void task1_task(void *pvParameters);
//任务优先级
#define TASK2_TASK_PRIO 4
//任务堆栈大小
#define TASK2_STK_SIZE 250
//任务堆栈
StackType_t Task2TaskStack[TASK2_STK_SIZE];
//任务控制块
StaticTask_t Task2TaskTCB;
//任务句柄
TaskHandle_t Task2Task_Handler;
//任务函数
void task2_task(void *pvParameters);
//空闲任务任务堆栈
static StackType_t IdleTaskStack[configMINIMAL_STACK_SIZE];
//空闲任务控制块
static StaticTask_t IdleTaskTCB;
//定时器服务任务堆栈
static StackType_t TimerTaskStack[configTIMER_TASK_STACK_DEPTH];
//定时器服务任务控制块
static StaticTask_t TimerTaskTCB;
void vApplicationGetIdleTaskMemory(StaticTask_t **ppxIdleTaskTCBBuffer,
StackType_t **ppxIdleTaskStackBuffer,
uint32_t *pulIdleTaskStackSize)
{
*ppxIdleTaskTCBBuffer=&IdleTaskTCB;
*ppxIdleTaskStackBuffer=IdleTaskStack;
*pulIdleTaskStackSize=configMINIMAL_STACK_SIZE;
}
//获取定时器服务任务的任务堆栈和任务控制块内存
//ppxTimerTaskTCBBuffer:任务控制块内存
//ppxTimerTaskStackBuffer:任务堆栈内存
//pulTimerTaskStackSize:任务堆栈大小
void vApplicationGetTimerTaskMemory(StaticTask_t **ppxTimerTaskTCBBuffer,
StackType_t **ppxTimerTaskStackBuffer,
uint32_t *pulTimerTaskStackSize)
{
*ppxTimerTaskTCBBuffer=&TimerTaskTCB;
*ppxTimerTaskStackBuffer=TimerTaskStack;
*pulTimerTaskStackSize=configTIMER_TASK_STACK_DEPTH;
}
//消息队列句柄
QueueHandle_t Key_Queue;
QueueHandle_t Message_Queue;
//消息队列长度
#define KEYMSG_Q_LEN 1
#define MESSAGE_Q_LEN 4
//信号量句柄
SemaphoreHandle_t xSemaphore = NULL;
//命令解析
#define LED1ON 1
#define LED1OFF 2
#define BEEPON 3
#define BEEPOFF 4
#define COMMANDERR 0XFF
//字符串大小写转换
void LowerToCap(u8 *str, u8 len)
{
u8 i;
for(i=0;i<len;i++)
{
if((96<str)&&(str<123))
str = str - 32;
}
}
u8 CommandProcess(u8 *str)
{
u8 CommandValue = COMMANDERR;
if(strcmp((char *)str, "LED1ON") == 0) CommandValue = LED1ON;
else if(strcmp((char *)str, "LED1OFF") == 0) CommandValue = LED1OFF;
else if(strcmp((char *)str, "BEEPON") == 0) CommandValue = BEEPON;
else if(strcmp((char *)str, "BEEPOFF") == 0) CommandValue = BEEPOFF;
return CommandValue;
}
int main(void)
{
Cache_Enable(); //打开L1-Cache
HAL_Init(); //初始化HAL库
Stm32_Clock_Init(432,25,2,9); //设置时钟,216Mhz
delay_init(216); //延时初始化
uart_init(115200); //串口初始化
LED_Init(); //初始化LED
KEY_Init(); //初始化KEY
PCF8574_Init(); //初始化PCF8574 (出问题的地方,这是为什么?难道哪里还需要配置?)
TIM9_Init(5000-1,(u16)216000-1); //初始化定时器9
//创建开始任务
xTaskCreate((TaskFunction_t )start_task, //任务函数
(const char* )"start_task", //任务名称
(uint16_t )START_STK_SIZE, //任务堆栈大小
(void* )NULL, //传递给任务函数的参数
(UBaseType_t )START_TASK_PRIO, //任务优先级
(TaskHandle_t* )&StartTask_Handler); //任务句柄
vTaskStartScheduler(); //开启任务调度
}
//开始任务任务函数
void start_task(void *pvParameters)
{
taskENTER_CRITICAL(); //进入临界区
//创建队列
Key_Queue = xQueueCreate(KEYMSG_Q_LEN, sizeof(u8));
Message_Queue = xQueueCreate(MESSAGE_Q_LEN, USART_REC_LEN);
//创建信号量
xSemaphore = xSemaphoreCreateBinary();
if(xSemaphore == NULL)
{
printf("xSemaphoreCreateBinary Failed!
");
}
if(Key_Queue == NULL)
{
printf("Create Queue Failed!
");
}
//创建LED0任务
xTaskCreate((TaskFunction_t )led0_task,
(const char* )"led0_task",
(uint16_t )LED0_STK_SIZE,
(void* )NULL,
(UBaseType_t )LED0_TASK_PRIO,
(TaskHandle_t* )&LED0Task_Handler);
//创建LED1任务
xTaskCreate((TaskFunction_t )led1_task,
(const char* )"led1_task",
(uint16_t )LED1_STK_SIZE,
(void* )NULL,
(UBaseType_t )LED1_TASK_PRIO,
(TaskHandle_t* )&LED1Task_Handler);
//查询测试任务
xTaskCreate((TaskFunction_t )query_task,
(const char* )"query_task",
(uint16_t )QUERY_STK_SIZE,
(void* )NULL,
(UBaseType_t )query_task_PRIO,
(TaskHandle_t* )&QUERYTask_Handler);
//创建TASK1任务
Task1Task_Handler=xTaskCreateStatic((TaskFunction_t )task1_task,
(const char* )"task1_task",
(uint32_t )TASK1_STK_SIZE,
(void* )NULL,
(UBaseType_t )TASK1_TASK_PRIO,
(StackType_t* )Task1TaskStack,
(StaticTask_t* )&Task1TaskTCB);
//创建TASK1任务
Task2Task_Handler=xTaskCreateStatic((TaskFunction_t )task2_task,
(const char* )"task2_task",
(uint32_t )TASK2_STK_SIZE,
(void* )NULL,
(UBaseType_t )TASK2_TASK_PRIO,
(StackType_t* )Task2TaskStack,
(StaticTask_t* )&Task2TaskTCB);
vTaskDelete(StartTask_Handler); //删除开始任务
taskEXIT_CRITICAL(); //退出临界区
}
//LED0任务函数
void led0_task(void *pvParameters)
{
while(1)
{
LED0_Toggle;
vTaskDelay(500);
}
}
//LED1任务函数
void led1_task(void *pvParameters)
{
while(1)
{
}
}
//查询任务
void query_task(void *pvParameters)
{
TaskHandle_t TaskHandle;
TaskStatus_t TaskStatus;
eTaskState TaskState;
printf("/************第二步:函数vTaskGetInfo()的使用**************/
");
TaskHandle=xTaskGetHandle("task1_task"); //根据任务名获取任务句柄。
//获取LED0_Task的任务信息
vTaskGetInfo((TaskHandle_t )TaskHandle, //任务句柄
(TaskStatus_t* )&TaskStatus, //任务信息结构体
(BaseType_t )pdTRUE, //允许统计任务堆栈历史最小剩余大小
(eTaskState )eInvalid); //函数自己获取任务运行壮态
//通过串口打印出指定任务的有关信息。
printf("任务名: %s
",TaskStatus.pcTaskName);
printf("任务编号: %d
",(int)TaskStatus.xTaskNumber);
printf("任务壮态: %d
",TaskStatus.eCurrentState);
printf("任务当前优先级: %d
",(int)TaskStatus.uxCurrentPriority);
printf("任务基优先级: %d
",(int)TaskStatus.uxBasePriority);
printf("任务堆栈基地址: %#x
",(int)TaskStatus.pxStackBase);
printf("任务堆栈历史剩余最小值:%d
",TaskStatus.usStackHighWaterMark);
printf("/**************************结束***************************/
");
TaskState = eTaskGetState(Task1Task_Handler);
printf("TaskState is %d
", TaskState);
vTaskList(InfoBuffer);
printf("%s
", InfoBuffer);
vTaskDelete(NULL);
}
void task1_task(void *pvParameters)
{
u8 key, i;
while(1)
{
key = KEY_Scan(0);
if(key == WKUP_PRES)
{
if(xSemaphore != NULL)
{
xSemaphoreGive(xSemaphore);
}
}
vTaskDelay(10);
}
}
void task2_task(void *pvParameters)
{
u8 count = 0;
u8 len = 0;
u8 CommandValue = COMMANDERR;
BaseType_t err = pdFALSE;
u8 *CommandStr;
while(1)
{
if(xSemaphore != NULL)
{
count++;
err = xSemaphoreTake(xSemaphore, 1000);
if(err == pdTRUE)
{
len = USART_RX_STA&0x3fff;
CommandStr = pvPortMalloc(USART_REC_LEN+1);
printf("xSemaphoreTaked time: %d !
", count);
sprintf((char*)CommandStr,"%s",USART_RX_BUF);
LowerToCap(CommandStr, len);
CommandValue = CommandProcess(CommandStr);
if(CommandValue != COMMANDERR)
{
printf("%s
", CommandStr);
switch(CommandValue)
{
case LED1ON:
LED1(0);
break;
case LED1OFF:
LED1(1);
break;
case BEEPOFF:
PCF8574_WriteBit(BEEP_IO, 0);
break;
case BEEPON:
PCF8574_WriteBit(BEEP_IO, 1);
break;
}
}
else
{
printf("CommandValue is invalid !
");
}
memset(USART_RX_BUF, 0, USART_REC_LEN);
USART_RX_STA = 0;
vPortFree(CommandStr);
}
else if(err == pdFALSE)
{
printf("xSemaphoreTaked Failed and err is %ld !
", err);
}
}
else
{
vTaskDelay(10);
}
}
}
一周热门 更多>