Esempio n. 1
0
uint8_t uart4_init(void) 
{

	
	//zmienna do zwracania statusu funkcji
	uint8_t uart_status;
	//inicjalizacja struktur od GPIO i USART
	GPIO_InitTypeDef     GPIO_InitStruct;

	uart_status = UART_INIT_ERROR;
 
/* Enable GPIO clock */
	__HAL_RCC_GPIOA_CLK_ENABLE();

// Enable clock for UART4 peripheral
	__HAL_RCC_UART4_CLK_ENABLE();
	
/* Configure USART Tx as alternate function */
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
GPIO_InitStruct.Alternate = GPIO_AF8_UART4;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
	
/* Configure USART Rx as alternate function */
GPIO_InitStruct.Pin = GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
GPIO_InitStruct.Alternate = GPIO_AF8_UART4;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

NVIC_SetPriority(UART4_IRQn, 1);
NVIC_EnableIRQ(UART4_IRQn);

//Handle
		UART4_HandleStruct.Instance = UART4;
		UART4_HandleStruct.Init.BaudRate = 115200;
		UART4_HandleStruct.Init.StopBits = UART_STOPBITS_1;
		UART4_HandleStruct.Init.WordLength = UART_WORDLENGTH_8B;
		UART4_HandleStruct.Init.Parity = UART_PARITY_NONE;
		UART4_HandleStruct.Init.Mode = UART_MODE_TX_RX;

//Init
uart_status = HAL_UART_Init(&UART4_HandleStruct);
//UART4 enable
__HAL_UART_ENABLE(&UART4_HandleStruct);


//Wlaczenie przerwan od RX dla UART4
#ifdef UART4_IT_RX_ON
UART4 -> CR1 |= USART_CR1_RXNEIE;
#endif
		return uart_status;
}
Esempio n. 2
0
uint8_t usart2_init(void) 
{
	//zmienna do zwracania statusu funkcji
	uint8_t usart_status;
	
	//inicjalizacja struktur od GPIO
	GPIO_InitTypeDef     GPIO_InitStruct;
	
	usart_status = UART_INIT_ERROR;
	
/* Enable GPIO clock */
	__HAL_RCC_GPIOA_CLK_ENABLE();

// Enable clock for USART1 peripheral
	__HAL_RCC_USART2_CLK_ENABLE();
	
/* Configure USART Tx as alternate function */
		GPIO_InitStruct.Pin = GPIO_PIN_2;
		GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
		GPIO_InitStruct.Pull = GPIO_NOPULL;
		GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
		GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
		HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
	
/* Configure USART Rx as alternate function */
		GPIO_InitStruct.Pin = GPIO_PIN_3;
		GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
		GPIO_InitStruct.Pull = GPIO_PULLUP;
		GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
		GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
		HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
		
		//Priorytet przerwania
	//	HAL_NVIC_SetPriority(USART2_IRQn , 0 , 1);
	//	HAL_NVIC_EnableIRQ(USART2_IRQn);
		
//Handle
		USART2_HandleStruct.Instance = USART2;
		USART2_HandleStruct.Init.BaudRate = BAUDRATE_USART2;
		USART2_HandleStruct.Init.StopBits = USART_STOPBITS_1;
		USART2_HandleStruct.Init.Parity = USART_PARITY_NONE;
		USART2_HandleStruct.Init.Mode = USART_MODE_TX_RX;
		
//Init
		usart_status = HAL_UART_Init(&USART2_HandleStruct);
	
//USART2 enable	
		__HAL_UART_ENABLE(&USART2_HandleStruct);

		//Przerwania od RX ON
			#ifdef USART2_IT_RX_ON
		//	USART2 -> CR1 |= USART_CR1_RXNEIE;
			#endif
			
		return usart_status;
}
/**
  * @brief Initializes the RS485 Driver enable feature according to the specified
  *         parameters in the UART_InitTypeDef and creates the associated handle .
  * @param huart: uart handle
  * @param Polarity: select the driver enable polarity
  *        This parameter can be one of the following values:
  *          @arg UART_DE_POLARITY_HIGH: DE signal is active high
  *          @arg UART_DE_POLARITY_LOW: DE signal is active low
  * @param AssertionTime: Driver Enable assertion time
  *                         5-bit value defining the time between the activation of the DE (Driver Enable)
  *                         signal and the beginning of the start bit. It is expressed in sample time
  *                         units (1/8 or 1/16 bit time, depending on the oversampling rate)         
  * @param DeassertionTime: Driver Enable deassertion time          
  *                         5-bit value defining the time between the end of the last stop bit, in a
  *                         transmitted message, and the de-activation of the DE (Driver Enable) signal.
  *                         It is expressed in sample time units (1/8 or 1/16 bit time, depending on the
  *                         oversampling rate).
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_RS485Ex_Init(UART_HandleTypeDef *huart, uint32_t Polarity, uint32_t AssertionTime, uint32_t DeassertionTime)
{
  uint32_t temp = 0x0;
  
  /* Check the UART handle allocation */
  if(huart == NULL)
  {
    return HAL_ERROR;
  }

  /* Check the Driver Enable polarity */
  assert_param(IS_UART_DE_POLARITY(Polarity));
  
  /* Check the Driver Enable assertion time */
  assert_param(IS_UART_ASSERTIONTIME(AssertionTime));
  
  /* Check the Driver Enable deassertion time */
  assert_param(IS_UART_DEASSERTIONTIME(DeassertionTime));
  
  if(huart->State == HAL_UART_STATE_RESET)
  {   
    /* Init the low level hardware : GPIO, CLOCK, CORTEX */
    HAL_UART_MspInit(huart);
  }

  huart->State = HAL_UART_STATE_BUSY;

  /* Disable the Peripheral */
  __HAL_UART_DISABLE(huart);
  
  /* Set the UART Communication parameters */
  UART_SetConfig(huart);
  
  if(huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
  {
    UART_AdvFeatureConfig(huart);
  }
  
  /* Enable the Driver Enable mode by setting the DEM bit in the CR3 register */
  huart->Instance->CR3 |= USART_CR3_DEM;
  
  /* Set the Driver Enable polarity */
  MODIFY_REG(huart->Instance->CR3, USART_CR3_DEP, Polarity);
  
  /* Set the Driver Enable assertion and deassertion times */
  temp = (AssertionTime << UART_CR1_DEAT_ADDRESS_LSB_POS);
  temp |= (DeassertionTime << UART_CR1_DEDT_ADDRESS_LSB_POS);
  MODIFY_REG(huart->Instance->CR1, (USART_CR1_DEDT|USART_CR1_DEAT), temp);
  
  /* Enable the Peripheral */
  __HAL_UART_ENABLE(huart);
  
  /* TEACK and/or REACK to check before moving huart->State to Ready */
  return (UART_CheckIdleState(huart));
}
Esempio n. 4
0
void Uart::Active(FunctionalState newState)
{
	if(ENABLE == newState)
	{
		__HAL_UART_ENABLE(&m_huart);
	}
	else
	{
		__HAL_UART_DISABLE(&m_huart);
	}

}
Esempio n. 5
0
/**
  * @brief Initializes the LIN mode according to the specified
  *         parameters in the UART_InitTypeDef and creates the associated handle .
  * @param huart: uart handle
  * @param BreakDetectLength: specifies the LIN break detection length.
  *        This parameter can be one of the following values:
  *          @arg UART_LINBREAKDETECTLENGTH_10B: 10-bit break detection
  *          @arg UART_LINBREAKDETECTLENGTH_11B: 11-bit break detection
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_LIN_Init(UART_HandleTypeDef *huart, uint16_t BreakDetectLength)
{
  /* Check the UART handle allocation */
  if(huart == NULL)
  {
    return HAL_ERROR;
  }
  /* Check the Break detection length parameter */
  assert_param(IS_UART_LIN_BREAK_DETECT_LENGTH(BreakDetectLength));
  
  /* LIN mode limited to 16-bit oversampling only */
  if(huart->Init.OverSampling == UART_OVERSAMPLING_8)
  {
    return HAL_ERROR;
  }
  
  /* Init the low level hardware : GPIO, CLOCK, CORTEX */
  HAL_UART_MspInit(huart);
  
  /* Disable the Peripheral */
  __HAL_UART_DISABLE(huart);
  
  /* Set the UART Communication parameters */
  if (UART_SetConfig(huart) == HAL_ERROR)
  {
    return HAL_ERROR;
  } 
  
  if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
  {
    UART_AdvFeatureConfig(huart);
  }
  
  /* In LIN mode, the following bits must be kept cleared: 
  - LINEN and CLKEN bits in the USART_CR2 register,
  - SCEN and IREN bits in the USART_CR3 register.*/
  huart->Instance->CR2 &= ~(USART_CR2_CLKEN);
  huart->Instance->CR3 &= ~(USART_CR3_HDSEL | USART_CR3_IREN | USART_CR3_SCEN);
  
  /* Enable the LIN mode by setting the LINEN bit in the CR2 register */
  huart->Instance->CR2 |= USART_CR2_LINEN;
  
  /* Set the USART LIN Break detection length. */
  MODIFY_REG(huart->Instance->CR2, USART_CR2_LBDL, BreakDetectLength);
  
    /* Enable the Peripheral */
  __HAL_UART_ENABLE(huart);
  
  /* TEACK and/or REACK to check before moving huart->State to Ready */
  return (UART_CheckIdleState(huart));
}
Esempio n. 6
0
/** @brief: init state flag acc to info in huart
 *  @details: used in Uart3Debug_Init, private
 ****************************************************************/
