2018.04.20 22:21

ST에서 이제 Stdlib를 지원하지 않고 Hal만 지원하여 답답했었는데 보다보니

Hal 드라이버만이 아닌 LL Driver을 지원했습니다.

Drivers\STM32L1xx_HAL_Driver\Src 폴더에 보면 stm32fxxx_hal로 시작하는 것말고 stm32fxxx_ll로 시작하는 것들이 있는데 이게 Low Layer Driver이었습니다.

처음에 드라이버 추가할 때 뭘까 했었는데 이게 Low Layer였다니...

기존에 Stdlib를 썼거나 hal이 뜻하는대로 되지 않아 불편한 사람들에게는 정말 좋은 것 같습니다. (다른 건 몰라도 SPI나 UART등등 통신으로 Hal 처리하는 게 원하는 대로 되지 않아 불편했었는데 다행이었습니다.)

 

/*******************************************************************************
* Function Name :
* Parameters    :
* Return        :
* Description   : 

RX만 인터럽트 사용
*******************************************************************************/
void UART_Init(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_AHB1_GRP1_EnableClock(LL_AHB1_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);

  /* (3) Enable USART peripheral clock and clock source ***********************/
  LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_USART1);

  /* (4) Configure USART functional parameters ********************************/
  
  /* Disable USART prior modifying configuration registers */
  /* Note: Commented as corresponding to Reset value */
  // LL_USART_Disable(USARTx_INSTANCE);

  /* 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 Baudrate to 115200 using APB frequency set to 32000000 Hz */
  /* Frequency available for USART peripheral can also be calculated through LL RCC macro */
  /* Ex :
    Periphclk = LL_RCC_GetUSARTClockFreq(Instance); or LL_RCC_GetUARTClockFreq(Instance); depending on USART/UART instance
  
    In this example, Peripheral Clock is expected to be equal to 32000000 Hz => equal to SystemCoreClock
  */
  LL_USART_SetBaudRate(USART1, SystemCoreClock, LL_USART_OVERSAMPLING_16, a_nBaud);

  /* (5) Enable USART *********************************************************/
  LL_USART_Enable(USART1);

  /* (2) NVIC Configuration for USART interrupts */
  /*  - Set priority for USARTx_IRQn */
  /*  - Enable USARTx_IRQn */
  NVIC_SetPriority(USART1_IRQn, 0); 
  NVIC_EnableIRQ(USART1_IRQn);

 

  /* Enable RXNE and Error interrupts */
  LL_USART_EnableIT_RXNE(USART1);
  LL_USART_EnableIT_ERROR(USART1);

  }
  else if(a_chUart == 2)
  {

  }
}

 

/*******************************************************************************
* Function Name :
* Parameters    :
* Return        :
* Description   : 
*******************************************************************************/
void UART_PutChar(uint8_t data)
{
 /* Wait for TXE flag to be raised */
 while (!LL_USART_IsActiveFlag_TXE(USART1)){}

 /* Write character in Transmit Data register.
 TXE flag is cleared by writing data in DR register */
 LL_USART_TransmitData8(USART1, data);

 /* Wait for TC flag to be raised for last char */
 while (!LL_USART_IsActiveFlag_TC(USART1)){} 
}

 

/*******************************************************************************
* Function Name : 
* Parameters    :
* Return        :

* Description   : 
stm32fxxx_it.c 파일 내용


void USARTx_IRQHandler(void)
{
  if(LL_USART_IsActiveFlag_RXNE(USARTx_INSTANCE) && LL_USART_IsEnabledIT_RXNE(USARTx_INSTANCE))
  {
    USART_CharReception_Callback();
  }
  else
  {
    Error_Callback();
  } 
}
*******************************************************************************/
void USART_CharReception_Callback(void)

 Uart1RxBuf = LL_USART_ReceiveData8(USART1);
 /* ECHO mode Start */
// LL_USART_TransmitData8(USARTx_INSTANCE, Uart1RxBuf);
 /* ECHO mode End */
}

 

 

Posted by 초보 HW 엔지니어 로망와니

댓글을 달아 주세요