/*FUNCTION********************************************************************** * * Function Name : LPSCI_DRV_Init * Description : This function initializes a LPSCI 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 LPSCI 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 * LPSCI module transmitter and receiver. * The following is an example of how to set up the lpsci_state_t and the * lpsci_user_config_t parameters and how to call the LPSCI_DRV_Init function * by passing in these parameters: * lpsci_user_config_t lpsciConfig; * lpsciConfig.clockSource = kClockLpsciSrcPllFllSel; * lpsciConfig.baudRate = 9600; * lpsciConfig.bitCountPerChar = kLpsci8BitsPerChar; * lpsciConfig.parityMode = kLpsciParityDisabled; * lpsciConfig.stopBitCount = kLpsciOneStopBit; * lpsci_state_t lpsciState; * LPSCI_DRV_Init(instance, &lpsciState, &lpsciConfig); * *END**************************************************************************/ lpsci_status_t LPSCI_DRV_Init(uint32_t instance, lpsci_state_t * lpsciStatePtr, const lpsci_user_config_t * lpsciUserConfig) { assert(lpsciStatePtr && lpsciUserConfig); assert(instance < HW_UART0_INSTANCE_COUNT); uint32_t baseAddr = g_lpsciBaseAddr[instance]; uint32_t lpsciSourceClock; /* Exit if current instance is already initialized. */ if (g_lpsciStatePtr[instance]) { return kStatus_LPSCI_Initialized; } /* Clear the state structure for this instance. */ memset(lpsciStatePtr, 0, sizeof(lpsci_state_t)); /* Save runtime structure pointer.*/ g_lpsciStatePtr[instance] = lpsciStatePtr; /* Un-gate LPSCI module clock */ CLOCK_SYS_EnableLpsciClock(instance); /* Set LPSCI clock source */ CLOCK_SYS_SetLpsciSrc(instance, lpsciUserConfig->clockSource); /* Initialize LPSCI to a known state. */ LPSCI_HAL_Init(baseAddr); /* Create Semaphore for txIrq and rxIrq. */ OSA_SemaCreate(&lpsciStatePtr->txIrqSync, 0); OSA_SemaCreate(&lpsciStatePtr->rxIrqSync, 0); lpsciSourceClock = CLOCK_SYS_GetLpsciFreq(instance); /* Initialize LPSCI baud rate, bit count, parity and stop bit. */ LPSCI_HAL_SetBaudRate(baseAddr, lpsciSourceClock, lpsciUserConfig->baudRate); LPSCI_HAL_SetBitCountPerChar(baseAddr, lpsciUserConfig->bitCountPerChar); LPSCI_HAL_SetParityMode(baseAddr, lpsciUserConfig->parityMode); #if FSL_FEATURE_LPSCI_HAS_STOP_BIT_CONFIG_SUPPORT LPSCI_HAL_SetStopBitCount(baseAddr, lpsciUserConfig->stopBitCount); #endif /* Enable LPSCI interrupt on NVIC level. */ INT_SYS_EnableIRQ(g_lpsciRxTxIrqId[instance]); /* Finally, enable the LPSCI transmitter and receiver*/ LPSCI_HAL_EnableTransmitter(baseAddr); LPSCI_HAL_EnableReceiver(baseAddr); return kStatus_LPSCI_Success; }
void uart_init(void) { uint32_t lpsciSourceClock = 0; // Set the LPSCI clock source selection CLOCK_SYS_SetLpsciSrc(0, kClockLpsciSrcFll); // Enable the clock for LPSCI module CLOCK_SYS_EnableLpsciClock(0); // Gets the clock frequency for LPSCI module lpsciSourceClock = CLOCK_SYS_GetLpsciFreq(0); // Initialize LPSCI baud rate, bit count, parity and stop bit LPSCI_HAL_SetBaudRate(UART0, lpsciSourceClock, 9600); LPSCI_HAL_SetBitCountPerChar(UART0, kLpsci8BitsPerChar); LPSCI_HAL_SetParityMode(UART0, kLpsciParityDisabled); LPSCI_HAL_SetStopBitCount(UART0, kLpsciOneStopBit); // Enable the LPSCI transmitter and receiver LPSCI_HAL_EnableTransmitter(UART0); LPSCI_HAL_EnableReceiver(UART0); }
/*FUNCTION********************************************************************** * * Function Name : LPSCI_DRV_DmaInit * Description : This function initializes a LPSCI 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 LPSCI module, initialize the * module to user defined settings and default settings, configure LPSCI DMA * and enable the LPSCI module transmitter and receiver. * The following is an example of how to set up the lpsci_dma_state_t and the * lpsci_user_config_t parameters and how to call the LPSCI_DRV_DmaInit function * by passing in these parameters: * lpsci_user_config_t lpsciConfig; * lpsciConfig.baudRate = 9600; * lpsciConfig.bitCountPerChar = kLpsci8BitsPerChar; * lpsciConfig.parityMode = kLpsciParityDisabled; * lpsciConfig.stopBitCount = kLpsciOneStopBit; * lpsci_dma_state_t lpsciDmaState; * LPSCI_DRV_DmaInit(instance, &lpsciDmaState, &lpsciConfig); * *END**************************************************************************/ lpsci_status_t LPSCI_DRV_DmaInit(uint32_t instance, lpsci_dma_state_t * lpsciDmaStatePtr, const lpsci_dma_user_config_t * lpsciUserConfig) { assert(lpsciDmaStatePtr && lpsciUserConfig); assert(instance < UART0_INSTANCE_COUNT); UART0_Type * base = g_lpsciBase[instance]; uint32_t lpsciSourceClock = 0; dma_request_source_t lpsciTxDmaRequest = kDmaRequestMux0Disable; dma_request_source_t lpsciRxDmaRequest = kDmaRequestMux0Disable; dma_channel_t *chn; DMA_Type * dmaBase; dma_channel_link_config_t config; config.channel1 = 0; config.channel2 = 0; config.linkType = kDmaChannelLinkDisable; /* Exit if current instance is already initialized. */ if (g_lpsciStatePtr[instance]) { return kStatus_LPSCI_Initialized; } /* Clear the state structure for this instance. */ memset(lpsciDmaStatePtr, 0, sizeof(lpsci_dma_state_t)); /* Save runtime structure pointer.*/ g_lpsciStatePtr[instance] = lpsciDmaStatePtr; /* Un-gate LPSCI module clock */ CLOCK_SYS_EnableLpsciClock(instance); /* Set LPSCI clock source */ CLOCK_SYS_SetLpsciSrc(instance, lpsciUserConfig->clockSource); /* Initialize LPSCI to a known state. */ LPSCI_HAL_Init(base); /* Create Semaphore for txIrq and rxIrq. */ OSA_SemaCreate(&lpsciDmaStatePtr->txIrqSync, 0); OSA_SemaCreate(&lpsciDmaStatePtr->rxIrqSync, 0); /* LPSCI clock source is either system or bus clock depending on instance */ lpsciSourceClock = CLOCK_SYS_GetLpsciFreq(instance); /* Initialize LPSCI baud rate, bit count, parity and stop bit. */ LPSCI_HAL_SetBaudRate(base, lpsciSourceClock, lpsciUserConfig->baudRate); LPSCI_HAL_SetBitCountPerChar(base, lpsciUserConfig->bitCountPerChar); LPSCI_HAL_SetParityMode(base, lpsciUserConfig->parityMode); #if FSL_FEATURE_LPSCI_HAS_STOP_BIT_CONFIG_SUPPORT LPSCI_HAL_SetStopBitCount(base, lpsciUserConfig->stopBitCount); #endif /* Enable DMA trigger when transmit data register empty, * and receive data register full. */ LPSCI_HAL_SetTxDmaCmd(base, true); LPSCI_HAL_SetRxDmaCmd(base, true); switch (instance) { case 0: lpsciRxDmaRequest = kDmaRequestMux0LPSCI0Rx; lpsciTxDmaRequest = kDmaRequestMux0LPSCI0Tx; break; default : break; } /* Request DMA channels for RX FIFO. */ DMA_DRV_RequestChannel(kDmaAnyChannel, lpsciRxDmaRequest, &lpsciDmaStatePtr->dmaLpsciRx); DMA_DRV_RegisterCallback(&lpsciDmaStatePtr->dmaLpsciRx, LPSCI_DRV_DmaRxCallback, (void *)instance); chn = &lpsciDmaStatePtr->dmaLpsciRx; dmaBase = g_dmaBase[chn->channel/FSL_FEATURE_DMA_DMAMUX_CHANNELS]; DMA_HAL_SetAutoAlignCmd(dmaBase, chn->channel, false); DMA_HAL_SetCycleStealCmd(dmaBase, chn->channel, true); DMA_HAL_SetAsyncDmaRequestCmd(dmaBase, chn->channel, false); DMA_HAL_SetDisableRequestAfterDoneCmd(dmaBase, chn->channel, true); DMA_HAL_SetChanLink(dmaBase, chn->channel, &config); DMA_HAL_SetSourceAddr(dmaBase, chn->channel, LPSCI_HAL_GetDataRegAddr(base)); DMA_HAL_SetSourceModulo(dmaBase, chn->channel, kDmaModuloDisable); DMA_HAL_SetSourceTransferSize(dmaBase, chn->channel, kDmaTransfersize8bits); DMA_HAL_SetSourceIncrementCmd(dmaBase, chn->channel, false); DMA_HAL_SetDestModulo(dmaBase, chn->channel, kDmaModuloDisable); DMA_HAL_SetDestTransferSize(dmaBase, chn->channel, kDmaTransfersize8bits); DMA_HAL_SetDestIncrementCmd(dmaBase, chn->channel, true); DMA_HAL_SetIntCmd(dmaBase, chn->channel, true); /* Request DMA channels for TX FIFO. */ DMA_DRV_RequestChannel(kDmaAnyChannel, lpsciTxDmaRequest, &lpsciDmaStatePtr->dmaLpsciTx); DMA_DRV_RegisterCallback(&lpsciDmaStatePtr->dmaLpsciTx, LPSCI_DRV_DmaTxCallback, (void *)instance); chn = &lpsciDmaStatePtr->dmaLpsciTx; dmaBase = g_dmaBase[chn->channel/FSL_FEATURE_DMA_DMAMUX_CHANNELS]; DMA_HAL_SetAutoAlignCmd(dmaBase, chn->channel, false); DMA_HAL_SetCycleStealCmd(dmaBase, chn->channel, true); DMA_HAL_SetAsyncDmaRequestCmd(dmaBase, chn->channel, false); DMA_HAL_SetDisableRequestAfterDoneCmd(dmaBase, chn->channel, true); DMA_HAL_SetChanLink(dmaBase, chn->channel, &config); DMA_HAL_SetSourceModulo(dmaBase, chn->channel, kDmaModuloDisable); DMA_HAL_SetSourceTransferSize(dmaBase, chn->channel, kDmaTransfersize8bits); DMA_HAL_SetSourceIncrementCmd(dmaBase, chn->channel, true); DMA_HAL_SetDestAddr(dmaBase, chn->channel, LPSCI_HAL_GetDataRegAddr(base)); DMA_HAL_SetDestModulo(dmaBase, chn->channel, kDmaModuloDisable); DMA_HAL_SetDestTransferSize(dmaBase, chn->channel, kDmaTransfersize8bits); DMA_HAL_SetDestIncrementCmd(dmaBase, chn->channel, false); DMA_HAL_SetIntCmd(dmaBase, chn->channel, true); /* Finally, enable the LPSCI transmitter and receiver*/ LPSCI_HAL_EnableTransmitter(base); LPSCI_HAL_EnableReceiver(base); return kStatus_LPSCI_Success; }
/* 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; }
/* 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; }