초보의 아웅다웅 설계하기/STM32

STM32H7 hal Driver를 사용한 RX Interrupt

로망와니 2021. 2. 11. 22:15
#define RBUF_SIZE       512

typedef struct{
    uint16_t RxInCnt,RxOutCnt;
    uint8_t RxBuf[RBUF_SIZE];
}tUart;

tUart Uart1,Uart2;

void Uart_Conf(void)
{
  UartHandle.Instance        = USARTx;

  UartHandle.Init.BaudRate     = 115200;
  UartHandle.Init.WordLength   = UART_WORDLENGTH_8B;
  UartHandle.Init.StopBits     = UART_STOPBITS_1;
  UartHandle.Init.Parity       = UART_PARITY_NONE;
  UartHandle.Init.HwFlowCtl    = UART_HWCONTROL_NONE;
  UartHandle.Init.Mode         = UART_MODE_TX_RX;
  UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
  UartHandle.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;

  if(HAL_UART_DeInit(&UartHandle) != HAL_OK)
  {
    Error_Handler();
  }  
  if(HAL_UART_Init(&UartHandle) != HAL_OK)
  {
    Error_Handler();
  }
  HAL_NVIC_SetPriority(USART1_IRQn, 1, 0);
  HAL_NVIC_EnableIRQ(USART1_IRQn);
	
  __HAL_UART_ENABLE_IT(&UartHandle, UART_IT_RXNE);
}

int main(void)
{
  MPU_Config();
  /* Enable the CPU Cache */
  CPU_CACHE_Enable();

  /* STM32H7xx HAL library initialization:
       - Systick timer is configured by default as source of time base, but user 
         can eventually implement his proper time base source (a general purpose 
         timer for example or other time source), keeping in mind that Time base 
         duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and 
         handled in milliseconds basis.
       - Set NVIC Group Priority to 4
       - Low Level Initialization
     */
  HAL_Init();

  /* Configure the system clock to 200 MHz */
  SystemClock_Config();
		
  Uart_Conf();
}


/* stm32h7xx_it.c */

uint32_t LL_USART_IsActiveFlag_RXNE_RXFNE()
{
  return ((READ_BIT(USART1->ISR, USART_ISR_RXNE_RXFNE) == (USART_ISR_RXNE_RXFNE)) ? 1UL : 0UL);
}

uint32_t LL_USART_IsEnabledIT_RXNE_RXFNE()
{
  return ((READ_BIT(USART1->CR1, USART_CR1_RXNEIE_RXFNEIE) == (USART_CR1_RXNEIE_RXFNEIE)) ? 1UL : 0UL);
}

#define LL_USART_IsActiveFlag_RXNE  LL_USART_IsActiveFlag_RXNE_RXFNE
#define LL_USART_IsEnabledIT_RXNE  LL_USART_IsEnabledIT_RXNE_RXFNE

void USART1_IRQHandler(void)
{
  /* Check RXNE flag value in ISR register */
  if(LL_USART_IsActiveFlag_RXNE() && LL_USART_IsEnabledIT_RXNE()){
    /* RXNE flag will be cleared by reading of RDR register (done in call) */
    /* Call function in charge of handling Character reception */
		Uart1.RxBuf[Uart1.RxInCnt] = (uint8_t)(READ_BIT(USART1->RDR, USART_RDR_RDR) & 0xFFU);;
		/* ECHO mode Start */
		USART1->TDR = Uart1.RxBuf[Uart1.RxInCnt];
		/* ECHO mode End */

		if(Uart1.RxInCnt<RBUF_SIZE-1) Uart1.RxInCnt++;
		else Uart1.RxInCnt = 0;
  }
}