高级会员
- 积分
- 542
- 金钱
- 542
- 注册时间
- 2017-10-17
- 在线时间
- 118 小时
|
/*
* hal_USART4_stm32f1.c
*/
#include "hal_usart.h"
#include "stm32f10x.h"
#include "bsp.h"
#include <string.h>
static uint8_t USART4_RxBuffer[0x100];
static uint8_t USART4_TxBuffer[0x100];
static uint32_t USART4_ReadIndex = 0;
/**
* ��ʼ������
*/
void USART4_Init(void)
{
/**
* 1.��ʼ��ʱ��
*/
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1|RCC_AHBPeriph_DMA2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);
/**
* 2.��ʼ��IO
*/
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure USART4 Rx as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* Configure USART4 Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/**
* 3.��ʼ��������
*/
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 115200;
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;
/* Configure USARTy */
USART_Init(UART4, &USART_InitStructure);
USART_ITConfig(UART4, USART_IT_IDLE, ENABLE);
/**
* 4.��ʼ��DMA
*/
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA2_Channel3);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(UART4->DR);
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&USART4_RxBuffer[0];
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = sizeof(USART4_RxBuffer);
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����
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA2_Channel3, &DMA_InitStructure);
USART_DMACmd(UART4, USART_DMAReq_Rx, ENABLE);
DMA_Cmd(DMA2_Channel3, ENABLE);
DMA_DeInit(DMA2_Channel5);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(UART4->DR);
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&USART4_TxBuffer[0];
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = sizeof(USART4_TxBuffer);
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_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA2_Channel5, &DMA_InitStructure);
USART_DMACmd(UART4, USART_DMAReq_Tx, ENABLE);
DMA_ITConfig(DMA2_Channel5, DMA_IT_TC, ENABLE);
USART_Cmd(UART4, ENABLE);
/**
* 5.��ʼ���ж�ϵͳ
*/
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable the USART4 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Enable the DMA1_Channel2 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = DMA2_Channel4_5_IRQn; //TX
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/**
*
* @param buffer
* @param length
* @return
*/
uint32_t USART4_Send(void* buffer, uint32_t length)
{
uint32_t send_length;
if( length > sizeof(USART4_TxBuffer) )
{
send_length = sizeof(USART4_TxBuffer);
}
else
{
send_length = length;
}
memcpy(USART4_TxBuffer, buffer, send_length);
DMA_Cmd(DMA2_Channel5, DISABLE);
DMA_SetCurrDataCounter(DMA2_Channel5, send_length);
DMA_Cmd(DMA2_Channel5, ENABLE);
return send_length;
}
static USART_SendDoneISR SendDoneISR4 = 0;
/**
*
* @param isr
*/
void USART4_SetSendDoneISR (USART_SendDoneISR isr)
{
SendDoneISR4 = isr;
}
/**
*
* @param buffer
* @param max_length
* @return
*/
uint32_t USART4_Recv(uint8_t* buffer, uint32_t max_length)
{
uint32_t recv_length;
uint32_t write_index;
write_index = sizeof(USART4_RxBuffer) - DMA_GetCurrDataCounter(DMA2_Channel3);
recv_length = 0;
while( (write_index - USART4_ReadIndex) != 0 ) // �����
{
buffer[recv_length] = USART4_RxBuffer[USART4_ReadIndex];
recv_length ++;
USART4_ReadIndex ++;
if( USART4_ReadIndex == sizeof(USART4_RxBuffer) )
{
USART4_ReadIndex = 0;
}
if( recv_length == max_length )
{
break;
}
}
return recv_length;
}
uint32_t USART4_DataAvailable(void)
{
uint32_t filled_length;
uint32_t write_index;
write_index = sizeof(USART4_RxBuffer) - DMA_GetCurrDataCounter(DMA2_Channel3);
if( USART4_ReadIndex > write_index )
{
filled_length = (sizeof(USART4_RxBuffer) - USART4_ReadIndex) + write_index;
}
else
{
filled_length = write_index - USART4_ReadIndex;
}
return filled_length;
}
uint32_t USART4_Flush(void)
{
uint32_t flush_length;
uint32_t write_index;
write_index = sizeof(USART4_RxBuffer) - DMA_GetCurrDataCounter(DMA2_Channel3);
if( USART4_ReadIndex > write_index )
{
flush_length = (sizeof(USART4_RxBuffer) - USART4_ReadIndex) + write_index;
}
else
{
flush_length = write_index - USART4_ReadIndex;
}
USART4_ReadIndex = write_index;
return flush_length;
}
static USART_RecvTimeoutISR RecvTimeoutISR4 = 0;
/**
*
* @param isr
*/
void USART4_SetRecvTimeoutISR (USART_RecvTimeoutISR isr)
{
RecvTimeoutISR4 = isr;
}
void UART4_IRQHandler(void)
{
// uint16_t recv;
// SYS_EnterInt();
if(USART_GetITStatus(UART4, USART_IT_IDLE) != RESET)//���տ�������Ϊ��ʱ
{
// recv =
USART_ReceiveData(UART4);
if( RecvTimeoutISR4 != 0 )
{
RecvTimeoutISR4();
}
USART_ClearITPendingBit(UART4, USART_IT_IDLE);
}
// SYS_ExitInt();
}
void DMA2_Channel4_5_IRQHandler(void)
{
// SYS_EnterInt();
if ( DMA_GetITStatus(DMA2_IT_TC5) )
{
if( SendDoneISR4 != 0 )
{
SendDoneISR4();
}
DMA_ClearITPendingBit(DMA2_IT_TC5);
}
// SYS_ExitInt();
}
以上是一个DMA 串口4收发的初始化及函数调用的方式
|
|