1、【优化】STM32的串口底层驱动,提升可移植性。

Signed-off-by: armink <armink.ztl@gmail.com>
This commit is contained in:
armink 2015-01-31 16:47:18 +08:00
parent 66d377bd34
commit 7b4d3c52ab
7 changed files with 2200 additions and 2287 deletions

View File

@ -12,6 +12,7 @@
* 2009-01-05 Bernard the first version
* 2010-03-29 Bernard remove interrupt Tx and DMA Rx mode
* 2013-05-13 aozima update for kehong-lingtai.
* 2015-01-31 armink make sure the serial transmit complete in putc()
*/
#include "stm32f10x.h"
@ -44,8 +45,6 @@ struct stm32_uart
{
USART_TypeDef* uart_device;
IRQn_Type irq;
/* transmit interrupt type */
rt_uint32_t tx_irq_type;
};
static rt_err_t stm32_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
@ -93,7 +92,6 @@ static rt_err_t stm32_configure(struct rt_serial_device *serial, struct serial_c
static rt_err_t stm32_control(struct rt_serial_device *serial, int cmd, void *arg)
{
struct stm32_uart* uart;
rt_uint32_t irq_type = (rt_uint32_t)(arg);
RT_ASSERT(serial != RT_NULL);
uart = (struct stm32_uart *)serial->parent.user_data;
@ -102,75 +100,20 @@ static rt_err_t stm32_control(struct rt_serial_device *serial, int cmd, void *ar
{
/* disable interrupt */
case RT_DEVICE_CTRL_CLR_INT:
if (irq_type == RT_DEVICE_FLAG_INT_RX)
{
/* disable rx irq */
USART_ITConfig(uart->uart_device, USART_IT_RXNE, DISABLE);
}
else if (irq_type == RT_DEVICE_FLAG_INT_TX)
{
/* disable tx irq */
USART_ITConfig(uart->uart_device, uart->tx_irq_type, DISABLE);
}
else if (irq_type == RT_NULL)
{
/* disable irq */
UART_DISABLE_IRQ(uart->irq);
}
/* disable rx irq */
UART_DISABLE_IRQ(uart->irq);
/* disable interrupt */
USART_ITConfig(uart->uart_device, USART_IT_RXNE, DISABLE);
break;
/* enable interrupt */
case RT_DEVICE_CTRL_SET_INT:
if (irq_type == RT_DEVICE_FLAG_INT_RX)
{
/* enable rx irq */
USART_ITConfig(uart->uart_device, USART_IT_RXNE, ENABLE);
}
else if (irq_type == RT_DEVICE_FLAG_INT_TX)
{
/* enable tx irq */
USART_ITConfig(uart->uart_device, uart->tx_irq_type, ENABLE);
}
else if (irq_type == RT_NULL)
{
/* enable irq */
UART_ENABLE_IRQ(uart->irq);
}
break;
/* get interrupt flag */
case RT_DEVICE_CTRL_GET_INT:
if (irq_type == RT_DEVICE_FLAG_INT_RX)
{
/* return rx irq flag */
return USART_GetITStatus(uart->uart_device, USART_IT_RXNE);
}
else if (irq_type == RT_DEVICE_FLAG_INT_TX)
{
/* return tx irq flag */
return USART_GetITStatus(uart->uart_device, uart->tx_irq_type);
}
break;
/* get USART flag */
case RT_DEVICE_CTRL_GET_FLAG:
if (irq_type == RT_DEVICE_FLAG_INT_RX)
{
/* return rx irq flag */
return USART_GetFlagStatus(uart->uart_device, USART_FLAG_RXNE);
}
else if (irq_type == RT_DEVICE_FLAG_INT_TX)
{
/* return tx flag */
if (uart->tx_irq_type == USART_IT_TC)
{
return USART_GetFlagStatus(uart->uart_device, USART_FLAG_TC);
}
else if (uart->tx_irq_type == USART_IT_TXE)
{
return USART_GetFlagStatus(uart->uart_device, USART_FLAG_TXE);
}
}
/* enable rx irq */
UART_ENABLE_IRQ(uart->irq);
/* enable interrupt */
USART_ITConfig(uart->uart_device, USART_IT_RXNE, ENABLE);
break;
}
return RT_EOK;
}
@ -181,8 +124,8 @@ static int stm32_putc(struct rt_serial_device *serial, char c)
RT_ASSERT(serial != RT_NULL);
uart = (struct stm32_uart *)serial->parent.user_data;
while (!(uart->uart_device->SR & USART_FLAG_TXE));
uart->uart_device->DR = c;
while (!(uart->uart_device->SR & USART_FLAG_TC));
return 1;
}
@ -218,7 +161,6 @@ struct stm32_uart uart1 =
{
USART1,
USART1_IRQn,
USART_IT_TC,
};
struct rt_serial_device serial1;
@ -237,14 +179,10 @@ void USART1_IRQHandler(void)
USART_ClearITPendingBit(uart->uart_device, USART_IT_RXNE);
}
if (USART_GetITStatus(uart->uart_device, uart->tx_irq_type) != RESET)
if (USART_GetITStatus(uart->uart_device, USART_IT_TC) != RESET)
{
/* clear interrupt */
rt_hw_serial_isr(&serial1, RT_SERIAL_EVENT_TX_DONE);
if (uart->tx_irq_type == USART_IT_TC)
{
USART_ClearITPendingBit(uart->uart_device, uart->tx_irq_type);
}
USART_ClearITPendingBit(uart->uart_device, USART_IT_TC);
}
if (USART_GetFlagStatus(uart->uart_device, USART_FLAG_ORE) == SET)
{
@ -261,7 +199,6 @@ struct stm32_uart uart2 =
{
USART2,
USART2_IRQn,
USART_IT_TC,
};
struct rt_serial_device serial2;
@ -279,13 +216,10 @@ void USART2_IRQHandler(void)
/* clear interrupt */
USART_ClearITPendingBit(uart->uart_device, USART_IT_RXNE);
}
if (USART_GetITStatus(uart->uart_device, uart->tx_irq_type) != RESET)
if (USART_GetITStatus(uart->uart_device, USART_IT_TC) != RESET)
{
/* clear interrupt */
if (uart->tx_irq_type == USART_IT_TC)
{
USART_ClearITPendingBit(uart->uart_device, uart->tx_irq_type);
}
USART_ClearITPendingBit(uart->uart_device, USART_IT_TC);
}
if (USART_GetFlagStatus(uart->uart_device, USART_FLAG_ORE) == SET)
{
@ -303,7 +237,6 @@ struct stm32_uart uart3 =
{
USART3,
USART3_IRQn,
USART_IT_TC,
};
struct rt_serial_device serial3;
@ -321,13 +254,10 @@ void USART3_IRQHandler(void)
/* clear interrupt */
USART_ClearITPendingBit(uart->uart_device, USART_IT_RXNE);
}
if (USART_GetITStatus(uart->uart_device, uart->tx_irq_type) != RESET)
if (USART_GetITStatus(uart->uart_device, USART_IT_TC) != RESET)
{
/* clear interrupt */
if (uart->tx_irq_type == USART_IT_TC)
{
USART_ClearITPendingBit(uart->uart_device, uart->tx_irq_type);
}
USART_ClearITPendingBit(uart->uart_device, USART_IT_TC);
}
if (USART_GetFlagStatus(uart->uart_device, USART_FLAG_ORE) == SET)
{
@ -395,7 +325,7 @@ static void GPIO_Configuration(void)
GPIO_Init(UART1_GPIO_REMAP, &GPIO_InitStructure);
#endif /* RT_USING_UART1 */
#ifdef RT_USING_UART2
#if defined(RT_USING_UART2)
/* Configure USART Rx/tx PIN */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = UART2_GPIO_RX;
@ -406,7 +336,7 @@ static void GPIO_Configuration(void)
GPIO_Init(UART2_GPIO, &GPIO_InitStructure);
#endif /* RT_USING_UART2 */
#ifdef RT_USING_UART3
#if defined(RT_USING_UART3)
/* Configure USART Rx/tx PIN */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = UART3_GPIO_RX;
@ -453,7 +383,7 @@ void rt_hw_usart_init(void)
uart);
#endif /* RT_USING_UART1 */
#ifdef RT_USING_UART2
#if defined(RT_USING_UART2)
uart = &uart2;
config.baud_rate = BAUD_RATE_115200;
@ -468,7 +398,7 @@ void rt_hw_usart_init(void)
uart);
#endif /* RT_USING_UART2 */
#ifdef RT_USING_UART3
#if defined(RT_USING_UART3)
uart = &uart3;
config.baud_rate = BAUD_RATE_115200;

