216 lines
6.9 KiB
C
216 lines
6.9 KiB
C
#include "sys.h"
|
|
#include "usart.h"
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|
//If you are using ucos, include the following header file.
|
|
#if SYSTEM_SUPPORT_OS
|
|
#include "includes.h" //ucos Use
|
|
#endif
|
|
/////////////////////////////////////////////////////////////////////////////////
|
|
//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 1
|
|
//Hot Atomic @ALIENTEK
|
|
//Forum: www.openedv.com
|
|
//Date modified: 2014/6/10
|
|
//Version: V1.5
|
|
//All rights reserved pirated reserved.
|
|
//Copyright(C) Guangzhou wing electronic technology limited company 2009-2019
|
|
//All rights reserved
|
|
//********************************************************************************
|
|
//V1.3 changes description
|
|
// Supports serial port baud rate setting at different frequencies.
|
|
// Added support for printf
|
|
// Serial port receive command features have been added.
|
|
// Fixed printf first character missing bug
|
|
//V1.4 amended description
|
|
// 1, modify the initialize serial IO bug
|
|
// 2, modify the USART_RX_STA makes serial port maximum bytes received 2 of 14
|
|
// 3, USART_REC_LEN, defines the serial port the maximum allowable number of bytes received (not more than 2 of 14)
|
|
// 4, the modified EN_USART1_RX method
|
|
//V1.5 modify the description
|
|
// 1, added support for UCOSII
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
//Add the following code, support for printf function and do not need to select use MicroLIB
|
|
#if 1
|
|
#pragma import(__use_no_semihosting)
|
|
//Standard library required support function
|
|
struct __FILE
|
|
{
|
|
int handle;
|
|
};
|
|
|
|
FILE __stdout;
|
|
//Define _sys_exit () to avoid using host model
|
|
void _sys_exit(int x)
|
|
{
|
|
x = x;
|
|
}
|
|
//Fputc function is defined
|
|
int fputc(int ch, FILE *f)
|
|
{
|
|
#ifdef CONSOLE_DEBUG
|
|
UART1_PutChar(ch);
|
|
#endif // #ifdef CONSOLE_DEBUG
|
|
|
|
return ch;
|
|
}
|
|
#endif
|
|
|
|
#ifdef CONSOLE_DEBUG
|
|
|
|
UART1_Info Uart1;
|
|
|
|
|
|
//Initializes IO serial port 1
|
|
//Bound: baud rate
|
|
void uart1_init(u32 bound)
|
|
{
|
|
//GPIO port settings
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
USART_InitTypeDef USART_InitStructure;
|
|
|
|
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //Enables GPIOA clock
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//Enable USART1
|
|
|
|
//Serial port 1 pins reuse maps
|
|
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1); //GPIOA9 multiplex USART1
|
|
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1); //GPIOA10 multiplex USART1
|
|
|
|
//USART1 port configuration
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; //GPIOA9 and GPIOA10
|
|
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(GPIOA,&GPIO_InitStructure); //Initialize PA9,PA10
|
|
|
|
//USART1 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(USART1, &USART_InitStructure); //Initialize serial port 1
|
|
|
|
USART_Cmd(USART1, ENABLE); //Enabling serial port 1
|
|
|
|
USART_ClearFlag(USART1, USART_FLAG_TC);
|
|
|
|
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//Open break
|
|
}
|
|
|
|
void USART1_IRQHandler(void) //Serial port 1 interrupt service routine
|
|
{
|
|
USART_TypeDef *usart;
|
|
CPU_INT08U rx_data;
|
|
|
|
#if SYSTEM_SUPPORT_OS //Use operating system UCOS
|
|
OSIntEnter();
|
|
#endif
|
|
usart = USART1;
|
|
|
|
if (USART_GetITStatus(usart, USART_IT_RXNE) != RESET)
|
|
{
|
|
rx_data = USART_ReceiveData(usart) & 0xFF; /* Read one byte from the receive data register. */
|
|
|
|
Uart1.RxBuf[Uart1.RxInPos++] = rx_data;
|
|
Uart1.RxInPos %= UART1_RX_BUFFER_SIZE;
|
|
|
|
if (Uart1.RxInPos == Uart1.RxOutPos)
|
|
{
|
|
Uart1.RxOutPos++;
|
|
Uart1.RxOutPos %= UART1_RX_BUFFER_SIZE;
|
|
}
|
|
|
|
USART_ClearITPendingBit(usart, USART_IT_RXNE); /* Clear the USART1 receive interrupt. */
|
|
}
|
|
|
|
if (USART_GetITStatus(usart, USART_IT_TXE) != RESET)
|
|
{
|
|
/* Write one byte to the transmit data register */
|
|
USART_SendData(usart, Uart1.TxBuf[Uart1.TxOutPos++]);
|
|
Uart1.TxOutPos %= UART1_TX_BUFFER_SIZE;
|
|
|
|
if (Uart1.TxOutPos == Uart1.TxInPos)
|
|
{
|
|
/* Disable the USART1 Transmit interrupt */
|
|
USART_ITConfig(usart, USART_IT_TXE, DISABLE);
|
|
}
|
|
}
|
|
#if SYSTEM_SUPPORT_OS
|
|
OSIntExit(); //Exit interrupt
|
|
#endif
|
|
}
|
|
|
|
/*******************************************************************************
|
|
* Function Name : void UART1_PutChar(char data)
|
|
* Description : UART1 1문자 출력
|
|
* Parameters : 아스키코드
|
|
* Return : None
|
|
*******************************************************************************/
|
|
void UART1_PutChar(const u8 ascii)
|
|
{
|
|
CPU_SR_ALLOC();
|
|
|
|
while (((Uart1.TxInPos + 1)% UART1_TX_BUFFER_SIZE) == (Uart1.TxOutPos % UART1_TX_BUFFER_SIZE))
|
|
USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
|
|
|
|
CPU_CRITICAL_ENTER();
|
|
|
|
Uart1.TxBuf[Uart1.TxInPos++] = ascii;
|
|
Uart1.TxInPos %= UART1_TX_BUFFER_SIZE;
|
|
|
|
USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
|
|
|
|
CPU_CRITICAL_EXIT();
|
|
}
|
|
|
|
/*******************************************************************************
|
|
* Function Name : u8 UART1_GetChar(char *data)
|
|
* Description : UART1로 수신버퍼에서 데이타(1byte) 가져오기
|
|
* Parameters : none
|
|
* Return : 데이타 수신 여부(0: 수신버퍼가 비었음, 1: 수신버퍼에 데이타가 있음)
|
|
*******************************************************************************/
|
|
ErrorStatus UART1_GetChar(u8 *data)
|
|
{
|
|
if (Uart1.RxInPos != Uart1.RxOutPos)
|
|
{
|
|
data[0] = Uart1.RxBuf[Uart1.RxOutPos];
|
|
Uart1.RxOutPos++;
|
|
Uart1.RxOutPos %= UART1_RX_BUFFER_SIZE;
|
|
return SUCCESS;
|
|
}
|
|
else
|
|
return ERROR;
|
|
}
|
|
|
|
/*******************************************************************************
|
|
* Function Name : u8 UART1_GetChar(char *data)
|
|
* Description : UART1로 수신버퍼에서 데이타(1byte) 가져오기
|
|
* Parameters : none
|
|
* Return : 데이타 수신 여부(0: 수신버퍼가 비었음, 1: 수신버퍼에 데이타가 있음)
|
|
*******************************************************************************/
|
|
u8 UART1_GC(void)
|
|
{
|
|
u8 result = 0;
|
|
|
|
if (Uart1.RxInPos != Uart1.RxOutPos)
|
|
{
|
|
result = Uart1.RxBuf[Uart1.RxOutPos];
|
|
Uart1.RxOutPos++;
|
|
Uart1.RxOutPos %= UART1_RX_BUFFER_SIZE;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
#endif // #ifdef CONSOLE_DEBUG
|
|
|
|
|
|
|
|
|
|
|