/*************************************************************************
 * Function Name: UART1_init
 * Parameters: none
 * Return: none
 * Description: UART port configuration
 *************************************************************************/
void UART1_init(void)
{
  UART_HAL_DisableTransmitter(UART1_BASE_PTR);
  UART_HAL_DisableReceiver(UART1_BASE_PTR);/* disable uart operation */
  UART_HAL_SetStopBitCount(UART1_BASE_PTR, kUartOneStopBit);
  UART_HAL_SetBaudRate(UART1_BASE_PTR, BUS_CLOCK, UART_BAUDRATE);
  //Configures the number of bits per character in the UART controller./
  UART_HAL_SetBitCountPerChar(UART1_BASE_PTR, kUart8BitsPerChar);/*!< 8-bit data characters */
  UART_HAL_SetParityMode(UART1_BASE_PTR, kUartParityDisabled);/*!< parity disabled */
  UART_HAL_SetWaitModeOperation(UART1_BASE_PTR, kUartOperates);/*!< UART continues to operate normally */
  UART_HAL_SetLoopCmd(UART1_BASE_PTR, false);/*£¡<Disable The UART loopback mode configuration */
  UART_HAL_SetReceiverSource(UART1_BASE_PTR, kUartLoopBack);/*!< Internal loop back mode. */
  UART_HAL_SetReceiverWakeupMethod(UART1_BASE_PTR, kUartIdleLineWake);/*!< The idle-line wakes UART receiver from standby */
  UART_BWR_C1_ILT(UART1_BASE_PTR, 0);/*!<Idle character bit count starts after start bit.*/

  UART_HAL_EnableTransmitter(UART1_BASE_PTR);
  UART_HAL_EnableReceiver(UART1_BASE_PTR);/* Enable uart operation */
#if defined(KV10Z7_SERIES)
  // Configure UART1 port pins: PTD0, PTD1 - ALT5
  PORT_HAL_SetMuxMode(PORTD_BASE_PTR, 0, kPortMuxAlt5);
  PORT_HAL_SetMuxMode(PORTD_BASE_PTR, 1, kPortMuxAlt5);
#elif (defined(KV10Z1287_SERIES) || defined(KV11Z7_SERIES))
  // Configure UART1 port pins: PTE0, PTE1 - ALT3
  PORT_HAL_SetMuxMode(PORTE_BASE_PTR, 0, kPortMuxAlt3);
  PORT_HAL_SetMuxMode(PORTE_BASE_PTR, 1, kPortMuxAlt3);
#endif
}
void Uart::begin(uint32_t baudrate)
{
    SIM_HAL_EnableClock(SIM, gate_name);

    PORT_CLOCK_ENABLE(rx);
    PORT_CLOCK_ENABLE(tx);
    PORT_SET_MUX_UART(rx);
    PORT_SET_MUX_UART(tx);

#if FSL_FEATURE_SOC_UART_COUNT
    UART_HAL_Init(instance);
    UART_HAL_SetBaudRate(instance, clock, baudrate);
    UART_HAL_SetBitCountPerChar(instance, kUart8BitsPerChar);
    UART_HAL_SetParityMode(instance, kUartParityDisabled);
#if FSL_FEATURE_UART_HAS_STOP_BIT_CONFIG_SUPPORT
    UART_HAL_SetStopBitCount(instance, kUartOneStopBit);
#endif

    UART_HAL_SetIntMode(instance, kUartIntRxDataRegFull, true);
    NVIC_EnableIRQ(irqNumber);

    UART_HAL_EnableTransmitter(instance);
    UART_HAL_EnableReceiver(instance);
#endif
}
Example #3
0
void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) {
    uint32_t uart_addrs[] = UART_BASE_ADDRS;
    UART_HAL_SetBitCountPerChar(uart_addrs[obj->index], (uart_bit_count_per_char_t)data_bits);
    UART_HAL_SetParityMode(uart_addrs[obj->index], (uart_parity_mode_t)parity);
    #if FSL_FEATURE_UART_HAS_STOP_BIT_CONFIG_SUPPORT
    UART_HAL_SetStopBitCount(uart_addrs[obj->index], (uart_stop_bit_count_t)--stop_bits);
    #endif
}
Example #4
0
/*FUNCTION**********************************************************************
 *
 * Function Name : UART_DRV_Init
 * Description   : This function initializes a UART instance for operation.
 * This function will initialize the run-time state structure to keep track of the on-going
 * transfers, ungate the clock to the UART module, initialize the module
 * to user defined settings and default settings, configure the IRQ state structure and enable
 * the module-level interrupt to the core, and enable the UART module transmitter and receiver.
 * The following is an example of how to set up the uart_state_t and the
 * uart_user_config_t parameters and how to call the UART_DRV_Init function by passing
 * in these parameters:
 *    uart_user_config_t uartConfig;
 *    uartConfig.baudRate = 9600;
 *    uartConfig.bitCountPerChar = kUart8BitsPerChar;
 *    uartConfig.parityMode = kUartParityDisabled;
 *    uartConfig.stopBitCount = kUartOneStopBit;
 *    uart_state_t uartState;
 *    UART_DRV_Init(instance, &uartState, &uartConfig);
 *
 *END**************************************************************************/