File diff suppressed because it is too large Load Diff

View File

@ -1,24 +1,24 @@
@REM This batch file has been generated by the IAR Embedded Workbench
@REM C-SPY Debugger, as an aid to preparing a command line for running
@REM the cspybat command line utility using the appropriate settings.
@REM
@REM Note that this file is generated every time a new debug session
@REM is initialized, so you may want to move or rename the file before
@REM making changes.
@REM
@REM You can launch cspybat by typing the name of this batch file followed
@REM by the name of the debug file (usually an ELF/DWARF or UBROF file).
@REM
@REM Read about available command line parameters in the C-SPY Debugging
@REM Guide. Hints about additional command line parameters that may be
@REM useful in specific cases:
@REM --download_only Downloads a code image without starting a debug
@REM session afterwards.
@REM --silent Omits the sign-on message.
@REM --timeout Limits the maximum allowed execution time.
@REM
"C:\Program Files\IAR Systems\Embedded Workbench 6.5\common\bin\cspybat" "C:\Program Files\IAR Systems\Embedded Workbench 6.5\arm\bin\armproc.dll" "C:\Program Files\IAR Systems\Embedded Workbench 6.5\arm\bin\armjlink.dll" %1 --plugin "C:\Program Files\IAR Systems\Embedded Workbench 6.5\arm\bin\armbat.dll" --macro "C:\Program Files\IAR Systems\Embedded Workbench 6.5\arm\config\debugger\ST\Trace_STM32F1xx.dmac" --backend -B "--endian=little" "--cpu=Cortex-M3" "--fpu=None" "-p" "C:\Program Files\IAR Systems\Embedded Workbench 6.5\arm\CONFIG\debugger\ST\STM32F103xB.ddf" "--semihosting" "--device=STM32F103xB" "--drv_communication=USB0" "--jlink_speed=auto" "--jlink_initial_speed=32" "--jlink_reset_strategy=0,0" "--drv_catch_exceptions=0x000" "--drv_swo_clock_setup=72000000,0,2000000"
@REM This batch file has been generated by the IAR Embedded Workbench
@REM C-SPY Debugger, as an aid to preparing a command line for running
@REM the cspybat command line utility using the appropriate settings.
@REM
@REM Note that this file is generated every time a new debug session
@REM is initialized, so you may want to move or rename the file before
@REM making changes.
@REM
@REM You can launch cspybat by typing the name of this batch file followed
@REM by the name of the debug file (usually an ELF/DWARF or UBROF file).
@REM
@REM Read about available command line parameters in the C-SPY Debugging
@REM Guide. Hints about additional command line parameters that may be
@REM useful in specific cases:
@REM --download_only Downloads a code image without starting a debug
@REM session afterwards.
@REM --silent Omits the sign-on message.
@REM --timeout Limits the maximum allowed execution time.
@REM
"C:\Program Files\IAR Systems\Embedded Workbench 6.5\common\bin\cspybat" "C:\Program Files\IAR Systems\Embedded Workbench 6.5\arm\bin\armproc.dll" "C:\Program Files\IAR Systems\Embedded Workbench 6.5\arm\bin\armjlink.dll" %1 --plugin "C:\Program Files\IAR Systems\Embedded Workbench 6.5\arm\bin\armbat.dll" --macro "C:\Program Files\IAR Systems\Embedded Workbench 6.5\arm\config\debugger\ST\Trace_STM32F1xx.dmac" --backend -B "--endian=little" "--cpu=Cortex-M3" "--fpu=None" "-p" "C:\Program Files\IAR Systems\Embedded Workbench 6.5\arm\CONFIG\debugger\ST\STM32F103xB.ddf" "--semihosting" "--device=STM32F103xB" "--drv_communication=USB0" "--jlink_speed=auto" "--jlink_initial_speed=32" "--jlink_reset_strategy=0,0" "--drv_catch_exceptions=0x000" "--drv_swo_clock_setup=72000000,0,2000000"

