#include "stm32l4xx_ll_bus.h"
#include "stm32l4xx_ll_rcc.h"
#include "stm32l4xx_ll_gpio.h"
#include "stm32l4xx_ll_usart.h"
#include "stm32l4xx_ll_lpuart.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro ------------------------------------------------------------*/
#define USART_1 1
#define USART_2 2
#define USART_3 3
#define USART_4 4
#define BAUDRATE_9600 9600
#define BAUDRATE_38400 38400
#define BAUDRATE_57600 57600
#define BAUDRATE_115200 115200
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes ---------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/*******************************************************************************
* Function Name :
* Parameters : None
* Return : None
* Description :
*******************************************************************************/
void Config_UART(void)
{
Init_UART(USART_1, BAUDRATE_115200);
Init_LPUART(USART_1, BAUDRATE_115200);
}
/*******************************************************************************
* Function Name : void Init_UART(uint8_t a_chUart,uint32_t baud)
* Parameters : 통신포트. 통신속도
* Return : None
* Description : UART 초기화
*******************************************************************************/
void Init_UART(uint8_t a_chUart, uint32_t a_nBaud)
{
if(a_chUart == 1){
/* (1) Enable GPIO clock and configures the USART pins *********************/
/* Enable the peripheral clock of GPIO Port */
LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOA);
/* Configure Tx Pin as : Alternate function, High Speed, Push pull, Pull up */
LL_GPIO_SetPinMode(GPIOA, LL_GPIO_PIN_9, LL_GPIO_MODE_ALTERNATE);
LL_GPIO_SetAFPin_8_15(GPIOA, LL_GPIO_PIN_9, LL_GPIO_AF_7);
LL_GPIO_SetPinSpeed(GPIOA, LL_GPIO_PIN_9, LL_GPIO_SPEED_FREQ_HIGH);
LL_GPIO_SetPinOutputType(GPIOA, LL_GPIO_PIN_9, LL_GPIO_OUTPUT_PUSHPULL);
LL_GPIO_SetPinPull(GPIOA, LL_GPIO_PIN_9, LL_GPIO_PULL_UP);
/* Configure Rx Pin as : Alternate function, High Speed, Push pull, Pull up */
LL_GPIO_SetPinMode(GPIOA, LL_GPIO_PIN_10, LL_GPIO_MODE_ALTERNATE);
LL_GPIO_SetAFPin_8_15(GPIOA, LL_GPIO_PIN_10, LL_GPIO_AF_7);
LL_GPIO_SetPinSpeed(GPIOA, LL_GPIO_PIN_10, LL_GPIO_SPEED_FREQ_HIGH);
LL_GPIO_SetPinOutputType(GPIOA, LL_GPIO_PIN_10, LL_GPIO_OUTPUT_PUSHPULL);
LL_GPIO_SetPinPull(GPIOA, LL_GPIO_PIN_10, LL_GPIO_PULL_UP);
/* (2) NVIC Configuration for USART interrupts */
/* - Set priority for USARTx_IRQn */
/* - Enable USARTx_IRQn */
NVIC_SetPriority(USART1_IRQn, 2);
NVIC_EnableIRQ(USART1_IRQn);
/* (3) Enable USART peripheral clock and clock source ***********************/
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_USART1);
/* Set clock source */
LL_RCC_SetUSARTClockSource(LL_RCC_USART1_CLKSOURCE_PCLK2);
/* TX/RX direction */
LL_USART_SetTransferDirection(USART1, LL_USART_DIRECTION_TX_RX);
/* 8 data bit, 1 start bit, 1 stop bit, no parity */
LL_USART_ConfigCharacter(USART1, LL_USART_DATAWIDTH_8B, LL_USART_PARITY_NONE, LL_USART_STOPBITS_1);
/* No Hardware Flow control */
/* Reset value is LL_USART_HWCONTROL_NONE */
LL_USART_SetHWFlowCtrl(USART1, LL_USART_HWCONTROL_NONE);
/* Oversampling by 16 */
/* Reset value is LL_USART_OVERSAMPLING_16 */
LL_USART_SetOverSampling(USART1, LL_USART_OVERSAMPLING_16);
/* Set Boad Rate */
LL_USART_SetBaudRate(USART1, SystemCoreClock, LL_USART_OVERSAMPLING_16, a_nBaud);
/* (5) Enable USART *********************************************************/
LL_USART_Enable(USART1);
while((!(LL_USART_IsActiveFlag_TEACK(USART1))) || (!(LL_USART_IsActiveFlag_REACK(USART1))))
{
}
/* Enable RXNE and Error interrupts */
LL_USART_EnableIT_RXNE(USART1);
LL_USART_EnableIT_ERROR(USART1);
ClearBuf_UART1();
}
else if(a_chUart == 2){
}
else if(a_chUart == 3){
}
else if(a_chUart == 4){
#ifdef USE_USART4
/* (1) Enable GPIO clock and configures the USART pins *********************/
/* Enable the peripheral clock of GPIO Port */
LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOC);
/* Configure Tx Pin as : Alternate function, High Speed, Push pull, Pull up */
LL_GPIO_SetPinMode(GPIOC, LL_GPIO_PIN_10, LL_GPIO_MODE_ALTERNATE);
LL_GPIO_SetAFPin_8_15(GPIOC, LL_GPIO_PIN_10, LL_GPIO_AF_8);
LL_GPIO_SetPinSpeed(GPIOC, LL_GPIO_PIN_10, LL_GPIO_SPEED_FREQ_HIGH);
LL_GPIO_SetPinOutputType(GPIOC, LL_GPIO_PIN_10, LL_GPIO_OUTPUT_PUSHPULL);
LL_GPIO_SetPinPull(GPIOC, LL_GPIO_PIN_10, LL_GPIO_PULL_UP);
/* Configure Rx Pin as : Alternate function, High Speed, Push pull, Pull up */
LL_GPIO_SetPinMode(GPIOC, LL_GPIO_PIN_11, LL_GPIO_MODE_ALTERNATE);
LL_GPIO_SetAFPin_8_15(GPIOC, LL_GPIO_PIN_11, LL_GPIO_AF_8);
LL_GPIO_SetPinSpeed(GPIOC, LL_GPIO_PIN_11, LL_GPIO_SPEED_FREQ_HIGH);
LL_GPIO_SetPinOutputType(GPIOC, LL_GPIO_PIN_11, LL_GPIO_OUTPUT_PUSHPULL);
LL_GPIO_SetPinPull(GPIOC, LL_GPIO_PIN_11, LL_GPIO_PULL_UP);
/* (2) NVIC Configuration for USART interrupts */
/* - Set priority for USARTx_IRQn */
/* - Enable USARTx_IRQn */
NVIC_SetPriority(UART4_IRQn, 2);
NVIC_EnableIRQ(UART4_IRQn);
/* (3) Enable USART peripheral clock and clock source ***********************/
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_UART4);
/* Set clock source */
LL_RCC_SetUSARTClockSource(LL_RCC_UART4_CLKSOURCE_PCLK1);
/* TX/RX direction */
LL_USART_SetTransferDirection(UART4, LL_USART_DIRECTION_TX_RX);
/* 8 data bit, 1 start bit, 1 stop bit, no parity */
LL_USART_ConfigCharacter(UART4, LL_USART_DATAWIDTH_8B, LL_USART_PARITY_NONE, LL_USART_STOPBITS_1);
/* No Hardware Flow control */
/* Reset value is LL_USART_HWCONTROL_NONE */
LL_USART_SetHWFlowCtrl(UART4, LL_USART_HWCONTROL_NONE);
/* Oversampling by 16 */
/* Reset value is LL_USART_OVERSAMPLING_16 */
LL_USART_SetOverSampling(UART4, LL_USART_OVERSAMPLING_16);
/* Set Boad Rate */
LL_USART_SetBaudRate(UART4, SystemCoreClock, LL_USART_OVERSAMPLING_16, a_nBaud);
/* (5) Enable USART *********************************************************/
LL_USART_Enable(UART4);
while((!(LL_USART_IsActiveFlag_TEACK(UART4))) || (!(LL_USART_IsActiveFlag_REACK(UART4))))
{
}
/* Enable RXNE and Error interrupts */
LL_USART_EnableIT_RXNE(UART4);
LL_USART_EnableIT_ERROR(UART4);
ClearBuf_UART4();
#endif
}
}
/*******************************************************************************
* Function Name : void Init_UART(uint8_t a_chUart,uint32_t baud)
* Parameters : 통신포트. 통신속도
* Return : None
* Description : UART 초기화
*******************************************************************************/
void Init_LPUART(uint8_t a_chUart, uint32_t a_nBaud)
{
if(a_chUart == 1){
/* (1) Enable GPIO clock and configures the LPUART1 pins *******************/
/* (TX on PC.1, RX on PC.0) **********************/
/* Enable the peripheral clock of GPIOC */
LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOC);
/* Configure TX Pin as : Alternate function, High Speed, PushPull, Pull up */
LL_GPIO_SetPinMode(GPIOC, LL_GPIO_PIN_1, LL_GPIO_MODE_ALTERNATE);
LL_GPIO_SetAFPin_0_7(GPIOC, LL_GPIO_PIN_1, LL_GPIO_AF_8);
LL_GPIO_SetPinSpeed(GPIOC, LL_GPIO_PIN_1, LL_GPIO_SPEED_FREQ_HIGH);
LL_GPIO_SetPinOutputType(GPIOC, LL_GPIO_PIN_1, LL_GPIO_OUTPUT_PUSHPULL);
LL_GPIO_SetPinPull(GPIOC, LL_GPIO_PIN_1, LL_GPIO_PULL_NO);
/* Configure RX Pin as : Alternate function, High Speed, PushPull, Pull up */
LL_GPIO_SetPinMode(GPIOC, LL_GPIO_PIN_0, LL_GPIO_MODE_ALTERNATE);
LL_GPIO_SetAFPin_0_7(GPIOC, LL_GPIO_PIN_0, LL_GPIO_AF_8);
LL_GPIO_SetPinSpeed(GPIOC, LL_GPIO_PIN_0, LL_GPIO_SPEED_FREQ_HIGH);
LL_GPIO_SetPinOutputType(GPIOC, LL_GPIO_PIN_0, LL_GPIO_OUTPUT_PUSHPULL);
LL_GPIO_SetPinPull(GPIOC, LL_GPIO_PIN_0, LL_GPIO_PULL_NO);
/* (2) NVIC Configuration for LPUART1 interrupts */
/* - Set priority for LPUART1_IRQn */
/* - Enable LPUART1_IRQn */
NVIC_SetPriority(LPUART1_IRQn, 0);
NVIC_EnableIRQ(LPUART1_IRQn);
/* (3) Enable the LPUART1 peripheral clock and clock source ****************/
LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_LPUART1);
/* Set LPUART1 clock source as HSI */
LL_RCC_SetLPUARTClockSource(LL_RCC_LPUART1_CLKSOURCE_PCLK1);// LL_RCC_SetLPUARTClockSource(LL_RCC_LPUART1_CLKSOURCE_HSI);
/* (4) Configure LPUART1 functional parameters ********************************/
/* Disable LPUART1 prior modifying configuration registers */
/* Note: Commented as corresponding to Reset value */
// LL_LPUART_Disable(LPUART1);
/* TX/RX direction */
LL_LPUART_SetTransferDirection(LPUART1, LL_LPUART_DIRECTION_TX_RX);
/* 8 data bit, 1 start bit, 1 stop bit, no parity */
LL_LPUART_ConfigCharacter(LPUART1, LL_LPUART_DATAWIDTH_8B, LL_LPUART_PARITY_NONE, LL_LPUART_STOPBITS_1);
/* No Hardware Flow control */
/* Reset value is LL_USART_HWCONTROL_NONE */
// LL_USART_SetHWFlowCtrl(LPUART1, LL_USART_HWCONTROL_NONE);
/* Set Baudrate to 9600 using HSI frequency set to HSI_VALUE */
LL_LPUART_SetBaudRate(LPUART1, SystemCoreClock, a_nBaud); // LL_LPUART_SetBaudRate(LPUART1, HSI_VALUE, a_nBaud);
#ifdef USE_USARTWUFMODE
/* Set the wake-up event type : specify wake-up on RXNE flag */
LL_LPUART_SetWKUPType(LPUART1, LL_LPUART_WAKEUP_ON_RXNE);
#endif
/* (5) Enable LPUART1 **********************************************************/
LL_LPUART_Enable(LPUART1);
while((!(LL_USART_IsActiveFlag_TEACK(LPUART1))) || (!(LL_USART_IsActiveFlag_REACK(LPUART1))))
{
}
/* Enable RXNE and Error interrupts */
LL_USART_EnableIT_RXNE(LPUART1);
LL_USART_EnableIT_ERROR(LPUART1);
ClearBuf_LPUART1();
}
else if(a_chUart == 2){
}
else if(a_chUart == 3){
}
else if(a_chUart == 4){
}
}
'초보의 아웅다웅 설계하기 > STM32' 카테고리의 다른 글
J-Link를 사용하여 MCU내 라이팅되어 있는 메모리 확인하기 (0) | 2019.06.28 |
---|---|
ST의 디자인 툴 (0) | 2019.06.04 |
STM32L4 SPI LL_DRIVER (0) | 2019.05.25 |
Keil 자동완성 기능 끄기 (0) | 2019.03.21 |
PID 제어 (0) | 2019.03.02 |