Created
November 27, 2014 08:30
-
-
Save earlwlkr/243a024bf88e05f70171 to your computer and use it in GitHub Desktop.
UART driver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/************************************************************************/ | |
/* Author: cqkhanh */ | |
/* Date: 21/11/2014*/ | |
/* Brief: Driver for UART*/ | |
/* History Prepared/Updated by */ | |
/* +21/11/2014: initial version cqkhanh */ | |
/* +26/11/2014: update send and receive functions earlwlkr */ | |
/************************************************************************/ | |
/* Include */ | |
#include <stdint.h> | |
#include "stm32f4xx_gpio.h" | |
#include "stm32f4xx_usart.h" | |
#include "uart.h" | |
/* Private Macro */ | |
#define UART_NUM USART3 | |
#define UART_CLK RCC_APB1Periph_USART3 | |
#define UART_PORT_CLK RCC_AHB1Periph_GPIOB | |
#define UART_PORT GPIOB | |
#define UART_PIN_TX GPIO_Pin_10 | |
#define UART_PINSOURCE_TX GPIO_PinSource10 | |
#define UART_PIN_RX GPIO_Pin_11 | |
#define UART_PINSOURCE_RX GPIO_PinSource11 | |
/* Public function body */ | |
/************************************************************************/ | |
/** | |
* @brief Initialize uart | |
* @param baud: user baudrate | |
* @retval uart_err_t | |
*/ | |
uart_err_t UART_Init (void) | |
{ | |
uart_err_t res = UERR_OK; | |
USART_InitTypeDef USART_InitStruct; | |
GPIO_InitTypeDef gpioInitStructure; | |
// Unlock clocks for GPIO. | |
RCC_APB1PeriphClockCmd(UART_CLK, ENABLE); | |
RCC_AHB1PeriphClockCmd(UART_PORT_CLK, ENABLE); | |
// Config GPI struct variable. | |
gpioInitStructure.GPIO_Pin = UART_PIN_TX | UART_PIN_RX; | |
gpioInitStructure.GPIO_Mode = GPIO_Mode_AF; | |
gpioInitStructure.GPIO_Speed = GPIO_Speed_100MHz; | |
gpioInitStructure.GPIO_OType = GPIO_OType_PP; | |
gpioInitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; | |
// Connect the pin to the desired peripherals' Alternate Function. | |
GPIO_PinAFConfig(UART_PORT, UART_PIN_TX, GPIO_AF_USART3); | |
GPIO_PinAFConfig(UART_PORT, UART_PIN_RX, GPIO_AF_USART3); | |
GPIO_Init(UART_PORT, &gpioInitStructure); | |
// Init USART struct. | |
USART_StructInit(&USART_InitStruct); | |
USART_Init(UART_NUM, &USART_InitStruct); | |
USART_Cmd(UART_NUM, ENABLE); | |
return res; | |
} | |
/************************************************************************/ | |
/************************************************************************/ | |
/** | |
* @brief Sends a character. | |
* @param ch: the character to send. | |
* @retval uart_err_t | |
*/ | |
uart_err_t UART_Send (uint8_t ch) | |
{ | |
uart_err_t res = UERR_OK; | |
USART_SendData(UART_NUM, (uint16_t)ch); | |
return res; | |
} | |
/************************************************************************/ | |
/************************************************************************/ | |
/** | |
* @brief Receives a character. | |
* @param p_ch: the character to store value to. | |
* @retval uart_err_t | |
*/ | |
uart_err_t UART_Receive (uint8_t* p_ch) | |
{ | |
uart_err_t res = UERR_OK; | |
*p_ch = USART_ReceiveData(UART_NUM); | |
return res; | |
} | |
/************************************************************************/ | |
/* End module */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment