📄 usart.c
字号:
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_conf.h"
#include "stm32f10x_lib.h"
#include "ucos_ii.h"
//OS_EVENT* hUartSend;
INT8U uart_tx_buffer[128];
INT8U m_tx_header,m_tx_tail;
void USART_GPIO_Configuration(void);
//void USART_DMA_SendData(INT8U* buffer,INT8U size);
void USART_NVIC_Configuration(void);
/*******************************************************************************
* Function Name : USART_Configuration
* Description : Configures the different system clocks.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
//初始化
//stm_init();
m_tx_header = m_tx_tail = 0;
USART_GPIO_Configuration();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
// RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA, ENABLE);
/* USARTx configuration ------------------------------------------------------*/
/* 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 Clock disabled
- USART CPOL: Clock is active low
- USART CPHA: Data is captured on the second edge
- USART LastBit: The clock pulse of the last data bit is not output to
the SCLK pin
*/
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;
USART_InitStructure.USART_Clock = USART_Clock_Disable;
USART_InitStructure.USART_CPOL = USART_CPOL_Low;
USART_InitStructure.USART_CPHA = USART_CPHA_2Edge;
USART_InitStructure.USART_LastBit = USART_LastBit_Disable;
// hUartSend = OSSemCreate(1);
/* Configure the USARTx */
USART_Init(USART1, &USART_InitStructure);
USART_NVIC_Configuration();
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_ITConfig(USART1, USART_IT_TC, ENABLE);
/* Enable the USARTx */
USART_Cmd(USART1, ENABLE);
/* Enable USART1 DMA Rx and TX request */
// USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE);
//
}
/*
void USART_DMA_SendData(INT8U* buffer,INT8U size)
{
INT8U perr;
DMA_InitTypeDef DMA_InitStructure;
//等待空闲
OSSemPend(hUartSend,0,&perr);
// DMA Channel4 (triggered by USART1 Tx event) Config
DMA_DeInit(DMA_Channel4);
DMA_InitStructure.DMA_PeripheralBaseAddr = 0x40013804;//USART1->DR;
DMA_InitStructure.DMA_MemoryBaseAddr = (INT32U)buffer;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = 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_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA_Channel4, &DMA_InitStructure);
DMA_ITConfig(DMA_Channel4,DMA_IT_TC,ENABLE);
// Enable DMA Channel4
DMA_Cmd(DMA_Channel4, ENABLE);
}
*/
/*******************************************************************************
* Function Name : NVIC_Configuration
* Description : Configures the nested vectored interrupt controller.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void USART_NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
//Enable the USART1 Interrupt
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Enable the USART1 DMA Interrupt */
}
/*******************************************************************************
* Function Name : GPIO_Configuration
* Description : Configures the different GPIO ports.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void USART_GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure USART1 Tx (PA.09) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void USART_DMA_SEND_ISR(void)
{
OS_CPU_SR cpu_sr;
OS_ENTER_CRITICAL(); /* Tell uC/OS-II that we are starting an ISR */
OSIntNesting++;
OS_EXIT_CRITICAL();
if(DMA_GetITStatus(DMA_IT_TC4) == SET)
{
// OSSemPost(hUartSend);
DMA_ClearITPendingBit(DMA_IT_TC4);
}
OSIntExit(); /* Tell uC/OS-II that we are leaving the ISR */
}
void USART_RECV_ISR(void)
{
INT16U inChar;
OS_CPU_SR cpu_sr;
OS_ENTER_CRITICAL(); /* Tell uC/OS-II that we are starting an ISR */
OSIntNesting++;
OS_EXIT_CRITICAL();
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
/* Clear the USART2 Receive interrupt */
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
inChar = USART_ReceiveData(USART1);
// ProcInput((INT8U)inChar);
}
if(USART_GetITStatus(USART1,USART_IT_TC) != RESET)
{
if(m_tx_header != m_tx_tail)
{
USART_SendData(USART1,(INT16U)uart_tx_buffer[m_tx_header++]);
m_tx_header &= 0x7f;
}
USART_ClearITPendingBit(USART1,USART_IT_TC);
}
OSIntExit();
}
void USART_SendBuffer(INT8U* data,INT8U length)
{
BOOLEAN bNeedStart = 0;
if(length == 0)
{
return;
}
if(m_tx_header == m_tx_tail)
{
bNeedStart = 1;
}
for(; length > 0 ; length--)
{
if(!(m_tx_tail == 127 && m_tx_header == 0)
&& m_tx_header != (m_tx_tail+1))
{
uart_tx_buffer[m_tx_tail++] = *data++;
m_tx_tail &= 0x7f;
}
else
{
break;
}
}
if(bNeedStart != 0)
{
USART_SendData(USART1,(INT16U)uart_tx_buffer[m_tx_header++]);
m_tx_header &= 0x7f;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -