/* * @brief Changes the baudrate of UART channel. * * @param UART_t * Pointer to the UART APP handle. * @param baud Value of new baudrate. * @param oversampling Number of samples to be considered for each symbol. 16 is the standard value. * * @return UART_STATUS_t UART_STATUS_SUCCESS if baudrate changed successfully. * UART_STATUS_BUSY if the UART channel is busy. * * \par<b>Description:</b><br> * The function stops the channel, calculates the clock divider values to achieve the desired baudrate. * Sets the divider values and reconfigures the channel as per the configuration in the UI. The channel is * enabled at the end of configuration. */ UART_STATUS_t UART_SetBaudrate(const UART_t * handle, uint32_t baud, uint32_t oversampling) { UART_STATUS_t ret_stat = UART_STATUS_BUSY; const UART_TX_CONFIG_t * ptr_tx_conf = handle->config->tx_pin_config; XMC_ASSERT("UART_SetBaudrate: UART APP handle invalid", ((handle != NULL)&& ((handle->config != NULL) && (handle->runtime != NULL)))) if ((handle->runtime->tx_busy == false) && (handle->runtime->rx_busy == false)) { /* Set UART TX pin as input pin to avoid spikes on the pin.*/ if (handle->config->mode != UART_MODE_LOOPBACK) { XMC_GPIO_SetMode(ptr_tx_conf->port, ptr_tx_conf->pin, XMC_GPIO_MODE_INPUT_TRISTATE); } /* Stop the UART channel before changing the baudrate.*/ if (XMC_UART_CH_Stop(handle->channel) == XMC_UART_CH_STATUS_OK) { /*Change the baudrate*/ ret_stat = (UART_STATUS_t)XMC_UART_CH_SetBaudrate(handle->channel, baud, oversampling); /*Set the sample point if the baudrate is modified*/ if (ret_stat == UART_STATUS_SUCCESS) { XMC_UART_CH_SetSamplePoint(handle->channel, (uint32_t)(oversampling >> 1U)+1U); } /*Enable UART*/ XMC_UART_CH_Start(handle->channel); /* Initialize UART TX pin */ if (handle->config->mode != UART_MODE_LOOPBACK) { XMC_GPIO_Init(ptr_tx_conf->port, ptr_tx_conf->pin, ptr_tx_conf->config); } }
/*Channel initialization function*/ UART_STATUS_t UART_0_init() { UART_STATUS_t status = UART_STATUS_SUCCESS; status = (UART_STATUS_t)GLOBAL_DMA_Init(&GLOBAL_DMA_0); XMC_DMA_CH_Init(XMC_DMA0, 5U, &UART_0_tx_dma_ch_config); XMC_DMA_CH_EnableEvent(XMC_DMA0, 5U, XMC_DMA_CH_EVENT_TRANSFER_COMPLETE); XMC_DMA_CH_Init(XMC_DMA0, 4U, &UART_0_rx_dma_ch_config); XMC_DMA_CH_EnableEvent(XMC_DMA0, 4U, XMC_DMA_CH_EVENT_TRANSFER_COMPLETE); /*Configure Receive pin*/ XMC_GPIO_Init((XMC_GPIO_PORT_t *)RX_PIN_PORT_BASE, 4U, &UART_0_rx_pin_config); /* Initialize USIC channel in UART mode*/ XMC_UART_CH_Init(SELECTED_UART_CHANNEL, &UART_0_channel_config); /*Set input source path*/ XMC_USIC_CH_SetInputSource(SELECTED_UART_CHANNEL, XMC_USIC_CH_INPUT_DX0, SOURCE_INPUT_SELECT); /* Start UART */ XMC_UART_CH_Start(SELECTED_UART_CHANNEL); /* Initialize UART TX pin */ XMC_GPIO_Init((XMC_GPIO_PORT_t *)TX_PIN_PORT_BASE, 5U, &UART_0_tx_pin_config); /*Set service request for transmit interrupt*/ XMC_USIC_CH_SetInterruptNodePointer(SELECTED_UART_CHANNEL, XMC_USIC_CH_INTERRUPT_NODE_POINTER_TRANSMIT_BUFFER, 0U); /*Set service request for receive interrupt*/ XMC_USIC_CH_SetInterruptNodePointer(SELECTED_UART_CHANNEL, XMC_USIC_CH_INTERRUPT_NODE_POINTER_RECEIVE, 1U); XMC_USIC_CH_SetInterruptNodePointer(SELECTED_UART_CHANNEL, XMC_USIC_CH_INTERRUPT_NODE_POINTER_ALTERNATE_RECEIVE, 1U); /*Set service request for UART protocol events*/ XMC_USIC_CH_SetInterruptNodePointer(SELECTED_UART_CHANNEL, XMC_USIC_CH_INTERRUPT_NODE_POINTER_PROTOCOL, 2U); /*Register transfer complete event handler*/ XMC_DMA_CH_SetEventHandler(XMC_DMA0, 4U, UART_0_dma_rx_handler); /*Register transfer complete event handler*/ XMC_DMA_CH_SetEventHandler(XMC_DMA0, 5U, UART_0_dma_tx_handler); /* make DMA ready for transmission*/ XMC_USIC_CH_TriggerServiceRequest(SELECTED_UART_CHANNEL, 0U); return status; }
/*Channel initialization function*/ UART_STATUS_t TempUart_init() { UART_STATUS_t status = UART_STATUS_SUCCESS; /*Configure Receive pin*/ XMC_GPIO_Init((XMC_GPIO_PORT_t *)PORT5_BASE, 0U, &TempUart_rx_pin_config); /* Initialize USIC channel in UART mode*/ XMC_UART_CH_Init(XMC_UART0_CH0, &TempUart_channel_config); /*Set input source path*/ XMC_USIC_CH_SetInputSource(XMC_UART0_CH0, XMC_USIC_CH_INPUT_DX0, 3U); /* Start UART */ XMC_UART_CH_Start(XMC_UART0_CH0); /* Initialize UART TX pin */ XMC_GPIO_Init((XMC_GPIO_PORT_t *)PORT5_BASE, 1U, &TempUart_tx_pin_config); /*Set service request for transmit interrupt*/ XMC_USIC_CH_SetInterruptNodePointer(XMC_UART0_CH0, XMC_USIC_CH_INTERRUPT_NODE_POINTER_TRANSMIT_BUFFER, 3U); /*Set service request for receive interrupt*/ XMC_USIC_CH_SetInterruptNodePointer(XMC_UART0_CH0, XMC_USIC_CH_INTERRUPT_NODE_POINTER_RECEIVE, 5U); XMC_USIC_CH_SetInterruptNodePointer(XMC_UART0_CH0, XMC_USIC_CH_INTERRUPT_NODE_POINTER_ALTERNATE_RECEIVE, 5U); /*Set service request for UART protocol events*/ XMC_USIC_CH_SetInterruptNodePointer(XMC_UART0_CH0, XMC_USIC_CH_INTERRUPT_NODE_POINTER_PROTOCOL, 0U); /*Set priority and enable NVIC node for transmit interrupt*/ NVIC_SetPriority((IRQn_Type)87, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 18U, 0U)); NVIC_EnableIRQ((IRQn_Type)87); /*Set priority and enable NVIC node for receive interrupt*/ NVIC_SetPriority((IRQn_Type)89, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 18U, 0U)); NVIC_EnableIRQ((IRQn_Type)89); return status; }