View File

@ -131,14 +131,6 @@ void vMBPortSerialEnable(BOOL xRxEnable, BOOL xTxEnable)
rt_uint32_t recved_event;
if (xRxEnable)
{
/* waiting for last transmit complete */
while (1)
{
if (serial->ops->control(serial, RT_DEVICE_CTRL_GET_FLAG, (void *)RT_DEVICE_FLAG_INT_TX))
{
break;
}
}
/* enable RX interrupt */
serial->ops->control(serial, RT_DEVICE_CTRL_SET_INT, (void *)RT_DEVICE_FLAG_INT_RX);
/* switch 485 to receive mode */

View File

@ -132,14 +132,6 @@ void vMBMasterPortSerialEnable(BOOL xRxEnable, BOOL xTxEnable)
rt_uint32_t recved_event;
if (xRxEnable)
{
/* waiting for last transmit complete */
while (1)
{
if (serial->ops->control(serial, RT_DEVICE_CTRL_GET_FLAG, (void *)RT_DEVICE_FLAG_INT_TX))
{
break;
}
}
/* enable RX interrupt */
serial->ops->control(serial, RT_DEVICE_CTRL_SET_INT, (void *)RT_DEVICE_FLAG_INT_RX);
/* switch 485 to receive mode */
@ -173,7 +165,7 @@ void vMBMasterPortClose(void)
BOOL xMBMasterPortSerialPutByte(CHAR ucByte)
{
serial->parent.write(&(serial->parent), 0, &ucByte, 1);
serial->parent.write(&(serial->parent), 0, &ucByte, 1);
return TRUE;
}

View File

@ -66,10 +66,9 @@
#endif
#define RT_DEVICE_CTRL_CONFIG 0x03 /* configure device */
#define RT_DEVICE_CTRL_SET_INT 0x10 /* enable serial irq */
#define RT_DEVICE_CTRL_CLR_INT 0x11 /* disable serial irq */
#define RT_DEVICE_CTRL_SET_INT 0x10 /* enable receive irq */
#define RT_DEVICE_CTRL_CLR_INT 0x11 /* disable receive irq */
#define RT_DEVICE_CTRL_GET_INT 0x12
#define RT_DEVICE_CTRL_GET_FLAG 0x13
#define RT_SERIAL_EVENT_RX_IND 0x01 /* Rx indication */
#define RT_SERIAL_EVENT_TX_DONE 0x02 /* Tx complete */

View File

@ -1,17 +1,17 @@
[FLASH]
SkipProgOnCRCMatch = 1
VerifyDownload = 1
AllowCaching = 1
EnableFlashDL = 2
Override = 0
Device="ADUC7020X62"
[BREAKPOINTS]
ShowInfoWin = 1
EnableFlashBP = 2
BPDuringExecution = 0
[CPU]
OverrideMemMap = 0
AllowSimulation = 1
ScriptFile=""
[SWO]
SWOLogFile=""
[FLASH]
SkipProgOnCRCMatch = 1
VerifyDownload = 1
AllowCaching = 1
EnableFlashDL = 2
Override = 0
Device="ADUC7020X62"
[BREAKPOINTS]
ShowInfoWin = 1
EnableFlashBP = 2
BPDuringExecution = 0
[CPU]
OverrideMemMap = 0
AllowSimulation = 1
ScriptFile=""
[SWO]
SWOLogFile=""