/**************************************************************************//** * @brief Initialize board support package functionality. * * @param[in] flags Initialization mask, use 0 or @ref BSP_INIT_STK_BCUART. * * @return * @ref BSP_STATUS_OK *****************************************************************************/ int BSP_Init(uint32_t flags) { if ( flags & BSP_INIT_STK_BCUART ) { CMU_ClockEnable(cmuClock_HFPER, true); CMU_ClockEnable(cmuClock_GPIO, true); /* Configure GPIO pin for USART TX */ /* To avoid false start, configure output as high. */ GPIO_PinModeSet(BSP_BC_USART_TXPORT, BSP_BC_USART_TXPIN, gpioModePushPull, 1); /* Configure GPIO pin for USART RX */ GPIO_PinModeSet(BSP_BC_USART_RXPORT, BSP_BC_USART_RXPIN, gpioModeInput, 1); /* Enable switch U602A "VMCU switch" - to enable USART communication. */ /* See board schematics for details. */ GPIO_PinModeSet(BSP_BC_U602A_PORT, BSP_BC_U602A_PIN, gpioModePushPull, 1); CMU_ClockEnable(BSP_BC_USART_CLK, true); /* Initialize USART */ USART_InitAsync(BSP_BC_USART, &usartInit); /* Enable correct USART location. */ BSP_BC_USART->ROUTE = USART_ROUTE_RXPEN | USART_ROUTE_TXPEN | BSP_BC_USART_LOCATION; } return BSP_STATUS_OK; }
void uart_init(void) { /* To avoid false start, configure output US1_TX as high on PD7 */ GPIO->P[3].DOUT |= (1 << 7); /* Pin PD7 is configured to Push-pull */ GPIO->P[3].MODEL = (GPIO->P[3].MODEL & ~_GPIO_P_MODEL_MODE7_MASK) | GPIO_P_MODEL_MODE7_PUSHPULL; /* Enable clock for USART1 */ CMU_ClockEnable(cmuClock_USART1, true); USART_InitAsync_TypeDef init = USART_INITASYNC_DEFAULT; init.baudrate = 2000000; init.oversampling = usartOVS4; init.databits = usartDatabits8; init.parity = usartNoParity; init.stopbits = usartStopbits1; init.mvdis = 0; init.prsRxEnable = 0; USART_InitAsync(USART1, &init); /* Module USART1 is configured to location 2 */ USART1->ROUTE = (USART1->ROUTE & ~_USART_ROUTE_LOCATION_MASK) | USART_ROUTE_LOCATION_LOC2; /* Enable signal TX */ USART1->ROUTE |= USART_ROUTE_TXPEN; }
/***************************************************************************//** * @brief * Init USART for asynchronous IrDA mode. * * @details * This function will configure basic settings in order to operate in * asynchronous IrDA mode. * * Special control setup not covered by this function must be done after * using this function by direct modification of the CTRL and IRCTRL * registers. * * Notice that pins used by the USART/UART module must be properly configured * by the user explicitly, in order for the USART/UART to work as intended. * (When configuring pins, one should remember to consider the sequence of * configuration, in order to avoid unintended pulses/glitches on output * pins.) * * @param[in] usart * Pointer to USART peripheral register block. * * @param[in] init * Pointer to initialization structure used to configure async IrDA setup. * * @note * Not all USART instances support IrDA. See the datasheet for your device. * ******************************************************************************/ void USARTn_InitIrDA(USART_TypeDef *usart, const USART_InitIrDA_TypeDef *init) { EFM_ASSERT(USART_IRDA_VALID(usart)); /* Init USART as async device */ USART_InitAsync(usart, &(init->async)); /* Set IrDA modulation to RZI (return-to-zero-inverted) */ usart->CTRL |= USART_CTRL_TXINV; /* Invert Rx signal before demodulator if enabled */ if (init->irRxInv) { usart->CTRL |= USART_CTRL_RXINV; } /* Configure IrDA */ usart->IRCTRL |= (uint32_t)init->irPw | (uint32_t)init->irPrsSel | ((uint32_t)init->irFilt << _USART_IRCTRL_IRFILT_SHIFT) | ((uint32_t)init->irPrsEn << _USART_IRCTRL_IRPRSEN_SHIFT); /* Enable IrDA */ usart->IRCTRL |= USART_IRCTRL_IREN; }
static int uart_gecko_init(struct device *dev) { const struct uart_gecko_config *config = dev->config->config_info; USART_InitAsync_TypeDef usartInit = USART_INITASYNC_DEFAULT; /* The peripheral and gpio clock are already enabled from soc and gpio * driver */ usartInit.baudrate = config->baud_rate; /* Enable USART clock */ CMU_ClockEnable(config->clock, true); /* Init USART */ USART_InitAsync(config->base, &usartInit); /* Initialize USART pins */ uart_gecko_init_pins(dev); #ifdef CONFIG_UART_INTERRUPT_DRIVEN config->irq_config_func(dev); #endif return 0; }
/***************************************************************************//** * @brief * Init USART0 for asynchronous IrDA mode. * * @details * This function will configure basic settings in order to operate in * asynchronous IrDA mode. * * Special control setup not covered by this function must be done after * using this function by direct modification of the CTRL and IRCTRL * registers. * * Notice that pins used by the USART/UART module must be properly configured * by the user explicitly, in order for the USART/UART to work as intended. * (When configuring pins, one should remember to consider the sequence of * configuration, in order to avoid unintended pulses/glitches on output * pins.) * * @param[in] init * Pointer to initialization structure used to configure async IrDA setup. * * @note * This function only applies to USART0 as IrDA is not supported on the other * USART modules. * ******************************************************************************/ void USART_InitIrDA(const USART_InitIrDA_TypeDef *init) { #if (USART_COUNT == 1) && defined(USART1) USART_TypeDef *usart = USART1; #else USART_TypeDef *usart = USART0; #endif /* Init USART as async device */ USART_InitAsync(usart, &(init->async)); /* Set IrDA modulation to RZI (return-to-zero-inverted) */ usart->CTRL |= USART_CTRL_TXINV; /* Invert Rx signal before demodulator if enabled */ if (init->irRxInv) { usart->CTRL |= USART_CTRL_RXINV; } /* Configure IrDA */ usart->IRCTRL |= (uint32_t) init->irPw | (uint32_t) init->irPrsSel | ((uint32_t) init->irFilt << _USART_IRCTRL_IRFILT_SHIFT) | ((uint32_t) init->irPrsEn << _USART_IRCTRL_IRPRSEN_SHIFT); /* Enable IrDA */ usart->IRCTRL |= USART_IRCTRL_IREN; }
/** * Initialize the UART. * */ void usart2_init(unsigned long baudrate) { USART_InitAsync_TypeDef init = USART_INITASYNC_DEFAULT; /* Configure controller */ // Enable clocks CMU_ClockEnable(cmuClock_HFPER, true); CMU_ClockEnable(cmuClock_USART2, true); init.enable = usartDisable; init.baudrate = baudrate; USART_InitAsync(USART2, &init); /* Enable pins at USART2 location */ USART2->ROUTE = USART_ROUTE_RXPEN | USART_ROUTE_TXPEN | (USART2_LOCATION << _USART_ROUTE_LOCATION_SHIFT); /* Clear previous RX interrupts */ USART_IntClear(USART2, USART_IF_RXDATAV); NVIC_ClearPendingIRQ(USART2_RX_IRQn); /* Enable RX interrupts */ USART_IntEnable(USART2, USART_IF_RXDATAV); NVIC_EnableIRQ(USART2_RX_IRQn); /* Finally enable it */ USART_Enable(USART2, usartEnable); }
/***************************************************************************//** * @brief * Init USART0 for asynchronous IrDA mode. * * @details * This function will configure basic settings in order to operate in * asynchronous IrDA mode. Consider using USART_Reset() prior to this function * if state of configuration is not known, since only configuration settings * specified by @p init are set. * * Special control setup not covered by this function may be done either * before or after using this function (but normally before enabling) * by direct modification of the CTRL register. * * Notice that pins used by the USART/UART module must be properly configured * by the user explicitly, in order for the USART/UART to work as intended. * (When configuring pins, one should remember to consider the sequence of * configuration, in order to avoid unintended pulses/glitches on output * pins.) * * @param[in] init * Pointer to initialization structure used to configure async IrDA setup. * * @note * This function only applies to USART0 as IrDA is not supported on the other * USART modules. * ******************************************************************************/ void USART_InitIrDA(USART_InitIrDA_TypeDef *init) { /* Init USART0 as async device */ USART_InitAsync(USART0, &(init->async)); /* Set IrDA modulation to RZI (return-to-zero-inverted) */ USART0->CTRL |= USART_CTRL_TXINV; /* Invert Rx signal before demodulator if enabled */ if (init->irRxInv) { USART0->CTRL |= USART_CTRL_RXINV; } else { USART0->CTRL &= ~(USART_CTRL_RXINV); } /* Configure IrDA */ USART0->IRCTRL |= (uint32_t)init->irPw | (uint32_t)init->irPrsSel | ((uint32_t)init->irFilt << _USART_IRCTRL_IRFILT_SHIFT) | ((uint32_t)init->irPrsEn << _USART_IRCTRL_IRPRSEN_SHIFT); /* Enable IrDA */ USART0->IRCTRL |= USART_IRCTRL_IREN; }
//================================================================================ // UART0_enter_DefaultMode_from_RESET //================================================================================ extern void UART0_enter_DefaultMode_from_RESET(void) { // $[UART_InitAsync] USART_InitAsync_TypeDef initasync = USART_INITASYNC_DEFAULT; initasync.baudrate = 115200; initasync.databits = usartDatabits8; initasync.parity = usartNoParity; initasync.stopbits = usartStopbits1; initasync.oversampling = usartOVS16; #if defined( USART_INPUT_RXPRS ) && defined( USART_CTRL_MVDIS ) initasync.mvdis = 0; initasync.prsRxEnable = 0; initasync.prsRxCh = 0; #endif USART_InitAsync(UART0, &initasync); // [UART_InitAsync]$ // $[USART_InitPrsTrigger] USART_PrsTriggerInit_TypeDef initprs = USART_INITPRSTRIGGER_DEFAULT; initprs.rxTriggerEnable = 0; initprs.txTriggerEnable = 0; initprs.prsTriggerChannel = usartPrsTriggerCh0; USART_InitPrsTrigger(UART0, &initprs); // [USART_InitPrsTrigger]$ }
/**************************************************************************//** * @brief Intializes STK USART1 for UART mode towards Board Controller *****************************************************************************/ static void STK_USARTInit(void) { /* Initialize USART */ USART_InitAsync(usart, &usartInit); /* Enable location 2 - PD6 + PD7 */ usart->ROUTE = USART_ROUTE_RXPEN | USART_ROUTE_TXPEN | USART_ROUTE_LOCATION_LOC2; }
void USART1_setup(void) { USART_InitAsync_TypeDef init = USART_INITASYNC_DEFAULT; init.baudrate = 115200; init.oversampling = usartOVS16; init.databits = usartDatabits8; init.parity = usartNoParity; init.stopbits = usartStopbits1; init.mvdis = 0; init.prsRxEnable = 0; USART_InitAsync(USART1, &init); }
/****************************************************************************** * Enables UART for the bootloader. *****************************************************************************/ void BLUART_init(void) { /* Enable the required clocks */ CMU_ClockEnable(UART_CLOCK, true); CMU_ClockEnable(cmuClock_GPIO, true); /* Configure GPIO */ GPIO_PinModeSet(TXPORT, TXPIN, gpioModePushPull, 1); GPIO_PinModeSet(RXPORT, RXPIN, gpioModeInput, 0); /* Configure USART peripheral. Default configuration * is fine. We only need to set the baud rate. */ USART_InitAsync_TypeDef uartInit = USART_INITASYNC_DEFAULT; uartInit.baudrate = BOOTLOADER_BAUDRATE; USART_InitAsync(UART, &uartInit); /* Enable RX and TX and set location */ UART->ROUTE = USART_ROUTE_TXPEN | USART_ROUTE_RXPEN | UART_LOC; }
/**************************************************************************//** * @brief Intializes UART/LEUART *****************************************************************************/ void UART1_SerialInit(void) { /* Configure GPIO pins */ CMU_ClockEnable(cmuClock_GPIO, true); /* To avoid false start, configure output as high */ GPIO_PinModeSet(gpioPortB, 9, gpioModePushPull, 1); GPIO_PinModeSet(gpioPortB, 10, gpioModeInput, 0); USART_TypeDef *usart = UART1; USART_InitAsync_TypeDef init = USART_INITASYNC_DEFAULT; /* Enable EFM32GG_DK3750 RS232/UART switch */ BSP_PeripheralAccess(BSP_RS232_UART, true); /* Enable peripheral clocks */ CMU_ClockEnable(cmuClock_HFPER, true); CMU_ClockEnable(cmuClock_UART1, true); /* Configure USART for basic async operation */ init.enable = usartDisable; USART_InitAsync(usart, &init); /* Enable pins at UART1 location #2 */ usart->ROUTE = USART_ROUTE_RXPEN | USART_ROUTE_TXPEN | USART_ROUTE_LOCATION_LOC2; /* Clear previous RX interrupts */ USART_IntClear(UART1, USART_IF_RXDATAV); NVIC_ClearPendingIRQ(UART1_RX_IRQn); /* Enable RX interrupts */ USART_IntEnable(UART1, USART_IF_RXDATAV); NVIC_EnableIRQ(UART1_RX_IRQn); /* Finally enable it */ USART_Enable(usart, usartEnable); #if !defined(__CROSSWORKS_ARM) && defined(__GNUC__) setvbuf(stdout, NULL, _IONBF, 0); /*Set unbuffered mode for stdout (newlib)*/ #endif initialized = true; }
/****************************************************************************** * @brief usartSetup function * ******************************************************************************/ void usartSetup(void) { cmuSetup(); /* Configure GPIO pin as open drain */ GPIO_PinModeSet(SC_GPIO_DATA_PORT, SC_GPIO_DATA_PIN, gpioModeWiredAndPullUp, 1); /* Prepare struct for initializing USART in asynchronous mode*/ usartInit.enable = usartDisable; /* Don't enable USART upon intialization */ usartInit.refFreq = 0; /* Provide information on reference frequency. When set to 0, the reference frequency is */ usartInit.baudrate = SC_BAUD_RATE; /* Baud rate */ usartInit.oversampling = usartOVS16; /* Oversampling. Range is 4x, 6x, 8x or 16x */ usartInit.databits = usartDatabits8; /* Number of data bits. Range is 4 to 10 */ usartInit.parity = usartEvenParity; /* Parity mode */ usartInit.stopbits = usartStopbits1p5; /* Number of stop bits. Range is 0 to 2, 1.5 for smartcard. */ usartInit.mvdis = false; /* Disable majority voting */ usartInit.prsRxEnable = false; /* Enable USART Rx via Peripheral Reflex System */ usartInit.prsRxCh = usartPrsRxCh0; /* Select PRS channel if enabled */ /* Initialize USART with usartInit struct */ USART_InitAsync(usart, &usartInit); /* Smart card specific settings for T0 mode. */ usart->CTRL |= USART_CTRL_SCMODE | USART_CTRL_AUTOTRI | USART_CTRL_LOOPBK; /* Prepare USART Rx interrupt */ USART_IntClear(usart, _USART_IF_MASK); USART_IntEnable(usart, USART_IF_RXDATAV); NVIC_ClearPendingIRQ(USART1_RX_IRQn); NVIC_EnableIRQ(USART1_RX_IRQn); /* Enable I/O pins at USART1 location #2 */ usart->ROUTE = USART_ROUTE_TXPEN | SC_USART_LOCATION; /* Disable reception before enabling uart to discard erroneus stuff while card is unpowered. */ usartFlushBuffer(); usartAcceptRX(false); /* Enable USART */ USART_Enable(usart, usartEnable); }
/**************************************************************************//** * @brief Initialize board controller communication support (BCC) * functionality. * * @return @ref BSP_STATUS_OK. *****************************************************************************/ int BSP_BccInit( void ) { #if defined( BSP_BCC_LEUART ) LEUART_Init_TypeDef leuartInit = LEUART_INIT_DEFAULT; #else USART_InitAsync_TypeDef usartInit = USART_INITASYNC_DEFAULT; #endif rxByteCount = 0; txByteCount = 0; /* Enable High Frequency Peripherals */ CMU_ClockEnable(cmuClock_HFPER, true); /* Enable clocks to GPIO */ CMU_ClockEnable(cmuClock_GPIO, true); /* Enable UART clock */ CMU_ClockEnable( BSP_BCC_CLK, true ); #if defined( BSP_BCC_LEUART ) /* Enable CORE LE clock in order to access LE modules */ CMU_ClockEnable(cmuClock_CORELE, true); /* Select CORE LE clock for LE modules */ CMU_ClockSelectSet( cmuClock_LFB, cmuSelect_CORELEDIV2 ); /* Initialize LEUART */ leuartInit.baudrate = 115200; LEUART_Init( BSP_BCC_LEUART, &leuartInit ); #else /* Initialize USART */ USART_InitAsync( BSP_BCC_USART, &usartInit ); #endif /* Initialize UART pins */ BSP_BccPinsEnable( true ); return BSP_STATUS_OK; }
/****************************************************************************** * @brief uartSetup function * ******************************************************************************/ void uartSetup(void) { /* Enable clock for GPIO module (required for pin configuration) */ CMU_ClockEnable(cmuClock_GPIO, true); /* Configure GPIO pins */ GPIO_PinModeSet(gpioPortB, 9, gpioModePushPull, 1); GPIO_PinModeSet(gpioPortB, 10, gpioModeInput, 0); /* Prepare struct for initializing UART in asynchronous mode*/ uartInit.enable = usartDisable; /* Don't enable UART upon intialization */ uartInit.refFreq = 0; /* Provide information on reference frequency. When set to 0, the reference frequency is */ uartInit.baudrate = 115200; /* Baud rate */ uartInit.oversampling = usartOVS16; /* Oversampling. Range is 4x, 6x, 8x or 16x */ uartInit.databits = usartDatabits8; /* Number of data bits. Range is 4 to 10 */ uartInit.parity = usartNoParity; /* Parity mode */ uartInit.stopbits = usartStopbits1; /* Number of stop bits. Range is 0 to 2 */ uartInit.mvdis = false; /* Disable majority voting */ uartInit.prsRxEnable = false; /* Enable USART Rx via Peripheral Reflex System */ uartInit.prsRxCh = usartPrsRxCh0; /* Select PRS channel if enabled */ /* Initialize USART with uartInit struct */ USART_InitAsync(uart, &uartInit); /* Prepare UART Rx and Tx interrupts */ USART_IntClear(uart, _UART_IF_MASK); USART_IntEnable(uart, UART_IF_RXDATAV); NVIC_ClearPendingIRQ(UART1_RX_IRQn); NVIC_ClearPendingIRQ(UART1_TX_IRQn); NVIC_EnableIRQ(UART1_RX_IRQn); NVIC_EnableIRQ(UART1_TX_IRQn); /* Enable I/O pins at UART1 location #2 */ uart->ROUTE = UART_ROUTE_RXPEN | UART_ROUTE_TXPEN | UART_ROUTE_LOCATION_LOC2; /* Enable UART */ USART_Enable(uart, usartEnable); }
void DebugInterface::initializeDebugUart() { #if SentioEM_Emulator_Interface == OFF && SentioEM_Debug_Interface == ON // Configuration of the GPIO-Pins which belong to the UART/USART configured. Furthermore the RX-Interrupt //Service Routine of the UART is enabled. // In a first switch-case statement the UART/USART Module is selected. Then the RX/TX Pins which have // to be activated are selected // Select module UART0, valid Location on the EFM32G280 are (0,1,2,3) #if _DEBUG_USART_ < 8 //Enable the required periphery clock CMU_ClockEnable(cmuClock_UART0,true); // Enable the RX-Interrupt Service Routine NVIC_EnableIRQ( UART0_RX_IRQn ); #define DEBUG_USART UART0 // Select the IO-Pins dependent on Module-Location #if _DEBUG_USART_ == _UART0_LOC0_ #define port gpioPortF #define pinRX 7 #define pinTX 6 #define DEBUG_LOCATION USART_ROUTE_LOCATION_LOC0 #elif _DEBUG_USART_ == _UART0_LOC1_ #define port gpioPortE #define pinRX 1 #define pinTX 0 #define DEBUG_LOCATION USART_ROUTE_LOCATION_LOC1 #elif _DEBUG_USART_ == _UART0_LOC2_ #define port gpioPortA #define pinRX 4 #define pinTX 3 #define DEBUG_LOCATION USART_ROUTE_LOCATION_LOC2 #elif _DEBUG_USART_ == _UART0_LOC3_ #define port gpioPortC #define pinRX 15 #define pinTX 14 #define DEBUG_LOCATION USART_ROUTE_LOCATION_LOC3 #endif // Select module USART0, valid Location on the EFM32G280 are (0,1,2) #elif ( _DEBUG_USART_ > 9 ) && ( _DEBUG_USART_ < 19 ) //Enable the required periphery clock CMU_ClockEnable(cmuClock_USART0,true); // Enable the RX-Interrupt Service Routine NVIC_EnableIRQ( USART0_RX_IRQn ); #define _DEBUG_USART_ USART0 // Select the IO-Pins dependent on Module-Location #if _DEBUG_USART_ == _USART0_LOC0_ #define port gpioPortE #define pinRX 11 #define pinTX 10 #define DEBUG_LOCATION USART_ROUTE_LOCATION_LOC0 #elif _DEBUG_USART_ == _USART0_LOC1_ #define port gpioPortE #define pinRX 6 #define pinTX 7 #define DEBUG_LOCATION USART_ROUTE_LOCATION_LOC1 #elif _DEBUG_USART_ == _USART0_LOC2_ #define port gpioPortC #define pinRX 10 #define pinTX 11 #define DEBUG_LOCATION USART_ROUTE_LOCATION_LOC2 #endif // Select module USART1, valid Location on the EFM32G280 are (0,1) #elif( _DEBUG_USART_ > 19 ) && ( _DEBUG_USART_ < 29 ) //Enable the required periphery clock CMU_ClockEnable(cmuClock_USART1,true); // Enable the RX-Interrupt Service Routine NVIC_EnableIRQ( USART1_RX_IRQn ); #define DEBUG_USART USART1 // Select the IO-Pins dependent on Module-Location #if _DEBUG_USART_ == _USART1_LOC0_ #define port gpioPortC #define pinRX 1 #define pinTX 0 #define DEBUG_LOCATION USART_ROUTE_LOCATION_LOC0 #elif _DEBUG_USART_ == _USART1_LOC1_ #define port gpioPortD #define pinRX 1 #define pinTX 0 #define DEBUG_LOCATION USART_ROUTE_LOCATION_LOC1 #endif // Select module USART2, valid Location on the EFM32G280 are (0,1) #elif( _DEBUG_USART_ > 29 ) && ( _DEBUG_USART_ < 39 ) //Enable the required periphery clock CMU_ClockEnable(cmuClock_USART1,true); // Enable the RX-Interrupt Service Routine NVIC_EnableIRQ( USART2_RX_IRQn ); #define XBEE_USART USART2 // Select the IO-Pins dependent on Module-Location #if _DEBUG_USART_ == _USART2_LOC0_ #define port gpioPortC #define pinRX 3 #define pinTX 2 #define DEBUG_LOCATION USART_ROUTE_LOCATION_LOC0 #elif _DEBUG_USART_ == _USART2_LOC1_ #define port gpioPortB #define pinRX 4 #define pinTX 3 #define DEBUG_LOCATION USART_ROUTE_LOCATION_LOC1 #endif #endif USART_InitAsync( DEBUG_USART, &initDebugUart ); // The Routing Register of the selected UART/USART Register is configured. The following register-access // enables the UART modules RX/TX shift-register and furthermore selects the one of the possible Locations of the modules IO-Pins // // NOTE!!! // Beside setting a modules Routing Register the functionality of the GPIO-Pins IO-Port-driver has to be configured separately DEBUG_USART->ROUTE = USART_ROUTE_RXPEN| USART_ROUTE_TXPEN| DEBUG_LOCATION; // Configure the IO-Port driver of the EFM32-GPIO Pins which were selected before GPIO_PinModeSet( port, pinRX, gpioModeInputPullFilter, 1 ); // RX needs to be a Input GPIO_PinModeSet( port, pinTX, gpioModePushPull, 0 ); // TX needs to be a Output // Configure the Interrupt DEBUG_USART->IFC = ~0; // Clear pending Interrupts DEBUG_USART->IEN = USART_IEN_RXDATAV; // Enable the RX-Interrupt when One-Byte is in the buffer, // Flag is cleared when the RX buffer is read-out #endif }
/***************************************************************************//** * @brief * Initialize the specified USART unit * * @details * * @note * * @param[in] device * Pointer to device descriptor * * @param[in] unitNumber * Unit number * * @param[in] location * Pin location number * * @param[in] flag * Configuration flag * * @param[in] dmaChannel * DMA channel number for TX * * @param[in] console * Indicate if using as console * * @return * Pointer to USART device ******************************************************************************/ static struct efm32_usart_device_t *rt_hw_usart_unit_init( rt_device_t device, rt_uint8_t unitNumber, rt_uint8_t location, rt_uint32_t flag, rt_uint32_t dmaChannel, rt_uint8_t config) { struct efm32_usart_device_t *usart; struct efm32_usart_dma_mode_t *dma_mode; DMA_CB_TypeDef *callback; CMU_Clock_TypeDef usartClock; rt_uint32_t txDmaSelect; GPIO_Port_TypeDef port_tx, port_rx, port_clk, port_cs; rt_uint32_t pin_tx, pin_rx, pin_clk, pin_cs; efm32_irq_hook_init_t hook; do { /* Allocate device */ usart = rt_malloc(sizeof(struct efm32_usart_device_t)); if (usart == RT_NULL) { usart_debug("USART%d err: no mem\n", usart->unit); break; } usart->counter = 0; usart->unit = unitNumber; usart->state = config; usart->tx_mode = RT_NULL; usart->rx_mode = RT_NULL; /* Allocate TX */ dma_mode = RT_NULL; if (flag & RT_DEVICE_FLAG_DMA_TX) { usart->tx_mode = dma_mode = rt_malloc(sizeof(struct efm32_usart_dma_mode_t)); if (dma_mode == RT_NULL) { usart_debug("USART%d err: no mem for DMA TX\n", usart->unit); break; } dma_mode->dma_channel = dmaChannel; } /* Allocate RX */ if (flag & RT_DEVICE_FLAG_INT_RX) { usart->rx_mode = rt_malloc(sizeof(struct efm32_usart_int_mode_t)); if (usart->rx_mode == RT_NULL) { usart_debug("USART%d err: no mem for INT RX\n", usart->unit); break; } } /* Initialization */ #if defined(UART_PRESENT) if ((!(config & USART_STATE_ASYNC_ONLY) && (unitNumber >= USART_COUNT)) || \ ((config & USART_STATE_ASYNC_ONLY) && (unitNumber >= UART_COUNT))) #else if (unitNumber >= USART_COUNT) #endif { break; } switch (unitNumber) { case 0: #if defined(UART_PRESENT) if (config & USART_STATE_ASYNC_ONLY) { usart->usart_device = UART0; usartClock = (CMU_Clock_TypeDef)cmuClock_UART0; txDmaSelect = DMAREQ_UART0_TXBL; port_tx = AF_UART0_TX_PORT(location); pin_tx = AF_UART0_TX_PIN(location); port_rx = AF_UART0_RX_PORT(location); pin_rx = AF_UART0_RX_PIN(location); } else #endif { usart->usart_device = USART0; usartClock = (CMU_Clock_TypeDef)cmuClock_USART0; txDmaSelect = DMAREQ_USART0_TXBL; port_tx = AF_USART0_TX_PORT(location); pin_tx = AF_USART0_TX_PIN(location); port_rx = AF_USART0_RX_PORT(location); pin_rx = AF_USART0_RX_PIN(location); port_clk = AF_USART0_CLK_PORT(location); pin_clk = AF_USART0_CLK_PIN(location); port_cs = AF_USART0_CS_PORT(location); pin_cs = AF_USART0_CS_PIN(location); } break; #if ((defined(USART_PRESENT) && (USART_COUNT > 1)) || \ (defined(UART_PRESENT) && (UART_COUNT > 1))) case 1: #if (defined(UART_PRESENT) && (UART_COUNT > 1)) if (config & USART_STATE_ASYNC_ONLY) { usart->usart_device = UART1; usartClock = (CMU_Clock_TypeDef)cmuClock_UART1; txDmaSelect = DMAREQ_UART1_TXBL; port_tx = AF_UART1_TX_PORT(location); pin_tx = AF_UART1_TX_PIN(location); port_rx = AF_UART1_RX_PORT(location); pin_rx = AF_UART1_RX_PIN(location); } else #endif { usart->usart_device = USART1; usartClock = (CMU_Clock_TypeDef)cmuClock_USART1; txDmaSelect = DMAREQ_USART1_TXBL; port_tx = AF_USART1_TX_PORT(location); pin_tx = AF_USART1_TX_PIN(location); port_rx = AF_USART1_RX_PORT(location); pin_rx = AF_USART1_RX_PIN(location); port_clk = AF_USART1_CLK_PORT(location); pin_clk = AF_USART1_CLK_PIN(location); port_cs = AF_USART1_CS_PORT(location); pin_cs = AF_USART1_CS_PIN(location); } break; #endif #if ((defined(USART_PRESENT) && (USART_COUNT > 2)) || \ (defined(UART_PRESENT) && (UART_COUNT > 2))) case 2: #if (defined(UART_PRESENT) && (UART_COUNT > 2)) if (config & USART_STATE_ASYNC_ONLY) { usart->usart_device = UART2; usartClock = (CMU_Clock_TypeDef)cmuClock_UART2; txDmaSelect = DMAREQ_UART2_TXBL; port_tx = AF_UART2_TX_PORT(location); pin_tx = AF_UART2_TX_PIN(location); port_rx = AF_UART2_RX_PORT(location); pin_rx = AF_UART2_RX_PIN(location); } else #endif { usart->usart_device = USART2; usartClock = (CMU_Clock_TypeDef)cmuClock_USART2; txDmaSelect = DMAREQ_USART2_TXBL; port_tx = AF_USART2_TX_PORT(location); pin_tx = AF_USART2_TX_PIN(location); port_rx = AF_USART2_RX_PORT(location); pin_rx = AF_USART2_RX_PIN(location); port_clk = AF_USART2_CLK_PORT(location); pin_clk = AF_USART2_CLK_PIN(location); port_cs = AF_USART2_CS_PORT(location); pin_cs = AF_USART2_CS_PIN(location); } break; #endif default: break; } /* Enable USART clock */ CMU_ClockEnable(usartClock, true); /* Config GPIO */ GPIO_PinModeSet( port_tx, pin_tx, gpioModePushPull, 0); GPIO_PinModeSet( port_rx, pin_rx, gpioModeInputPull, 1); if (config & USART_STATE_SYNC) { GPIO_PinModeSet( port_clk, pin_clk, gpioModePushPull, 0); } if (config & USART_STATE_AUTOCS) { GPIO_PinModeSet( port_cs, pin_cs, gpioModePushPull, 1); } /* Config interrupt and NVIC */ if (flag & RT_DEVICE_FLAG_INT_RX) { hook.type = efm32_irq_type_usart; hook.unit = unitNumber * 2 + 1; #if defined(UART_PRESENT) if (config & USART_STATE_ASYNC_ONLY) { hook.unit += USART_COUNT * 2; } #endif hook.cbFunc = rt_hw_usart_rx_isr; hook.userPtr = device; efm32_irq_hook_register(&hook); } /* Config DMA */ if (flag & RT_DEVICE_FLAG_DMA_TX) { DMA_CfgChannel_TypeDef chnlCfg; DMA_CfgDescr_TypeDef descrCfg; hook.type = efm32_irq_type_dma; hook.unit = dmaChannel; hook.cbFunc = rt_hw_usart_dma_tx_isr; hook.userPtr = device; efm32_irq_hook_register(&hook); callback = (DMA_CB_TypeDef *)rt_malloc(sizeof(DMA_CB_TypeDef)); if (callback == RT_NULL) { usart_debug("USART%d err: no mem for callback\n", usart->unit); break; } callback->cbFunc = DMA_IRQHandler_All; callback->userPtr = RT_NULL; callback->primary = 0; /* Setting up DMA channel */ chnlCfg.highPri = false; /* Can't use with peripherals */ chnlCfg.enableInt = true; /* Interrupt for callback function */ chnlCfg.select = txDmaSelect; chnlCfg.cb = callback; DMA_CfgChannel(dmaChannel, &chnlCfg); /* Setting up DMA channel descriptor */ descrCfg.dstInc = dmaDataIncNone; descrCfg.srcInc = dmaDataInc1; descrCfg.size = dmaDataSize1; descrCfg.arbRate = dmaArbitrate1; descrCfg.hprot = 0; DMA_CfgDescr(dmaChannel, true, &descrCfg); } /* Init specified USART unit */ if (config & USART_STATE_SYNC) { USART_InitSync_TypeDef init_sync = USART_INITSYNC_DEFAULT; init_sync.enable = usartEnable; init_sync.refFreq = 0; init_sync.baudrate = SPI_BAUDRATE; if (config & USART_STATE_9BIT) { init_sync.databits = usartDatabits9; } else { init_sync.databits = usartDatabits8; } if (config & USART_STATE_MASTER) { init_sync.master = true; } else { init_sync.master = false; } init_sync.msbf = true; switch (USART_CLK_MODE_GET(config)) { case 0: init_sync.clockMode = usartClockMode0; break; case 1: init_sync.clockMode = usartClockMode1; break; case 2: init_sync.clockMode = usartClockMode2; break; case 3: init_sync.clockMode = usartClockMode3; break; } USART_InitSync(usart->usart_device, &init_sync); } else { USART_InitAsync_TypeDef init_async = USART_INITASYNC_DEFAULT; init_async.enable = usartEnable; init_async.refFreq = 0; init_async.baudrate = UART_BAUDRATE; init_async.oversampling = USART_CTRL_OVS_X4; init_async.databits = USART_FRAME_DATABITS_EIGHT; init_async.parity = USART_FRAME_PARITY_NONE; init_async.stopbits = USART_FRAME_STOPBITS_ONE; USART_InitAsync(usart->usart_device, &init_async); } /* Enable RX and TX pins and set location */ usart->usart_device->ROUTE = USART_ROUTE_RXPEN | USART_ROUTE_TXPEN | \ (location << _USART_ROUTE_LOCATION_SHIFT); if (config & USART_STATE_SYNC) { usart->usart_device->ROUTE |= USART_ROUTE_CLKPEN; } if (config & USART_STATE_AUTOCS) { usart->usart_device->ROUTE |= USART_ROUTE_CSPEN; if (config & USART_STATE_MASTER) { usart->usart_device->CTRL |= USART_CTRL_AUTOCS; } } /* Clear RX/TX buffers */ usart->usart_device->CMD = USART_CMD_CLEARRX | USART_CMD_CLEARTX; return usart; } while(0); if (usart->rx_mode) { rt_free(usart->rx_mode); } if (usart->tx_mode) { rt_free(usart->tx_mode); } if (usart) { rt_free(usart); } if (callback) { rt_free(callback); } #if defined(UART_PRESENT) if (config & USART_STATE_ASYNC_ONLY) { usart_debug("UART%d err: init failed!\n", unitNumber); } else #endif { usart_debug("USART%d err: init failed!\n", unitNumber); } return RT_NULL; }
/**************************************************************************//** * @brief UART/LEUART IRQ Handler *****************************************************************************/ void RETARGET_IRQ_NAME(void) { #if defined(RETARGET_USART) if (RETARGET_UART->STATUS & USART_STATUS_RXDATAV) { #else if (RETARGET_UART->IF & LEUART_IF_RXDATAV) { #endif /* Store Data */ rxBuffer[rxWriteIndex] = RETARGET_RX(RETARGET_UART); rxWriteIndex++; rxCount++; if (rxWriteIndex == RXBUFSIZE) { rxWriteIndex = 0; } /* Check for overflow - flush buffer */ if (rxCount > RXBUFSIZE) { rxWriteIndex = 0; rxCount = 0; rxReadIndex = 0; } } } /** @} (end group RetargetIo) */ /**************************************************************************//** * @brief UART/LEUART toggle LF to CRLF conversion * @param on If non-zero, automatic LF to CRLF conversion will be enabled *****************************************************************************/ void RETARGET_SerialCrLf(int on) { if (on) LFtoCRLF = 1; else LFtoCRLF = 0; } /**************************************************************************//** * @brief Intializes UART/LEUART *****************************************************************************/ void RETARGET_SerialInit(void) { /* Enable peripheral clocks */ CMU_ClockEnable(cmuClock_HFPER, true); /* Configure GPIO pins */ CMU_ClockEnable(cmuClock_GPIO, true); /* To avoid false start, configure output as high */ GPIO_PinModeSet(RETARGET_TXPORT, RETARGET_TXPIN, gpioModePushPull, 1); GPIO_PinModeSet(RETARGET_RXPORT, RETARGET_RXPIN, gpioModeInput, 0); #if defined(RETARGET_USART) USART_TypeDef *usart = RETARGET_UART; USART_InitAsync_TypeDef init = USART_INITASYNC_DEFAULT; /* Enable DK RS232/UART switch */ RETARGET_PERIPHERAL_ENABLE(); CMU_ClockEnable(RETARGET_CLK, true); /* Configure USART for basic async operation */ init.enable = usartDisable; USART_InitAsync(usart, &init); /* Enable pins at correct UART/USART location. */ #if defined( USART_ROUTEPEN_RXPEN ) usart->ROUTEPEN = USART_ROUTEPEN_RXPEN | USART_ROUTEPEN_TXPEN; usart->ROUTELOC0 = ( usart->ROUTELOC0 & ~( _USART_ROUTELOC0_TXLOC_MASK | _USART_ROUTELOC0_RXLOC_MASK ) ) | ( RETARGET_TX_LOCATION << _USART_ROUTELOC0_TXLOC_SHIFT ) | ( RETARGET_RX_LOCATION << _USART_ROUTELOC0_RXLOC_SHIFT ); #else usart->ROUTE = USART_ROUTE_RXPEN | USART_ROUTE_TXPEN | RETARGET_LOCATION; #endif /* Clear previous RX interrupts */ USART_IntClear(RETARGET_UART, USART_IF_RXDATAV); NVIC_ClearPendingIRQ(RETARGET_IRQn); /* Enable RX interrupts */ USART_IntEnable(RETARGET_UART, USART_IF_RXDATAV); NVIC_EnableIRQ(RETARGET_IRQn); /* Finally enable it */ USART_Enable(usart, usartEnable); #else LEUART_TypeDef *leuart = RETARGET_UART; LEUART_Init_TypeDef init = LEUART_INIT_DEFAULT; /* Enable DK LEUART/RS232 switch */ RETARGET_PERIPHERAL_ENABLE(); /* Enable CORE LE clock in order to access LE modules */ CMU_ClockEnable(cmuClock_CORELE, true); #if defined(RETARGET_VCOM) /* Select HFXO/2 for LEUARTs (and wait for it to stabilize) */ CMU_ClockSelectSet(cmuClock_LFB, cmuSelect_CORELEDIV2); #else /* Select LFXO for LEUARTs (and wait for it to stabilize) */ CMU_ClockSelectSet(cmuClock_LFB, cmuSelect_LFXO); #endif CMU_ClockEnable(RETARGET_CLK, true); /* Do not prescale clock */ CMU_ClockDivSet(RETARGET_CLK, cmuClkDiv_1); /* Configure LEUART */ init.enable = leuartDisable; #if defined(RETARGET_VCOM) init.baudrate = 115200; #endif LEUART_Init(leuart, &init); /* Enable pins at default location */ leuart->ROUTE = LEUART_ROUTE_RXPEN | LEUART_ROUTE_TXPEN | RETARGET_LOCATION; /* Clear previous RX interrupts */ LEUART_IntClear(RETARGET_UART, LEUART_IF_RXDATAV); NVIC_ClearPendingIRQ(RETARGET_IRQn); /* Enable RX interrupts */ LEUART_IntEnable(RETARGET_UART, LEUART_IF_RXDATAV); NVIC_EnableIRQ(RETARGET_IRQn); /* Finally enable it */ LEUART_Enable(leuart, leuartEnable); #endif #if !defined(__CROSSWORKS_ARM) && defined(__GNUC__) setvbuf(stdout, NULL, _IONBF, 0); /*Set unbuffered mode for stdout (newlib)*/ #endif initialized = true; }
int uart_init_blocking(uart_t _uart, uint32_t baudrate) { #if UART_0_EN || UART_1_EN USART_TypeDef *uart; USART_InitAsync_TypeDef uartInit = USART_INITASYNC_DEFAULT; #endif #if UART_2_EN LEUART_Init_TypeDef leuartInit; #endif switch(_uart) { #if UART_0_EN case UART_0: uart = UART_0_DEV; /* Enabling clock to USART0 */ CMU_ClockEnable(cmuClock_USART0, true); /* Output PIN */ gpio_init(GPIO_T(UART_0_PORT, UART_0_TX_PIN), GPIO_DIR_PUSH_PULL, GPIO_PULLUP); gpio_init(GPIO_T(UART_0_PORT, UART_0_RX_PIN), GPIO_DIR_INPUT, GPIO_PULLDOWN); /* Prepare struct for initializing UART in asynchronous mode*/ uartInit.enable = usartDisable; /* Don't enable UART upon intialization */ uartInit.refFreq = 0; /* Provide information on reference frequency. When set to 0, the reference frequency is */ uartInit.baudrate = baudrate; /* Baud rate */ uartInit.oversampling = usartOVS16; /* Oversampling. Range is 4x, 6x, 8x or 16x */ uartInit.databits = usartDatabits8; /* Number of data bits. Range is 4 to 10 */ uartInit.parity = usartNoParity; /* Parity mode */ uartInit.stopbits = usartStopbits1; /* Number of stop bits. Range is 0 to 2 */ uartInit.mvdis = false; /* Disable majority voting */ uartInit.prsRxEnable = false; /* Enable USART Rx via Peripheral Reflex System */ uartInit.prsRxCh = usartPrsRxCh0; /* Select PRS channel if enabled */ /* Initialize USART with uartInit struct */ USART_InitAsync(uart, &uartInit); uart->ROUTE = UART_ROUTE_RXPEN | UART_ROUTE_TXPEN | UART_0_LOC; USART_Enable(UART_0_DEV, usartEnable); break; #endif #if UART_1_EN case UART_1: uart = UART_1_DEV; /* Enabling clock to USART0 */ uart = UART_1_DEV; CMU_ClockEnable(cmuClock_USART1, true); /* Output PIN */ gpio_init(GPIO_T(UART_1_PORT, UART_1_TX_PIN), GPIO_DIR_PUSH_PULL, GPIO_PULLUP); gpio_init(GPIO_T(UART_1_PORT, UART_1_RX_PIN), GPIO_DIR_INPUT, GPIO_PULLDOWN); /* Prepare struct for initializing UART in asynchronous mode*/ uartInit.enable = usartDisable; /* Don't enable UART upon intialization */ uartInit.refFreq = 0; /* Provide information on reference frequency. When set to 0, the reference frequency is */ uartInit.baudrate = baudrate; /* Baud rate */ uartInit.oversampling = usartOVS16; /* Oversampling. Range is 4x, 6x, 8x or 16x */ uartInit.databits = usartDatabits8; /* Number of data bits. Range is 4 to 10 */ uartInit.parity = usartNoParity; /* Parity mode */ uartInit.stopbits = usartStopbits1; /* Number of stop bits. Range is 0 to 2 */ uartInit.mvdis = false; /* Disable majority voting */ uartInit.prsRxEnable = false; /* Enable USART Rx via Peripheral Reflex System */ uartInit.prsRxCh = usartPrsRxCh0; /* Select PRS channel if enabled */ /* Initialize USART with uartInit struct */ USART_InitAsync(uart, &uartInit); uart->ROUTE = UART_ROUTE_RXPEN | UART_ROUTE_TXPEN | UART_1_LOC; USART_Enable(UART_1_DEV, usartEnable); break; #endif #if UART_2_EN case UART_2: CMU_ClockEnable(cmuClock_LEUART0, true); gpio_init(GPIO_T(UART_2_PORT, UART_2_TX_PIN), GPIO_DIR_PUSH_PULL, GPIO_PULLUP); gpio_init(GPIO_T(UART_2_PORT, UART_2_RX_PIN), GPIO_DIR_INPUT, GPIO_PULLDOWN); LEUART_Reset(UART_2_DEV); leuartInit.enable = leuartEnable; leuartInit.baudrate = baudrate; leuartInit.databits = leuartDatabits8; leuartInit.parity = leuartNoParity; leuartInit.stopbits = leuartStopbits1; leuartInit.refFreq = 0; LEUART_Init(UART_2_DEV, &leuartInit); UART_2_DEV->ROUTE = LEUART_ROUTE_TXPEN | LEUART_ROUTE_RXPEN | UART_2_LOC; LEUART_Enable(UART_2_DEV, usartEnable); break; #endif default: return -1; } return 0; }