USART3_DR的地址
DMA的通道
DMA的中斷
USART接收回調(diào)函數(shù)
頭文件源碼
DMA的基本配置
環(huán)形隊列接收數(shù)據(jù)
函數(shù)原型
參考用例
總結(jié)
硬件:stm32f103cbt6
軟件:STM32F10x_StdPeriph_Lib_V3.5.0
DMA
,直接內(nèi)存存取,可以用它的雙手釋放CPU
的靈魂,所以,本文通過USART3
進(jìn)行串口收發(fā),接受使用DMA的方式,無需CPU
進(jìn)行干預(yù),當(dāng)接受完成之后,數(shù)據(jù)可以直接從內(nèi)存的緩沖區(qū)讀取,從而減少了CPU
的壓力。
具體的代碼實現(xiàn)如下:
usart_driver.h
封裝了接口,數(shù)據(jù)接收回調(diào)函數(shù)類型,基本數(shù)據(jù)結(jié)構(gòu)等;usart_driver.c
函數(shù)原型實現(xiàn),中斷服務(wù)函數(shù)實現(xiàn)等;拷貝這兩個文件即可,可以根據(jù)目錄下的參考用例,進(jìn)行初始化。
頭文件usart_driver.h
已經(jīng)聲明了外部函數(shù)可能用到的接口;
因為USART3
接收到數(shù)據(jù)會存在DR
寄存器中,而DMA
控制器則負(fù)責(zé)將該寄存器中的內(nèi)容一一搬運(yùn)到內(nèi)存的緩沖區(qū)中(比如你定義的某個數(shù)組中),所以這里需要告訴DMA
控制去哪里搬運(yùn),因此需要設(shè)置USART3_DR
的總線地址。
USART3
的基址如下圖所示;
DR
寄存器的偏移地址如下圖所示;
所以最終地址為:0x40004800 + 0x004#define USART_DR_Base 0x40004804
因為有很多外設(shè)都可以使用DMA
,比如ADC
,I2C
,SPI
等等,所以,不同的外設(shè)就要選擇屬于自己的DMA
通道,查找參考手冊;
因此USART3_RX
在這里會使用DMA1
的通道3,這都是硬件上已經(jīng)預(yù)先分配好的,我們需要遵循這個規(guī)則。所以在代碼中我們做出相應(yīng)的定義;如下所示;
#define USART_Rx_DMA_Channel DMA1_Channel3
DMA
支持三種中斷:傳輸過半,傳輸完成,傳輸出錯;
因此在使用是相當(dāng)安全也相當(dāng)靈活,而本文只是用了傳輸完成中斷;如下定義了,傳輸完成中斷的標(biāo)志位,DMA1_FLAG_TC3
也就對應(yīng)了圖中的TCIF
;
#define USART_Rx_DMA_FLAG DMA1_FLAG_TC3
在STM32
的HAL
中封裝了大量外設(shè)的回調(diào)函數(shù),使用起來十分方便,但是標(biāo)準(zhǔn)庫中則沒有這樣的做法,但是這里我們可以自己實現(xiàn),rx_cbk
就是回調(diào),即串口數(shù)據(jù)接收完成就會執(zhí)行已經(jīng)注冊的回調(diào)函數(shù);
typedef void (*rx_cbk)(void* args);
通過使用接口usart_set_rx_cbk
進(jìn)行回調(diào)函數(shù)的注冊,pargs
為將傳遞的參數(shù)指針;
void usart_set_rx_cbk(uart_mod_t *pmod, rx_cbk pfunc,void *pargs);
#ifndef USART_DRIVER_H
#define USART_DRIVER_H
#include <stdio.h>
#include <stdint.h>
/* Private function prototypes -----------------------------------------------*/
#define USE_MICROLIB_USART 1
#if USE_MICROLIB_USART
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
//#define GETCHAR_PROTOTYPE int fgetc(FILE *f)
#endif /* __GNUC__ */
extern PUTCHAR_PROTOTYPE;
#else
#endif
//default 8N1
#define COM_PORT USART3
#define TX_PIN GPIO_Pin_10
#define RX_PIN GPIO_Pin_11
#define BAUDRATE 115200
#define IRQ_UART_PRE 3
#define IRQ_UART_SUB 3
#define USART_Rx_DMA_Channel DMA1_Channel3
#define USART_Rx_DMA_FLAG DMA1_FLAG_TC3
#define USART_DR_Base 0x40004804
#define USART_BUF_SIZE ((uint16_t)16)
typedef void (*rx_cbk)(void* args);
struct uart_mod {
uint8_t rx_buf[USART_BUF_SIZE];
uint8_t rx_dat_len;
uint8_t head;
uint8_t tail;
void (*init)(void);
void *pargs;
rx_cbk pfunc_rx_cbk;
};
typedef struct uart_mod uart_mod_t;
extern uart_mod_t user_uart_mod;
void usart_init(void);
void usart_set_rx_cbk(uart_mod_t *pmod, rx_cbk pfunc,void *pargs);
void usart_send_char(char ch);
void usart_test_echo(void);
uint8_t usart_recv_char(void);
int usart_printf(const char *fmt, ...);
//extern GETCHAR_PROTOTYPE;
#endif
串口接收DMA
的配置在函數(shù)dma_init
中;
static void dma_init(void)
已經(jīng)定義了數(shù)據(jù)緩沖區(qū),如下:
uint8_t RxBuffer[USART_BUF_SIZE] = { 0 };
因此需要在DMA
的配置中設(shè)置USART_DR
的地址,和數(shù)據(jù)緩沖區(qū)的地址,以及兩者的大??;還有就是數(shù)據(jù)流向;
DMA_InitStructure.DMA_PeripheralBaseAddr = USART_DR_Base;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)RxBuffer;
DMA_InitStructure.DMA_BufferSize = USART_BUF_SIZE;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
注意:DMA_DIR_PeripheralSRC
表示,外設(shè)作為源地址,數(shù)據(jù)是從外設(shè)寄存器流向內(nèi)存,即DMA會把數(shù)據(jù)從地址USART_DR_Base
搬運(yùn)到RxBuffer
去。如果這個地方搞錯,會導(dǎo)致RxBuffer
始終沒有你想要的數(shù)據(jù)。
線性緩沖區(qū)會因為緩沖器接收數(shù)據(jù)已滿導(dǎo)致無法繼續(xù)接收的問題;而環(huán)形隊列進(jìn)行接收的話,會自動進(jìn)行覆蓋,這樣一來,在讀取數(shù)據(jù)的時候,也要配置一個環(huán)形隊列進(jìn)行數(shù)據(jù)處理,下面的配置是把DMA
配置為循環(huán)模式;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
在結(jié)構(gòu)體user_uart_mod
中,則用兩個變量分別指向隊首head
和隊尾tail
;具體數(shù)據(jù)的讀取在函數(shù)USART3_IRQHandler
中,會把數(shù)據(jù)從內(nèi)存的RxBuffer
讀取到結(jié)構(gòu)體user_uart_mod
的成員變量rx_buf
中;最終調(diào)用回調(diào)函數(shù)。
usart_driver.c
#include <stdio.h>
#include <stdarg.h>
#include 'stm32f10x_usart.h'
#include 'usart_driver.h'
uint8_t RxBuffer[USART_BUF_SIZE] = { 0 };
uart_mod_t user_uart_mod = {
.rx_dat_len = 0,
.head = 0,
.tail = 0,
.pfunc_rx_cbk = NULL,
.pargs = NULL
};
static USART_InitTypeDef USART_InitStructure;
static void rcc_init(void){
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
/* Enable GPIO clock */
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB
| RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART3, ENABLE);
}
static void gpio_init(void){
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure USART Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = TX_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Configure USART Rx as input floating */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = RX_PIN;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
static void dma_init(void){
DMA_InitTypeDef DMA_InitStructure;
/* USARTy_Tx_DMA_Channel (triggered by USARTy Tx event) Config */
DMA_DeInit(USART_Rx_DMA_Channel);
DMA_InitStructure.DMA_PeripheralBaseAddr = USART_DR_Base;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)RxBuffer;
//DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = USART_BUF_SIZE;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(USART_Rx_DMA_Channel, &DMA_InitStructure);
}
static void irq_init(void){
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable the USART3_IRQn Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = IRQ_UART_PRE;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = IRQ_UART_SUB;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void usart_send_char(char ch){
/* Loop until the end of transmission */
//while (USART_GetFlagStatus(COM_PORT, USART_FLAG_TC) == RESET){}
while((COM_PORT->SR & USART_FLAG_TC) != USART_FLAG_TC){
}
USART_SendData(COM_PORT, (uint8_t) ch);
}
uint8_t usart_recv_char(){
/* Wait the byte is entirely received by USARTy */
//while(USART_GetFlagStatus(COM_PORT, USART_FLAG_RXNE) == RESET){}
while((COM_PORT->SR & USART_FLAG_RXNE) != USART_FLAG_RXNE){
}
/* Store the received byte in the RxBuffer1 */
return (uint8_t)USART_ReceiveData(COM_PORT);
}
int usart_printf(const char *fmt, ... )
{
uint8_t i = 0;
uint8_t usart_tx_buf[128] = { 0 };
va_list ap;
va_start(ap, fmt );
vsprintf((char*)usart_tx_buf, fmt, ap);
va_end(ap);
while(usart_tx_buf[i] && i < 128){
usart_send_char(usart_tx_buf[i]);
i++;
}
usart_send_char('\0');
return 0;
}
void usart_test_echo(){
uint8_t tmp_dat = 0xff;
tmp_dat = usart_recv_char();
usart_send_char(tmp_dat);
}
void usart_init(void){
rcc_init ();
gpio_init ();
irq_init();
/* USARTx configured as follow:
- BaudRate = 115200 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
USART_InitStructure.USART_BaudRate = BAUDRATE;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
/* USART configuration */
USART_Init(COM_PORT, &USART_InitStructure);
USART_ITConfig(COM_PORT, USART_IT_IDLE, ENABLE);
//USART_ITConfig(COM_PORT, USART_IT_RXNE, ENABLE);
/* Enable USART */
USART_Cmd(COM_PORT, ENABLE);
USART_DMACmd(COM_PORT,USART_DMAReq_Rx, ENABLE);
dma_init();
DMA_ITConfig(USART_Rx_DMA_Channel, DMA_IT_TC, ENABLE);
DMA_ITConfig(USART_Rx_DMA_Channel, DMA_IT_TE, ENABLE);
DMA_Cmd(USART_Rx_DMA_Channel, ENABLE);
}
void usart_set_rx_cbk(uart_mod_t *pmod, rx_cbk pfunc,void *pargs){
pmod->pargs = pargs;
pmod->pfunc_rx_cbk = pfunc;
}
void DMA1_Channel3_IRQHandler(void){
if(DMA_GetITStatus(USART_Rx_DMA_FLAG) == SET){
DMA_ClearITPendingBit(USART_Rx_DMA_FLAG);
}
}
/**
* @brief This function handles USART3 global interrupt request.
* @param None
* @retval None
*/
void USART3_IRQHandler(void)
{
uint8_t buf[USART_BUF_SIZE];
uint16_t rect_len = 0;
if(USART_GetITStatus(COM_PORT, USART_IT_IDLE) != RESET)
{
uint8_t i = 0;
USART_ReceiveData(COM_PORT);
user_uart_mod.head = USART_BUF_SIZE - DMA_GetCurrDataCounter(USART_Rx_DMA_Channel);
//fifo is not full
while(user_uart_mod.head%USART_BUF_SIZE != user_uart_mod.tail%USART_BUF_SIZE){
user_uart_mod.rx_buf[i++] = RxBuffer[user_uart_mod.tail++%USART_BUF_SIZE];
}
user_uart_mod.rx_dat_len = i;
//DMA_Cmd(USART_Rx_DMA_Channel, ENABLE);
if(user_uart_mod.pfunc_rx_cbk != NULL){
user_uart_mod.pfunc_rx_cbk(user_uart_mod.pargs);
}
}
USART_ClearITPendingBit(COM_PORT, USART_IT_IDLE);
//USART_ClearITPendingBit(COM_PORT, USART_IT_RXNE);
}
#if USE_MICROLIB_USART
/**
* @brief Retargets the C library printf function to the USART.
* @param None
* @retval None
*/
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
USART_SendData(COM_PORT, (uint8_t) ch);
/* Loop until the end of transmission */
while (USART_GetFlagStatus(COM_PORT, USART_FLAG_TC) == RESET)
{}
return ch;
}
#else
#pragma import(__use_no_semihosting)
struct __FILE
{
int handle;
};
FILE __stdout;
int _sys_exit(int x)
{
x = x;
return 0;
}
int fputc(int ch, FILE *f)
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
USART_SendData(COM_PORT, (uint8_t) ch);
/* Loop until the end of transmission */
while (USART_GetFlagStatus(COM_PORT, USART_FLAG_TC) == RESET)
{}
return ch;
}
#endif
這里需要調(diào)用usart_init
,并設(shè)置回調(diào)函數(shù),如果不設(shè)置,則不會執(zhí)行回調(diào)。
void motor_get_cmd_from_uart(void *pargs){
if(pargs == NULL){
return;
}
uart_mod_t *p = pargs;
if(p->rx_dat_len > 0 && p->rx_dat_len == PACKAGE_SIZE){
if(p->rx_buf[0] == PACKAGE_HEAD
&& p->rx_buf[PACKAGE_SIZE - 1] == PACKAGE_TAIL){
user_cmd_mod.head = p->rx_buf[0];
user_cmd_mod.cmd.value_n[0] = p->rx_buf[1];
user_cmd_mod.cmd.value_n[1] = p->rx_buf[2];
user_cmd_mod.option = p->rx_buf[3];
user_cmd_mod.data.value_n[0] = p->rx_buf[4];
user_cmd_mod.data.value_n[1] = p->rx_buf[5];
user_cmd_mod.data.value_n[2] = p->rx_buf[6];
user_cmd_mod.data.value_n[3] = p->rx_buf[7];
user_cmd_mod.tail = p->rx_buf[PACKAGE_SIZE - 1];
user_cmd_mod.process_flag = 1;
}
}
p->rx_dat_len = 0;
}
int main(void){
usart_init();
usart_set_rx_cbk(&user_uart_mod, motor_get_cmd_from_uart,&user_uart_mod);
}
本文簡單介紹了基于STM32基于DMA,利用串口空閑中斷進(jìn)行串口數(shù)據(jù)接收的具體配置和實現(xiàn)方法,代碼基于標(biāo)準(zhǔn)庫3.5版本;
因為標(biāo)準(zhǔn)庫ST目前已經(jīng)不再更新,并且ST提供了cubemx工具可以進(jìn)行基于HAL庫和LL庫的外設(shè)快速配置,從而簡化大量工作;當(dāng)然為了不掉頭發(fā)感覺擼寄存器也不錯,最終適合自己的才是最好的。
聯(lián)系客服