#include <stdio.h>
#include "M051Series.h"
//AIOS
#include "AIOS.h"
#define PLLCON_SETTING CLK_PLLCON_50MHz_HXT
#define PLL_CLOCK 50000000
OSTimerHandle_t Timer500msHandle = NULL;
OSTaskHandle_t CtrlTaskHandle = NULL;
OSTaskHandle_t LedTaskHandle = NULL;
OSSemHandle_t LedSemHandle = NULL;
void CtrlTask( void *pvParameters );
void LedTask( void *pvParameters );
void Timer500ms( void *pvParameters );
void SYS_Init(void)
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init System Clock */
/*---------------------------------------------------------------------------------------------------------*/
/* Enable Internal RC 22.1184MHz clock */
CLK->PWRCON |= CLK_PWRCON_OSC22M_EN_Msk;
/* Waiting for Internal RC clock ready */
while(!(CLK->CLKSTATUS & CLK_CLKSTATUS_OSC22M_STB_Msk));
/* Switch HCLK clock source to Internal RC and and HCLK source divide 1 */
CLK->CLKSEL0 &= ~CLK_CLKSEL0_HCLK_S_Msk;
CLK->CLKSEL0 |= CLK_CLKSEL0_HCLK_S_HIRC;
CLK->CLKDIV &= ~CLK_CLKDIV_HCLK_N_Msk;
CLK->CLKDIV |= CLK_CLKDIV_HCLK(1);
/* Enable external XTAL 12MHz clock */
CLK->PWRCON |= CLK_PWRCON_XTL12M_EN_Msk;
/* Waiting for external XTAL clock ready */
while(!(CLK->CLKSTATUS & CLK_CLKSTATUS_XTL12M_STB_Msk));
/* Set core clock as PLL_CLOCK from PLL */
CLK->PLLCON = PLLCON_SETTING;
while(!(CLK->CLKSTATUS & CLK_CLKSTATUS_PLL_STB_Msk));
CLK->CLKSEL0 &= (~CLK_CLKSEL0_HCLK_S_Msk);
CLK->CLKSEL0 |= CLK_CLKSEL0_HCLK_S_PLL;
/* Update System Core Clock */
/* User can use SystemCoreClockUpdate() to calculate PllClock, SystemCoreClock and CycylesPerUs automatically. */
//SystemCoreClockUpdate();
PllClock = PLL_CLOCK; // PLL
SystemCoreClock = PLL_CLOCK / 1; // HCLK
CyclesPerUs = PLL_CLOCK / 1000000; // For SYS_SysTickDelay()
/* Enable UART module clock */
CLK->APBCLK |= CLK_APBCLK_UART0_EN_Msk;
/* Select UART module clock source */
CLK->CLKSEL1 &= ~CLK_CLKSEL1_UART_S_Msk;
CLK->CLKSEL1 |= CLK_CLKSEL1_UART_S_HXT;
/*---------------------------------------------------------------------------------------------------------*/
/* Init I/O Multi-function */
/*---------------------------------------------------------------------------------------------------------*/
/* Set P3 multi-function pins for UART0 RXD , TXD and CKO */
SYS->P3_MFP &= ~(SYS_MFP_P30_Msk | SYS_MFP_P31_Msk);
SYS->P3_MFP |= (SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0);
}
void UART0_Init()
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init UART */
/*---------------------------------------------------------------------------------------------------------*/
/* Reset UART0 */
SYS->IPRSTC2 |= SYS_IPRSTC2_UART0_RST_Msk;
SYS->IPRSTC2 &= ~SYS_IPRSTC2_UART0_RST_Msk;
/* Configure UART0 and set UART0 Baudrate */
UART0->BAUD = UART_BAUD_MODE2 | UART_BAUD_MODE2_DIVIDER(__HXT, 115200);
UART0->LCR = UART_WORD_LEN_8 | UART_PARITY_NONE | UART_STOP_BIT_1;
}
static void SetupHardware(void)
{
/* Unlock protected registers */
SYS_UnlockReg();
/* Init System, peripheral clock and multi-function I/O */
SYS_Init();
/* Lock protected registers */
SYS_LockReg();
/* Init UART0 for printf */
UART0_Init();
printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %dHz\n", SystemCoreClock);
/*
This sample code will blinking LED on NuTiny EVB board of M058SSAN.
The I/O for LED is P2.3 or P3.6
*/
printf("+---------------------------------------+\n");
printf("| NuTiny EVB LED Sample Code |\n");
printf("+---------------------------------------+\n");
/* Init P2.3 and P3.6 to be output mode */
P2->PMD = (P2->PMD & ~(0x3 << 2*3)) | (1 << 2*3);
P3->PMD = (P3->PMD & ~(0x3 << 2*6)) | (1 << 2*6);
}
/*---------------------------------------------------------------------------------------------------------*/
/* Main Function */
/*---------------------------------------------------------------------------------------------------------*/
int32_t main(void)
{
SetupHardware();
//创建500ms的定时器,分配定时器服务函数为Timer500ms
Timer500msHandle = OSTimerCreate(OSM2T(500), OS_TRUE, Timer500ms, NULL, "T500ms");
//创建信号量
LedSemHandle = OSSemCreate(0);
//创建任务,控制LED灯
CtrlTaskHandle = OSTaskCreate(CtrlTask, NULL, OSMINIMAL_STACK_SIZE, OSLOWEAST_PRIORITY+1, "Ctrl" );
LedTaskHandle = OSTaskCreate(LedTask, NULL, OSMINIMAL_STACK_SIZE, OSLOWEAST_PRIORITY+1, "Led" );
OSTimerStart(Timer500msHandle);
// Start the scheduler.
OSStart();
//if everything is ok, can't reach here
for( ;; );
}
void CtrlTask( void *pvParameters )
{
for( ;; )
{
OSSemPost(LedSemHandle);
OSTaskSleep( OSM2T(500) );
}
}
void LedTask( void *pvParameters )
{
for( ;; )
{
OSSemPend(LedSemHandle, OSPEND_FOREVER_VALUE);
/* Toggle P2.3 */
P23 ^= 1;
/* Toggle P3.6 */
P36 ^= 1;
}
}
void Timer500ms( void *pvParameters )
{
// 500ms软件定时器.
}
|