Files

188 lines
6.0 KiB
C

#include "sys.h"
#include "delay.h"
#include "uart2.h"
#include "includes.h" //ucos Use
//////////////////////////////////////////////////////////////////////////////////
//This program is for educational purposes only, without the author's permission, may not be used for any other purpose
//ALIENTEK STM32F4 Explorer Board
//Initialize serial port 2
//Hot Atomic @ALIENTEK
//Forum: www.openedv.com
//Date modified: 2015/9/12
//Version: V1.0
//All rights reserved pirated reserved.
//Copyright(C) Guangzhou wing electronic technology limited company 2009-2019
//All rights reserved
//********************************************************************************
//////////////////////////////////////////////////////////////////////////////////
/* Defines -------------------------------------------------------------------*/
#define TX_BUFFER_SIZE 512
#define RX_BUFFER_SIZE 512
/* Type declarations ---------------------------------------------------------*/
typedef struct
{
USART_TypeDef *usart;
u16 RxInPos, RxOutPos;
u16 TxInPos, TxOutPos;
u8 RxBuf[RX_BUFFER_SIZE];
u8 TxBuf[TX_BUFFER_SIZE];
} UART_Info;
/* Exported macro ------------------------------------------------------------*/
UART_Info Uart;
//Initializes IO serial port 2
//Bound: baud rate
void uart2_init(u32 bound)
{
//GPIO port settings
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
Uart.usart = USART2;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD,ENABLE); //Enable GPIOD clock
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); //Enable USART2 clock
//Serial port 2 pins reuse maps
GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_USART2); //GPIOD5 multiplex USART2 TX
GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_USART2); //GPIOD6 multiplex USART2 RX
//USART2 port configuration
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6; //GPIOD5, 6
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //Multiplexing function
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //Speed 50MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //Push-pull multiplexed output
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //Pull-up
GPIO_Init(GPIOD, &GPIO_InitStructure); //Initialize PD5, 6 for USART2 TX, RX
//USART2 Initialization settings
USART_InitStructure.USART_BaudRate = bound; //Baud rate setting
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //Word is 8-bit data format
USART_InitStructure.USART_StopBits = USART_StopBits_1; //One stop bit
USART_InitStructure.USART_Parity = USART_Parity_No; //No parity bit
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //No hardware flow control
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //Send and receive mode
USART_Init(Uart.usart, &USART_InitStructure); //Initialize serial port 2
USART_Cmd(Uart.usart, ENABLE); //Enabling serial port 2
USART_ClearFlag(Uart.usart, USART_FLAG_TC);
USART_ITConfig(Uart.usart, USART_IT_RXNE, ENABLE);//Open break
}
void USART2_IRQHandler(void) //Serial port 2 interrupt service routine
{
u8 rx_data;
OSIntEnter();
if (USART_GetITStatus(Uart.usart, USART_IT_RXNE) != RESET)
{
rx_data = USART_ReceiveData(Uart.usart) & 0xFF; /* Read one byte from the receive data register. */
Uart.RxBuf[Uart.RxInPos++] = rx_data;
Uart.RxInPos %= RX_BUFFER_SIZE;
if (Uart.RxInPos == Uart.RxOutPos)
{
Uart.RxOutPos++;
Uart.RxOutPos %= RX_BUFFER_SIZE;
}
USART_ClearITPendingBit(Uart.usart, USART_IT_RXNE); /* Clear the USART2 receive interrupt. */
}
if (USART_GetITStatus(Uart.usart, USART_IT_TXE) != RESET)
{
/* Write one byte to the transmit data register */
USART_SendData(Uart.usart, Uart.TxBuf[Uart.TxOutPos++]);
Uart.TxOutPos %= RX_BUFFER_SIZE;
if (Uart.TxOutPos == Uart.TxInPos)
{
/* Disable the Transmit interrupt */
USART_ITConfig(Uart.usart, USART_IT_TXE, DISABLE);
}
}
if (USART_GetITStatus(Uart.usart, USART_IT_TC) != RESET)
{
/* Disable the Transmit complete interrupt */
USART_ITConfig(Uart.usart, USART_IT_TC, DISABLE);
}
OSIntExit(); //Exit interrupt
}
/*******************************************************************************
* Function Name : void UART2_PutChar(char data)
* Description : Send 1 byte via USART2
* Parameters : ASCII code
* Return : None
*******************************************************************************/
void UART2_PutChar(const u8 ascii)
{
CPU_SR_ALLOC();
while (((Uart.TxInPos + 1)% TX_BUFFER_SIZE) == (Uart.TxOutPos % TX_BUFFER_SIZE))
USART_ITConfig(Uart.usart, USART_IT_TXE, ENABLE);
CPU_CRITICAL_ENTER();
Uart.TxBuf[Uart.TxInPos++] = ascii;
Uart.TxInPos %= RX_BUFFER_SIZE;
USART_ITConfig(Uart.usart, USART_IT_TXE, ENABLE);
CPU_CRITICAL_EXIT();
}
/*******************************************************************************
* Function Name : void UART2_PutChar(char data)
* Description : Send 1 byte via USART2
* Parameters : ASCII code
* Return : None
*******************************************************************************/
void UART2_PutStr(u8 *str, u16 len)
{
u16 i;
for (i = 0; i < len; i++)
{
UART2_PutChar(str[i]);
}
}
/*******************************************************************************
* Function Name : u8 UART2_GetChar(char *data)
* Description : Send 1 byte data to USART2 transmit buffer
* Parameters : none
* Return : Transmission result (0: buffer empty, 1: buffer has data)
*******************************************************************************/
ErrorStatus UART2_GetChar(u8 *data)
{
if (Uart.RxInPos != Uart.RxOutPos)
{
data[0] = Uart.RxBuf[Uart.RxOutPos];
Uart.RxOutPos++;
Uart.RxOutPos %= RX_BUFFER_SIZE;
return SUCCESS;
}
else
return ERROR;
}
void UART2_Flush(void)
{
CPU_SR_ALLOC();
CPU_CRITICAL_ENTER();
Uart.RxInPos = 0;
Uart.RxOutPos = 0;
CPU_CRITICAL_EXIT();
}