#include "sys.h" #include "delay.h" #include "uart3.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 ------------------------------------------------------------*/ /* Select SPI FLASH: Chip Select pin low */ #define COMM3_CS_DISABLE GPIO_WriteBit(GPIOD, GPIO_Pin_11, Bit_RESET);\ GPIO_WriteBit(GPIOD, GPIO_Pin_12, Bit_RESET); /* Deselect SPI FLASH: Chip Select pin high */ #define COMM3_CS_ENABLE GPIO_WriteBit(GPIOD, GPIO_Pin_11, Bit_SET);\ GPIO_WriteBit(GPIOD, GPIO_Pin_12, Bit_SET);\ delay_ms(2); /* Private variables ---------------------------------------------------------*/ UART_Info Uart3; //Initializes IO serial port 3 //Bound: baud rate void uart3_init(u32 bound) { //GPIO port settings GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; Uart3.usart = USART3; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD , ENABLE); // Enable GPIOD clock RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); // Enable USART3 clock //Serial port3 pins reuse maps GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_USART3); // GPIOD8 multiplex USART3 TX GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_USART3); // GPIOD9 multiplex USART3 RX //USART3 port configuration GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9; // GPIOD 8, 9 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 PC 10, 11 for USART3 TX, RX //UART3 port TX enable configuration GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11; // GPIOD10, 11 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; // 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_NOPULL; // Pull-up GPIO_Init(GPIOD,&GPIO_InitStructure); // Initialize PD1 for UART5 RTS //USART3 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(Uart3.usart, &USART_InitStructure); // Initialize serial port 3 USART_Cmd(Uart3.usart, ENABLE); // Enabling serial port 3 USART_ClearFlag(Uart3.usart, USART_FLAG_TC); USART_ITConfig(Uart3.usart, USART_IT_RXNE, ENABLE); // Rx Interrupt COMM3_CS_DISABLE; } void USART3_IRQHandler(void) //Serial port 3 interrupt service routine { u8 rx_data; OSIntEnter(); // RX interrupt if (USART_GetITStatus(Uart3.usart, USART_IT_RXNE) != RESET) { rx_data = USART_ReceiveData(Uart3.usart) & 0xFF; /* Read one byte from the receive data register. */ Uart3.RxBuf[Uart3.RxInPos++] = rx_data; Uart3.RxInPos %= RX_BUFFER_SIZE; if (Uart3.RxInPos == Uart3.RxOutPos) { Uart3.RxOutPos++; Uart3.RxOutPos %= RX_BUFFER_SIZE; } USART_ClearITPendingBit(Uart3.usart, USART_IT_RXNE); /* Clear the USART3 receive interrupt. */ } // TX interrupt if (USART_GetITStatus(Uart3.usart, USART_IT_TXE) != RESET) { /* Write one byte to the transmit data register */ USART_SendData(Uart3.usart, Uart3.TxBuf[Uart3.TxOutPos++]); Uart3.TxOutPos %= RX_BUFFER_SIZE; if (Uart3.TxOutPos == Uart3.TxInPos) { /* Disable the Transmit interrupt */ USART_ITConfig(Uart3.usart, USART_IT_TXE, DISABLE); } } // TX Complete interrupt if (USART_GetITStatus(Uart3.usart, USART_IT_TC) != RESET) { /* Disable the Transmit complete interrupt */ USART_ITConfig(Uart3.usart, USART_IT_TC, DISABLE); /* Disable the Tx enable */ COMM3_CS_DISABLE; } OSIntExit(); //Exit interrupt } /******************************************************************************* * Function Name : void UART3_PutChar(char data) * Description : Send 1 byte via USART2 * Parameters : ASCII code * Return : None *******************************************************************************/ void UART3_PutChar(const u8 ascii) { CPU_SR_ALLOC(); while (((Uart3.TxInPos + 1)% TX_BUFFER_SIZE) == (Uart3.TxOutPos % TX_BUFFER_SIZE)) USART_ITConfig(Uart3.usart, USART_IT_TXE, ENABLE); CPU_CRITICAL_ENTER(); Uart3.TxBuf[Uart3.TxInPos++] = ascii; Uart3.TxInPos %= TX_BUFFER_SIZE; USART_ITConfig(Uart3.usart, USART_IT_TXE, ENABLE); CPU_CRITICAL_EXIT(); } /******************************************************************************* * Function Name : void UART3_PutChar(char data) * Description : Send 1 byte via USART3 * Parameters : ASCII code * Return : None *******************************************************************************/ void UART3_PutStr(u8 *str, u16 len) { u16 i; COMM3_CS_ENABLE; for (i = 0; i < len; i++) { UART3_PutChar(str[i]); } USART_ITConfig(Uart3.usart, USART_IT_TC, ENABLE); } /******************************************************************************* * Function Name : u8 UART3_GetChar(char *data) * Description : Send 1 byte data to USART3 transmit buffer * Parameters : none * Return : Transmission result (0: buffer empty, 1: buffer has data) *******************************************************************************/ ErrorStatus UART3_GetChar(u8 *data) { if (Uart3.RxInPos != Uart3.RxOutPos) { data[0] = Uart3.RxBuf[Uart3.RxOutPos]; Uart3.RxOutPos++; Uart3.RxOutPos %= RX_BUFFER_SIZE; return SUCCESS; } else return ERROR; } void UART3_Flush(void) { CPU_SR_ALLOC(); CPU_CRITICAL_ENTER(); Uart3.RxInPos = 0; Uart3.RxOutPos = 0; CPU_CRITICAL_EXIT(); }