static HAL_StatusTypeDef MyHAL_UARTInit(UART_HandleTypeDef * huart) {
    /* Check the UART handle allocation */
    if (huart == NULL) {
        return HAL_ERROR;
    }

    /* Check the parameters */
    if (huart->Init.HwFlowCtl != UART_HWCONTROL_NONE) {
        /* The hardware flow control is available only for USART1, USART2, USART3 and USART6 */
        assert_param(IS_UART_HWFLOW_INSTANCE(huart->Instance));
        assert_param(IS_UART_HARDWARE_FLOW_CONTROL(huart->Init.HwFlowCtl));
    } else {
        assert_param(IS_UART_INSTANCE(huart->Instance));
    }
    assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength));
    assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling));

    if (huart->State == HAL_UART_STATE_RESET) {
        /* Allocate lock resource and initialize it */
        huart->Lock = HAL_UNLOCKED;
        /* Init the low level hardware */
//		HAL_NVIC_SetPriority(USART3_IRQn, 0, 0);
//		HAL_NVIC_EnableIRQ(USART3_IRQn);
    }

    huart->State = HAL_UART_STATE_BUSY;

    /* Disable the peripheral */
    __HAL_UART_DISABLE(huart);

    /* Set the UART Communication parameters */
    MyUARTSetConfig(huart);

    /* In asynchronous mode, the following bits must be kept cleared:
     * - LINEN and CLKEN bits in the USART_CR2 register,
     * - SCEN, HDSEL and IREN  bits in the USART_CR3 register.*/
    huart->Instance->CR2 &= ~(USART_CR2_LINEN | USART_CR2_CLKEN);
    huart->Instance->CR3 &=
        ~(USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN);

    /* Enable the peripheral */
    __HAL_UART_ENABLE(huart);

    /* Initialize the UART state */
    huart->ErrorCode = HAL_UART_ERROR_NONE;
    huart->State = HAL_UART_STATE_READY;

    return HAL_OK;
} /* MyHAL_UARTInit */
Esempio n. 7
0
/**
  * @brief Set Wakeup from Stop mode interrupt flag selection.
  * @param huart: UART handle.
  * @param WakeUpSelection: address match, Start Bit detection or RXNE bit status.
  * This parameter can be one of the following values:
  *      @arg @ref UART_WAKEUP_ON_ADDRESS
  *      @arg @ref UART_WAKEUP_ON_STARTBIT
  *      @arg @ref UART_WAKEUP_ON_READDATA_NONEMPTY
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_UARTEx_StopModeWakeUpSourceConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection)
{
  HAL_StatusTypeDef status = HAL_OK;
  uint32_t tickstart = 0;

  /* check the wake-up from stop mode UART instance */
  assert_param(IS_UART_WAKEUP_FROMSTOP_INSTANCE(huart->Instance));
  /* check the wake-up selection parameter */
  assert_param(IS_UART_WAKEUP_SELECTION(WakeUpSelection.WakeUpEvent));

  /* Process Locked */
  __HAL_LOCK(huart);

  huart->gState = HAL_UART_STATE_BUSY;

  /* Disable the Peripheral */
  __HAL_UART_DISABLE(huart);

  /* Set the wake-up selection scheme */
  MODIFY_REG(huart->Instance->CR3, USART_CR3_WUS, WakeUpSelection.WakeUpEvent);

  if (WakeUpSelection.WakeUpEvent == UART_WAKEUP_ON_ADDRESS)
  {
    UARTEx_Wakeup_AddressConfig(huart, WakeUpSelection);
  }

  /* Enable the Peripheral */
  __HAL_UART_ENABLE(huart);

  /* Init tickstart for timeout managment*/
  tickstart = HAL_GetTick();

  /* Wait until REACK flag is set */
  if(UART_WaitOnFlagUntilTimeout(huart, USART_ISR_REACK, RESET, tickstart, HAL_UART_TIMEOUT_VALUE) != HAL_OK)
  {
    status = HAL_TIMEOUT;
  }
  else
  {
    /* Initialize the UART State */
    huart->gState = HAL_UART_STATE_READY;
  }

  /* Process Unlocked */
  __HAL_UNLOCK(huart);

  return status;
}
Esempio n. 8
0
void USART_Configuration(UART_HandleTypeDef* usartHandle)
{
	usartHandle->Instance			= USART1;
	usartHandle->Init.BaudRate		= 9600;
	usartHandle->Init.WordLength	= UART_WORDLENGTH_8B;
	usartHandle->Init.StopBits		= UART_STOPBITS_1;
	usartHandle->Init.Parity		= UART_PARITY_NONE;
	usartHandle->Init.HwFlowCtl		= UART_HWCONTROL_NONE;
	usartHandle->Init.Mode			= UART_MODE_TX_RX;
	usartHandle->Init.OverSampling  = UART_OVERSAMPLING_16;
	if (HAL_UART_Init(usartHandle) != HAL_OK)
	{
		errorHandler("USART_INIT");
	}
	__HAL_UART_ENABLE(usartHandle);
}
Esempio n. 9
0
/*====================================================================================================*/
void Serial_Config( void )
{
  GPIO_InitTypeDef GPIO_InitStruct;

  /* UART Clk ******************************************************************/
  UARTx_TX_GPIO_CLK_ENABLE();
  UARTx_RX_GPIO_CLK_ENABLE();
  UARTx_CLK_ENABLE();

  /* UART Pin ******************************************************************/
  GPIO_InitStruct.Mode      = GPIO_MODE_ANALOG;
  GPIO_InitStruct.Pull      = GPIO_NOPULL;
  GPIO_InitStruct.Speed     = GPIO_SPEED_HIGH;

  GPIO_InitStruct.Pin       = GPIO_PIN_15;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  GPIO_InitStruct.Pin       = GPIO_PIN_7;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  GPIO_InitStruct.Mode      = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull      = GPIO_PULLUP;
  GPIO_InitStruct.Speed     = GPIO_SPEED_HIGH;

  GPIO_InitStruct.Pin       = UARTx_TX_PIN;
  GPIO_InitStruct.Alternate = UARTx_TX_AF;
  HAL_GPIO_Init(UARTx_TX_GPIO_PORT, &GPIO_InitStruct);

  GPIO_InitStruct.Pin       = UARTx_RX_PIN;
  GPIO_InitStruct.Alternate = UARTx_RX_AF;
  HAL_GPIO_Init(UARTx_RX_GPIO_PORT, &GPIO_InitStruct);

  /* UART Init *****************************************************************/
  Serial_HandleStruct.Instance          = UARTx;
  Serial_HandleStruct.Init.BaudRate     = UARTx_BAUDRATE;
  Serial_HandleStruct.Init.WordLength   = UARTx_BYTESIZE;
  Serial_HandleStruct.Init.StopBits     = UARTx_STOPBITS;
  Serial_HandleStruct.Init.Parity       = UARTx_PARITY;
  Serial_HandleStruct.Init.HwFlowCtl    = UARTx_HARDWARECTRL;
  Serial_HandleStruct.Init.Mode         = UARTx_MODE;
  Serial_HandleStruct.Init.OverSampling = UARTx_OVERSAMPLE;
  HAL_UART_Init(&Serial_HandleStruct);

  /* UART Enable ***************************************************************/
  __HAL_UART_ENABLE(&Serial_HandleStruct);
  __HAL_UART_CLEAR_FLAG(&Serial_HandleStruct, UART_FLAG_TC);
}
Esempio n. 10
0
/*====================================================================================================*/
void Serial_Config( void )
{
  GPIO_InitTypeDef GPIO_InitStruct;

  /* UART Clk ******************************************************************/
  UARTx_TX_GPIO_CLK_ENABLE();
  UARTx_RX_GPIO_CLK_ENABLE();
  UARTx_CLK_ENABLE();

  /* UART Pin ******************************************************************/
  GPIO_InitStruct.Mode      = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull      = GPIO_NOPULL;
  GPIO_InitStruct.Speed     = GPIO_SPEED_HIGH;

  GPIO_InitStruct.Pin       = UARTx_TX_PIN;
  HAL_GPIO_Init(UARTx_TX_GPIO_PORT, &GPIO_InitStruct);

  GPIO_InitStruct.Mode      = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull      = GPIO_NOPULL;
  GPIO_InitStruct.Speed     = GPIO_SPEED_HIGH;

  GPIO_InitStruct.Pin       = UARTx_RX_PIN;
  HAL_GPIO_Init(UARTx_RX_GPIO_PORT, &GPIO_InitStruct);

  /* UART IT *******************************************************************/
//  HAL_NVIC_SetPriority(UARTx_IRQn, 0, 1);
//  HAL_NVIC_EnableIRQ(UARTx_IRQn);

  /* UART Init *****************************************************************/
  Serial_InitStruct.Instance          = UARTx;
  Serial_InitStruct.Init.BaudRate     = UARTx_BAUDRATE;
  Serial_InitStruct.Init.WordLength   = UARTx_BYTESIZE;
  Serial_InitStruct.Init.StopBits     = UARTx_STOPBITS;
  Serial_InitStruct.Init.Parity       = UARTx_PARITY;
  Serial_InitStruct.Init.HwFlowCtl    = UARTx_HARDWARECTRL;
  Serial_InitStruct.Init.Mode         = UARTx_MODE;
  HAL_UART_Init(&Serial_InitStruct);

  /* UART Enable ***************************************************************/
//  __HAL_UART_ENABLE_IT(&Serial_InitStruct, UART_IT_RXNE);
  __HAL_UART_ENABLE(&Serial_InitStruct);
  __HAL_UART_CLEAR_FLAG(&Serial_InitStruct, UART_FLAG_TC);
}
/**
  * @brief Set Wakeup from Stop mode interrupt flag selection
  * @param huart: uart handle, 
  * @param WakeUpSelection: address match, Start Bit detection or RXNE bit status.
  * This parameter can be one of the following values:
  *      @arg UART_WAKEUP_ON_ADDRESS
  *      @arg UART_WAKEUP_ON_STARTBIT
  *      @arg UART_WAKEUP_ON_READDATA_NONEMPTY
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_UARTEx_StopModeWakeUpSourceConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection)
{

  /* check the wake-up from stop mode UART instance */
  assert_param(IS_UART_WAKEUP_FROMSTOP_INSTANCE(huart->Instance));
  /* Check the wake-up selection parameter */
  assert_param(IS_UART_WAKEUP_SELECTION(WakeUpSelection.WakeUpEvent));

  /* Process Locked */
  __HAL_LOCK(huart);

  huart->State = HAL_UART_STATE_BUSY;

  /* Disable the Peripheral */
  __HAL_UART_DISABLE(huart);

  /* Set the wake-up selection scheme */
  MODIFY_REG(huart->Instance->CR3, USART_CR3_WUS, WakeUpSelection.WakeUpEvent);

  if(WakeUpSelection.WakeUpEvent == UART_WAKEUP_ON_ADDRESS)
  {
    UART_Wakeup_AddressConfig(huart, WakeUpSelection);
  }

  /* Enable the Peripheral */
  __HAL_UART_ENABLE(huart);

  /* Wait until REACK flag is set before moving huart->State to Ready */
  if(UART_WaitOnFlagUntilTimeout(huart, USART_ISR_REACK, RESET, UART_REACK_TIMEOUT) != HAL_OK)
  {
    return HAL_TIMEOUT;
  }

  /* Process Unlocked */
  __HAL_UNLOCK(huart);

  /* Initialize the UART state*/
  huart->ErrorCode = HAL_UART_ERROR_NONE;
  huart->State= HAL_UART_STATE_READY;

  return HAL_OK;
}
Esempio n. 12
0
/*====================================================================================================*/
void RS232_Config( void )
{
  GPIO_InitTypeDef GPIO_InitStruct;

  /* UARTx Clk */
  UARTx_TX_GPIO_CLK_ENABLE();
  UARTx_RX_GPIO_CLK_ENABLE();
  UARTx_CLK_ENABLE();

  /* UARTx Pin */
  GPIO_InitStruct.Mode      = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull      = GPIO_PULLUP;
  GPIO_InitStruct.Speed     = GPIO_SPEED_HIGH;

  GPIO_InitStruct.Pin       = UARTx_TX_PIN;
  GPIO_InitStruct.Alternate = UARTx_TX_AF;
  HAL_GPIO_Init(UARTx_TX_GPIO_PORT, &GPIO_InitStruct);

  GPIO_InitStruct.Pin       = UARTx_RX_PIN;
  GPIO_InitStruct.Alternate = UARTx_RX_AF;
  HAL_GPIO_Init(UARTx_RX_GPIO_PORT, &GPIO_InitStruct);

  /* UART Init *****************************************************************/
  UART_HandleStruct.Instance          = UARTx;
  UART_HandleStruct.Init.BaudRate     = UARTx_BAUDRATE;
  UART_HandleStruct.Init.WordLength   = UARTx_BYTESIZE;
  UART_HandleStruct.Init.StopBits     = UARTx_STOPBITS;
  UART_HandleStruct.Init.Parity       = UARTx_PARITY;
  UART_HandleStruct.Init.HwFlowCtl    = UARTx_HARDWARECTRL;
  UART_HandleStruct.Init.Mode         = UARTx_MODE;
//  UART_InitStruct.OverSampling = UARTx_OVERSAMPLE;
  HAL_UART_Init(&UART_HandleStruct);

  __HAL_UART_ENABLE(&UART_HandleStruct);
  __HAL_UART_CLEAR_FLAG(&UART_HandleStruct, UART_FLAG_TC);
}
Esempio n. 13
0
/**
  * @brief Initialize the LIN mode according to the specified
  *        parameters in the UART_InitTypeDef and creates the associated handle .
  * @param huart UART handle.
  * @param BreakDetectLength specifies the LIN break detection length.
  *        This parameter can be one of the following values:
  *          @arg @ref UART_LINBREAKDETECTLENGTH_10B 10-bit break detection
  *          @arg @ref UART_LINBREAKDETECTLENGTH_11B 11-bit break detection
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_LIN_Init(UART_HandleTypeDef *huart, uint32_t BreakDetectLength)
{
  /* Check the UART handle allocation */
  if(huart == NULL)
  {
    return HAL_ERROR;
  }

  /* Check the LIN UART instance */
  assert_param(IS_UART_LIN_INSTANCE(huart->Instance));
  /* Check the Break detection length parameter */
  assert_param(IS_UART_LIN_BREAK_DETECT_LENGTH(BreakDetectLength));

  /* LIN mode limited to 16-bit oversampling only */
  if(huart->Init.OverSampling == UART_OVERSAMPLING_8)
  {
    return HAL_ERROR;
  }
  /* LIN mode limited to 8-bit data length */
  if(huart->Init.WordLength != UART_WORDLENGTH_8B)
  {
    return HAL_ERROR;
  }

  if(huart->gState == HAL_UART_STATE_RESET)
  {
    /* Allocate lock resource and initialize it */
    huart->Lock = HAL_UNLOCKED;

    /* Init the low level hardware : GPIO, CLOCK */
    HAL_UART_MspInit(huart);
  }

  huart->gState = HAL_UART_STATE_BUSY;

  /* Disable the Peripheral */
  __HAL_UART_DISABLE(huart);

  /* Set the UART Communication parameters */
  if (UART_SetConfig(huart) == HAL_ERROR)
  {
    return HAL_ERROR;
  }

  if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
  {
    UART_AdvFeatureConfig(huart);
  }

  /* In LIN mode, the following bits must be kept cleared:
  - LINEN and CLKEN bits in the USART_CR2 register,
  - SCEN and IREN bits in the USART_CR3 register.*/
  CLEAR_BIT(huart->Instance->CR2, USART_CR2_CLKEN);
  CLEAR_BIT(huart->Instance->CR3, (USART_CR3_HDSEL | USART_CR3_IREN | USART_CR3_SCEN));

  /* Enable the LIN mode by setting the LINEN bit in the CR2 register */
  SET_BIT(huart->Instance->CR2, USART_CR2_LINEN);

  /* Set the USART LIN Break detection length. */
  MODIFY_REG(huart->Instance->CR2, USART_CR2_LBDL, BreakDetectLength);

  /* Enable the Peripheral */
  __HAL_UART_ENABLE(huart);

  /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
  return (UART_CheckIdleState(huart));
}
/**
  * @brief Initialize the RS485 Driver enable feature according to the specified
  *         parameters in the UART_InitTypeDef and creates the associated handle.
  * @param huart            UART handle.
  * @param Polarity         Select the driver enable polarity.
  *          This parameter can be one of the following values:
  *          @arg @ref UART_DE_POLARITY_HIGH DE signal is active high
  *          @arg @ref UART_DE_POLARITY_LOW  DE signal is active low
  * @param AssertionTime    Driver Enable assertion time:
  *       5-bit value defining the time between the activation of the DE (Driver Enable)
  *       signal and the beginning of the start bit. It is expressed in sample time
  *       units (1/8 or 1/16 bit time, depending on the oversampling rate)
  * @param DeassertionTime  Driver Enable deassertion time:
  *       5-bit value defining the time between the end of the last stop bit, in a
  *       transmitted message, and the de-activation of the DE (Driver Enable) signal.
  *       It is expressed in sample time units (1/8 or 1/16 bit time, depending on the
  *       oversampling rate).
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_RS485Ex_Init(UART_HandleTypeDef *huart, uint32_t Polarity, uint32_t AssertionTime, uint32_t DeassertionTime)
{
  uint32_t temp;

  /* Check the UART handle allocation */
  if (huart == NULL)
  {
    return HAL_ERROR;
  }
  /* Check the Driver Enable UART instance */
  assert_param(IS_UART_DRIVER_ENABLE_INSTANCE(huart->Instance));

  /* Check the Driver Enable polarity */
  assert_param(IS_UART_DE_POLARITY(Polarity));

  /* Check the Driver Enable assertion time */
  assert_param(IS_UART_ASSERTIONTIME(AssertionTime));

  /* Check the Driver Enable deassertion time */
  assert_param(IS_UART_DEASSERTIONTIME(DeassertionTime));

  if (huart->gState == HAL_UART_STATE_RESET)
  {
    /* Allocate lock resource and initialize it */
    huart->Lock = HAL_UNLOCKED;

#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
    UART_InitCallbacksToDefault(huart);

    if (huart->MspInitCallback == NULL)
    {
      huart->MspInitCallback = HAL_UART_MspInit;
    }

    /* Init the low level hardware */
    huart->MspInitCallback(huart);
#else
    /* Init the low level hardware : GPIO, CLOCK, CORTEX */
    HAL_UART_MspInit(huart);
#endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
  }

  huart->gState = HAL_UART_STATE_BUSY;

  /* Disable the Peripheral */
  __HAL_UART_DISABLE(huart);

  /* Set the UART Communication parameters */
  if (UART_SetConfig(huart) == HAL_ERROR)
  {
    return HAL_ERROR;
  }

  if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
  {
    UART_AdvFeatureConfig(huart);
  }

  /* Enable the Driver Enable mode by setting the DEM bit in the CR3 register */
  SET_BIT(huart->Instance->CR3, USART_CR3_DEM);

  /* Set the Driver Enable polarity */
  MODIFY_REG(huart->Instance->CR3, USART_CR3_DEP, Polarity);

  /* Set the Driver Enable assertion and deassertion times */
  temp = (AssertionTime << UART_CR1_DEAT_ADDRESS_LSB_POS);
  temp |= (DeassertionTime << UART_CR1_DEDT_ADDRESS_LSB_POS);
  MODIFY_REG(huart->Instance->CR1, (USART_CR1_DEDT | USART_CR1_DEAT), temp);

  /* Enable the Peripheral */
  __HAL_UART_ENABLE(huart);

  /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
  return (UART_CheckIdleState(huart));
}
Esempio n. 15
0
//Initializes communication peripheral and buffers
void xb_Init(void){
	GPIO_InitTypeDef GPIO_InitStructure = {0};
	DMA_HandleTypeDef hDMA_RX = {0};

	//Configure GPIO pins for UART
	XB_PORT_ENPERCLK();
	GPIO_InitStructure.Pin = XB_PIN_TX | XB_PIN_RX;
	GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
	GPIO_InitStructure.Pull = GPIO_NOPULL;
	GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
	GPIO_InitStructure.Alternate = XB_PORT_AF;
	HAL_GPIO_Init(XB_PORT_INST, &GPIO_InitStructure);

	//Configure USART peripheral
	XB_USART_INST->CR2 = 0xFF000000;																		//I wish I could remember what this line was supposed to do then ... :)
	XB_USART_ENPERCLK();
	xb_ComPort.Instance = XB_USART_INST;
	xb_ComPort.Init.BaudRate = XB_USART_BAUDRATE;
	xb_ComPort.Init.WordLength = UART_WORDLENGTH_8B;
	xb_ComPort.Init.StopBits = UART_STOPBITS_1;
	xb_ComPort.Init.Parity = UART_PARITY_NONE;
	xb_ComPort.Init.HwFlowCtl = UART_HWCONTROL_NONE;
	xb_ComPort.Init.Mode = UART_MODE_TX_RX;
	HAL_UART_Init(&xb_ComPort);

	//Set character match and disable error interrupts (this can be done only while Receiver is enabled and USART is disabled (RE=1, UE=0)
	__HAL_UART_DISABLE(&xb_ComPort);
	//__HAL_UART_DISABLE_IT(&ComPort, UART_IT_ERR);
	MODIFY_REG(XB_USART_INST->CR3, USART_CR3_OVRDIS, USART_CR3_OVRDIS);
	MODIFY_REG(XB_USART_INST->CR2, USART_CR2_ADD, ((unsigned)XB_ENDOFCMDCHAR << UART_CR2_ADDRESS_LSB_POS));
	__HAL_UART_ENABLE(&xb_ComPort);

	//Configure DMA for TX and RX
	XB_DMA_ENPERCLK();
	xb_hDMA_TX.Instance = XB_DMA_TX;
	xb_hDMA_TX.Init.Direction = DMA_MEMORY_TO_PERIPH;
	xb_hDMA_TX.Init.PeriphInc = DMA_PINC_DISABLE;
	xb_hDMA_TX.Init.MemInc = DMA_MINC_ENABLE;
	xb_hDMA_TX.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
	xb_hDMA_TX.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
	xb_hDMA_TX.Init.Mode = DMA_NORMAL;//DMA_CIRCULAR;
	xb_hDMA_TX.Init.Priority = DMA_PRIORITY_MEDIUM;
	HAL_DMA_Init(&xb_hDMA_TX);
	__HAL_LINKDMA(&xb_ComPort, hdmatx, xb_hDMA_TX);
	HAL_NVIC_SetPriority(XB_DMA_TX_IRQN, 3, 1);
	HAL_NVIC_EnableIRQ(XB_DMA_TX_IRQN);

	hDMA_RX.Instance = XB_DMA_RX;
	hDMA_RX.Init.Direction = DMA_PERIPH_TO_MEMORY;
	hDMA_RX.Init.PeriphInc = DMA_PINC_DISABLE;
	hDMA_RX.Init.MemInc = DMA_MINC_ENABLE;
	hDMA_RX.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
	hDMA_RX.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
	hDMA_RX.Init.Mode = DMA_CIRCULAR;
	hDMA_RX.Init.Priority = DMA_PRIORITY_MEDIUM;
	HAL_DMA_Init(&hDMA_RX);
	__HAL_LINKDMA(&xb_ComPort, hdmarx, hDMA_RX);

	//Enable USART interrupts
	HAL_NVIC_SetPriority(XB_USART_IRQN, 3, 3);
	HAL_NVIC_EnableIRQ(XB_USART_IRQN);
	__HAL_UART_ENABLE_IT(&xb_ComPort, UART_IT_CM);

	//Starts port reading
	HAL_UART_Receive_DMA(&xb_ComPort, xb_RxBuffer, XB_RXBUFSIZE);

	xb_MutexId = osMutexCreate(osMutex(xb_Mutex));
}