From c19fb2ab58d0ea77263a5eafa28fe2cf6ca9dec6 Mon Sep 17 00:00:00 2001 From: lin <429426691@qq.com> Date: Fri, 15 Sep 2017 18:10:51 +0800 Subject: Add bsp apollo2 --- bsp/apollo2/board/Sconscript | 14 ++ bsp/apollo2/board/board.c | 146 +++++++++++++++ bsp/apollo2/board/board.h | 17 ++ bsp/apollo2/board/hw_led.c | 302 +++++++++++++++++++++++++++++++ bsp/apollo2/board/hw_led.h | 60 +++++++ bsp/apollo2/board/hw_uart.c | 413 +++++++++++++++++++++++++++++++++++++++++++ bsp/apollo2/board/hw_uart.h | 23 +++ 7 files changed, 975 insertions(+) create mode 100644 bsp/apollo2/board/Sconscript create mode 100644 bsp/apollo2/board/board.c create mode 100644 bsp/apollo2/board/board.h create mode 100644 bsp/apollo2/board/hw_led.c create mode 100644 bsp/apollo2/board/hw_led.h create mode 100644 bsp/apollo2/board/hw_uart.c create mode 100644 bsp/apollo2/board/hw_uart.h (limited to 'bsp/apollo2/board') diff --git a/bsp/apollo2/board/Sconscript b/bsp/apollo2/board/Sconscript new file mode 100644 index 0000000000..aa1e87d860 --- /dev/null +++ b/bsp/apollo2/board/Sconscript @@ -0,0 +1,14 @@ +Import('RTT_ROOT') +Import('rtconfig') +from building import * + +cwd = GetCurrentDir() +src = Glob('*.c') +CPPPATH = [cwd] + +#remove other no use files +#SrcRemove(src, '*.c') + +group = DefineGroup('Board', src, depend = [''], CPPPATH = CPPPATH) + +Return('group') \ No newline at end of file diff --git a/bsp/apollo2/board/board.c b/bsp/apollo2/board/board.c new file mode 100644 index 0000000000..8e1c3b8e88 --- /dev/null +++ b/bsp/apollo2/board/board.c @@ -0,0 +1,146 @@ +/* + * File : board.c + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2009 RT-Thread Develop 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 + * 2017-09-14 Haley first implementation + */ +#include "board.h" + +#include +#include + +#include "am_mcu_apollo.h" +#include "hal/am_hal_clkgen.h" +#include "hal/am_hal_cachectrl.h" +#include "hw_uart.h" + +#define TICK_RATE_HZ RT_TICK_PER_SECOND +#define SYSTICK_CLOCK_HZ ( 32768UL ) +#define WAKE_INTERVAL ( (uint32_t) ((SYSTICK_CLOCK_HZ / TICK_RATE_HZ)) ) + +/** + * This is the timer interrupt service routine. + * + */ +void am_stimer_cmpr0_isr(void) +{ + /* Check the timer interrupt status */ + am_hal_stimer_int_clear(AM_HAL_STIMER_INT_COMPAREA); + am_hal_stimer_compare_delta_set(0, WAKE_INTERVAL); + + if (rt_thread_self() != RT_NULL) + { + /* enter interrupt */ + rt_interrupt_enter(); + + rt_tick_increase(); + + /* leave interrupt */ + rt_interrupt_leave(); + } +} + +/** + * This is the SysTick Configure. + * + */ +void SysTick_Configuration(void) +{ + /* Set the main clk */ + am_hal_clkgen_sysclk_select(AM_HAL_CLKGEN_SYSCLK_MAX); + + /* Enable compare A interrupt in STIMER */ + am_hal_stimer_int_enable(AM_HAL_STIMER_INT_COMPAREA); + + /* Enable the timer interrupt in the NVIC */ + am_hal_interrupt_enable(AM_HAL_INTERRUPT_STIMER_CMPR0); + + /* Configure the STIMER and run */ + am_hal_stimer_config(AM_HAL_STIMER_CFG_CLEAR | AM_HAL_STIMER_CFG_FREEZE); + am_hal_stimer_compare_delta_set(0, WAKE_INTERVAL); + am_hal_stimer_config(AM_HAL_STIMER_XTAL_32KHZ | + AM_HAL_STIMER_CFG_COMPARE_A_ENABLE); +} + +/** + * This is the CacheCtrl Enable. + * + */ +void CacheCtrl_Enable(void) +{ + am_hal_cachectrl_enable(&am_hal_cachectrl_defaults); +} + +/** + * This is the low power operation. + * This function enables several power-saving features of the MCU, and + * disables some of the less-frequently used peripherals. It also sets the + * system clock to 24 MHz. + */ +void am_low_power_init(void) +{ + /* Enable internal buck converters */ + am_hal_pwrctrl_bucks_init(); + + /* Initialize for low power in the power control block */ + am_hal_pwrctrl_low_power_init(); + + /* Turn off the voltage comparator as this is enabled on reset */ + am_hal_vcomp_disable(); + + /* Run the RTC off the LFRC */ + am_hal_rtc_osc_select(AM_HAL_RTC_OSC_LFRC); + + /* Stop the XT and LFRC */ + am_hal_clkgen_osc_stop(AM_HAL_CLKGEN_OSC_XT); + // am_hal_clkgen_osc_stop(AM_HAL_CLKGEN_OSC_LFRC); + + /* Disable the RTC */ + am_hal_rtc_osc_disable(); +} + +/** + * This is the deep power save. + * + */ +void deep_power_save(void) +{ + am_hal_sysctrl_sleep(AM_HAL_SYSCTRL_SLEEP_DEEP); +} + +/** + * This function will initial APOLLO2 board. + */ +void rt_hw_board_init(void) +{ + /* Set the clock frequency */ + SysTick_Configuration(); + + /* Set the default cache configuration */ + CacheCtrl_Enable(); + + /* Configure the board for low power operation */ + //am_low_power_init(); + +#ifdef RT_USING_IDLE_HOOK + rt_thread_idle_sethook(deep_power_save); +#endif + +#ifdef RT_USING_CONSOLE + rt_hw_uart_init(); + rt_console_set_device(RT_CONSOLE_DEVICE_NAME); +#endif + +#ifdef RT_USING_COMPONENTS_INIT + rt_components_board_init(); +#endif +} + +/*@}*/ diff --git a/bsp/apollo2/board/board.h b/bsp/apollo2/board/board.h new file mode 100644 index 0000000000..8bafd7dfa2 --- /dev/null +++ b/bsp/apollo2/board/board.h @@ -0,0 +1,17 @@ +#ifndef __BOARD_H_ +#define __BOARD_H_ + +#include + +// Internal SRAM memory size[Kbytes] <8-256> +// Default: 256 +#define AM_SRAM_SIZE 256 +#define AM_SRAM_END (0x10000000 + AM_SRAM_SIZE * 1024) + +/* USART driver select. */ +#define RT_USING_UART0 +#define RT_USING_UART1 + +void rt_hw_board_init(void); + +#endif /* __BOARD_H__ */ diff --git a/bsp/apollo2/board/hw_led.c b/bsp/apollo2/board/hw_led.c new file mode 100644 index 0000000000..71f6c47db5 --- /dev/null +++ b/bsp/apollo2/board/hw_led.c @@ -0,0 +1,302 @@ +/* + * File : hw_led.c + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2017, 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 + * 2017-09-14 Haley the first version + */ + +#include +#include "am_mcu_apollo.h" +#include "hw_led.h" + +#define AM_GPIO_LED0 46 +#define AM_GPIO_LED1 47 +#define AM_GPIO_LED2 48 +#define AM_GPIO_LED3 49 + +#define AM_NUM_LEDS 4 + +rt_hw_led_t am_psLEDs[AM_NUM_LEDS] = +{ + {AM_GPIO_LED0, AM_LED_ON_LOW | AM_LED_POL_DIRECT_DRIVE_M}, + {AM_GPIO_LED1, AM_LED_ON_LOW | AM_LED_POL_DIRECT_DRIVE_M}, + {AM_GPIO_LED2, AM_LED_ON_LOW | AM_LED_POL_DIRECT_DRIVE_M}, + {AM_GPIO_LED3, AM_LED_ON_LOW | AM_LED_POL_DIRECT_DRIVE_M}, +}; + +/** + * @brief Configures the necessary pins for an array of LEDs + * + * @param LEDNum is the LED number. + * + * This function configures a GPIO to drive an LED in a low-power way. + * + * @return None. + */ +void rt_hw_led_init(rt_uint32_t LEDNum) +{ + rt_hw_led_t *psLED = am_psLEDs + LEDNum; + + /* Handle Direct Drive Versus 3-State (with pull-up or no buffer) */ + if ( AM_LED_POL_DIRECT_DRIVE_M & psLED->Polarity ) + { + /* Configure the pin as a push-pull GPIO output */ + am_hal_gpio_pin_config(psLED->GPIONumber, AM_HAL_GPIO_OUTPUT); + + /* Enable the output driver, and set the output value to the LEDs "ON" state */ + am_hal_gpio_out_enable_bit_set(psLED->GPIONumber); + am_hal_gpio_out_bit_replace(psLED->GPIONumber, + psLED->Polarity & + AM_LED_POL_DIRECT_DRIVE_M); + } + else + { + /* Configure the pin as a tri-state GPIO */ + am_hal_gpio_pin_config(psLED->GPIONumber, AM_HAL_GPIO_3STATE); + + /* Disable the output driver, and set the output value to the LEDs "ON" state */ + am_hal_gpio_out_enable_bit_clear(psLED->GPIONumber); + am_hal_gpio_out_bit_replace(psLED->GPIONumber, + psLED->Polarity & + AM_LED_POL_DIRECT_DRIVE_M ); + } +} + +/** + * @brief Configures the necessary pins for an array of LEDs + * + * @param NumLEDs is the total number of LEDs in the array. + * + * This function configures the GPIOs for an array of LEDs. + * + * @return None. + */ +void rt_hw_led_array_init(rt_uint32_t NumLEDs) +{ + /* Loop through the list of LEDs, configuring each one individually */ + for ( int i = 0; i < NumLEDs; i++ ) + { + rt_hw_led_init(i); + } +} + +/** + * @brief Disables an array of LEDs + * + * @param NumLEDs is the total number of LEDs in the array. + * + * This function disables the GPIOs for an array of LEDs. + * + * @return None. + */ +void rt_hw_led_array_disable(rt_uint32_t NumLEDs) +{ + rt_hw_led_t *psLEDs = am_psLEDs; + + /* Loop through the list of LEDs, configuring each one individually */ + for ( int i = 0; i < NumLEDs; i++ ) + { + am_hal_gpio_pin_config((psLEDs + i)->GPIONumber, AM_HAL_GPIO_DISABLE); + } +} + +/** + * @brief Turns on the requested LED. + * + * @param LEDNum is the LED number for the light to turn on. + * + * This function turns on a single LED. + * + * @return None. + */ +void rt_hw_led_on(rt_uint32_t LEDNum) +{ + rt_hw_led_t *psLEDs = am_psLEDs; + + /* Handle Direct Drive Versus 3-State (with pull-up or no buffer) */ + if ( AM_LED_POL_DIRECT_DRIVE_M & psLEDs[LEDNum].Polarity ) + { + /* Set the output to the correct state for the LED */ + am_hal_gpio_out_bit_replace(psLEDs[LEDNum].GPIONumber, + psLEDs[LEDNum].Polarity & + AM_LED_POL_POLARITY_M ); + } + else + { + /* Turn on the output driver for the LED */ + am_hal_gpio_out_enable_bit_set(psLEDs[LEDNum].GPIONumber); + } +} + +/** + * @brief Turns off the requested LED. + * + * @param LEDNum is the LED number for the light to turn off. + * + * This function turns off a single LED. + * + * @return None. + */ +void rt_hw_led_off(rt_uint32_t LEDNum) +{ + rt_hw_led_t *psLEDs = am_psLEDs; + + /* Handle Direct Drive Versus 3-State (with pull-up or no buffer) */ + if ( AM_LED_POL_DIRECT_DRIVE_M & psLEDs[LEDNum].Polarity ) + { + /* Set the output to the correct state for the LED */ + am_hal_gpio_out_bit_replace(psLEDs[LEDNum].GPIONumber, + !(psLEDs[LEDNum].Polarity & + AM_LED_POL_POLARITY_M) ); + } + else + { + /* Turn off the output driver for the LED */ + am_hal_gpio_out_enable_bit_clear(psLEDs[LEDNum].GPIONumber); + } +} + +/** + * @brief Toggles the requested LED. + * + * @param LEDNum is the LED number for the light to toggle. + * + * This function toggles a single LED. + * + * @return None. + */ +void rt_hw_led_toggle(rt_uint32_t LEDNum) +{ + rt_hw_led_t *psLEDs = am_psLEDs; + + /* Handle Direct Drive Versus 3-State (with pull-up or no buffer) */ + if ( AM_LED_POL_DIRECT_DRIVE_M & psLEDs[LEDNum].Polarity ) + { + am_hal_gpio_out_bit_toggle(psLEDs[LEDNum].GPIONumber); + } + else + { + /* Check to see if the LED pin is enabled */ + if ( am_hal_gpio_out_enable_bit_get(psLEDs[LEDNum].GPIONumber) ) + { + /* If it was enabled, turn if off */ + am_hal_gpio_out_enable_bit_clear(psLEDs[LEDNum].GPIONumber); + } + else + { + /* If it was not enabled, turn if on */ + am_hal_gpio_out_enable_bit_set(psLEDs[LEDNum].GPIONumber); + } + } +} + +/** + * @brief Gets the state of the requested LED. + * + * @param LEDNum is the LED to check. + * + * This function checks the state of a single LED. + * + * @return 1(true) if the LED is on. + */ +int rt_hw_led_get(rt_uint32_t LEDNum) +{ + rt_hw_led_t *psLEDs = am_psLEDs; + + /* Handle Direct Drive Versus 3-State (with pull-up or no buffer) */ + if ( AM_LED_POL_DIRECT_DRIVE_M & psLEDs[LEDNum].Polarity ) + { + /* Mask to the GPIO bit position for this GPIO number */ + uint64_t ui64Mask = 0x01l << psLEDs[LEDNum].GPIONumber; + + /* Extract the state of this bit and return it */ + return !!(am_hal_gpio_input_read() & ui64Mask); + } + else + { + return am_hal_gpio_out_enable_bit_get( + psLEDs[LEDNum].GPIONumber); + } +} + +/** + * @brief Display a binary value using LEDs. + * + * @param NumLEDs is the number of LEDs in the array. + * @param Value is the value to display on the LEDs. + * + * This function displays a value in binary across an array of LEDs. + * + * @return None. + */ +void rt_hw_led_array_out(rt_uint32_t NumLEDs, rt_uint32_t Value) +{ + for ( int i = 0; i < NumLEDs; i++ ) + { + if ( Value & (1 << i) ) + { + rt_hw_led_on(i); + } + else + { + rt_hw_led_off(i); + } + } +} + +#ifdef RT_USING_FINSH +#include +static rt_uint8_t led_inited = 0; +void led(rt_uint32_t led, rt_uint32_t value) +{ + /* init led configuration if it's not inited. */ + if (!led_inited) + { +// rt_hw_led_init(0); +// rt_hw_led_init(1); + led_inited = 1; + } + + if ( led == 0 ) + { + /* set led status */ + switch (value) + { + case 0: + rt_hw_led_off(0); + break; + case 1: + rt_hw_led_on(0); + break; + default: + break; + } + } + + if ( led == 1 ) + { + /* set led status */ + switch (value) + { + case 0: + rt_hw_led_off(1); + break; + case 1: + rt_hw_led_on(1); + break; + default: + break; + } + } +} +FINSH_FUNCTION_EXPORT(led, set led[0 - 1] on[1] or off[0].) +#endif + +/*@}*/ diff --git a/bsp/apollo2/board/hw_led.h b/bsp/apollo2/board/hw_led.h new file mode 100644 index 0000000000..8f954a7705 --- /dev/null +++ b/bsp/apollo2/board/hw_led.h @@ -0,0 +1,60 @@ +/* + * File : hw_led.h + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2017, 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 + * 2017-09-14 Haley the first version + */ + +#ifndef __HW_LED_H +#define __HW_LED_H + +#include + +/** + * @brief LED polarity macros + * + */ +#define AM_LED_POL_POLARITY_M 0x1 +#define AM_LED_ON_HIGH 0x1 +#define AM_LED_ON_LOW 0x0 + +/** + * @brief LED direct drive indicator macro + * Or this in with the polarity value to use the GPIO DATA register instead of + * the GPIO DATA ENABLE register to directly drive an LED buffer. + */ +#define AM_LED_POL_DIRECT_DRIVE_M 0x2 + + +/** + * @brief Structure for keeping track of LEDs + * + */ +typedef struct +{ + rt_uint32_t GPIONumber; + rt_uint32_t Polarity; +} +rt_hw_led_t; + +/** + * @brief External function definitions + * + */ +void rt_hw_led_init(rt_uint32_t LEDNum); +void rt_hw_led_array_init(rt_uint32_t NumLEDs); +void rt_hw_led_array_disable(rt_uint32_t NumLEDs); +void rt_hw_led_on(rt_uint32_t LEDNum); +void rt_hw_led_off(rt_uint32_t LEDNum); +void rt_hw_led_toggle(rt_uint32_t LEDNum); +int rt_hw_led_get(rt_uint32_t LEDNum); +void rt_hw_led_array_out(rt_uint32_t NumLEDs, rt_uint32_t Value); + +#endif // __HW_LED_H diff --git a/bsp/apollo2/board/hw_uart.c b/bsp/apollo2/board/hw_uart.c new file mode 100644 index 0000000000..d1fa7af771 --- /dev/null +++ b/bsp/apollo2/board/hw_uart.c @@ -0,0 +1,413 @@ +/* + * File : hw_uart.c + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2017, 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 + * 2017-09-15 Haley the first version + */ + +#include "am_mcu_apollo.h" +#include "hw_uart.h" +#include "board.h" +#include + +/* USART0 */ +#define AM_UART0_INST 0 + +#define UART0_GPIO_RX 2 +#define UART0_GPIO_CFG_RX AM_HAL_PIN_2_UART0RX +#define UART0_GPIO_TX 1 +#define UART0_GPIO_CFG_TX AM_HAL_PIN_1_UART0TX + +/* USART1 */ +#define AM_UART1_INST 1 + +#define UART1_GPIO_RX 9 +#define UART1_GPIO_CFG_RX AM_HAL_PIN_9_UART1RX +#define UART1_GPIO_TX 8 +#define UART1_GPIO_CFG_TX AM_HAL_PIN_8_UART1TX + +/* AM uart driver */ +struct am_uart +{ + uint32_t uart_device; + uint32_t uart_interrupt; +}; + +/** + * @brief UART configuration settings + * + */ +am_hal_uart_config_t g_sUartConfig = +{ + .ui32BaudRate = 115200, + .ui32DataBits = AM_HAL_UART_DATA_BITS_8, + .bTwoStopBits = false, + .ui32Parity = AM_HAL_UART_PARITY_NONE, + .ui32FlowCtrl = AM_HAL_UART_FLOW_CTRL_NONE, +}; + +/** + * @brief Enable the UART + * + * @param Uart driver + * + * This function is Enable the UART + * + * @return None. + */ +static void rt_hw_uart_enable(struct am_uart* uart) +{ + /* Enable the UART clock */ + am_hal_uart_clock_enable(uart->uart_device); + + /* Enable the UART */ + am_hal_uart_enable(uart->uart_device); + +#if defined(RT_USING_UART0) + /* Make sure the UART RX and TX pins are enabled */ + am_hal_gpio_pin_config(UART0_GPIO_TX, UART0_GPIO_CFG_TX); + am_hal_gpio_pin_config(UART0_GPIO_RX, UART0_GPIO_CFG_RX | AM_HAL_GPIO_PULL12K); +#endif /* RT_USING_UART0 */ + +#if defined(RT_USING_UART1) + /* Make sure the UART RX and TX pins are enabled */ + am_hal_gpio_pin_config(UART1_GPIO_TX, UART1_GPIO_CFG_TX); + am_hal_gpio_pin_config(UART1_GPIO_RX, UART1_GPIO_CFG_RX | AM_HAL_GPIO_PULL12K); +#endif /* RT_USING_UART1 */ +} + +/** + * @brief Disable the UART + * + * @param Uart driver + * + * This function is Disable the UART + * + * @return None. + */ +static void rt_hw_uart_disable(struct am_uart* uart) +{ + /* Clear all interrupts before sleeping as having a pending UART interrupt burns power */ + am_hal_uart_int_clear(uart->uart_device, 0xFFFFFFFF); + + /* Disable the UART */ + am_hal_uart_disable(uart->uart_device); + +#if defined(RT_USING_UART0) + /* Disable the UART pins */ + am_hal_gpio_pin_config(UART0_GPIO_TX, AM_HAL_PIN_DISABLE); + am_hal_gpio_pin_config(UART0_GPIO_RX, AM_HAL_PIN_DISABLE); +#endif /* RT_USING_UART0 */ + +#if defined(RT_USING_UART1) + /* Disable the UART pins */ + am_hal_gpio_pin_config(UART1_GPIO_TX, AM_HAL_PIN_DISABLE); + am_hal_gpio_pin_config(UART1_GPIO_RX, AM_HAL_PIN_DISABLE); +#endif /* RT_USING_UART1 */ + + /* Disable the UART clock */ + am_hal_uart_clock_disable(uart->uart_device); +} + +/** + * @brief UART-based string print function. + * + * @param Send buff + * + * This function is used for printing a string via the UART, which for some + * MCU devices may be multi-module. + * + * @return None. + */ +void rt_hw_uart_send_string(char *pcString) +{ + am_hal_uart_string_transmit_polled(AM_UART0_INST, pcString); + + /* Wait until busy bit clears to make sure UART fully transmitted last byte */ + while ( am_hal_uart_flags_get(AM_UART0_INST) & AM_HAL_UART_FR_BUSY ); +} + +static rt_err_t am_configure(struct rt_serial_device *serial, struct serial_configure *cfg) +{ + struct am_uart* uart; + + RT_ASSERT(serial != RT_NULL); + RT_ASSERT(cfg != RT_NULL); + + uart = (struct am_uart *)serial->parent.user_data; + + RT_ASSERT(uart != RT_NULL); + + /* Get the configure */ + g_sUartConfig.ui32BaudRate = cfg->baud_rate; + g_sUartConfig.ui32DataBits = cfg->data_bits; + + if (cfg->stop_bits == STOP_BITS_1) + g_sUartConfig.bTwoStopBits = false; + else if (cfg->stop_bits == STOP_BITS_2) + g_sUartConfig.bTwoStopBits = true; + + g_sUartConfig.ui32Parity = cfg->parity; + g_sUartConfig.ui32FlowCtrl = AM_HAL_UART_PARITY_NONE; + + /* Configure the UART */ + am_hal_uart_config(uart->uart_device, &g_sUartConfig); + + /* Enable the UART */ + am_hal_uart_enable(uart->uart_device); + + return RT_EOK; +} + +static rt_err_t am_control(struct rt_serial_device *serial, int cmd, void *arg) +{ + struct am_uart* uart; + //rt_uint32_t ctrl_arg = (rt_uint32_t)(arg); + + RT_ASSERT(serial != RT_NULL); + uart = (struct am_uart *)serial->parent.user_data; + + RT_ASSERT(uart != RT_NULL); + + switch (cmd) + { + /* disable interrupt */ + case RT_DEVICE_CTRL_CLR_INT: + rt_hw_uart_disable(uart); + break; + /* enable interrupt */ + case RT_DEVICE_CTRL_SET_INT: + rt_hw_uart_enable(uart); + break; + /* UART config */ + case RT_DEVICE_CTRL_CONFIG : + break; + } + + return RT_EOK; +} + +static int am_putc(struct rt_serial_device *serial, char c) +{ + struct am_uart* uart; + + RT_ASSERT(serial != RT_NULL); + uart = (struct am_uart *)serial->parent.user_data; + + RT_ASSERT(uart != RT_NULL); + + am_hal_uart_char_transmit_polled(uart->uart_device, c); + + return 1; +} + +static int am_getc(struct rt_serial_device *serial) +{ + char c; + int ch; + struct am_uart* uart; + + RT_ASSERT(serial != RT_NULL); + uart = (struct am_uart *)serial->parent.user_data; + + RT_ASSERT(uart != RT_NULL); + + ch = -1; + if (am_hal_uart_flags_get(uart->uart_device) & AM_HAL_UART_FR_RX_FULL) + { + am_hal_uart_char_receive_polled(uart->uart_device, &c); + ch = c & 0xff; + } + + return ch; +} + +/** + * Uart common interrupt process. This need add to uart ISR. + * + * @param serial serial device + */ +static void uart_isr(struct rt_serial_device *serial) +{ + uint32_t status; + + RT_ASSERT(serial != RT_NULL); + struct am_uart *uart = (struct am_uart *) serial->parent.user_data; + + RT_ASSERT(uart != RT_NULL); + + /* Read the interrupt status */ + status = am_hal_uart_int_status_get(uart->uart_device, false); + + //rt_kprintf("status is %d\r\n", status); + + /* Clear the UART interrupt */ + am_hal_uart_int_clear(uart->uart_device, status); + + if (status & (AM_HAL_UART_INT_RX_TMOUT)) + { + rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_TIMEOUT); + } + + if (status & AM_HAL_UART_INT_RX) + { + rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND); + } + + if (status & AM_HAL_UART_INT_TX) + { + //rt_hw_serial_isr(serial, RT_SERIAL_EVENT_TX_DONE); + } +} + +static const struct rt_uart_ops am_uart_ops = +{ + am_configure, + am_control, + am_putc, + am_getc, +}; + +#if defined(RT_USING_UART0) +/* UART0 device driver structure */ +struct am_uart uart0 = +{ + AM_UART0_INST, + AM_HAL_INTERRUPT_UART0 +}; +static struct rt_serial_device serial0; + +void am_uart0_isr(void) +{ + /* enter interrupt */ + rt_interrupt_enter(); + + uart_isr(&serial0); + + /* leave interrupt */ + rt_interrupt_leave(); +} +#endif /* RT_USING_UART0 */ + +#if defined(RT_USING_UART1) +/* UART1 device driver structure */ +struct am_uart uart1 = +{ + AM_UART1_INST, + AM_HAL_INTERRUPT_UART1 +}; +static struct rt_serial_device serial1; + +void am_uart1_isr(void) +{ + /* enter interrupt */ + rt_interrupt_enter(); + + uart_isr(&serial1); + + /* leave interrupt */ + rt_interrupt_leave(); +} +#endif /* RT_USING_UART1 */ + +static void GPIO_Configuration(void) +{ +#if defined(RT_USING_UART0) + /* Make sure the UART RX and TX pins are enabled */ + am_hal_gpio_pin_config(UART0_GPIO_TX, UART0_GPIO_CFG_TX); + am_hal_gpio_pin_config(UART0_GPIO_RX, UART0_GPIO_CFG_RX | AM_HAL_GPIO_PULL12K); +#endif /* RT_USING_UART0 */ + +#if defined(RT_USING_UART1) + /* Make sure the UART RX and TX pins are enabled */ + am_hal_gpio_pin_config(UART1_GPIO_TX, UART1_GPIO_CFG_TX); + am_hal_gpio_pin_config(UART1_GPIO_RX, UART1_GPIO_CFG_RX | AM_HAL_GPIO_PULL12K); +#endif /* RT_USING_UART1 */ +} + +static void RCC_Configuration(struct am_uart* uart) +{ + /* Power on the selected UART */ + am_hal_uart_pwrctrl_enable(uart->uart_device); + + /* Start the UART interface, apply the desired configuration settings */ + am_hal_uart_clock_enable(uart->uart_device); + + /* Disable the UART before configuring it */ + am_hal_uart_disable(uart->uart_device); + + /* Configure the UART */ + am_hal_uart_config(uart->uart_device, &g_sUartConfig); + + /* Enable the UART */ + am_hal_uart_enable(uart->uart_device); + + /* Enable the UART FIFO */ + //am_hal_uart_fifo_config(uart->uart_device, AM_HAL_UART_TX_FIFO_1_2 | AM_HAL_UART_RX_FIFO_1_2); +} + +static void NVIC_Configuration(struct am_uart* uart) +{ + /* Enable interrupts */ + am_hal_uart_int_enable(uart->uart_device, AM_HAL_UART_INT_RX); + + /* Enable the uart interrupt in the NVIC */ + am_hal_interrupt_enable(uart->uart_interrupt); + am_hal_uart_int_clear(uart->uart_device, 0xFFFFFFFF); +} + +/** + * @brief Initialize the UART + * + * This function initialize the UART + * + * @return None. + */ +void rt_hw_uart_init(void) +{ + struct am_uart* uart; + struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT; + + GPIO_Configuration(); + +#if defined(RT_USING_UART0) + uart = &uart0; + config.baud_rate = BAUD_RATE_115200; + + RCC_Configuration(uart); + NVIC_Configuration(uart); + + serial0.ops = &am_uart_ops; + serial0.config = config; + + /* register UART1 device */ + rt_hw_serial_register(&serial0, "uart0", + RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | + RT_DEVICE_FLAG_INT_TX, uart); +#endif /* RT_USING_UART0 */ + +#if defined(RT_USING_UART1) + uart = &uart1; + config.baud_rate = BAUD_RATE_115200; + + RCC_Configuration(uart); + NVIC_Configuration(uart); + + serial1.ops = &am_uart_ops; + serial1.config = config; + + /* register UART1 device */ + rt_hw_serial_register(&serial1, "uart1", + RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | + RT_DEVICE_FLAG_INT_TX, uart); +#endif /* RT_USING_UART1 */ +} + +/*@}*/ diff --git a/bsp/apollo2/board/hw_uart.h b/bsp/apollo2/board/hw_uart.h new file mode 100644 index 0000000000..afef169804 --- /dev/null +++ b/bsp/apollo2/board/hw_uart.h @@ -0,0 +1,23 @@ +/* + * File : hw_uart.c + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2017, 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 + * 2017-09-14 Haley the first version + */ + +#ifndef __HW_UART_H_ +#define __HW_UART_H_ + +#include + +void rt_hw_uart_init(void); +void rt_hw_uart_send_string(char *pcString); + +#endif // __HW_UART_H_ -- cgit v1.3.1