179 lines
6.4 KiB
C
179 lines
6.4 KiB
C
#include "sys.h"
|
|
#include "uart5.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 5
|
|
//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];
|
|
} COM_Info;
|
|
|
|
COM_Info Com;
|
|
|
|
//Initializes IO serial port 5
|
|
//Bound: baud rate
|
|
void uart5_init(u32 bound)
|
|
{
|
|
//GPIO port settings
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
USART_InitTypeDef USART_InitStructure;
|
|
|
|
memset(&Com, 0x00, sizeof(COM_Info));
|
|
Com.usart = UART5;
|
|
|
|
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE); //Enable GPIOC clock
|
|
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD,ENABLE); //Enable GPIOD clock
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5,ENABLE); //Enable UART5 clock
|
|
|
|
//Serial port 5 pins reuse maps
|
|
GPIO_PinAFConfig(GPIOC,GPIO_PinSource12, GPIO_AF_UART5); //GPIOC12 multiplex UART5 TX
|
|
GPIO_PinAFConfig(GPIOD,GPIO_PinSource2 , GPIO_AF_UART5); //GPIOD2 multiplex UART5 RX
|
|
|
|
//UART5 port configuration
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //GPIOC12
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //Multiplexing function
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //Speed 50MHz
|
|
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //Push-pull multiplexed output
|
|
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //Pull-up
|
|
GPIO_Init(GPIOC,&GPIO_InitStructure); //Initialize PC12 for UART5 TX
|
|
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //GPIOD2
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //Multiplexing function
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //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 PD2 for UART5 RX
|
|
|
|
//UART5 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(Com.usart, &USART_InitStructure); //Initialize serial port 5
|
|
|
|
USART_Cmd(Com.usart, ENABLE); //Enabling serial port 5
|
|
|
|
USART_ClearFlag(Com.usart, USART_FLAG_TC);
|
|
|
|
USART_ITConfig(Com.usart, USART_IT_RXNE, ENABLE);//Open break
|
|
}
|
|
|
|
void UART5_IRQHandler(void) //Serial port 5 interrupt service routine
|
|
{
|
|
CPU_INT08U rx_data;
|
|
|
|
OSIntEnter();
|
|
|
|
if (USART_GetITStatus(Com.usart, USART_IT_RXNE) != RESET) {
|
|
rx_data = USART_ReceiveData(Com.usart) & 0xFF; /* Read one byte from the receive data register. */
|
|
|
|
Com.RxBuf[Com.RxInPos++] = rx_data;
|
|
Com.RxInPos %= RX_BUFFER_SIZE;
|
|
|
|
if (Com.RxInPos == Com.RxOutPos) {
|
|
Com.RxOutPos++;
|
|
Com.RxOutPos %= RX_BUFFER_SIZE;
|
|
}
|
|
|
|
USART_ClearITPendingBit(Com.usart, USART_IT_RXNE); /* Clear the UART5 receive interrupt. */
|
|
}
|
|
|
|
if (USART_GetITStatus(Com.usart, USART_IT_TXE) != RESET) {
|
|
/* Write one byte to the transmit data register */
|
|
USART_SendData(Com.usart, Com.TxBuf[Com.TxOutPos++]);
|
|
Com.TxOutPos %= RX_BUFFER_SIZE;
|
|
|
|
if (Com.TxOutPos == Com.TxInPos) {
|
|
/* Disable the USART1 Transmit interrupt */
|
|
USART_ITConfig(Com.usart, USART_IT_TXE, DISABLE);
|
|
}
|
|
}
|
|
if (USART_GetITStatus(Com.usart, USART_IT_TC) != RESET) {
|
|
/* Disable the Transmit complete interrupt */
|
|
USART_ITConfig(Com.usart, USART_IT_TC, DISABLE);
|
|
}
|
|
OSIntExit(); //Exit interrupt
|
|
}
|
|
|
|
/*******************************************************************************
|
|
* Function Name : void UART5_PutChar(char data)
|
|
* Description : Send 1 byte via UART5
|
|
* Parameters : ASCII code
|
|
* Return : None
|
|
*******************************************************************************/
|
|
void UART5_PutChar(const u8 ascii)
|
|
{
|
|
CPU_SR_ALLOC();
|
|
|
|
while (((Com.TxInPos + 1)% TX_BUFFER_SIZE) == (Com.TxOutPos % TX_BUFFER_SIZE))
|
|
USART_ITConfig(Com.usart, USART_IT_TXE, ENABLE);
|
|
|
|
CPU_CRITICAL_ENTER();
|
|
|
|
Com.TxBuf[Com.TxInPos++] = ascii;
|
|
Com.TxInPos %= TX_BUFFER_SIZE;
|
|
|
|
USART_ITConfig(Com.usart, USART_IT_TXE, ENABLE);
|
|
|
|
CPU_CRITICAL_EXIT();
|
|
}
|
|
|
|
/*******************************************************************************
|
|
* Function Name : void UART5_PutChar(char data)
|
|
* Description : Send 1 byte via UART5
|
|
* Parameters : ASCII code
|
|
* Return : None
|
|
*******************************************************************************/
|
|
void UART5_PutStr(u8 *str, u16 len)
|
|
{
|
|
u16 i;
|
|
|
|
for (i = 0; i < len; i++) {
|
|
UART5_PutChar(str[i]);
|
|
}
|
|
}
|
|
|
|
/*******************************************************************************
|
|
* Function Name : u8 UART5_GetChar(char *data)
|
|
* Description : Send 1 byte data to UART5 transmit buffer
|
|
* Parameters : none
|
|
* Return : Transmission result (0: buffer empty, 1: buffer has data)
|
|
*******************************************************************************/
|
|
ErrorStatus UART5_GetChar(u8 *data)
|
|
{
|
|
if (Com.RxInPos != Com.RxOutPos) {
|
|
data[0] = Com.RxBuf[Com.RxOutPos];
|
|
Com.RxOutPos++;
|
|
Com.RxOutPos %= RX_BUFFER_SIZE;
|
|
return SUCCESS;
|
|
}
|
|
else
|
|
return ERROR;
|
|
}
|
|
|
|
|
|
|
|
|