TA的每日心情 | 慵懒 2022-10-25 08:55 |
---|
签到天数: 103 天 连续签到: 1 天 [LV.6]常住居民II
|
NXP的MCUXpress上面也集成了外设配置生成代码的功能,还集成了第三方可选的OS。
现在我们想移植一个针对于LPC55S69的FreeRTOS例程。以往均通过手动移植,但
现在很多IDE都集成了自动植入第三方OS的功能。
流程如下:
配置好后,直接修改main函数所在文档:
代码修改为:
/**
* @file test_code.c
* @brief Application entry point.
*/
#include <stdio.h>
#include "board.h"
#include "peripherals.h"
#include "pin_mux.h"
#include "clock_config.h"
#include "LPC55S69_cm33_core0.h"
#include "fsl_debug_console.h"
#include "FreeRTOS.h"
#include "task.h"
/* TODO: insert other include files here. */
#define LED_R_TOGGLE GPIO_PortToggle(BOARD_INITPINS_LED_R_GPIO, BOARD_INITPINS_LED_R_PORT, \
1U << BOARD_INITPINS_LED_R_PIN) /*!< Toggle on target LED1 */
#define LED_B_TOGGLE GPIO_PortToggle(BOARD_INITPINS_LED_B_GPIO, BOARD_INITPINS_LED_B_PORT, \
1U << BOARD_INITPINS_LED_B_PIN) /*!< Toggle on target LED1 */
#define LED_G_TOGGLE GPIO_PortToggle(BOARD_INITPINS_LED_G_GPIO, BOARD_INITPINS_LED_G_PORT, \
1U << BOARD_INITPINS_LED_G_PIN) /*!< Toggle on target LED1 */
#define LED_R_ON GPIO_PortClear(BOARD_INITPINS_LED_R_GPIO, BOARD_INITPINS_LED_R_PORT, \
1U << BOARD_INITPINS_LED_R_PIN) /*!< Toggle on target LED1 */
#define LED_B_ON GPIO_PortClear(BOARD_INITPINS_LED_B_GPIO, BOARD_INITPINS_LED_B_PORT, \
1U << BOARD_INITPINS_LED_B_PIN) /*!< Toggle on target LED1 */
#define LED_G_ON GPIO_PortClear(BOARD_INITPINS_LED_G_GPIO, BOARD_INITPINS_LED_G_PORT, \
1U << BOARD_INITPINS_LED_G_PIN) /*!< Toggle on target LED1 */
#define LED_R_OFF GPIO_PortSet(BOARD_INITPINS_LED_R_GPIO, BOARD_INITPINS_LED_R_PORT, \
1U << BOARD_INITPINS_LED_R_PIN) /*!< Toggle on target LED1 */
#define LED_B_OFF GPIO_PortSet(BOARD_INITPINS_LED_B_GPIO, BOARD_INITPINS_LED_B_PORT, \
1U << BOARD_INITPINS_LED_B_PIN) /*!< Toggle on target LED1 */
#define LED_G_OFF GPIO_PortSet(BOARD_INITPINS_LED_G_GPIO, BOARD_INITPINS_LED_G_PORT, \
1U << BOARD_INITPINS_LED_G_PIN) /*!< Toggle on target LED1 */
//FreeRTOS
//任务优先级
#define START_TASK_PRIO 1
//任务堆栈大小
#define START_STK_SIZE 128
//任务句柄
TaskHandle_t StartTask_Handler;
//任务函数
void start_task(void *pvParameters);
//任务优先级
#define TASK1_TASK_PRIO 2
//任务堆栈大小
#define TASK1_STK_SIZE 128
//任务句柄
TaskHandle_t Task1Task_Handler;
//任务函数
void task1_task(void *pvParameters);
//任务优先级
#define TASK2_TASK_PRIO 3
//任务堆栈大小
#define TASK2_STK_SIZE 128
//任务句柄
TaskHandle_t Task2Task_Handler;
//任务函数
void task2_task(void *pvParameters);
/* TODO: insert other definitions and declarations here. */
void delay_us(uint32_t us)
{
SDK_DelayAtLeastUs(us,SystemCoreClock);
}
void delay_ms(uint32_t ms)
{
while(ms--)
delay_us(1000);
}
/*
* @brief Application entry point.
*/
int main(void) {
/* Init board hardware. */
BOARD_InitBootPins();
BOARD_InitBootClocks();
BOARD_InitBootPeripherals();
#ifndef BOARD_INIT_DEBUG_CONSOLE_PERIPHERAL
/* Init FSL debug console. */
BOARD_InitDebugConsole();
#endif
LED_R_OFF;
LED_B_OFF;
LED_G_OFF;
PRINTF("FreeRTOS test!\n");
//创建开始任务
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(); //开启任务调度
/* Force the counter to be placed into memory. */
//volatile static int i = 0 ;
/* Enter an infinite loop, just incrementing a counter. */
while(1) {
//i++ ;
/* 'Dummy' NOP to allow source level single stepping of
tight while() loop */
//__asm volatile ("nop");
/* LED_R_TOGGLE;
LED_B_TOGGLE;
LED_G_TOGGLE;
delay_ms(500);*/
}
return 0 ;
}
//开始任务任务函数
void start_task(void *pvParameters)
{
taskENTER_CRITICAL(); //进入临界区
//创建TASK1任务
xTaskCreate((TaskFunction_t )task1_task,
(const char* )"task1_task",
(uint16_t )TASK1_STK_SIZE,
(void* )NULL,
(UBaseType_t )TASK1_TASK_PRIO,
(TaskHandle_t* )&Task1Task_Handler);
//创建TASK2任务
xTaskCreate((TaskFunction_t )task2_task,
(const char* )"task2_task",
(uint16_t )TASK2_STK_SIZE,
(void* )NULL,
(UBaseType_t )TASK2_TASK_PRIO,
(TaskHandle_t* )&Task2Task_Handler);
vTaskDelete(StartTask_Handler); //删除开始任务
taskEXIT_CRITICAL(); //退出临界区
}
//task1任务函数
void task1_task(void *pvParameters)
{
uint8_t task1_num=0;
printf("Task1 Run:000\r\n");
while(1)
{
task1_num++; //任务执1行次数加1 注意task1_num1加到255的时候会清零!!
LED_R_TOGGLE;
printf("Task1 already run: %d times\r\n",task1_num);
if(task1_num==5)
{
if(Task2Task_Handler != NULL) //任务2是否存在?
{
vTaskDelete(Task2Task_Handler); //任务1执行5次删除任务2
Task2Task_Handler=NULL; //任务句柄清零
LED_B_OFF;
printf("Task1 deleted Task2!\r\n");
}
}
printf("Task1 Run:%d\r\n",task1_num);
vTaskDelay(1000); //延时1s,也就是1000个时钟节拍
}
}
//task2任务函数
void task2_task(void *pvParameters)
{
uint8_t task2_num=0;
printf("Task2 Run:000\r\n");
while(1)
{
task2_num++; //任务2执行次数加1 注意task1_num2加到255的时候会清零!!
LED_B_TOGGLE;
printf("Task2 already run: %d times\r\n",task2_num);
printf("Task2 Run:%d\r\n",task2_num);
vTaskDelay(1000); //延时1s,也就是1000个时钟节拍
}
}
编译下载:
串口输出如下:
LED闪烁现象:
|
|