現在最新應該是RTOS2 (API) Keil RTX5,但是這篇是講解API1的….
至於為什麼呢? …..
總之一言難盡,就隨手記著了
上圖黃黃的很苦惱,對,我沒有解掉,因為我想應該是警告我要選 retarget.c、startup_m460.s、startup_m460.c,如下圖所示
但是,如果是拿新唐的BSP project 來改個話,應該本來就有了,這樣會衝突到(重複定義),所以這邊就姑且先不選,讓她黃。
main :
Thread 和 Thread1 就會去搶 CPU 資源,如下:
設定 osSignalSet (Thread_ID1,0x01); , 如果 osSignalWait (0x01,osWaitForever); 沒有做完,會在Thread1 裡面一直等到Thread1 做完
所以,他一定是整齊的 Thread1 做完換 Thread2, Thread2 做完換 Thread1,達到 Synchronize
/*************************************************************************//**
* @file main.c
* @version V3.00
* @brief A project template for M460 MCU.
*
* @copyright SPDX-License-Identifier: Apache-2.0
* @copyright Copyright (C) 2021 Nuvoton Technology Corp. All rights reserved.
*****************************************************************************/
#include <stdio.h>
#include "NuMicro.h"
#include "cmsis_os.h"
void Thread1 (void const *argument);
void Thread2 (void const *argument);
osThreadDef(Thread1, osPriorityAboveNormal, 1, 0);
osThreadDef(Thread2, osPriorityNormal, 1, 0);
osThreadId Thread_ID1;
osThreadId Thread_ID2;
/*----------------------------------------------------------------------------
Flash LED 1 when signaled by the Thread2
*---------------------------------------------------------------------------*/
void Thread1 (void const *argument) {
for (;;) {
osSignalWait (0x01,osWaitForever);
printf("Thread1 \n");
osSignalWait (0x01,osWaitForever);
}
}
/*----------------------------------------------------------------------------
Flash LED two and synchronize the flashing of LED 1 by setting a signal flag
*---------------------------------------------------------------------------*/
void Thread2 (void const *argument) {
for (;;) {
osSignalSet (Thread_ID1,0x01);
osDelay(5000000);
printf("Thread2 \n");
osSignalSet (Thread_ID1,0x01);
osDelay(5000000);
}
}
void SYS_Init(void)
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init System Clock */
/*---------------------------------------------------------------------------------------------------------*/
/* Enable HXT clock */
CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);
/* Wait for HXT clock ready */
CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);
/* Set PCLK0 and PCLK1 to HCLK/2 */
CLK->PCLKDIV = (CLK_PCLKDIV_APB0DIV_DIV2 | CLK_PCLKDIV_APB1DIV_DIV2);
/* Set core clock to 200MHz */
CLK_SetCoreClock(200000000);
/* Enable UART0 module clock */
CLK_EnableModuleClock(UART0_MODULE);
/* Select UART0 module clock source as HIRC and UART0 module clock divider as 1 */
CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HIRC, CLK_CLKDIV0_UART0(1));
/* Set GPB multi-function pins for UART0 RXD and TXD */
SET_UART0_RXD_PB12();
SET_UART0_TXD_PB13();
}
/*
* This is a template project for M460 series MCU. Users could based on this project to create their
* own application without worry about the IAR/Keil project settings.
*
* This template application uses external crystal as HCLK source and configures UART0 to print out
* "Hello World", users may need to do extra system configuration based on their system design.
*/
int main()
{
/* Unlock protected registers */
SYS_UnlockReg();
SYS_Init();
/* Init UART to 115200-8n1 for print message */
UART_Open(UART0, 115200);
/* Connect UART to PC, and open a terminal tool to receive following message */
printf("main() Hello World\n");
osKernelInitialize (); // initialize CMSIS-RTOS
Thread_ID1 = osThreadCreate(osThread(Thread1), NULL);
Thread_ID2 = osThreadCreate(osThread(Thread2), NULL);
osKernelStart (); // start thread execution
}
所以,他一定是整齊的 Thread1 做完換 11111, 再換 Thread2 做完換 22222,達到 Synchronize
如果沒有做Synchronize,會112221112… 1跟2 會搶著印。
/*************************************************************************//**
* @file main.c
* @version V3.00
* @brief A project template for M460 MCU.
*
* @copyright SPDX-License-Identifier: Apache-2.0
* @copyright Copyright (C) 2021 Nuvoton Technology Corp. All rights reserved.
*****************************************************************************/
#include <stdio.h>
#include "NuMicro.h"
#include "cmsis_os.h"
void uart_Thread1 (void const *argument);
void uart_Thread2 (void const *argument);
osThreadDef(uart_Thread1, osPriorityNormal, 1, 0);
osThreadDef(uart_Thread2, osPriorityNormal, 1, 0);
osThreadId T_uart1;
osThreadId T_uart2;
osMutexId uart_mutex;
osMutexDef(uart_mutex);
/*----------------------------------------------------------------------------
Thread two writes the character '1' to UART
*---------------------------------------------------------------------------*/
void uart_Thread1 (void const *argument) {
uint32_t i;
for (;;) {
osMutexWait(uart_mutex, osWaitForever);
osDelay(5000000);
printf("11111 ");
osMutexRelease(uart_mutex);
}
}
/*----------------------------------------------------------------------------
Thread two writes the character '2' to UART
*---------------------------------------------------------------------------*/
void uart_Thread2 (void const *argument) {
uint32_t i;
for(;;) {
osMutexWait(uart_mutex, osWaitForever);
osDelay(3000000);
printf("22222 ");
osMutexRelease(uart_mutex);
}
}
void SYS_Init(void)
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init System Clock */
/*---------------------------------------------------------------------------------------------------------*/
/* Enable HXT clock */
CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);
/* Wait for HXT clock ready */
CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);
/* Set PCLK0 and PCLK1 to HCLK/2 */
CLK->PCLKDIV = (CLK_PCLKDIV_APB0DIV_DIV2 | CLK_PCLKDIV_APB1DIV_DIV2);
/* Set core clock to 200MHz */
CLK_SetCoreClock(200000000);
/* Enable UART0 module clock */
CLK_EnableModuleClock(UART0_MODULE);
/* Select UART0 module clock source as HIRC and UART0 module clock divider as 1 */
CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HIRC, CLK_CLKDIV0_UART0(1));
/* Set GPB multi-function pins for UART0 RXD and TXD */
SET_UART0_RXD_PB12();
SET_UART0_TXD_PB13();
}
/*
* This is a template project for M460 series MCU. Users could based on this project to create their
* own application without worry about the IAR/Keil project settings.
*
* This template application uses external crystal as HCLK source and configures UART0 to print out
* "Hello World", users may need to do extra system configuration based on their system design.
*/
int main()
{
/* Unlock protected registers */
SYS_UnlockReg();
SYS_Init();
/* Init UART to 115200-8n1 for print message */
UART_Open(UART0, 115200);
/* Connect UART to PC, and open a terminal tool to receive following message */
printf("main() Hello World\n");
osKernelInitialize (); // initialize CMSIS-RTOS
uart_mutex = osMutexCreate(osMutex(uart_mutex));
T_uart1 = osThreadCreate(osThread(uart_Thread1), NULL);
T_uart2 = osThreadCreate(osThread(uart_Thread2), NULL);
osKernelStart (); // start thread execution
}