RT-Thread學習筆記八
--------USART DMA方式發(fā)送
老規(guī)矩,首先說一下我的配置
STM32F207IGT6 MDK RTT 1.1.0
使用UART2作為finsh組件,UART3作為串口輸出
/* register uart2 */
rt_hw_serial_register(&uart2_device,"uart2",RT_DEVICE_FLAG_RDWR| RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,&uart2);
配置成中斷接收,數(shù)據(jù)流發(fā)送。
/* register uart3 */
rt_hw_serial_register(&uart3_device,"uart3",RT_DEVICE_FLAG_RDWR| RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_DMA_TX,&uart3);
配置成中斷接收,DMA發(fā)送
相信大家已經(jīng)能夠正常使用finsh組件了,所以小弟就不獻丑了,呵呵!
shaolin前輩寫的finsh的使用
連接1
rt-thread下的串口驅動程序分析
連接2
主要寫一下UART3 使用DMA發(fā)送
#ifdef RT_USING_UART3
struct stm32_serial_int_rx uart3_int_rx;
struct stm32_serial_dma_tx uart3_dma_tx;
struct stm32_serial_device uart3 =
{
USART3,
&uart3_int_rx,
&uart3_dma_tx
};
struct rt_device uart3_device;
#endif
我仍然按照usart.c中rt_hw_usart_init()的順序進行分析
/************************************ ******************************/
首先時鐘配置RCC_Configuration();
#ifdef RT_USING_UART3
/* Enable USART3 and GPIOC clocks */
RCC_AHB1PeriphClockCmd(UART3_APBPeriph_GPIOX, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APBPeriph_UART3, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_APBPeriph_UART3_DMA,ENABLE);
#endif
配置參數(shù)時都采用宏定義,如UART3_APBPeriph_GPIOX,RCC_APBPeriph_UART3,之所以不直接采用ST官方的參數(shù),是因為這樣不僅方便以后程序的移植,而且不容易出錯。
#define UART3_GPIO_RX GPIO_Pin_11
#define UART3_GPIO_TX GPIO_Pin_10
#define UART3_GPIO GPIOC
#define UART3_APBPeriph_GPIOX RCC_AHB1Periph_GPIOC
#define UART3_TX_PinSource GPIO_PinSource10
#define UART3_RX_PinSource GPIO_PinSource11
#define RCC_APBPeriph_UART3 RCC_APB1Periph_USART3
#define RCC_APBPeriph_UART3_DMA RCC_AHB1Periph_DMA1
#define UART3_TX_DMAy_Streamx DMA1_Stream3
#define UART3_TX_DMA_Channel DMA_Channel_4
#define UART3_TX_DMA_IRQHandler DMA1_Stream3_IRQn
#define UART3_TX_DMA_FLAG_TCIF DMA_FLAG_TCIF3
//#define UART3_RX_DMAy_Streamx DMA1_Stream1
//#define UART3_RX_DMA_Channel DMA_Channel_4
/************************************ ******************************/
然后進行管腳配置GPIO_Configuration();
#ifdef RT_USING_UART3
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType=GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_UP;
GPIO_InitStruct.GPIO_Pin=UART3_GPIO_TX;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(UART3_GPIO,&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF;
GPIO_InitStruct.GPIO_Pin=UART3_GPIO_RX;
GPIO_Init(UART3_GPIO,&GPIO_InitStruct);
GPIO_PinAFConfig(UART3_GPIO, UART3_TX_PinSource, GPIO_AF_USART3);
GPIO_PinAFConfig(UART3_GPIO, UART3_RX_PinSource, GPIO_AF_USART3);
#endif
/************************************ ******************************/
接著中斷配置NVIC_Configuration();
#ifdef RT_USING_UART3
/* Enable the USART3 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Enable the DMA1 Stream4 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = UART3_TX_DMA_IRQHandler;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
#endif
這里需要注意說明的是STM32F2系列和STM32F1系列的DMA配置有些不同,F(xiàn)1系列配置時要注意DMAy_Channelx,F2系列用了另外一個名字DMAy_Streamx
DMAy_Channelx,: where y can be 1 or 2 to select the DMA and x can be 1 to 7 for DMA1 and 1 to 5 for DMA2 to select the DMA Channel.
DMAy_Streamx,: where y can be 1 or 2 to select the DMA and x can be 0 to 7 to select the DMA Stream
關于STM32F207的DMA有篇博客寫的比較詳細,大家可以參考一下。
連接3
/************************************ ******************************/
然后配置DMADMA_Configuration();
DMA_InitTypeDef DMA_InitStructure;
DMA_InitStructure.DMA_Channel = UART3_TX_DMA_Channel;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; /* Specifies whether the Peripheral address register should be incremented or not */
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; /* Specifies whether the memory address register should be incremented or not */
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
DMA_DeInit(UART3_TX_DMAy_Streamx);
DMA_InitStructure.DMA_PeripheralBaseAddr = USART3_DR_Base;
DMA_InitStructure.DMA_Memory0BaseAddr = (u32)0;
DMA_InitStructure.DMA_BufferSize = 1;
DMA_Init(UART3_TX_DMAy_Streamx,&DMA_InitStructure);
// DMA_Cmd(UART3_TX_DMAy_Streamx, ENABLE);/* move to rt_serial_enable_dma() by RTT */
DMA_ITConfig(UART3_TX_DMAy_Streamx, DMA_IT_TC | DMA_IT_TE, ENABLE);
DMA_ClearFlag(UART3_TX_DMAy_Streamx, UART3_TX_DMA_FLAG_TCIF);
/************************************ ******************************/
最后進行UART的配置
#ifdef RT_USING_UART3
USART_DeInit(USART3);
USART_InitStructure.USART_BaudRate = 19200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No ;
USART_InitStructure.USART_HardwareFlowContro=USART_HardwareFlowControl_None;
USART_InitStructure.USART_M = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART3, &USART_InitStructure);
uart3_dma_tx.dma_channel= UART3_TX_DMAy_Streamx;
/* register uart3 */
rt_hw_serial_register(&uart3_device, "uart3",
RT_DEVICE_FLAG_RDWR|RT_DEVICE_FLAG_INT_RX|RT_DEVICE_FLAG_DMA_TX,&uart3);
/* Enable USART3 DMA Tx request */
USART_DMACmd(USART3, USART_DMAReq_Tx , ENABLE);
/* enable interrupt */
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
USART_ClearFlag(USART3,USART_FLAG_TXE);
#endif
uart3_dma_tx.dma_channel= UART3_TX_DMAy_Streamx;這條語句一定要當心,這里不是配置dma_channel,因為在seria.c中rt_serial_enable_dma()進行DMA的使能和失能
例如DMA_Cmd(dma_channel, ENABLE);
但是st的F2系列的庫函數(shù)中DMA配置的是Stream,而不是channel
DMA_Cmd(DMA_Stream_TypeDef* DMAy_Streamx, FunctionalState NewState)
到這里UART3的初始化就OK了,再進入中斷函數(shù)stm32f2xx_it.c配置
/*********************DMA中斷******************************/
#define UART3_TX_DMAy_Streamx DMA1_Stream3
#define UART3_TX_DMA_IT_TCIF DMA_IT_TCIF3
#define UART3_TX_DMA_IT_TEIF DMA_IT_TCIF3
#define UART3_TX_DMA_FLAG_TCIF DMA_FLAG_TCIF3
#define UART3_TX_DMA_FLAG_TEIF DMA_FLAG_TCIF3
void DMA1_Stream3_IRQHandler(void)
{
#ifdef RT_USING_UART3
extern struct rt_device uart3_device;
extern void rt_hw_serial_dma_tx_isr(struct rt_device *device);
/* enter interrupt */
rt_interrupt_enter();
if(DMA_GetITStatus(UART3_TX_DMAy_Streamx,UART3_TX_DMA_IT_TCIF))
{
/* transmission complete, invoke serial dma tx isr */
rt_hw_serial_dma_tx_isr(&uart3_device);
}
/* clear DMA flag */
DMA_ClearFlag(UART3_TX_DMAy_Streamx,DMA_FLAG_TCIF3 | UART3_TX_DMA_FLAG_TEIF);
/* leave interrupt */
rt_interrupt_leave();
#endif
}
在中斷函數(shù)中,大家一定要注意DMA_GetITStatus() 和 DMA_GetFLAGStatus(),不能混淆了,我就吃過虧。一旦弄錯了,DMA中斷程序不正常,就導致了DMA只能發(fā)送一次。
在APPTask.c中創(chuàng)建了一個任務,進行發(fā)送。
void usart_tx_thread_entry(void *p)
{
char tx_buf[]="hello pc!rn";
u8 datalen;
while(1)
{
datalen = strlen(tx_buf);
dev_uart3->write(dev_uart3,0,tx_buf,datalen);
rt_thread_delay(RT_TICK_PER_SECOND*2);
}
}
串口輸出
因為我剛接觸RTT系統(tǒng),所以會有很多考慮不周全的方面,請大家指出,呵呵!2013.1.6
因為我不能發(fā)連接,所以文中提到連接地方到放到了附件pdf中