/*! * @brief Check send/receive non blocking functionality * */ int main(void) { uint8_t rxChar = 0, txChar = 0; uint32_t byteCountBuff = 0; lpuart_state_t lpuartStatePtr; // Fill in lpuart config data lpuart_user_config_t lpuartConfig = { .clockSource = BOARD_LPUART_CLOCK_SOURCE, .bitCountPerChar = kLpuart8BitsPerChar, .parityMode = kLpuartParityDisabled, .stopBitCount = kLpuartOneStopBit, .baudRate = BOARD_DEBUG_UART_BAUD }; // Enable clock for PORTs, setup board clock source hardware_init(); // Initialize the lpuart module with instance number and config structure LPUART_DRV_Init(BOARD_DEBUG_UART_INSTANCE, &lpuartStatePtr, &lpuartConfig); // Inform to start non blocking example byteCountBuff = sizeof(buffStart); LPUART_DRV_SendData(BOARD_DEBUG_UART_INSTANCE, buffStart, byteCountBuff); while (kStatus_LPUART_TxBusy == LPUART_DRV_GetTransmitStatus(BOARD_DEBUG_UART_INSTANCE, NULL)){} // Inform user of what to do byteCountBuff = sizeof(bufferData1); LPUART_DRV_SendData(BOARD_DEBUG_UART_INSTANCE, bufferData1, byteCountBuff); while (kStatus_LPUART_TxBusy == LPUART_DRV_GetTransmitStatus(BOARD_DEBUG_UART_INSTANCE, NULL)){} // Send/receive non blocking function while(true) { // Wait to receive input data LPUART_DRV_ReceiveData(BOARD_DEBUG_UART_INSTANCE, &rxChar, 1); while (kStatus_LPUART_RxBusy == LPUART_DRV_GetReceiveStatus(BOARD_DEBUG_UART_INSTANCE, NULL)){} txChar = rxChar; // Wait for the transfer finish, OR do something else LPUART_DRV_SendData(BOARD_DEBUG_UART_INSTANCE, &txChar, 1); } }
static int nio_serial_init(void *init_data, void **dev_context) { osa_status_t status; NIO_SERIAL_DEV_CONTEXT_STRUCT *serial_dev_context = (NIO_SERIAL_DEV_CONTEXT_STRUCT *)dev_context; NIO_SERIAL_INIT_DATA_STRUCT *init = (NIO_SERIAL_INIT_DATA_STRUCT*)init_data; #if PLATFORM_LPUART_ENABLED assert(init->UART_INSTANCE < HW_LPUART_INSTANCE_COUNT); lpuart_user_config_t uartConfig = #else assert(init->UART_INSTANCE < HW_UART_INSTANCE_COUNT); uart_user_config_t uartConfig = #endif { .baudRate = init->BAUDRATE, .parityMode = init->PARITY_MODE, .stopBitCount = init->STOPBIT_COUNT, .bitCountPerChar = init->BITCOUNT_PERCHAR, }; serial_dev_context = (NIO_SERIAL_DEV_CONTEXT_STRUCT*) OSA_MemAlloc(sizeof(NIO_SERIAL_DEV_CONTEXT_STRUCT)); /* SDK HAL init */ #if PLATFORM_LPUART_ENABLED if ( kStatus_UART_Success != LPUART_DRV_Init(init->UART_INSTANCE, &serial_dev_context->uart_state, &uartConfig)) { errno = ENXIO; } /* LPUART handler interrupt installation */ status = OSA_InstallIntHandler(g_lpuartRxTxIrqId[init->UART_INSTANCE], init->handler); if (kStatus_OSA_Success != status) { errno = ENXIO; } NVIC_SetPriority(g_lpuartRxTxIrqId[init->UART_INSTANCE], init->RXTX_PRIOR); NVIC_EnableIRQ(g_lpuartRxTxIrqId[init->UART_INSTANCE]); #else if ( kStatus_UART_Success != UART_DRV_Init(init->UART_INSTANCE, &serial_dev_context->uart_state, &uartConfig)) { errno = ENXIO; } /* UART handler interrupt installation */ status = OSA_InstallIntHandler(g_uartRxTxIrqId[init->UART_INSTANCE], init->handler); if (kStatus_OSA_Success != status) { errno = ENXIO; } NVIC_SetPriority(g_uartRxTxIrqId[init->UART_INSTANCE], init->RXTX_PRIOR); NVIC_EnableIRQ(g_uartRxTxIrqId[init->UART_INSTANCE]); #endif serial_dev_context->instance = init->UART_INSTANCE; *dev_context = (void*)serial_dev_context; return 0; } static int nio_serial_deinit(void *dev_context) { NIO_SERIAL_DEV_CONTEXT_STRUCT *serial_dev_context = (NIO_SERIAL_DEV_CONTEXT_STRUCT *)dev_context; #if PLATFORM_LPUART_ENABLED LPUART_DRV_Deinit(serial_dev_context->instance); #else UART_DRV_Deinit(serial_dev_context->instance); #endif OSA_MemFree(dev_context); return 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; /* Switch between different device. */ switch (device) { #if defined(HW_UART_INSTANCE_COUNT) case kDebugConsoleUART: { /* Declare config sturcuture to initialize a uart instance. */ uart_user_config_t uartConfig; uart_status_t status; /* Config the structure. */ uartConfig.baudRate = baudRate; uartConfig.bitCountPerChar = kUart8BitsPerChar; uartConfig.parityMode = kUartParityDisabled; uartConfig.stopBitCount = kUartOneStopBit; /* Init UART device. */ status = UART_DRV_Init(uartInstance, &s_debugConsole.state.uartState, &uartConfig); if ((status != kStatus_UART_Success)&&(status != kStatus_UART_Initialized)) { s_debugConsole.type = kDebugConsoleNone; return kStatus_DEBUGCONSOLE_Failed; } /* Set the funciton pointer for send and receive for this kind of device. */ s_debugConsole.ops.Send = UART_DRV_SendPollBlocking; s_debugConsole.ops.Receive = UART_DRV_ReceivePollBlocking; } break; #endif #if 0 #if defined(HW_LPUART_INSTANCE_COUNT) case kDebugConsoleLPUART: { /* Declare config sturcuture to initialize a uart instance. */ lpuart_user_config_t lpuartConfig; lpuart_status_t status; /* Config the structure. */ lpuartConfig.baudRate = baudRate; lpuartConfig.bitCountPerChar = kLpuart8BitsPerChar; lpuartConfig.parityMode = kLpuartParityDisabled; lpuartConfig.stopBitCount = kLpuartOneStopBit; /* Init LPUART device. */ status = LPUART_DRV_Init(uartInstance, &s_debugConsole.state.lpuartState, &lpuartConfig); if ((status != kStatus_UART_Success)&&(status != kStatus_UART_Initialized)) { s_debugConsole.type = kDebugConsoleNone; return kStatus_DEBUGCONSOLE_Failed; } /* Set the funciton pointer for send and receive for this kind of device. */ s_debugConsole.ops.Send = LPUART_DRV_SendPollBlocking; s_debugConsole.ops.Receive = LPUART_DRV_ReceivePollBlocking; } break; #endif #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))) setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stdin, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); #endif return kStatus_DEBUGCONSOLE_Success; }
/*! * @brief Main function */ int main (void) { /* enable clock for PORTs */ CLOCK_SYS_EnablePortClock(PORTA_IDX); //CLOCK_SYS_EnablePortClock(PORTB_IDX); CLOCK_SYS_EnablePortClock(PORTC_IDX); CLOCK_SYS_EnablePortClock(PORTD_IDX); CLOCK_SYS_EnablePortClock(PORTE_IDX); /* Set allowed power mode, allow all. */ SMC_HAL_SetProtection(SMC, kAllowPowerModeAll); /* Set system clock configuration. */ CLOCK_SYS_SetConfiguration(&g_defaultClockConfigVlpr); /* Initialize LPTMR */ lptmr_state_t lptmrState; LPTMR_DRV_Init(LPTMR0_IDX, &lptmrState, &g_lptmrConfig); LPTMR_DRV_SetTimerPeriodUs(LPTMR0_IDX, 100000); LPTMR_DRV_InstallCallback(LPTMR0_IDX, lptmr_call_back); /* Initialize DMA */ dma_state_t dma_state; DMA_DRV_Init(&dma_state); /* Initialize PIT */ PIT_DRV_Init(0, false); PIT_DRV_InitChannel(0, 0, &g_pitChan0); /* Initialize CMP */ CMP_DRV_Init(0, &g_cmpState, &g_cmpConf); CMP_DRV_ConfigDacChn(0, &g_cmpDacConf); PORT_HAL_SetMuxMode(g_portBase[GPIOC_IDX], 0, kPortMuxAlt5); CMP_DRV_Start(0); /* Buttons */ GPIO_DRV_InputPinInit(&g_switch1); GPIO_DRV_InputPinInit(&g_switch2); GPIO_DRV_InputPinInit(&g_switchUp); GPIO_DRV_InputPinInit(&g_switchDown); GPIO_DRV_InputPinInit(&g_switchLeft); GPIO_DRV_InputPinInit(&g_switchRight); GPIO_DRV_InputPinInit(&g_switchSelect); /* Start LPTMR */ LPTMR_DRV_Start(LPTMR0_IDX); /* Setup LPUART1 */ LPUART_DRV_Init(1, &g_lpuartState, &g_lpuartConfig); LPUART_DRV_InstallRxCallback(1, lpuartRxCallback, rxBuff, NULL, true); LPUART_DRV_InstallTxCallback(1, lpuartTxCallback, NULL, NULL); LPUART_BWR_CTRL_TXINV(g_lpuartBase[1], 1); PORT_HAL_SetMuxMode(g_portBase[GPIOE_IDX], 0, kPortMuxAlt3); PORT_HAL_SetMuxMode(g_portBase[GPIOE_IDX], 1, kPortMuxAlt3); /* Setup FlexIO for the WS2812B */ FLEXIO_Type *fiobase = g_flexioBase[0]; CLOCK_SYS_SetFlexioSrc(0, kClockFlexioSrcMcgIrClk); FLEXIO_DRV_Init(0, &g_flexioConfig); FLEXIO_HAL_ConfigureTimer(fiobase, 0, &g_timerConfig); FLEXIO_HAL_ConfigureShifter(fiobase, 0, &g_shifterConfig); PORT_HAL_SetMuxMode(g_portBase[GPIOE_IDX], 20, kPortMuxAlt6); FLEXIO_DRV_Start(0); FLEXIO_HAL_SetShifterStatusDmaCmd(fiobase, 1, true); DMA_DRV_RequestChannel(kDmaAnyChannel, kDmaRequestMux0FlexIOChannel0, &g_fioChan); DMA_DRV_RegisterCallback(&g_fioChan, fioDmaCallback, NULL); /* Connect buzzer to TPM0_CH3 */ PORT_HAL_SetMuxMode(g_portBase[GPIOE_IDX], 30, kPortMuxAlt3); tpm_general_config_t tmpConfig = { .isDBGMode = false, .isGlobalTimeBase = false, .isTriggerMode = false, .isStopCountOnOveflow = false, .isCountReloadOnTrig = false, .triggerSource = kTpmTrigSel0, }; TPM_DRV_Init(0, &tmpConfig); TPM_DRV_SetClock(0, kTpmClockSourceModuleMCGIRCLK, kTpmDividedBy1); /* Blank LED just in case, saves power */ led(0x00, 0x00, 0x00); /* Init e-paper display */ EPD_Init(); /* Throw up first image */ int ret = EPD_Draw(NULL, images[current_image]); if (-1 == ret) { led(0xff, 0x00, 0x00); } else if (-2 == ret) { led(0xff, 0xff, 0x00); } else if (-3 == ret) { led(0x00, 0x00, 0xff); } else { led(0x00, 0xff, 0x00); } blank_led = 30; /* Deinit so we can mess around on the bus pirate */ //EPD_Deinit(); /* We're done, everything else is triggered through interrupts */ for(;;) { if (cue_next_image) { int old_image = current_image; current_image = (current_image + 1) % image_count; EPD_Draw(images[old_image], images[current_image]); cue_next_image = 0; } #ifndef DEBUG SMC_HAL_SetMode(SMC, &g_idlePowerMode); #endif } }