From 3abf1fd5baa60954ed95259e33d1d483110c6125 Mon Sep 17 00:00:00 2001 From: Bright Pan Date: Sat, 29 Nov 2014 12:34:18 +0800 Subject: [PATCH] Add uart driver and finsh function --- .../Source/ARM/startup_M051Series.s | 2 +- bsp/nuvoton_m05x/applications/application.c | 2 +- bsp/nuvoton_m05x/drivers/board.c | 32 ++++- bsp/nuvoton_m05x/drivers/board.h | 2 + bsp/nuvoton_m05x/drivers/usart.c | 133 ++++++++++++++++++ bsp/nuvoton_m05x/drivers/usart.h | 26 ++++ bsp/nuvoton_m05x/project.uvprojx | 83 ++++++++++- bsp/nuvoton_m05x/rtconfig.h | 14 +- 8 files changed, 278 insertions(+), 16 deletions(-) create mode 100644 bsp/nuvoton_m05x/drivers/usart.c create mode 100644 bsp/nuvoton_m05x/drivers/usart.h diff --git a/bsp/nuvoton_m05x/Libraries/CMSIS/Nuvoton/M051Series/Source/ARM/startup_M051Series.s b/bsp/nuvoton_m05x/Libraries/CMSIS/Nuvoton/M051Series/Source/ARM/startup_M051Series.s index 7cc01d3ec..b8e08d5bc 100644 --- a/bsp/nuvoton_m05x/Libraries/CMSIS/Nuvoton/M051Series/Source/ARM/startup_M051Series.s +++ b/bsp/nuvoton_m05x/Libraries/CMSIS/Nuvoton/M051Series/Source/ARM/startup_M051Series.s @@ -10,7 +10,7 @@ ; * ; ******************************************************************************/ -Stack_Size EQU 0x00000200 +Stack_Size EQU 0x00000100 AREA STACK, NOINIT, READWRITE, ALIGN=3 Stack_Mem SPACE Stack_Size diff --git a/bsp/nuvoton_m05x/applications/application.c b/bsp/nuvoton_m05x/applications/application.c index bf48aeb4a..652c12a1a 100644 --- a/bsp/nuvoton_m05x/applications/application.c +++ b/bsp/nuvoton_m05x/applications/application.c @@ -56,7 +56,7 @@ static void rt_init_thread_entry(void* parameter) /* Create led thread */ led_thread = rt_thread_create("led", led_thread_entry, RT_NULL, - 256, 20, 20); + 128, 20, 20); if(led_thread != RT_NULL) rt_thread_startup(led_thread); } diff --git a/bsp/nuvoton_m05x/drivers/board.c b/bsp/nuvoton_m05x/drivers/board.c index 8c9bd8ea7..15beaa6d8 100644 --- a/bsp/nuvoton_m05x/drivers/board.c +++ b/bsp/nuvoton_m05x/drivers/board.c @@ -14,9 +14,8 @@ #include #include - #include "board.h" - +#include "usart.h" /* RT_USING_COMPONENTS_INIT */ #ifdef RT_USING_COMPONENTS_INIT #include @@ -44,7 +43,7 @@ void NVIC_Configuration(void) * @param nCount: specifies the delay time length. * @retval None */ -static void Delay(__IO uint32_t nCount) +static void delay(__IO uint32_t nCount) { /* Decrement nCount value */ while (nCount != 0) @@ -53,6 +52,30 @@ static void Delay(__IO uint32_t nCount) } } +static void rt_hw_system_init(void) +{ + /*---------------------------------------------------------------------------------------------------------*/ + /* Init System Clock */ + /*---------------------------------------------------------------------------------------------------------*/ + SYS_UnlockReg(); + /* Enable Internal RC 22.1184MHz clock */ + CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk); + + /* Waiting for Internal RC clock ready */ + CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk); + + /* Switch HCLK clock source to Internal RC and HCLK source divide 1 */ + CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1)); + + /* Set core clock as PLL_CLOCK from PLL */ + CLK_SetCoreClock(BOARD_PLL_CLOCK); + + /* Set SysTick clock source to HCLK source divide 2 */ + CLK_SetSysTickClockSrc(CLK_CLKSEL0_STCLK_S_HCLK_DIV2); + + SYS_LockReg(); +} + /** * This is the timer interrupt service routine. * @@ -75,10 +98,13 @@ void rt_hw_board_init() /* NVIC Configuration */ NVIC_Configuration(); + /* Configure the system clock */ + rt_hw_system_init(); /* Configure the SysTick */ SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND); /* Initial usart deriver, and set console device */ + rt_hw_usart_init(); #ifdef RT_USING_CONSOLE rt_console_set_device(RT_CONSOLE_DEVICE_NAME); #endif diff --git a/bsp/nuvoton_m05x/drivers/board.h b/bsp/nuvoton_m05x/drivers/board.h index e2f4ed673..6c6894402 100644 --- a/bsp/nuvoton_m05x/drivers/board.h +++ b/bsp/nuvoton_m05x/drivers/board.h @@ -18,6 +18,8 @@ #include "M051Series.h" +#define BOARD_PLL_CLOCK 50000000 + /* board configuration */ // Internal SRAM memory size[Kbytes] // Default: 64 diff --git a/bsp/nuvoton_m05x/drivers/usart.c b/bsp/nuvoton_m05x/drivers/usart.c new file mode 100644 index 000000000..91afa4130 --- /dev/null +++ b/bsp/nuvoton_m05x/drivers/usart.c @@ -0,0 +1,133 @@ +/* + * File : usart.c + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2006-2014, RT-Thread Development Team + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rt-thread.org/license/LICENSE + * + * Change Logs: + * Date Author Notes + * 2014-11-29 Bright the first version + */ + +#include "M051Series.h" +#include +#include "usart.h" + + +static rt_err_t m05x_configure(struct rt_serial_device *serial, struct serial_configure *cfg) +{ + UART_T* uart; + + uart = (UART_T *)serial->parent.user_data; +#if defined(RT_USING_UART0) + if (uart == UART0) { + /* Enable UART module clock */ + CLK_EnableModuleClock(UART0_MODULE); + /* Select UART module clock source */ + CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_PLL, CLK_CLKDIV_UART(1)); + + /* Set P3 multi-function pins for UART0 RXD and TXD */ + SYS->P3_MFP &= ~(SYS_MFP_P30_Msk | SYS_MFP_P31_Msk); + SYS->P3_MFP |= (SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0); + + /* Reset IP */ + SYS_ResetModule(UART0_RST); + /* Configure UART0 and set UART0 Baudrate */ + UART_Open(UART0, cfg->baud_rate); + + /* Enable Interrupt */ + UART_EnableInt(UART0, UART_IER_RDA_IEN_Msk); + } +#endif /* RT_USING_UART0 */ + +#if defined(RT_USING_UART1) +#endif /* RT_USING_UART1 */ + return RT_EOK; +} + +static rt_err_t m05x_control(struct rt_serial_device *serial, int cmd, void *arg) +{ + UART_T* uart; + uart = (UART_T *)serial->parent.user_data; + + switch (cmd) + { + case RT_DEVICE_CTRL_CLR_INT: { + /* Disable Interrupt */ + UART_DisableInt(uart, UART_IER_RDA_IEN_Msk); + break; + } + case RT_DEVICE_CTRL_SET_INT: { + UART_EnableInt(uart, UART_IER_RDA_IEN_Msk); + break; + } + } + + return RT_EOK; +} + +static int m05x_putc(struct rt_serial_device *serial, char c) +{ + UART_T* uart; + uart = (UART_T *)serial->parent.user_data; + if (UART_IS_TX_FULL(uart)) { + UART_WAIT_TX_EMPTY(uart); + + } + UART_WRITE(uart, c); + return 1; +} + +static int m05x_getc(struct rt_serial_device *serial) +{ + int ch = -1; + UART_T* uart; + uart = (UART_T *)serial->parent.user_data; + + if (UART_IS_RX_READY(uart)) + ch = UART_READ(uart); + return ch; +} + +static const struct rt_uart_ops m05x_uart_ops = +{ + m05x_configure, + m05x_control, + m05x_putc, + m05x_getc, +}; + +#if defined(RT_USING_UART0) +struct rt_serial_device serial0; + +void UART0_IRQHandler(void) +{ + /* enter interrupt */ + rt_interrupt_enter(); + + if (UART_IS_RX_READY(UART0)) { + rt_hw_serial_isr(&serial0, RT_SERIAL_EVENT_RX_IND); + } + /* leave interrupt */ + rt_interrupt_leave(); +} +#endif /* RT_USING_UART0 */ + +void rt_hw_usart_init(void) +{ +#ifdef RT_USING_UART0 + struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT; + config.baud_rate = BAUD_RATE_115200; + serial0.ops = &m05x_uart_ops; + serial0.config = config; + + /* register UART0 device */ + rt_hw_serial_register(&serial0, "uart0", + RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX, + UART0); +#endif /* RT_USING_UART0 */ + +} diff --git a/bsp/nuvoton_m05x/drivers/usart.h b/bsp/nuvoton_m05x/drivers/usart.h new file mode 100644 index 000000000..ba66eba27 --- /dev/null +++ b/bsp/nuvoton_m05x/drivers/usart.h @@ -0,0 +1,26 @@ +/* + * File : usart.c + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2006-2014, RT-Thread Development Team + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rt-thread.org/license/LICENSE + * + * Change Logs: + * Date Author Notes + * 2014-11-29 Bright the first version + */ + +#ifndef __USART_H__ +#define __USART_H__ + +#include +#include + +#define RT_USING_UART0 +//#define RT_USING_UART1 + +void rt_hw_usart_init(void); + +#endif diff --git a/bsp/nuvoton_m05x/project.uvprojx b/bsp/nuvoton_m05x/project.uvprojx index 8972ab2de..a1d4df386 100644 --- a/bsp/nuvoton_m05x/project.uvprojx +++ b/bsp/nuvoton_m05x/project.uvprojx @@ -207,8 +207,8 @@ 1 1 1 - 1 - 1 + 0 + 0 "Cortex-M0" 0 @@ -363,7 +363,7 @@ INIT_SYSCLK_AT_BOOTING - .;..\..\components\drivers\include;..\..\components\init;..\..\include;..\..\libcpu\arm\common;..\..\libcpu\arm\cortex-m0;Libraries\CMSIS\Include;Libraries\CMSIS\Nuvoton\M051Series\Include;Libraries\StdDriver\inc;applications;drivers + .;..\..\components\drivers\include;..\..\components\finsh;..\..\components\init;..\..\include;..\..\libcpu\arm\common;..\..\libcpu\arm\cortex-m0;Libraries\CMSIS\Include;Libraries\CMSIS\Nuvoton\M051Series\Include;Libraries\StdDriver\inc;applications;drivers @@ -396,7 +396,7 @@ .\nuvoton_m05x.sct - --keep __rt_init* + --keep __fsym_* --keep __vsym_* --keep __rt_init* @@ -431,6 +431,11 @@ 1 drivers\led.c + + usart.c + 1 + drivers\usart.c + @@ -648,6 +653,76 @@ + + finsh + + + shell.c + 1 + ..\..\components\finsh\shell.c + + + symbol.c + 1 + ..\..\components\finsh\symbol.c + + + cmd.c + 1 + ..\..\components\finsh\cmd.c + + + finsh_compiler.c + 1 + ..\..\components\finsh\finsh_compiler.c + + + finsh_error.c + 1 + ..\..\components\finsh\finsh_error.c + + + finsh_heap.c + 1 + ..\..\components\finsh\finsh_heap.c + + + finsh_init.c + 1 + ..\..\components\finsh\finsh_init.c + + + finsh_node.c + 1 + ..\..\components\finsh\finsh_node.c + + + finsh_ops.c + 1 + ..\..\components\finsh\finsh_ops.c + + + finsh_parser.c + 1 + ..\..\components\finsh\finsh_parser.c + + + finsh_var.c + 1 + ..\..\components\finsh\finsh_var.c + + + finsh_vm.c + 1 + ..\..\components\finsh\finsh_vm.c + + + finsh_token.c + 1 + ..\..\components\finsh\finsh_token.c + + + Components diff --git a/bsp/nuvoton_m05x/rtconfig.h b/bsp/nuvoton_m05x/rtconfig.h index a8d3a4881..7face48d7 100644 --- a/bsp/nuvoton_m05x/rtconfig.h +++ b/bsp/nuvoton_m05x/rtconfig.h @@ -10,7 +10,7 @@ /* PRIORITY_MAX */ #define RT_THREAD_PRIORITY_MAX 32 - +#define IDLE_THREAD_STACK_SIZE 128 /* Tick per Second */ #define RT_TICK_PER_SECOND 100 @@ -54,7 +54,6 @@ /* Using Small MM */ #define RT_USING_SMALL_MEM -#define RT_USING_TINY_SIZE // #define RT_USING_COMPONENTS_INIT @@ -68,19 +67,20 @@ #define RT_USING_SERIAL /* SECTION: Console options */ -//#define RT_USING_CONSOLE +#define RT_USING_CONSOLE /* the buffer size of console*/ -#define RT_CONSOLEBUF_SIZE 128 +#define RT_CONSOLEBUF_SIZE 64 // -#define RT_CONSOLE_DEVICE_NAME "uart1" +#define RT_CONSOLE_DEVICE_NAME "uart0" /* SECTION: finsh, a C-Express shell */ -//#define RT_USING_FINSH +#define RT_USING_FINSH /* configure finsh parameters */ #define FINSH_THREAD_PRIORITY 25 -#define FINSH_THREAD_STACK_SIZE 1024 +#define FINSH_THREAD_STACK_SIZE 512 +#define FINSH_USING_HISTORY 0 #define FINSH_HISTORY_LINES 1 /* Using symbol table */ #define FINSH_USING_SYMTAB -- GitLab