/*FUNCTION********************************************************************** * * Function Name : POWER_SYS_SetWakeupPin * Description : This function allows to set wake up pin in low leakage wake up unit (LLWU). * Each of the available wake-up sources can be individually enabled or disabled. * *END**************************************************************************/ void POWER_SYS_SetWakeupPin(power_wakeup_pin_t pin, llwu_external_pin_modes_t pinMode, const gpio_input_pin_t * config) { llwu_wakeup_pin_t llwuPin = POWER_EXTRACT_LLWU_PIN(pin); uint32_t gpioPin = POWER_EXTRACT_GPIO_PINNAME(pin); assert( (uint32_t)llwuPin < FSL_FEATURE_LLWU_HAS_EXTERNAL_PIN); LLWU_HAL_SetExternalInputPinMode(LLWU,pinMode, llwuPin); /* Configures gpio pin if config is passed */ if( (gpioPin != POWER_GPIO_RESERVED) && (config != NULL) ) { /* Configures pin as input pin and configures electrical parameters */ gpio_input_pin_user_config_t pinConfig; pinConfig.pinName = gpioPin; pinConfig.config = *config; GPIO_DRV_InputPinInit(&pinConfig); } }
/*FUNCTION********************************************************************** * * Function Name : GPIO_DRV_Init * Description : Initialize all GPIO pins used by board. * To initialize the GPIO driver, two arrays similar with gpio_input_pin_user_config_t * inputPin[] and gpio_output_pin_user_config_t outputPin[] should be defined in user's file. * Then simply call GPIO_DRV_Init() and pass into these two arrays. If input or output * pins are not needed, pass in a NULL. * *END**************************************************************************/ void GPIO_DRV_Init(const gpio_input_pin_user_config_t * inputPins, const gpio_output_pin_user_config_t * outputPins) { if (inputPins) { /* Initialize input pins.*/ while (inputPins->pinName != GPIO_PINS_OUT_OF_RANGE) { GPIO_DRV_InputPinInit(inputPins++); } } if (outputPins) { /* Initialize output pins.*/ while (outputPins->pinName != GPIO_PINS_OUT_OF_RANGE) { GPIO_DRV_OutputPinInit(outputPins++); } } }
/*TASK*----------------------------------------------------- * * Task Name : sdcard_task * Comments : Open device and install MFS on device * * *END*-----------------------------------------------------*/ void sdcard_task(void) { /* Body */ int dev_fd = -1, a_fd = -1; bool inserted = FALSE, last = FALSE; int32_t error_code; uint32_t isFormatted = 0; int esdhc_handle = -1; char partman_name[] = "pm:"; char partition_name[] = "pm:1"; int partition_handle; SDCARD_INIT_STRUCT sdcard_init_data; LWSEM_STRUCT lwsem; /* Create and post card detection semaphore */ if( _lwsem_create(&lwsem, 0) != MQX_OK ){ printf("\nlwsem create failed!"); _task_block(); } if( _lwsem_post(&lwsem) != MQX_OK ){ printf("\nlwsem post failed!"); _task_block(); } #ifdef BOARD_SDHC_GPIO_INSTANCE configure_gpio_pins(GPIO_EXTRACT_PORT(BOARD_SDHC_GPIO_INSTANCE)); #endif /* initialize SDHC pins */ configure_sdhc_pins(BOARD_SDHC_INSTANCE); /* install SDHC low-level driver */ _nio_dev_install("esdhc:", &nio_esdhc_dev_fn, (void*)&_bsp_esdhc_init, NULL); /* get an instance of the SDHC driver */ esdhc_handle = open("esdhc:", 0); if(esdhc_handle < 0) { printf("\nCould not open esdhc!"); _task_block(); } /* prepare init data structure */ sdcard_init_data.com_dev = esdhc_handle; sdcard_init_data.const_data = (SDCARD_CONST_INIT_STRUCT_PTR)&sdcard_esdhc_init_data; /* install device */ if (_nio_dev_install("sdcard:", &nio_sdcard_dev_fn, (void*)&sdcard_init_data, NULL) == NULL) { /* Number of sectors is returned by ioctl IO_IOCTL_GET_NUM_SECTORS function */ /* If another disc structure is desired, use MFS_FORMAT_DATA structure to */ /* define it and call standart format function instead default_format */ printf("\nError installing memory device"); _task_block(); } /* Endif */ /* install isr for card detection handling and initialize gpio pin */ _int_install_isr(g_portIrqId[GPIO_EXTRACT_PORT(CDET_PIN->pinName)], card_detect_isr, &lwsem); GPIO_DRV_InputPinInit(CDET_PIN); for(;;){ /* Wait for card insertion or removal */ if( _lwsem_wait(&lwsem) != MQX_OK ){ printf("\nlwsem_wait failed!"); _task_block(); } /* Check if card is present */ if(CDET_PIN->config.pullSelect == kPortPullDown) inserted = GPIO_DRV_ReadPinInput(CDET_PIN->pinName); else inserted = !GPIO_DRV_ReadPinInput(CDET_PIN->pinName); if(last != inserted){ last = inserted; /* Card detection switch debounce delay */ _time_delay(100); if(inserted) { /* Open the device which MFS will be installed on */ dev_fd = open("sdcard:", O_RDWR); if ( dev_fd <= 0 ) { printf("\nUnable to open SDCARD device"); _task_block(); } /* Endif */ /* Install partition manager over SD card driver */ error_code = _io_part_mgr_install(dev_fd, partman_name, 0); if (error_code != MFS_NO_ERROR) { printf("Error installing partition manager: %s\n", MFS_Error_text((uint32_t)error_code)); _task_block(); } /* Open partition */ partition_handle = open(partition_name, O_RDWR); if (partition_handle >= 0) { printf("Installing MFS over partition...\n"); /* Validate partition */ error_code = ioctl(partition_handle, IO_IOCTL_VAL_PART, NULL); if (error_code != MFS_NO_ERROR) { printf("Error validating partition: %s\n", MFS_Error_text((uint32_t)error_code)); printf("Not installing MFS.\n"); _task_block(); } /* Install MFS over partition */ error_code = _io_mfs_install(partition_handle, "a:", 0); if (error_code != MFS_NO_ERROR) { printf("Error initializing MFS over partition: %s\n", MFS_Error_text((uint32_t)error_code)); } } else { printf("Installing MFS over SD card driver...\n"); /* Install MFS over SD card driver */ error_code = _io_mfs_install(dev_fd, "a:", (_file_size)0); if (error_code != MFS_NO_ERROR) { printf("Error initializing MFS: %s\n", MFS_Error_text((uint32_t)error_code)); } } /* Open the filesystem and format detect, if format is required */ a_fd = open("a:", O_RDWR); if (0 >= a_fd) { printf("\nError while opening a:\\ (%s)", MFS_Error_text((uint32_t)errno)); _task_block(); } _io_register_file_system(a_fd, "a:"); /* We check if the device is formatted with the ioctl command. */ error_code = ioctl(a_fd, IO_IOCTL_CHECK_FORMATTED, &isFormatted); if (0 > error_code) { printf("\nError while accessing a:\\ (%s)", MFS_Error_text((uint32_t)error_code)); _task_block(); } else{ if(!isFormatted){ printf("\nNOT A DOS DISK! You must format to continue."); } } } else { /* Close the filesystem */ if ((a_fd >= 0) && (MQX_OK != close(a_fd))) { printf("Error closing filesystem.\n"); } a_fd = -1; /* Force uninstall filesystem */ error_code = _nio_dev_uninstall_force("a:", NULL); if (error_code < 0) { printf("Error uninstalling filesystem.\n"); } /* Close partition */ if ((partition_handle >= 0) && (MQX_OK != close(partition_handle))) { printf("Error closing partition.\n"); } partition_handle = -1; /* Uninstall partition manager */ error_code = _nio_dev_uninstall_force(partman_name, NULL); if (error_code < 0) { printf("Error uninstalling partition manager.\n"); } /* Close the SD card device */ if ((dev_fd >= 0) && (MQX_OK != close(dev_fd))) { printf("Error closing SD card device.\n"); } dev_fd = -1; printf ("SD card uninstalled.\n"); } } } } /* Endbody */
/*! * @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 } }