`本章节我们将结合 RTOS【实时操作系统(Real-time operating system, RTOS)】试用LinkIt 7687
LinkIt 7687提供了基于ARM Cortex-M4硬件架构以及FreeRTOS操作系统
实时操作系统(RTOS)是指当外界事件或数据产生时,能够接受并以足够快的速度予以处理,其处理的结果又能在规定的时间之内来控制生产过程或对处理系统做出快速响应,调度一切可利用的资源完成实时任务,并控制所有实时任务协调一致运行的操作系统。提供及时响应和高可靠性是其主要特点。
RTOS代码的核心包含在三个文件中:tasks.c、queue.c、list.c这三个文件位于FreeRTOS/Source目录。在该目录下还包含三个可选的文件:timers.c、event_groups.c、croutine.c,分别实现软件定时、事件组和协程功能。
官方例程中提供了一个简单实用的例程,详情可见路径D:KeilMDK5ARMPACKMediaTekMTx4.0.0projectmt7687_hdk emplatesfreertos_create_thread
当然我的Keil5装在D盘的,大家可以根据你们的Keil的安装盘找到工程文件
第一个小例程
vTestTask函数:
- static void vTestTask( void *pvParameters )
- {
- uint32_t idx = (int)pvParameters;
- portTickType xLastExecutionTime, xDelayTime;
- xLastExecutionTime = xTaskGetTickCount();
- xDelayTime = (1 << idx)*mainCHECK_DELAY;
-
- while (1) {
- vTaskDelayUntil( &xLastExecutionTime, xDelayTime );
- LOG_I(freertos, "Hello World from %d
", idx);
- }
- }
复制代码
main函数:
- int main(void)
- {
- int idx;
- system_init();
- log_init(NULL, NULL, NULL);
- for (idx=0; idx<4; idx++) {
- xTaskCreate( vTestTask, FREERTOS_EXAMPLE_TASK_NAME, FREERTOS_EXAMPLE_TASK_STACKSIZE/sizeof(portSTACK_TYPE), (void*)idx, FREERTOS_EXAMPLE_TASK_PRIO, NULL );
- }
- vTaskStartScheduler();
- for( ;; );
- }
复制代码
该例程在任务调度的过程中通过变量idx来判断调度次数
下边我们把上次讲解的GPIO的控制运用到RTOS中,通过任务调度来控制LED的闪烁详情请看代码
- void LEDinit()
- {
- hal_gpio_direction_t gpio_direction;
- hal_gpio_init ( HAL_GPIO_31 );
- hal_gpio_init ( HAL_GPIO_35 );
- hal_gpio_init ( HAL_GPIO_34 );
- hal_gpio_init ( HAL_GPIO_33 );
- hal_gpio_init ( HAL_GPIO_6 );
-
- hal_pinmux_set_function ( HAL_GPIO_31,HAL_GPIO_31_GPIO31 );
- hal_pinmux_set_function ( HAL_GPIO_35,HAL_GPIO_35_GPIO35 );
- hal_pinmux_set_function ( HAL_GPIO_34,HAL_GPIO_34_GPIO34 );
- hal_pinmux_set_function ( HAL_GPIO_33,HAL_GPIO_33_GPIO33 );
- hal_pinmux_set_function ( HAL_GPIO_6,HAL_GPIO_6_GPIO6 );
- hal_gpio_set_direction ( HAL_GPIO_31,HAL_GPIO_DIRECTION_OUTPUT);
- hal_gpio_set_direction ( HAL_GPIO_35,HAL_GPIO_DIRECTION_OUTPUT);
- hal_gpio_set_direction ( HAL_GPIO_34,HAL_GPIO_DIRECTION_OUTPUT);
- hal_gpio_set_direction ( HAL_GPIO_33,HAL_GPIO_DIRECTION_OUTPUT);
- hal_gpio_set_direction ( HAL_GPIO_6,HAL_GPIO_DIRECTION_OUTPUT);
- }
- static void LEDTask(void *pvParameters )
- {
- LEDinit();
- while(1)
- {
- hal_gpio_set_output ( HAL_GPIO_31,HAL_GPIO_DATA_LOW );
- hal_gpio_set_output ( HAL_GPIO_35,HAL_GPIO_DATA_LOW );
- hal_gpio_set_output ( HAL_GPIO_34,HAL_GPIO_DATA_LOW );
- hal_gpio_set_output ( HAL_GPIO_33,HAL_GPIO_DATA_LOW );
- hal_gpio_set_output ( HAL_GPIO_6,HAL_GPIO_DATA_LOW );
- hal_gpt_delay_ms(500);
- hal_gpio_set_output ( HAL_GPIO_31,HAL_GPIO_DATA_HIGH );
- hal_gpio_set_output ( HAL_GPIO_35,HAL_GPIO_DATA_HIGH );
- hal_gpio_set_output ( HAL_GPIO_34,HAL_GPIO_DATA_HIGH );
- hal_gpio_set_output ( HAL_GPIO_33,HAL_GPIO_DATA_HIGH );
- hal_gpio_set_output ( HAL_GPIO_6,HAL_GPIO_DATA_HIGH );
- hal_gpt_delay_ms(500);
- }
- }
- int main(void)
- {
- int idx;
-
- /* Do system initialization, eg: hardware, nvdm and random seed. */
- system_init();
- /* system log initialization.
- * This is the simplest way to initialize system log, that just inputs three NULLs
- * as input arguments. User can use advanved feature of system log along with NVDM.
- * For more details, please refer to the log dev guide under /doc folder or projects
- * under project/mtxxxx_hdk/apps/.
- */
- log_init(NULL, NULL, NULL);
- // /* Create 4 tasks*/
- // for (idx=0; idx<4; idx++) {
- // xTaskCreate( vTestTask, FREERTOS_EXAMPLE_TASK_NAME, FREERTOS_EXAMPLE_TASK_STACKSIZE/sizeof(portSTACK_TYPE), (void*)idx, FREERTOS_EXAMPLE_TASK_PRIO, NULL );
- // }
- xTaskCreate( LEDTask, "LEDTask", 256, NULL, 3, NULL );
- /* Start the scheduler. */
- vTaskStartScheduler();
- /* If all is well, the scheduler will now be running, and the following line
- will never be reached. If the following line does execute, then there was
- insufficient FreeRTOS heap memory available for the idle and/or timer tasks
- to be created. See the memory management section on the FreeRTOS web site
- for more details. */
- for( ;; );
- }
复制代码
`
0
|
|
|
|