[bsp] update uart driver and fixed typo

This commit is contained in:
liang yongxiang 2018-03-02 16:13:02 +08:00
parent b9c2acba87
commit dc4fde203d
3 changed files with 90 additions and 226 deletions

View File

@ -24,7 +24,7 @@ V2M-MPS2板级包支持MDK5已测试MDK5.23~MDK5.25
- [Windows 7: Enabling Telnet Client](https://social.technet.microsoft.com/wiki/contents/articles/910.windows-7-enabling-telnet-client.aspx) - [Windows 7: Enabling Telnet Client](https://social.technet.microsoft.com/wiki/contents/articles/910.windows-7-enabling-telnet-client.aspx)
- [Windows 10: Enabling Telnet Client](https://social.technet.microsoft.com/wiki/contents/articles/38433.windows-10-enabling-telnet-client.aspx) - [Windows 10: Enabling Telnet Client](https://social.technet.microsoft.com/wiki/contents/articles/38433.windows-10-enabling-telnet-client.aspx)
打开project.uvprojx编译点击Debug->Start/Stop Seccion就可以进入仿真了。 打开project.uvprojx编译点击Debug->Start/Stop Session就可以进入仿真了。
### 3.2 如何选择其他内核 ### 3.2 如何选择其他内核
@ -54,7 +54,7 @@ finsh />
| 驱动 | 支持情况 | 备注 | | 驱动 | 支持情况 | 备注 |
| ------ | ---- | :------: | | ------ | ---- | :------: |
| UART | 支持 | UART0/1/2/3 | | UART | 支持 | UART0/1/2 |
| GPIO | 未支持 | | | GPIO | 未支持 | |
| LED | 未支持 | | | LED | 未支持 | |
| BUTTOM | 未支持 | | | BUTTOM | 未支持 | |

View File

@ -31,16 +31,14 @@
#ifndef RT_USING_DEVICE #ifndef RT_USING_DEVICE
#error "you must define RT_USING_DEVICE with uart device" #error "you must define RT_USING_DEVICE with uart device"
#endif #else
#include <rtdevice.h>
#ifndef RT_UART_RX_BUFFER_SIZE
#define RT_UART_RX_BUFFER_SIZE 16
#endif #endif
/* uart driver */ /* uart driver */
struct fvp_uart struct v2m_uart
{ {
struct rt_device parent; struct rt_serial_device serial;
CMSDK_UART_TypeDef * uart_base; CMSDK_UART_TypeDef * uart_base;
CMSDK_GPIO_TypeDef * rx_pingpio; // Pin GPIO CMSDK_GPIO_TypeDef * rx_pingpio; // Pin GPIO
@ -49,62 +47,34 @@ struct fvp_uart
uint8_t tx_pinnum; uint8_t tx_pinnum;
IRQn_Type uart_irq_rx; IRQn_Type uart_irq_rx;
IRQn_Type uart_irq_tx; //IRQn_Type uart_irq_tx;
/* buffer for reception */
rt_uint8_t read_index, save_index;
rt_uint8_t rx_buffer[RT_UART_RX_BUFFER_SIZE];
}; };
#ifdef RT_USING_UART0 #ifdef RT_USING_UART0
struct fvp_uart uart0_device; struct v2m_uart uart0_device;
#endif #endif
#ifdef RT_USING_UART1 #ifdef RT_USING_UART1
struct fvp_uart uart1_device; struct v2m_uart uart1_device;
#endif #endif
#ifdef RT_USING_UART2 #ifdef RT_USING_UART2
struct fvp_uart uart2_device; struct v2m_uart uart2_device;
#endif #endif
#ifdef RT_USING_UART3 static void uart_irq_handler(struct rt_serial_device *serial)
struct fvp_uart uart3_device;
#endif
static void uart_irq_handler(struct fvp_uart* uart)
{ {
rt_ubase_t level;
uint32_t status; uint32_t status;
uint8_t data; struct v2m_uart *uart;
status = uart->uart_base->INTSTATUS; uart = (struct v2m_uart *)serial->parent.user_data;
data = uart->uart_base->DATA;
/* enter interrupt */ /* enter interrupt */
rt_interrupt_enter(); rt_interrupt_enter();
level = rt_hw_interrupt_disable(); status = uart->uart_base->INTSTATUS;
uart->rx_buffer[uart->save_index] = data; rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
uart->save_index ++;
if (uart->save_index >= RT_UART_RX_BUFFER_SIZE)
{
uart->save_index = 0;
}
rt_hw_interrupt_enable(level);
/* invoke callback */
if (uart->parent.rx_indicate != RT_NULL)
{
rt_size_t length;
if (uart->read_index > uart->save_index)
length = RT_UART_RX_BUFFER_SIZE - uart->read_index + uart->save_index;
else
length = uart->save_index - uart->read_index;
uart->parent.rx_indicate(&uart->parent, length);
}
uart->uart_base->INTCLEAR = status; uart->uart_base->INTCLEAR = status;
/* leave interrupt */ /* leave interrupt */
@ -114,223 +84,144 @@ static void uart_irq_handler(struct fvp_uart* uart)
#ifdef RT_USING_UART0 #ifdef RT_USING_UART0
void UART0RX_Handler(void) void UART0RX_Handler(void)
{ {
uart_irq_handler(&uart0_device); uart_irq_handler(&uart0_device.serial);
} }
#endif #endif
#ifdef RT_USING_UART1 #ifdef RT_USING_UART1
void UART1RX_Handler(void) void UART1RX_Handler(void)
{ {
uart_irq_handler(&uart1_device); uart_irq_handler(&uart1_device.serial);
} }
#endif #endif
#ifdef RT_USING_UART2 #ifdef RT_USING_UART2
void UART2RX_Handler(void) void UART2RX_Handler(void)
{ {
uart_irq_handler(&uart2_device); uart_irq_handler(&uart2_device.serial);
} }
#endif #endif
#ifdef RT_USING_UART3 static rt_err_t v2m_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
void UART3RX_Handler(void)
{ {
uart_irq_handler(&uart3_device); struct v2m_uart *uart;
}
#endif
static rt_err_t rt_uart_init (rt_device_t dev) RT_ASSERT(serial != RT_NULL);
{ RT_ASSERT(cfg != RT_NULL);
struct fvp_uart* uart;
RT_ASSERT(dev != RT_NULL); uart = (struct v2m_uart *)serial->parent.user_data;
uart = (struct fvp_uart *)dev;
uart->uart_base->BAUDDIV = SystemCoreClock / cfg->baud_rate;
uart->uart_base->CTRL = CMSDK_UART_CTRL_TXEN_Msk | CMSDK_UART_CTRL_RXEN_Msk | CMSDK_UART_CTRL_RXIRQEN_Msk;
uart->rx_pingpio->ALTFUNCSET |= (1u << uart->rx_pinnum); uart->rx_pingpio->ALTFUNCSET |= (1u << uart->rx_pinnum);
uart->tx_pingpio->ALTFUNCSET |= (1u << uart->tx_pinnum); uart->tx_pingpio->ALTFUNCSET |= (1u << uart->tx_pinnum);
uart->uart_base->CTRL = CMSDK_UART_CTRL_TXEN_Msk | CMSDK_UART_CTRL_RXEN_Msk | CMSDK_UART_CTRL_RXIRQEN_Msk;
uart->uart_base->BAUDDIV = SystemCoreClock / 115200;
return RT_EOK; return RT_EOK;
} }
static rt_err_t rt_uart_open(rt_device_t dev, rt_uint16_t oflag) static rt_err_t v2m_control(struct rt_serial_device *serial, int cmd, void *arg)
{ {
struct fvp_uart* uart; struct v2m_uart *uart;
RT_ASSERT(dev != RT_NULL);
uart = (struct fvp_uart *)dev;
if (dev->flag & RT_DEVICE_FLAG_INT_RX) RT_ASSERT(serial != RT_NULL);
{ uart = (struct v2m_uart *)serial->parent.user_data;
/* Enable the UART Interrupt */
NVIC_EnableIRQ(uart->uart_irq_rx);
}
return RT_EOK; 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;
} }
static rt_err_t rt_uart_close(rt_device_t dev) static int v2m_putc(struct rt_serial_device *serial, char c)
{ {
struct fvp_uart* uart; struct v2m_uart *uart;
RT_ASSERT(dev != RT_NULL);
uart = (struct fvp_uart *)dev;
if (dev->flag & RT_DEVICE_FLAG_INT_RX) RT_ASSERT(serial != RT_NULL);
{ uart = (struct v2m_uart *)serial->parent.user_data;
/* Disable the UART Interrupt */
NVIC_DisableIRQ(uart->uart_irq_rx);
}
return RT_EOK; while (uart->uart_base->STATE & CMSDK_UART_STATE_TXBF_Msk);
uart->uart_base->DATA = c;
return 1;
} }
static rt_size_t rt_uart_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size) static int v2m_getc(struct rt_serial_device *serial)
{ {
struct fvp_uart* uart = (struct fvp_uart *)dev; int ch;
rt_uint8_t *ptr; struct v2m_uart *uart;
rt_size_t length;
RT_ASSERT(dev != RT_NULL); RT_ASSERT(serial != RT_NULL);
RT_ASSERT(buffer != RT_NULL); uart = (struct v2m_uart *)serial->parent.user_data;
ptr = (rt_uint8_t *) buffer; ch = -1;
while (size) if (uart->uart_base->STATE & CMSDK_UART_STATE_RXBF_Msk)
{ ch = uart->uart_base->DATA & 0xff;
/* interrupt receive */ return ch;
rt_base_t level;
/* disable interrupt */
level = rt_hw_interrupt_disable();
if (uart->read_index != uart->save_index)
{
*ptr = uart->rx_buffer[uart->read_index];
uart->read_index ++;
if (uart->read_index >= RT_UART_RX_BUFFER_SIZE)
uart->read_index = 0;
}
else
{
/* no data in rx buffer */
/* enable interrupt */
rt_hw_interrupt_enable(level);
break;
}
/* enable interrupt */
rt_hw_interrupt_enable(level);
ptr ++;
size --;
}
length = (rt_uint32_t)ptr - (rt_uint32_t)buffer;
return length;
} }
static rt_size_t rt_uart_write(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size) static const struct rt_uart_ops v2m_uart_ops =
{ {
char *ptr = (char*) buffer; v2m_configure,
struct fvp_uart* uart = (struct fvp_uart *)dev; v2m_control,
v2m_putc,
RT_ASSERT(dev != RT_NULL); v2m_getc,
RT_ASSERT(buffer != RT_NULL); };
if (dev->open_flag & RT_DEVICE_FLAG_STREAM)
{
/* stream mode */
while (size)
{
if (*ptr == '\n')
{
while (uart->uart_base->STATE & CMSDK_UART_STATE_TXBF_Msk);
uart->uart_base->DATA = '\r';
}
while (uart->uart_base->STATE & CMSDK_UART_STATE_TXBF_Msk);
uart->uart_base->DATA = *ptr;
ptr++;
size--;
}
}
else
{
while (size)
{
while (uart->uart_base->STATE & CMSDK_UART_STATE_TXBF_Msk);
uart->uart_base->DATA = *ptr;
ptr++;
size--;
}
}
return (rt_size_t)ptr - (rt_size_t)buffer;
}
int rt_hw_usart_init(void) int rt_hw_usart_init(void)
{ {
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
#ifdef RT_USING_UART0 #ifdef RT_USING_UART0
{ {
struct fvp_uart* uart; struct v2m_uart* uart;
/* get uart device */ /* get uart device */
uart = &uart0_device; uart = &uart0_device;
/* device initialization */ /* device initialization */
uart->parent.type = RT_Device_Class_Char;
uart->uart_base = CMSDK_UART0; uart->uart_base = CMSDK_UART0;
uart->uart_irq_rx = UART0RX_IRQn; uart->uart_irq_rx = UART0RX_IRQn;
uart->read_index = 0;
uart->save_index = 0;
rt_memset(uart->rx_buffer, 0, sizeof(uart->rx_buffer));
/* device interface */ uart->serial.ops = &v2m_uart_ops;
uart->parent.init = rt_uart_init; uart->serial.config = config;
uart->parent.open = rt_uart_open;
uart->parent.close = rt_uart_close;
uart->parent.read = rt_uart_read;
uart->parent.write = rt_uart_write;
uart->parent.control = RT_NULL;
uart->parent.user_data = RT_NULL;
rt_device_register(&uart->parent, "uart0", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX); rt_hw_serial_register(&uart->serial,
"uart0",
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
uart);
} }
#endif /* RT_USING_UART1 */ #endif /* RT_USING_UART0 */
#ifdef RT_USING_UART1 #ifdef RT_USING_UART1
{ {
struct fvp_uart* uart; struct v2m_uart* uart;
/* get uart device */ /* get uart device */
uart = &uart1_device; uart = &uart1_device;
/* device initialization */ /* device initialization */
uart->parent.type = RT_Device_Class_Char;
uart->uart_base = CMSDK_UART1; uart->uart_base = CMSDK_UART1;
uart->uart_irq_rx = UART1RX_IRQn; uart->uart_irq_rx = UART1RX_IRQn;
uart->read_index = 0;
uart->save_index = 0;
rt_memset(uart->rx_buffer, 0, sizeof(uart->rx_buffer));
/* device interface */ rt_hw_serial_register(&uart->serial,
uart->parent.init = rt_uart_init; "uart1",
uart->parent.open = rt_uart_open; RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
uart->parent.close = rt_uart_close; uart);
uart->parent.read = rt_uart_read;
uart->parent.write = rt_uart_write;
uart->parent.control = RT_NULL;
uart->parent.user_data = RT_NULL;
rt_device_register(&uart->parent, "uart1", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX);
} }
#endif /* RT_USING_UART1 */ #endif /* RT_USING_UART1 */
#ifdef RT_USING_UART2 #ifdef RT_USING_UART2
{ {
struct fvp_uart* uart; struct v2m_uart* uart;
/* get uart device */ /* get uart device */
uart = &uart2_device; uart = &uart2_device;
@ -338,21 +229,11 @@ int rt_hw_usart_init(void)
/* device initialization */ /* device initialization */
uart->uart_base = CMSDK_UART2; uart->uart_base = CMSDK_UART2;
uart->uart_irq_rx = UART2RX_IRQn; uart->uart_irq_rx = UART2RX_IRQn;
uart->read_index = 0;
uart->save_index = 0;
rt_memset(uart->rx_buffer, 0, sizeof(uart->rx_buffer));
/* device interface */ rt_hw_serial_register(&uart->serial,
uart->parent.type = RT_Device_Class_Char; "uart2",
uart->parent.init = rt_uart_init; RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
uart->parent.open = rt_uart_open; uart);
uart->parent.close = rt_uart_close;
uart->parent.read = rt_uart_read;
uart->parent.write = rt_uart_write;
uart->parent.control = RT_NULL;
uart->parent.user_data = RT_NULL;
rt_device_register(&uart->parent, "uart2", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX);
} }
#endif /* RT_USING_UART2 */ #endif /* RT_USING_UART2 */
return 0; return 0;

View File

@ -148,24 +148,7 @@
<Name>UL2CM3(-S0 -C0 -P0 -FD20000000 -FC1000)</Name> <Name>UL2CM3(-S0 -C0 -P0 -FD20000000 -FC1000)</Name>
</SetRegEntry> </SetRegEntry>
</TargetDriverDllRegistry> </TargetDriverDllRegistry>
<Breakpoint> <Breakpoint/>
<Bp>
<Number>0</Number>
<Type>0</Type>
<LineNumber>150</LineNumber>
<EnabledFlag>1</EnabledFlag>
<Address>2206</Address>
<ByteObject>0</ByteObject>
<HtxType>0</HtxType>
<ManyObjects>0</ManyObjects>
<SizeOfObject>0</SizeOfObject>
<BreakByAccess>0</BreakByAccess>
<BreakIfRCount>1</BreakIfRCount>
<Filename>..\..\src\components.c</Filename>
<ExecCommand></ExecCommand>
<Expression>\\rtthread_v2m_mps2\../../src/components.c\150</Expression>
</Bp>
</Breakpoint>
<Tracepoint> <Tracepoint>
<THDelay>0</THDelay> <THDelay>0</THDelay>
</Tracepoint> </Tracepoint>