在uCOS全家桶宣佈開源以後被微軟收購的ThreadX也開源了,真是喜大普奔,對於咱們這些嵌入式行業從業者來講,可以學習這些高含金量的代碼對於眼界的開拓和能力的提高都是頗有幫助的。ThreadX還包含了NETX、GUIX、FILEX、USBX等豐富的組件,幾乎包含了嵌入式開發中的全部場景,這些組件和ThreadX-RTOS可以完美的配合完成一條龍的開發。ThreadX的github地址:[Azure RTOS (github.com)]:git
ThreadX在更新了幾個版本以後已經提供了多個平臺的移植端口,包括AC五、AC六、GCC、IAR,如今能夠更方便地移植到各編譯器平臺了。本次移植是在STM32H743 + MDK-AC6 + ThreadX6.1.2
下完成。首先使用STM32CubeMX
新建一個工程,配置了LED、KEY、UART,而後將下載的ThreadX放在同一工程下(移植用到common
和ports/cortex_m7/ac6
下的文件)。github
工程新增ThreadX/Port
、ThreadX/Src
分組,ThreadX/Port
f組中添加ports/cortex_m7/ac6/src
目錄下全部文件以及ports\cortex_m7\ac6\example_build\sample_threadx
目錄下的tx_initialize_low_level.S
文件; ThreadX/Src
組中添加threadx\common\src
下全部文件。添加完成以下圖所示:shell
配置工程宏定義參數和文件包含路徑以下所示。api
其中USE_HAL_DRIVER
和STM32H743xx
是使用HAL庫建立工程須要包含的定義,TX_ENABLE_FPU_SUPPORT
是使能ThreadX的浮點運算支持須要定義的,在readme_threadx.txt
文檔中有以下描述:app
If saving the context of the FPU registers is needed, the ThreadX library should be re-built with TX_ENABLE_FPU_SUPPORT defined.
固然要正確的使用硬件浮點運算功能還應該開啓MDK中的以下配置:函數
主要是對tx_initialize_low_level.S
文件進行修改,該文件做用是初始化棧地址和向量表、配置系統心跳節拍、配置部分中斷優先級、定義部分中斷處理函數。可是其中有些工做在STM32H743的啓動文件startup_stm32h743xx.s
中已經作了,而STM32啓動文件所實現的一些功能這個文件又沒有實現。由於該文件是針對cortex_m7內核芯片作的,沒有針對具體某一款芯片,因此沒有STM32的啓動文件實現的功能完善,所以不能替代啓動文件。這裏的修改方法是不動STM32啓動文件,修改tx_initialize_low_level.S文件,在此提供兩種修改方案:一、根據衝突修改tx_initialize_low_level.S文件內容;二、將tx_initialize_low_level.S文件改成C語言實現(STM32啓動文件已經實現的無論,只需將ThreadX須要配置的功能用C實現)。學習
方案1修改後內容以下(修改處已標註):測試
@/**************************************************************************/ @/* */ @/* Copyright (c) Microsoft Corporation. All rights reserved. */ @/* */ @/* This software is licensed under the Microsoft Software License */ @/* Terms for Microsoft Azure RTOS. Full text of the license can be */ @/* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */ @/* and in the root directory of this software. */ @/* */ @/**************************************************************************/ @ @ @/**************************************************************************/ @/**************************************************************************/ @/** */ @/** ThreadX Component */ @/** */ @/** Initialize */ @/** */ @/**************************************************************************/ @/**************************************************************************/ @ @ .global _tx_thread_system_stack_ptr .global _tx_initialize_unused_memory .global _tx_timer_interrupt .global __main .global __tx_SVCallHandler .global __tx_PendSVHandler .global __tx_NMIHandler @ NMI .global __tx_BadHandler @ HardFault .global __tx_SVCallHandler @ SVCall .global __tx_DBGHandler @ Monitor .global __tx_PendSVHandler @ PendSV .global __tx_SysTickHandler @ SysTick .global __tx_IntHandler @ Int 0 @ @ SYSTEM_CLOCK = 480000000 @---modify by user(系統頻率) SYSTICK_CYCLES = ((SYSTEM_CLOCK / 1000) -1) @---modify by user(時鐘節拍) .text 32 .align 4 .syntax unified @/**************************************************************************/ @/* */ @/* FUNCTION RELEASE */ @/* */ @/* _tx_initialize_low_level Cortex-M7/AC6 */ @/* 6.1 */ @/* AUTHOR */ @/* */ @/* William E. Lamie, Microsoft Corporation */ @/* */ @/* DESCRIPTION */ @/* */ @/* This function is responsible for any low-level processor */ @/* initialization, including setting up interrupt vectors, setting */ @/* up a periodic timer interrupt source, saving the system stack */ @/* pointer for use in ISR processing later, and finding the first */ @/* available RAM memory address for tx_application_define. */ @/* */ @/* INPUT */ @/* */ @/* None */ @/* */ @/* OUTPUT */ @/* */ @/* None */ @/* */ @/* CALLS */ @/* */ @/* None */ @/* */ @/* CALLED BY */ @/* */ @/* _tx_initialize_kernel_enter ThreadX entry function */ @/* */ @/* RELEASE HISTORY */ @/* */ @/* DATE NAME DESCRIPTION */ @/* */ @/* 09-30-2020 William E. Lamie Initial Version 6.1 */ @/* */ @/**************************************************************************/ @VOID _tx_initialize_low_level(VOID) @{ .global _tx_initialize_low_level .thumb_func _tx_initialize_low_level: @ @ /* Disable interrupts during ThreadX initialization. */ @ CPSID i @ @ /* Set base of available memory to end of non-initialised RAM area. */ @ LDR r0, =_tx_initialize_unused_memory @ Build address of unused memory pointer LDR r1, =__initial_sp @ Image$$ARM_LIB_STACKHEAP$$ZI$$Limit @ Build first free address ---modify by user ADD r1, r1, #4 @ STR r1, [r0] @ Setup first unused memory pointer @ @ /* Setup Vector Table Offset Register. */ @ MOV r0, #0xE000E000 @ Build address of NVIC registers LDR r1, =__Vectors @ vector_table @ Pickup address of vector table ---modify by user STR r1, [r0, #0xD08] @ Set vector table address @ @ /* Set system stack pointer from vector value. */ @ LDR r0, =_tx_thread_system_stack_ptr @ Build address of system stack pointer LDR r1, =__Vectors @ vector_table @ Pickup address of vector table ---modify by user LDR r1, [r1] @ Pickup reset stack pointer STR r1, [r0] @ Save system stack pointer @ @ /* Enable the cycle count register. */ @ LDR r0, =0xE0001000 @ Build address of DWT register LDR r1, [r0] @ Pickup the current value ORR r1, r1, #1 @ Set the CYCCNTENA bit STR r1, [r0] @ Enable the cycle count register @ @ /* Configure SysTick for 100Hz clock, or 16384 cycles if no reference. */ @ MOV r0, #0xE000E000 @ Build address of NVIC registers LDR r1, =SYSTICK_CYCLES STR r1, [r0, #0x14] @ Setup SysTick Reload Value MOV r1, #0x7 @ Build SysTick Control Enable Value STR r1, [r0, #0x10] @ Setup SysTick Control @ @ /* Configure handler priorities. */ @ LDR r1, =0x00000000 @ Rsrv, UsgF, BusF, MemM STR r1, [r0, #0xD18] @ Setup System Handlers 4-7 Priority Registers LDR r1, =0xFF000000 @ SVCl, Rsrv, Rsrv, Rsrv STR r1, [r0, #0xD1C] @ Setup System Handlers 8-11 Priority Registers @ Note: SVC must be lowest priority, which is 0xFF LDR r1, =0x40FF0000 @ SysT, PnSV, Rsrv, DbgM STR r1, [r0, #0xD20] @ Setup System Handlers 12-15 Priority Registers @ Note: PnSV must be lowest priority, which is 0xFF @ @ /* Return to caller. */ @ BX lr @} @ @/* Define shells for each of the unused vectors. */ @ .global __tx_BadHandler .thumb_func __tx_BadHandler: B __tx_BadHandler @ /* added to catch the hardfault */ .global __tx_HardfaultHandler .thumb_func __tx_HardfaultHandler: B __tx_HardfaultHandler @ /* added to catch the SVC */ .global __tx_SVCallHandler .thumb_func __tx_SVCallHandler: B __tx_SVCallHandler @ /* Generic interrupt handler template */ .global __tx_IntHandler .thumb_func __tx_IntHandler: @ VOID InterruptHandler (VOID) @ { PUSH {r0, lr} #ifdef TX_ENABLE_EXECUTION_CHANGE_NOTIFY BL _tx_execution_isr_enter @ Call the ISR enter function #endif @ /* Do interrupt handler work here */ @ /* BL <your C Function>.... */ #ifdef TX_ENABLE_EXECUTION_CHANGE_NOTIFY BL _tx_execution_isr_exit @ Call the ISR exit function #endif POP {r0, lr} BX LR @ } @ /* System Tick timer interrupt handler */ .global __tx_SysTickHandler .global SysTick_Handler .thumb_func __tx_SysTickHandler: .thumb_func SysTick_Handler: @ VOID TimerInterruptHandler (VOID) @ { @ PUSH {r0, lr} #ifdef TX_ENABLE_EXECUTION_CHANGE_NOTIFY BL _tx_execution_isr_enter @ Call the ISR enter function #endif BL _tx_timer_interrupt #ifdef TX_ENABLE_EXECUTION_CHANGE_NOTIFY BL _tx_execution_isr_exit @ Call the ISR exit function #endif POP {r0, lr} BX LR @ } @ /* NMI, DBG handlers */ .global __tx_NMIHandler .thumb_func __tx_NMIHandler: B __tx_NMIHandler .global __tx_DBGHandler .thumb_func __tx_DBGHandler: B __tx_DBGHandler
方案2修改內容以下(工程中用該文件tx_initialize_low_level.C
替換tx_initialize_low_level.S
文件)ui
#include "stm32h7xx_hal.h" #include "tx_api.h" static const uint32_t SYSTEM_CLOCK = 480000000; static const uint32_t SYSTICK_CYCLES = ((SYSTEM_CLOCK / 1000) -1); extern void _tx_timer_interrupt(void); /*in "tx_timer_interrupt.S" file*/ void _tx_initialize_low_level(void) { /* Disable interrupts during ThreadX initialization. */ __set_PRIMASK(1); /* Configure SysTick for 1000Hz clock, or 16384 cycles if no reference. */ SysTick_Config(SYSTICK_CYCLES); /* Configure handler priorities. */ HAL_NVIC_SetPriority(SVCall_IRQn, 15, 0); /*Note: SVC must be lowest priority*/ HAL_NVIC_EnableIRQ(SVCall_IRQn); HAL_NVIC_SetPriority(PendSV_IRQn, 15, 0); HAL_NVIC_EnableIRQ(PendSV_IRQn); /*Note: PnSV must be lowest priority*/ HAL_NVIC_SetPriority(SysTick_IRQn, 4, 0); HAL_NVIC_EnableIRQ(SysTick_IRQn); } /* System Tick timer interrupt handler */ void SysTick_Handler (void) { /*BL _tx_timer_interrupt*/ _tx_timer_interrupt(); }
經過上述修改文件可見ThreadX修改底層移植文件的內容並很少,移植仍是比較方便。this
最後將stm32h7xx_it.c
文件中的PendSV_Handler
和SysTick_Handler
中斷處理函數屏蔽(前者用於實現RTOS的任務調度,後者用於實現RTOS的心跳節拍)。
#ifndef ENABLE_RTOS void PendSV_Handler(void) { } void SysTick_Handler(void) { } #endif
通過上述步驟,工程編譯已經沒有錯誤,接下來就是建立任務測試ThreadX可否正常運行起來。
在threadx\ports\cortex_m7\ac6\example_build\sample_threadx
下的sample_threadx.c
文件中演示了任務的建立,依葫蘆畫瓢建立本身的任務便可。main.c
文件內容以下:
#include "main.h" #include "usart.h" #include "gpio.h" #include "tx_api.h" #define DEMO_STACK_SIZE (2 * 1024) #define DEMO_BYTE_POOL_SIZE (32 * 1024) TX_BYTE_POOL byte_pool_0; UCHAR memory_area[DEMO_BYTE_POOL_SIZE]; TX_THREAD thread_0; TX_THREAD thread_1; TX_THREAD thread_2; void thread_0_entry(ULONG thread_input); void thread_1_entry(ULONG thread_input); void thread_2_entry(ULONG thread_input); int main(void) { HAL_Init(); SystemClock_Config(); MX_GPIO_Init(); MX_USART1_UART_Init(); while (1) { tx_kernel_enter(); } } void tx_application_define(void *first_unused_memory) { CHAR *pointer = TX_NULL; /* Create a byte memory pool from which to allocate the thread stacks. */ tx_byte_pool_create(&byte_pool_0, "byte pool 0", memory_area, DEMO_BYTE_POOL_SIZE); /* Allocate the stack for thread 0. */ tx_byte_allocate(&byte_pool_0, (VOID **) &pointer, DEMO_STACK_SIZE, TX_NO_WAIT); /* Create the main thread. */ tx_thread_create(&thread_0, "thread 0", thread_0_entry, 0, pointer, DEMO_STACK_SIZE, 1, 1, TX_NO_TIME_SLICE, TX_AUTO_START); /* Allocate the stack for thread 1. */ tx_byte_allocate(&byte_pool_0, (VOID **) &pointer, DEMO_STACK_SIZE, TX_NO_WAIT); /* Create threads 1 */ tx_thread_create(&thread_1, "thread 1", thread_1_entry, 0, pointer, DEMO_STACK_SIZE, 2, 2, TX_NO_TIME_SLICE, TX_AUTO_START); /* Allocate the stack for thread 2. */ tx_byte_allocate(&byte_pool_0, (VOID **) &pointer, DEMO_STACK_SIZE, TX_NO_WAIT); /* Create threads 1 */ tx_thread_create(&thread_2, "thread 2", thread_2_entry, 0, pointer, DEMO_STACK_SIZE, 3, 3, TX_NO_TIME_SLICE, TX_AUTO_START); } void thread_0_entry(ULONG thread_input) { while(1) { HAL_GPIO_TogglePin(LED1_GPIO_Port,LED1_Pin); tx_thread_sleep(200); } } void thread_1_entry(ULONG thread_input) { while(1) { HAL_GPIO_TogglePin(LED2_GPIO_Port,LED2_Pin); tx_thread_sleep(200); } } void thread_2_entry(ULONG thread_input) { double a = 0.1, b = 1.0; while(1) { a += 0.00000000001; b -= 0.00000000001; printf("floating test:\ta = %.11f, b = %.11f\r\n", a, b); tx_thread_sleep(2000); } }
到這裏,任務就正常運行起來了。
任務建立函數說明
- 參數:
- thread_ptr:指向線程控制塊的指針
- name_ptr :指向線程名稱的指針
- entry_function:指定用於線程執行的初始C函數。 當線程今後入口函數返回時,它將處於完成狀態並沒有限期掛起。
- entry_input:首次執行時傳遞給線程的入口函數的32位值。 此輸入的使用徹底由應用程序肯定
- stack_start:堆棧內存區域的起始地址
- stack_size:堆棧內存區中的字節數
- priority:線程的優先級。 有效值的範圍是0 ~(TX_MAX_PRIORITES-1),其中0表示最高優先級
- preempt_threshold:禁用的搶佔的最高優先級。 只有高於此級別的優先級才能夠搶佔該線程,該值必須小於或等於指定的優先級,等於線程優先級的值將禁用搶佔閾值
- time_slice:在其餘具備相同優先級的就緒線程有機會運行以前,容許此線程運行的時鐘節拍數。請注意,使用搶佔閾值將禁用時間片。合法的時間片值範圍從1到0xffffff (包括)。TX_NO_TIME_SLICE (值爲0)禁用此線程的時間片。(使用時間片會致使少許的系統開銷。因爲時間片僅在多個線程共享相同優先級的狀況下有用,所以不該爲具備惟一優先級的線程分配時間片)
- auto_start:指定線程是當即啓動仍是處於暫停狀態。指定選項是TX_AUTO_START (0x01)和 TX_DONT_START (0x00)。若是指定了TX_DONT_START,則應用程序必須調用 tx_thread_resume 才能使線程運行。
- 返回值
- TX_SUCCESS (0x00):線程建立成功
- TX_THREAD_ERROR (0x0E):無效的線程控制指針。指針爲 NULL 或線程已建立
- TX_PTR_ERROR (0x03):入口點的起始地址無效或堆棧區域無效,一般爲 NULL。
- TX_SIZE_ERROR (0x05):堆棧區域的大小無效。 線程必須至少具備TX_MINIMUM_STACK字節才能執行。
- TX_PRIORITY_ERROR (0x0F):無效的線程優先級,該值超出(0 ~(TX_MAX_PRIORITIES-1))的範圍。
- TX_THRESH_ERROR (0x18):指定的搶佔閾值無效。 該值的有效優先級必須小於或等於線程的初始優先級。
- TX_START_ERROR (0x10):自動啓動選擇無效
- TX_CALLER_ERROR (0x13):該服務的調用者無效
UINT tx_thread_create( TX_THREAD *thread_ptr, CHAR *name_ptr, VOID (*entry_function)(ULONG), ULONG entry_input, VOID *stack_start, ULONG stack_size, UINT priority, UINT preempt_threshold, ULONG time_slice, UINT auto_start);