From 6fa8bfd92a3052d3715145fee9c3456ab3d11918 Mon Sep 17 00:00:00 2001 From: "Aubr.Cool" Date: Thu, 19 Jan 2017 09:55:39 +0800 Subject: [PATCH 1/2] prevent change serial rx bufsz when serial is opened --- components/drivers/serial/serial.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/components/drivers/serial/serial.c b/components/drivers/serial/serial.c index a7f1bcfc08..c55406bcd6 100644 --- a/components/drivers/serial/serial.c +++ b/components/drivers/serial/serial.c @@ -31,6 +31,7 @@ * in open function. * 2015-11-10 bernard fix the poll rx issue when there is no data. * 2016-05-10 armink add fifo mode to DMA rx when serial->config.bufsz != 0. + * 2017-01-19 aubr.cool prevent change serial rx bufsz when serial is opened. */ #include @@ -603,17 +604,22 @@ static rt_err_t rt_serial_control(struct rt_device *dev, break; case RT_DEVICE_CTRL_CONFIG: - if (args) - { - /* set serial configure */ - serial->config = *(struct serial_configure *)args; - - if (dev->ref_count) - { - /* serial device has been opened, to configure it */ - serial->ops->configure(serial, (struct serial_configure *)args); - } - } + if (args) + { + struct serial_configure *pconfig = (struct serial_configure *) args; + if(pconfig->bufsz != serial->config.bufsz && serial->parent.ref_count) + { + /*can not change buffer size*/ + return RT_EBUSY; + } + /* set serial configure */ + serial->config = *pconfig; + if(serial->parent.ref_count) + { + /* serial device has been opened, to configure it */ + serial->ops->configure(serial, (struct serial_configure *)args); + } + } break; From 4008a0a5fb099670dc0b13f3b90ec0d72b78c22a Mon Sep 17 00:00:00 2001 From: "Aubr.Cool" Date: Thu, 19 Jan 2017 09:56:36 +0800 Subject: [PATCH 2/2] stm32f10x usart add interrupt tx mode --- bsp/stm32f10x/drivers/usart.c | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/bsp/stm32f10x/drivers/usart.c b/bsp/stm32f10x/drivers/usart.c index 7aef75deda..4990395eac 100644 --- a/bsp/stm32f10x/drivers/usart.c +++ b/bsp/stm32f10x/drivers/usart.c @@ -14,6 +14,7 @@ * 2013-05-13 aozima update for kehong-lingtai. * 2015-01-31 armink make sure the serial transmit complete in putc() * 2016-05-13 armink add DMA Rx mode + * 2017-01-19 aubr.cool add interrupt Tx mode */ #include "stm32f10x.h" @@ -144,8 +145,21 @@ static int stm32_putc(struct rt_serial_device *serial, char c) RT_ASSERT(serial != RT_NULL); uart = (struct stm32_uart *)serial->parent.user_data; - uart->uart_device->DR = c; - while (!(uart->uart_device->SR & USART_FLAG_TC)); + if(serial->parent.open_flag & RT_DEVICE_FLAG_INT_TX) + { + if(!(uart->uart_device->SR & USART_FLAG_TXE)) + { + USART_ITConfig(uart->uart_device, USART_IT_TC, ENABLE); + return -1; + } + uart->uart_device->DR = c; + USART_ITConfig(uart->uart_device, USART_IT_TC, ENABLE); + } + else + { + uart->uart_device->DR = c; + while (!(uart->uart_device->SR & USART_FLAG_TC)); + } return 1; } @@ -244,6 +258,11 @@ static void uart_isr(struct rt_serial_device *serial) { if (USART_GetITStatus(uart->uart_device, USART_IT_TC) != RESET) { /* clear interrupt */ + if(serial->parent.open_flag & RT_DEVICE_FLAG_INT_TX) + { + rt_hw_serial_isr(serial, RT_SERIAL_EVENT_TX_DONE); + } + USART_ITConfig(uart->uart_device, USART_IT_RXNE, DISABLE); USART_ClearITPendingBit(uart->uart_device, USART_IT_TC); } if (USART_GetFlagStatus(uart->uart_device, USART_FLAG_ORE) == SET) @@ -561,7 +580,8 @@ void rt_hw_usart_init(void) /* register UART1 device */ rt_hw_serial_register(&serial1, "uart1", - RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_DMA_RX, + RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | + RT_DEVICE_FLAG_INT_TX | RT_DEVICE_FLAG_DMA_RX, uart); #endif /* RT_USING_UART1 */ @@ -576,7 +596,8 @@ void rt_hw_usart_init(void) /* register UART2 device */ rt_hw_serial_register(&serial2, "uart2", - RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_DMA_RX, + RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | + RT_DEVICE_FLAG_INT_TX | RT_DEVICE_FLAG_DMA_RX, uart); #endif /* RT_USING_UART2 */ @@ -592,7 +613,8 @@ void rt_hw_usart_init(void) /* register UART3 device */ rt_hw_serial_register(&serial3, "uart3", - RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_DMA_RX, + RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | + RT_DEVICE_FLAG_INT_TX | RT_DEVICE_FLAG_DMA_RX, uart); #endif /* RT_USING_UART3 */ @@ -608,7 +630,8 @@ void rt_hw_usart_init(void) /* register UART4 device */ rt_hw_serial_register(&serial4, "uart4", - RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_DMA_RX, + RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | + RT_DEVICE_FLAG_INT_TX | RT_DEVICE_FLAG_DMA_RX, uart); #endif /* RT_USING_UART4 */ }