2018-03-01 21:01:23 +08:00
|
|
|
/*
|
|
|
|
* File : drv_uart.c
|
|
|
|
* This file is part of RT-Thread RTOS
|
|
|
|
* COPYRIGHT (C) 2012, RT-Thread Development Team
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2018-02-22 Tanek first version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rtthread.h>
|
|
|
|
#include <rthw.h>
|
|
|
|
|
|
|
|
#include <SMM_MPS2.h>
|
|
|
|
|
|
|
|
#ifdef RT_USING_UART
|
|
|
|
|
|
|
|
#ifndef RT_USING_DEVICE
|
|
|
|
#error "you must define RT_USING_DEVICE with uart device"
|
2018-03-02 16:13:02 +08:00
|
|
|
#else
|
|
|
|
#include <rtdevice.h>
|
2018-03-01 21:01:23 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* uart driver */
|
2018-03-02 16:13:02 +08:00
|
|
|
struct v2m_uart
|
2018-03-01 21:01:23 +08:00
|
|
|
{
|
2018-03-02 16:13:02 +08:00
|
|
|
struct rt_serial_device serial;
|
2018-03-01 21:01:23 +08:00
|
|
|
CMSDK_UART_TypeDef * uart_base;
|
|
|
|
|
|
|
|
CMSDK_GPIO_TypeDef * rx_pingpio; // Pin GPIO
|
|
|
|
CMSDK_GPIO_TypeDef * tx_pingpio;
|
|
|
|
uint8_t rx_pinnum; // Pin Number
|
|
|
|
uint8_t tx_pinnum;
|
|
|
|
|
|
|
|
IRQn_Type uart_irq_rx;
|
2018-03-02 16:13:02 +08:00
|
|
|
//IRQn_Type uart_irq_tx;
|
2018-03-01 21:01:23 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef RT_USING_UART0
|
2018-03-02 16:13:02 +08:00
|
|
|
struct v2m_uart uart0_device;
|
2018-03-01 21:01:23 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RT_USING_UART1
|
2018-03-02 16:13:02 +08:00
|
|
|
struct v2m_uart uart1_device;
|
2018-03-01 21:01:23 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RT_USING_UART2
|
2018-03-02 16:13:02 +08:00
|
|
|
struct v2m_uart uart2_device;
|
2018-03-01 21:01:23 +08:00
|
|
|
#endif
|
|
|
|
|
2018-03-02 16:13:02 +08:00
|
|
|
static void uart_irq_handler(struct rt_serial_device *serial)
|
2018-03-01 21:01:23 +08:00
|
|
|
{
|
|
|
|
uint32_t status;
|
2018-03-02 16:13:02 +08:00
|
|
|
struct v2m_uart *uart;
|
2018-03-01 21:01:23 +08:00
|
|
|
|
2018-03-02 16:13:02 +08:00
|
|
|
uart = (struct v2m_uart *)serial->parent.user_data;
|
2018-03-01 21:01:23 +08:00
|
|
|
|
|
|
|
/* enter interrupt */
|
|
|
|
rt_interrupt_enter();
|
2018-03-02 16:13:02 +08:00
|
|
|
|
|
|
|
status = uart->uart_base->INTSTATUS;
|
|
|
|
rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
|
2018-03-01 21:01:23 +08:00
|
|
|
uart->uart_base->INTCLEAR = status;
|
|
|
|
|
|
|
|
/* leave interrupt */
|
|
|
|
rt_interrupt_leave();
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef RT_USING_UART0
|
|
|
|
void UART0RX_Handler(void)
|
|
|
|
{
|
2018-03-02 16:13:02 +08:00
|
|
|
uart_irq_handler(&uart0_device.serial);
|
2018-03-01 21:01:23 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RT_USING_UART1
|
|
|
|
void UART1RX_Handler(void)
|
|
|
|
{
|
2018-03-02 16:13:02 +08:00
|
|
|
uart_irq_handler(&uart1_device.serial);
|
2018-03-01 21:01:23 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef RT_USING_UART2
|
|
|
|
void UART2RX_Handler(void)
|
|
|
|
{
|
2018-03-02 16:13:02 +08:00
|
|
|
uart_irq_handler(&uart2_device.serial);
|
2018-03-01 21:01:23 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-03-02 16:13:02 +08:00
|
|
|
static rt_err_t v2m_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
|
2018-03-01 21:01:23 +08:00
|
|
|
{
|
2018-03-02 16:13:02 +08:00
|
|
|
struct v2m_uart *uart;
|
2018-03-01 21:01:23 +08:00
|
|
|
|
2018-03-02 16:13:02 +08:00
|
|
|
RT_ASSERT(serial != RT_NULL);
|
|
|
|
RT_ASSERT(cfg != RT_NULL);
|
2018-03-01 21:01:23 +08:00
|
|
|
|
2018-03-02 16:13:02 +08:00
|
|
|
uart = (struct v2m_uart *)serial->parent.user_data;
|
2018-03-01 21:01:23 +08:00
|
|
|
|
2018-03-02 16:13:02 +08:00
|
|
|
uart->uart_base->BAUDDIV = SystemCoreClock / cfg->baud_rate;
|
2018-03-01 21:01:23 +08:00
|
|
|
uart->uart_base->CTRL = CMSDK_UART_CTRL_TXEN_Msk | CMSDK_UART_CTRL_RXEN_Msk | CMSDK_UART_CTRL_RXIRQEN_Msk;
|
2018-03-02 16:13:02 +08:00
|
|
|
|
|
|
|
uart->rx_pingpio->ALTFUNCSET |= (1u << uart->rx_pinnum);
|
|
|
|
uart->tx_pingpio->ALTFUNCSET |= (1u << uart->tx_pinnum);
|
2018-03-01 21:01:23 +08:00
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
2018-03-02 16:13:02 +08:00
|
|
|
static rt_err_t v2m_control(struct rt_serial_device *serial, int cmd, void *arg)
|
2018-03-01 21:01:23 +08:00
|
|
|
{
|
2018-03-02 16:13:02 +08:00
|
|
|
struct v2m_uart *uart;
|
|
|
|
|
|
|
|
RT_ASSERT(serial != RT_NULL);
|
|
|
|
uart = (struct v2m_uart *)serial->parent.user_data;
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case RT_DEVICE_CTRL_CLR_INT:
|
|
|
|
/* disable rx irq */
|
|
|
|
NVIC_DisableIRQ(uart->uart_irq_rx);
|
|
|
|
break;
|
|
|
|
case RT_DEVICE_CTRL_SET_INT:
|
|
|
|
/* enable rx irq */
|
|
|
|
NVIC_EnableIRQ(uart->uart_irq_rx);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return RT_EOK;
|
2018-03-01 21:01:23 +08:00
|
|
|
}
|
|
|
|
|
2018-03-02 16:13:02 +08:00
|
|
|
static int v2m_putc(struct rt_serial_device *serial, char c)
|
2018-03-01 21:01:23 +08:00
|
|
|
{
|
2018-03-02 16:13:02 +08:00
|
|
|
struct v2m_uart *uart;
|
|
|
|
|
|
|
|
RT_ASSERT(serial != RT_NULL);
|
|
|
|
uart = (struct v2m_uart *)serial->parent.user_data;
|
|
|
|
|
|
|
|
while (uart->uart_base->STATE & CMSDK_UART_STATE_TXBF_Msk);
|
|
|
|
uart->uart_base->DATA = c;
|
|
|
|
|
|
|
|
return 1;
|
2018-03-01 21:01:23 +08:00
|
|
|
}
|
|
|
|
|
2018-03-02 16:13:02 +08:00
|
|
|
static int v2m_getc(struct rt_serial_device *serial)
|
2018-03-01 21:01:23 +08:00
|
|
|
{
|
2018-03-02 16:13:02 +08:00
|
|
|
int ch;
|
|
|
|
struct v2m_uart *uart;
|
2018-03-01 21:01:23 +08:00
|
|
|
|
2018-03-02 16:13:02 +08:00
|
|
|
RT_ASSERT(serial != RT_NULL);
|
|
|
|
uart = (struct v2m_uart *)serial->parent.user_data;
|
2018-03-01 21:01:23 +08:00
|
|
|
|
2018-03-02 16:13:02 +08:00
|
|
|
ch = -1;
|
|
|
|
if (uart->uart_base->STATE & CMSDK_UART_STATE_RXBF_Msk)
|
|
|
|
ch = uart->uart_base->DATA & 0xff;
|
|
|
|
return ch;
|
2018-03-01 21:01:23 +08:00
|
|
|
}
|
|
|
|
|
2018-03-02 16:13:02 +08:00
|
|
|
static const struct rt_uart_ops v2m_uart_ops =
|
2018-03-01 21:01:23 +08:00
|
|
|
{
|
2018-03-02 16:13:02 +08:00
|
|
|
v2m_configure,
|
|
|
|
v2m_control,
|
|
|
|
v2m_putc,
|
|
|
|
v2m_getc,
|
|
|
|
};
|
2018-03-01 21:01:23 +08:00
|
|
|
|
|
|
|
int rt_hw_usart_init(void)
|
|
|
|
{
|
2018-03-02 16:13:02 +08:00
|
|
|
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
|
2018-03-01 21:01:23 +08:00
|
|
|
#ifdef RT_USING_UART0
|
|
|
|
{
|
2018-03-02 16:13:02 +08:00
|
|
|
struct v2m_uart* uart;
|
2018-03-01 21:01:23 +08:00
|
|
|
|
|
|
|
/* get uart device */
|
|
|
|
uart = &uart0_device;
|
|
|
|
|
|
|
|
/* device initialization */
|
|
|
|
uart->uart_base = CMSDK_UART0;
|
|
|
|
uart->uart_irq_rx = UART0RX_IRQn;
|
2018-03-02 16:13:02 +08:00
|
|
|
|
|
|
|
uart->serial.ops = &v2m_uart_ops;
|
|
|
|
uart->serial.config = config;
|
|
|
|
|
|
|
|
rt_hw_serial_register(&uart->serial,
|
|
|
|
"uart0",
|
|
|
|
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
|
|
|
|
uart);
|
2018-03-01 21:01:23 +08:00
|
|
|
}
|
2018-03-02 16:13:02 +08:00
|
|
|
#endif /* RT_USING_UART0 */
|
2018-03-01 21:01:23 +08:00
|
|
|
|
|
|
|
#ifdef RT_USING_UART1
|
|
|
|
{
|
2018-03-02 16:13:02 +08:00
|
|
|
struct v2m_uart* uart;
|
2018-03-01 21:01:23 +08:00
|
|
|
|
|
|
|
/* get uart device */
|
|
|
|
uart = &uart1_device;
|
|
|
|
|
|
|
|
/* device initialization */
|
|
|
|
uart->uart_base = CMSDK_UART1;
|
|
|
|
uart->uart_irq_rx = UART1RX_IRQn;
|
2018-03-02 16:13:02 +08:00
|
|
|
|
|
|
|
rt_hw_serial_register(&uart->serial,
|
|
|
|
"uart1",
|
|
|
|
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
|
|
|
|
uart);
|
2018-03-01 21:01:23 +08:00
|
|
|
}
|
|
|
|
#endif /* RT_USING_UART1 */
|
|
|
|
|
|
|
|
#ifdef RT_USING_UART2
|
|
|
|
{
|
2018-03-02 16:13:02 +08:00
|
|
|
struct v2m_uart* uart;
|
2018-03-01 21:01:23 +08:00
|
|
|
|
|
|
|
/* get uart device */
|
|
|
|
uart = &uart2_device;
|
|
|
|
|
|
|
|
/* device initialization */
|
|
|
|
uart->uart_base = CMSDK_UART2;
|
|
|
|
uart->uart_irq_rx = UART2RX_IRQn;
|
2018-03-02 16:13:02 +08:00
|
|
|
|
|
|
|
rt_hw_serial_register(&uart->serial,
|
|
|
|
"uart2",
|
|
|
|
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
|
|
|
|
uart);
|
2018-03-01 21:01:23 +08:00
|
|
|
}
|
|
|
|
#endif /* RT_USING_UART2 */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
INIT_BOARD_EXPORT(rt_hw_usart_init);
|
|
|
|
|
|
|
|
#endif /*RT_USING_UART*/
|