目录
- 任务的简单创建
- 图形化配置上手
- 简简单单点个灯
- 配置引脚
- 建立任务
- 实验结果
- 整体代码
- main.c
- myTask.c
- myTask.h
平台:Code Composer Studio 10.4.0
MSP432P401R SimpleLink™ 微控制器 LaunchPad™ 开发套件
(MSP-EXP432P401R)
任务的简单创建
根据上篇机翻的函数作用,及观察本例程模板,可总结出使用默认参数初始化线程任务的函数:
三个参数分别是线程任务名,优先级和堆栈大小
void My_Task_Init(void (*TaskName)(void *arg), int priority, size_t stacksize)
{pthread_t thread;pthread_attr_t attrs;struct sched_param priParam;int retc;/* Initialize the attributes structure with default values */pthread_attr_init(&attrs);/* Set priority, detach state, and stack size attributes */priParam.sched_priority = priority;retc = pthread_attr_setschedparam(&attrs, &priParam);retc |= pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED);retc |= pthread_attr_setstacksize(&attrs, stacksize);if (retc != 0){/* failed to set attributes */while (1){}}retc = pthread_create(&thread, &attrs, TaskName, NULL);if (retc != 0){/* pthread_create() failed */while (1){}}
}
为方便管理
我在工程目录下新建了src文件夹和inc文件夹
在其中建立了myTask.c 、myTask.h,同时还把工程的一些文件名改了,并删掉了例程模板中的empty.c
myTask.c
/** myTask.c** Created on: 2021年8月2日* Author: Royic*/#include <stdint.h>/* POSIX Header files */
#include <pthread.h>/* RTOS header files */
#include <ti/sysbios/BIOS.h>/* Driver configuration */
#include "ti_drivers_config.h"/* For usleep() */
#include <unistd.h>
#include <stdint.h>
#include <stddef.h>#include "./inc/myTask.h"void My_Task_Init(void *(*startroutine)(void *), int priority, size_t stacksize)
{pthread_t thread;pthread_attr_t attrs;struct sched_param priParam;int retc;/* Initialize the attributes structure with default values */pthread_attr_init(&attrs);/* Set priority, detach state, and stack size attributes */priParam.sched_priority = priority;retc = pthread_attr_setschedparam(&attrs, &priParam);retc |= pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED);retc |= pthread_attr_setstacksize(&attrs, stacksize);if (retc != 0){/* failed to set attributes */while (1){}}retc = pthread_create(&thread, &attrs, startroutine, NULL);if (retc != 0){/* pthread_create() failed */while (1){}}
}
myTask.h
/** myTask.h** Created on: 2021年8月2日* Author: Royic*/#ifndef INC_MYTASK_H_
#define INC_MYTASK_H_void My_Task_Init(void *(*startroutine)(void *), int priority, size_t stacksize);#endif /* INC_MYTASK_H_ */
图形化配置上手
打开.syscfg格式的文件,此为CCS用于图形化配置的文件
应该是之前TI出的图形化配置工具TI PinMux的CCS内置版(下图为TI PinMux)
简简单单点个灯
配置引脚
由原理图可知,P1.0 控制LED1,LED2为RGB灯,其中P2.0控制其中的红灯
仿造原工程修改其中的GPIO模块,设LED1为输出模式,正常(非开漏)模式,驱动能力中等,初始电平为低电平,不上拉下拉
Use Hardware选项是可选板子预设好的GPIO种类,选为None后可在下面的GPIO Pin改为自定义引脚。
同理,设置LED2的红灯
建立任务
在myTask.c中建立两个任务
其中一个1s反转一次LED1,另一个2s反转一次LED2的红灯
sleep(n)用于秒级的挂起
usleep(n)用于微秒级的挂起
#include <ti/drivers/GPIO.h>
void *LED1_Task(void *arg0)
{while (1){sleep(1);GPIO_toggle(LED1);}
}void *LED2_Task(void *arg0)
{while (1){sleep(2);GPIO_toggle(LED2);}
}
在myTask.h中声明
extern void *LED1_Task(void *arg0);
extern void *LED2_Task(void *arg0);
在main中初始化
int main(void)
{/* Call driver init functions */Board_init();GPIO_init();My_Task_Init(LED1_Task, 1, 1024);My_Task_Init(LED2_Task, 1, 1024);BIOS_start();return (0);
}
实验结果
可以观察到两个任务近似并行同时执行,LED1 约1s翻转一次,LED2 约2s翻转一次。
整体代码
main.c
/** ======== main_tirtos.c ========*/
#include <stdint.h>/* POSIX Header files */
#include <pthread.h>/* RTOS header files */
#include <ti/sysbios/BIOS.h>/* Driver configuration */
#include <ti/drivers/Board.h>
#include <ti/drivers/GPIO.h>#include "./inc/myTask.h"/** ======== main ========*/
int main(void)
{/* Call driver init functions */Board_init();GPIO_init();My_Task_Init(LED1_Task, 1, 1024);My_Task_Init(LED2_Task, 1, 1024);BIOS_start();return (0);
}
myTask.c
/** myTask.c** Created on: 2021年8月2日* Author: Royic*/#include <stdint.h>/* POSIX Header files */
#include <pthread.h>/* RTOS header files */
#include <ti/sysbios/BIOS.h>/* Driver configuration */
#include "ti_drivers_config.h"/* For usleep() */
#include <unistd.h>
#include <stdint.h>
#include <stddef.h>#include "./inc/myTask.h"#include <ti/drivers/GPIO.h>void My_Task_Init(void *(*startroutine)(void *), int priority, size_t stacksize)
{pthread_t thread;pthread_attr_t attrs;struct sched_param priParam;int retc;/* Initialize the attributes structure with default values */pthread_attr_init(&attrs);/* Set priority, detach state, and stack size attributes */priParam.sched_priority = priority;retc = pthread_attr_setschedparam(&attrs, &priParam);retc |= pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED);retc |= pthread_attr_setstacksize(&attrs, stacksize);if (retc != 0){/* failed to set attributes */while (1){}}retc = pthread_create(&thread, &attrs, startroutine, NULL);if (retc != 0){/* pthread_create() failed */while (1){}}
}void *LED1_Task(void *arg0)
{while (1){sleep(1);GPIO_toggle(LED1);}
}void *LED2_Task(void *arg0)
{while (1){sleep(2);GPIO_toggle(LED2);}
}
myTask.h
/** myTask.h** Created on: 2021年8月2日* Author: Royic*/#ifndef INC_MYTASK_H_
#define INC_MYTASK_H_void My_Task_Init(void *(*startroutine)(void *), int priority, size_t stacksize);extern void *LED1_Task(void *arg0);
extern void *LED2_Task(void *arg0);#endif /* INC_MYTASK_H_ */