#include "stdio.h"
#include "NuMicro.h"
#include "tos_k.h"
#define task1_size 240 // size of task1 stack
#define task2_size 240
k_task_t task1; // structure of task1
k_task_t task2;
k_stack_t task1_stack[task1_size]; //stack of task1
k_stack_t task2_stack[task2_size];
void SYS_Init(void)
{
/* Set XT1_OUT(PF.2) and XT1_IN(PF.3) to input mode */
PF->MODE &= ~(GPIO_MODE_MODE2_Msk | GPIO_MODE_MODE3_Msk);
/* Enable HIRC clock (Internal RC 48 MHz) */
CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);
/* Wait for HIRC clock ready */
CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);
/* Switch HCLK clock source to external */
CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HXT, CLK_CLKDIV0_HCLK(1));
/* Set core clock as 51MHz from PLL */
CLK_SetCoreClock(FREQ_51MHZ);
/* Enable UART clock */
CLK_EnableModuleClock(UART0_MODULE);
/* Select UART clock source from HIRC */
CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HIRC, CLK_CLKDIV0_UART0(1));
CLK_EnableSysTick(CLK_CLKSEL0_STCLKSEL_HCLK_DIV2,51000);
CLK_SetSysTickClockSrc(CLK_CLKSEL0_STCLKSEL_HCLK_DIV2);
/* Update System Core Clock */
/* User can use SystemCoreClockUpdate() to calculate SystemCoreClock. */
SystemCoreClockUpdate();
/* Set GPB multi-function pins for UART0 RXD and TXD */
SYS->GPB_MFPH = (SYS->GPB_MFPH & ~(SYS_GPB_MFPH_PB12MFP_Msk | SYS_GPB_MFPH_PB13MFP_Msk)) |
(SYS_GPB_MFPH_PB12MFP_UART0_RXD | SYS_GPB_MFPH_PB13MFP_UART0_TXD);
/* Lock protected registers */
SYS_LockReg();
}
void UART0_Init()
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init UART */
/*---------------------------------------------------------------------------------------------------------*/
/* Reset UART module */
SYS_ResetModule(UART0_RST);
/* Configure UART0 and set UART0 baud rate */
UART_Open(UART0, 115200);
}
//task1 function
void task1_fun(void *Parameter)
{
while(1)
{
printf("Task1: Thansplant CORTEX-M4 Succeed\r\n");
PB14 =~ PB14;
tos_task_delay(1000);
}
}
void task2_task(void *Parameter)
{
// k_err_t err;
int i=0;
while(1)
{
printf("Task2: hello world, hello Nuvoton \r\n");
for(i=0;i<5;i++)
CLK_SysTickDelay(250000);
}
}
int main()
{
k_err_t err;
/* Unlock protected registers */
SYS_UnlockReg();
/* Init System, peripheral clock and multi-function I/O */
SYS_Init();
/* tos init ,include SystickInit,should init after protected registers unlock */
tos_knl_init();
/* Lock protected registers */
SYS_LockReg();
/* Init UART0 for printf */
UART0_Init();
printf("system clock to %d Hz, PLL clock to %d Hz...................... ", SystemCoreClock, CLK_SetCoreClock(FREQ_51MHZ));
GPIO_SetMode(PB, BIT14, GPIO_MODE_OUTPUT);
err = tos_task_create(
&task1,
"Task1",
task1_fun,
NULL,
2,
task1_stack,
task1_size,
100);
err = tos_task_create(
&task2,
"display",
task2_task,
NULL,
2,
task2_stack,
task2_size,
100);
if(err != K_ERR_NONE)
printf("TencentOS create task fail! errcode is %d\r\n",err);
tos_knl_start(); //Start TOS TINY
}
|