uart_status_t UART_DRV_Init(uint32_t instance, uart_state_t * uartStatePtr,
                            const uart_user_config_t * uartUserConfig)
{
    assert(uartStatePtr && uartUserConfig);
    assert(instance < HW_UART_INSTANCE_COUNT);

    uint32_t baseAddr = g_uartBaseAddr[instance];
    uint32_t uartSourceClock;

    /* Exit if current instance is already initialized. */
    if (g_uartStatePtr[instance])
    {
        return kStatus_UART_Initialized;
    }

    /* Clear the state structure for this instance. */
    memset(uartStatePtr, 0, sizeof(uart_state_t));

    /* Save runtime structure pointer.*/
    g_uartStatePtr[instance] = uartStatePtr;

    /* Un-gate UART module clock */
    CLOCK_SYS_EnableUartClock(instance);

    /* Initialize UART to a known state. */
    UART_HAL_Init(baseAddr);

    /* Create Semaphore for txIrq and rxIrq. */
    OSA_SemaCreate(&uartStatePtr->txIrqSync, 0);
    OSA_SemaCreate(&uartStatePtr->rxIrqSync, 0);

    /* UART clock source is either system clock or bus clock depending on the instance */
    uartSourceClock = CLOCK_SYS_GetUartFreq(instance);

    /* Initialize UART baud rate, bit count, parity and stop bit. */
    UART_HAL_SetBaudRate(baseAddr, uartSourceClock, uartUserConfig->baudRate);
    UART_HAL_SetBitCountPerChar(baseAddr, uartUserConfig->bitCountPerChar);
    UART_HAL_SetParityMode(baseAddr, uartUserConfig->parityMode);
#if FSL_FEATURE_UART_HAS_STOP_BIT_CONFIG_SUPPORT
    UART_HAL_SetStopBitCount(baseAddr, uartUserConfig->stopBitCount);
#endif

#if FSL_FEATURE_UART_HAS_FIFO
    uint8_t fifoSize;
    /* Obtain raw TX FIFO size bit setting */
    fifoSize = UART_HAL_GetTxFifoSize(baseAddr);
    /* Now calculate the number of data words per given FIFO size */
    uartStatePtr->txFifoEntryCount = (fifoSize == 0 ? 1 : 0x1 << (fifoSize + 1));

    /* Configure the TX FIFO watermark to be 1/2 of the total entry or 0 if entry count = 1
     * A watermark setting of 0 for TX FIFO entry count of 1 means that TDRE will only interrupt
     * when the TX buffer (the one entry in the TX FIFO) is empty. Otherwise, if we set the
     * watermark to 1, the TDRE will always be set regardless if the TX buffer was empty or not
     * as the spec says TDRE will set when the FIFO is at or below the configured watermark. */
    if (uartStatePtr->txFifoEntryCount > 1)
    {
        UART_HAL_SetTxFifoWatermark(baseAddr, (uartStatePtr->txFifoEntryCount >> 1U));
    }
Example #5
0
/*!
 * @brief Check send/receive polling functionality
 *
 */
int main(void)
{
    uint8_t  rxChar          = 0;
    uint32_t byteCountBuff   = 0;
    uint32_t uartSourceClock = 0;
    UART_Type * baseAddr     = BOARD_DEBUG_UART_BASEADDR;

    // Enable clock for PORTs, setup board clock source, config pin
    hardware_init();

    // Initialize the uart module with base address and config structure
    CLOCK_SYS_EnableUartClock(BOARD_DEBUG_UART_INSTANCE);

    // Get working uart clock
    uartSourceClock = CLOCK_SYS_GetUartFreq(BOARD_DEBUG_UART_INSTANCE);

    // Initialize UART baud rate, bit count, parity and stop bit
    UART_HAL_SetBaudRate(baseAddr, uartSourceClock, BOARD_DEBUG_UART_BAUD);
    UART_HAL_SetBitCountPerChar(baseAddr, kUart8BitsPerChar);
    UART_HAL_SetParityMode(baseAddr, kUartParityDisabled);
#if FSL_FEATURE_UART_HAS_STOP_BIT_CONFIG_SUPPORT
    UART_HAL_SetStopBitCount(baseAddr, kUartOneStopBit);
#endif

    // Enable the UART transmitter and receiver
    UART_HAL_EnableTransmitter(baseAddr);
    UART_HAL_EnableReceiver(baseAddr);

    // Inform to start polling example
    byteCountBuff = sizeof(buffStart);
    UART_HAL_SendDataPolling(baseAddr, buffStart, byteCountBuff);

    // Inform user of what to do
    byteCountBuff = sizeof(bufferData1);
    UART_HAL_SendDataPolling(baseAddr, bufferData1, byteCountBuff);

    while(true)
    {
        // Wait to receive input data
        if (kStatus_UART_Success == UART_HAL_ReceiveDataPolling(baseAddr, &rxChar, 1u))
        {
            // Send any character that received
            UART_HAL_SendDataPolling(baseAddr, &rxChar, 1u);
        }
    }

}
Example #6
0
void serial_init(serial_t *obj, PinName tx, PinName rx) {
    uint32_t uart_tx = pinmap_peripheral(tx, PinMap_UART_TX);
    uint32_t uart_rx = pinmap_peripheral(rx, PinMap_UART_RX);
    obj->index = pinmap_merge(uart_tx, uart_rx);
    MBED_ASSERT((int)obj->index != NC);

    uint32_t uartSourceClock = CLOCK_SYS_GetUartFreq(obj->index);

    CLOCK_SYS_EnableUartClock(obj->index);
    uint32_t uart_addrs[] = UART_BASE_ADDRS;
    UART_HAL_Init(uart_addrs[obj->index]);
    UART_HAL_SetBaudRate(uart_addrs[obj->index], uartSourceClock, 9600);
    UART_HAL_SetParityMode(uart_addrs[obj->index], kUartParityDisabled);
    #if FSL_FEATURE_UART_HAS_STOP_BIT_CONFIG_SUPPORT
    UART_HAL_SetStopBitCount(uart_addrs[obj->index], kUartOneStopBit);
    #endif
    UART_HAL_SetBitCountPerChar(uart_addrs[obj->index], kUart8BitsPerChar);
    UART_HAL_DisableTransmitter(uart_addrs[obj->index]);
    UART_HAL_DisableReceiver(uart_addrs[obj->index]);

    pinmap_pinout(tx, PinMap_UART_TX);
    pinmap_pinout(rx, PinMap_UART_RX);

    if (tx != NC) {
        UART_HAL_FlushTxFifo(uart_addrs[obj->index]);
        UART_HAL_EnableTransmitter(uart_addrs[obj->index]);

        pin_mode(tx, PullUp);
    }
    if (rx != NC) {
        UART_HAL_EnableReceiver(uart_addrs[obj->index]);
        pin_mode(rx, PullUp);
    }

    if (obj->index == STDIO_UART) {
        stdio_uart_inited = 1;
        memcpy(&stdio_uart, obj, sizeof(serial_t));
    }
}
/* See fsl_debug_console.h for documentation of this function.*/
debug_console_status_t DbgConsole_Init(
        uint32_t uartInstance, uint32_t baudRate, debug_console_device_type_t device)
{
    if (s_debugConsole.type != kDebugConsoleNone)
    {
        return kStatus_DEBUGCONSOLE_Failed;
    }

    /* Set debug console to initialized to avoid duplicated init operation.*/
    s_debugConsole.type = device;
    s_debugConsole.instance = uartInstance;

    /* Switch between different device. */
    switch (device)
    {
#if defined(HW_UART_INSTANCE_COUNT)
        case kDebugConsoleUART:
            {
                uint32_t g_Addr[HW_UART_INSTANCE_COUNT] = UART_BASE_ADDRS;
                uint32_t baseAddr = g_Addr[uartInstance];
                uint32_t uartSourceClock;

                s_debugConsole.baseAddr = baseAddr;
                CLOCK_SYS_EnableUartClock(uartInstance);

                /* UART clock source is either system or bus clock depending on instance */
                uartSourceClock = CLOCK_SYS_GetUartFreq(uartInstance);

                /* Initialize UART baud rate, bit count, parity and stop bit. */
                UART_HAL_SetBaudRate(baseAddr, uartSourceClock, baudRate);
                UART_HAL_SetBitCountPerChar(baseAddr, kUart8BitsPerChar);
                UART_HAL_SetParityMode(baseAddr, kUartParityDisabled);
#if FSL_FEATURE_UART_HAS_STOP_BIT_CONFIG_SUPPORT
                UART_HAL_SetStopBitCount(baseAddr, kUartOneStopBit);
#endif

                /* Finally, enable the UART transmitter and receiver*/
                UART_HAL_EnableTransmitter(baseAddr);
                UART_HAL_EnableReceiver(baseAddr);

                /* Set the funciton pointer for send and receive for this kind of device. */
                s_debugConsole.ops.Send = UART_HAL_SendDataPolling;
                s_debugConsole.ops.rx_union.UART_Receive = UART_HAL_ReceiveDataPolling;
            }
            break;
#endif
#if defined(HW_UART0_INSTANCE_COUNT)
        case kDebugConsoleLPSCI:
            {
                /* Declare config sturcuture to initialize a uart instance. */
                uint32_t g_Addr[HW_UART0_INSTANCE_COUNT] = UART0_BASE_ADDRS;
                uint32_t baseAddr = g_Addr[uartInstance];
                uint32_t uartSourceClock;

                s_debugConsole.baseAddr = baseAddr;
                CLOCK_SYS_EnableLpsciClock(uartInstance);

                uartSourceClock = CLOCK_SYS_GetLpsciFreq(uartInstance);

                /* Initialize LPSCI baud rate, bit count, parity and stop bit. */
                LPSCI_HAL_SetBaudRate(baseAddr, uartSourceClock, baudRate);
                LPSCI_HAL_SetBitCountPerChar(baseAddr, kLpsci8BitsPerChar);
                LPSCI_HAL_SetParityMode(baseAddr, kLpsciParityDisabled);
#if FSL_FEATURE_LPSCI_HAS_STOP_BIT_CONFIG_SUPPORT
                LPSCI_HAL_SetStopBitCount(baseAddr, kLpsciOneStopBit);
#endif

                /* Finally, enable the LPSCI transmitter and receiver*/
                LPSCI_HAL_EnableTransmitter(baseAddr);
                LPSCI_HAL_EnableReceiver(baseAddr);

                /* Set the funciton pointer for send and receive for this kind of device. */
                s_debugConsole.ops.Send = LPSCI_HAL_SendDataPolling;
                s_debugConsole.ops.rx_union.UART0_Receive = LPSCI_HAL_ReceiveDataPolling;
            }     
            break;
#endif
#if defined(HW_LPUART_INSTANCE_COUNT)
        case kDebugConsoleLPUART:
            {
                uint32_t g_Addr[HW_LPUART_INSTANCE_COUNT] = LPUART_BASE_ADDRS;
                uint32_t baseAddr = g_Addr[uartInstance];
                uint32_t lpuartSourceClock;

                s_debugConsole.baseAddr = baseAddr;
                CLOCK_SYS_EnableLpuartClock(uartInstance);

                /* LPUART clock source is either system or bus clock depending on instance */
                lpuartSourceClock = CLOCK_SYS_GetLpuartFreq(uartInstance);

                /* initialize the parameters of the LPUART config structure with desired data */
                LPUART_HAL_SetBaudRate(baseAddr, lpuartSourceClock, baudRate);
                LPUART_HAL_SetBitCountPerChar(baseAddr, kLpuart8BitsPerChar);
                LPUART_HAL_SetParityMode(baseAddr, kLpuartParityDisabled);
                LPUART_HAL_SetStopBitCount(baseAddr, kLpuartOneStopBit);

                /* finally, enable the LPUART transmitter and receiver */
                LPUART_HAL_SetTransmitterCmd(baseAddr, true);
                LPUART_HAL_SetReceiverCmd(baseAddr, true);

                /* Set the funciton pointer for send and receive for this kind of device. */
                s_debugConsole.ops.Send = LPUART_HAL_SendDataPolling;
                s_debugConsole.ops.rx_union.LPUART_Receive = LPUART_HAL_ReceiveDataPolling;

            }
            break;
#endif
        /* If new device is requried as the low level device for debug console,
         * Add the case branch and add the preprocessor macro to judge whether
         * this kind of device exist in this SOC. */
        default:
            /* Device identified is invalid, return invalid device error code. */
            return kStatus_DEBUGCONSOLE_InvalidDevice;
    }

    /* Configure the s_debugConsole structure only when the inti operation is successful. */
    s_debugConsole.instance = uartInstance;

#if ((defined(__GNUC__)) && (!defined(FSL_RTOS_MQX))) && (!defined(__KSDK_STDLIB__))
    setvbuf(stdout, NULL, _IONBF, 0);
    setvbuf(stdin, NULL, _IONBF, 0);
    setvbuf(stderr, NULL, _IONBF, 0);
#endif

    return kStatus_DEBUGCONSOLE_Success;
}
Example #8
0
/*lint -save  -e970 Disable MISRA rule (6.3) checking. */
int main(void)
/*lint -restore Enable MISRA rule (6.3) checking. */
{
  /* Write your local variable definition here */
	   uint8_t  rxChar          = 0;
	    uint32_t byteCountBuff   = 0;
	    uint32_t uartSourceClock = 0;
	    UART_Type * baseAddr     = UART2_BASE; // was BOARD_DEBUG_UART_BASEADDR;

  /*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
  PE_low_level_init();
  /*** End of Processor Expert internal initialization.                    ***/

  /* Write your code here */
  /* For example: for(;;) { } */
  // Initialize the uart module with base address and config structure
   CLOCK_SYS_EnableUartClock(BOARD_DEBUG_UART_INSTANCE);

   // Get working uart clock
   uartSourceClock = CLOCK_SYS_GetUartFreq(BOARD_DEBUG_UART_INSTANCE);

   // Initialize UART baud rate, bit count, parity and stop bit
   UART_HAL_SetBaudRate(baseAddr, uartSourceClock, 9600); // was BOARD_DEBUG_UART_BAUD
   UART_HAL_SetBitCountPerChar(baseAddr, kUart8BitsPerChar);
   UART_HAL_SetParityMode(baseAddr, kUartParityDisabled);
#if FSL_FEATURE_UART_HAS_STOP_BIT_CONFIG_SUPPORT
   UART_HAL_SetStopBitCount(baseAddr, kUartOneStopBit);
#endif

   // Enable the UART transmitter and receiver
   UART_HAL_EnableTransmitter(baseAddr);
   UART_HAL_EnableReceiver(baseAddr);

   // Inform to start polling example
   byteCountBuff = sizeof(buffStart);
   UART_HAL_SendDataPolling(baseAddr, buffStart, byteCountBuff);

   // Inform user of what to do
   byteCountBuff = sizeof(bufferData1);
   UART_HAL_SendDataPolling(baseAddr, bufferData1, byteCountBuff);

   while(true)
   {
       // Wait to receive input data
       if (kStatus_UART_Success == UART_HAL_ReceiveDataPolling(baseAddr, &rxChar, 1u))
       {
           // Send any character that received
           UART_HAL_SendDataPolling(baseAddr, &rxChar, 1u);
       }
   }

  /*** Don't write any code pass this line, or it will be deleted during code generation. ***/
  /*** RTOS startup code. Macro PEX_RTOS_START is defined by the RTOS component. DON'T MODIFY THIS CODE!!! ***/
  #ifdef PEX_RTOS_START
    PEX_RTOS_START();                  /* Startup of the selected RTOS. Macro is defined by the RTOS component. */
  #endif
  /*** End of RTOS startup code.  ***/
  /*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/
  for(;;){}
  /*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/
} /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/
/*FUNCTION**********************************************************************
 *
 * Function Name : UART_DRV_DmaInit
 * Description   : This function initializes a UART instance for operation.
 * This function will initialize the run-time state structure to keep track of
 * the on-going transfers, ungate the clock to the UART module, initialize the
 * module to user defined settings and default settings, configure UART DMA
 * and enable the UART module transmitter and receiver.
 * The following is an example of how to set up the uart_dma_state_t and the
 * uart_user_config_t parameters and how to call the UART_DRV_DmaInit function
 * by passing in these parameters:
 *    uart_user_config_t uartConfig;
 *    uartConfig.baudRate = 9600;
 *    uartConfig.bitCountPerChar = kUart8BitsPerChar;
 *    uartConfig.parityMode = kUartParityDisabled;
 *    uartConfig.stopBitCount = kUartOneStopBit;
 *    uart_dma_state_t uartDmaState;
 *    UART_DRV_DmaInit(instance, &uartDmaState, &uartConfig);
 *
 *END**************************************************************************/
uart_status_t UART_DRV_DmaInit(uint32_t instance,
                               uart_dma_state_t * uartDmaStatePtr,
                               const uart_dma_user_config_t * uartUserConfig)
{
    assert(uartDmaStatePtr && uartUserConfig);
    assert(g_uartBase[instance]);
    assert(instance < UART_INSTANCE_COUNT);
    /* This driver only support UART instances with separate DMA channels for
     * both Tx and Rx.*/
    assert(FSL_FEATURE_UART_HAS_SEPARATE_DMA_RX_TX_REQn(instance) == 1);

    UART_Type * base = g_uartBase[instance];
    uint32_t uartSourceClock = 0;
    dma_request_source_t uartTxDmaRequest = kDmaRequestMux0Disable;
    dma_request_source_t uartRxDmaRequest = kDmaRequestMux0Disable;

    /* Exit if current instance is already initialized. */
    if (g_uartStatePtr[instance])
    {
        return kStatus_UART_Initialized;
    }

    /* Clear the state structure for this instance. */
    memset(uartDmaStatePtr, 0, sizeof(uart_dma_state_t));

    /* Save runtime structure pointer.*/
    g_uartStatePtr[instance] = uartDmaStatePtr;

    /* Un-gate UART module clock */
    CLOCK_SYS_EnableUartClock(instance);

    /* Initialize UART to a known state. */
    UART_HAL_Init(base);

    /* Create Semaphore for txIrq and rxIrq. */
    OSA_SemaCreate(&uartDmaStatePtr->txIrqSync, 0);
    OSA_SemaCreate(&uartDmaStatePtr->rxIrqSync, 0);

    /* UART clock source is either system or bus clock depending on instance */
    uartSourceClock = CLOCK_SYS_GetUartFreq(instance);

    /* Initialize UART baud rate, bit count, parity and stop bit. */
    UART_HAL_SetBaudRate(base, uartSourceClock, uartUserConfig->baudRate);
    UART_HAL_SetBitCountPerChar(base, uartUserConfig->bitCountPerChar);
    UART_HAL_SetParityMode(base, uartUserConfig->parityMode);
#if FSL_FEATURE_UART_HAS_STOP_BIT_CONFIG_SUPPORT
    UART_HAL_SetStopBitCount(base, uartUserConfig->stopBitCount);
#endif

    /* Enable DMA trigger when transmit data register empty,
     * and receive data register full. */
    UART_HAL_SetTxDmaCmd(base, true);
    UART_HAL_SetRxDmaCmd(base, true);

    switch (instance)
    {
#if (FSL_FEATURE_UART_HAS_SEPARATE_DMA_RX_TX_REQn(0) == 1)
        case 0:
            uartRxDmaRequest = kDmaRequestMux0UART0Rx;
            uartTxDmaRequest = kDmaRequestMux0UART0Tx;
            break;
#endif
#if (FSL_FEATURE_UART_HAS_SEPARATE_DMA_RX_TX_REQn(1) == 1)
        case 1:
            uartRxDmaRequest = kDmaRequestMux0UART1Rx;
            uartTxDmaRequest = kDmaRequestMux0UART1Tx;
            break;
#endif
#if (FSL_FEATURE_UART_HAS_SEPARATE_DMA_RX_TX_REQn(2) == 1)
        case 2:
            uartRxDmaRequest = kDmaRequestMux0UART2Rx;
            uartTxDmaRequest = kDmaRequestMux0UART2Tx;
            break;
#endif
        default :
            break;
    }

    /* Request DMA channels for RX FIFO. */
    DMA_DRV_RequestChannel(kDmaAnyChannel, uartRxDmaRequest,
                            &uartDmaStatePtr->dmaUartRx);
    DMA_DRV_RegisterCallback(&uartDmaStatePtr->dmaUartRx,
                    UART_DRV_DmaRxCallback, (void *)instance);

    /* Request DMA channels for TX FIFO. */
    DMA_DRV_RequestChannel(kDmaAnyChannel, uartTxDmaRequest,
                            &uartDmaStatePtr->dmaUartTx);
    DMA_DRV_RegisterCallback(&uartDmaStatePtr->dmaUartTx,
                    UART_DRV_DmaTxCallback, (void *)instance);

    /* Finally, enable the UART transmitter and receiver*/
    UART_HAL_EnableTransmitter(base);
    UART_HAL_EnableReceiver(base);

    return kStatus_UART_Success;
}
Example #10
0
/* See fsl_debug_console.h for documentation of this function.*/
debug_console_status_t DbgConsole_Init(
        uint32_t uartInstance, uint32_t baudRate, debug_console_device_type_t device)
{
    if (s_debugConsole.type != kDebugConsoleNone)
    {
        return kStatus_DEBUGCONSOLE_Failed;
    }

    /* Set debug console to initialized to avoid duplicated init operation.*/
    s_debugConsole.type = device;
    s_debugConsole.instance = uartInstance;

    /* Switch between different device. */
    switch (device)
    {
#if (defined(USB_INSTANCE_COUNT) && defined(BOARD_USE_VIRTUALCOM))  /*&& defined()*/
       case kDebugConsoleUSBCDC:
         {
                VirtualCom_Init();
                s_debugConsole.base = (void*)g_app_handle;
                s_debugConsole.ops.tx_union.USB_Send = VirtualCom_SendDataBlocking;
                s_debugConsole.ops.rx_union.USB_Receive = VirtualCom_ReceiveDataBlocking;
         }
         break;
#endif
#if defined(UART_INSTANCE_COUNT)
        case kDebugConsoleUART:
            {
                UART_Type * g_Base[UART_INSTANCE_COUNT] = UART_BASE_PTRS;
                UART_Type * base = g_Base[uartInstance];
                uint32_t uartSourceClock;

                s_debugConsole.base = base;
                CLOCK_SYS_EnableUartClock(uartInstance);

                /* UART clock source is either system or bus clock depending on instance */
                uartSourceClock = CLOCK_SYS_GetUartFreq(uartInstance);

                /* Initialize UART baud rate, bit count, parity and stop bit. */
                UART_HAL_SetBaudRate(base, uartSourceClock, baudRate);
                UART_HAL_SetBitCountPerChar(base, kUart8BitsPerChar);
                UART_HAL_SetParityMode(base, kUartParityDisabled);
#if FSL_FEATURE_UART_HAS_STOP_BIT_CONFIG_SUPPORT
                UART_HAL_SetStopBitCount(base, kUartOneStopBit);
#endif

                /* Finally, enable the UART transmitter and receiver*/
                UART_HAL_EnableTransmitter(base);
                UART_HAL_EnableReceiver(base);

                /* Set the funciton pointer for send and receive for this kind of device. */
                s_debugConsole.ops.tx_union.UART_Send = UART_HAL_SendDataPolling;
                s_debugConsole.ops.rx_union.UART_Receive = UART_HAL_ReceiveDataPolling;
            }
            break;
#endif
#if defined(UART0_INSTANCE_COUNT)
        case kDebugConsoleLPSCI:
            {
                /* Declare config sturcuture to initialize a uart instance. */
                UART0_Type * g_Base[UART0_INSTANCE_COUNT] = UART0_BASE_PTRS;
                UART0_Type * base = g_Base[uartInstance];
                uint32_t uartSourceClock;

                s_debugConsole.base = base;
                CLOCK_SYS_EnableLpsciClock(uartInstance);

                uartSourceClock = CLOCK_SYS_GetLpsciFreq(uartInstance);

                /* Initialize LPSCI baud rate, bit count, parity and stop bit. */
                LPSCI_HAL_SetBaudRate(base, uartSourceClock, baudRate);
                LPSCI_HAL_SetBitCountPerChar(base, kLpsci8BitsPerChar);
                LPSCI_HAL_SetParityMode(base, kLpsciParityDisabled);
#if FSL_FEATURE_LPSCI_HAS_STOP_BIT_CONFIG_SUPPORT
                LPSCI_HAL_SetStopBitCount(base, kLpsciOneStopBit);
#endif

                /* Finally, enable the LPSCI transmitter and receiver*/
                LPSCI_HAL_EnableTransmitter(base);
                LPSCI_HAL_EnableReceiver(base);

                /* Set the funciton pointer for send and receive for this kind of device. */
                s_debugConsole.ops.tx_union.UART0_Send = LPSCI_HAL_SendDataPolling;
                s_debugConsole.ops.rx_union.UART0_Receive = LPSCI_HAL_ReceiveDataPolling;
            }
            break;
#endif
#if defined(LPUART_INSTANCE_COUNT)
        case kDebugConsoleLPUART:
            {
                LPUART_Type* g_Base[LPUART_INSTANCE_COUNT] = LPUART_BASE_PTRS;
                LPUART_Type* base = g_Base[uartInstance];
                uint32_t lpuartSourceClock;

                s_debugConsole.base = base;
                CLOCK_SYS_EnableLpuartClock(uartInstance);

                /* LPUART clock source is either system or bus clock depending on instance */
                lpuartSourceClock = CLOCK_SYS_GetLpuartFreq(uartInstance);

                /* initialize the parameters of the LPUART config structure with desired data */
                LPUART_HAL_SetBaudRate(base, lpuartSourceClock, baudRate);
                LPUART_HAL_SetBitCountPerChar(base, kLpuart8BitsPerChar);
                LPUART_HAL_SetParityMode(base, kLpuartParityDisabled);
                LPUART_HAL_SetStopBitCount(base, kLpuartOneStopBit);

                /* finally, enable the LPUART transmitter and receiver */
                LPUART_HAL_SetTransmitterCmd(base, true);
                LPUART_HAL_SetReceiverCmd(base, true);

                /* Set the funciton pointer for send and receive for this kind of device. */
                s_debugConsole.ops.tx_union.LPUART_Send = LPUART_HAL_SendDataPolling;
                s_debugConsole.ops.rx_union.LPUART_Receive = LPUART_HAL_ReceiveDataPolling;

            }
            break;
#endif
        /* If new device is requried as the low level device for debug console,
         * Add the case branch and add the preprocessor macro to judge whether
         * this kind of device exist in this SOC. */
        default:
            /* Device identified is invalid, return invalid device error code. */
            return kStatus_DEBUGCONSOLE_InvalidDevice;
    }

    /* Configure the s_debugConsole structure only when the inti operation is successful. */
    s_debugConsole.instance = uartInstance;

    return kStatus_DEBUGCONSOLE_Success;
}